Spawn.cpp 139 KB

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