Spawn.cpp 110 KB

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