Spawn.cpp 158 KB

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