Spawn.cpp 111 KB

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