Spawn.cpp 131 KB

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