Spawn.cpp 137 KB

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