Spawn.cpp 135 KB

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