Spawn.cpp 155 KB

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