Spawn.cpp 135 KB

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