Spawn.cpp 114 KB

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