Spawn.cpp 111 KB

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