Spawn.cpp 124 KB

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