Spawn.cpp 147 KB

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