Spawn.cpp 126 KB

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