Spawn.cpp 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "Spawn.h"
  17. #include <stdio.h>
  18. #include "../common/timer.h"
  19. #include <time.h>
  20. #include <math.h>
  21. #include "Entity.h"
  22. #include "Widget.h"
  23. #include "Sign.h"
  24. #include "../common/MiscFunctions.h"
  25. #include "../common/Log.h"
  26. #include "Rules/Rules.h"
  27. #include "World.h"
  28. #include "LuaInterface.h"
  29. #include "Zone/SPGrid.h"
  30. #include "Bots/Bot.h"
  31. #include "Zone/raycast_mesh.h"
  32. extern ConfigReader configReader;
  33. extern RuleManager rule_manager;
  34. extern World world;
  35. Spawn::Spawn(){
  36. group_id = 0;
  37. size_offset = 0;
  38. merchant_id = 0;
  39. memset(&appearance, 0, sizeof(AppearanceData));
  40. memset(&basic_info, 0, sizeof(BasicInfoStruct));
  41. appearance.encounter_level =6;
  42. size = 32;
  43. appearance.pos.collision_radius = 32;
  44. id = Spawn::NextID();
  45. oversized_packet = 0xFF;
  46. zone = 0;
  47. spawn_location_id = 0;
  48. spawn_entry_id = 0;
  49. spawn_location_spawns_id = 0;
  50. respawn = 0;
  51. expire_time = 0;
  52. expire_offset = 0;
  53. x_offset = 0;
  54. y_offset = 0;
  55. z_offset = 0;
  56. database_id = 0;
  57. packet_num = 1;
  58. changed = false;
  59. vis_changed = false;
  60. position_changed = false;
  61. send_spawn_changes = true;
  62. info_changed = false;
  63. appearance.pos.Speed1 = 0;
  64. last_attacker = 0;
  65. faction_id = 0;
  66. running_to = 0;
  67. tmp_visual_state = -1;
  68. tmp_action_state = -1;
  69. transporter_id = 0;
  70. invulnerable = false;
  71. spawn_group_list = 0;
  72. MSpawnGroup = 0;
  73. movement_locations = 0;
  74. MMovementLocations = 0;
  75. target = 0;
  76. primary_command_list_id = 0;
  77. secondary_command_list_id = 0;
  78. is_pet = false;
  79. m_followTarget = 0;
  80. following = false;
  81. req_quests_continued_access = false;
  82. req_quests_override = 0;
  83. req_quests_private = false;
  84. m_illusionModel = 0;
  85. Cell_Info.CurrentCell = nullptr;
  86. Cell_Info.CellListIndex = -1;
  87. m_addedToWorldTimestamp = 0;
  88. m_spawnAnim = 0;
  89. m_spawnAnimLeeway = 0;
  90. m_Update.SetName("Spawn::m_Update");
  91. m_requiredHistory.SetName("Spawn::m_requiredHistory");
  92. m_requiredQuests.SetName("Spawn::m_requiredQuests");
  93. last_heading_angle = 0.0;
  94. last_grid_update = 0;
  95. last_location_update = 0.0;
  96. last_movement_update = Timer::GetCurrentTime2();
  97. forceMapCheck = false;
  98. }
  99. Spawn::~Spawn(){
  100. for(int32 i=0;i<primary_command_list.size();i++){
  101. safe_delete(primary_command_list[i]);
  102. }
  103. for(int32 i=0;i<secondary_command_list.size();i++){
  104. safe_delete(secondary_command_list[i]);
  105. }
  106. RemoveSpawnFromGroup();
  107. if (MMovementLocations)
  108. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  109. if(movement_locations){
  110. while(movement_locations->size()){
  111. safe_delete(movement_locations->front());
  112. movement_locations->pop_front();
  113. }
  114. safe_delete(movement_locations);
  115. }
  116. if (MMovementLocations)
  117. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  118. safe_delete(MMovementLocations);
  119. MMovementLoop.lock();
  120. for (int32 i = 0; i < movement_loop.size(); i++)
  121. safe_delete(movement_loop.at(i));
  122. MMovementLoop.unlock();
  123. map<int32, vector<int16>* >::iterator rq_itr;
  124. m_requiredQuests.writelock(__FUNCTION__, __LINE__);
  125. for (rq_itr = required_quests.begin(); rq_itr != required_quests.end(); rq_itr++){
  126. safe_delete(rq_itr->second);
  127. }
  128. m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
  129. }
  130. void Spawn::InitializeHeaderPacketData(Player* player, PacketStruct* header, int16 index) {
  131. header->setDataByName("index", index);
  132. if (GetSpawnAnim() > 0 && Timer::GetCurrentTime2() < (GetAddedToWorldTimestamp() + GetSpawnAnimLeeway())) {
  133. if (header->GetVersion() >= 57080)
  134. header->setDataByName("spawn_anim", GetSpawnAnim());
  135. else
  136. header->setDataByName("spawn_anim", (int16)GetSpawnAnim());
  137. }
  138. else {
  139. if (header->GetVersion() >= 57080)
  140. header->setDataByName("spawn_anim", 0xFFFFFFFF);
  141. else
  142. header->setDataByName("spawn_anim", 0xFFFF);
  143. }
  144. if (primary_command_list.size() > 0){
  145. if (primary_command_list.size() > 1) {
  146. header->setArrayLengthByName("command_list", primary_command_list.size());
  147. for (int32 i = 0; i < primary_command_list.size(); i++) {
  148. header->setArrayDataByName("command_list_name", primary_command_list[i]->name.c_str(), i);
  149. header->setArrayDataByName("command_list_max_distance", primary_command_list[i]->distance, i);
  150. header->setArrayDataByName("command_list_error", primary_command_list[i]->error_text.c_str(), i);
  151. header->setArrayDataByName("command_list_command", primary_command_list[i]->command.c_str(), i);
  152. }
  153. }
  154. header->setMediumStringByName("default_command", primary_command_list[0]->command.c_str());
  155. header->setDataByName("max_distance", primary_command_list[0]->distance);
  156. }
  157. if (spawn_group_list && MSpawnGroup){
  158. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  159. header->setArrayLengthByName("group_size", spawn_group_list->size());
  160. vector<Spawn*>::iterator itr;
  161. int i = 0;
  162. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++, i++){
  163. header->setArrayDataByName("group_spawn_id", player->GetIDWithPlayerSpawn((*itr)), i);
  164. }
  165. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  166. }
  167. header->setDataByName("spawn_id", player->GetIDWithPlayerSpawn(this));
  168. header->setDataByName("crc", id);
  169. header->setDataByName("time_stamp", Timer::GetCurrentTime2());
  170. }
  171. void Spawn::InitializeVisPacketData(Player* player, PacketStruct* vis_packet) {
  172. int16 version = vis_packet->GetVersion();
  173. if (IsPlayer())
  174. appearance.pos.grid_id = 0xFFFFFFFF;
  175. if (appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1){
  176. if (!IsGroundSpawn()){
  177. int8 arrow_color = ARROW_COLOR_WHITE;
  178. sint8 npc_con = player->GetFactions()->GetCon(faction_id);
  179. if (appearance.attackable == 1)
  180. arrow_color = player->GetArrowColor(GetLevel());
  181. vis_packet->setDataByName("arrow_color", arrow_color);
  182. vis_packet->setDataByName("locked_no_loot", appearance.locked_no_loot);
  183. if (player->GetArrowColor(GetLevel()) == ARROW_COLOR_GRAY)
  184. if (npc_con == -4)
  185. npc_con = -3;
  186. vis_packet->setDataByName("npc_con", npc_con);
  187. if (appearance.attackable == 1 && IsNPC() && (player->GetFactions()->GetCon(faction_id) <= -4 || ((NPC*)this)->Brain()->GetHate(player) > 1))
  188. vis_packet->setDataByName("npc_hate", ((NPC*)this)->Brain()->GetHatePercentage(player));
  189. int8 quest_flag = player->CheckQuestFlag(this);
  190. if (version < 1188 && quest_flag >= 16)
  191. quest_flag = 1;
  192. vis_packet->setDataByName("quest_flag",quest_flag);
  193. if( player->CheckQuestsKillUpdate(this, false)){
  194. vis_packet->setDataByName("name_quest_icon", 1);
  195. }
  196. }
  197. }
  198. int8 vis_flags = 0;
  199. if (MeetsSpawnAccessRequirements(player)){
  200. if (appearance.attackable == 1)
  201. vis_flags += 64; //attackable icon
  202. if (appearance.show_level == 1)
  203. vis_flags += 32;
  204. if (appearance.display_name == 1)
  205. vis_flags += 16;
  206. if (IsPlayer() || appearance.targetable == 1)
  207. vis_flags += 4;
  208. if (appearance.show_command_icon == 1)
  209. vis_flags += 2;
  210. }
  211. else{
  212. //Check to see if there's an override value set
  213. if (req_quests_override > 0)
  214. vis_flags = req_quests_override & 0xFF;
  215. }
  216. vis_packet->setDataByName("vis_flags", vis_flags);
  217. if (MeetsSpawnAccessRequirements(player))
  218. vis_packet->setDataByName("hand_flag", appearance.display_hand_icon);
  219. else {
  220. if ((req_quests_override & 256) > 0)
  221. vis_packet->setDataByName("hand_flag", 1);
  222. }
  223. }
  224. void Spawn::InitializeFooterPacketData(Player* player, PacketStruct* footer) {
  225. if (IsWidget()){
  226. Widget* widget = (Widget*)this;
  227. if (widget->GetMultiFloorLift()) {
  228. footer->setDataByName("widget_x", widget->GetX());
  229. footer->setDataByName("widget_y", widget->GetY());
  230. footer->setDataByName("widget_z", widget->GetZ());
  231. }
  232. else {
  233. footer->setDataByName("widget_x", widget->GetWidgetX());
  234. footer->setDataByName("widget_y", widget->GetWidgetY());
  235. footer->setDataByName("widget_z", widget->GetWidgetZ());
  236. }
  237. footer->setDataByName("widget_id", widget->GetWidgetID());
  238. footer->setDataByName("unknown3c", 6);
  239. }
  240. else if (IsSign()){
  241. Sign* sign = (Sign*)this;
  242. footer->setDataByName("widget_id", sign->GetWidgetID());
  243. footer->setDataByName("widget_x", sign->GetWidgetX());
  244. footer->setDataByName("widget_y", sign->GetWidgetY());
  245. footer->setDataByName("widget_z", sign->GetWidgetZ());
  246. footer->setDataByName("unknown2b", 6);
  247. if (sign->GetSignTitle())
  248. footer->setMediumStringByName("title", sign->GetSignTitle());
  249. if (sign->GetSignDescription())
  250. footer->setMediumStringByName("description", sign->GetSignDescription());
  251. footer->setDataByName("sign_distance", sign->GetSignDistance());
  252. footer->setDataByName("show", 1);
  253. }
  254. footer->setMediumStringByName("name", appearance.name);
  255. footer->setMediumStringByName("guild", appearance.sub_title);
  256. footer->setMediumStringByName("prefix", appearance.prefix_title);
  257. footer->setMediumStringByName("suffix", appearance.suffix_title);
  258. footer->setMediumStringByName("last_name", appearance.last_name);
  259. if (appearance.attackable == 0 && GetLevel() > 0)
  260. footer->setDataByName("spawn_type", 1);
  261. else if (appearance.attackable == 0)
  262. footer->setDataByName("spawn_type", 6);
  263. else
  264. footer->setDataByName("spawn_type", 3);
  265. }
  266. EQ2Packet* Spawn::spawn_serialize(Player* player, int16 version){
  267. // If spawn is NPC AND is pet && owner is a player && owner is the player passed to this function && player's char sheet pet id is 0
  268. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer() && player == ((NPC*)this)->GetOwner() && player->GetInfoStruct()->pet_id == 0) {
  269. ((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->pet_id = player->spawn_id;
  270. player->SetCharSheetChanged(true);
  271. }
  272. int16 index;
  273. if (player->player_spawn_index_map.count(this) > 0) {
  274. index = player->player_spawn_index_map[this];
  275. player->player_spawn_map[index] = this;
  276. }
  277. else {
  278. player->spawn_index++;
  279. if(player->spawn_index == 255)
  280. player->spawn_index++; //just so we dont have to worry about overloading
  281. index = player->spawn_index;
  282. player->player_spawn_index_map[this] = index;
  283. player->player_spawn_map[index] = this;
  284. }
  285. // Jabantiz - [Bug] Client Crash on Revive
  286. if (player->player_spawn_reverse_id_map.count(this) == 0) {
  287. int32 spawn_id = ++player->spawn_id;
  288. player->player_spawn_id_map[spawn_id] = this;
  289. player->player_spawn_reverse_id_map[this] = spawn_id;
  290. }
  291. PacketStruct* header = player->GetSpawnHeaderStruct();
  292. header->ResetData();
  293. InitializeHeaderPacketData(player, header, index);
  294. PacketStruct* footer;
  295. if(IsWidget())
  296. footer = player->GetWidgetFooterStruct();
  297. else if(IsSign())
  298. footer = player->GetSignFooterStruct();
  299. else
  300. footer = player->GetSpawnFooterStruct();
  301. footer->ResetData();
  302. InitializeFooterPacketData(player, footer);
  303. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  304. PacketStruct* info_struct = player->GetSpawnInfoStruct();
  305. PacketStruct* pos_struct = player->GetSpawnPosStruct();
  306. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  307. vis_struct->ResetData();
  308. InitializeVisPacketData(player, vis_struct);
  309. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  310. info_struct->ResetData();
  311. InitializeInfoPacketData(player, info_struct);
  312. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  313. pos_struct->ResetData();
  314. InitializePosPacketData(player, pos_struct);
  315. string* vis_data= vis_struct->serializeString();
  316. string* pos_data = pos_struct->serializeString();
  317. string* info_data = info_struct->serializeString();
  318. int16 part2_size = pos_data->length() + vis_data->length() + info_data->length();
  319. uchar* part2 = new uchar[part2_size];
  320. player->AddSpawnPosPacketForXOR(id, (uchar*)pos_data->c_str(), pos_data->length());
  321. player->AddSpawnVisPacketForXOR(id, (uchar*)vis_data->c_str(), vis_data->length());
  322. player->AddSpawnInfoPacketForXOR(id, (uchar*)info_data->c_str(), info_data->length());
  323. uchar* ptr = part2;
  324. memcpy(ptr, pos_data->c_str(), pos_data->length());
  325. ptr += pos_data->length();
  326. memcpy(ptr, vis_data->c_str(), vis_data->length());
  327. ptr += vis_data->length();
  328. memcpy(ptr, info_data->c_str(), info_data->length());
  329. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  330. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  331. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  332. string* part1 = header->serializeString();
  333. string* part3 = footer->serializeString();
  334. uchar tmp[900];
  335. part2_size = Pack(tmp, part2, part2_size, 900, version);
  336. int32 total_size = part1->length() + part2_size + part3->length() + 3;
  337. uchar* final_packet = new uchar[total_size + 4];
  338. ptr = final_packet;
  339. memcpy(ptr, &total_size, sizeof(total_size));
  340. ptr += sizeof(total_size);
  341. memcpy(ptr, &oversized_packet, sizeof(oversized_packet));
  342. ptr += sizeof(oversized_packet);
  343. memcpy(ptr, &opcode, sizeof(opcode));
  344. ptr += sizeof(opcode);
  345. memcpy(ptr, part1->c_str(), part1->length());
  346. ptr += part1->length();
  347. memcpy(ptr, tmp, part2_size);
  348. ptr += part2_size;
  349. memcpy(ptr, part3->c_str(), part3->length());
  350. delete[] part2;
  351. EQ2Packet* ret = new EQ2Packet(OP_ClientCmdMsg, final_packet, total_size + 4);
  352. delete[] final_packet;
  353. return ret;
  354. }
  355. uchar* Spawn::spawn_info_changes(Player* player, int16 version){
  356. int16 index = player->player_spawn_index_map[this];
  357. PacketStruct* packet = player->GetSpawnInfoStruct();
  358. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  359. packet->ResetData();
  360. InitializeInfoPacketData(player, packet);
  361. string* data = packet->serializeString();
  362. int32 size = data->length();
  363. uchar* xor_info_packet = player->GetTempInfoPacketForXOR();
  364. if (!xor_info_packet)
  365. xor_info_packet = player->SetTempInfoPacketForXOR(size);
  366. uchar* orig_packet = player->GetSpawnInfoPacketForXOR(id);
  367. if(orig_packet){
  368. memcpy(xor_info_packet, (uchar*)data->c_str(), size);
  369. Encode(xor_info_packet, orig_packet, size);
  370. }
  371. uchar* tmp = new uchar[size + 10];
  372. size = Pack(tmp, xor_info_packet, size, size, version);
  373. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  374. int32 orig_size = size;
  375. size-=sizeof(int32);
  376. size+=CheckOverLoadSize(index);
  377. info_packet_size = size + CheckOverLoadSize(size);
  378. uchar* tmp2 = new uchar[info_packet_size];
  379. uchar* ptr = tmp2;
  380. ptr += DoOverLoad(size, ptr);
  381. ptr += DoOverLoad(index, ptr);
  382. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  383. delete[] tmp;
  384. return tmp2;
  385. }
  386. uchar* Spawn::spawn_vis_changes(Player* player, int16 version){
  387. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  388. int16 index = player->player_spawn_index_map[this];
  389. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  390. uchar* orig_packet = player->GetSpawnVisPacketForXOR(id);
  391. vis_struct->ResetData();
  392. InitializeVisPacketData(player, vis_struct);
  393. string* data = vis_struct->serializeString();
  394. int32 size = data->length();
  395. uchar* xor_vis_packet = player->GetTempVisPacketForXOR();
  396. if (!xor_vis_packet)
  397. xor_vis_packet = player->SetTempVisPacketForXOR(size);
  398. if(orig_packet){
  399. memcpy(xor_vis_packet, (uchar*)data->c_str(), size);
  400. Encode(xor_vis_packet, orig_packet, size);
  401. }
  402. uchar* tmp = new uchar[size + 10];
  403. size = Pack(tmp, xor_vis_packet, size, size, version);
  404. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  405. int32 orig_size = size;
  406. size-=sizeof(int32);
  407. size+=CheckOverLoadSize(index);
  408. vis_packet_size = size + CheckOverLoadSize(size);
  409. uchar* tmp2 = new uchar[vis_packet_size];
  410. uchar* ptr = tmp2;
  411. ptr += DoOverLoad(size, ptr);
  412. ptr += DoOverLoad(index, ptr);
  413. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  414. delete[] tmp;
  415. return tmp2;
  416. }
  417. uchar* Spawn::spawn_pos_changes(Player* player, int16 version){
  418. int16 index = player->GetIndexForSpawn(this);
  419. PacketStruct* packet = player->GetSpawnPosStruct();
  420. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  421. uchar* orig_packet = player->GetSpawnPosPacketForXOR(id);
  422. packet->ResetData();
  423. InitializePosPacketData(player, packet, true);
  424. string* data = packet->serializeString();
  425. int32 size = data->length();
  426. uchar* xor_pos_packet = player->GetTempPosPacketForXOR();
  427. if (!xor_pos_packet)
  428. xor_pos_packet = player->SetTempPosPacketForXOR(size);
  429. if(orig_packet){
  430. memcpy(xor_pos_packet, (uchar*)data->c_str(), size);
  431. Encode(xor_pos_packet, orig_packet, size);
  432. }
  433. uchar* tmp;
  434. if (IsPlayer())
  435. tmp = new uchar[size + 14];
  436. else
  437. tmp = new uchar[size + 10];
  438. size = Pack(tmp, xor_pos_packet, size, size, version);
  439. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  440. int32 orig_size = size;
  441. // Needed for CoE+ clients
  442. if (version >= 1188)
  443. size += 1;
  444. if(IsPlayer())
  445. size += 4;
  446. size-=sizeof(int32);
  447. size+=CheckOverLoadSize(index);
  448. pos_packet_size = size + CheckOverLoadSize(size);
  449. uchar* tmp2 = new uchar[pos_packet_size];
  450. uchar* ptr = tmp2;
  451. ptr += DoOverLoad(size, ptr);
  452. ptr += DoOverLoad(index, ptr);
  453. // extra byte in coe+ clients, 0 for NPC's 1 for Players
  454. int8 x = 0;
  455. if(IsPlayer()){
  456. if (version >= 1188) {
  457. // set x to 1 and add it to the packet
  458. x = 1;
  459. memcpy(ptr, &x, sizeof(int8));
  460. ptr += sizeof(int8);
  461. }
  462. int32 now = Timer::GetCurrentTime2();
  463. memcpy(ptr, &now, sizeof(int32));
  464. ptr += sizeof(int32);
  465. }
  466. else if (version >= 1188) {
  467. // add x to packet
  468. memcpy(ptr, &x, sizeof(int8));
  469. ptr += sizeof(int8);
  470. }
  471. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  472. delete[] tmp;
  473. return tmp2;
  474. }
  475. EQ2Packet* Spawn::player_position_update_packet(Player* player, int16 version){
  476. if(!player || player->IsPlayer() == false){
  477. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called player_position_update_packet without player!");
  478. return 0;
  479. }
  480. else if(IsPlayer() == false){
  481. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called player_position_update_packet from spawn!");
  482. return 0;
  483. }
  484. static const int8 info_size = 1;
  485. static const int8 vis_size = 1;
  486. m_Update.writelock(__FUNCTION__, __LINE__);
  487. uchar* pos_changes = spawn_pos_changes(player, version);
  488. int32 tmp_pos_packet_size = pos_packet_size;
  489. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  490. int32 size = info_size + tmp_pos_packet_size + vis_size + 11;
  491. static const int8 oversized = 255;
  492. int16 opcode_val = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqUpdateGhostCmd);
  493. uchar* tmp = new uchar[size];
  494. memset(tmp, 0, size);
  495. uchar* ptr = tmp;
  496. size -=4;
  497. memcpy(ptr, &size, sizeof(int32));
  498. size +=4;
  499. ptr += sizeof(int32);
  500. memcpy(ptr, &oversized, sizeof(int8));
  501. ptr += sizeof(int8);
  502. memcpy(ptr, &opcode_val, sizeof(int16));
  503. ptr += sizeof(int16);
  504. ptr += sizeof(int32);
  505. ptr += info_size;
  506. memcpy(ptr, pos_changes, tmp_pos_packet_size);
  507. EQ2Packet* ret_packet = new EQ2Packet(OP_ClientCmdMsg, tmp, size);
  508. delete[] tmp;
  509. delete[] pos_changes;
  510. return ret_packet;
  511. }
  512. EQ2Packet* Spawn::spawn_update_packet(Player* player, int16 version, bool override_changes, bool override_vis_changes){
  513. if(!player || player->IsPlayer() == false){
  514. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called spawn_update_packet without player!");
  515. return 0;
  516. }
  517. else if((IsPlayer() && info_changed == false && vis_changed == false) || (info_changed == false && vis_changed == false && position_changed == false)){
  518. if(!override_changes && !override_vis_changes)
  519. return 0;
  520. }
  521. static const uchar null_byte = 0;
  522. uchar* info_changes = 0;
  523. uchar* pos_changes = 0;
  524. uchar* vis_changes = 0;
  525. static const int8 oversized = 255;
  526. int16 opcode_val = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqUpdateGhostCmd);
  527. int16 tmp_info_packet_size;
  528. int16 tmp_vis_packet_size;
  529. int16 tmp_pos_packet_size;
  530. //We need to lock these variables up to make this thread safe
  531. m_Update.writelock(__FUNCTION__, __LINE__);
  532. //These variables are set in the spawn_info_changes, pos and vis changes functions
  533. info_packet_size = 1;
  534. pos_packet_size = 1;
  535. vis_packet_size = 1;
  536. if (info_changed || override_changes)
  537. info_changes = spawn_info_changes(player, version);
  538. if ((position_changed || override_changes) && IsPlayer() == false)
  539. pos_changes = spawn_pos_changes(player, version);
  540. if (vis_changed || override_changes || override_vis_changes)
  541. vis_changes = spawn_vis_changes(player, version);
  542. tmp_info_packet_size = info_packet_size;
  543. tmp_pos_packet_size = pos_packet_size;
  544. tmp_vis_packet_size = vis_packet_size;
  545. int32 size = info_packet_size + pos_packet_size + vis_packet_size + 11;
  546. uchar* tmp = new uchar[size];
  547. memset(tmp, 0, size);
  548. uchar* ptr = tmp;
  549. size -=4;
  550. memcpy(ptr, &size, sizeof(int32));
  551. size +=4;
  552. ptr += sizeof(int32);
  553. memcpy(ptr, &oversized, sizeof(int8));
  554. ptr += sizeof(int8);
  555. memcpy(ptr, &opcode_val, sizeof(int16));
  556. ptr += sizeof(int16);
  557. if (IsPlayer() == false){ //this isnt sent for player updates, it is sent on position update
  558. //int32 time = Timer::GetCurrentTime2();
  559. packet_num = Timer::GetCurrentTime2();
  560. memcpy(ptr, &packet_num, sizeof(int32));
  561. }
  562. ptr += sizeof(int32);
  563. memcpy(ptr, info_changes ? info_changes : &null_byte, tmp_info_packet_size);
  564. ptr += info_packet_size;
  565. memcpy(ptr, pos_changes ? pos_changes : &null_byte, tmp_pos_packet_size);
  566. ptr += pos_packet_size;
  567. memcpy(ptr, vis_changes ? vis_changes : &null_byte, tmp_vis_packet_size);
  568. EQ2Packet* ret_packet = new EQ2Packet(OP_ClientCmdMsg, tmp, size);
  569. delete[] tmp;
  570. safe_delete_array(info_changes);
  571. safe_delete_array(vis_changes);
  572. safe_delete_array(pos_changes);
  573. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  574. return ret_packet;
  575. }
  576. uchar* Spawn::spawn_info_changes_ex(Player* player, int16 version) {
  577. int16 index = player->player_spawn_index_map[this];
  578. PacketStruct* packet = player->GetSpawnInfoStruct();
  579. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  580. packet->ResetData();
  581. InitializeInfoPacketData(player, packet);
  582. string* data = packet->serializeString();
  583. int32 size = data->length();
  584. uchar* xor_info_packet = player->GetTempInfoPacketForXOR();
  585. if (!xor_info_packet) {
  586. xor_info_packet = player->SetTempInfoPacketForXOR(size);
  587. }
  588. uchar* orig_packet = player->GetSpawnInfoPacketForXOR(id);
  589. if (orig_packet) {
  590. memcpy(xor_info_packet, (uchar*)data->c_str(), size);
  591. Encode(xor_info_packet, orig_packet, size);
  592. }
  593. bool changed = false;
  594. for (int i = 0; i < size; ++i) {
  595. if (xor_info_packet[i]) {
  596. changed = true;
  597. break;
  598. }
  599. }
  600. if (!changed) {
  601. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  602. return nullptr;
  603. }
  604. uchar* tmp = new uchar[size + 10];
  605. size = Pack(tmp, xor_info_packet, size, size, version);
  606. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  607. int32 orig_size = size;
  608. size -= sizeof(int32);
  609. size += CheckOverLoadSize(index);
  610. info_packet_size = size;
  611. uchar* tmp2 = new uchar[size];
  612. uchar* ptr = tmp2;
  613. ptr += DoOverLoad(index, ptr);
  614. memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
  615. delete[] tmp;
  616. return move(tmp2);
  617. }
  618. uchar* Spawn::spawn_vis_changes_ex(Player* player, int16 version) {
  619. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  620. int16 index = player->player_spawn_index_map[this];
  621. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  622. uchar* orig_packet = player->GetSpawnVisPacketForXOR(id);
  623. vis_struct->ResetData();
  624. InitializeVisPacketData(player, vis_struct);
  625. string* data = vis_struct->serializeString();
  626. int32 size = data->length();
  627. uchar* xor_vis_packet = player->GetTempVisPacketForXOR();
  628. if (!xor_vis_packet) {
  629. xor_vis_packet = player->SetTempVisPacketForXOR(size);
  630. }
  631. if (orig_packet) {
  632. memcpy(xor_vis_packet, (uchar*)data->c_str(), size);
  633. Encode(xor_vis_packet, orig_packet, size);
  634. }
  635. bool changed = false;
  636. for (int i = 0; i < size; ++i) {
  637. if (xor_vis_packet[i]) {
  638. changed = true;
  639. break;
  640. }
  641. }
  642. if (!changed) {
  643. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  644. return nullptr;
  645. }
  646. uchar* tmp = new uchar[size + 10];
  647. size = Pack(tmp, xor_vis_packet, size, size, version);
  648. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  649. int32 orig_size = size;
  650. size -= sizeof(int32);
  651. size += CheckOverLoadSize(index);
  652. vis_packet_size = size;
  653. uchar* tmp2 = new uchar[size];
  654. uchar* ptr = tmp2;
  655. ptr += DoOverLoad(index, ptr);
  656. memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
  657. delete[] tmp;
  658. return move(tmp2);
  659. }
  660. uchar* Spawn::spawn_pos_changes_ex(Player* player, int16 version) {
  661. int16 index = player->GetIndexForSpawn(this);
  662. PacketStruct* packet = player->GetSpawnPosStruct();
  663. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  664. uchar* orig_packet = player->GetSpawnPosPacketForXOR(id);
  665. packet->ResetData();
  666. InitializePosPacketData(player, packet);
  667. string* data = packet->serializeString();
  668. int32 size = data->length();
  669. uchar* xor_pos_packet = player->GetTempPosPacketForXOR();
  670. if (!xor_pos_packet) {
  671. xor_pos_packet = player->SetTempPosPacketForXOR(size);
  672. }
  673. if (orig_packet) {
  674. memcpy(xor_pos_packet, (uchar*)data->c_str(), size);
  675. Encode(xor_pos_packet, orig_packet, size);
  676. }
  677. bool changed = false;
  678. for (int i = 0; i < size; ++i) {
  679. if (xor_pos_packet[i]) {
  680. changed = true;
  681. break;
  682. }
  683. }
  684. if (!changed) {
  685. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  686. return nullptr;
  687. }
  688. uchar* tmp = new uchar[size + 10];
  689. size = Pack(tmp, xor_pos_packet, size, size, version);
  690. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  691. int32 orig_size = size;
  692. if (version >= 1188) {
  693. size += 1;
  694. }
  695. if (IsPlayer()) {
  696. size += 4;
  697. }
  698. size -= sizeof(int32);
  699. size += CheckOverLoadSize(index);
  700. pos_packet_size = size;
  701. uchar* tmp2 = new uchar[size];
  702. uchar* ptr = tmp2;
  703. ptr += DoOverLoad(index, ptr);
  704. // extra byte in coe+ clients, 0 for NPC's 1 for Players
  705. int8 x = 0;
  706. if (version >= 1188) {
  707. if (IsPlayer()) {
  708. x = 1;
  709. memcpy(ptr, &x, sizeof(int8));
  710. ptr += sizeof(int8);
  711. int32 now = Timer::GetCurrentTime2();
  712. memcpy(ptr, &now, sizeof(int32));
  713. ptr += sizeof(int32);
  714. }
  715. else {
  716. memcpy(ptr, &x, sizeof(int8));
  717. ptr += sizeof(int8);
  718. }
  719. }
  720. memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
  721. delete[] tmp;
  722. return move(tmp2);
  723. }
  724. EQ2Packet* Spawn::serialize(Player* player, int16 version){
  725. return 0;
  726. }
  727. Spawn* Spawn::GetTarget(){
  728. Spawn* ret = 0;
  729. // only attempt to get a spawn if we had a target stored
  730. if (target != 0)
  731. {
  732. ret = GetZone()->GetSpawnByID(target);
  733. if (!ret)
  734. target = 0;
  735. }
  736. return ret;
  737. }
  738. void Spawn::SetTarget(Spawn* spawn){
  739. SetInfo(&target, spawn ? spawn->GetID() : 0);
  740. }
  741. Spawn* Spawn::GetLastAttacker() {
  742. Spawn* ret = 0;
  743. ret = GetZone()->GetSpawnByID(last_attacker);
  744. if (!ret)
  745. last_attacker = 0;
  746. return ret;
  747. }
  748. void Spawn::SetLastAttacker(Spawn* spawn){
  749. last_attacker = spawn->GetID();
  750. }
  751. void Spawn::SetInvulnerable(bool val){
  752. invulnerable = val;
  753. }
  754. bool Spawn::GetInvulnerable(){
  755. return invulnerable;
  756. }
  757. bool Spawn::TakeDamage(int32 damage){
  758. if(invulnerable)
  759. return false;
  760. if (IsEntity()) {
  761. if (((Entity*)this)->IsMezzed())
  762. ((Entity*)this)->RemoveAllMezSpells();
  763. if (damage == 0)
  764. return true;
  765. }
  766. int32 hp = GetHP();
  767. if(damage >= hp) {
  768. SetHP(0);
  769. if (IsPlayer()) {
  770. ((Player*)this)->InCombat(false);
  771. ((Player*)this)->SetRangeAttack(false);
  772. GetZone()->TriggerCharSheetTimer(); // force char sheet updates now
  773. }
  774. }
  775. else {
  776. SetHP(hp - damage);
  777. // if player flag the char sheet as changed so the ui updates properly
  778. if (IsPlayer())
  779. ((Player*)this)->SetCharSheetChanged(true);
  780. }
  781. return true;
  782. }
  783. void Spawn::TakeDamage(Spawn* attacker, int32 damage){
  784. if (TakeDamage(damage))
  785. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_HEALTHCHANGED, attacker);
  786. SetLastAttacker(attacker);
  787. }
  788. ZoneServer* Spawn::GetZone(){
  789. return zone;
  790. }
  791. void Spawn::SetZone(ZoneServer* in_zone){
  792. zone = in_zone;
  793. }
  794. /*** HIT POINT ***/
  795. void Spawn::SetHP(sint32 new_val, bool setUpdateFlags){
  796. if(new_val == 0){
  797. ClearRunningLocations();
  798. CalculateRunningLocation(true);
  799. }
  800. if(new_val > basic_info.max_hp)
  801. SetInfo(&basic_info.max_hp, new_val, setUpdateFlags);
  802. SetInfo(&basic_info.cur_hp, new_val, setUpdateFlags);
  803. if(/*IsPlayer() &&*/ GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
  804. GetZone()->AddDamagedSpawn(this);
  805. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  806. ((Entity*)this)->UpdateGroupMemberInfo();
  807. if (IsPlayer())
  808. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  809. else
  810. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  811. }
  812. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  813. Player* player = (Player*)((NPC*)this)->GetOwner();
  814. if (player->GetPet() && player->GetCharmedPet()) {
  815. if (this == player->GetPet()) {
  816. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  817. player->SetCharSheetChanged(true);
  818. }
  819. }
  820. else {
  821. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  822. player->SetCharSheetChanged(true);
  823. }
  824. }
  825. }
  826. void Spawn::SetTotalHP(sint32 new_val){
  827. if(basic_info.hp_base == 0)
  828. SetTotalHPBase(new_val);
  829. SetInfo(&basic_info.max_hp, new_val);
  830. if(GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
  831. GetZone()->AddDamagedSpawn(this);
  832. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  833. ((Entity*)this)->UpdateGroupMemberInfo();
  834. if (IsPlayer())
  835. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  836. else
  837. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  838. }
  839. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  840. Player* player = (Player*)((NPC*)this)->GetOwner();
  841. if (player->GetPet() && player->GetCharmedPet()) {
  842. if (this == player->GetPet()) {
  843. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  844. player->SetCharSheetChanged(true);
  845. }
  846. }
  847. else {
  848. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  849. player->SetCharSheetChanged(true);
  850. }
  851. }
  852. }
  853. void Spawn::SetTotalHPBase(sint32 new_val)
  854. {
  855. SetInfo(&basic_info.hp_base, new_val);
  856. if(GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
  857. GetZone()->AddDamagedSpawn(this);
  858. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  859. ((Entity*)this)->UpdateGroupMemberInfo();
  860. if (IsPlayer())
  861. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  862. else
  863. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  864. }
  865. }
  866. sint32 Spawn::GetHP()
  867. {
  868. return basic_info.cur_hp;
  869. }
  870. sint32 Spawn::GetTotalHP()
  871. {
  872. return basic_info.max_hp;
  873. }
  874. sint32 Spawn::GetTotalHPBase()
  875. {
  876. return basic_info.hp_base;
  877. }
  878. /*** POWER ***/
  879. void Spawn::SetPower(sint32 power, bool setUpdateFlags){
  880. if(power > basic_info.max_power)
  881. SetInfo(&basic_info.max_power, power, setUpdateFlags);
  882. SetInfo(&basic_info.cur_power, power, setUpdateFlags);
  883. if(/*IsPlayer() &&*/ GetZone() && basic_info.cur_power < basic_info.max_power)
  884. GetZone()->AddDamagedSpawn(this);
  885. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  886. ((Entity*)this)->UpdateGroupMemberInfo();
  887. if (IsPlayer())
  888. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  889. else
  890. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  891. }
  892. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  893. Player* player = (Player*)((NPC*)this)->GetOwner();
  894. if (player->GetPet() && player->GetCharmedPet()) {
  895. if (this == player->GetPet()) {
  896. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  897. player->SetCharSheetChanged(true);
  898. }
  899. }
  900. else {
  901. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  902. player->SetCharSheetChanged(true);
  903. }
  904. }
  905. }
  906. void Spawn::SetTotalPower(sint32 new_val)
  907. {
  908. if(basic_info.power_base == 0)
  909. SetTotalPowerBase(new_val);
  910. SetInfo(&basic_info.max_power, new_val);
  911. if(GetZone() && basic_info.cur_power < basic_info.max_power)
  912. GetZone()->AddDamagedSpawn(this);
  913. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  914. ((Entity*)this)->UpdateGroupMemberInfo();
  915. if (IsPlayer())
  916. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  917. else
  918. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  919. }
  920. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  921. Player* player = (Player*)((NPC*)this)->GetOwner();
  922. if (player->GetPet() && player->GetCharmedPet()) {
  923. if (this == player->GetPet()) {
  924. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  925. player->SetCharSheetChanged(true);
  926. }
  927. }
  928. else {
  929. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  930. player->SetCharSheetChanged(true);
  931. }
  932. }
  933. }
  934. void Spawn::SetTotalPowerBase(sint32 new_val)
  935. {
  936. SetInfo(&basic_info.power_base, new_val);
  937. if(GetZone() && basic_info.cur_power < basic_info.max_power)
  938. GetZone()->AddDamagedSpawn(this);
  939. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  940. ((Entity*)this)->UpdateGroupMemberInfo();
  941. if (IsPlayer())
  942. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  943. else
  944. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  945. }
  946. }
  947. sint32 Spawn::GetPower()
  948. {
  949. return basic_info.cur_power;
  950. }
  951. sint32 Spawn::GetTotalPower(){
  952. return basic_info.max_power;
  953. }
  954. sint32 Spawn::GetTotalPowerBase()
  955. {
  956. return basic_info.power_base;
  957. }
  958. /*** SAVAGERY ***/
  959. void Spawn::SetSavagery(sint32 savagery, bool setUpdateFlags)
  960. {
  961. /* JA: extremely limited functionality until we better understand Savagery */
  962. if(savagery > basic_info.max_savagery)
  963. SetInfo(&basic_info.max_savagery, savagery, setUpdateFlags);
  964. SetInfo(&basic_info.cur_savagery, savagery, setUpdateFlags);
  965. }
  966. void Spawn::SetTotalSavagery(sint32 new_val)
  967. {
  968. /* JA: extremely limited functionality until we better understand Savagery */
  969. if(basic_info.savagery_base == 0)
  970. SetTotalSavageryBase(new_val);
  971. SetInfo(&basic_info.max_savagery, new_val);
  972. }
  973. void Spawn::SetTotalSavageryBase(sint32 new_val){
  974. SetInfo(&basic_info.savagery_base, new_val);
  975. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  976. ((Entity*)this)->UpdateGroupMemberInfo();
  977. if (IsPlayer())
  978. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  979. else
  980. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  981. }
  982. }
  983. sint32 Spawn::GetTotalSavagery()
  984. {
  985. return basic_info.max_savagery;
  986. }
  987. sint32 Spawn::GetSavagery()
  988. {
  989. return basic_info.cur_savagery;
  990. }
  991. /*** DISSONANCE ***/
  992. void Spawn::SetDissonance(sint32 dissonance, bool setUpdateFlags)
  993. {
  994. /* JA: extremely limited functionality until we better understand Dissonance */
  995. if(dissonance > basic_info.max_dissonance)
  996. SetInfo(&basic_info.max_dissonance, dissonance, setUpdateFlags);
  997. SetInfo(&basic_info.cur_dissonance, dissonance, setUpdateFlags);
  998. }
  999. void Spawn::SetTotalDissonance(sint32 new_val)
  1000. {
  1001. /* JA: extremely limited functionality until we better understand Dissonance */
  1002. if(basic_info.dissonance_base == 0)
  1003. SetTotalDissonanceBase(new_val);
  1004. SetInfo(&basic_info.max_dissonance, new_val);
  1005. }
  1006. void Spawn::SetTotalDissonanceBase(sint32 new_val)
  1007. {
  1008. SetInfo(&basic_info.dissonance_base, new_val);
  1009. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  1010. ((Entity*)this)->UpdateGroupMemberInfo();
  1011. if (IsPlayer())
  1012. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  1013. else
  1014. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  1015. }
  1016. }
  1017. sint32 Spawn::GetTotalDissonance()
  1018. {
  1019. return basic_info.max_dissonance;
  1020. }
  1021. sint32 Spawn::GetDissonance()
  1022. {
  1023. return basic_info.cur_dissonance;
  1024. }
  1025. /* --< Alternate Advancement Points >-- */
  1026. void Spawn::SetAssignedAA(sint16 new_val)
  1027. {
  1028. SetInfo(&basic_info.assigned_aa, new_val);
  1029. }
  1030. void Spawn::SetUnassignedAA(sint16 new_val)
  1031. {
  1032. SetInfo(&basic_info.unassigned_aa, new_val);
  1033. }
  1034. void Spawn::SetTradeskillAA(sint16 new_val)
  1035. {
  1036. SetInfo(&basic_info.tradeskill_aa, new_val);
  1037. }
  1038. void Spawn::SetUnassignedTradeskillAA(sint16 new_val)
  1039. {
  1040. SetInfo(&basic_info.unassigned_tradeskill_aa, new_val);
  1041. }
  1042. void Spawn::SetPrestigeAA(sint16 new_val)
  1043. {
  1044. SetInfo(&basic_info.prestige_aa, new_val);
  1045. }
  1046. void Spawn::SetUnassignedPrestigeAA(sint16 new_val)
  1047. {
  1048. SetInfo(&basic_info.unassigned_prestige_aa, new_val);
  1049. }
  1050. void Spawn::SetTradeskillPrestigeAA(sint16 new_val)
  1051. {
  1052. SetInfo(&basic_info.tradeskill_prestige_aa, new_val);
  1053. }
  1054. void Spawn::SetUnassignedTradeskillPrestigeAA(sint16 new_val)
  1055. {
  1056. SetInfo(&basic_info.unassigned_tradeskill_prestige_aa, new_val);
  1057. }
  1058. sint16 Spawn::GetAssignedAA()
  1059. {
  1060. return basic_info.assigned_aa;
  1061. }
  1062. sint16 Spawn::GetUnassignedAA()
  1063. {
  1064. return basic_info.unassigned_aa;
  1065. }
  1066. sint16 Spawn::GetTradeskillAA()
  1067. {
  1068. return basic_info.tradeskill_aa;
  1069. }
  1070. sint16 Spawn::GetUnassignedTradeskillAA()
  1071. {
  1072. return basic_info.unassigned_tradeskill_aa;
  1073. }
  1074. sint16 Spawn::GetPrestigeAA()
  1075. {
  1076. return basic_info.prestige_aa;
  1077. }
  1078. sint16 Spawn::GetUnassignedPretigeAA()
  1079. {
  1080. return basic_info.unassigned_prestige_aa;
  1081. }
  1082. sint16 Spawn::GetTradeskillPrestigeAA()
  1083. {
  1084. return basic_info.tradeskill_prestige_aa;
  1085. }
  1086. sint16 Spawn::GetUnassignedTradeskillPrestigeAA()
  1087. {
  1088. return basic_info.unassigned_tradeskill_prestige_aa;
  1089. }
  1090. float Spawn::GetDistance(float x1, float y1, float z1, float x2, float y2, float z2){
  1091. x1 = x1 - x2;
  1092. y1 = y1 - y2;
  1093. z1 = z1 - z2;
  1094. return sqrt(x1*x1 + y1*y1 + z1*z1);
  1095. }
  1096. float Spawn::GetDistance(float x, float y, float z, float radius, bool ignore_y) {
  1097. if (ignore_y)
  1098. return GetDistance(x, y, z, GetX(), y, GetZ()) - radius;
  1099. else
  1100. return GetDistance(x, y, z, GetX(), GetY(), GetZ()) - radius;
  1101. }
  1102. float Spawn::GetDistance(float x, float y, float z, bool ignore_y) {
  1103. return GetDistance(x, y, z, 0.0f, ignore_y);
  1104. }
  1105. float Spawn::GetDistance(Spawn* spawn, bool ignore_y, bool includeRadius){
  1106. float ret = 0;
  1107. if (spawn)
  1108. {
  1109. float radius = 0.0f;
  1110. if (includeRadius)
  1111. radius = CalculateRadius(spawn);
  1112. ret = GetDistance(spawn->GetX(), spawn->GetY(), spawn->GetZ(), radius, ignore_y);
  1113. }
  1114. // maybe distance against ourselves, in that case we want to nullify the radius check
  1115. if (ret < 0)
  1116. ret = 0.0f;
  1117. return ret;
  1118. }
  1119. float Spawn::GetDistance(Spawn* spawn, float x1, float y1, float z1, bool includeRadius) {
  1120. float ret = 0;
  1121. if (spawn)
  1122. {
  1123. float radius = 0.0f;
  1124. if (includeRadius)
  1125. radius = CalculateRadius(spawn);
  1126. ret = GetDistance(x1, y1, z1, spawn->GetX(), spawn->GetY(), spawn->GetZ()) - radius;
  1127. }
  1128. // maybe distance against ourselves, in that case we want to nullify the radius check
  1129. if (ret < 0)
  1130. ret = 0.0f;
  1131. return ret;
  1132. }
  1133. float Spawn::CalculateRadius(Spawn* target)
  1134. {
  1135. float srcRadius = short_to_float(appearance.pos.collision_radius);
  1136. if (target)
  1137. {
  1138. float targRadius = short_to_float(target->appearance.pos.collision_radius);
  1139. return (targRadius / 32.0f) + (srcRadius / 32.0f);
  1140. }
  1141. else
  1142. return (srcRadius / 32.0f);
  1143. }
  1144. int32 Spawn::GetRespawnTime(){
  1145. return respawn;
  1146. }
  1147. void Spawn::SetRespawnTime(int32 time){
  1148. respawn = time;
  1149. }
  1150. int32 Spawn::GetExpireOffsetTime(){
  1151. return expire_offset;
  1152. }
  1153. void Spawn::SetExpireOffsetTime(int32 time){
  1154. expire_offset = time;
  1155. }
  1156. int32 Spawn::GetSpawnLocationID(){
  1157. return spawn_location_id;
  1158. }
  1159. void Spawn::SetSpawnLocationID(int32 id){
  1160. spawn_location_id = id;
  1161. }
  1162. int32 Spawn::GetSpawnEntryID(){
  1163. return spawn_entry_id;
  1164. }
  1165. void Spawn::SetSpawnEntryID(int32 id){
  1166. spawn_entry_id = id;
  1167. }
  1168. int32 Spawn::GetSpawnLocationPlacementID(){
  1169. return spawn_location_spawns_id;
  1170. }
  1171. void Spawn::SetSpawnLocationPlacementID(int32 id){
  1172. spawn_location_spawns_id = id;
  1173. }
  1174. const char* Spawn::GetSpawnScript(){
  1175. if(spawn_script.length() > 0)
  1176. return spawn_script.c_str();
  1177. else
  1178. return 0;
  1179. }
  1180. void Spawn::SetSpawnScript(string name){
  1181. spawn_script = name;
  1182. }
  1183. void Spawn::SetPrimaryCommand(const char* name, const char* command, float distance){
  1184. EntityCommand* entity_command = CreateEntityCommand(name, distance, command, "", 0, 0);
  1185. if(primary_command_list.size() > 0 && primary_command_list[0]){
  1186. safe_delete(primary_command_list[0]);
  1187. primary_command_list[0] = entity_command;
  1188. }
  1189. else
  1190. primary_command_list.push_back(entity_command);
  1191. }
  1192. void Spawn::SetSecondaryCommands(vector<EntityCommand*>* commands){
  1193. if(commands && commands->size() > 0){
  1194. vector<EntityCommand*>::iterator itr;
  1195. if(secondary_command_list.size() > 0){
  1196. for(itr = secondary_command_list.begin(); itr != secondary_command_list.end(); itr++){
  1197. safe_delete(*itr);
  1198. }
  1199. secondary_command_list.clear();
  1200. }
  1201. EntityCommand* command = 0;
  1202. for(itr = commands->begin(); itr != commands->end(); itr++){
  1203. command = CreateEntityCommand(*itr);
  1204. secondary_command_list.push_back(command);
  1205. }
  1206. }
  1207. }
  1208. void Spawn::SetPrimaryCommands(vector<EntityCommand*>* commands){
  1209. if(commands && commands->size() > 0){
  1210. vector<EntityCommand*>::iterator itr;
  1211. if(primary_command_list.size() > 0){
  1212. for(itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++){
  1213. safe_delete(*itr);
  1214. }
  1215. primary_command_list.clear();
  1216. }
  1217. EntityCommand* command = 0;
  1218. for(itr = commands->begin(); itr != commands->end(); itr++){
  1219. command = CreateEntityCommand(*itr);
  1220. primary_command_list.push_back(command);
  1221. }
  1222. }
  1223. }
  1224. EntityCommand* Spawn::FindEntityCommand(string command) {
  1225. EntityCommand* entity_command = 0;
  1226. if (primary_command_list.size() > 0) {
  1227. vector<EntityCommand*>::iterator itr;
  1228. for (itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++) {
  1229. if ((*itr)->command == command) {
  1230. entity_command = *itr;
  1231. break;
  1232. }
  1233. }
  1234. }
  1235. if (!entity_command && secondary_command_list.size() > 0) {
  1236. vector<EntityCommand*>::iterator itr;
  1237. for (itr = secondary_command_list.begin(); itr != secondary_command_list.end(); itr++) {
  1238. if ((*itr)->command == command) {
  1239. entity_command = *itr;
  1240. break;
  1241. }
  1242. }
  1243. }
  1244. return entity_command;
  1245. }
  1246. void Spawn::SetSizeOffset(int8 offset){
  1247. size_offset = offset;
  1248. }
  1249. int8 Spawn::GetSizeOffset(){
  1250. return size_offset;
  1251. }
  1252. void Spawn::SetMerchantID(int32 val){
  1253. merchant_id = val;
  1254. }
  1255. int32 Spawn::GetMerchantID(){
  1256. return merchant_id;
  1257. }
  1258. void Spawn::SetMerchantType(int8 val){
  1259. merchant_type = val;
  1260. }
  1261. int8 Spawn::GetMerchantType(){
  1262. return merchant_type;
  1263. }
  1264. void Spawn::SetQuestsRequired(map<int32, vector<int16>* >* quests){
  1265. if(quests){
  1266. map<int32, vector<int16>* >::iterator itr;
  1267. for(itr = quests->begin(); itr != quests->end(); itr++){
  1268. vector<int16>* quest_steps = itr->second;
  1269. for (int32 i = 0; i < quest_steps->size(); i++)
  1270. SetQuestsRequired(itr->first, quest_steps->at(i));
  1271. }
  1272. }
  1273. }
  1274. void Spawn::SetQuestsRequired(int32 quest_id, int16 quest_step){
  1275. m_requiredQuests.writelock(__FUNCTION__, __LINE__);
  1276. if (required_quests.count(quest_id) == 0)
  1277. required_quests[quest_id] = new vector<int16>;
  1278. else{
  1279. for (int32 i = 0; i < required_quests[quest_id]->size(); i++){
  1280. if (required_quests[quest_id]->at(i) == quest_step){
  1281. m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
  1282. return;
  1283. }
  1284. }
  1285. }
  1286. required_quests[quest_id]->push_back(quest_step);
  1287. m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
  1288. }
  1289. void Spawn::SetRequiredHistory(int32 event_id, int32 value1, int32 value2){
  1290. LUAHistory set_value;
  1291. set_value.Value = value1;
  1292. set_value.Value2 = value2;
  1293. set_value.SaveNeeded = false;
  1294. m_requiredHistory.writelock(__FUNCTION__, __LINE__);
  1295. required_history[event_id] = set_value;
  1296. m_requiredHistory.releasewritelock(__FUNCTION__, __LINE__);
  1297. }
  1298. map<int32, vector<int16>* >* Spawn::GetQuestsRequired(){
  1299. return &required_quests;
  1300. }
  1301. void Spawn::SetTransporterID(int32 id){
  1302. transporter_id = id;
  1303. }
  1304. int32 Spawn::GetTransporterID(){
  1305. return transporter_id;
  1306. }
  1307. void Spawn::InitializePosPacketData(Player* player, PacketStruct* packet, bool bSpawnUpdate) {
  1308. int16 version = packet->GetVersion();
  1309. packet->setDataByName("pos_grid_id", appearance.pos.grid_id);
  1310. bool include_heading = true;
  1311. if (IsWidget() && ((Widget*)this)->GetIncludeHeading() == false)
  1312. include_heading = false;
  1313. else if (IsSign() && ((Sign*)this)->GetIncludeHeading() == false)
  1314. include_heading = false;
  1315. else if (IsGroundSpawn())
  1316. include_heading = false;
  1317. if (include_heading){
  1318. packet->setDataByName("pos_heading1", appearance.pos.Dir1);
  1319. packet->setDataByName("pos_heading2", appearance.pos.Dir2);
  1320. }
  1321. packet->setDataByName("pos_collision_radius", appearance.pos.collision_radius > 0 ? appearance.pos.collision_radius : 32);
  1322. if (version <= 910) {
  1323. packet->setDataByName("pos_size", size > 0 ? size : 32);
  1324. packet->setDataByName("pos_size_multiplier", 32); //32 is normal
  1325. }
  1326. else {
  1327. if (IsPlayer()) {
  1328. if (this != player)
  1329. packet->setDataByName("pos_size", 49152);
  1330. packet->setDataByName("pos_size_ratio", 1);
  1331. packet->setDataByName("pos_size_multiplier_ratio", 1); // used for growth with players
  1332. }
  1333. else {
  1334. packet->setDataByName("pos_size_ratio", size > 0 ? (((float)size) / 32) : 1);
  1335. packet->setDataByName("pos_size_multiplier_ratio", 1);
  1336. }
  1337. }
  1338. packet->setDataByName("pos_state", appearance.pos.state);
  1339. bool include_location = true;
  1340. if (IsWidget() && ((Widget*)this)->GetIncludeLocation() == false)
  1341. include_location = false;
  1342. else if (IsSign() && ((Sign*)this)->GetIncludeLocation() == false)
  1343. include_location = false;
  1344. if (include_location){
  1345. if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
  1346. Widget* widget = (Widget*)this;
  1347. float x = appearance.pos.X - widget->GetWidgetX();
  1348. float y = appearance.pos.Y - widget->GetWidgetY();
  1349. float z = appearance.pos.Z - widget->GetWidgetZ();
  1350. packet->setDataByName("pos_x", x);
  1351. packet->setDataByName("pos_y", y);
  1352. packet->setDataByName("pos_z", z);
  1353. }
  1354. else {
  1355. packet->setDataByName("pos_x", appearance.pos.X);
  1356. packet->setDataByName("pos_y", appearance.pos.Y);
  1357. packet->setDataByName("pos_z", appearance.pos.Z);
  1358. }
  1359. if (IsSign())
  1360. packet->setDataByName("pos_unknown6", 3, 2);
  1361. }
  1362. if (IsPlayer()) {
  1363. packet->setDataByName("pos_x_velocity", static_cast<sint16>(GetSpeedX() * 32));
  1364. packet->setDataByName("pos_y_velocity", static_cast<sint16>(GetSpeedY() * 32));
  1365. packet->setDataByName("pos_z_velocity", static_cast<sint16>(GetSpeedZ() * 32));
  1366. }
  1367. bool bSendSpeed = true;
  1368. if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
  1369. Widget* widget = (Widget*)this;
  1370. float x;
  1371. float y;
  1372. float z;
  1373. if (IsRunning()){
  1374. x = appearance.pos.X2 - widget->GetWidgetX();
  1375. y = appearance.pos.Y2 - widget->GetWidgetY();
  1376. z = appearance.pos.Z2- widget->GetWidgetZ();
  1377. }
  1378. else {
  1379. x = appearance.pos.X - widget->GetWidgetX();
  1380. y = appearance.pos.Y - widget->GetWidgetY();
  1381. z = appearance.pos.Z - widget->GetWidgetZ();
  1382. }
  1383. packet->setDataByName("pos_next_x", x);
  1384. packet->setDataByName("pos_next_y", y);
  1385. packet->setDataByName("pos_next_z", z);
  1386. packet->setDataByName("pos_x3", x);
  1387. packet->setDataByName("pos_y3", y);
  1388. packet->setDataByName("pos_z3", z);
  1389. }
  1390. //If this is a spawn update or this spawn is currently moving we can send these values, otherwise set speed and next_xyz to 0
  1391. //This fixes the bug where spawns with movement scripts face south when initially spawning if they are at their target location.
  1392. else if (bSpawnUpdate || memcmp(&appearance.pos.X, &appearance.pos.X2, sizeof(float) * 3) != 0) {
  1393. packet->setDataByName("pos_next_x", appearance.pos.X2);
  1394. packet->setDataByName("pos_next_y", appearance.pos.Y2);
  1395. packet->setDataByName("pos_next_z", appearance.pos.Z2);
  1396. packet->setDataByName("pos_x3", appearance.pos.X3);
  1397. packet->setDataByName("pos_y3", appearance.pos.Y3);
  1398. packet->setDataByName("pos_z3", appearance.pos.Z3);
  1399. }
  1400. else
  1401. {
  1402. bSendSpeed = false;
  1403. }
  1404. //packet->setDataByName("pos_unknown2", 4, 2);
  1405. int16 speed_multiplier = rule_manager.GetGlobalRule(R_Spawn, SpeedMultiplier)->GetInt16(); // was 1280, 600 and now 300... investigating why
  1406. if (IsPlayer()) {
  1407. Player* player = static_cast<Player*>(this);
  1408. packet->setDataByName("pos_speed", player->GetPosPacketSpeed() * speed_multiplier);
  1409. packet->setDataByName("pos_side_speed", player->GetSideSpeed() * speed_multiplier);
  1410. }
  1411. else if (bSendSpeed) {
  1412. packet->setDataByName("pos_speed", GetSpeed() * speed_multiplier);
  1413. }
  1414. if (IsNPC() || IsPlayer()) {
  1415. packet->setDataByName("pos_move_type", 25);
  1416. }
  1417. else if (IsWidget() || IsSign()) {
  1418. packet->setDataByName("pos_move_type", 11);
  1419. }
  1420. else if(IsGroundSpawn()) {
  1421. packet->setDataByName("pos_move_type", 16);
  1422. }
  1423. if (!IsPlayer())
  1424. packet->setDataByName("pos_movement_mode", 2);
  1425. if(version <= 910)
  1426. packet->setDataByName("pos_unknown10", 0xFFFF, 1);
  1427. else
  1428. packet->setDataByName("pos_unknown10", 0xFFFF);
  1429. if(version <= 910)
  1430. packet->setDataByName("pos_unknown10", 0xFFFF, 2);
  1431. else
  1432. packet->setDataByName("pos_unknown10", 0XFFFF, 1);
  1433. packet->setDataByName("pos_pitch1", appearance.pos.Pitch1);
  1434. packet->setDataByName("pos_pitch2", appearance.pos.Pitch2);
  1435. packet->setDataByName("pos_roll", appearance.pos.Roll);
  1436. }
  1437. void Spawn::InitializeInfoPacketData(Player* spawn, PacketStruct* packet){
  1438. int16 version = packet->GetVersion();
  1439. bool spawnHiddenFromClient = false;
  1440. // radius of 0 is always seen, -1 is never seen (unless items/spells override), larger than 0 is a defined radius to restrict visibility
  1441. sint32 radius = rule_manager.GetGlobalRule(R_PVP, InvisPlayerDiscoveryRange)->GetSInt32();
  1442. if (radius != 0 && (Spawn*)spawn != this && this->IsPlayer() && !spawn->CanSeeInvis((Entity*)this))
  1443. spawnHiddenFromClient = true;
  1444. if(!spawnHiddenFromClient && (appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1)){
  1445. appearance.locked_no_loot = 1; //for now
  1446. if(!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign()){
  1447. int8 percent = 0;
  1448. if(GetHP() > 0)
  1449. percent = (int8)(((float)GetHP()/GetTotalHP()) * 100);
  1450. if(percent < 100){
  1451. packet->setDataByName("hp_remaining", 100 ^ percent);
  1452. }
  1453. else
  1454. packet->setDataByName("hp_remaining", 0);
  1455. if(GetTotalPower() > 0){
  1456. percent = (int8)(((float)GetPower()/GetTotalPower()) * 100);
  1457. if(percent > 0)
  1458. packet->setDataByName("power_percent", percent);
  1459. else
  1460. packet->setDataByName("power_percent", 0);
  1461. }
  1462. }
  1463. }
  1464. packet->setDataByName("level", (int8)GetLevel());
  1465. packet->setDataByName("unknown4", (int8)GetLevel());
  1466. packet->setDataByName("difficulty", appearance.encounter_level); //6);
  1467. packet->setDataByName("heroic_flag", appearance.heroic_flag);
  1468. if(!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign())
  1469. packet->setDataByName("interaction_flag", 12); //this makes NPCs head turn to look at you
  1470. if (version >= 1188 && (IsPlayer() || IsBot()))
  1471. packet->setDataByName("spawn_type", 0);
  1472. else
  1473. packet->setDataByName("spawn_type", spawn_type);
  1474. packet->setDataByName("class", appearance.adventure_class);
  1475. int16 model_type = appearance.model_type;
  1476. if (GetIllusionModel() != 0) {
  1477. if (IsPlayer()) {
  1478. if (((Player*)this)->get_character_flag(CF_SHOW_ILLUSION)) {
  1479. model_type = GetIllusionModel();
  1480. }
  1481. }
  1482. else
  1483. model_type = GetIllusionModel();
  1484. }
  1485. int16 sogaModelType = appearance.soga_model_type;
  1486. if (spawnHiddenFromClient)
  1487. {
  1488. model_type = 0;
  1489. sogaModelType = 0;
  1490. }
  1491. packet->setDataByName("model_type", model_type);
  1492. if(appearance.soga_model_type == 0)
  1493. packet->setDataByName("soga_model_type", model_type);
  1494. else
  1495. packet->setDataByName("soga_model_type", sogaModelType);
  1496. if(GetTempActionState() >= 0)
  1497. packet->setDataByName("action_state", GetTempActionState());
  1498. else
  1499. packet->setDataByName("action_state", appearance.action_state);
  1500. if(GetTempVisualState() >= 0)
  1501. packet->setDataByName("visual_state", GetTempVisualState());
  1502. else
  1503. packet->setDataByName("visual_state", appearance.visual_state);
  1504. packet->setDataByName("emote_state", appearance.emote_state);
  1505. packet->setDataByName("mood_state", appearance.mood_state);
  1506. packet->setDataByName("gender", appearance.gender);
  1507. packet->setDataByName("race", appearance.race);
  1508. packet->setDataByName("gender", appearance.gender);
  1509. if(IsEntity()){
  1510. Entity* entity = ((Entity*)this);
  1511. packet->setDataByName("combat_voice", entity->GetCombatVoice());
  1512. packet->setDataByName("emote_voice", entity->GetEmoteVoice());
  1513. for(int i=0;i<25;i++){
  1514. if(i == 2){ //don't send helm if hidden flag
  1515. if(IsPlayer()){
  1516. if(((Player*)this)->get_character_flag(CF_HIDE_HELM)){
  1517. packet->setDataByName("equipment_types", 0, i);
  1518. packet->setColorByName("equipment_colors", 0, i);
  1519. packet->setColorByName("equipment_highlights", 0, i);
  1520. continue;
  1521. }
  1522. }
  1523. if (IsBot()) {
  1524. if (!((Bot*)this)->ShowHelm) {
  1525. packet->setDataByName("equipment_types", 0, i);
  1526. packet->setColorByName("equipment_colors", 0, i);
  1527. packet->setColorByName("equipment_highlights", 0, i);
  1528. continue;
  1529. }
  1530. }
  1531. }
  1532. else if(i == 19){ //don't send cloak if hidden
  1533. if(IsPlayer()){
  1534. if(!((Player*)this)->get_character_flag(CF_SHOW_CLOAK)){
  1535. packet->setDataByName("equipment_types", 0, i);
  1536. packet->setColorByName("equipment_colors", 0, i);
  1537. packet->setColorByName("equipment_highlights", 0, i);
  1538. continue;
  1539. }
  1540. }
  1541. if (IsBot()) {
  1542. if (!((Bot*)this)->ShowCloak) {
  1543. packet->setDataByName("equipment_types", 0, i);
  1544. packet->setColorByName("equipment_colors", 0, i);
  1545. packet->setColorByName("equipment_highlights", 0, i);
  1546. continue;
  1547. }
  1548. }
  1549. }
  1550. packet->setDataByName("equipment_types", entity->equipment.equip_id[i], i);
  1551. packet->setColorByName("equipment_colors", entity->equipment.color[i], i);
  1552. packet->setColorByName("equipment_highlights", entity->equipment.highlight[i], i);
  1553. }
  1554. packet->setDataByName("mount_type", entity->GetMount());
  1555. // find the visual flags
  1556. int8 vis_flag = 0;
  1557. //Invis + crouch flag check
  1558. if (entity->IsStealthed())
  1559. vis_flag += (INFO_VIS_FLAG_INVIS + INFO_VIS_FLAG_CROUCH);
  1560. //Invis flag check
  1561. else if (entity->IsInvis())
  1562. vis_flag += INFO_VIS_FLAG_INVIS;
  1563. //Mount flag check
  1564. if (entity->GetMount() > 0)
  1565. vis_flag += INFO_VIS_FLAG_MOUNTED;
  1566. //Hide hood check
  1567. if ((IsPlayer() && ((Player*)this)->get_character_flag(CF_HIDE_HOOD)) || appearance.hide_hood)
  1568. vis_flag += INFO_VIS_FLAG_HIDE_HOOD;
  1569. packet->setDataByName("visual_flag", vis_flag);
  1570. packet->setColorByName("mount_saddle_color", entity->GetMountSaddleColor());
  1571. packet->setColorByName("mount_color", entity->GetMountColor());
  1572. packet->setDataByName("hair_type_id", entity->features.hair_type);
  1573. packet->setDataByName("chest_type_id", entity->features.chest_type);
  1574. packet->setDataByName("wing_type_id", entity->features.wing_type);
  1575. packet->setDataByName("legs_type_id", entity->features.legs_type);
  1576. packet->setDataByName("soga_hair_type_id", entity->features.soga_hair_type);
  1577. packet->setDataByName("facial_hair_type_id", entity->features.hair_face_type);
  1578. packet->setDataByName("soga_facial_hair_type_id", entity->features.soga_hair_face_type);
  1579. for(int i=0;i<3;i++){
  1580. packet->setDataByName("eye_type", entity->features.eye_type[i], i);
  1581. packet->setDataByName("ear_type", entity->features.ear_type[i], i);
  1582. packet->setDataByName("eye_brow_type", entity->features.eye_brow_type[i], i);
  1583. packet->setDataByName("cheek_type", entity->features.cheek_type[i], i);
  1584. packet->setDataByName("lip_type", entity->features.lip_type[i], i);
  1585. packet->setDataByName("chin_type", entity->features.chin_type[i], i);
  1586. packet->setDataByName("nose_type", entity->features.nose_type[i], i);
  1587. packet->setDataByName("soga_eye_type", entity->features.soga_eye_type[i], i);
  1588. packet->setDataByName("soga_ear_type", entity->features.soga_ear_type[i], i);
  1589. packet->setDataByName("soga_eye_brow_type", entity->features.soga_eye_brow_type[i], i);
  1590. packet->setDataByName("soga_cheek_type", entity->features.soga_cheek_type[i], i);
  1591. packet->setDataByName("soga_lip_type", entity->features.soga_lip_type[i], i);
  1592. packet->setDataByName("soga_chin_type", entity->features.soga_chin_type[i], i);
  1593. packet->setDataByName("soga_nose_type", entity->features.soga_nose_type[i], i);
  1594. }
  1595. packet->setColorByName("skin_color", entity->features.skin_color);
  1596. packet->setColorByName("eye_color", entity->features.eye_color);
  1597. packet->setColorByName("hair_type_color", entity->features.hair_type_color);
  1598. packet->setColorByName("hair_type_highlight_color", entity->features.hair_type_highlight_color);
  1599. packet->setColorByName("hair_face_color", entity->features.hair_face_color);
  1600. packet->setColorByName("hair_face_highlight_color", entity->features.hair_face_highlight_color);
  1601. packet->setColorByName("hair_highlight", entity->features.hair_highlight_color);
  1602. packet->setColorByName("wing_color1", entity->features.wing_color1);
  1603. packet->setColorByName("wing_color2", entity->features.wing_color2);
  1604. packet->setColorByName("hair_color1", entity->features.hair_color1);
  1605. packet->setColorByName("hair_color2", entity->features.hair_color2);
  1606. packet->setColorByName("soga_skin_color", entity->features.soga_skin_color);
  1607. packet->setColorByName("soga_eye_color", entity->features.soga_eye_color);
  1608. packet->setColorByName("soga_hair_color1", entity->features.soga_hair_color1);
  1609. packet->setColorByName("soga_hair_color2", entity->features.soga_hair_color2);
  1610. packet->setColorByName("soga_hair_type_color", entity->features.soga_hair_type_color);
  1611. packet->setColorByName("soga_hair_type_highlight_color", entity->features.soga_hair_type_highlight_color);
  1612. packet->setColorByName("soga_hair_face_color", entity->features.soga_hair_face_color);
  1613. packet->setColorByName("soga_hair_face_highlight_color", entity->features.soga_hair_face_highlight_color);
  1614. packet->setColorByName("soga_hair_highlight", entity->features.soga_hair_highlight_color);
  1615. packet->setDataByName("body_age", entity->features.body_age);
  1616. }
  1617. else{
  1618. EQ2_Color empty;
  1619. empty.red = 255;
  1620. empty.blue = 255;
  1621. empty.green = 255;
  1622. packet->setColorByName("skin_color", empty);
  1623. packet->setColorByName("eye_color", empty);
  1624. packet->setColorByName("soga_skin_color", empty);
  1625. packet->setColorByName("soga_eye_color", empty);
  1626. }
  1627. if(appearance.icon == 0){
  1628. if(appearance.attackable == 1)
  1629. appearance.icon = 0;
  1630. else if(appearance.encounter_level > 0)
  1631. appearance.icon = 4;
  1632. else
  1633. appearance.icon = 6;
  1634. }
  1635. // If Coe+ clients modify the values before we send
  1636. // if not then just send the value we have.
  1637. int8 temp_icon = appearance.icon;
  1638. //Check if we need to add the hand icon..
  1639. if ((temp_icon & 6) != 6 && appearance.display_hand_icon) {
  1640. temp_icon |= 6;
  1641. }
  1642. //Icon value 28 for boats, set this without modifying the value
  1643. if (version >= 1188 && temp_icon != 28) {
  1644. if ((temp_icon & 64) > 0) {
  1645. temp_icon -= 64; // remove the DoV value;
  1646. temp_icon += 128; // add the CoE value
  1647. }
  1648. if ((temp_icon & 32) > 0) {
  1649. temp_icon -= 32; // remove the DoV value;
  1650. temp_icon += 64; // add the CoE value
  1651. }
  1652. if ((temp_icon & 4) > 0) {
  1653. temp_icon -= 4; // remove DoV value
  1654. temp_icon += 8; // add the CoE icon
  1655. }
  1656. if ((temp_icon & 6) > 0) {
  1657. temp_icon -= 10; // remove DoV value
  1658. temp_icon += 12; // add the CoE icon
  1659. }
  1660. }
  1661. packet->setDataByName("icon", temp_icon);//appearance.icon);
  1662. int16 temp_activity_status = 0;
  1663. if (version >= 1188) {
  1664. if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
  1665. temp_activity_status += ACTIVITY_STATUS_ROLEPLAYING_1188;
  1666. if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
  1667. temp_activity_status += ACTIVITY_STATUS_ANONYMOUS_1188;
  1668. if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
  1669. temp_activity_status += ACTIVITY_STATUS_LINKDEAD_1188;
  1670. if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
  1671. temp_activity_status += ACTIVITY_STATUS_CAMPING_1188;
  1672. if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
  1673. temp_activity_status += ACTIVITY_STATUS_LFG_1188;
  1674. if ((appearance.activity_status & ACTIVITY_STATUS_LFW) > 0)
  1675. temp_activity_status += ACTIVITY_STATUS_LFW_1188;
  1676. if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
  1677. temp_activity_status += ACTIVITY_STATUS_SOLID_1188;
  1678. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_GAINED) > 0)
  1679. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_GAINED_1188;
  1680. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_REMAINING) > 0)
  1681. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_REMAINING_1188;
  1682. }
  1683. else
  1684. temp_activity_status = appearance.activity_status;
  1685. packet->setDataByName("activity_status", temp_activity_status); //appearance.activity_status);
  1686. // If player and player has a follow target
  1687. if (IsPlayer()) {
  1688. if (((Player*)this)->GetFollowTarget())
  1689. packet->setDataByName("follow_target", ((((Player*)this)->GetIDWithPlayerSpawn(((Player*)this)->GetFollowTarget()) * -1) - 1));
  1690. else
  1691. packet->setDataByName("follow_target", 0);
  1692. }
  1693. if (GetTarget() && GetTarget()->GetTargetable())
  1694. packet->setDataByName("target_id", ((spawn->GetIDWithPlayerSpawn(GetTarget()) * -1) - 1));
  1695. else
  1696. packet->setDataByName("target_id", 0);
  1697. //Send spell effects for target window
  1698. if(IsEntity()){
  1699. InfoStruct* info = ((Entity*)this)->GetInfoStruct();
  1700. int8 i = 0;
  1701. int16 backdrop = 0;
  1702. int16 spell_icon = 0;
  1703. int32 spell_id = 0;
  1704. LuaSpell* spell = 0;
  1705. ((Entity*)this)->GetSpellEffectMutex()->readlock(__FUNCTION__, __LINE__);
  1706. while(i < 30){
  1707. //Change value of spell id for this packet if spell exists
  1708. spell_id = info->spell_effects[i].spell_id;
  1709. if(spell_id > 0)
  1710. spell_id = 0xFFFFFFFF - spell_id;
  1711. else
  1712. spell_id = 0;
  1713. packet->setSubstructDataByName("spell_effects", "spell_id", spell_id, i);
  1714. //Change value of spell icon for this packet if spell exists
  1715. spell_icon = info->spell_effects[i].icon;
  1716. if(spell_icon > 0){
  1717. if(!(spell_icon == 0xFFFF))
  1718. spell_icon = 0xFFFF - spell_icon;
  1719. }
  1720. else
  1721. spell_icon = 0;
  1722. packet->setSubstructDataByName("spell_effects", "spell_icon", spell_icon, i);
  1723. //Change backdrop values to match values in this packet
  1724. backdrop = info->spell_effects[i].icon_backdrop;
  1725. switch(backdrop){
  1726. case 312:
  1727. backdrop = 33080;
  1728. break;
  1729. case 313:
  1730. backdrop = 33081;
  1731. break;
  1732. case 314:
  1733. backdrop = 33082;
  1734. break;
  1735. case 315:
  1736. backdrop = 33083;
  1737. break;
  1738. case 316:
  1739. backdrop = 33084;
  1740. break;
  1741. case 317:
  1742. backdrop = 33085;
  1743. break;
  1744. case (318 || 319):
  1745. backdrop = 33086;
  1746. break;
  1747. default:
  1748. break;
  1749. }
  1750. packet->setSubstructDataByName("spell_effects", "spell_icon_backdrop", backdrop, i);
  1751. spell = info->spell_effects[i].spell;
  1752. if (spell)
  1753. packet->setSubstructDataByName("spell_effects", "spell_triggercount", spell->num_triggers, i);
  1754. i++;
  1755. }
  1756. ((Entity*)this)->GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
  1757. }
  1758. }
  1759. void Spawn::MoveToLocation(Spawn* spawn, float distance, bool immediate, bool mapped){
  1760. if(!spawn)
  1761. return;
  1762. SetRunningTo(spawn);
  1763. FaceTarget(spawn);
  1764. if (!IsPlayer() && distance > 0.0f)
  1765. {
  1766. if (GetInitialState() == 49156 && CheckLoS(spawn))
  1767. {
  1768. if (immediate)
  1769. ClearRunningLocations();
  1770. AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", true);
  1771. }
  1772. else if (/*!mapped && */GetZone())
  1773. {
  1774. GetZone()->movementMgr->NavigateTo((Entity*)this, spawn->GetX(), spawn->GetY(), spawn->GetZ());
  1775. last_grid_update = Timer::GetCurrentTime2();
  1776. }
  1777. else
  1778. {
  1779. if (immediate)
  1780. ClearRunningLocations();
  1781. AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", mapped);
  1782. }
  1783. }
  1784. }
  1785. void Spawn::ProcessMovement(bool isSpawnListLocked){
  1786. if(IsPlayer()){
  1787. //Check if player is riding a boat, if so update pos (boat's current location + XYZ offsets)
  1788. Player* player = ((Player*)this);
  1789. int32 boat_id = player->GetBoatSpawn();
  1790. Spawn* boat = 0;
  1791. if(boat_id > 0)
  1792. boat = GetZone()->GetSpawnByID(boat_id, isSpawnListLocked);
  1793. if(boat){
  1794. SetX(boat->GetX() + player->GetBoatX());
  1795. SetY(boat->GetY() + player->GetBoatY());
  1796. SetZ(boat->GetZ() + player->GetBoatZ());
  1797. }
  1798. return;
  1799. }
  1800. if (forceMapCheck && GetZone() != nullptr && zone->zonemap != nullptr && zone->zonemap->IsMapLoaded())
  1801. {
  1802. FixZ(true);
  1803. forceMapCheck = false;
  1804. }
  1805. if (GetHP() <= 0 && !IsWidget())
  1806. return;
  1807. if (EngagedInCombat())
  1808. {
  1809. int locations = 0;
  1810. if (movement_locations && MMovementLocations)
  1811. {
  1812. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  1813. locations = movement_locations->size();
  1814. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  1815. }
  1816. if (locations < 1 && GetZone() && ((Entity*)this)->IsFeared())
  1817. {
  1818. CalculateNewFearpoint();
  1819. }
  1820. }
  1821. MMovementLoop.lock();
  1822. Spawn* followTarget = GetZone()->GetSpawnByID(m_followTarget, isSpawnListLocked);
  1823. if (!followTarget && m_followTarget > 0)
  1824. m_followTarget = 0;
  1825. if (following && followTarget && !((Entity*)this)->IsFeared()) {
  1826. // Need to clear m_followTarget before the zoneserver deletes it
  1827. if (followTarget->GetHP() <= 0) {
  1828. followTarget = 0;
  1829. MMovementLoop.unlock();
  1830. return;
  1831. }
  1832. if (!IsEntity() || (!((Entity*)this)->IsCasting() && !((Entity*)this)->IsMezzedOrStunned() && !((Entity*)this)->IsRooted())) {
  1833. if (GetBaseSpeed() > 0) {
  1834. CalculateRunningLocation();
  1835. }
  1836. else {
  1837. float speed = 4.0f;
  1838. if (IsEntity())
  1839. speed = ((Entity*)this)->GetMaxSpeed();
  1840. SetSpeed(speed);
  1841. }
  1842. MovementLocation* loc = GetCurrentRunningLocation();
  1843. if ((GetDistance(followTarget, true) <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
  1844. ClearRunningLocations();
  1845. CalculateRunningLocation(true);
  1846. }
  1847. else if (loc) {
  1848. float distance = GetDistance(followTarget, loc->x, loc->y, loc->z);
  1849. if (distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  1850. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat(), true, loc->mapped);
  1851. CalculateRunningLocation();
  1852. }
  1853. }
  1854. else {
  1855. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat());
  1856. CalculateRunningLocation();
  1857. }
  1858. }
  1859. }
  1860. // Movement loop is only for scripted paths
  1861. else if(!EngagedInCombat() && !NeedsToResumeMovement() && movement_loop.size() > 0 && movement_index < movement_loop.size() && (!IsNPC() || !((NPC*)this)->m_runningBack)){
  1862. // Get the target location
  1863. MovementData* data = movement_loop[movement_index];
  1864. // need to resume our movement
  1865. if(resume_movement){
  1866. if (movement_locations){
  1867. while (movement_locations->size()){
  1868. safe_delete(movement_locations->front());
  1869. movement_locations->pop_front();
  1870. }
  1871. movement_locations->clear();
  1872. }
  1873. data = movement_loop[movement_index];
  1874. ((Entity*)this)->SetSpeed(data->speed);
  1875. SetSpeed(data->speed);
  1876. if(!IsWidget())
  1877. FaceTarget(data->x, data->z);
  1878. // 0 delay at target location, need to set multiple locations
  1879. if(data->delay == 0 && movement_loop.size() > 0) {
  1880. int16 tmp_index = movement_index+1;
  1881. MovementData* data2 = 0;
  1882. if(tmp_index < movement_loop.size())
  1883. data2 = movement_loop[tmp_index];
  1884. else
  1885. data2 = movement_loop[0];
  1886. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  1887. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  1888. }
  1889. // delay at target location, only need to set 1 location
  1890. else
  1891. AddRunningLocation(data->x, data->y, data->z, data->speed);
  1892. movement_start_time = 0;
  1893. resume_movement = false;
  1894. }
  1895. // If we are not moving or we have arrived at our destination
  1896. else if(!IsRunning() || (data && data->x == GetX() && data->y == GetY() && data->z == GetZ())){
  1897. // If we were moving remove the last running location (the point we just arrived at)
  1898. if(IsRunning())
  1899. RemoveRunningLocation();
  1900. // If this waypoint has a delay and we just arrived here (movement_start_time == 0)
  1901. if(data->delay > 0 && movement_start_time == 0){
  1902. // Set the current time
  1903. movement_start_time = Timer::GetCurrentTime2();
  1904. // If this waypoint had a lua function then call it
  1905. if(data->lua_function.length() > 0)
  1906. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  1907. int16 nextMove;
  1908. if ((int16)(movement_index + 1) < movement_loop.size())
  1909. nextMove = movement_index + 1;
  1910. else
  1911. nextMove = 0;
  1912. // Get the next target location
  1913. data = movement_loop[nextMove];
  1914. //Go ahead and face the next location
  1915. FaceTarget(data->x, data->z);
  1916. }
  1917. // If this waypoint has no delay or we have waited the required time (current time >= delay + movement_start_time)
  1918. else if(data->delay == 0 || (data->delay > 0 && Timer::GetCurrentTime2() >= (data->delay+movement_start_time))) {
  1919. // if no delay at this waypoint but a lua function for it then call the function
  1920. if(data->delay == 0 && data->lua_function.length() > 0)
  1921. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  1922. // Advance the current movement loop index
  1923. if((int16)(movement_index+1) < movement_loop.size())
  1924. movement_index++;
  1925. else
  1926. movement_index = 0;
  1927. // Get the next target location
  1928. data = movement_loop[movement_index];
  1929. // set the speed for that location
  1930. SetSpeed(data->speed);
  1931. if(!IsWidget())
  1932. // turn towards the location
  1933. FaceTarget(data->x, data->z);
  1934. // If 0 delay at location get and set data for the point after it
  1935. if(data->delay == 0 && movement_loop.size() > 0){
  1936. while (movement_locations->size()){
  1937. safe_delete(movement_locations->front());
  1938. movement_locations->pop_front();
  1939. }
  1940. // clear current target locations
  1941. movement_locations->clear();
  1942. // get the data for the location after out new location
  1943. int16 tmp_index = movement_index+1;
  1944. MovementData* data2 = 0;
  1945. if(tmp_index < movement_loop.size())
  1946. data2 = movement_loop[tmp_index];
  1947. else
  1948. data2 = movement_loop[0];
  1949. // set the first location (adds it to movement_locations that we just cleared)
  1950. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  1951. // set the location after that
  1952. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  1953. }
  1954. // there is a delay at the next location so we only need to set it
  1955. else
  1956. AddRunningLocation(data->x, data->y, data->z, data->speed);
  1957. // reset this timer to 0 now that we are moving again
  1958. movement_start_time = 0;
  1959. }
  1960. }
  1961. // moving and not at target location yet
  1962. else if(GetBaseSpeed() > 0)
  1963. CalculateRunningLocation();
  1964. // not moving, have a target location but not at it yet
  1965. else if (data) {
  1966. SetSpeed(data->speed);
  1967. AddRunningLocation(data->x, data->y, data->z, data->speed);
  1968. }
  1969. }
  1970. else if (IsRunning()) {
  1971. CalculateRunningLocation();
  1972. }
  1973. /*else if (IsNPC() && !IsRunning() && !EngagedInCombat() && ((NPC*)this)->GetRunbackLocation()) {
  1974. // Is an npc that is not moving and not engaged in combat but has a run back location set then clear the runback location
  1975. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Clear runback location for %s", GetName());
  1976. ((NPC*)this)->ClearRunback();
  1977. resume_movement = true;
  1978. NeedsToResumeMovement(false);
  1979. }*/
  1980. MMovementLoop.unlock();
  1981. }
  1982. void Spawn::ResetMovement(){
  1983. MMovementLoop.lock();
  1984. vector<MovementData*>::iterator itr;
  1985. for(itr = movement_loop.begin(); itr != movement_loop.end(); itr++){
  1986. safe_delete(*itr);
  1987. }
  1988. MMovementLoop.unlock();
  1989. resume_movement = true;
  1990. movement_index = 0;
  1991. }
  1992. void Spawn::AddMovementLocation(float x, float y, float z, float speed, int16 delay, const char* lua_function){
  1993. LogWrite(LUA__DEBUG, 5, "LUA", "AddMovementLocation: x: %.2f, y: %.2f, z: %.2f, speed: %.2f, delay: %i, lua: %s",
  1994. x, y, z, speed, delay, string(lua_function).c_str());
  1995. MovementData* data = new MovementData;
  1996. data->x = x;
  1997. data->y = y;
  1998. data->z = z;
  1999. data->speed = speed;
  2000. data->delay = delay*1000;
  2001. if(lua_function)
  2002. data->lua_function = string(lua_function);
  2003. MMovementLoop.lock();
  2004. movement_loop.push_back(data);
  2005. MMovementLoop.unlock();
  2006. }
  2007. bool Spawn::IsRunning(){
  2008. if(movement_locations && movement_locations->size() > 0)
  2009. return true;
  2010. else
  2011. return false;
  2012. }
  2013. void Spawn::RunToLocation(float x, float y, float z, float following_x, float following_y, float following_z){
  2014. if(!IsWidget())
  2015. FaceTarget(x, z);
  2016. SetPos(&appearance.pos.X2, x, false);
  2017. SetPos(&appearance.pos.Z2, z, false);
  2018. SetPos(&appearance.pos.Y2, y, false);
  2019. if(following_x == 0 && following_y == 0 && following_z == 0){
  2020. SetPos(&appearance.pos.X3, x, false);
  2021. SetPos(&appearance.pos.Z3, z, false);
  2022. SetPos(&appearance.pos.Y3, y, false);
  2023. }
  2024. else{
  2025. SetPos(&appearance.pos.X3, following_x, false);
  2026. SetPos(&appearance.pos.Y3, following_y, false);
  2027. SetPos(&appearance.pos.Z3, following_z, false);
  2028. }
  2029. position_changed = true;
  2030. changed = true;
  2031. GetZone()->AddChangedSpawn(this);
  2032. }
  2033. MovementLocation* Spawn::GetCurrentRunningLocation(){
  2034. MovementLocation* ret = 0;
  2035. if(movement_locations && movement_locations->size() > 0){
  2036. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2037. ret = movement_locations->front();
  2038. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2039. }
  2040. return ret;
  2041. }
  2042. MovementLocation* Spawn::GetLastRunningLocation(){
  2043. MovementLocation* ret = 0;
  2044. if(movement_locations && movement_locations->size() > 0){
  2045. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2046. ret = movement_locations->back();
  2047. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2048. }
  2049. return ret;
  2050. }
  2051. void Spawn::AddRunningLocation(float x, float y, float z, float speed, float distance_away, bool attackable, bool finished_adding_locations, string lua_function, bool isMapped){
  2052. if(speed == 0)
  2053. return;
  2054. MovementLocation* current_location = 0;
  2055. float distance = GetDistance(x, y, z, distance_away != 0);
  2056. if(distance_away != 0){
  2057. distance -= distance_away;
  2058. x = x - (GetX() - x)*distance_away/distance;
  2059. z = z - (GetZ() - z)*distance_away/distance;
  2060. }
  2061. if(!movement_locations){
  2062. movement_locations = new deque<MovementLocation*>();
  2063. MMovementLocations = new Mutex();
  2064. }
  2065. MovementLocation* data = new MovementLocation;
  2066. data->mapped = isMapped;
  2067. data->x = x;
  2068. data->y = y;
  2069. data->z = z;
  2070. data->speed = speed;
  2071. data->attackable = attackable;
  2072. data->lua_function = lua_function;
  2073. data->gridid = 0; // used for runback defaults
  2074. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2075. if(movement_locations->size() > 0)
  2076. current_location = movement_locations->back();
  2077. if(!current_location){
  2078. SetSpawnOrigX(GetX());
  2079. SetSpawnOrigY(GetY());
  2080. SetSpawnOrigZ(GetZ());
  2081. SetSpawnOrigHeading(GetHeading());
  2082. }
  2083. movement_locations->push_back(data);
  2084. if(finished_adding_locations){
  2085. current_location = movement_locations->front();
  2086. SetSpeed(current_location->speed);
  2087. if(movement_locations->size() > 1){
  2088. data = movement_locations->at(1);
  2089. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2090. }
  2091. else
  2092. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2093. }
  2094. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2095. }
  2096. bool Spawn::RemoveRunningLocation(){
  2097. bool ret = false;
  2098. if(movement_locations){
  2099. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2100. if(movement_locations->size() > 0){
  2101. delete movement_locations->front();
  2102. movement_locations->pop_front();
  2103. ret = true;
  2104. }
  2105. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2106. }
  2107. return ret;
  2108. }
  2109. void Spawn::ClearRunningLocations(){
  2110. while(RemoveRunningLocation()){}
  2111. }
  2112. bool Spawn::CalculateChange(){
  2113. bool remove_needed = false;
  2114. if(movement_locations && MMovementLocations){
  2115. MovementLocation* data = 0;
  2116. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2117. if(movement_locations->size() > 0){
  2118. // Target location
  2119. data = movement_locations->front();
  2120. // If no target or we are at the target location need to remove this point
  2121. if(!data || (data->x == GetX() && data->y == GetY() && data->z == GetZ()))
  2122. remove_needed = true;
  2123. if(data){
  2124. if(NeedsToResumeMovement()){
  2125. resume_movement = true;
  2126. NeedsToResumeMovement(false);
  2127. }
  2128. if(!data->attackable)
  2129. SetHeading(GetSpawnOrigHeading());
  2130. }
  2131. }
  2132. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2133. if(remove_needed) {
  2134. if (data && data->lua_function.length() > 0)
  2135. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2136. RemoveRunningLocation();
  2137. //CalculateChange();
  2138. }
  2139. else if(data){
  2140. // Speed is per second so we need a time_step (amount of time since the last update) to modify movement by
  2141. float time_step = (Timer::GetCurrentTime2() - last_movement_update) * 0.001; // * 0.001 is the same as / 1000, float muliplications is suppose to be faster though
  2142. // Get current location
  2143. float nx = GetX();
  2144. float ny = GetY();
  2145. float nz = GetZ();
  2146. // Get Forward vecotr
  2147. float tar_vx = data->x - nx;
  2148. float tar_vy = data->y - ny;
  2149. float tar_vz = data->z - nz;
  2150. // Multiply speed by the time_step to get how much should have changed over the last tick
  2151. float speed = GetSpeed() * time_step;
  2152. // Normalize the forward vector and multiply by speed, this gives us our change in coords, just need to add them to our current coords
  2153. float len = sqrtf(tar_vx * tar_vx + tar_vy * tar_vy + tar_vz * tar_vz);
  2154. tar_vx = (tar_vx / len) * speed;
  2155. tar_vy = (tar_vy / len) * speed;
  2156. tar_vz = (tar_vz / len) * speed;
  2157. // Distance less then 0.5 just set the npc to the target location
  2158. if (GetDistance(data->x, data->y, data->z, IsWidget() ? false : true) <= 0.5f) {
  2159. SetX(data->x, false);
  2160. SetZ(data->z, false);
  2161. SetY(data->y, false, true);
  2162. }
  2163. else {
  2164. SetX(nx + tar_vx, false);
  2165. SetZ(nz + tar_vz, false);
  2166. SetY(ny + tar_vy, false);
  2167. }
  2168. if (GetZone()->Grid != nullptr) {
  2169. Cell* newCell = GetZone()->Grid->GetCell(GetX(), GetZ());
  2170. if (newCell != Cell_Info.CurrentCell) {
  2171. GetZone()->Grid->RemoveSpawnFromCell(this);
  2172. GetZone()->Grid->AddSpawn(this, newCell);
  2173. }
  2174. int32 newGrid = GetZone()->Grid->GetGridID(this);
  2175. if (GetInitialState() != 49156 && newGrid != 0 && newGrid != appearance.pos.grid_id)
  2176. SetPos(&(appearance.pos.grid_id), newGrid);
  2177. }
  2178. }
  2179. }
  2180. return remove_needed;
  2181. }
  2182. void Spawn::CalculateRunningLocation(bool stop){
  2183. if (!stop && (last_location_update + 100) > Timer::GetCurrentTime2())
  2184. return;
  2185. else if (!stop)
  2186. last_location_update = Timer::GetCurrentTime2();
  2187. bool removed = CalculateChange();
  2188. if (stop) {
  2189. //following = false;
  2190. SetPos(&appearance.pos.X2, GetX(), false);
  2191. SetPos(&appearance.pos.Y2, GetY(), false);
  2192. SetPos(&appearance.pos.Z2, GetZ(), false);
  2193. SetPos(&appearance.pos.X3, GetX(), false);
  2194. SetPos(&appearance.pos.Y3, GetY(), false);
  2195. SetPos(&appearance.pos.Z3, GetZ(), false);
  2196. }
  2197. else if (removed && movement_locations->size() > 0) {
  2198. MovementLocation* current_location = movement_locations->at(0);
  2199. if (movement_locations->size() > 1) {
  2200. MovementLocation* data = movement_locations->at(1);
  2201. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2202. }
  2203. else
  2204. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2205. }
  2206. else if (GetZone() && GetTarget() != NULL && EngagedInCombat())
  2207. {
  2208. if (GetDistance(GetTarget()) > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  2209. {
  2210. if (GetInitialState() == 49156 && CheckLoS(GetTarget()))
  2211. AddRunningLocation(GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ(), GetSpeed(), 0, false);
  2212. else
  2213. GetZone()->movementMgr->NavigateTo((Entity*)this, GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ());
  2214. }
  2215. else
  2216. GetZone()->movementMgr->StopNavigation((Entity*)this);
  2217. }
  2218. }
  2219. float Spawn::GetFaceTarget(float x, float z) {
  2220. float angle;
  2221. double diff_x = x - GetX();
  2222. double diff_z = z - GetZ();
  2223. //If we're very close to the same spot don't bother changing heading
  2224. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  2225. return GetHeading();
  2226. }
  2227. if (diff_z == 0) {
  2228. if (diff_x > 0)
  2229. angle = 90;
  2230. else
  2231. angle = 270;
  2232. }
  2233. else
  2234. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  2235. if (angle < 0)
  2236. angle = angle + 360;
  2237. else
  2238. angle = angle + 180;
  2239. if (diff_x < 0)
  2240. angle = angle + 180;
  2241. if (last_heading_angle == angle) return angle;
  2242. return angle;
  2243. }
  2244. void Spawn::FaceTarget(float x, float z){
  2245. float angle;
  2246. double diff_x = x - GetX();
  2247. double diff_z = z - GetZ();
  2248. //If we're very close to the same spot don't bother changing heading
  2249. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  2250. return;
  2251. }
  2252. if(diff_z==0){
  2253. if(diff_x > 0)
  2254. angle = 90;
  2255. else
  2256. angle = 270;
  2257. }
  2258. else
  2259. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  2260. if(angle < 0)
  2261. angle = angle + 360;
  2262. else
  2263. angle = angle + 180;
  2264. if(diff_x < 0)
  2265. angle = angle + 180;
  2266. if (last_heading_angle == angle) return;
  2267. SetHeading(angle);
  2268. }
  2269. void Spawn::FaceTarget(Spawn* target){
  2270. if(!target)
  2271. return;
  2272. FaceTarget(target->GetX(), target->GetZ());
  2273. if(GetHP() > 0 && target->IsPlayer() && !EngagedInCombat()){
  2274. GetZone()->AddHeadingTimer(this);
  2275. SetTempActionState(0);
  2276. }
  2277. }
  2278. bool Spawn::MeetsSpawnAccessRequirements(Player* player){
  2279. bool ret = false;
  2280. Quest* quest = 0;
  2281. //Check if we meet all quest requirements first..
  2282. m_requiredQuests.readlock(__FUNCTION__, __LINE__);
  2283. if (player && required_quests.size() > 0) {
  2284. map<int32, vector<int16>* >::iterator itr;
  2285. for (itr = required_quests.begin(); itr != required_quests.end(); itr++) {
  2286. player->AddQuestRequiredSpawn(this, itr->first);
  2287. vector<int16>* quest_steps = itr->second;
  2288. for (int32 i = 0; i < quest_steps->size(); i++) {
  2289. quest = player->GetQuest(itr->first);
  2290. if (req_quests_continued_access) {
  2291. if (quest) {
  2292. if (quest->GetQuestStepCompleted(quest_steps->at(i))) {
  2293. ret = true;
  2294. break;
  2295. }
  2296. }
  2297. else if (player->GetCompletedQuest(itr->first)) {
  2298. ret = true;
  2299. break;
  2300. }
  2301. }
  2302. if (quest && quest->QuestStepIsActive(quest_steps->at(i))) {
  2303. ret = true;
  2304. break;
  2305. }
  2306. }
  2307. }
  2308. }
  2309. else
  2310. ret = true;
  2311. m_requiredQuests.releasereadlock(__FUNCTION__, __LINE__);
  2312. if (!ret)
  2313. return ret;
  2314. //Now check if the player meets all history requirements
  2315. m_requiredHistory.readlock(__FUNCTION__, __LINE__);
  2316. if (required_history.size() > 0){
  2317. map<int32, LUAHistory>::iterator itr;
  2318. for (itr = required_history.begin(); itr != required_history.end(); itr++){
  2319. player->AddHistoryRequiredSpawn(this, itr->first);
  2320. LUAHistory* player_history = player->GetLUAHistory(itr->first);
  2321. if (player_history){
  2322. if (player_history->Value != itr->second.Value || player_history->Value2 != itr->second.Value2)
  2323. ret = false;
  2324. }
  2325. else
  2326. ret = false;
  2327. if (!ret)
  2328. break;
  2329. }
  2330. }
  2331. m_requiredHistory.releasereadlock(__FUNCTION__, __LINE__);
  2332. return ret;
  2333. }
  2334. vector<Spawn*>* Spawn::GetSpawnGroup(){
  2335. vector<Spawn*>* ret_list = 0;
  2336. if(spawn_group_list){
  2337. ret_list = new vector<Spawn*>();
  2338. if(MSpawnGroup)
  2339. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  2340. ret_list->insert(ret_list->begin(), spawn_group_list->begin(), spawn_group_list->end());
  2341. if(MSpawnGroup)
  2342. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  2343. }
  2344. return ret_list;
  2345. }
  2346. bool Spawn::HasSpawnGroup() {
  2347. return spawn_group_list && spawn_group_list->size() > 0;
  2348. }
  2349. bool Spawn::IsInSpawnGroup(Spawn* spawn) {
  2350. bool ret = false;
  2351. if (HasSpawnGroup() && spawn) {
  2352. vector<Spawn*>::iterator itr;
  2353. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  2354. if ((*itr) == spawn) {
  2355. ret = true;
  2356. break;
  2357. }
  2358. }
  2359. }
  2360. return ret;
  2361. }
  2362. void Spawn::AddSpawnToGroup(Spawn* spawn){
  2363. if(!spawn)
  2364. return;
  2365. if(!spawn_group_list){
  2366. spawn_group_list = new vector<Spawn*>();
  2367. spawn_group_list->push_back(this);
  2368. safe_delete(MSpawnGroup);
  2369. MSpawnGroup = new Mutex();
  2370. MSpawnGroup->SetName("Spawn::MSpawnGroup");
  2371. }
  2372. vector<Spawn*>::iterator itr;
  2373. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2374. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2375. if((*itr) == spawn){
  2376. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2377. return;
  2378. }
  2379. }
  2380. spawn_group_list->push_back(spawn);
  2381. spawn->SetSpawnGroupList(spawn_group_list, MSpawnGroup);
  2382. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2383. }
  2384. void Spawn::SetSpawnGroupList(vector<Spawn*>* list, Mutex* mutex){
  2385. spawn_group_list = list;
  2386. MSpawnGroup = mutex;
  2387. }
  2388. void Spawn::RemoveSpawnFromGroup(bool erase_all){
  2389. SetSpawnGroupID(0);
  2390. bool del = false;
  2391. if(MSpawnGroup){
  2392. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2393. if(spawn_group_list){
  2394. vector<Spawn*>::iterator itr;
  2395. Spawn* spawn = 0;
  2396. if(spawn_group_list->size() == 1)
  2397. erase_all = true;
  2398. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2399. spawn = *itr;
  2400. if (spawn) {
  2401. if(!erase_all){
  2402. if(spawn == this){
  2403. spawn_group_list->erase(itr);
  2404. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2405. spawn_group_list = 0;
  2406. MSpawnGroup = 0;
  2407. return;
  2408. }
  2409. }
  2410. else{
  2411. if (spawn != this)
  2412. spawn->SetSpawnGroupList(0, 0);
  2413. }
  2414. }
  2415. }
  2416. if (erase_all)
  2417. spawn_group_list->clear();
  2418. del = (spawn_group_list->size() == 0);
  2419. }
  2420. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2421. if (del){
  2422. safe_delete(MSpawnGroup);
  2423. safe_delete(spawn_group_list);
  2424. }
  2425. }
  2426. }
  2427. void Spawn::SetSpawnGroupID(int32 id){
  2428. group_id = id;
  2429. }
  2430. int32 Spawn::GetSpawnGroupID(){
  2431. return group_id;
  2432. }
  2433. void Spawn::AddChangedZoneSpawn(){
  2434. if(send_spawn_changes && GetZone())
  2435. GetZone()->AddChangedSpawn(this);
  2436. }
  2437. void Spawn::RemoveSpawnAccess(Spawn* spawn) {
  2438. if (allowed_access.count(spawn->GetID()) > 0) {
  2439. allowed_access.erase(spawn->GetID());
  2440. GetZone()->HidePrivateSpawn(this);
  2441. }
  2442. }
  2443. void Spawn::SetFollowTarget(Spawn* spawn) {
  2444. if (spawn && spawn != this) {
  2445. m_followTarget = spawn->GetID();
  2446. }
  2447. else {
  2448. m_followTarget = 0;
  2449. if (following)
  2450. following = false;
  2451. }
  2452. }
  2453. void Spawn::AddTempVariable(string var, string val) {
  2454. m_tempVariableTypes[var] = 5;
  2455. m_tempVariables[var] = val;
  2456. }
  2457. void Spawn::AddTempVariable(string var, Spawn* val) {
  2458. m_tempVariableTypes[var] = 1;
  2459. m_tempVariableSpawn[var] = val->GetID();
  2460. }
  2461. void Spawn::AddTempVariable(string var, ZoneServer* val) {
  2462. m_tempVariableTypes[var] = 2;
  2463. m_tempVariableZone[var] = val;
  2464. }
  2465. void Spawn::AddTempVariable(string var, Item* val) {
  2466. m_tempVariableTypes[var] = 3;
  2467. m_tempVariableItem[var] = val;
  2468. }
  2469. void Spawn::AddTempVariable(string var, Quest* val) {
  2470. m_tempVariableTypes[var] = 4;
  2471. m_tempVariableQuest[var] = val;
  2472. }
  2473. string Spawn::GetTempVariable(string var) {
  2474. string ret = "";
  2475. if (m_tempVariables.count(var) > 0)
  2476. ret = m_tempVariables[var];
  2477. return ret;
  2478. }
  2479. Spawn* Spawn::GetTempVariableSpawn(string var) {
  2480. Spawn* ret = 0;
  2481. if (m_tempVariableSpawn.count(var) > 0)
  2482. ret = GetZone()->GetSpawnByID(m_tempVariableSpawn[var]);
  2483. return ret;
  2484. }
  2485. ZoneServer* Spawn::GetTempVariableZone(string var) {
  2486. ZoneServer* ret = 0;
  2487. if (m_tempVariableZone.count(var) > 0)
  2488. ret = m_tempVariableZone[var];
  2489. return ret;
  2490. }
  2491. Item* Spawn::GetTempVariableItem(string var) {
  2492. Item* ret = 0;
  2493. if (m_tempVariableItem.count(var) > 0)
  2494. ret = m_tempVariableItem[var];
  2495. return ret;
  2496. }
  2497. Quest* Spawn::GetTempVariableQuest(string var) {
  2498. Quest* ret = 0;
  2499. if (m_tempVariableQuest.count(var) > 0)
  2500. ret = m_tempVariableQuest[var];
  2501. return ret;
  2502. }
  2503. int8 Spawn::GetTempVariableType(string var) {
  2504. int8 ret = 0;
  2505. if (m_tempVariableTypes.count(var) > 0)
  2506. ret = m_tempVariableTypes[var];
  2507. return ret;
  2508. }
  2509. void Spawn::DeleteTempVariable(string var) {
  2510. int8 type = GetTempVariableType(var);
  2511. switch (type) {
  2512. case 1:
  2513. m_tempVariableSpawn.erase(var);
  2514. break;
  2515. case 2:
  2516. m_tempVariableZone.erase(var);
  2517. break;
  2518. case 3:
  2519. m_tempVariableItem.erase(var);
  2520. break;
  2521. case 4:
  2522. m_tempVariableQuest.erase(var);
  2523. break;
  2524. case 5:
  2525. m_tempVariables.erase(var);
  2526. break;
  2527. }
  2528. m_tempVariableTypes.erase(var);
  2529. }
  2530. Spawn* Spawn::GetRunningTo() {
  2531. return GetZone()->GetSpawnByID(running_to);
  2532. }
  2533. Spawn* Spawn::GetFollowTarget() {
  2534. return GetZone()->GetSpawnByID(m_followTarget);
  2535. }
  2536. void Spawn::CopySpawnAppearance(Spawn* spawn){
  2537. if (!spawn)
  2538. return;
  2539. //This function copies the appearace of the provided spawn to this one
  2540. if (spawn->IsEntity() && IsEntity()){
  2541. memcpy(&((Entity*)this)->features, &((Entity*)spawn)->features, sizeof(CharFeatures));
  2542. memcpy(&((Entity*)this)->equipment, &((Entity*)spawn)->equipment, sizeof(EQ2_Equipment));
  2543. }
  2544. SetSize(spawn->GetSize());
  2545. SetModelType(spawn->GetModelType());
  2546. }
  2547. void Spawn::SetY(float y, bool updateFlags, bool disableYMapCheck)
  2548. {
  2549. SetPos(&appearance.pos.Y, y, updateFlags);
  2550. if (!disableYMapCheck)
  2551. FixZ();
  2552. }
  2553. float Spawn::FindDestGroundZ(glm::vec3 dest, float z_offset)
  2554. {
  2555. float best_z = BEST_Z_INVALID;
  2556. if (GetZone() != nullptr && GetZone()->zonemap != nullptr)
  2557. {
  2558. dest.z += z_offset;
  2559. best_z = zone->zonemap->FindBestZ(dest, nullptr);
  2560. }
  2561. return best_z;
  2562. }
  2563. float Spawn::GetFixedZ(const glm::vec3& destination, int32 z_find_offset) {
  2564. BenchTimer timer;
  2565. timer.reset();
  2566. float new_z = destination.z;
  2567. if (GetZone() != nullptr && zone->zonemap != nullptr) {
  2568. /* if (flymode == GravityBehavior::Flying)
  2569. return new_z;
  2570. */
  2571. /* if (zone->HasWaterMap() && zone->watermap->InLiquid(glm::vec3(m_Position)))
  2572. return new_z;
  2573. */
  2574. /*
  2575. * Any more than 5 in the offset makes NPC's hop/snap to ceiling in small corridors
  2576. */
  2577. new_z = this->FindDestGroundZ(destination, z_find_offset);
  2578. if (new_z != BEST_Z_INVALID) {
  2579. if (new_z < -2000) {
  2580. new_z = GetY();
  2581. }
  2582. }
  2583. auto duration = timer.elapsed();
  2584. LogWrite(MAP__DEBUG, 0, "Map", "Mob::GetFixedZ() ([{%s}]) returned [{%f}] at [{%f}], [{%f}], [{%f}] - Took [{%f}]",
  2585. this->GetName(),
  2586. new_z,
  2587. destination.x,
  2588. destination.y,
  2589. destination.z,
  2590. duration);
  2591. }
  2592. return new_z;
  2593. }
  2594. void Spawn::FixZ(bool forceUpdate) {
  2595. if (IsPlayer() || (this->GetInitialState() == 49156)) {
  2596. return;
  2597. }
  2598. /*
  2599. if (flymode == GravityBehavior::Flying) {
  2600. return;
  2601. }*/
  2602. /*
  2603. if (zone->watermap && zone->watermap->InLiquid(m_Position)) {
  2604. return;
  2605. }*/
  2606. glm::vec3 current_loc(GetX(), GetZ(), GetY());
  2607. float new_z = GetFixedZ(current_loc, 1);
  2608. if (new_z == GetY())
  2609. return;
  2610. if ((new_z > -2000) && new_z != BEST_Z_INVALID) {
  2611. SetY(new_z, forceUpdate, true);
  2612. }
  2613. else {
  2614. LogWrite(MAP__DEBUG, 0, "Map", "[{%s}] is failing to find Z [{%f}]", this->GetName(), std::abs(GetY() - new_z));
  2615. }
  2616. }
  2617. bool Spawn::CheckLoS(Spawn* target)
  2618. {
  2619. glm::vec3 targpos(target->GetX(), target->GetZ(), target->GetY()+1.0f);
  2620. glm::vec3 pos(GetX(), GetZ(), GetY()+1.0f);
  2621. return CheckLoS(pos, targpos);
  2622. }
  2623. bool Spawn::CheckLoS(glm::vec3 myloc, glm::vec3 oloc)
  2624. {
  2625. ZoneServer* zone = GetZone();
  2626. if (zone == NULL || zone->zonemap == NULL)
  2627. return true;
  2628. else
  2629. return zone->zonemap->CheckLoS(myloc, oloc);
  2630. return false;
  2631. }
  2632. void Spawn::CalculateNewFearpoint()
  2633. {
  2634. if (GetZone() && GetZone()->pathing) {
  2635. auto Node = zone->pathing->GetRandomLocation(glm::vec3(GetX(), GetZ(), GetY()));
  2636. if (Node.x != 0.0f || Node.y != 0.0f || Node.z != 0.0f) {
  2637. AddRunningLocation(Node.x, Node.y, Node.z, GetSpeed(), 0, true, true, "", true);
  2638. }
  2639. }
  2640. }