Spawn.cpp 112 KB

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