Spawn.cpp 89 KB

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