Spawn.cpp 124 KB

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