Spawn.cpp 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744
  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_ratio", size > 0 ? (((float)size) / 32) : 1);
  1682. else
  1683. packet->setDataByName("pos_size_ratio", 1.0f);
  1684. }
  1685. packet->setDataByName("pos_state", appearance.pos.state);
  1686. bool include_location = true;
  1687. if (IsWidget() && ((Widget*)this)->GetIncludeLocation() == false)
  1688. include_location = false;
  1689. else if (IsSign() && ((Sign*)this)->GetIncludeLocation() == false)
  1690. include_location = false;
  1691. if (include_location){
  1692. if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
  1693. Widget* widget = (Widget*)this;
  1694. float x = appearance.pos.X - widget->GetWidgetX();
  1695. float y = appearance.pos.Y - widget->GetWidgetY();
  1696. float z = appearance.pos.Z - widget->GetWidgetZ();
  1697. packet->setDataByName("pos_x", x);
  1698. packet->setDataByName("pos_y", y);
  1699. packet->setDataByName("pos_z", z);
  1700. }
  1701. else {
  1702. packet->setDataByName("pos_x", appearance.pos.X);
  1703. packet->setDataByName("pos_y", appearance.pos.Y);
  1704. packet->setDataByName("pos_z", appearance.pos.Z);
  1705. }
  1706. if (IsSign())
  1707. packet->setDataByName("pos_unknown6", 3, 2);
  1708. }
  1709. if (IsPlayer()) {
  1710. packet->setDataByName("pos_swim_speed_modifier", 20);
  1711. packet->setDataByName("pos_x_velocity", TransformFromFloat(GetSpeedX(), 5));
  1712. packet->setDataByName("pos_y_velocity", TransformFromFloat(GetSpeedY(), 5));
  1713. packet->setDataByName("pos_z_velocity", TransformFromFloat(GetSpeedZ(), 5));
  1714. }
  1715. if (appearance.pos.X2 == 0 && appearance.pos.Y2 == 0 && appearance.pos.Z2 && (appearance.pos.X != 0 || appearance.pos.Y != 0 || appearance.pos.Z != 0)) {
  1716. appearance.pos.X2 = appearance.pos.X;
  1717. appearance.pos.Y2 = appearance.pos.Y;
  1718. appearance.pos.Z2 = appearance.pos.Z;
  1719. }
  1720. if (appearance.pos.X3 == 0 && appearance.pos.Y3 == 0 && appearance.pos.Z3 && (appearance.pos.X != 0 || appearance.pos.Y != 0 || appearance.pos.Z != 0)) {
  1721. appearance.pos.X3 = appearance.pos.X;
  1722. appearance.pos.Y3 = appearance.pos.Y;
  1723. appearance.pos.Z3 = appearance.pos.Z;
  1724. }
  1725. //Transform To/From Float bits (original client)
  1726. //pos_loc_offset[3]: 5
  1727. //pos_x_velocity: 5
  1728. //pos_y_velocity: 5
  1729. //pos_z_velocity: 5
  1730. //pos_heading1: 6
  1731. //pos_heading2: 6
  1732. //pos_speed: 8
  1733. //pos_dest_loc_offset[3]: 5
  1734. //pos_dest_loc_offset2[3]: 5
  1735. //pos_heading_speed: 5
  1736. //pos_move_type: 5 (speed_modifier)
  1737. //pos_swim_speed_modifier: 5
  1738. //pos_side_speed: 8
  1739. //pos_vert_speed: 8
  1740. //pos_requested_pitch: 6
  1741. //pos_requested_pitch_speed: 5
  1742. //pos_pitch: 6
  1743. //pos_collision_radius: 5
  1744. //pos_size: 5
  1745. //actor_stop_range: 5
  1746. //this is for original box client, destinations used to be offsets
  1747. if (appearance.pos.X2 != 0 || appearance.pos.Y2 != 0 || appearance.pos.Z2 != 0) {
  1748. packet->setDataByName("pos_dest_loc_offset", TransformFromFloat(appearance.pos.X2 - appearance.pos.X, 5));
  1749. packet->setDataByName("pos_dest_loc_offset", TransformFromFloat(appearance.pos.Y2 - appearance.pos.Y, 5), 1);
  1750. packet->setDataByName("pos_dest_loc_offset", TransformFromFloat(appearance.pos.Z2 - appearance.pos.Z, 5), 2);
  1751. }
  1752. if (appearance.pos.X3 != 0 || appearance.pos.Y3 != 0 || appearance.pos.Z3 != 0) {
  1753. packet->setDataByName("pos_dest_loc_offset2", TransformFromFloat(appearance.pos.X3 - appearance.pos.X, 5));
  1754. packet->setDataByName("pos_dest_loc_offset2", TransformFromFloat(appearance.pos.Y3 - appearance.pos.Y, 5), 1);
  1755. packet->setDataByName("pos_dest_loc_offset2", TransformFromFloat(appearance.pos.Z3 - appearance.pos.Z, 5), 2);
  1756. }
  1757. bool bSendSpeed = true;
  1758. if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
  1759. Widget* widget = (Widget*)this;
  1760. float x;
  1761. float y;
  1762. float z;
  1763. if (IsRunning()){
  1764. x = appearance.pos.X2 - widget->GetWidgetX();
  1765. y = appearance.pos.Y2 - widget->GetWidgetY();
  1766. z = appearance.pos.Z2- widget->GetWidgetZ();
  1767. }
  1768. else {
  1769. x = appearance.pos.X - widget->GetWidgetX();
  1770. y = appearance.pos.Y - widget->GetWidgetY();
  1771. z = appearance.pos.Z - widget->GetWidgetZ();
  1772. }
  1773. packet->setDataByName("pos_next_x", x);
  1774. packet->setDataByName("pos_next_y", y);
  1775. packet->setDataByName("pos_next_z", z);
  1776. packet->setDataByName("pos_x3", x);
  1777. packet->setDataByName("pos_y3", y);
  1778. packet->setDataByName("pos_z3", z);
  1779. }
  1780. //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
  1781. //This fixes the bug where spawns with movement scripts face south when initially spawning if they are at their target location.
  1782. else if (bSpawnUpdate || memcmp(&appearance.pos.X, &appearance.pos.X2, sizeof(float) * 3) != 0) {
  1783. packet->setDataByName("pos_next_x", appearance.pos.X2);
  1784. packet->setDataByName("pos_next_y", appearance.pos.Y2);
  1785. packet->setDataByName("pos_next_z", appearance.pos.Z2);
  1786. packet->setDataByName("pos_x3", appearance.pos.X3);
  1787. packet->setDataByName("pos_y3", appearance.pos.Y3);
  1788. packet->setDataByName("pos_z3", appearance.pos.Z3);
  1789. }
  1790. else
  1791. {
  1792. bSendSpeed = false;
  1793. }
  1794. //packet->setDataByName("pos_unknown2", 4, 2);
  1795. int16 speed_multiplier = rule_manager.GetGlobalRule(R_Spawn, SpeedMultiplier)->GetInt16(); // was 1280, 600 and now 300... investigating why
  1796. if (IsPlayer()) {
  1797. Player* player = static_cast<Player*>(this);
  1798. packet->setDataByName("pos_speed", player->GetPosPacketSpeed() * speed_multiplier);
  1799. packet->setDataByName("pos_side_speed", player->GetSideSpeed() * speed_multiplier);
  1800. }
  1801. else if (bSendSpeed) {
  1802. packet->setDataByName("pos_speed", GetSpeed() * speed_multiplier);
  1803. }
  1804. if (IsNPC() || IsPlayer()) {
  1805. packet->setDataByName("pos_move_type", 25);
  1806. }
  1807. else if (IsWidget() || IsSign()) {
  1808. packet->setDataByName("pos_move_type", 11);
  1809. }
  1810. else if(IsGroundSpawn()) {
  1811. packet->setDataByName("pos_move_type", 16);
  1812. }
  1813. if (!IsPlayer())
  1814. packet->setDataByName("pos_movement_mode", 2);
  1815. if(version <= 910)
  1816. packet->setDataByName("pos_unknown10", 0xFFFF, 1);
  1817. else if (version >= 1119)
  1818. packet->setDataByName("face_actor_id", 0xFFFFFFFF);
  1819. else
  1820. packet->setDataByName("pos_unknown10", 0xFFFF);
  1821. if(version <= 910)
  1822. packet->setDataByName("pos_unknown10", 0xFFFF, 2);
  1823. else if (version >= 1119)
  1824. packet->setDataByName("face_actor_id", 0xFFFFFFFF);
  1825. else
  1826. packet->setDataByName("pos_unknown10", 0XFFFF, 1);
  1827. packet->setDataByName("pos_pitch1", appearance.pos.Pitch1);
  1828. packet->setDataByName("pos_pitch2", appearance.pos.Pitch2);
  1829. packet->setDataByName("pos_roll", appearance.pos.Roll);
  1830. }
  1831. void Spawn::InitializeInfoPacketData(Player* spawn, PacketStruct* packet) {
  1832. int16 version = packet->GetVersion();
  1833. bool spawnHiddenFromClient = false;
  1834. // radius of 0 is always seen, -1 is never seen (unless items/spells override), larger than 0 is a defined radius to restrict visibility
  1835. sint32 radius = rule_manager.GetGlobalRule(R_PVP, InvisPlayerDiscoveryRange)->GetSInt32();
  1836. if (radius != 0 && (Spawn*)spawn != this && this->IsPlayer() && !spawn->CanSeeInvis((Entity*)this))
  1837. spawnHiddenFromClient = true;
  1838. if (!spawnHiddenFromClient && (appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1)) {
  1839. appearance.locked_no_loot = 1; //for now
  1840. if (!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign()) {
  1841. int8 percent = 0;
  1842. if (GetHP() > 0)
  1843. percent = (int8)(((float)GetHP() / GetTotalHP()) * 100);
  1844. if (version >= 284) {
  1845. if (percent < 100) {
  1846. packet->setDataByName("hp_remaining", 100 ^ percent);
  1847. }
  1848. else
  1849. packet->setDataByName("hp_remaining", 0);
  1850. }
  1851. else {
  1852. if (percent > 100)
  1853. percent = 100;
  1854. packet->setDataByName("hp_remaining", percent);
  1855. }
  1856. if (GetTotalPower() > 0) {
  1857. percent = (int8)(((float)GetPower() / GetTotalPower()) * 100);
  1858. if (percent > 0)
  1859. packet->setDataByName("power_percent", percent);
  1860. else
  1861. packet->setDataByName("power_percent", 0);
  1862. }
  1863. }
  1864. }
  1865. if (version <= 546) {
  1866. packet->setDataByName("name", appearance.name);
  1867. for (int8 i = 0; i < 8; i++)
  1868. packet->setDataByName("unknown1", 0xFF, i);
  1869. if (appearance.show_level == 0)
  1870. packet->setDataByName("hide_health", 1);
  1871. }
  1872. if (GetHP() <= 0 && IsEntity())
  1873. packet->setDataByName("corpse", 1);
  1874. if (!IsPlayer())
  1875. packet->setDataByName("npc", 1);
  1876. if (GetMerchantID() > 0)
  1877. packet->setDataByName("merchant", 1);
  1878. if (EngagedInCombat())
  1879. packet->setDataByName("in_combat", 1);
  1880. packet->setDataByName("level", (int8)GetLevel());
  1881. packet->setDataByName("unknown4", (int8)GetLevel());
  1882. packet->setDataByName("difficulty", appearance.encounter_level); //6);
  1883. packet->setDataByName("heroic_flag", appearance.heroic_flag);
  1884. if (!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign())
  1885. packet->setDataByName("interaction_flag", 12); //this makes NPCs head turn to look at you
  1886. packet->setDataByName("class", appearance.adventure_class);
  1887. int16 model_type = appearance.model_type;
  1888. if (GetIllusionModel() != 0) {
  1889. if (IsPlayer()) {
  1890. if (((Player*)this)->get_character_flag(CF_SHOW_ILLUSION)) {
  1891. model_type = GetIllusionModel();
  1892. }
  1893. }
  1894. else
  1895. model_type = GetIllusionModel();
  1896. }
  1897. int16 sogaModelType = appearance.soga_model_type;
  1898. if (spawnHiddenFromClient)
  1899. {
  1900. model_type = 0;
  1901. sogaModelType = 0;
  1902. }
  1903. packet->setDataByName("model_type", model_type);
  1904. if (appearance.soga_model_type == 0)
  1905. packet->setDataByName("soga_model_type", model_type);
  1906. else
  1907. packet->setDataByName("soga_model_type", sogaModelType);
  1908. if (GetTempActionState() >= 0)
  1909. packet->setDataByName("action_state", GetTempActionState());
  1910. else
  1911. packet->setDataByName("action_state", appearance.action_state);
  1912. if (GetTempVisualState() >= 0)
  1913. packet->setDataByName("visual_state", GetTempVisualState());
  1914. else
  1915. packet->setDataByName("visual_state", appearance.visual_state);
  1916. packet->setDataByName("emote_state", appearance.emote_state);
  1917. packet->setDataByName("mood_state", appearance.mood_state);
  1918. packet->setDataByName("gender", appearance.gender);
  1919. packet->setDataByName("race", appearance.race);
  1920. packet->setDataByName("gender", appearance.gender);
  1921. if (IsEntity()) {
  1922. Entity* entity = ((Entity*)this);
  1923. packet->setDataByName("combat_voice", entity->GetCombatVoice());
  1924. packet->setDataByName("emote_voice", entity->GetEmoteVoice());
  1925. for (int i = 0; i < 25; i++) {
  1926. if (i == 2) { //don't send helm if hidden flag
  1927. if (IsPlayer()) {
  1928. if (((Player*)this)->get_character_flag(CF_HIDE_HELM)) {
  1929. packet->setDataByName("equipment_types", 0, i);
  1930. packet->setColorByName("equipment_colors", 0, i);
  1931. packet->setColorByName("equipment_highlights", 0, i);
  1932. continue;
  1933. }
  1934. }
  1935. if (IsBot()) {
  1936. if (!((Bot*)this)->ShowHelm) {
  1937. packet->setDataByName("equipment_types", 0, i);
  1938. packet->setColorByName("equipment_colors", 0, i);
  1939. packet->setColorByName("equipment_highlights", 0, i);
  1940. continue;
  1941. }
  1942. }
  1943. }
  1944. else if (i == 19) { //don't send cloak if hidden
  1945. if (IsPlayer()) {
  1946. if (!((Player*)this)->get_character_flag(CF_SHOW_CLOAK)) {
  1947. packet->setDataByName("equipment_types", 0, i);
  1948. packet->setColorByName("equipment_colors", 0, i);
  1949. packet->setColorByName("equipment_highlights", 0, i);
  1950. continue;
  1951. }
  1952. }
  1953. if (IsBot()) {
  1954. if (!((Bot*)this)->ShowCloak) {
  1955. packet->setDataByName("equipment_types", 0, i);
  1956. packet->setColorByName("equipment_colors", 0, i);
  1957. packet->setColorByName("equipment_highlights", 0, i);
  1958. continue;
  1959. }
  1960. }
  1961. }
  1962. packet->setDataByName("equipment_types", entity->equipment.equip_id[i], i);
  1963. packet->setColorByName("equipment_colors", entity->equipment.color[i], i);
  1964. packet->setColorByName("equipment_highlights", entity->equipment.highlight[i], i);
  1965. }
  1966. packet->setDataByName("mount_type", entity->GetMount());
  1967. // find the visual flags
  1968. int8 vis_flag = 0;
  1969. //Invis + crouch flag check
  1970. if (entity->IsStealthed())
  1971. vis_flag += (INFO_VIS_FLAG_INVIS + INFO_VIS_FLAG_CROUCH);
  1972. //Invis flag check
  1973. else if (entity->IsInvis())
  1974. vis_flag += INFO_VIS_FLAG_INVIS;
  1975. //Mount flag check
  1976. if (entity->GetMount() > 0)
  1977. vis_flag += INFO_VIS_FLAG_MOUNTED;
  1978. //Hide hood check
  1979. if ((IsPlayer() && ((Player*)this)->get_character_flag(CF_HIDE_HOOD)) || appearance.hide_hood)
  1980. vis_flag += INFO_VIS_FLAG_HIDE_HOOD;
  1981. packet->setDataByName("visual_flag", vis_flag);
  1982. packet->setColorByName("mount_saddle_color", entity->GetMountSaddleColor());
  1983. packet->setColorByName("mount_color", entity->GetMountColor());
  1984. packet->setDataByName("hair_type_id", entity->features.hair_type);
  1985. packet->setDataByName("chest_type_id", entity->features.chest_type);
  1986. packet->setDataByName("wing_type_id", entity->features.wing_type);
  1987. packet->setDataByName("legs_type_id", entity->features.legs_type);
  1988. packet->setDataByName("soga_hair_type_id", entity->features.soga_hair_type);
  1989. packet->setDataByName("facial_hair_type_id", entity->features.hair_face_type);
  1990. packet->setDataByName("soga_facial_hair_type_id", entity->features.soga_hair_face_type);
  1991. for (int i = 0; i < 3; i++) {
  1992. packet->setDataByName("eye_type", entity->features.eye_type[i], i);
  1993. packet->setDataByName("ear_type", entity->features.ear_type[i], i);
  1994. packet->setDataByName("eye_brow_type", entity->features.eye_brow_type[i], i);
  1995. packet->setDataByName("cheek_type", entity->features.cheek_type[i], i);
  1996. packet->setDataByName("lip_type", entity->features.lip_type[i], i);
  1997. packet->setDataByName("chin_type", entity->features.chin_type[i], i);
  1998. packet->setDataByName("nose_type", entity->features.nose_type[i], i);
  1999. packet->setDataByName("soga_eye_type", entity->features.soga_eye_type[i], i);
  2000. packet->setDataByName("soga_ear_type", entity->features.soga_ear_type[i], i);
  2001. packet->setDataByName("soga_eye_brow_type", entity->features.soga_eye_brow_type[i], i);
  2002. packet->setDataByName("soga_cheek_type", entity->features.soga_cheek_type[i], i);
  2003. packet->setDataByName("soga_lip_type", entity->features.soga_lip_type[i], i);
  2004. packet->setDataByName("soga_chin_type", entity->features.soga_chin_type[i], i);
  2005. packet->setDataByName("soga_nose_type", entity->features.soga_nose_type[i], i);
  2006. }
  2007. packet->setColorByName("skin_color", entity->features.skin_color);
  2008. packet->setColorByName("eye_color", entity->features.eye_color);
  2009. packet->setColorByName("hair_type_color", entity->features.hair_type_color);
  2010. packet->setColorByName("hair_type_highlight_color", entity->features.hair_type_highlight_color);
  2011. packet->setColorByName("hair_face_color", entity->features.hair_face_color);
  2012. packet->setColorByName("hair_face_highlight_color", entity->features.hair_face_highlight_color);
  2013. packet->setColorByName("hair_highlight", entity->features.hair_highlight_color);
  2014. packet->setColorByName("wing_color1", entity->features.wing_color1);
  2015. packet->setColorByName("wing_color2", entity->features.wing_color2);
  2016. packet->setColorByName("hair_color1", entity->features.hair_color1);
  2017. packet->setColorByName("hair_color2", entity->features.hair_color2);
  2018. packet->setColorByName("soga_skin_color", entity->features.soga_skin_color);
  2019. packet->setColorByName("soga_eye_color", entity->features.soga_eye_color);
  2020. packet->setColorByName("soga_hair_color1", entity->features.soga_hair_color1);
  2021. packet->setColorByName("soga_hair_color2", entity->features.soga_hair_color2);
  2022. packet->setColorByName("soga_hair_type_color", entity->features.soga_hair_type_color);
  2023. packet->setColorByName("soga_hair_type_highlight_color", entity->features.soga_hair_type_highlight_color);
  2024. packet->setColorByName("soga_hair_face_color", entity->features.soga_hair_face_color);
  2025. packet->setColorByName("soga_hair_face_highlight_color", entity->features.soga_hair_face_highlight_color);
  2026. packet->setColorByName("soga_hair_highlight", entity->features.soga_hair_highlight_color);
  2027. packet->setDataByName("body_age", entity->features.body_age);
  2028. }
  2029. else {
  2030. EQ2_Color empty;
  2031. empty.red = 255;
  2032. empty.blue = 255;
  2033. empty.green = 255;
  2034. packet->setColorByName("skin_color", empty);
  2035. packet->setColorByName("eye_color", empty);
  2036. packet->setColorByName("soga_skin_color", empty);
  2037. packet->setColorByName("soga_eye_color", empty);
  2038. }
  2039. if (appearance.icon == 0) {
  2040. if (appearance.attackable == 1)
  2041. appearance.icon = 0;
  2042. else if (appearance.encounter_level > 0)
  2043. appearance.icon = 4;
  2044. else
  2045. appearance.icon = 6;
  2046. }
  2047. // If Coe+ clients modify the values before we send
  2048. // if not then just send the value we have.
  2049. int8 temp_icon = appearance.icon;
  2050. //Check if we need to add the hand icon..
  2051. if ((temp_icon & 6) != 6 && appearance.display_hand_icon) {
  2052. temp_icon |= 6;
  2053. }
  2054. //Icon value 28 for boats, set this without modifying the value
  2055. if (version >= 1188 && temp_icon != 28) {
  2056. if ((temp_icon & 64) > 0) {
  2057. temp_icon -= 64; // remove the DoV value;
  2058. temp_icon += 128; // add the CoE value
  2059. }
  2060. if ((temp_icon & 32) > 0) {
  2061. temp_icon -= 32; // remove the DoV value;
  2062. temp_icon += 64; // add the CoE value
  2063. }
  2064. if ((temp_icon & 4) > 0) {
  2065. temp_icon -= 4; // remove DoV value
  2066. temp_icon += 8; // add the CoE icon
  2067. }
  2068. if ((temp_icon & 6) > 0) {
  2069. temp_icon -= 10; // remove DoV value
  2070. temp_icon += 12; // add the CoE icon
  2071. }
  2072. }
  2073. packet->setDataByName("icon", temp_icon);//appearance.icon);
  2074. int32 temp_activity_status = 0;
  2075. if (!Alive() && GetTotalHP() > 0 && !IsObject() && !IsGroundSpawn())
  2076. temp_activity_status = 1;
  2077. temp_activity_status += (IsNPC() || IsObject() || IsGroundSpawn()) ? 1 << 1 : 0;
  2078. if (version >= 1188) {
  2079. if (IsGroundSpawn() || GetShowHandIcon())
  2080. temp_activity_status += ACTIVITY_STATUS_INTERACTABLE_1188;
  2081. if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
  2082. temp_activity_status += ACTIVITY_STATUS_ROLEPLAYING_1188;
  2083. if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
  2084. temp_activity_status += ACTIVITY_STATUS_ANONYMOUS_1188;
  2085. if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
  2086. temp_activity_status += ACTIVITY_STATUS_LINKDEAD_1188;
  2087. if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
  2088. temp_activity_status += ACTIVITY_STATUS_CAMPING_1188;
  2089. if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
  2090. temp_activity_status += ACTIVITY_STATUS_LFG_1188;
  2091. if ((appearance.activity_status & ACTIVITY_STATUS_LFW) > 0)
  2092. temp_activity_status += ACTIVITY_STATUS_LFW_1188;
  2093. if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
  2094. temp_activity_status += ACTIVITY_STATUS_SOLID_1188;
  2095. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_GAINED) > 0)
  2096. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_GAINED_1188;
  2097. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_REMAINING) > 0)
  2098. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_REMAINING_1188;
  2099. if ((appearance.activity_status & ACTIVITY_STATUS_AFK) > 0)
  2100. temp_activity_status += ACTIVITY_STATUS_AFK_1188;
  2101. if (EngagedInCombat())
  2102. temp_activity_status += ACTIVITY_STATUS_INCOMBAT_1188;
  2103. // if this is either a boat or lift let the client be manipulated by the object
  2104. // doesn't work for DoF client version 546
  2105. if (appearance.icon == 28 || appearance.icon == 12)
  2106. {
  2107. temp_activity_status += ACTIVITY_STATUS_ISTRANSPORT_1188;
  2108. }
  2109. }
  2110. else
  2111. {
  2112. temp_activity_status = appearance.activity_status;
  2113. if(IsNPC())
  2114. temp_activity_status = 0xFF;
  2115. // this only partially fixes lifts in classic 283 client if you move just as the lift starts to move
  2116. if (appearance.icon == 28 || appearance.icon == 12)
  2117. packet->setDataByName("is_transport", 1);
  2118. if (MeetsSpawnAccessRequirements(spawn))
  2119. packet->setDataByName("hand_icon", appearance.display_hand_icon);
  2120. else {
  2121. if ((req_quests_override & 256) > 0)
  2122. packet->setDataByName("hand_icon", 1);
  2123. }
  2124. if (IsPlayer()) {
  2125. if (((Player*)this)->get_character_flag(CF_AFK))
  2126. packet->setDataByName("afk", 1);
  2127. if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
  2128. packet->setDataByName("roleplaying", 1);
  2129. if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
  2130. packet->setDataByName("anonymous", 1);
  2131. if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
  2132. packet->setDataByName("linkdead", 1);
  2133. if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
  2134. packet->setDataByName("camping", 1);
  2135. if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
  2136. packet->setDataByName("lfg", 1);
  2137. }
  2138. if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
  2139. packet->setDataByName("solid_object", 1);
  2140. }
  2141. packet->setDataByName("activity_status", temp_activity_status); //appearance.activity_status);
  2142. // If player and player has a follow target
  2143. if (IsPlayer()) {
  2144. if (((Player*)this)->GetFollowTarget())
  2145. packet->setDataByName("follow_target", ((((Player*)this)->GetIDWithPlayerSpawn(((Player*)this)->GetFollowTarget()) * -1) - 1));
  2146. else
  2147. packet->setDataByName("follow_target", 0xFFFFFFFF);
  2148. }
  2149. if (GetTarget() && GetTarget()->GetTargetable())
  2150. packet->setDataByName("target_id", ((spawn->GetIDWithPlayerSpawn(GetTarget()) * -1) - 1));
  2151. else
  2152. packet->setDataByName("target_id", 0xFFFFFFFF);
  2153. //Send spell effects for target window
  2154. if(IsEntity()){
  2155. InfoStruct* info = ((Entity*)this)->GetInfoStruct();
  2156. int8 i = 0;
  2157. int16 backdrop = 0;
  2158. int16 spell_icon = 0;
  2159. int32 spell_id = 0;
  2160. LuaSpell* spell = 0;
  2161. ((Entity*)this)->GetSpellEffectMutex()->readlock(__FUNCTION__, __LINE__);
  2162. while(i < 30){
  2163. //Change value of spell id for this packet if spell exists
  2164. spell_id = info->spell_effects[i].spell_id;
  2165. if(spell_id > 0)
  2166. spell_id = 0xFFFFFFFF - spell_id;
  2167. else
  2168. spell_id = 0;
  2169. packet->setSubstructDataByName("spell_effects", "spell_id", spell_id, i);
  2170. //Change value of spell icon for this packet if spell exists
  2171. spell_icon = info->spell_effects[i].icon;
  2172. if(spell_icon > 0){
  2173. if(!(spell_icon == 0xFFFF))
  2174. spell_icon = 0xFFFF - spell_icon;
  2175. }
  2176. else
  2177. spell_icon = 0;
  2178. packet->setSubstructDataByName("spell_effects", "spell_icon", spell_icon, i);
  2179. //Change backdrop values to match values in this packet
  2180. backdrop = info->spell_effects[i].icon_backdrop;
  2181. switch(backdrop){
  2182. case 312:
  2183. backdrop = 33080;
  2184. break;
  2185. case 313:
  2186. backdrop = 33081;
  2187. break;
  2188. case 314:
  2189. backdrop = 33082;
  2190. break;
  2191. case 315:
  2192. backdrop = 33083;
  2193. break;
  2194. case 316:
  2195. backdrop = 33084;
  2196. break;
  2197. case 317:
  2198. backdrop = 33085;
  2199. break;
  2200. case (318 || 319):
  2201. backdrop = 33086;
  2202. break;
  2203. default:
  2204. break;
  2205. }
  2206. packet->setSubstructDataByName("spell_effects", "spell_icon_backdrop", backdrop, i);
  2207. spell = info->spell_effects[i].spell;
  2208. if (spell)
  2209. packet->setSubstructDataByName("spell_effects", "spell_triggercount", spell->num_triggers, i);
  2210. i++;
  2211. }
  2212. ((Entity*)this)->GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
  2213. }
  2214. }
  2215. void Spawn::MoveToLocation(Spawn* spawn, float distance, bool immediate, bool mapped){
  2216. if(!spawn)
  2217. return;
  2218. SetRunningTo(spawn);
  2219. FaceTarget(spawn);
  2220. if (!IsPlayer() && distance > 0.0f)
  2221. {
  2222. if (IsFlying() && CheckLoS(spawn))
  2223. {
  2224. if (immediate)
  2225. ClearRunningLocations();
  2226. AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", true);
  2227. }
  2228. else if (/*!mapped && */GetZone())
  2229. {
  2230. GetZone()->movementMgr->NavigateTo((Entity*)this, spawn->GetX(), spawn->GetY(), spawn->GetZ());
  2231. last_grid_update = Timer::GetCurrentTime2();
  2232. }
  2233. else
  2234. {
  2235. if (immediate)
  2236. ClearRunningLocations();
  2237. AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", mapped);
  2238. }
  2239. }
  2240. }
  2241. void Spawn::ProcessMovement(bool isSpawnListLocked){
  2242. CheckProximities();
  2243. if(IsPlayer()){
  2244. //Check if player is riding a boat, if so update pos (boat's current location + XYZ offsets)
  2245. Player* player = ((Player*)this);
  2246. int32 boat_id = player->GetBoatSpawn();
  2247. Spawn* boat = 0;
  2248. if(boat_id > 0)
  2249. boat = GetZone()->GetSpawnByID(boat_id, isSpawnListLocked);
  2250. //TODO: MAYBE do this for real boats, not lifts... GetWidgetTypeNameByTypeID
  2251. /*if(boat){
  2252. SetX(boat->GetX() + player->GetBoatX());
  2253. SetY(boat->GetY() + player->GetBoatY());
  2254. SetZ(boat->GetZ() + player->GetBoatZ());
  2255. }*/
  2256. return;
  2257. }
  2258. if (forceMapCheck && GetZone() != nullptr && zone->zonemap != nullptr && zone->zonemap->IsMapLoaded())
  2259. {
  2260. FixZ(true);
  2261. int32 newGrid = GetZone()->Grid->GetGridID(this);
  2262. if (!IsFlying() && newGrid != 0 && newGrid != appearance.pos.grid_id)
  2263. SetPos(&(appearance.pos.grid_id), newGrid);
  2264. forceMapCheck = false;
  2265. }
  2266. if (GetHP() <= 0 && !IsWidget())
  2267. return;
  2268. if (EngagedInCombat())
  2269. {
  2270. int locations = 0;
  2271. if (movement_locations && MMovementLocations)
  2272. {
  2273. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2274. locations = movement_locations->size();
  2275. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2276. }
  2277. if (locations < 1 && GetZone() && ((Entity*)this)->IsFeared())
  2278. {
  2279. CalculateNewFearpoint();
  2280. }
  2281. }
  2282. MMovementLoop.lock();
  2283. Spawn* followTarget = GetZone()->GetSpawnByID(m_followTarget, isSpawnListLocked);
  2284. if (!followTarget && m_followTarget > 0)
  2285. m_followTarget = 0;
  2286. if (following && followTarget && !((Entity*)this)->IsFeared()) {
  2287. // Need to clear m_followTarget before the zoneserver deletes it
  2288. if (followTarget->GetHP() <= 0) {
  2289. followTarget = 0;
  2290. MMovementLoop.unlock();
  2291. return;
  2292. }
  2293. if (!IsEntity() || (!((Entity*)this)->IsCasting() && !((Entity*)this)->IsMezzedOrStunned() && !((Entity*)this)->IsRooted())) {
  2294. if (GetBaseSpeed() > 0) {
  2295. CalculateRunningLocation();
  2296. }
  2297. else {
  2298. float speed = 4.0f;
  2299. if (IsEntity())
  2300. speed = ((Entity*)this)->GetMaxSpeed();
  2301. SetSpeed(speed);
  2302. }
  2303. MovementLocation* loc = GetCurrentRunningLocation();
  2304. float dist = GetDistance(followTarget, true);
  2305. if ((!EngagedInCombat() && m_followDistance > 0 && dist <= m_followDistance) ||
  2306. (dist <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
  2307. ClearRunningLocations();
  2308. CalculateRunningLocation(true);
  2309. }
  2310. else if (loc) {
  2311. float distance = GetDistance(followTarget, loc->x, loc->y, loc->z);
  2312. if ( (!EngagedInCombat() && m_followDistance > 0 && distance > m_followDistance) ||
  2313. ( EngagedInCombat() && distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
  2314. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat(), true, loc->mapped);
  2315. CalculateRunningLocation();
  2316. }
  2317. }
  2318. else {
  2319. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat());
  2320. CalculateRunningLocation();
  2321. }
  2322. }
  2323. }
  2324. // Movement loop is only for scripted paths
  2325. else if(!EngagedInCombat() && !NeedsToResumeMovement() && movement_loop.size() > 0 && movement_index < movement_loop.size() && (!IsNPC() || !((NPC*)this)->m_runningBack)){
  2326. // Get the target location
  2327. MovementData* data = movement_loop[movement_index];
  2328. // need to resume our movement
  2329. if(resume_movement){
  2330. if (movement_locations){
  2331. while (movement_locations->size()){
  2332. safe_delete(movement_locations->front());
  2333. movement_locations->pop_front();
  2334. }
  2335. movement_locations->clear();
  2336. }
  2337. data = movement_loop[movement_index];
  2338. ((Entity*)this)->SetSpeed(data->speed);
  2339. SetSpeed(data->speed);
  2340. if(!IsWidget())
  2341. FaceTarget(data->x, data->z);
  2342. // 0 delay at target location, need to set multiple locations
  2343. if(data->delay == 0 && movement_loop.size() > 0) {
  2344. int16 tmp_index = movement_index+1;
  2345. MovementData* data2 = 0;
  2346. if(tmp_index < movement_loop.size())
  2347. data2 = movement_loop[tmp_index];
  2348. else
  2349. data2 = movement_loop[0];
  2350. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  2351. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  2352. }
  2353. // delay at target location, only need to set 1 location
  2354. else
  2355. AddRunningLocation(data->x, data->y, data->z, data->speed);
  2356. movement_start_time = 0;
  2357. resume_movement = false;
  2358. }
  2359. // If we are not moving or we have arrived at our destination
  2360. else if(!IsRunning() || (data && data->x == GetX() && data->y == GetY() && data->z == GetZ())){
  2361. // If we were moving remove the last running location (the point we just arrived at)
  2362. if(IsRunning())
  2363. RemoveRunningLocation();
  2364. // If this waypoint has a delay and we just arrived here (movement_start_time == 0)
  2365. if(data->delay > 0 && movement_start_time == 0){
  2366. // Set the current time
  2367. movement_start_time = Timer::GetCurrentTime2();
  2368. // If this waypoint had a lua function then call it
  2369. if(data->lua_function.length() > 0)
  2370. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2371. int16 nextMove;
  2372. if ((int16)(movement_index + 1) < movement_loop.size())
  2373. nextMove = movement_index + 1;
  2374. else
  2375. nextMove = 0;
  2376. // Get the next target location
  2377. data = movement_loop[nextMove];
  2378. //Go ahead and face the next location
  2379. FaceTarget(data->x, data->z);
  2380. }
  2381. // If this waypoint has no delay or we have waited the required time (current time >= delay + movement_start_time)
  2382. else if(data->delay == 0 || (data->delay > 0 && Timer::GetCurrentTime2() >= (data->delay+movement_start_time))) {
  2383. // if no delay at this waypoint but a lua function for it then call the function
  2384. if(data->delay == 0 && data->lua_function.length() > 0)
  2385. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2386. // Advance the current movement loop index
  2387. if((int16)(movement_index+1) < movement_loop.size())
  2388. movement_index++;
  2389. else
  2390. movement_index = 0;
  2391. // Get the next target location
  2392. data = movement_loop[movement_index];
  2393. // set the speed for that location
  2394. SetSpeed(data->speed);
  2395. if(!IsWidget())
  2396. // turn towards the location
  2397. FaceTarget(data->x, data->z);
  2398. // If 0 delay at location get and set data for the point after it
  2399. if(data->delay == 0 && movement_loop.size() > 0){
  2400. while (movement_locations->size()){
  2401. safe_delete(movement_locations->front());
  2402. movement_locations->pop_front();
  2403. }
  2404. // clear current target locations
  2405. movement_locations->clear();
  2406. // get the data for the location after out new location
  2407. int16 tmp_index = movement_index+1;
  2408. MovementData* data2 = 0;
  2409. if(tmp_index < movement_loop.size())
  2410. data2 = movement_loop[tmp_index];
  2411. else
  2412. data2 = movement_loop[0];
  2413. // set the first location (adds it to movement_locations that we just cleared)
  2414. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  2415. // set the location after that
  2416. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  2417. }
  2418. // there is a delay at the next location so we only need to set it
  2419. else
  2420. AddRunningLocation(data->x, data->y, data->z, data->speed);
  2421. // reset this timer to 0 now that we are moving again
  2422. movement_start_time = 0;
  2423. }
  2424. }
  2425. // moving and not at target location yet
  2426. else if(GetBaseSpeed() > 0)
  2427. CalculateRunningLocation();
  2428. // not moving, have a target location but not at it yet
  2429. else if (data) {
  2430. SetSpeed(data->speed);
  2431. AddRunningLocation(data->x, data->y, data->z, data->speed);
  2432. }
  2433. }
  2434. else if (IsRunning()) {
  2435. CalculateRunningLocation();
  2436. }
  2437. /*else if (IsNPC() && !IsRunning() && !EngagedInCombat() && ((NPC*)this)->GetRunbackLocation()) {
  2438. // Is an npc that is not moving and not engaged in combat but has a run back location set then clear the runback location
  2439. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Clear runback location for %s", GetName());
  2440. ((NPC*)this)->ClearRunback();
  2441. resume_movement = true;
  2442. NeedsToResumeMovement(false);
  2443. }*/
  2444. MMovementLoop.unlock();
  2445. }
  2446. void Spawn::ResetMovement(){
  2447. MMovementLoop.lock();
  2448. vector<MovementData*>::iterator itr;
  2449. for(itr = movement_loop.begin(); itr != movement_loop.end(); itr++){
  2450. safe_delete(*itr);
  2451. }
  2452. MMovementLoop.unlock();
  2453. resume_movement = true;
  2454. movement_index = 0;
  2455. }
  2456. void Spawn::AddMovementLocation(float x, float y, float z, float speed, int16 delay, const char* lua_function){
  2457. LogWrite(LUA__DEBUG, 5, "LUA", "AddMovementLocation: x: %.2f, y: %.2f, z: %.2f, speed: %.2f, delay: %i, lua: %s",
  2458. x, y, z, speed, delay, string(lua_function).c_str());
  2459. MovementData* data = new MovementData;
  2460. data->x = x;
  2461. data->y = y;
  2462. data->z = z;
  2463. data->speed = speed;
  2464. data->delay = delay*1000;
  2465. if(lua_function)
  2466. data->lua_function = string(lua_function);
  2467. MMovementLoop.lock();
  2468. movement_loop.push_back(data);
  2469. MMovementLoop.unlock();
  2470. }
  2471. bool Spawn::IsRunning(){
  2472. if(movement_locations && movement_locations->size() > 0)
  2473. return true;
  2474. else
  2475. return false;
  2476. }
  2477. void Spawn::RunToLocation(float x, float y, float z, float following_x, float following_y, float following_z){
  2478. if(!IsWidget())
  2479. FaceTarget(x, z);
  2480. SetPos(&appearance.pos.X2, x, false);
  2481. SetPos(&appearance.pos.Z2, z, false);
  2482. SetPos(&appearance.pos.Y2, y, false);
  2483. if(following_x == 0 && following_y == 0 && following_z == 0){
  2484. SetPos(&appearance.pos.X3, x, false);
  2485. SetPos(&appearance.pos.Z3, z, false);
  2486. SetPos(&appearance.pos.Y3, y, false);
  2487. }
  2488. else{
  2489. SetPos(&appearance.pos.X3, following_x, false);
  2490. SetPos(&appearance.pos.Y3, following_y, false);
  2491. SetPos(&appearance.pos.Z3, following_z, false);
  2492. }
  2493. position_changed = true;
  2494. changed = true;
  2495. GetZone()->AddChangedSpawn(this);
  2496. }
  2497. MovementLocation* Spawn::GetCurrentRunningLocation(){
  2498. MovementLocation* ret = 0;
  2499. if(movement_locations && movement_locations->size() > 0){
  2500. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2501. ret = movement_locations->front();
  2502. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2503. }
  2504. return ret;
  2505. }
  2506. MovementLocation* Spawn::GetLastRunningLocation(){
  2507. MovementLocation* ret = 0;
  2508. if(movement_locations && movement_locations->size() > 0){
  2509. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2510. ret = movement_locations->back();
  2511. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2512. }
  2513. return ret;
  2514. }
  2515. 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){
  2516. if(speed == 0)
  2517. return;
  2518. ((Entity*)this)->SetSpeed(speed);
  2519. MovementLocation* current_location = 0;
  2520. float distance = GetDistance(x, y, z, distance_away != 0);
  2521. if(distance_away != 0){
  2522. distance -= distance_away;
  2523. x = x - (GetX() - x)*distance_away/distance;
  2524. z = z - (GetZ() - z)*distance_away/distance;
  2525. }
  2526. if(!movement_locations){
  2527. movement_locations = new deque<MovementLocation*>();
  2528. MMovementLocations = new Mutex();
  2529. }
  2530. MovementLocation* data = new MovementLocation;
  2531. data->mapped = isMapped;
  2532. data->x = x;
  2533. data->y = y;
  2534. data->z = z;
  2535. data->speed = speed;
  2536. data->attackable = attackable;
  2537. data->lua_function = lua_function;
  2538. data->gridid = 0; // used for runback defaults
  2539. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2540. if(movement_locations->size() > 0)
  2541. current_location = movement_locations->back();
  2542. if(!current_location){
  2543. SetSpawnOrigX(GetX());
  2544. SetSpawnOrigY(GetY());
  2545. SetSpawnOrigZ(GetZ());
  2546. SetSpawnOrigHeading(GetHeading());
  2547. }
  2548. movement_locations->push_back(data);
  2549. if(finished_adding_locations){
  2550. current_location = movement_locations->front();
  2551. SetSpeed(current_location->speed);
  2552. if(movement_locations->size() > 1){
  2553. data = movement_locations->at(1);
  2554. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2555. }
  2556. else
  2557. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2558. }
  2559. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2560. }
  2561. bool Spawn::RemoveRunningLocation(){
  2562. bool ret = false;
  2563. if(movement_locations){
  2564. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2565. if(movement_locations->size() > 0){
  2566. delete movement_locations->front();
  2567. movement_locations->pop_front();
  2568. ret = true;
  2569. }
  2570. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2571. }
  2572. return ret;
  2573. }
  2574. void Spawn::ClearRunningLocations(){
  2575. while(RemoveRunningLocation()){}
  2576. }
  2577. bool Spawn::CalculateChange(){
  2578. bool remove_needed = false;
  2579. if(movement_locations && MMovementLocations){
  2580. MovementLocation* data = 0;
  2581. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2582. if(movement_locations->size() > 0){
  2583. // Target location
  2584. data = movement_locations->front();
  2585. // If no target or we are at the target location need to remove this point
  2586. if(!data || (data->x == GetX() && data->y == GetY() && data->z == GetZ()))
  2587. remove_needed = true;
  2588. if(data){
  2589. if(NeedsToResumeMovement()){
  2590. resume_movement = true;
  2591. NeedsToResumeMovement(false);
  2592. }
  2593. if(!data->attackable)
  2594. SetHeading(GetSpawnOrigHeading());
  2595. }
  2596. }
  2597. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2598. if(remove_needed) {
  2599. if (data && data->lua_function.length() > 0)
  2600. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2601. RemoveRunningLocation();
  2602. //CalculateChange();
  2603. }
  2604. else if(data){
  2605. // Speed is per second so we need a time_step (amount of time since the last update) to modify movement by
  2606. 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
  2607. // Get current location
  2608. float nx = GetX();
  2609. float ny = GetY();
  2610. float nz = GetZ();
  2611. // Get Forward vecotr
  2612. float tar_vx = data->x - nx;
  2613. float tar_vy = data->y - ny;
  2614. float tar_vz = data->z - nz;
  2615. // Multiply speed by the time_step to get how much should have changed over the last tick
  2616. float speed = GetSpeed() * time_step;
  2617. // Normalize the forward vector and multiply by speed, this gives us our change in coords, just need to add them to our current coords
  2618. float len = sqrtf(tar_vx * tar_vx + tar_vy * tar_vy + tar_vz * tar_vz);
  2619. tar_vx = (tar_vx / len) * speed;
  2620. tar_vy = (tar_vy / len) * speed;
  2621. tar_vz = (tar_vz / len) * speed;
  2622. // Distance less then 0.5 just set the npc to the target location
  2623. if (GetDistance(data->x, data->y, data->z, IsWidget() ? false : true) <= 0.5f) {
  2624. SetX(data->x, false);
  2625. SetZ(data->z, false);
  2626. SetY(data->y, false, true);
  2627. }
  2628. else {
  2629. SetX(nx + tar_vx, false);
  2630. SetZ(nz + tar_vz, false);
  2631. if ( IsWidget() )
  2632. SetY(ny + tar_vy, false, true);
  2633. else
  2634. SetY(ny + tar_vy, false);
  2635. }
  2636. if (GetZone()->Grid != nullptr) {
  2637. Cell* newCell = GetZone()->Grid->GetCell(GetX(), GetZ());
  2638. if (newCell != Cell_Info.CurrentCell) {
  2639. GetZone()->Grid->RemoveSpawnFromCell(this);
  2640. GetZone()->Grid->AddSpawn(this, newCell);
  2641. }
  2642. int32 newGrid = GetZone()->Grid->GetGridID(this);
  2643. if (!IsFlying() && newGrid != 0 && newGrid != appearance.pos.grid_id)
  2644. SetPos(&(appearance.pos.grid_id), newGrid);
  2645. }
  2646. }
  2647. }
  2648. return remove_needed;
  2649. }
  2650. void Spawn::CalculateRunningLocation(bool stop){
  2651. if (!stop && (last_location_update + 100) > Timer::GetCurrentTime2())
  2652. return;
  2653. else if (!stop)
  2654. last_location_update = Timer::GetCurrentTime2();
  2655. bool removed = CalculateChange();
  2656. if (stop) {
  2657. //following = false;
  2658. SetPos(&appearance.pos.X2, GetX(), false);
  2659. SetPos(&appearance.pos.Y2, GetY(), false);
  2660. SetPos(&appearance.pos.Z2, GetZ(), false);
  2661. SetPos(&appearance.pos.X3, GetX(), false);
  2662. SetPos(&appearance.pos.Y3, GetY(), false);
  2663. SetPos(&appearance.pos.Z3, GetZ(), false);
  2664. }
  2665. else if (removed && movement_locations && movement_locations->size() > 0) {
  2666. MovementLocation* current_location = movement_locations->at(0);
  2667. if (movement_locations->size() > 1) {
  2668. MovementLocation* data = movement_locations->at(1);
  2669. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2670. }
  2671. else
  2672. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2673. }
  2674. else if (GetZone() && GetTarget() != NULL && EngagedInCombat())
  2675. {
  2676. if (GetDistance(GetTarget()) > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  2677. {
  2678. if (IsFlying() && CheckLoS(GetTarget()))
  2679. AddRunningLocation(GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ(), GetSpeed(), 0, false);
  2680. else
  2681. GetZone()->movementMgr->NavigateTo((Entity*)this, GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ());
  2682. }
  2683. else
  2684. ((Entity*)this)->HaltMovement();
  2685. }
  2686. }
  2687. float Spawn::GetFaceTarget(float x, float z) {
  2688. float angle;
  2689. double diff_x = x - GetX();
  2690. double diff_z = z - GetZ();
  2691. //If we're very close to the same spot don't bother changing heading
  2692. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  2693. return GetHeading();
  2694. }
  2695. if (diff_z == 0) {
  2696. if (diff_x > 0)
  2697. angle = 90;
  2698. else
  2699. angle = 270;
  2700. }
  2701. else
  2702. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  2703. if (angle < 0)
  2704. angle = angle + 360;
  2705. else
  2706. angle = angle + 180;
  2707. if (diff_x < 0)
  2708. angle = angle + 180;
  2709. if (last_heading_angle == angle) return angle;
  2710. return angle;
  2711. }
  2712. void Spawn::FaceTarget(float x, float z){
  2713. float angle;
  2714. double diff_x = x - GetX();
  2715. double diff_z = z - GetZ();
  2716. //If we're very close to the same spot don't bother changing heading
  2717. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  2718. return;
  2719. }
  2720. if(diff_z==0){
  2721. if(diff_x > 0)
  2722. angle = 90;
  2723. else
  2724. angle = 270;
  2725. }
  2726. else
  2727. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  2728. if(angle < 0)
  2729. angle = angle + 360;
  2730. else
  2731. angle = angle + 180;
  2732. if(diff_x < 0)
  2733. angle = angle + 180;
  2734. if (last_heading_angle == angle) return;
  2735. SetHeading(angle);
  2736. }
  2737. void Spawn::FaceTarget(Spawn* target){
  2738. if(!target)
  2739. return;
  2740. FaceTarget(target->GetX(), target->GetZ());
  2741. if(GetHP() > 0 && target->IsPlayer() && !EngagedInCombat()){
  2742. GetZone()->AddHeadingTimer(this);
  2743. SetTempActionState(0);
  2744. }
  2745. }
  2746. bool Spawn::MeetsSpawnAccessRequirements(Player* player){
  2747. bool ret = false;
  2748. Quest* quest = 0;
  2749. //Check if we meet all quest requirements first..
  2750. m_requiredQuests.readlock(__FUNCTION__, __LINE__);
  2751. if (player && required_quests.size() > 0) {
  2752. map<int32, vector<int16>* >::iterator itr;
  2753. for (itr = required_quests.begin(); itr != required_quests.end(); itr++) {
  2754. player->AddQuestRequiredSpawn(this, itr->first);
  2755. vector<int16>* quest_steps = itr->second;
  2756. for (int32 i = 0; i < quest_steps->size(); i++) {
  2757. quest = player->GetQuest(itr->first);
  2758. if (req_quests_continued_access) {
  2759. if (quest) {
  2760. if (quest->GetQuestStepCompleted(quest_steps->at(i))) {
  2761. ret = true;
  2762. break;
  2763. }
  2764. }
  2765. else if (player->GetCompletedQuest(itr->first)) {
  2766. ret = true;
  2767. break;
  2768. }
  2769. }
  2770. if (quest && quest->QuestStepIsActive(quest_steps->at(i))) {
  2771. ret = true;
  2772. break;
  2773. }
  2774. }
  2775. }
  2776. }
  2777. else
  2778. ret = true;
  2779. m_requiredQuests.releasereadlock(__FUNCTION__, __LINE__);
  2780. if (!ret)
  2781. return ret;
  2782. //Now check if the player meets all history requirements
  2783. m_requiredHistory.readlock(__FUNCTION__, __LINE__);
  2784. if (required_history.size() > 0){
  2785. map<int32, LUAHistory>::iterator itr;
  2786. for (itr = required_history.begin(); itr != required_history.end(); itr++){
  2787. player->AddHistoryRequiredSpawn(this, itr->first);
  2788. LUAHistory* player_history = player->GetLUAHistory(itr->first);
  2789. if (player_history){
  2790. if (player_history->Value != itr->second.Value || player_history->Value2 != itr->second.Value2)
  2791. ret = false;
  2792. }
  2793. else
  2794. ret = false;
  2795. if (!ret)
  2796. break;
  2797. }
  2798. }
  2799. m_requiredHistory.releasereadlock(__FUNCTION__, __LINE__);
  2800. return ret;
  2801. }
  2802. vector<Spawn*>* Spawn::GetSpawnGroup(){
  2803. vector<Spawn*>* ret_list = 0;
  2804. if(spawn_group_list){
  2805. ret_list = new vector<Spawn*>();
  2806. if(MSpawnGroup)
  2807. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  2808. ret_list->insert(ret_list->begin(), spawn_group_list->begin(), spawn_group_list->end());
  2809. if(MSpawnGroup)
  2810. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  2811. }
  2812. return ret_list;
  2813. }
  2814. bool Spawn::HasSpawnGroup() {
  2815. return spawn_group_list && spawn_group_list->size() > 0;
  2816. }
  2817. bool Spawn::IsInSpawnGroup(Spawn* spawn) {
  2818. bool ret = false;
  2819. if (HasSpawnGroup() && spawn) {
  2820. vector<Spawn*>::iterator itr;
  2821. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  2822. if ((*itr) == spawn) {
  2823. ret = true;
  2824. break;
  2825. }
  2826. }
  2827. }
  2828. return ret;
  2829. }
  2830. void Spawn::AddSpawnToGroup(Spawn* spawn){
  2831. if(!spawn)
  2832. return;
  2833. if(!spawn_group_list){
  2834. spawn_group_list = new vector<Spawn*>();
  2835. spawn_group_list->push_back(this);
  2836. safe_delete(MSpawnGroup);
  2837. MSpawnGroup = new Mutex();
  2838. MSpawnGroup->SetName("Spawn::MSpawnGroup");
  2839. }
  2840. vector<Spawn*>::iterator itr;
  2841. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2842. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2843. if((*itr) == spawn){
  2844. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2845. return;
  2846. }
  2847. }
  2848. spawn_group_list->push_back(spawn);
  2849. spawn->SetSpawnGroupList(spawn_group_list, MSpawnGroup);
  2850. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2851. }
  2852. void Spawn::SetSpawnGroupList(vector<Spawn*>* list, Mutex* mutex){
  2853. spawn_group_list = list;
  2854. MSpawnGroup = mutex;
  2855. }
  2856. void Spawn::RemoveSpawnFromGroup(bool erase_all){
  2857. SetSpawnGroupID(0);
  2858. bool del = false;
  2859. if(MSpawnGroup){
  2860. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2861. if(spawn_group_list){
  2862. vector<Spawn*>::iterator itr;
  2863. Spawn* spawn = 0;
  2864. if(spawn_group_list->size() == 1)
  2865. erase_all = true;
  2866. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2867. spawn = *itr;
  2868. if (spawn) {
  2869. if(!erase_all){
  2870. if(spawn == this){
  2871. spawn_group_list->erase(itr);
  2872. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2873. spawn_group_list = 0;
  2874. MSpawnGroup = 0;
  2875. return;
  2876. }
  2877. }
  2878. else{
  2879. if (spawn != this)
  2880. spawn->SetSpawnGroupList(0, 0);
  2881. }
  2882. }
  2883. }
  2884. if (erase_all)
  2885. spawn_group_list->clear();
  2886. del = (spawn_group_list->size() == 0);
  2887. }
  2888. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2889. if (del){
  2890. safe_delete(MSpawnGroup);
  2891. safe_delete(spawn_group_list);
  2892. }
  2893. }
  2894. }
  2895. void Spawn::SetSpawnGroupID(int32 id){
  2896. group_id = id;
  2897. }
  2898. int32 Spawn::GetSpawnGroupID(){
  2899. return group_id;
  2900. }
  2901. void Spawn::AddChangedZoneSpawn(){
  2902. if(send_spawn_changes && GetZone())
  2903. GetZone()->AddChangedSpawn(this);
  2904. }
  2905. void Spawn::RemoveSpawnAccess(Spawn* spawn) {
  2906. if (allowed_access.count(spawn->GetID()) > 0) {
  2907. allowed_access.erase(spawn->GetID());
  2908. GetZone()->HidePrivateSpawn(this);
  2909. }
  2910. }
  2911. void Spawn::SetFollowTarget(Spawn* spawn, int32 follow_distance) {
  2912. if (spawn && spawn != this) {
  2913. m_followTarget = spawn->GetID();
  2914. m_followDistance = follow_distance;
  2915. }
  2916. else {
  2917. m_followTarget = 0;
  2918. if (following)
  2919. following = false;
  2920. m_followDistance = 0;
  2921. }
  2922. }
  2923. void Spawn::AddTempVariable(string var, string val) {
  2924. m_tempVariableTypes[var] = 5;
  2925. m_tempVariables[var] = val;
  2926. }
  2927. void Spawn::AddTempVariable(string var, Spawn* val) {
  2928. m_tempVariableTypes[var] = 1;
  2929. m_tempVariableSpawn[var] = val->GetID();
  2930. }
  2931. void Spawn::AddTempVariable(string var, ZoneServer* val) {
  2932. m_tempVariableTypes[var] = 2;
  2933. m_tempVariableZone[var] = val;
  2934. }
  2935. void Spawn::AddTempVariable(string var, Item* val) {
  2936. m_tempVariableTypes[var] = 3;
  2937. m_tempVariableItem[var] = val;
  2938. }
  2939. void Spawn::AddTempVariable(string var, Quest* val) {
  2940. m_tempVariableTypes[var] = 4;
  2941. m_tempVariableQuest[var] = val;
  2942. }
  2943. string Spawn::GetTempVariable(string var) {
  2944. string ret = "";
  2945. if (m_tempVariables.count(var) > 0)
  2946. ret = m_tempVariables[var];
  2947. return ret;
  2948. }
  2949. Spawn* Spawn::GetTempVariableSpawn(string var) {
  2950. Spawn* ret = 0;
  2951. if (m_tempVariableSpawn.count(var) > 0)
  2952. ret = GetZone()->GetSpawnByID(m_tempVariableSpawn[var]);
  2953. return ret;
  2954. }
  2955. ZoneServer* Spawn::GetTempVariableZone(string var) {
  2956. ZoneServer* ret = 0;
  2957. if (m_tempVariableZone.count(var) > 0)
  2958. ret = m_tempVariableZone[var];
  2959. return ret;
  2960. }
  2961. Item* Spawn::GetTempVariableItem(string var) {
  2962. Item* ret = 0;
  2963. if (m_tempVariableItem.count(var) > 0)
  2964. ret = m_tempVariableItem[var];
  2965. return ret;
  2966. }
  2967. Quest* Spawn::GetTempVariableQuest(string var) {
  2968. Quest* ret = 0;
  2969. if (m_tempVariableQuest.count(var) > 0)
  2970. ret = m_tempVariableQuest[var];
  2971. return ret;
  2972. }
  2973. int8 Spawn::GetTempVariableType(string var) {
  2974. int8 ret = 0;
  2975. if (m_tempVariableTypes.count(var) > 0)
  2976. ret = m_tempVariableTypes[var];
  2977. return ret;
  2978. }
  2979. void Spawn::DeleteTempVariable(string var) {
  2980. int8 type = GetTempVariableType(var);
  2981. switch (type) {
  2982. case 1:
  2983. m_tempVariableSpawn.erase(var);
  2984. break;
  2985. case 2:
  2986. m_tempVariableZone.erase(var);
  2987. break;
  2988. case 3:
  2989. m_tempVariableItem.erase(var);
  2990. break;
  2991. case 4:
  2992. m_tempVariableQuest.erase(var);
  2993. break;
  2994. case 5:
  2995. m_tempVariables.erase(var);
  2996. break;
  2997. }
  2998. m_tempVariableTypes.erase(var);
  2999. }
  3000. Spawn* Spawn::GetRunningTo() {
  3001. return GetZone()->GetSpawnByID(running_to);
  3002. }
  3003. Spawn* Spawn::GetFollowTarget() {
  3004. return GetZone()->GetSpawnByID(m_followTarget);
  3005. }
  3006. void Spawn::CopySpawnAppearance(Spawn* spawn){
  3007. if (!spawn)
  3008. return;
  3009. //This function copies the appearace of the provided spawn to this one
  3010. if (spawn->IsEntity() && IsEntity()){
  3011. memcpy(&((Entity*)this)->features, &((Entity*)spawn)->features, sizeof(CharFeatures));
  3012. memcpy(&((Entity*)this)->equipment, &((Entity*)spawn)->equipment, sizeof(EQ2_Equipment));
  3013. }
  3014. SetSize(spawn->GetSize());
  3015. SetModelType(spawn->GetModelType());
  3016. }
  3017. void Spawn::SetY(float y, bool updateFlags, bool disableYMapCheck)
  3018. {
  3019. SetPos(&appearance.pos.Y, y, updateFlags);
  3020. if (!disableYMapCheck)
  3021. FixZ();
  3022. }
  3023. float Spawn::FindDestGroundZ(glm::vec3 dest, float z_offset)
  3024. {
  3025. float best_z = BEST_Z_INVALID;
  3026. if (GetZone() != nullptr && GetZone()->zonemap != nullptr)
  3027. {
  3028. dest.z += z_offset;
  3029. best_z = zone->zonemap->FindBestZ(dest, nullptr);
  3030. }
  3031. return best_z;
  3032. }
  3033. float Spawn::GetFixedZ(const glm::vec3& destination, int32 z_find_offset) {
  3034. BenchTimer timer;
  3035. timer.reset();
  3036. float new_z = destination.z;
  3037. if (GetZone() != nullptr && zone->zonemap != nullptr) {
  3038. /* if (flymode == GravityBehavior::Flying)
  3039. return new_z;
  3040. */
  3041. /* if (zone->HasWaterMap() && zone->watermap->InLiquid(glm::vec3(m_Position)))
  3042. return new_z;
  3043. */
  3044. /*
  3045. * Any more than 5 in the offset makes NPC's hop/snap to ceiling in small corridors
  3046. */
  3047. new_z = this->FindDestGroundZ(destination, z_find_offset);
  3048. if (new_z != BEST_Z_INVALID) {
  3049. if (new_z < -2000) {
  3050. new_z = GetY();
  3051. }
  3052. }
  3053. auto duration = timer.elapsed();
  3054. LogWrite(MAP__DEBUG, 0, "Map", "Mob::GetFixedZ() ([{%s}]) returned [{%f}] at [{%f}], [{%f}], [{%f}] - Took [{%f}]",
  3055. this->GetName(),
  3056. new_z,
  3057. destination.x,
  3058. destination.y,
  3059. destination.z,
  3060. duration);
  3061. }
  3062. return new_z;
  3063. }
  3064. void Spawn::FixZ(bool forceUpdate) {
  3065. if (IsPlayer() || IsFlying() || !GetZone() || (IsObject() && GetZone()->GetInstanceType() == Instance_Type::PERSONAL_HOUSE_INSTANCE)) {
  3066. return;
  3067. }
  3068. /*
  3069. if (flymode == GravityBehavior::Flying) {
  3070. return;
  3071. }*/
  3072. /*
  3073. if (zone->watermap && zone->watermap->InLiquid(m_Position)) {
  3074. return;
  3075. }*/
  3076. glm::vec3 current_loc(GetX(), GetZ(), GetY());
  3077. float new_z = GetFixedZ(current_loc, 1);
  3078. if (new_z == GetY())
  3079. return;
  3080. if ((new_z > -2000) && new_z != BEST_Z_INVALID) {
  3081. SetY(new_z, forceUpdate, true);
  3082. }
  3083. else {
  3084. LogWrite(MAP__DEBUG, 0, "Map", "[{%s}] is failing to find Z [{%f}]", this->GetName(), std::abs(GetY() - new_z));
  3085. }
  3086. }
  3087. bool Spawn::CheckLoS(Spawn* target)
  3088. {
  3089. glm::vec3 targpos(target->GetX(), target->GetZ(), target->GetY()+1.0f);
  3090. glm::vec3 pos(GetX(), GetZ(), GetY()+1.0f);
  3091. return CheckLoS(pos, targpos);
  3092. }
  3093. bool Spawn::CheckLoS(glm::vec3 myloc, glm::vec3 oloc)
  3094. {
  3095. ZoneServer* zone = GetZone();
  3096. if (zone == NULL || zone->zonemap == NULL || !zone->zonemap->IsMapLoaded())
  3097. return true;
  3098. else
  3099. return zone->zonemap->CheckLoS(myloc, oloc);
  3100. return false;
  3101. }
  3102. void Spawn::CalculateNewFearpoint()
  3103. {
  3104. if (GetZone() && GetZone()->pathing) {
  3105. auto Node = zone->pathing->GetRandomLocation(glm::vec3(GetX(), GetZ(), GetY()));
  3106. if (Node.x != 0.0f || Node.y != 0.0f || Node.z != 0.0f) {
  3107. AddRunningLocation(Node.x, Node.y, Node.z, GetSpeed(), 0, true, true, "", true);
  3108. }
  3109. }
  3110. }
  3111. Item* Spawn::LootItem(int32 id) {
  3112. Item* ret = 0;
  3113. vector<Item*>::iterator itr;
  3114. MLootItems.lock();
  3115. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3116. if ((*itr)->details.item_id == id) {
  3117. ret = *itr;
  3118. loot_items.erase(itr);
  3119. break;
  3120. }
  3121. }
  3122. MLootItems.unlock();
  3123. return ret;
  3124. }
  3125. int32 Spawn::GetLootItemID() {
  3126. int32 ret = 0;
  3127. vector<Item*>::iterator itr;
  3128. MLootItems.lock();
  3129. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3130. ret = (*itr)->details.item_id;
  3131. break;
  3132. }
  3133. MLootItems.unlock();
  3134. return ret;
  3135. }
  3136. bool Spawn::HasLootItemID(int32 id) {
  3137. bool ret = false;
  3138. vector<Item*>::iterator itr;
  3139. MLootItems.readlock(__FUNCTION__, __LINE__);
  3140. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3141. if ((*itr)->details.item_id == id) {
  3142. ret = true;
  3143. break;
  3144. }
  3145. }
  3146. MLootItems.releasereadlock(__FUNCTION__, __LINE__);
  3147. return ret;
  3148. }
  3149. void Spawn::CheckProximities()
  3150. {
  3151. if (!has_spawn_proximities)
  3152. return;
  3153. if (spawn_proximities.size() > 0)
  3154. {
  3155. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3156. while (itr.Next()) {
  3157. SpawnProximity* prox = itr.value;
  3158. map<int32, bool>::iterator spawnsItr;
  3159. for (spawnsItr = prox->spawns_in_proximity.begin(); spawnsItr != prox->spawns_in_proximity.end(); spawnsItr++) {
  3160. Spawn* tmpSpawn = 0;
  3161. if (spawnsItr->first &&
  3162. ((prox->spawn_type == SPAWNPROXIMITY_DATABASE_ID && (tmpSpawn = GetZone()->GetSpawnByDatabaseID(spawnsItr->first)) != 0) ||
  3163. (prox->spawn_type == SPAWNPROXIMITY_LOCATION_ID && (tmpSpawn = GetZone()->GetSpawnByLocationID(spawnsItr->first)) != 0)))
  3164. {
  3165. if (!spawnsItr->second && tmpSpawn->GetDistance(this) <= prox->distance)
  3166. {
  3167. if (prox->in_range_lua_function.size() > 0)
  3168. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->in_range_lua_function.c_str());
  3169. spawnsItr->second = true;
  3170. }
  3171. else if (spawnsItr->second && tmpSpawn->GetDistance(this) > prox->distance)
  3172. {
  3173. if (prox->leaving_range_lua_function.size() > 0)
  3174. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->leaving_range_lua_function.c_str());
  3175. spawnsItr->second = false;
  3176. }
  3177. }
  3178. }
  3179. }
  3180. }
  3181. }
  3182. void Spawn::AddSpawnToProximity(int32 spawnValue, SpawnProximityType type)
  3183. {
  3184. if (!has_spawn_proximities)
  3185. return;
  3186. if (spawn_proximities.size() > 0)
  3187. {
  3188. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3189. while (itr.Next()) {
  3190. SpawnProximity* prox = itr->value;
  3191. if (prox->spawn_value == spawnValue && prox->spawn_type == type)
  3192. prox->spawns_in_proximity.insert(make_pair(spawnValue, false));
  3193. }
  3194. }
  3195. }
  3196. void Spawn::RemoveSpawnFromProximity(int32 spawnValue, SpawnProximityType type)
  3197. {
  3198. if (!has_spawn_proximities)
  3199. return;
  3200. if (spawn_proximities.size() > 0)
  3201. {
  3202. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3203. while (itr.Next()) {
  3204. SpawnProximity* prox = itr->value;
  3205. if (prox->spawn_value == spawnValue && prox->spawn_type == type &&
  3206. prox->spawns_in_proximity.count(spawnValue) > 0)
  3207. prox->spawns_in_proximity.erase(spawnValue);
  3208. }
  3209. }
  3210. }
  3211. 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) {
  3212. EntityCommand* cmd = FindEntityCommand(string(command), true);
  3213. bool newCommand = false;
  3214. if (!cmd)
  3215. {
  3216. newCommand = true;
  3217. cmd = CreateEntityCommand(name, distance, command, error_text, cast_time, spell_visual, !defaultDenyList);
  3218. }
  3219. if (defaultDenyList)
  3220. SetPermissionToEntityCommand(cmd, player, true);
  3221. if (newCommand)
  3222. primary_command_list.push_back(cmd);
  3223. }
  3224. void Spawn::RemovePrimaryEntityCommand(const char* command) {
  3225. vector<EntityCommand*>::iterator itr;
  3226. string tmpStr(command);
  3227. for (itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++) {
  3228. EntityCommand* cmd = *itr;
  3229. if (cmd->command.compare(tmpStr) == 0)
  3230. {
  3231. primary_command_list.erase(itr);
  3232. delete cmd;
  3233. break;
  3234. }
  3235. }
  3236. }
  3237. bool Spawn::SetPermissionToEntityCommand(EntityCommand* command, Player* player, bool permissionValue)
  3238. {
  3239. if (player != NULL)
  3240. {
  3241. map<int32, bool>::iterator itr = command->allow_or_deny.find(player->GetCharacterID());
  3242. if (itr == command->allow_or_deny.end())
  3243. command->allow_or_deny.insert(make_pair(player->GetCharacterID(), permissionValue));
  3244. else if (itr->second != permissionValue)
  3245. itr->second = permissionValue;
  3246. return true;
  3247. }
  3248. return false;
  3249. }