Spawn.cpp 114 KB

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