Spawn.cpp 95 KB

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