Spawn.cpp 96 KB

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