Spawn.cpp 158 KB

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