Spawn.cpp 112 KB

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