Spawn.cpp 125 KB

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