Spawn.cpp 125 KB

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