Spawn.cpp 116 KB

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