9
3

Spawn.cpp 98 KB

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