9
3

Spawn.cpp 148 KB

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