Spawn.cpp 93 KB

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