Spawn.cpp 98 KB

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