Spawn.cpp 110 KB

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