Spawn.cpp 132 KB

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