Spawn.cpp 132 KB

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