Spawn.cpp 140 KB

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