Spawn.cpp 111 KB

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