Spawn.cpp 134 KB

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