Spawn.cpp 126 KB

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