Spawn.cpp 132 KB

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