Spawn.cpp 117 KB

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