Spawn.cpp 93 KB

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