Spawn.cpp 147 KB

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