Spawn.cpp 148 KB

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