Spawn.cpp 128 KB

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