Spawn.cpp 116 KB

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