Spawn.cpp 137 KB

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