9
3

Spawn.cpp 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495
  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(IsBot() && ((Bot*)this)->IsCamping()) {
  2426. ((Bot*)this)->Begin_Camp();
  2427. }
  2428. if(IsPlayer()){
  2429. //Check if player is riding a boat, if so update pos (boat's current location + XYZ offsets)
  2430. Player* player = ((Player*)this);
  2431. int32 boat_id = player->GetBoatSpawn();
  2432. Spawn* boat = 0;
  2433. if(boat_id > 0)
  2434. boat = GetZone()->GetSpawnByID(boat_id, isSpawnListLocked);
  2435. //TODO: MAYBE do this for real boats, not lifts... GetWidgetTypeNameByTypeID
  2436. /*if(boat){
  2437. SetX(boat->GetX() + player->GetBoatX());
  2438. SetY(boat->GetY() + player->GetBoatY());
  2439. SetZ(boat->GetZ() + player->GetBoatZ());
  2440. }*/
  2441. return;
  2442. }
  2443. if (forceMapCheck && GetZone() != nullptr && GetMap() != nullptr && GetMap()->IsMapLoaded())
  2444. {
  2445. FixZ(true);
  2446. forceMapCheck = false;
  2447. }
  2448. if (GetHP() <= 0 && !IsWidget())
  2449. return;
  2450. if (EngagedInCombat())
  2451. {
  2452. if(IsEntity() && (((Entity*)this)->IsMezzedOrStunned() || ((Entity*)this)->IsRooted())) {
  2453. SetAppearancePosition(GetX(),GetY(),GetZ());
  2454. if ( IsEntity() )
  2455. ((Entity*)this)->SetSpeed(0.0f);
  2456. SetSpeed(0.0f);
  2457. position_changed = true;
  2458. changed = true;
  2459. GetZone()->AddChangedSpawn(this);
  2460. StopMovement();
  2461. return;
  2462. }
  2463. int locations = 0;
  2464. if (movement_locations && MMovementLocations)
  2465. {
  2466. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2467. locations = movement_locations->size();
  2468. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2469. }
  2470. if (locations < 1 && GetZone() && ((Entity*)this)->IsFeared())
  2471. {
  2472. CalculateNewFearpoint();
  2473. }
  2474. }
  2475. Spawn* followTarget = GetZone()->GetSpawnByID(m_followTarget, isSpawnListLocked);
  2476. if (!followTarget && m_followTarget > 0)
  2477. m_followTarget = 0;
  2478. if (following && !IsPauseMovementTimerActive() && followTarget && !((Entity*)this)->IsFeared()) {
  2479. // Need to clear m_followTarget before the zoneserver deletes it
  2480. if (followTarget->GetHP() <= 0) {
  2481. followTarget = 0;
  2482. return;
  2483. }
  2484. if (!IsEntity() || (!((Entity*)this)->IsCasting() && !((Entity*)this)->IsMezzedOrStunned() && !((Entity*)this)->IsRooted())) {
  2485. if (GetBaseSpeed() > 0) {
  2486. CalculateRunningLocation();
  2487. }
  2488. else {
  2489. float speed = 4.0f;
  2490. if (IsEntity())
  2491. speed = ((Entity*)this)->GetMaxSpeed();
  2492. if (IsEntity())
  2493. ((Entity*)this)->SetSpeed(speed);
  2494. SetSpeed(speed);
  2495. }
  2496. MovementLocation tmpLoc;
  2497. MovementLocation* loc = 0;
  2498. if(MMovementLocations) {
  2499. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2500. if(movement_locations && movement_locations->size() > 0){
  2501. loc = movement_locations->front();
  2502. if(loc) {
  2503. tmpLoc.attackable = loc->attackable;
  2504. tmpLoc.gridid = loc->gridid;
  2505. tmpLoc.lua_function = string(loc->lua_function);
  2506. tmpLoc.mapped = loc->mapped;
  2507. tmpLoc.reset_hp_on_runback = loc->reset_hp_on_runback;
  2508. tmpLoc.speed = loc->speed;
  2509. tmpLoc.stage = loc->stage;
  2510. tmpLoc.x = loc->x;
  2511. tmpLoc.y = loc->y;
  2512. tmpLoc.z = loc->z;
  2513. loc = &tmpLoc;
  2514. }
  2515. }
  2516. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2517. }
  2518. float dist = GetDistance(followTarget, true);
  2519. if ((!EngagedInCombat() && m_followDistance > 0 && dist <= m_followDistance) ||
  2520. (dist <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
  2521. ClearRunningLocations();
  2522. CalculateRunningLocation(true);
  2523. }
  2524. else if (loc) {
  2525. float distance = GetDistance(followTarget, loc->x, loc->y, loc->z);
  2526. if ( (!EngagedInCombat() && m_followDistance > 0 && distance > m_followDistance) ||
  2527. ( EngagedInCombat() && distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
  2528. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat(), true, loc->mapped);
  2529. CalculateRunningLocation();
  2530. }
  2531. }
  2532. else {
  2533. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat(), false);
  2534. CalculateRunningLocation();
  2535. }
  2536. }
  2537. }
  2538. bool movementCase = false;
  2539. // Movement loop is only for scripted paths
  2540. if(!EngagedInCombat() && !IsPauseMovementTimerActive() && !NeedsToResumeMovement() && (!IsNPC() || !((NPC*)this)->m_runningBack)){
  2541. MMovementLoop.writelock();
  2542. if(movement_loop.size() > 0 && movement_index < movement_loop.size())
  2543. {
  2544. movementCase = true;
  2545. // Get the target location
  2546. MovementData* data = movement_loop[movement_index];
  2547. // need to resume our movement
  2548. if(resume_movement){
  2549. if (movement_locations && MMovementLocations){
  2550. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2551. while (movement_locations->size()){
  2552. safe_delete(movement_locations->front());
  2553. movement_locations->pop_front();
  2554. }
  2555. movement_locations->clear();
  2556. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2557. }
  2558. data = movement_loop[movement_index];
  2559. if(data)
  2560. {
  2561. ((Entity*)this)->SetSpeed(data->speed);
  2562. SetSpeed(data->speed);
  2563. if(data->use_movement_location_heading)
  2564. SetHeading(data->heading);
  2565. else if(!IsWidget())
  2566. FaceTarget(data->x, data->z);
  2567. // 0 delay at target location, need to set multiple locations
  2568. if(data->delay == 0 && movement_loop.size() > 0) {
  2569. int16 tmp_index = movement_index+1;
  2570. MovementData* data2 = 0;
  2571. if(tmp_index < movement_loop.size())
  2572. data2 = movement_loop[tmp_index];
  2573. else
  2574. data2 = movement_loop[0];
  2575. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  2576. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  2577. }
  2578. // delay at target location, only need to set 1 location
  2579. else
  2580. AddRunningLocation(data->x, data->y, data->z, data->speed);
  2581. }
  2582. movement_start_time = 0;
  2583. resume_movement = false;
  2584. }
  2585. // If we are not moving or we have arrived at our destination
  2586. else if(!IsRunning() || (data && data->x == GetX() && data->y == GetY() && data->z == GetZ())){
  2587. // If we were moving remove the last running location (the point we just arrived at)
  2588. if(IsRunning())
  2589. RemoveRunningLocation();
  2590. // If this waypoint has a delay and we just arrived here (movement_start_time == 0)
  2591. if(data && data->delay > 0 && movement_start_time == 0){
  2592. // Set the current time
  2593. movement_start_time = Timer::GetCurrentTime2();
  2594. // If this waypoint had a lua function then call it
  2595. if(data->lua_function.length() > 0)
  2596. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2597. int16 nextMove;
  2598. if ((int16)(movement_index + 1) < movement_loop.size())
  2599. nextMove = movement_index + 1;
  2600. else
  2601. nextMove = 0;
  2602. // Get the next target location
  2603. data = movement_loop[nextMove];
  2604. //Go ahead and face the next location
  2605. if(data)
  2606. FaceTarget(data->x, data->z);
  2607. }
  2608. // If this waypoint has no delay or we have waited the required time (current time >= delay + movement_start_time)
  2609. else if(data && data->delay == 0 || (data && data->delay > 0 && Timer::GetCurrentTime2() >= (data->delay+movement_start_time))) {
  2610. // if no delay at this waypoint but a lua function for it then call the function
  2611. if(data->delay == 0 && data->lua_function.length() > 0)
  2612. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2613. // since we ran a lua function make sure the movement loop is still alive and accurate
  2614. if(movement_loop.size() > 0)
  2615. {
  2616. // Advance the current movement loop index
  2617. if((int16)(movement_index+1) < movement_loop.size())
  2618. movement_index++;
  2619. else
  2620. movement_index = 0;
  2621. // Get the next target location
  2622. data = movement_loop[movement_index];
  2623. // set the speed for that location
  2624. SetSpeed(data->speed);
  2625. if(!IsWidget())
  2626. // turn towards the location
  2627. FaceTarget(data->x, data->z);
  2628. // If 0 delay at location get and set data for the point after it
  2629. if(data->delay == 0 && movement_loop.size() > 0){
  2630. if(movement_locations && MMovementLocations)
  2631. {
  2632. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2633. while (movement_locations->size()){
  2634. safe_delete(movement_locations->front());
  2635. movement_locations->pop_front();
  2636. }
  2637. // clear current target locations
  2638. movement_locations->clear();
  2639. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2640. }
  2641. // get the data for the location after out new location
  2642. int16 tmp_index = movement_index+1;
  2643. MovementData* data2 = 0;
  2644. if(tmp_index < movement_loop.size())
  2645. data2 = movement_loop[tmp_index];
  2646. else
  2647. data2 = movement_loop[0];
  2648. // set the first location (adds it to movement_locations that we just cleared)
  2649. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  2650. // set the location after that
  2651. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  2652. }
  2653. // there is a delay at the next location so we only need to set it
  2654. else
  2655. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, true, "", true);
  2656. // reset this timer to 0 now that we are moving again
  2657. movement_start_time = 0;
  2658. }
  2659. }
  2660. }
  2661. // moving and not at target location yet
  2662. else if(GetBaseSpeed() > 0)
  2663. CalculateRunningLocation();
  2664. // not moving, have a target location but not at it yet
  2665. else if (data) {
  2666. SetSpeed(data->speed);
  2667. AddRunningLocation(data->x, data->y, data->z, data->speed);
  2668. }
  2669. }
  2670. MMovementLoop.releasewritelock();
  2671. }
  2672. if (!movementCase && IsRunning() && !IsPauseMovementTimerActive()) {
  2673. CalculateRunningLocation();
  2674. //last_movement_update = Timer::GetCurrentTime2();
  2675. }
  2676. else if(movementCase)
  2677. {
  2678. //last_movement_update = Timer::GetCurrentTime2();
  2679. }
  2680. /*else if (IsNPC() && !IsRunning() && !EngagedInCombat() && ((NPC*)this)->GetRunbackLocation()) {
  2681. // Is an npc that is not moving and not engaged in combat but has a run back location set then clear the runback location
  2682. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Clear runback location for %s", GetName());
  2683. ((NPC*)this)->ClearRunback();
  2684. resume_movement = true;
  2685. NeedsToResumeMovement(false);
  2686. }*/
  2687. }
  2688. void Spawn::ResetMovement(bool inFlight){
  2689. if(!inFlight)
  2690. MMovementLoop.writelock();
  2691. vector<MovementData*>::iterator itr;
  2692. for(itr = movement_loop.begin(); itr != movement_loop.end(); itr++){
  2693. safe_delete(*itr);
  2694. }
  2695. movement_loop.clear();
  2696. movement_index = 0;
  2697. resume_movement = true;
  2698. ClearRunningLocations();
  2699. if(!inFlight)
  2700. MMovementLoop.releasewritelock();
  2701. }
  2702. void Spawn::AddMovementLocation(float x, float y, float z, float speed, int16 delay, const char* lua_function, float heading, bool include_heading){
  2703. LogWrite(LUA__DEBUG, 5, "LUA", "AddMovementLocation: x: %.2f, y: %.2f, z: %.2f, speed: %.2f, delay: %i, lua: %s",
  2704. x, y, z, speed, delay, string(lua_function).c_str());
  2705. MovementData* data = new MovementData;
  2706. data->x = x;
  2707. data->y = y;
  2708. data->z = z;
  2709. data->speed = speed;
  2710. data->delay = delay*1000;
  2711. if(lua_function)
  2712. data->lua_function = string(lua_function);
  2713. data->heading = heading;
  2714. data->use_movement_location_heading = include_heading;
  2715. MMovementLoop.lock();
  2716. movement_loop.push_back(data);
  2717. MMovementLoop.unlock();
  2718. }
  2719. bool Spawn::IsRunning(){
  2720. bool movement = false;
  2721. if(movement_locations && MMovementLocations)
  2722. {
  2723. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2724. movement = movement_locations->size() > 0;
  2725. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2726. if(movement)
  2727. return true;
  2728. }
  2729. MMovementLoop.lock();
  2730. movement = movement_loop.size() > 0;
  2731. MMovementLoop.unlock();
  2732. return movement;
  2733. }
  2734. void Spawn::RunToLocation(float x, float y, float z, float following_x, float following_y, float following_z){
  2735. if(IsPauseMovementTimerActive() || (IsEntity() && (((Entity*)this)->IsMezzedOrStunned() || ((Entity*)this)->IsRooted())))
  2736. return;
  2737. if(!IsWidget())
  2738. FaceTarget(x, z);
  2739. SetPos(&appearance.pos.X2, x, false);
  2740. SetPos(&appearance.pos.Z2, z, false);
  2741. SetPos(&appearance.pos.Y2, y, false);
  2742. if(following_x == 0 && following_y == 0 && following_z == 0){
  2743. SetPos(&appearance.pos.X3, x, false);
  2744. SetPos(&appearance.pos.Z3, z, false);
  2745. SetPos(&appearance.pos.Y3, y, false);
  2746. }
  2747. else{
  2748. SetPos(&appearance.pos.X3, following_x, false);
  2749. SetPos(&appearance.pos.Y3, following_y, false);
  2750. SetPos(&appearance.pos.Z3, following_z, false);
  2751. }
  2752. position_changed = true;
  2753. changed = true;
  2754. GetZone()->AddChangedSpawn(this);
  2755. }
  2756. MovementLocation* Spawn::GetCurrentRunningLocation(){
  2757. MovementLocation* ret = 0;
  2758. if(MMovementLocations)
  2759. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2760. if(movement_locations && movement_locations->size() > 0){
  2761. ret = movement_locations->front();
  2762. }
  2763. if(MMovementLocations)
  2764. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2765. return ret;
  2766. }
  2767. MovementLocation* Spawn::GetLastRunningLocation(){
  2768. MovementLocation* ret = 0;
  2769. if(MMovementLocations)
  2770. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2771. if(movement_locations && movement_locations->size() > 0){
  2772. ret = movement_locations->back();
  2773. }
  2774. if(MMovementLocations)
  2775. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2776. return ret;
  2777. }
  2778. 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){
  2779. if(speed == 0)
  2780. return;
  2781. if ( IsEntity() )
  2782. ((Entity*)this)->SetSpeed(speed);
  2783. else
  2784. this->SetSpeed(speed);
  2785. MovementLocation* current_location = 0;
  2786. float distance = GetDistance(x, y, z, distance_away != 0);
  2787. if(distance_away != 0){
  2788. distance -= distance_away;
  2789. x = x - (GetX() - x)*distance_away/distance;
  2790. z = z - (GetZ() - z)*distance_away/distance;
  2791. }
  2792. if(!movement_locations){
  2793. movement_locations = new deque<MovementLocation*>();
  2794. safe_delete(MMovementLocations);
  2795. MMovementLocations = new Mutex();
  2796. }
  2797. MovementLocation* data = new MovementLocation;
  2798. data->mapped = isMapped;
  2799. data->x = x;
  2800. data->y = y;
  2801. data->z = z;
  2802. data->speed = speed;
  2803. data->attackable = attackable;
  2804. data->lua_function = lua_function;
  2805. data->gridid = 0; // used for runback defaults
  2806. data->reset_hp_on_runback = false;
  2807. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2808. if(movement_locations->size() > 0)
  2809. current_location = movement_locations->back();
  2810. if(!current_location){
  2811. SetSpawnOrigX(GetX());
  2812. SetSpawnOrigY(GetY());
  2813. SetSpawnOrigZ(GetZ());
  2814. SetSpawnOrigHeading(GetHeading());
  2815. }
  2816. movement_locations->push_back(data);
  2817. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2818. if(!IsPauseMovementTimerActive() && finished_adding_locations){
  2819. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2820. current_location = movement_locations->front();
  2821. SetSpeed(current_location->speed);
  2822. if(movement_locations->size() > 1){
  2823. data = movement_locations->at(1);
  2824. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2825. }
  2826. else
  2827. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2828. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2829. }
  2830. }
  2831. bool Spawn::RemoveRunningLocation(){
  2832. bool ret = false;
  2833. if(movement_locations){
  2834. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2835. if(movement_locations->size() > 0){
  2836. delete movement_locations->front();
  2837. movement_locations->pop_front();
  2838. ret = true;
  2839. }
  2840. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2841. }
  2842. return ret;
  2843. }
  2844. void Spawn::ClearRunningLocations(){
  2845. while(RemoveRunningLocation()){}
  2846. }
  2847. void Spawn::NewWaypointChange(MovementLocation* data){
  2848. if(data){
  2849. if(NeedsToResumeMovement()){
  2850. resume_movement = true;
  2851. NeedsToResumeMovement(false);
  2852. }
  2853. if(!data->attackable)
  2854. SetHeading(GetSpawnOrigHeading());
  2855. }
  2856. if (data && data->lua_function.length() > 0)
  2857. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2858. RemoveRunningLocation();
  2859. }
  2860. bool Spawn::CalculateChange(){
  2861. bool remove_needed = false;
  2862. MovementLocation* data = 0;
  2863. MovementLocation tmpLoc;
  2864. if(movement_locations && MMovementLocations){
  2865. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2866. if(movement_locations->size() > 0){
  2867. // Target location
  2868. data = movement_locations->front();
  2869. if(data) {
  2870. tmpLoc.attackable = data->attackable;
  2871. tmpLoc.gridid = data->gridid;
  2872. tmpLoc.lua_function = string(data->lua_function);
  2873. tmpLoc.mapped = data->mapped;
  2874. tmpLoc.reset_hp_on_runback = data->reset_hp_on_runback;
  2875. tmpLoc.speed = data->speed;
  2876. tmpLoc.stage = data->stage;
  2877. tmpLoc.x = data->x;
  2878. tmpLoc.y = data->y;
  2879. tmpLoc.z = data->z;
  2880. data = &tmpLoc;
  2881. }
  2882. // If no target or we are at the target location need to remove this point
  2883. if(!data || (data->x == GetX() && data->y == GetY() && data->z == GetZ()))
  2884. remove_needed = true;
  2885. }
  2886. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2887. }
  2888. if(remove_needed){
  2889. NewWaypointChange(data);
  2890. }
  2891. else if(data){
  2892. // Speed is per second so we need a time_step (amount of time since the last update) to modify movement by
  2893. 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
  2894. // Get current location
  2895. float nx = GetX();
  2896. float ny = GetY();
  2897. float nz = GetZ();
  2898. // Get Forward vecotr
  2899. float tar_vx = data->x - nx;
  2900. float tar_vy = data->y - ny;
  2901. float tar_vz = data->z - nz;
  2902. // Multiply speed by the time_step to get how much should have changed over the last tick
  2903. float speed = GetSpeed() * time_step;
  2904. // Normalize the forward vector and multiply by speed, this gives us our change in coords, just need to add them to our current coords
  2905. float len = sqrtf(tar_vx * tar_vx + tar_vy * tar_vy + tar_vz * tar_vz);
  2906. tar_vx = (tar_vx / len) * speed;
  2907. tar_vy = (tar_vy / len) * speed;
  2908. tar_vz = (tar_vz / len) * speed;
  2909. // Distance less then 0.5 just set the npc to the target location
  2910. if (GetDistance(data->x, data->y, data->z, IsWidget() ? false : true) <= speed) {
  2911. SetX(data->x, false);
  2912. SetZ(data->z, false);
  2913. SetY(data->y, false, true);
  2914. remove_needed = true;
  2915. NewWaypointChange(data);
  2916. }
  2917. else {
  2918. SetX(nx + tar_vx, false);
  2919. SetZ(nz + tar_vz, false);
  2920. if ( IsWidget() )
  2921. SetY(ny + tar_vy, false, true);
  2922. else
  2923. SetY(ny + tar_vy, false);
  2924. }
  2925. int32 newGrid = appearance.pos.grid_id;
  2926. if (GetMap() != nullptr) {
  2927. m_GridMutex.writelock(__FUNCTION__, __LINE__);
  2928. std::map<int32,TimedGridData>::iterator itr = established_grid_id.begin();
  2929. if ( itr == established_grid_id.end() || itr->second.timestamp <= (Timer::GetCurrentTime2()))
  2930. {
  2931. if(itr != established_grid_id.end() && itr->second.x == GetX() && itr->second.z == GetZ()) {
  2932. itr->second.timestamp = Timer::GetCurrentTime2()+1000;
  2933. itr->second.npc_save = true;
  2934. newGrid = itr->second.grid_id;
  2935. }
  2936. else {
  2937. auto loc = glm::vec3(GetX(), GetZ(), GetY());
  2938. float new_z = GetMap()->FindBestZ(loc, nullptr, &newGrid);
  2939. TimedGridData data;
  2940. data.grid_id = newGrid;
  2941. data.x = GetX();
  2942. data.y = GetY();
  2943. data.z = GetZ();
  2944. data.npc_save = true;
  2945. data.timestamp = Timer::GetCurrentTime2()+1000;
  2946. established_grid_id.insert(make_pair(0, data));
  2947. }
  2948. }
  2949. else
  2950. newGrid = itr->second.grid_id;
  2951. m_GridMutex.releasewritelock(__FUNCTION__, __LINE__);
  2952. }
  2953. if ((!IsFlyingCreature() || IsTransportSpawn()) && newGrid != 0 && newGrid != appearance.pos.grid_id)
  2954. SetPos(&(appearance.pos.grid_id), newGrid);
  2955. }
  2956. return remove_needed;
  2957. }
  2958. void Spawn::CalculateRunningLocation(bool stop){
  2959. bool pauseTimerEnabled = IsPauseMovementTimerActive();
  2960. if (!pauseTimerEnabled && !stop && (last_location_update + 100) > Timer::GetCurrentTime2())
  2961. return;
  2962. else if (!pauseTimerEnabled && !stop)
  2963. last_location_update = Timer::GetCurrentTime2();
  2964. bool continueElseIf = true;
  2965. bool removed = CalculateChange();
  2966. if (stop || pauseTimerEnabled) {
  2967. //following = false;
  2968. SetPos(&appearance.pos.X2, GetX(), false);
  2969. SetPos(&appearance.pos.Y2, GetY(), false);
  2970. SetPos(&appearance.pos.Z2, GetZ(), false);
  2971. SetPos(&appearance.pos.X3, GetX(), false);
  2972. SetPos(&appearance.pos.Y3, GetY(), false);
  2973. SetPos(&appearance.pos.Z3, GetZ(), false);
  2974. continueElseIf = false;
  2975. }
  2976. else if (removed && movement_locations && MMovementLocations) {
  2977. if (MMovementLocations)
  2978. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2979. if(movement_locations->size() > 0) {
  2980. MovementLocation* current_location = movement_locations->at(0);
  2981. if (movement_locations->size() > 1) {
  2982. MovementLocation* data = movement_locations->at(1);
  2983. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2984. }
  2985. else
  2986. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2987. continueElseIf = false;
  2988. }
  2989. if (MMovementLocations)
  2990. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2991. }
  2992. if (continueElseIf && GetZone() && GetTarget() != NULL && EngagedInCombat())
  2993. {
  2994. if (GetDistance(GetTarget()) > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  2995. {
  2996. if ((IsFlyingCreature() || IsWaterCreature()) && CheckLoS(GetTarget()))
  2997. AddRunningLocation(GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ(), GetSpeed(), 0, false);
  2998. else
  2999. GetZone()->movementMgr->NavigateTo((Entity*)this, GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ());
  3000. }
  3001. else
  3002. ((Entity*)this)->HaltMovement();
  3003. }
  3004. else if (continueElseIf && !following)
  3005. {
  3006. position_changed = true;
  3007. changed = true;
  3008. GetZone()->AddChangedSpawn(this);
  3009. }
  3010. }
  3011. float Spawn::GetFaceTarget(float x, float z) {
  3012. float angle;
  3013. double diff_x = x - GetX();
  3014. double diff_z = z - GetZ();
  3015. //If we're very close to the same spot don't bother changing heading
  3016. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  3017. return GetHeading();
  3018. }
  3019. if (diff_z == 0) {
  3020. if (diff_x > 0)
  3021. angle = 90;
  3022. else
  3023. angle = 270;
  3024. }
  3025. else
  3026. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  3027. if (angle < 0)
  3028. angle = angle + 360;
  3029. else
  3030. angle = angle + 180;
  3031. if (diff_x < 0)
  3032. angle = angle + 180;
  3033. if (last_heading_angle == angle) return angle;
  3034. return angle;
  3035. }
  3036. void Spawn::FaceTarget(float x, float z){
  3037. float angle;
  3038. double diff_x = x - GetX();
  3039. double diff_z = z - GetZ();
  3040. //If we're very close to the same spot don't bother changing heading
  3041. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  3042. return;
  3043. }
  3044. if(diff_z==0){
  3045. if(diff_x > 0)
  3046. angle = 90;
  3047. else
  3048. angle = 270;
  3049. }
  3050. else
  3051. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  3052. if(angle < 0)
  3053. angle = angle + 360;
  3054. else
  3055. angle = angle + 180;
  3056. if(diff_x < 0)
  3057. angle = angle + 180;
  3058. if (last_heading_angle == angle) return;
  3059. SetHeading(angle);
  3060. }
  3061. void Spawn::FaceTarget(Spawn* target, bool disable_action_state){
  3062. if(!target)
  3063. return;
  3064. if(GetHP() > 0 && target->IsPlayer() && !EngagedInCombat()){
  3065. if(!IsPet() && disable_action_state) {
  3066. if(IsNPC()) {
  3067. ((NPC*)this)->StartRunback();
  3068. ((NPC*)this)->PauseMovement(30000);
  3069. }
  3070. SetTempActionState(0);
  3071. }
  3072. }
  3073. FaceTarget(target->GetX(), target->GetZ());
  3074. }
  3075. bool Spawn::MeetsSpawnAccessRequirements(Player* player){
  3076. bool ret = false;
  3077. Quest* quest = 0;
  3078. //Check if we meet all quest requirements first..
  3079. m_requiredQuests.readlock(__FUNCTION__, __LINE__);
  3080. if (player && required_quests.size() > 0) {
  3081. map<int32, vector<int16>* >::iterator itr;
  3082. for (itr = required_quests.begin(); itr != required_quests.end(); itr++) {
  3083. player->AddQuestRequiredSpawn(this, itr->first);
  3084. vector<int16>* quest_steps = itr->second;
  3085. for (int32 i = 0; i < quest_steps->size(); i++) {
  3086. quest = player->GetQuest(itr->first);
  3087. if (req_quests_continued_access) {
  3088. if (quest) {
  3089. if (quest->GetQuestStepCompleted(quest_steps->at(i))) {
  3090. ret = true;
  3091. break;
  3092. }
  3093. }
  3094. else if (player->GetCompletedQuest(itr->first)) {
  3095. ret = true;
  3096. break;
  3097. }
  3098. }
  3099. if (quest && quest->QuestStepIsActive(quest_steps->at(i))) {
  3100. ret = true;
  3101. break;
  3102. }
  3103. }
  3104. }
  3105. }
  3106. else
  3107. ret = true;
  3108. m_requiredQuests.releasereadlock(__FUNCTION__, __LINE__);
  3109. if (!ret)
  3110. return ret;
  3111. //Now check if the player meets all history requirements
  3112. m_requiredHistory.readlock(__FUNCTION__, __LINE__);
  3113. if (required_history.size() > 0){
  3114. map<int32, LUAHistory*>::iterator itr;
  3115. for (itr = required_history.begin(); itr != required_history.end(); itr++){
  3116. player->AddHistoryRequiredSpawn(this, itr->first);
  3117. LUAHistory* player_history = player->GetLUAHistory(itr->first);
  3118. if (player_history){
  3119. if (player_history->Value != itr->second->Value || player_history->Value2 != itr->second->Value2)
  3120. ret = false;
  3121. }
  3122. else
  3123. ret = false;
  3124. if (!ret)
  3125. break;
  3126. }
  3127. }
  3128. m_requiredHistory.releasereadlock(__FUNCTION__, __LINE__);
  3129. return ret;
  3130. }
  3131. vector<Spawn*>* Spawn::GetSpawnGroup(){
  3132. vector<Spawn*>* ret_list = 0;
  3133. if(spawn_group_list){
  3134. ret_list = new vector<Spawn*>();
  3135. if(MSpawnGroup)
  3136. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  3137. ret_list->insert(ret_list->begin(), spawn_group_list->begin(), spawn_group_list->end());
  3138. if(MSpawnGroup)
  3139. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  3140. }
  3141. return ret_list;
  3142. }
  3143. bool Spawn::HasSpawnGroup() {
  3144. return spawn_group_list && spawn_group_list->size() > 0;
  3145. }
  3146. bool Spawn::IsInSpawnGroup(Spawn* spawn) {
  3147. bool ret = false;
  3148. if (HasSpawnGroup() && spawn) {
  3149. vector<Spawn*>::iterator itr;
  3150. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  3151. if ((*itr) == spawn) {
  3152. ret = true;
  3153. break;
  3154. }
  3155. }
  3156. }
  3157. return ret;
  3158. }
  3159. Spawn* Spawn::IsSpawnGroupMembersAlive(Spawn* ignore_spawn, bool npc_only) {
  3160. Spawn* ret = nullptr;
  3161. if (MSpawnGroup && HasSpawnGroup()) {
  3162. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  3163. vector<Spawn*>::iterator itr;
  3164. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  3165. if ((*itr) != ignore_spawn && (*itr)->Alive() && (!npc_only || (npc_only && (*itr)->IsNPC()))) {
  3166. ret = (*itr);
  3167. break;
  3168. }
  3169. }
  3170. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  3171. }
  3172. return ret;
  3173. }
  3174. void Spawn::AddSpawnToGroup(Spawn* spawn){
  3175. if(!spawn)
  3176. return;
  3177. if(!spawn_group_list){
  3178. spawn_group_list = new vector<Spawn*>();
  3179. spawn_group_list->push_back(this);
  3180. safe_delete(MSpawnGroup);
  3181. MSpawnGroup = new Mutex();
  3182. MSpawnGroup->SetName("Spawn::MSpawnGroup");
  3183. }
  3184. vector<Spawn*>::iterator itr;
  3185. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  3186. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  3187. if((*itr) == spawn){
  3188. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  3189. return;
  3190. }
  3191. }
  3192. spawn_group_list->push_back(spawn);
  3193. spawn->SetSpawnGroupList(spawn_group_list, MSpawnGroup);
  3194. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  3195. }
  3196. void Spawn::SetSpawnGroupList(vector<Spawn*>* list, Mutex* mutex){
  3197. spawn_group_list = list;
  3198. MSpawnGroup = mutex;
  3199. }
  3200. void Spawn::RemoveSpawnFromGroup(bool erase_all){
  3201. SetSpawnGroupID(0);
  3202. bool del = false;
  3203. if(MSpawnGroup){
  3204. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  3205. if(spawn_group_list){
  3206. vector<Spawn*>::iterator itr;
  3207. Spawn* spawn = 0;
  3208. if(spawn_group_list->size() == 1)
  3209. erase_all = true;
  3210. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  3211. spawn = *itr;
  3212. if (spawn) {
  3213. if(!erase_all){
  3214. if(spawn == this){
  3215. spawn_group_list->erase(itr);
  3216. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  3217. spawn_group_list = 0;
  3218. MSpawnGroup = 0;
  3219. return;
  3220. }
  3221. }
  3222. else{
  3223. if (spawn != this)
  3224. spawn->SetSpawnGroupList(0, 0);
  3225. }
  3226. }
  3227. }
  3228. if (erase_all)
  3229. spawn_group_list->clear();
  3230. del = (spawn_group_list->size() == 0);
  3231. }
  3232. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  3233. if (del){
  3234. safe_delete(MSpawnGroup);
  3235. safe_delete(spawn_group_list);
  3236. }
  3237. }
  3238. }
  3239. void Spawn::SetSpawnGroupID(int32 id){
  3240. m_SpawnMutex.writelock();
  3241. group_id = id;
  3242. m_SpawnMutex.releasewritelock();
  3243. }
  3244. int32 Spawn::GetSpawnGroupID(){
  3245. int32 groupid = 0;
  3246. m_SpawnMutex.readlock();
  3247. groupid = group_id;
  3248. m_SpawnMutex.releasereadlock();
  3249. return groupid;
  3250. }
  3251. void Spawn::AddChangedZoneSpawn(){
  3252. if(send_spawn_changes && GetZone())
  3253. GetZone()->AddChangedSpawn(this);
  3254. }
  3255. void Spawn::RemoveSpawnAccess(Spawn* spawn) {
  3256. if (allowed_access.count(spawn->GetID()) > 0) {
  3257. allowed_access.erase(spawn->GetID());
  3258. GetZone()->HidePrivateSpawn(this);
  3259. }
  3260. }
  3261. void Spawn::SetFollowTarget(Spawn* spawn, int32 follow_distance) {
  3262. if (spawn && spawn != this) {
  3263. m_followTarget = spawn->GetID();
  3264. m_followDistance = follow_distance;
  3265. }
  3266. else {
  3267. m_followTarget = 0;
  3268. if (following)
  3269. following = false;
  3270. m_followDistance = 0;
  3271. }
  3272. }
  3273. void Spawn::AddTempVariable(string var, string val) {
  3274. m_tempVariableTypes[var] = 5;
  3275. m_tempVariables[var] = val;
  3276. }
  3277. void Spawn::AddTempVariable(string var, Spawn* val) {
  3278. m_tempVariableTypes[var] = 1;
  3279. m_tempVariableSpawn[var] = val->GetID();
  3280. }
  3281. void Spawn::AddTempVariable(string var, ZoneServer* val) {
  3282. m_tempVariableTypes[var] = 2;
  3283. m_tempVariableZone[var] = val;
  3284. }
  3285. void Spawn::AddTempVariable(string var, Item* val) {
  3286. m_tempVariableTypes[var] = 3;
  3287. m_tempVariableItem[var] = val;
  3288. }
  3289. void Spawn::AddTempVariable(string var, Quest* val) {
  3290. m_tempVariableTypes[var] = 4;
  3291. m_tempVariableQuest[var] = val;
  3292. }
  3293. string Spawn::GetTempVariable(string var) {
  3294. string ret = "";
  3295. if (m_tempVariables.count(var) > 0)
  3296. ret = m_tempVariables[var];
  3297. return ret;
  3298. }
  3299. Spawn* Spawn::GetTempVariableSpawn(string var) {
  3300. Spawn* ret = 0;
  3301. if (m_tempVariableSpawn.count(var) > 0)
  3302. ret = GetZone()->GetSpawnByID(m_tempVariableSpawn[var]);
  3303. return ret;
  3304. }
  3305. ZoneServer* Spawn::GetTempVariableZone(string var) {
  3306. ZoneServer* ret = 0;
  3307. if (m_tempVariableZone.count(var) > 0)
  3308. ret = m_tempVariableZone[var];
  3309. return ret;
  3310. }
  3311. Item* Spawn::GetTempVariableItem(string var) {
  3312. Item* ret = 0;
  3313. if (m_tempVariableItem.count(var) > 0)
  3314. ret = m_tempVariableItem[var];
  3315. return ret;
  3316. }
  3317. Quest* Spawn::GetTempVariableQuest(string var) {
  3318. Quest* ret = 0;
  3319. if (m_tempVariableQuest.count(var) > 0)
  3320. ret = m_tempVariableQuest[var];
  3321. return ret;
  3322. }
  3323. int8 Spawn::GetTempVariableType(string var) {
  3324. int8 ret = 0;
  3325. if (m_tempVariableTypes.count(var) > 0)
  3326. ret = m_tempVariableTypes[var];
  3327. return ret;
  3328. }
  3329. void Spawn::DeleteTempVariable(string var) {
  3330. int8 type = GetTempVariableType(var);
  3331. switch (type) {
  3332. case 1:
  3333. m_tempVariableSpawn.erase(var);
  3334. break;
  3335. case 2:
  3336. m_tempVariableZone.erase(var);
  3337. break;
  3338. case 3:
  3339. m_tempVariableItem.erase(var);
  3340. break;
  3341. case 4:
  3342. m_tempVariableQuest.erase(var);
  3343. break;
  3344. case 5:
  3345. m_tempVariables.erase(var);
  3346. break;
  3347. }
  3348. m_tempVariableTypes.erase(var);
  3349. }
  3350. Spawn* Spawn::GetRunningTo() {
  3351. return GetZone()->GetSpawnByID(running_to);
  3352. }
  3353. Spawn* Spawn::GetFollowTarget() {
  3354. return GetZone()->GetSpawnByID(m_followTarget);
  3355. }
  3356. void Spawn::CopySpawnAppearance(Spawn* spawn){
  3357. if (!spawn)
  3358. return;
  3359. //This function copies the appearace of the provided spawn to this one
  3360. if (spawn->IsEntity() && IsEntity()){
  3361. memcpy(&((Entity*)this)->features, &((Entity*)spawn)->features, sizeof(CharFeatures));
  3362. memcpy(&((Entity*)this)->equipment, &((Entity*)spawn)->equipment, sizeof(EQ2_Equipment));
  3363. }
  3364. SetSize(spawn->GetSize());
  3365. SetModelType(spawn->GetModelType());
  3366. }
  3367. void Spawn::SetY(float y, bool updateFlags, bool disableYMapCheck)
  3368. {
  3369. SetPos(&appearance.pos.Y, y, updateFlags);
  3370. if (!disableYMapCheck)
  3371. FixZ();
  3372. }
  3373. float Spawn::FindDestGroundZ(glm::vec3 dest, float z_offset)
  3374. {
  3375. float best_z = BEST_Z_INVALID;
  3376. if (GetZone() != nullptr && GetMap() != nullptr)
  3377. {
  3378. dest.z += z_offset;
  3379. best_z = GetMap()->FindBestZ(dest, nullptr);
  3380. }
  3381. return best_z;
  3382. }
  3383. float Spawn::GetFixedZ(const glm::vec3& destination, int32 z_find_offset) {
  3384. BenchTimer timer;
  3385. timer.reset();
  3386. float new_z = destination.z;
  3387. if (GetZone() != nullptr && GetMap() != nullptr) {
  3388. /* if (flymode == GravityBehavior::Flying)
  3389. return new_z;
  3390. */
  3391. /* if (zone->HasWaterMap() && zone->watermap->InLiquid(glm::vec3(m_Position)))
  3392. return new_z;
  3393. */
  3394. /*
  3395. * Any more than 5 in the offset makes NPC's hop/snap to ceiling in small corridors
  3396. */
  3397. new_z = this->FindDestGroundZ(destination, z_find_offset);
  3398. if (new_z != BEST_Z_INVALID) {
  3399. if (new_z < -2000) {
  3400. new_z = GetY();
  3401. }
  3402. }
  3403. auto duration = timer.elapsed();
  3404. LogWrite(MAP__DEBUG, 0, "Map", "Mob::GetFixedZ() ([{%s}]) returned [{%f}] at [{%f}], [{%f}], [{%f}] - Took [{%f}]",
  3405. this->GetName(),
  3406. new_z,
  3407. destination.x,
  3408. destination.y,
  3409. destination.z,
  3410. duration);
  3411. }
  3412. return new_z;
  3413. }
  3414. void Spawn::FixZ(bool forceUpdate) {
  3415. if (!GetZone()) {
  3416. return;
  3417. }
  3418. /*
  3419. if (flymode == GravityBehavior::Flying) {
  3420. return;
  3421. }*/
  3422. /*
  3423. if (zone->watermap && zone->watermap->InLiquid(m_Position)) {
  3424. return;
  3425. }*/
  3426. // we do the inwater check here manually to avoid double calling for a Z coordinate
  3427. glm::vec3 current_loc(GetX(), GetZ(), GetY());
  3428. uint32 GridID = 0;
  3429. uint32 WidgetID = 0;
  3430. float new_z = GetY();
  3431. if(GetMap() != nullptr) {
  3432. float new_z = GetMap()->FindBestZ(current_loc, nullptr, &GridID, &WidgetID);
  3433. if ((IsTransportSpawn() || !IsFlyingCreature()) && GridID != 0 && GridID != appearance.pos.grid_id) {
  3434. LogWrite(PLAYER__DEBUG, 0, "Player", "%s left grid %u and entered grid %u", appearance.name, appearance.pos.grid_id, GridID);
  3435. const char* zone_script = world.GetZoneScript(GetZone()->GetZoneID());
  3436. if (zone_script && lua_interface)
  3437. {
  3438. lua_interface->RunZoneScript(zone_script, "enter_location", GetZone(), this, GridID);
  3439. lua_interface->RunZoneScript(zone_script, "leave_location", GetZone(), this, appearance.pos.grid_id);
  3440. }
  3441. SetPos(&(appearance.pos.grid_id), GridID);
  3442. }
  3443. trigger_widget_id = WidgetID;
  3444. }
  3445. // no need to go any further for players, flying creatures or objects, just needed the grid id set
  3446. if (IsPlayer() || IsFlyingCreature() || IsObject()) {
  3447. return;
  3448. }
  3449. if ( region_map != nullptr )
  3450. {
  3451. glm::vec3 targPos(GetX(), GetY(), GetZ());
  3452. if(region_map->InWater(targPos, appearance.pos.grid_id))
  3453. return;
  3454. }
  3455. if (new_z == GetY())
  3456. return;
  3457. if ((new_z > -2000) && new_z != BEST_Z_INVALID) {
  3458. SetY(new_z, forceUpdate, true);
  3459. }
  3460. else {
  3461. LogWrite(MAP__DEBUG, 0, "Map", "[{%s}] is failing to find Z [{%f}]", this->GetName(), std::abs(GetY() - new_z));
  3462. }
  3463. }
  3464. bool Spawn::CheckLoS(Spawn* target)
  3465. {
  3466. float radiusSrc = 2.0f;
  3467. float radiusTarg = 2.0f;
  3468. glm::vec3 targpos(target->GetX(), target->GetZ(), target->GetY()+radiusTarg);
  3469. glm::vec3 pos(GetX(), GetZ(), GetY()+radiusSrc);
  3470. return CheckLoS(pos, targpos);
  3471. }
  3472. bool Spawn::CheckLoS(glm::vec3 myloc, glm::vec3 oloc)
  3473. {
  3474. ZoneServer* zone = GetZone();
  3475. if (zone == NULL || GetMap() == NULL || !GetMap()->IsMapLoaded())
  3476. return true;
  3477. else
  3478. return GetMap()->CheckLoS(myloc, oloc);
  3479. return false;
  3480. }
  3481. void Spawn::CalculateNewFearpoint()
  3482. {
  3483. if (GetZone() && GetZone()->pathing) {
  3484. auto Node = zone->pathing->GetRandomLocation(glm::vec3(GetX(), GetZ(), GetY()));
  3485. if (Node.x != 0.0f || Node.y != 0.0f || Node.z != 0.0f) {
  3486. AddRunningLocation(Node.x, Node.y, Node.z, GetSpeed(), 0, true, true, "", true);
  3487. }
  3488. }
  3489. }
  3490. Item* Spawn::LootItem(int32 id) {
  3491. Item* ret = 0;
  3492. vector<Item*>::iterator itr;
  3493. MLootItems.lock();
  3494. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3495. if ((*itr)->details.item_id == id) {
  3496. ret = *itr;
  3497. loot_items.erase(itr);
  3498. break;
  3499. }
  3500. }
  3501. MLootItems.unlock();
  3502. return ret;
  3503. }
  3504. void Spawn::TransferLoot(Spawn* spawn) {
  3505. if(spawn == this || spawn == nullptr)
  3506. return; // mmm no
  3507. vector<Item*>::iterator itr;
  3508. MLootItems.lock();
  3509. for (itr = loot_items.begin(); itr != loot_items.end();) {
  3510. if (!(*itr)->IsBodyDrop()) {
  3511. spawn->AddLootItem(*itr);
  3512. itr = loot_items.erase(itr);
  3513. }
  3514. else {
  3515. itr++;
  3516. }
  3517. }
  3518. MLootItems.unlock();
  3519. }
  3520. int32 Spawn::GetLootItemID() {
  3521. int32 ret = 0;
  3522. vector<Item*>::iterator itr;
  3523. MLootItems.lock();
  3524. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3525. ret = (*itr)->details.item_id;
  3526. break;
  3527. }
  3528. MLootItems.unlock();
  3529. return ret;
  3530. }
  3531. bool Spawn::HasLootItemID(int32 id) {
  3532. bool ret = false;
  3533. vector<Item*>::iterator itr;
  3534. MLootItems.readlock(__FUNCTION__, __LINE__);
  3535. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3536. if ((*itr)->details.item_id == id) {
  3537. ret = true;
  3538. break;
  3539. }
  3540. }
  3541. MLootItems.releasereadlock(__FUNCTION__, __LINE__);
  3542. return ret;
  3543. }
  3544. void Spawn::CheckProximities()
  3545. {
  3546. if (!has_spawn_proximities)
  3547. return;
  3548. if (spawn_proximities.size() > 0)
  3549. {
  3550. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3551. while (itr.Next()) {
  3552. SpawnProximity* prox = itr.value;
  3553. map<int32, bool>::iterator spawnsItr;
  3554. for (spawnsItr = prox->spawns_in_proximity.begin(); spawnsItr != prox->spawns_in_proximity.end(); spawnsItr++) {
  3555. Spawn* tmpSpawn = 0;
  3556. if (spawnsItr->first &&
  3557. ((prox->spawn_type == SPAWNPROXIMITY_DATABASE_ID && (tmpSpawn = GetZone()->GetSpawnByDatabaseID(spawnsItr->first)) != 0) ||
  3558. (prox->spawn_type == SPAWNPROXIMITY_LOCATION_ID && (tmpSpawn = GetZone()->GetSpawnByLocationID(spawnsItr->first)) != 0)))
  3559. {
  3560. if (!spawnsItr->second && tmpSpawn->GetDistance(this) <= prox->distance)
  3561. {
  3562. if (prox->in_range_lua_function.size() > 0)
  3563. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->in_range_lua_function.c_str());
  3564. spawnsItr->second = true;
  3565. }
  3566. else if (spawnsItr->second && tmpSpawn->GetDistance(this) > prox->distance)
  3567. {
  3568. if (prox->leaving_range_lua_function.size() > 0)
  3569. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->leaving_range_lua_function.c_str());
  3570. spawnsItr->second = false;
  3571. }
  3572. }
  3573. }
  3574. }
  3575. }
  3576. }
  3577. void Spawn::AddSpawnToProximity(int32 spawnValue, SpawnProximityType type)
  3578. {
  3579. if (!has_spawn_proximities)
  3580. return;
  3581. if (spawn_proximities.size() > 0)
  3582. {
  3583. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3584. while (itr.Next()) {
  3585. SpawnProximity* prox = itr->value;
  3586. if (prox->spawn_value == spawnValue && prox->spawn_type == type)
  3587. prox->spawns_in_proximity.insert(make_pair(spawnValue, false));
  3588. }
  3589. }
  3590. }
  3591. void Spawn::RemoveSpawnFromProximity(int32 spawnValue, SpawnProximityType type)
  3592. {
  3593. if (!has_spawn_proximities)
  3594. return;
  3595. if (spawn_proximities.size() > 0)
  3596. {
  3597. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3598. while (itr.Next()) {
  3599. SpawnProximity* prox = itr->value;
  3600. if (prox->spawn_value == spawnValue && prox->spawn_type == type &&
  3601. prox->spawns_in_proximity.count(spawnValue) > 0)
  3602. prox->spawns_in_proximity.erase(spawnValue);
  3603. }
  3604. }
  3605. }
  3606. 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) {
  3607. EntityCommand* cmd = FindEntityCommand(string(command), true);
  3608. bool newCommand = false;
  3609. if (!cmd)
  3610. {
  3611. newCommand = true;
  3612. cmd = CreateEntityCommand(name, distance, command, error_text, cast_time, spell_visual, !defaultDenyList);
  3613. }
  3614. if (defaultDenyList)
  3615. SetPermissionToEntityCommand(cmd, player, true);
  3616. if (newCommand)
  3617. primary_command_list.push_back(cmd);
  3618. }
  3619. void Spawn::RemovePrimaryEntityCommand(const char* command) {
  3620. vector<EntityCommand*>::iterator itr;
  3621. string tmpStr(command);
  3622. for (itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++) {
  3623. EntityCommand* cmd = *itr;
  3624. if (cmd->command.compare(tmpStr) == 0)
  3625. {
  3626. primary_command_list.erase(itr);
  3627. delete cmd;
  3628. break;
  3629. }
  3630. }
  3631. }
  3632. bool Spawn::SetPermissionToEntityCommand(EntityCommand* command, Player* player, bool permissionValue)
  3633. {
  3634. if(!player)
  3635. return false;
  3636. return SetPermissionToEntityCommandByCharID(command, player->GetCharacterID(), permissionValue);
  3637. }
  3638. bool Spawn::SetPermissionToEntityCommandByCharID(EntityCommand* command, int32 charID, bool permissionValue)
  3639. {
  3640. map<int32, bool>::iterator itr = command->allow_or_deny.find(charID);
  3641. if (itr == command->allow_or_deny.end())
  3642. command->allow_or_deny.insert(make_pair(charID, permissionValue));
  3643. else if (itr->second != permissionValue)
  3644. itr->second = permissionValue;
  3645. return true;
  3646. }
  3647. void Spawn::RemoveSpawnFromPlayer(Player* player)
  3648. {
  3649. m_Update.writelock(__FUNCTION__, __LINE__);
  3650. player->RemoveSpawn(this); // sets it as removed
  3651. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  3652. }
  3653. bool Spawn::InWater()
  3654. {
  3655. bool inWater = false;
  3656. if ( region_map != nullptr )
  3657. {
  3658. glm::vec3 targPos(GetX(), GetY(), GetZ());
  3659. if ( IsGroundSpawn() )
  3660. targPos.y -= .5f;
  3661. if(region_map->InWater(targPos, appearance.pos.grid_id))
  3662. inWater = true;
  3663. }
  3664. return inWater;
  3665. }
  3666. bool Spawn::InLava()
  3667. {
  3668. bool inLava = false;
  3669. if ( region_map != nullptr )
  3670. {
  3671. glm::vec3 targPos(GetX(), GetY(), GetZ());
  3672. if ( IsGroundSpawn() )
  3673. targPos.y -= .5f;
  3674. if(region_map->InLava(targPos, appearance.pos.grid_id))
  3675. inLava = true;
  3676. }
  3677. return inLava;
  3678. }
  3679. void Spawn::DeleteRegion(Region_Node* inNode, ZBSP_Node* rootNode)
  3680. {
  3681. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  3682. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  3683. {
  3684. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  3685. Region_Node* node = actualItr->first;
  3686. ZBSP_Node* BSP_Root = actualItr->second;
  3687. if(inNode == node && rootNode == BSP_Root )
  3688. {
  3689. testitr = Regions.erase(testitr);
  3690. break;
  3691. }
  3692. }
  3693. }
  3694. bool Spawn::InRegion(Region_Node* inNode, ZBSP_Node* rootNode)
  3695. {
  3696. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  3697. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  3698. {
  3699. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  3700. Region_Node* node = actualItr->first;
  3701. ZBSP_Node* BSP_Root = actualItr->second;
  3702. if(inNode == node && rootNode == BSP_Root )
  3703. {
  3704. return testitr->second.inRegion;
  3705. }
  3706. }
  3707. return false;
  3708. }
  3709. int32 Spawn::GetRegionType(Region_Node* inNode, ZBSP_Node* rootNode)
  3710. {
  3711. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  3712. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  3713. {
  3714. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  3715. Region_Node* node = actualItr->first;
  3716. ZBSP_Node* BSP_Root = actualItr->second;
  3717. if(inNode == node && rootNode == BSP_Root )
  3718. {
  3719. return testitr->second.regionType;
  3720. }
  3721. }
  3722. return false;
  3723. }
  3724. float Spawn::SpawnAngle(Spawn* target, float selfx, float selfz)
  3725. {
  3726. if (!target || target == this)
  3727. return 0.0f;
  3728. float angle, lengthb, vectorx, vectorz, dotp;
  3729. float spx = (target->GetX()); // mob xloc (inverse because eq)
  3730. float spz = -(target->GetZ()); // mob yloc
  3731. float heading = target->GetHeading(); // mob heading
  3732. if (heading < 270)
  3733. heading += 90;
  3734. else
  3735. heading -= 270;
  3736. heading = heading * 3.1415f / 180.0f; // convert to radians
  3737. vectorx = spx + (10.0f * std::cos(heading)); // create a vector based on heading
  3738. vectorz = spz + (10.0f * std::sin(heading)); // of spawn length 10
  3739. // length of spawn to player vector
  3740. lengthb = (float) std::sqrt(((selfx - spx) * (selfx - spx)) + ((-selfz - spz) * (-selfz - spz)));
  3741. // calculate dot product to get angle
  3742. // Handle acos domain errors due to floating point rounding errors
  3743. dotp = ((vectorx - spx) * (selfx - spx) +
  3744. (vectorz - spz) * (-selfz - spz)) / (10.0f * lengthb);
  3745. if (dotp > 1)
  3746. return 0.0f;
  3747. else if (dotp < -1)
  3748. return 180.0f;
  3749. angle = std::acos(dotp);
  3750. angle = angle * 180.0f / 3.1415f;
  3751. return angle;
  3752. }
  3753. void Spawn::StopMovement()
  3754. {
  3755. ResetMovement(true);
  3756. }
  3757. bool Spawn::PauseMovement(int32 period_of_time_ms)
  3758. {
  3759. if(period_of_time_ms < 1)
  3760. period_of_time_ms = 1;
  3761. RunToLocation(GetX(),GetY(),GetZ());
  3762. pause_timer.Start(period_of_time_ms, true);
  3763. return true;
  3764. }
  3765. bool Spawn::IsPauseMovementTimerActive()
  3766. {
  3767. if(pause_timer.Check())
  3768. pause_timer.Disable();
  3769. return pause_timer.Enabled();
  3770. }
  3771. bool Spawn::IsFlyingCreature()
  3772. {
  3773. if(!IsEntity())
  3774. return false;
  3775. return ((Entity*)this)->GetInfoStruct()->get_flying_type();
  3776. }
  3777. bool Spawn::IsWaterCreature()
  3778. {
  3779. if(!IsEntity())
  3780. return false;
  3781. return ((Entity*)this)->GetInfoStruct()->get_water_type();
  3782. }
  3783. void Spawn::SetFlyingCreature() {
  3784. if(!IsEntity() || !rule_manager.GetGlobalRule(R_Spawn, UseHardCodeFlyingModelType)->GetInt8())
  3785. return;
  3786. if(((Entity*)this)->GetInfoStruct()->get_flying_type() > 0) // DB spawn npc flag already set
  3787. return;
  3788. switch (GetModelType())
  3789. {
  3790. case 260:
  3791. case 295:
  3792. ((Entity*)this)->GetInfoStruct()->set_flying_type(1);
  3793. is_flying_creature = true;
  3794. break;
  3795. default:
  3796. ((Entity*)this)->GetInfoStruct()->set_flying_type(0);
  3797. break;
  3798. }
  3799. }
  3800. void Spawn::SetWaterCreature() {
  3801. if(!IsEntity() || !rule_manager.GetGlobalRule(R_Spawn, UseHardCodeWaterModelType)->GetInt8())
  3802. return;
  3803. if(((Entity*)this)->GetInfoStruct()->get_water_type() > 0) // DB spawn npc flag already set
  3804. return;
  3805. switch (GetModelType())
  3806. {
  3807. case 194:
  3808. case 204:
  3809. case 210:
  3810. case 241:
  3811. case 242:
  3812. case 254:
  3813. case 10668:
  3814. case 20828:
  3815. ((Entity*)this)->GetInfoStruct()->set_water_type(1);
  3816. break;
  3817. default:
  3818. ((Entity*)this)->GetInfoStruct()->set_water_type(0);
  3819. break;
  3820. }
  3821. }
  3822. void Spawn::AddRailPassenger(int32 char_id)
  3823. {
  3824. std::lock_guard<std::mutex> lk(m_RailMutex);
  3825. rail_passengers.insert(make_pair(char_id,true));
  3826. }
  3827. void Spawn::RemoveRailPassenger(int32 char_id)
  3828. {
  3829. std::lock_guard<std::mutex> lk(m_RailMutex);
  3830. std::map<int32, bool>::iterator itr = rail_passengers.find(char_id);
  3831. if(itr != rail_passengers.end())
  3832. rail_passengers.erase(itr);
  3833. }
  3834. vector<Spawn*> Spawn::GetPassengersOnRail() {
  3835. vector<Spawn*> tmp_list;
  3836. Spawn* spawn;
  3837. m_RailMutex.lock();
  3838. std::map<int32, bool>::iterator itr = rail_passengers.begin();
  3839. while(itr != rail_passengers.end()){
  3840. Client* client = zone_list.GetClientByCharID(itr->first);
  3841. if(!client || !client->GetPlayer())
  3842. continue;
  3843. tmp_list.push_back(client->GetPlayer());
  3844. itr++;
  3845. }
  3846. m_RailMutex.unlock();
  3847. return tmp_list;
  3848. }
  3849. void Spawn::SetAppearancePosition(float x, float y, float z) {
  3850. appearance.pos.X = x;
  3851. appearance.pos.Y = y;
  3852. appearance.pos.Z = z;
  3853. appearance.pos.X2 = appearance.pos.X;
  3854. appearance.pos.Y2 = appearance.pos.Y;
  3855. appearance.pos.Z2 = appearance.pos.Z;
  3856. appearance.pos.X3 = appearance.pos.X;
  3857. appearance.pos.Y3 = appearance.pos.Y;
  3858. appearance.pos.Z3 = appearance.pos.Z;
  3859. SetSpeedX(0);
  3860. SetSpeedY(0);
  3861. SetSpeedZ(0);
  3862. if(IsPlayer()) {
  3863. ((Player*)this)->SetSideSpeed(0);
  3864. ((Player*)this)->pos_packet_speed = 0;
  3865. }
  3866. }
  3867. int32 Spawn::InsertRegionToSpawn(Region_Node* node, ZBSP_Node* bsp_root, WaterRegionType regionType, bool in_region) {
  3868. std::map<Region_Node*, ZBSP_Node*> newMap;
  3869. newMap.insert(make_pair(node, bsp_root));
  3870. Region_Status status;
  3871. status.inRegion = in_region;
  3872. status.regionType = regionType;
  3873. int32 returnValue = 0;
  3874. if(in_region) {
  3875. lua_interface->RunRegionScript(node->regionScriptName, "EnterRegion", GetZone(), this, regionType, &returnValue);
  3876. }
  3877. status.timerTic = returnValue;
  3878. status.lastTimerTic = returnValue ? Timer::GetCurrentTime2() : 0;
  3879. Regions.insert(make_pair(newMap, status));
  3880. return returnValue;
  3881. }
  3882. bool Spawn::HasRegionTracked(Region_Node* node, ZBSP_Node* bsp_root, bool in_region) {
  3883. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  3884. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  3885. {
  3886. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  3887. Region_Node *node = actualItr->first;
  3888. ZBSP_Node *BSP_Root = actualItr->second;
  3889. if(node == actualItr->first && BSP_Root == actualItr->second) {
  3890. if(testitr->second.inRegion == in_region)
  3891. return true;
  3892. else
  3893. break;
  3894. }
  3895. }
  3896. return false;
  3897. }