Spawn.cpp 120 KB

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