9
3

Spawn.cpp 129 KB

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