Spawn.cpp 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132
  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. Spawn::Spawn(){
  36. loot_coins = 0;
  37. trap_triggered = false;
  38. trap_state = 0;
  39. chest_drop_time = 0;
  40. trap_opened_time = 0;
  41. group_id = 0;
  42. size_offset = 0;
  43. merchant_id = 0;
  44. merchant_type = 0;
  45. merchant_min_level = 0;
  46. merchant_max_level = 0;
  47. memset(&appearance, 0, sizeof(AppearanceData));
  48. memset(&basic_info, 0, sizeof(BasicInfoStruct));
  49. appearance.pos.state = 0x4080;
  50. appearance.encounter_level =6;
  51. size = 32;
  52. appearance.pos.collision_radius = 32;
  53. id = Spawn::NextID();
  54. oversized_packet = 0xFF;
  55. zone = 0;
  56. spawn_location_id = 0;
  57. spawn_entry_id = 0;
  58. spawn_location_spawns_id = 0;
  59. respawn = 0;
  60. expire_time = 0;
  61. expire_offset = 0;
  62. x_offset = 0;
  63. y_offset = 0;
  64. z_offset = 0;
  65. database_id = 0;
  66. packet_num = 1;
  67. changed = false;
  68. vis_changed = false;
  69. position_changed = false;
  70. send_spawn_changes = true;
  71. info_changed = false;
  72. appearance.pos.Speed1 = 0;
  73. last_attacker = 0;
  74. faction_id = 0;
  75. running_to = 0;
  76. tmp_visual_state = -1;
  77. tmp_action_state = -1;
  78. transporter_id = 0;
  79. invulnerable = false;
  80. spawn_group_list = 0;
  81. MSpawnGroup = 0;
  82. movement_locations = 0;
  83. MMovementLocations = 0;
  84. target = 0;
  85. primary_command_list_id = 0;
  86. secondary_command_list_id = 0;
  87. is_pet = false;
  88. m_followTarget = 0;
  89. following = false;
  90. req_quests_continued_access = false;
  91. req_quests_override = 0;
  92. req_quests_private = false;
  93. m_illusionModel = 0;
  94. Cell_Info.CurrentCell = nullptr;
  95. Cell_Info.CellListIndex = -1;
  96. m_addedToWorldTimestamp = 0;
  97. m_spawnAnim = 0;
  98. m_spawnAnimLeeway = 0;
  99. m_Update.SetName("Spawn::m_Update");
  100. m_requiredHistory.SetName("Spawn::m_requiredHistory");
  101. m_requiredQuests.SetName("Spawn::m_requiredQuests");
  102. last_heading_angle = 0.0;
  103. last_grid_update = 0;
  104. last_location_update = 0.0;
  105. last_movement_update = Timer::GetCurrentTime2();
  106. forceMapCheck = false;
  107. m_followDistance = 0;
  108. MCommandMutex.SetName("Entity::MCommandMutex");
  109. has_spawn_proximities = false;
  110. pickup_item_id = 0;
  111. pickup_unique_item_id = 0;
  112. disable_sounds = false;
  113. has_quests_required = false;
  114. is_flying_creature = false;
  115. is_water_creature = false;
  116. region_map = nullptr;
  117. current_map = nullptr;
  118. RegionMutex.SetName("Spawn::RegionMutex");
  119. pause_timer.Disable();
  120. m_SpawnMutex.SetName("Spawn::SpawnMutex");
  121. appearance_equipment_list.SetAppearanceType(1);
  122. is_transport_spawn = false;
  123. rail_id = 0;
  124. }
  125. Spawn::~Spawn(){
  126. vector<Item*>::iterator itr;
  127. for (itr = loot_items.begin(); itr != loot_items.end(); itr++)
  128. safe_delete(*itr);
  129. loot_items.clear();
  130. RemovePrimaryCommands();
  131. for(int32 i=0;i<secondary_command_list.size();i++){
  132. safe_delete(secondary_command_list[i]);
  133. }
  134. secondary_command_list.clear();
  135. RemoveSpawnFromGroup();
  136. if (MMovementLocations)
  137. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  138. if(movement_locations){
  139. while(movement_locations->size()){
  140. safe_delete(movement_locations->front());
  141. movement_locations->pop_front();
  142. }
  143. safe_delete(movement_locations);
  144. }
  145. if (MMovementLocations)
  146. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  147. safe_delete(MMovementLocations);
  148. MMovementLoop.lock();
  149. for (int32 i = 0; i < movement_loop.size(); i++)
  150. safe_delete(movement_loop.at(i));
  151. movement_loop.clear();
  152. MMovementLoop.unlock();
  153. m_requiredHistory.writelock(__FUNCTION__, __LINE__);
  154. map<int32, LUAHistory*>::iterator lua_itr;
  155. for (lua_itr = required_history.begin(); lua_itr != required_history.end(); lua_itr++) {
  156. safe_delete(lua_itr->second);
  157. }
  158. required_history.clear();
  159. m_requiredHistory.releasewritelock(__FUNCTION__, __LINE__);
  160. m_requiredQuests.writelock(__FUNCTION__, __LINE__);
  161. map<int32, vector<int16>* >::iterator rq_itr;
  162. for (rq_itr = required_quests.begin(); rq_itr != required_quests.end(); rq_itr++){
  163. safe_delete(rq_itr->second);
  164. }
  165. required_quests.clear();
  166. m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
  167. // just in case to make sure data is destroyed
  168. RemoveSpawnProximities();
  169. Regions.clear();
  170. }
  171. void Spawn::RemovePrimaryCommands()
  172. {
  173. for (int32 i = 0; i < primary_command_list.size(); i++) {
  174. safe_delete(primary_command_list[i]);
  175. }
  176. primary_command_list.clear();
  177. }
  178. void Spawn::InitializeHeaderPacketData(Player* player, PacketStruct* header, int16 index) {
  179. header->setDataByName("index", index);
  180. if (GetSpawnAnim() > 0 && Timer::GetCurrentTime2() < (GetAddedToWorldTimestamp() + GetSpawnAnimLeeway())) {
  181. if (header->GetVersion() >= 57080)
  182. header->setDataByName("spawn_anim", GetSpawnAnim());
  183. else
  184. header->setDataByName("spawn_anim", (int16)GetSpawnAnim());
  185. }
  186. else {
  187. if (header->GetVersion() >= 57080)
  188. header->setDataByName("spawn_anim", 0xFFFFFFFF);
  189. else
  190. header->setDataByName("spawn_anim", 0xFFFF);
  191. }
  192. if (primary_command_list.size() > 0){
  193. if (primary_command_list.size() > 1) {
  194. header->setArrayLengthByName("command_list", primary_command_list.size());
  195. for (int32 i = 0; i < primary_command_list.size(); i++) {
  196. header->setArrayDataByName("command_list_name", primary_command_list[i]->name.c_str(), i);
  197. header->setArrayDataByName("command_list_max_distance", primary_command_list[i]->distance, i);
  198. header->setArrayDataByName("command_list_error", primary_command_list[i]->error_text.c_str(), i);
  199. header->setArrayDataByName("command_list_command", primary_command_list[i]->command.c_str(), i);
  200. }
  201. }
  202. if (header->GetVersion() <= 546) {
  203. header->setMediumStringByName("default_command", primary_command_list[0]->name.c_str());
  204. }
  205. else
  206. header->setMediumStringByName("default_command", primary_command_list[0]->command.c_str());
  207. header->setDataByName("max_distance", primary_command_list[0]->distance);
  208. }
  209. if (spawn_group_list && MSpawnGroup){
  210. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  211. header->setArrayLengthByName("group_size", spawn_group_list->size());
  212. vector<Spawn*>::iterator itr;
  213. int i = 0;
  214. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++, i++){
  215. int32 idx = 0;
  216. idx = player->GetIDWithPlayerSpawn((*itr));
  217. header->setArrayDataByName("group_spawn_id", idx, i);
  218. }
  219. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  220. }
  221. header->setDataByName("spawn_id", player->GetIDWithPlayerSpawn(this));
  222. header->setDataByName("crc", 1);
  223. header->setDataByName("time_stamp", Timer::GetCurrentTime2());
  224. }
  225. void Spawn::InitializeVisPacketData(Player* player, PacketStruct* vis_packet) {
  226. int16 version = vis_packet->GetVersion();
  227. if (IsPlayer())
  228. appearance.pos.grid_id = 0xFFFFFFFF;
  229. if (IsPlayer())
  230. vis_packet->setDataByName("player", 1);
  231. if (version <= 546) {
  232. vis_packet->setDataByName("targetable", appearance.targetable);
  233. vis_packet->setDataByName("show_name", appearance.display_name);
  234. vis_packet->setDataByName("attackable", appearance.attackable);
  235. if(appearance.attackable == 1)
  236. vis_packet->setDataByName("attackable_icon", 1);
  237. if (IsPlayer()) {
  238. if (((Player*)this)->IsGroupMember(player))
  239. vis_packet->setDataByName("group_member", 1);
  240. }
  241. }
  242. if (appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1) {
  243. if (!IsGroundSpawn()) {
  244. int8 arrow_color = ARROW_COLOR_WHITE;
  245. sint8 npc_con = player->GetFactions()->GetCon(faction_id);
  246. if (IsPlayer() && !((Player*)this)->CanSeeInvis(player))
  247. npc_con = 0;
  248. else if (!IsPlayer() && IsEntity() && !((Entity*)this)->CanSeeInvis(player))
  249. npc_con = 0;
  250. if (appearance.attackable == 1)
  251. arrow_color = player->GetArrowColor(GetLevel());
  252. if (version <= 283) {
  253. if (GetMerchantID() > 0)
  254. arrow_color += 7;
  255. else {
  256. if (primary_command_list.size() > 0) {
  257. int16 len = strlen(primary_command_list[0]->command.c_str());
  258. if(len >= 4 && strncmp(primary_command_list[0]->command.c_str(), "bank", 4) == 0)
  259. arrow_color += 14;
  260. else if (len >= 4 && strncmp(primary_command_list[0]->command.c_str(), "hail", 4) == 0)
  261. arrow_color += 21;
  262. else if (len >= 6 && strncmp(primary_command_list[0]->command.c_str(), "attack", 6) == 0) {
  263. if (arrow_color > 5)
  264. arrow_color = 34;
  265. else
  266. arrow_color += 29;
  267. }
  268. }
  269. }
  270. }
  271. vis_packet->setDataByName("arrow_color", arrow_color);
  272. if (appearance.attackable == 0)
  273. vis_packet->setDataByName("locked_no_loot", 1);
  274. else
  275. vis_packet->setDataByName("locked_no_loot", appearance.locked_no_loot);
  276. if (player->GetArrowColor(GetLevel()) == ARROW_COLOR_GRAY)
  277. if (npc_con == -4)
  278. npc_con = -3;
  279. vis_packet->setDataByName("npc_con", npc_con);
  280. if (appearance.attackable == 1 && IsNPC() && (player->GetFactions()->GetCon(faction_id) <= -4 || ((NPC*)this)->Brain()->GetHate(player) > 1)) {
  281. vis_packet->setDataByName("npc_hate", ((NPC*)this)->Brain()->GetHatePercentage(player));
  282. vis_packet->setDataByName("show_difficulty_arrows", 1);
  283. }
  284. int8 quest_flag = player->CheckQuestFlag(this);
  285. if (version < 1188 && quest_flag >= 16)
  286. quest_flag = 1;
  287. vis_packet->setDataByName("quest_flag", quest_flag);
  288. if (player->CheckQuestsKillUpdate(this, false)) {
  289. vis_packet->setDataByName("name_quest_icon", 1);
  290. }
  291. }
  292. }
  293. int8 vis_flags = 0;
  294. if (MeetsSpawnAccessRequirements(player)) {
  295. if (appearance.attackable == 1)
  296. vis_flags += 64; //attackable icon
  297. if (appearance.show_level == 1)
  298. vis_flags += 32;
  299. if (appearance.display_name == 1)
  300. vis_flags += 16;
  301. if (IsPlayer() || appearance.targetable == 1)
  302. vis_flags += 4;
  303. if (appearance.show_command_icon == 1)
  304. vis_flags += 2;
  305. if (this == player) {
  306. //if (version <= 283) {
  307. // vis_flags = 1;
  308. //}
  309. //else
  310. vis_flags += 1;
  311. }
  312. }
  313. else if (req_quests_override > 0)
  314. {
  315. //Check to see if there's an override value set
  316. vis_flags = req_quests_override & 0xFF;
  317. }
  318. if (player->HasGMVision())
  319. {
  320. if ((vis_flags & 16) == 0 && appearance.display_name == 0)
  321. vis_flags += 16;
  322. if ((vis_flags & 4) == 0)
  323. vis_flags += 4;
  324. }
  325. if (version <= 546 && (vis_flags > 1 || appearance.display_hand_icon > 0)) //interactable
  326. vis_flags = 1;
  327. vis_packet->setDataByName("vis_flags", vis_flags);
  328. if (MeetsSpawnAccessRequirements(player)) {
  329. vis_packet->setDataByName("hand_flag", appearance.display_hand_icon);
  330. }
  331. else {
  332. if ((req_quests_override & 256) > 0)
  333. vis_packet->setDataByName("hand_flag", 1);
  334. }
  335. if (version == 546 && GetMerchantID() > 0) {
  336. vis_packet->setDataByName("guild", "<Merchant>");
  337. }
  338. }
  339. void Spawn::InitializeFooterPacketData(Player* player, PacketStruct* footer) {
  340. if (IsWidget()){
  341. Widget* widget = (Widget*)this;
  342. if (widget->GetMultiFloorLift()) {
  343. footer->setDataByName("widget_x", widget->GetX());
  344. footer->setDataByName("widget_y", widget->GetY());
  345. footer->setDataByName("widget_z", widget->GetZ());
  346. }
  347. else {
  348. footer->setDataByName("widget_x", widget->GetWidgetX());
  349. footer->setDataByName("widget_y", widget->GetWidgetY());
  350. footer->setDataByName("widget_z", widget->GetWidgetZ());
  351. }
  352. footer->setDataByName("widget_id", widget->GetWidgetID());
  353. footer->setDataByName("unknown3c", 6);
  354. }
  355. else if (IsSign()){
  356. Sign* sign = (Sign*)this;
  357. footer->setDataByName("widget_id", sign->GetWidgetID());
  358. footer->setDataByName("widget_x", sign->GetWidgetX());
  359. footer->setDataByName("widget_y", sign->GetWidgetY());
  360. footer->setDataByName("widget_z", sign->GetWidgetZ());
  361. footer->setDataByName("unknown2b", 6);
  362. if (sign->GetSignTitle())
  363. footer->setMediumStringByName("title", sign->GetSignTitle());
  364. if (sign->GetSignDescription())
  365. footer->setMediumStringByName("description", sign->GetSignDescription());
  366. footer->setDataByName("sign_distance", sign->GetSignDistance());
  367. footer->setDataByName("show", 1);
  368. }
  369. if ( IsPlayer())
  370. footer->setDataByName("is_player", 1);
  371. if (strlen(appearance.name) < 1)
  372. strncpy(appearance.name,to_string(GetID()).c_str(),128);
  373. footer->setMediumStringByName("name", appearance.name);
  374. footer->setMediumStringByName("guild", appearance.sub_title);
  375. footer->setMediumStringByName("prefix", appearance.prefix_title);
  376. footer->setMediumStringByName("suffix", appearance.suffix_title);
  377. footer->setMediumStringByName("last_name", appearance.last_name);
  378. if (appearance.attackable == 0 && GetLevel() > 0)
  379. footer->setDataByName("spawn_type", 1);
  380. else if (appearance.attackable == 0)
  381. footer->setDataByName("spawn_type", 6);
  382. else
  383. footer->setDataByName("spawn_type", 3);
  384. }
  385. EQ2Packet* Spawn::spawn_serialize(Player* player, int16 version, int16 offset, int32 value, int16 offset2, int16 offset3, int16 offset4, int32 value2) {
  386. // 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
  387. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer() && player == ((NPC*)this)->GetOwner() && player->GetInfoStruct()->get_pet_id() == 0) {
  388. ((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->set_pet_id(player->spawn_id);
  389. player->SetCharSheetChanged(true);
  390. }
  391. m_Update.writelock(__FUNCTION__, __LINE__);
  392. int16 index = 0;
  393. if ((index = player->GetIndexForSpawn(this)) > 0) {
  394. player->SetSpawnMapIndex(this, index);
  395. }
  396. else {
  397. index = player->SetSpawnMapAndIndex(this);
  398. }
  399. // Jabantiz - [Bug] Client Crash on Revive
  400. if (player->GetIDWithPlayerSpawn(this) == 0) {
  401. player->SetSpawnMap(this);
  402. }
  403. PacketStruct* header = player->GetSpawnHeaderStruct();
  404. header->ResetData();
  405. InitializeHeaderPacketData(player, header, index);
  406. PacketStruct* footer = 0;
  407. if (IsWidget())
  408. footer = player->GetWidgetFooterStruct();
  409. else if (IsSign())
  410. footer = player->GetSignFooterStruct();
  411. else if (version > 546)
  412. footer = player->GetSpawnFooterStruct();
  413. if (footer) {
  414. footer->ResetData();
  415. InitializeFooterPacketData(player, footer);
  416. }
  417. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  418. PacketStruct* info_struct = player->GetSpawnInfoStruct();
  419. PacketStruct* pos_struct = player->GetSpawnPosStruct();
  420. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  421. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  422. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  423. info_struct->ResetData();
  424. InitializeInfoPacketData(player, info_struct);
  425. vis_struct->ResetData();
  426. InitializeVisPacketData(player, vis_struct);
  427. pos_struct->ResetData();
  428. InitializePosPacketData(player, pos_struct);
  429. if (version <= 283) {
  430. if (offset == 777) {
  431. info_struct->setDataByName("name", "This is a really long name\n");
  432. info_struct->setDataByName("last_name", "This is a really long LAST name\n");
  433. info_struct->setDataByName("name_suffix", "This is a really long SUFFIX\n");
  434. info_struct->setDataByName("name_prefix", "This is a really long PREFIX\n");
  435. info_struct->setDataByName("unknown", "This is a really long UNKNOWN\n");
  436. info_struct->setDataByName("second_suffix", "This is a really long 2nd SUFFIX\n");
  437. }
  438. //info_struct->setDataByName("unknown2", 3, 0); // level
  439. //info_struct->setDataByName("unknown2", 1, 1); //unknown, two down arrows
  440. //info_struct->setDataByName("unknown2", 1, 2); //unknown
  441. //info_struct->setDataByName("unknown2", 1, 3); //unknown
  442. //info_struct->setDataByName("unknown2", 1, 4); //solo fight
  443. //info_struct->setDataByName("unknown2", 1, 5); //unknown
  444. //info_struct->setDataByName("unknown2", 1, 6); //unknown
  445. //info_struct->setDataByName("unknown2", 1, 7); //merchant
  446. //info_struct->setDataByName("unknown2", 1, 8); //unknown
  447. //info_struct->setDataByName("unknown2", 1, 9); //unknown
  448. //info_struct->setDataByName("unknown2", 1, 10);
  449. //112: 00 00 00 01 02 03 04 05 - 06 07 08 09 0A 00 00 00 | ................ merchant, x4
  450. //112: 00 00 00 01 02 03 04 05 - 00 00 00 00 00 00 00 00 | ................ x4, epic, indifferent
  451. //info_struct->setDataByName("body_size", 42);
  452. //for(int i=0;i<8;i++)
  453. // info_struct->setDataByName("persistent_spell_visuals", 329, i);
  454. //info_struct->setDataByName("persistent_spell_levels", 20);
  455. }
  456. string* vis_data = vis_struct->serializeString();
  457. string* pos_data = pos_struct->serializeString();
  458. string* info_data = info_struct->serializeString();
  459. int16 part2_size = pos_data->length() + vis_data->length() + info_data->length();
  460. uchar* part2 = new uchar[part2_size];
  461. player->AddSpawnPosPacketForXOR(id, (uchar*)pos_data->c_str(), pos_data->length());
  462. player->AddSpawnVisPacketForXOR(id, (uchar*)vis_data->c_str(), vis_data->length());
  463. player->AddSpawnInfoPacketForXOR(id, (uchar*)info_data->c_str(), info_data->length());
  464. uchar* ptr = part2;
  465. memcpy(ptr, pos_data->c_str(), pos_data->length());
  466. ptr += pos_data->length();
  467. memcpy(ptr, vis_data->c_str(), vis_data->length());
  468. ptr += vis_data->length();
  469. memcpy(ptr, info_data->c_str(), info_data->length());
  470. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  471. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  472. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  473. string* part1 = header->serializeString();
  474. string* part3 = 0;
  475. if (footer)
  476. part3 = footer->serializeString();
  477. //uchar blah7[] = {0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x01,0x01,0x00 };
  478. //uchar blah7[] = { 0x03,0x01,0x00,0x01,0x01,0x01,0x00,0x01,0x01,0x00 }; base
  479. //uchar blah7[] = { 0x03,0x00,0x00,0x01,0x01,0x01,0x00,0x01,0x01,0x00 }; //no change
  480. //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x01,0x01,0x00,0x01,0x01,0x00 }; //blue instead of green
  481. //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x01,0x00 }; //no change
  482. //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00 }; //not selectable
  483. //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x01,0x00 }; //no change
  484. //uchar blah7[] = { 0x03,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00 }; //no longer have the two down arrows
  485. //uchar blah7[] = { 0x01,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00 }; //arrow color green instead of white/gray
  486. //memcpy(part2 + 77, blah7, sizeof(blah7));
  487. //DumpPacket(part2, 885);
  488. if (offset > 0) {
  489. if (offset4 > 0 && offset4 >= offset3) {
  490. uchar* ptr2 = (uchar*)part2;
  491. ptr2 += offset3;
  492. while (offset4 >= offset3) {
  493. int8 jumpsize = 1;
  494. if (value2 > 0xFFFF) {
  495. memcpy(ptr2, (uchar*)&value2, 4);
  496. jumpsize = 4;
  497. }
  498. else if (value2 > 0xFF) {
  499. memcpy(ptr2, (uchar*)&value2, 2);
  500. jumpsize = 2;
  501. }
  502. else {
  503. memcpy(ptr2, (uchar*)&value2, 1);
  504. jumpsize = 1;
  505. }
  506. ptr2 += jumpsize;
  507. offset4 -= jumpsize;
  508. }
  509. }
  510. if (offset2 > 0 && offset2 >= offset) {
  511. uchar* ptr2 = (uchar*)part2;
  512. ptr2 += offset;
  513. while (offset2 >= offset) {
  514. int8 jumpsize = 1;
  515. if (value > 0xFFFF) {
  516. memcpy(ptr2, (uchar*)&value, 4);
  517. jumpsize = 4;
  518. }
  519. else if (value > 0xFF) {
  520. memcpy(ptr2, (uchar*)&value, 2);
  521. jumpsize = 2;
  522. }
  523. else
  524. memcpy(ptr2, (uchar*)&value, 1);
  525. ptr2 += jumpsize;
  526. offset2 -= jumpsize;
  527. }
  528. }
  529. else {
  530. uchar* ptr2 = (uchar*)part2;
  531. ptr2 += offset;
  532. if (value > 0xFFFF)
  533. memcpy(ptr2, (uchar*)&value, 4);
  534. else if (value > 0xFF)
  535. memcpy(ptr2, (uchar*)&value, 2);
  536. else
  537. memcpy(ptr2, (uchar*)&value, 1);
  538. }
  539. cout << "setting offset: " << offset << " to: " << value << endl;
  540. }
  541. if (offset > 0)
  542. DumpPacket(part2, part2_size);
  543. uchar tmp[1100];
  544. bool reverse = (version > 283);
  545. part2_size = Pack(tmp, part2, part2_size, 1100, version, reverse);
  546. int32 total_size = part1->length() + part2_size + 3;
  547. if (part3)
  548. total_size += part3->length();
  549. int32 final_packet_size = total_size + 1;
  550. if (version > 283)
  551. final_packet_size += 3;
  552. else {
  553. if (final_packet_size >= 255) {
  554. final_packet_size += 2;
  555. }
  556. }
  557. uchar* final_packet = new uchar[final_packet_size];
  558. ptr = final_packet;
  559. if (version <= 283) {
  560. if ((final_packet_size - total_size) > 1) {
  561. memcpy(ptr, &oversized_packet, sizeof(oversized_packet));
  562. ptr += sizeof(oversized_packet);
  563. memcpy(ptr, &total_size, 2);
  564. ptr += 2;
  565. }
  566. else {
  567. memcpy(ptr, &total_size, 1);
  568. ptr += 1;
  569. }
  570. }
  571. else {
  572. memcpy(ptr, &total_size, sizeof(total_size));
  573. ptr += sizeof(total_size);
  574. }
  575. memcpy(ptr, &oversized_packet, sizeof(oversized_packet));
  576. ptr += sizeof(oversized_packet);
  577. int16 opcode = 0;
  578. if(IsWidget())
  579. opcode = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqCreateWidgetCmd);
  580. else if(IsSign())
  581. opcode = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqCreateSignWidgetCmd);
  582. else
  583. opcode = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqCreateGhostCmd);
  584. memcpy(ptr, &opcode, sizeof(opcode));
  585. ptr += sizeof(opcode);
  586. memcpy(ptr, part1->c_str(), part1->length());
  587. ptr += part1->length();
  588. memcpy(ptr, tmp, part2_size);
  589. ptr += part2_size;
  590. if (part3)
  591. memcpy(ptr, part3->c_str(), part3->length());
  592. delete[] part2;
  593. // printf("%s (%i): p1: %i, p2:% i (%i), p3:% i, ts: %i\n", GetName(), GetID(), part1->length(), part2_size, origPart2Size, part3->length(), total_size);
  594. EQ2Packet* ret = new EQ2Packet(OP_ClientCmdMsg, final_packet, final_packet_size);
  595. delete[] final_packet;
  596. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  597. return ret;
  598. }
  599. uchar* Spawn::spawn_info_changes(Player* player, int16 version){
  600. int16 index = player->GetIndexForSpawn(this);
  601. PacketStruct* packet = player->GetSpawnInfoStruct();
  602. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  603. packet->ResetData();
  604. InitializeInfoPacketData(player, packet);
  605. string* data = packet->serializeString();
  606. int32 size = data->length();
  607. uchar* xor_info_packet = player->GetTempInfoPacketForXOR();
  608. if (!xor_info_packet || size != player->GetTempInfoXorSize())
  609. {
  610. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateInfoPacket: %i, %i", size, player->GetTempInfoXorSize());
  611. safe_delete(xor_info_packet);
  612. xor_info_packet = player->SetTempInfoPacketForXOR(size);
  613. }
  614. uchar* orig_packet = player->GetSpawnInfoPacketForXOR(id);
  615. if(orig_packet){
  616. memcpy(xor_info_packet, (uchar*)data->c_str(), size);
  617. Encode(xor_info_packet, orig_packet, size);
  618. }
  619. bool changed = false;
  620. for (int i = 0; i < size; ++i) {
  621. if (xor_info_packet[i]) {
  622. changed = true;
  623. break;
  624. }
  625. }
  626. if (!changed) {
  627. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  628. return nullptr;
  629. }
  630. uchar* tmp = new uchar[size + 10];
  631. size = Pack(tmp, xor_info_packet, size, size, version);
  632. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  633. int32 orig_size = size;
  634. size-=sizeof(int32);
  635. size+=CheckOverLoadSize(index);
  636. info_packet_size = size + CheckOverLoadSize(size);
  637. uchar* tmp2 = new uchar[info_packet_size];
  638. uchar* ptr = tmp2;
  639. ptr += DoOverLoad(size, ptr);
  640. ptr += DoOverLoad(index, ptr);
  641. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  642. delete[] tmp;
  643. return tmp2;
  644. }
  645. uchar* Spawn::spawn_vis_changes(Player* player, int16 version){
  646. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  647. int16 index = player->GetIndexForSpawn(this);
  648. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  649. uchar* orig_packet = player->GetSpawnVisPacketForXOR(id);
  650. vis_struct->ResetData();
  651. InitializeVisPacketData(player, vis_struct);
  652. string* data = vis_struct->serializeString();
  653. int32 size = data->length();
  654. uchar* xor_vis_packet = player->GetTempVisPacketForXOR();
  655. if (!xor_vis_packet || size != player->GetTempVisXorSize())
  656. {
  657. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateVisPacket: %i, %i", size, player->GetTempVisXorSize());
  658. safe_delete(xor_vis_packet);
  659. xor_vis_packet = player->SetTempVisPacketForXOR(size);
  660. }
  661. if(orig_packet){
  662. memcpy(xor_vis_packet, (uchar*)data->c_str(), size);
  663. Encode(xor_vis_packet, orig_packet, size);
  664. }
  665. bool changed = false;
  666. for (int i = 0; i < size; ++i) {
  667. if (xor_vis_packet[i]) {
  668. changed = true;
  669. break;
  670. }
  671. }
  672. if (!changed) {
  673. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  674. return nullptr;
  675. }
  676. uchar* tmp = new uchar[size + 10];
  677. size = Pack(tmp, xor_vis_packet, size, size, version);
  678. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  679. int32 orig_size = size;
  680. size-=sizeof(int32);
  681. size+=CheckOverLoadSize(index);
  682. vis_packet_size = size + CheckOverLoadSize(size);
  683. uchar* tmp2 = new uchar[vis_packet_size];
  684. uchar* ptr = tmp2;
  685. ptr += DoOverLoad(size, ptr);
  686. ptr += DoOverLoad(index, ptr);
  687. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  688. delete[] tmp;
  689. return tmp2;
  690. }
  691. uchar* Spawn::spawn_pos_changes(Player* player, int16 version) {
  692. int16 index = player->GetIndexForSpawn(this);
  693. PacketStruct* packet = player->GetSpawnPosStruct();
  694. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  695. uchar* orig_packet = player->GetSpawnPosPacketForXOR(id);
  696. packet->ResetData();
  697. InitializePosPacketData(player, packet, true);
  698. string* data = packet->serializeString();
  699. int32 size = data->length();
  700. uchar* xor_pos_packet = player->GetTempPosPacketForXOR();
  701. if (!xor_pos_packet || size != player->GetTempPosXorSize())
  702. {
  703. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiatePosPacket: %i, %i", size, player->GetTempPosXorSize());
  704. safe_delete(xor_pos_packet);
  705. xor_pos_packet = player->SetTempPosPacketForXOR(size);
  706. }
  707. if(orig_packet){
  708. memcpy(xor_pos_packet, (uchar*)data->c_str(), size);
  709. Encode(xor_pos_packet, orig_packet, size);
  710. }
  711. bool changed = false;
  712. for (int i = 0; i < size; ++i) {
  713. if (xor_pos_packet[i]) {
  714. changed = true;
  715. break;
  716. }
  717. }
  718. if (!changed) {
  719. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  720. return nullptr;
  721. }
  722. uchar* tmp;
  723. if (IsPlayer() && version > 283)
  724. tmp = new uchar[size + 14];
  725. else
  726. tmp = new uchar[size + 10];
  727. size = Pack(tmp, xor_pos_packet, size, size, version);
  728. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  729. int32 orig_size = size;
  730. // Needed for CoE+ clients
  731. if (version >= 1188)
  732. size += 1;
  733. if(IsPlayer() && version > 546)
  734. size += 4;
  735. size-=sizeof(int32);
  736. size+=CheckOverLoadSize(index);
  737. pos_packet_size = size + CheckOverLoadSize(size);
  738. uchar* tmp2 = new uchar[pos_packet_size];
  739. uchar* ptr = tmp2;
  740. ptr += DoOverLoad(size, ptr);
  741. ptr += DoOverLoad(index, ptr);
  742. // extra byte in coe+ clients, 0 for NPC's 1 for Players
  743. int8 x = 0;
  744. if (IsPlayer() && version > 546) {
  745. if (version >= 1188) {
  746. // set x to 1 and add it to the packet
  747. x = 1;
  748. memcpy(ptr, &x, sizeof(int8));
  749. ptr += sizeof(int8);
  750. }
  751. int32 now = Timer::GetCurrentTime2();
  752. memcpy(ptr, &now, sizeof(int32));
  753. ptr += sizeof(int32);
  754. }
  755. else if (version >= 1188) {
  756. // add x to packet
  757. memcpy(ptr, &x, sizeof(int8));
  758. ptr += sizeof(int8);
  759. }
  760. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  761. delete[] tmp;
  762. return tmp2;
  763. }
  764. EQ2Packet* Spawn::player_position_update_packet(Player* player, int16 version){
  765. if(!player || player->IsPlayer() == false){
  766. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called player_position_update_packet without player!");
  767. return 0;
  768. }
  769. else if(IsPlayer() == false){
  770. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called player_position_update_packet from spawn!");
  771. return 0;
  772. }
  773. static const int8 info_size = 1;
  774. static const int8 vis_size = 1;
  775. m_Update.writelock(__FUNCTION__, __LINE__);
  776. uchar* pos_changes = spawn_pos_changes(player, version);
  777. if (pos_changes == NULL )
  778. {
  779. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  780. return NULL;
  781. }
  782. int32 tmp_pos_packet_size = pos_packet_size;
  783. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  784. int32 size = info_size + tmp_pos_packet_size + vis_size + 8;
  785. if (version >= 284)
  786. size += 3;
  787. else if (version <= 283 && size >= 255) {//1 byte to 3 for overloaded val
  788. size += 2;
  789. }
  790. static const int8 oversized = 255;
  791. int16 opcode_val = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqUpdateGhostCmd);
  792. uchar* tmp = new uchar[size];
  793. memset(tmp, 0, size);
  794. uchar* ptr = tmp;
  795. if (version >= 284) {
  796. size -= 4;
  797. memcpy(ptr, &size, sizeof(int32));
  798. size += 4;
  799. ptr += sizeof(int32);
  800. }
  801. else {
  802. if (size >= 255) {
  803. memcpy(ptr, &oversized, sizeof(int8));
  804. ptr += sizeof(int8);
  805. size -= 3;
  806. memcpy(ptr, &size, sizeof(int16));
  807. size += 3;
  808. ptr += 3;
  809. }
  810. else {
  811. size -= 1;
  812. memcpy(ptr, &size, sizeof(int8));
  813. ptr += sizeof(int8);
  814. size += 1;
  815. ptr += 1;
  816. }
  817. }
  818. memcpy(ptr, &oversized, sizeof(int8));
  819. ptr += sizeof(int8);
  820. memcpy(ptr, &opcode_val, sizeof(int16));
  821. ptr += sizeof(int16);
  822. if (version <= 283) {
  823. int32 timestamp = Timer::GetCurrentTime2();
  824. memcpy(ptr, &timestamp, sizeof(int32));
  825. }
  826. ptr += sizeof(int32);
  827. ptr += info_size;
  828. memcpy(ptr, pos_changes, tmp_pos_packet_size);
  829. EQ2Packet* ret_packet = new EQ2Packet(OP_ClientCmdMsg, tmp, size);
  830. DumpPacket(ret_packet);
  831. delete[] tmp;
  832. delete[] pos_changes;
  833. return ret_packet;
  834. }
  835. EQ2Packet* Spawn::spawn_update_packet(Player* player, int16 version, bool override_changes, bool override_vis_changes){
  836. if(!player || player->IsPlayer() == false){
  837. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called spawn_update_packet without player!");
  838. return 0;
  839. }
  840. else if((IsPlayer() && info_changed == false && vis_changed == false) || (info_changed == false && vis_changed == false && position_changed == false)){
  841. if(!override_changes && !override_vis_changes)
  842. return 0;
  843. }
  844. static const uchar null_byte = 0;
  845. uchar* info_changes = 0;
  846. uchar* pos_changes = 0;
  847. uchar* vis_changes = 0;
  848. static const int8 oversized = 255;
  849. int16 opcode_val = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqUpdateGhostCmd);
  850. int16 tmp_info_packet_size;
  851. int16 tmp_vis_packet_size;
  852. int16 tmp_pos_packet_size;
  853. //We need to lock these variables up to make this thread safe
  854. m_Update.writelock(__FUNCTION__, __LINE__);
  855. //These variables are set in the spawn_info_changes, pos and vis changes functions
  856. info_packet_size = 1;
  857. pos_packet_size = 1;
  858. vis_packet_size = 1;
  859. if (info_changed || override_changes)
  860. info_changes = spawn_info_changes(player, version);
  861. if ((position_changed || override_changes) && IsPlayer() == false)
  862. pos_changes = spawn_pos_changes(player, version);
  863. if (vis_changed || override_changes || override_vis_changes)
  864. vis_changes = spawn_vis_changes(player, version);
  865. tmp_info_packet_size = info_packet_size;
  866. tmp_pos_packet_size = pos_packet_size;
  867. tmp_vis_packet_size = vis_packet_size;
  868. int32 size = info_packet_size + pos_packet_size + vis_packet_size + 8;
  869. if (version >= 284)
  870. size += 3;
  871. else if (version <= 283 && size >= 255) {//1 byte to 3 for overloaded val
  872. size += 2;
  873. }
  874. uchar* tmp = new uchar[size];
  875. memset(tmp, 0, size);
  876. uchar* ptr = tmp;
  877. if (version >= 284) {
  878. size -= 4;
  879. memcpy(ptr, &size, sizeof(int32));
  880. size += 4;
  881. ptr += sizeof(int32);
  882. }
  883. else {
  884. if (size >= 255) {
  885. memcpy(ptr, &oversized, sizeof(int8));
  886. ptr += sizeof(int8);
  887. size -= 3;
  888. memcpy(ptr, &size, sizeof(int16));
  889. size += 3;
  890. ptr += 3;
  891. }
  892. else {
  893. size -= 1;
  894. memcpy(ptr, &size, sizeof(int8));
  895. ptr += sizeof(int8);
  896. size += 1;
  897. }
  898. }
  899. memcpy(ptr, &oversized, sizeof(int8));
  900. ptr += sizeof(int8);
  901. memcpy(ptr, &opcode_val, sizeof(int16));
  902. ptr += sizeof(int16);
  903. if (IsPlayer() == false || version <= 546) { //this isnt sent for player updates, it is sent on position update
  904. //int32 time = Timer::GetCurrentTime2();
  905. packet_num = Timer::GetCurrentTime2();
  906. memcpy(ptr, &packet_num, sizeof(int32));
  907. }
  908. ptr += sizeof(int32);
  909. memcpy(ptr, info_changes ? info_changes : &null_byte, tmp_info_packet_size);
  910. ptr += info_packet_size;
  911. memcpy(ptr, pos_changes ? pos_changes : &null_byte, tmp_pos_packet_size);
  912. ptr += pos_packet_size;
  913. memcpy(ptr, vis_changes ? vis_changes : &null_byte, tmp_vis_packet_size);
  914. EQ2Packet* ret_packet = 0;
  915. if(info_packet_size + pos_packet_size + vis_packet_size > 0)
  916. ret_packet = new EQ2Packet(OP_ClientCmdMsg, tmp, size);
  917. delete[] tmp;
  918. safe_delete_array(info_changes);
  919. safe_delete_array(vis_changes);
  920. safe_delete_array(pos_changes);
  921. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  922. return ret_packet;
  923. }
  924. uchar* Spawn::spawn_info_changes_ex(Player* player, int16 version) {
  925. int16 index = player->GetIndexForSpawn(this);
  926. PacketStruct* packet = player->GetSpawnInfoStruct();
  927. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  928. packet->ResetData();
  929. InitializeInfoPacketData(player, packet);
  930. string* data = packet->serializeString();
  931. int32 size = data->length();
  932. uchar* xor_info_packet = player->GetTempInfoPacketForXOR();
  933. if (!xor_info_packet || size != player->GetTempInfoXorSize()) {
  934. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateInfoExPacket: %i, %i", size, player->GetTempInfoXorSize());
  935. safe_delete(xor_info_packet);
  936. xor_info_packet = player->SetTempInfoPacketForXOR(size);
  937. }
  938. uchar* orig_packet = player->GetSpawnInfoPacketForXOR(id);
  939. if (orig_packet) {
  940. //if (!IsPlayer() && this->EngagedInCombat())
  941. //packet->PrintPacket();
  942. memcpy(xor_info_packet, (uchar*)data->c_str(), size);
  943. Encode(xor_info_packet, orig_packet, size);
  944. }
  945. bool changed = false;
  946. for (int i = 0; i < size; ++i) {
  947. if (xor_info_packet[i]) {
  948. changed = true;
  949. break;
  950. }
  951. }
  952. if (!changed) {
  953. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  954. return nullptr;
  955. }
  956. uchar* tmp = new uchar[size + 10];
  957. size = Pack(tmp, xor_info_packet, size, size, version);
  958. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  959. int32 orig_size = size;
  960. size -= sizeof(int32);
  961. size += CheckOverLoadSize(index);
  962. info_packet_size = size;
  963. uchar* tmp2 = new uchar[size];
  964. uchar* ptr = tmp2;
  965. ptr += DoOverLoad(index, ptr);
  966. memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
  967. delete[] tmp;
  968. return move(tmp2);
  969. }
  970. uchar* Spawn::spawn_vis_changes_ex(Player* player, int16 version) {
  971. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  972. int16 index = player->GetIndexForSpawn(this);
  973. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  974. uchar* orig_packet = player->GetSpawnVisPacketForXOR(id);
  975. vis_struct->ResetData();
  976. InitializeVisPacketData(player, vis_struct);
  977. string* data = vis_struct->serializeString();
  978. int32 size = data->length();
  979. uchar* xor_vis_packet = player->GetTempVisPacketForXOR();
  980. if (!xor_vis_packet || size != player->GetTempVisXorSize()) {
  981. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiateVisExPacket: %i, %i", size, player->GetTempVisXorSize());
  982. safe_delete(xor_vis_packet);
  983. xor_vis_packet = player->SetTempVisPacketForXOR(size);
  984. }
  985. if (orig_packet) {
  986. //if (!IsPlayer() && this->EngagedInCombat())
  987. // vis_struct->PrintPacket();
  988. memcpy(xor_vis_packet, (uchar*)data->c_str(), size);
  989. Encode(xor_vis_packet, orig_packet, size);
  990. }
  991. bool changed = false;
  992. for (int i = 0; i < size; ++i) {
  993. if (xor_vis_packet[i]) {
  994. changed = true;
  995. break;
  996. }
  997. }
  998. if (!changed) {
  999. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  1000. return nullptr;
  1001. }
  1002. uchar* tmp = new uchar[size + 10];
  1003. size = Pack(tmp, xor_vis_packet, size, size, version);
  1004. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  1005. int32 orig_size = size;
  1006. size -= sizeof(int32);
  1007. size += CheckOverLoadSize(index);
  1008. vis_packet_size = size;
  1009. uchar* tmp2 = new uchar[size];
  1010. uchar* ptr = tmp2;
  1011. ptr += DoOverLoad(index, ptr);
  1012. memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
  1013. delete[] tmp;
  1014. return move(tmp2);
  1015. }
  1016. uchar* Spawn::spawn_pos_changes_ex(Player* player, int16 version) {
  1017. int16 index = player->GetIndexForSpawn(this);
  1018. PacketStruct* packet = player->GetSpawnPosStruct();
  1019. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  1020. uchar* orig_packet = player->GetSpawnPosPacketForXOR(id);
  1021. packet->ResetData();
  1022. InitializePosPacketData(player, packet);
  1023. string* data = packet->serializeString();
  1024. int32 size = data->length();
  1025. uchar* xor_pos_packet = player->GetTempPosPacketForXOR();
  1026. if (!xor_pos_packet || size != player->GetTempPosXorSize()) {
  1027. LogWrite(ZONE__DEBUG, 0, "Zone", "InstantiatePosExPacket: %i, %i", size, player->GetTempPosXorSize());
  1028. safe_delete(xor_pos_packet);
  1029. xor_pos_packet = player->SetTempPosPacketForXOR(size);
  1030. }
  1031. if (orig_packet) {
  1032. //if (!IsPlayer() && this->EngagedInCombat())
  1033. // packet->PrintPacket();
  1034. memcpy(xor_pos_packet, (uchar*)data->c_str(), size);
  1035. Encode(xor_pos_packet, orig_packet, size);
  1036. }
  1037. bool changed = false;
  1038. for (int i = 0; i < size; ++i) {
  1039. if (xor_pos_packet[i]) {
  1040. changed = true;
  1041. break;
  1042. }
  1043. }
  1044. if (!changed) {
  1045. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  1046. return nullptr;
  1047. }
  1048. uchar* tmp = new uchar[size + 10];
  1049. size = Pack(tmp, xor_pos_packet, size, size, version);
  1050. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  1051. int32 orig_size = size;
  1052. if (version >= 1188) {
  1053. size += 1;
  1054. }
  1055. if (IsPlayer() && version > 546) {
  1056. size += 4;
  1057. }
  1058. size -= sizeof(int32);
  1059. size += CheckOverLoadSize(index);
  1060. pos_packet_size = size;
  1061. uchar* tmp2 = new uchar[size];
  1062. uchar* ptr = tmp2;
  1063. ptr += DoOverLoad(index, ptr);
  1064. // extra byte in coe+ clients, 0 for NPC's 1 for Players
  1065. int8 x = 0;
  1066. if (version > 546) {
  1067. if (IsPlayer()) {
  1068. if (version >= 1188) {
  1069. x = 1;
  1070. memcpy(ptr, &x, sizeof(int8));
  1071. ptr += sizeof(int8);
  1072. }
  1073. int32 now = Timer::GetCurrentTime2();
  1074. memcpy(ptr, &now, sizeof(int32));
  1075. ptr += sizeof(int32);
  1076. }
  1077. else if (version >= 1188) {
  1078. memcpy(ptr, &x, sizeof(int8));
  1079. ptr += sizeof(int8);
  1080. }
  1081. }
  1082. memcpy(ptr, tmp + sizeof(int32), orig_size - sizeof(int32));
  1083. delete[] tmp;
  1084. return move(tmp2);
  1085. }
  1086. EQ2Packet* Spawn::serialize(Player* player, int16 version){
  1087. return 0;
  1088. }
  1089. Spawn* Spawn::GetTarget(){
  1090. Spawn* ret = 0;
  1091. // only attempt to get a spawn if we had a target stored
  1092. if (target != 0)
  1093. {
  1094. ret = GetZone()->GetSpawnByID(target);
  1095. if (!ret)
  1096. target = 0;
  1097. }
  1098. return ret;
  1099. }
  1100. void Spawn::SetTarget(Spawn* spawn){
  1101. SetInfo(&target, spawn ? spawn->GetID() : 0);
  1102. }
  1103. Spawn* Spawn::GetLastAttacker() {
  1104. Spawn* ret = 0;
  1105. ret = GetZone()->GetSpawnByID(last_attacker);
  1106. if (!ret)
  1107. last_attacker = 0;
  1108. return ret;
  1109. }
  1110. void Spawn::SetLastAttacker(Spawn* spawn){
  1111. last_attacker = spawn->GetID();
  1112. }
  1113. void Spawn::SetInvulnerable(bool val){
  1114. invulnerable = val;
  1115. }
  1116. bool Spawn::GetInvulnerable(){
  1117. return invulnerable;
  1118. }
  1119. bool Spawn::TakeDamage(int32 damage){
  1120. if(invulnerable)
  1121. return false;
  1122. if (IsEntity()) {
  1123. if (((Entity*)this)->IsMezzed())
  1124. ((Entity*)this)->RemoveAllMezSpells();
  1125. if (damage == 0)
  1126. return true;
  1127. }
  1128. int32 hp = GetHP();
  1129. if(damage >= hp) {
  1130. SetHP(0);
  1131. if (IsPlayer()) {
  1132. ((Player*)this)->InCombat(false);
  1133. ((Player*)this)->SetRangeAttack(false);
  1134. GetZone()->TriggerCharSheetTimer(); // force char sheet updates now
  1135. }
  1136. }
  1137. else {
  1138. SetHP(hp - damage);
  1139. // if player flag the char sheet as changed so the ui updates properly
  1140. if (IsPlayer())
  1141. ((Player*)this)->SetCharSheetChanged(true);
  1142. }
  1143. return true;
  1144. }
  1145. void Spawn::TakeDamage(Spawn* attacker, int32 damage){
  1146. if (TakeDamage(damage))
  1147. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_HEALTHCHANGED, attacker);
  1148. SetLastAttacker(attacker);
  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)
  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_age", entity->features.body_age);
  2129. }
  2130. else {
  2131. EQ2_Color empty;
  2132. empty.red = 255;
  2133. empty.blue = 255;
  2134. empty.green = 255;
  2135. packet->setColorByName("skin_color", empty);
  2136. packet->setColorByName("model_color", empty);
  2137. packet->setColorByName("eye_color", empty);
  2138. packet->setColorByName("soga_skin_color", empty);
  2139. packet->setColorByName("soga_model_color", empty);
  2140. packet->setColorByName("soga_eye_color", empty);
  2141. }
  2142. if (appearance.icon == 0) {
  2143. if (appearance.attackable == 1)
  2144. appearance.icon = 0;
  2145. else if (appearance.encounter_level > 0)
  2146. appearance.icon = 4;
  2147. else
  2148. appearance.icon = 6;
  2149. }
  2150. // If Coe+ clients modify the values before we send
  2151. // if not then just send the value we have.
  2152. int8 temp_icon = appearance.icon;
  2153. //Check if we need to add the hand icon..
  2154. if ((temp_icon & 6) != 6 && appearance.display_hand_icon) {
  2155. temp_icon |= 6;
  2156. }
  2157. //Icon value 28 for boats, set this without modifying the value
  2158. if (version >= 1188 && temp_icon != 28) {
  2159. if ((temp_icon & 64) > 0) {
  2160. temp_icon -= 64; // remove the DoV value;
  2161. temp_icon += 128; // add the CoE value
  2162. }
  2163. if ((temp_icon & 32) > 0) {
  2164. temp_icon -= 32; // remove the DoV value;
  2165. temp_icon += 64; // add the CoE value
  2166. }
  2167. if ((temp_icon & 4) > 0) {
  2168. temp_icon -= 4; // remove DoV value
  2169. temp_icon += 8; // add the CoE icon
  2170. }
  2171. if ((temp_icon & 6) > 0) {
  2172. temp_icon -= 10; // remove DoV value
  2173. temp_icon += 12; // add the CoE icon
  2174. }
  2175. }
  2176. packet->setDataByName("icon", temp_icon);//appearance.icon);
  2177. int32 temp_activity_status = 0;
  2178. if (!Alive() && GetTotalHP() > 0 && !IsObject() && !IsGroundSpawn())
  2179. temp_activity_status = 1;
  2180. temp_activity_status += (IsNPC() || IsObject() || IsGroundSpawn()) ? 1 << 1 : 0;
  2181. if (version >= 1188) {
  2182. // Fix widget or sign having 'Play Legends of Norrath' or 'Tell' options in right click (client hard-coded entity commands)
  2183. if(IsWidget() || IsSign())
  2184. temp_activity_status = 2;
  2185. if (IsGroundSpawn() || GetShowHandIcon())
  2186. temp_activity_status += ACTIVITY_STATUS_INTERACTABLE_1188;
  2187. if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
  2188. temp_activity_status += ACTIVITY_STATUS_ROLEPLAYING_1188;
  2189. if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
  2190. temp_activity_status += ACTIVITY_STATUS_ANONYMOUS_1188;
  2191. if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
  2192. temp_activity_status += ACTIVITY_STATUS_LINKDEAD_1188;
  2193. if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
  2194. temp_activity_status += ACTIVITY_STATUS_CAMPING_1188;
  2195. if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
  2196. temp_activity_status += ACTIVITY_STATUS_LFG_1188;
  2197. if ((appearance.activity_status & ACTIVITY_STATUS_LFW) > 0)
  2198. temp_activity_status += ACTIVITY_STATUS_LFW_1188;
  2199. if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
  2200. temp_activity_status += ACTIVITY_STATUS_SOLID_1188;
  2201. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_GAINED) > 0)
  2202. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_GAINED_1188;
  2203. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_REMAINING) > 0)
  2204. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_REMAINING_1188;
  2205. if ((appearance.activity_status & ACTIVITY_STATUS_AFK) > 0)
  2206. temp_activity_status += ACTIVITY_STATUS_AFK_1188;
  2207. if (EngagedInCombat())
  2208. temp_activity_status += ACTIVITY_STATUS_INCOMBAT_1188;
  2209. // if this is either a boat or lift let the client be manipulated by the object
  2210. // doesn't work for DoF client version 546
  2211. if (this->GetModelType() == 7941 || appearance.icon == 28 || appearance.icon == 12)
  2212. {
  2213. temp_activity_status += ACTIVITY_STATUS_ISTRANSPORT_1188;
  2214. }
  2215. }
  2216. else
  2217. {
  2218. temp_activity_status = appearance.activity_status;
  2219. if(IsNPC())
  2220. temp_activity_status = 0xFF;
  2221. // this only partially fixes lifts in classic 283 client if you move just as the lift starts to move
  2222. if (this->GetModelType() == 7941 || appearance.icon == 28 || appearance.icon == 12)
  2223. packet->setDataByName("is_transport", 1);
  2224. if (MeetsSpawnAccessRequirements(spawn))
  2225. packet->setDataByName("hand_icon", appearance.display_hand_icon);
  2226. else {
  2227. if ((req_quests_override & 256) > 0)
  2228. packet->setDataByName("hand_icon", 1);
  2229. }
  2230. if (IsPlayer()) {
  2231. if (((Player*)this)->get_character_flag(CF_AFK))
  2232. packet->setDataByName("afk", 1);
  2233. if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
  2234. packet->setDataByName("roleplaying", 1);
  2235. if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
  2236. packet->setDataByName("anonymous", 1);
  2237. if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
  2238. packet->setDataByName("linkdead", 1);
  2239. if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
  2240. packet->setDataByName("camping", 1);
  2241. if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
  2242. packet->setDataByName("lfg", 1);
  2243. }
  2244. if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
  2245. packet->setDataByName("solid_object", 1);
  2246. }
  2247. packet->setDataByName("activity_status", temp_activity_status); //appearance.activity_status);
  2248. // If player and player has a follow target
  2249. /* 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.
  2250. ** 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?)
  2251. ** for now disabling this, if DoF needs it enabled for whatever reason then we need a version check added.
  2252. */
  2253. if (IsPlayer()) {
  2254. if (((Player*)this)->GetFollowTarget())
  2255. packet->setDataByName("follow_target", ((((Player*)this)->GetIDWithPlayerSpawn(((Player*)this)->GetFollowTarget()) * -1) - 1));
  2256. //else
  2257. // packet->setDataByName("follow_target", 0xFFFFFFFF);
  2258. }
  2259. //else if (!IsPet()) {
  2260. // packet->setDataByName("follow_target", 0xFFFFFFFF);
  2261. //}
  2262. if (GetTarget() && GetTarget()->GetTargetable())
  2263. packet->setDataByName("target_id", ((spawn->GetIDWithPlayerSpawn(GetTarget()) * -1) - 1));
  2264. else
  2265. packet->setDataByName("target_id", 0xFFFFFFFF);
  2266. //Send spell effects for target window
  2267. if(IsEntity()){
  2268. InfoStruct* info = ((Entity*)this)->GetInfoStruct();
  2269. int8 i = 0;
  2270. int16 backdrop = 0;
  2271. int16 spell_icon = 0;
  2272. int32 spell_id = 0;
  2273. LuaSpell* spell = 0;
  2274. ((Entity*)this)->GetSpellEffectMutex()->readlock(__FUNCTION__, __LINE__);
  2275. while(i < 30){
  2276. //Change value of spell id for this packet if spell exists
  2277. spell_id = info->spell_effects[i].spell_id;
  2278. if(spell_id > 0)
  2279. spell_id = 0xFFFFFFFF - spell_id;
  2280. else
  2281. spell_id = 0;
  2282. packet->setSubstructDataByName("spell_effects", "spell_id", spell_id, i);
  2283. //Change value of spell icon for this packet if spell exists
  2284. spell_icon = info->spell_effects[i].icon;
  2285. if(spell_icon > 0){
  2286. if(!(spell_icon == 0xFFFF))
  2287. spell_icon = 0xFFFF - spell_icon;
  2288. }
  2289. else
  2290. spell_icon = 0;
  2291. packet->setSubstructDataByName("spell_effects", "spell_icon", spell_icon, i);
  2292. //Change backdrop values to match values in this packet
  2293. backdrop = info->spell_effects[i].icon_backdrop;
  2294. switch(backdrop){
  2295. case 312:
  2296. backdrop = 33080;
  2297. break;
  2298. case 313:
  2299. backdrop = 33081;
  2300. break;
  2301. case 314:
  2302. backdrop = 33082;
  2303. break;
  2304. case 315:
  2305. backdrop = 33083;
  2306. break;
  2307. case 316:
  2308. backdrop = 33084;
  2309. break;
  2310. case 317:
  2311. backdrop = 33085;
  2312. break;
  2313. case (318 || 319):
  2314. backdrop = 33086;
  2315. break;
  2316. default:
  2317. break;
  2318. }
  2319. packet->setSubstructDataByName("spell_effects", "spell_icon_backdrop", backdrop, i);
  2320. spell = info->spell_effects[i].spell;
  2321. if (spell)
  2322. packet->setSubstructDataByName("spell_effects", "spell_triggercount", spell->num_triggers, i);
  2323. i++;
  2324. }
  2325. ((Entity*)this)->GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
  2326. }
  2327. }
  2328. void Spawn::MoveToLocation(Spawn* spawn, float distance, bool immediate, bool mapped){
  2329. if(!spawn)
  2330. return;
  2331. SetRunningTo(spawn);
  2332. FaceTarget(spawn);
  2333. if (!IsPlayer() && distance > 0.0f)
  2334. {
  2335. if ((IsFlyingCreature() || IsWaterCreature()) && CheckLoS(spawn))
  2336. {
  2337. if (immediate)
  2338. ClearRunningLocations();
  2339. AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", true);
  2340. }
  2341. else if (/*!mapped && */GetZone())
  2342. {
  2343. GetZone()->movementMgr->NavigateTo((Entity*)this, spawn->GetX(), spawn->GetY(), spawn->GetZ());
  2344. last_grid_update = Timer::GetCurrentTime2();
  2345. }
  2346. else
  2347. {
  2348. if (immediate)
  2349. ClearRunningLocations();
  2350. AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", mapped);
  2351. }
  2352. }
  2353. }
  2354. void Spawn::ProcessMovement(bool isSpawnListLocked){
  2355. CheckProximities();
  2356. if(IsPlayer()){
  2357. //Check if player is riding a boat, if so update pos (boat's current location + XYZ offsets)
  2358. Player* player = ((Player*)this);
  2359. int32 boat_id = player->GetBoatSpawn();
  2360. Spawn* boat = 0;
  2361. if(boat_id > 0)
  2362. boat = GetZone()->GetSpawnByID(boat_id, isSpawnListLocked);
  2363. //TODO: MAYBE do this for real boats, not lifts... GetWidgetTypeNameByTypeID
  2364. /*if(boat){
  2365. SetX(boat->GetX() + player->GetBoatX());
  2366. SetY(boat->GetY() + player->GetBoatY());
  2367. SetZ(boat->GetZ() + player->GetBoatZ());
  2368. }*/
  2369. return;
  2370. }
  2371. if (forceMapCheck && GetZone() != nullptr && GetMap() != nullptr && GetMap()->IsMapLoaded())
  2372. {
  2373. FixZ(true);
  2374. int32 newGrid = GetMap()->GetGrid()->GetGridID(this);
  2375. if (!IsFlyingCreature() && newGrid != 0 && newGrid != appearance.pos.grid_id)
  2376. SetPos(&(appearance.pos.grid_id), newGrid);
  2377. forceMapCheck = false;
  2378. }
  2379. if (GetHP() <= 0 && !IsWidget())
  2380. return;
  2381. if (EngagedInCombat())
  2382. {
  2383. int locations = 0;
  2384. if (movement_locations && MMovementLocations)
  2385. {
  2386. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2387. locations = movement_locations->size();
  2388. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2389. }
  2390. if (locations < 1 && GetZone() && ((Entity*)this)->IsFeared())
  2391. {
  2392. CalculateNewFearpoint();
  2393. }
  2394. }
  2395. Spawn* followTarget = GetZone()->GetSpawnByID(m_followTarget, isSpawnListLocked);
  2396. if (!followTarget && m_followTarget > 0)
  2397. m_followTarget = 0;
  2398. if (following && !IsPauseMovementTimerActive() && followTarget && !((Entity*)this)->IsFeared()) {
  2399. // Need to clear m_followTarget before the zoneserver deletes it
  2400. if (followTarget->GetHP() <= 0) {
  2401. followTarget = 0;
  2402. return;
  2403. }
  2404. if (!IsEntity() || (!((Entity*)this)->IsCasting() && !((Entity*)this)->IsMezzedOrStunned() && !((Entity*)this)->IsRooted())) {
  2405. if (GetBaseSpeed() > 0) {
  2406. CalculateRunningLocation();
  2407. }
  2408. else {
  2409. float speed = 4.0f;
  2410. if (IsEntity())
  2411. speed = ((Entity*)this)->GetMaxSpeed();
  2412. if (IsEntity())
  2413. ((Entity*)this)->SetSpeed(speed);
  2414. SetSpeed(speed);
  2415. }
  2416. MovementLocation* loc = GetCurrentRunningLocation();
  2417. float dist = GetDistance(followTarget, true);
  2418. if ((!EngagedInCombat() && m_followDistance > 0 && dist <= m_followDistance) ||
  2419. (dist <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
  2420. ClearRunningLocations();
  2421. CalculateRunningLocation(true);
  2422. }
  2423. else if (loc) {
  2424. float distance = GetDistance(followTarget, loc->x, loc->y, loc->z);
  2425. if ( (!EngagedInCombat() && m_followDistance > 0 && distance > m_followDistance) ||
  2426. ( EngagedInCombat() && distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
  2427. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat(), true, loc->mapped);
  2428. CalculateRunningLocation();
  2429. }
  2430. }
  2431. else {
  2432. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat(), false);
  2433. CalculateRunningLocation();
  2434. }
  2435. }
  2436. }
  2437. bool movementCase = false;
  2438. // Movement loop is only for scripted paths
  2439. if(!EngagedInCombat() && !IsPauseMovementTimerActive() && !NeedsToResumeMovement() && (!IsNPC() || !((NPC*)this)->m_runningBack)){
  2440. MMovementLoop.writelock();
  2441. if(movement_loop.size() > 0 && movement_index < movement_loop.size())
  2442. {
  2443. movementCase = true;
  2444. // Get the target location
  2445. MovementData* data = movement_loop[movement_index];
  2446. // need to resume our movement
  2447. if(resume_movement){
  2448. if (movement_locations){
  2449. while (movement_locations->size()){
  2450. safe_delete(movement_locations->front());
  2451. movement_locations->pop_front();
  2452. }
  2453. movement_locations->clear();
  2454. }
  2455. data = movement_loop[movement_index];
  2456. if(data)
  2457. {
  2458. ((Entity*)this)->SetSpeed(data->speed);
  2459. SetSpeed(data->speed);
  2460. if(!IsWidget())
  2461. FaceTarget(data->x, data->z);
  2462. // 0 delay at target location, need to set multiple locations
  2463. if(data->delay == 0 && movement_loop.size() > 0) {
  2464. int16 tmp_index = movement_index+1;
  2465. MovementData* data2 = 0;
  2466. if(tmp_index < movement_loop.size())
  2467. data2 = movement_loop[tmp_index];
  2468. else
  2469. data2 = movement_loop[0];
  2470. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  2471. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  2472. }
  2473. // delay at target location, only need to set 1 location
  2474. else
  2475. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, true, "", true);
  2476. }
  2477. movement_start_time = 0;
  2478. resume_movement = false;
  2479. }
  2480. // If we are not moving or we have arrived at our destination
  2481. else if(!IsRunning() || (data && data->x == GetX() && data->y == GetY() && data->z == GetZ())){
  2482. // If we were moving remove the last running location (the point we just arrived at)
  2483. if(IsRunning())
  2484. RemoveRunningLocation();
  2485. // If this waypoint has a delay and we just arrived here (movement_start_time == 0)
  2486. if(data && data->delay > 0 && movement_start_time == 0){
  2487. // Set the current time
  2488. movement_start_time = Timer::GetCurrentTime2();
  2489. // If this waypoint had a lua function then call it
  2490. if(data->lua_function.length() > 0)
  2491. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2492. int16 nextMove;
  2493. if ((int16)(movement_index + 1) < movement_loop.size())
  2494. nextMove = movement_index + 1;
  2495. else
  2496. nextMove = 0;
  2497. // Get the next target location
  2498. data = movement_loop[nextMove];
  2499. //Go ahead and face the next location
  2500. FaceTarget(data->x, data->z);
  2501. }
  2502. // If this waypoint has no delay or we have waited the required time (current time >= delay + movement_start_time)
  2503. else if(data && data->delay == 0 || (data && data->delay > 0 && Timer::GetCurrentTime2() >= (data->delay+movement_start_time))) {
  2504. // if no delay at this waypoint but a lua function for it then call the function
  2505. if(data->delay == 0 && data->lua_function.length() > 0)
  2506. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2507. // since we ran a lua function make sure the movement loop is still alive and accurate
  2508. if(movement_loop.size() > 0)
  2509. {
  2510. // Advance the current movement loop index
  2511. if((int16)(movement_index+1) < movement_loop.size())
  2512. movement_index++;
  2513. else
  2514. movement_index = 0;
  2515. // Get the next target location
  2516. data = movement_loop[movement_index];
  2517. // set the speed for that location
  2518. SetSpeed(data->speed);
  2519. if(!IsWidget())
  2520. // turn towards the location
  2521. FaceTarget(data->x, data->z);
  2522. // If 0 delay at location get and set data for the point after it
  2523. if(data->delay == 0 && movement_loop.size() > 0){
  2524. while (movement_locations->size()){
  2525. safe_delete(movement_locations->front());
  2526. movement_locations->pop_front();
  2527. }
  2528. // clear current target locations
  2529. movement_locations->clear();
  2530. // get the data for the location after out new location
  2531. int16 tmp_index = movement_index+1;
  2532. MovementData* data2 = 0;
  2533. if(tmp_index < movement_loop.size())
  2534. data2 = movement_loop[tmp_index];
  2535. else
  2536. data2 = movement_loop[0];
  2537. // set the first location (adds it to movement_locations that we just cleared)
  2538. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  2539. // set the location after that
  2540. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  2541. }
  2542. // there is a delay at the next location so we only need to set it
  2543. else
  2544. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, true, "", true);
  2545. // reset this timer to 0 now that we are moving again
  2546. movement_start_time = 0;
  2547. }
  2548. }
  2549. }
  2550. // moving and not at target location yet
  2551. else if(GetBaseSpeed() > 0)
  2552. CalculateRunningLocation();
  2553. // not moving, have a target location but not at it yet
  2554. else if (data) {
  2555. SetSpeed(data->speed);
  2556. AddRunningLocation(data->x, data->y, data->z, data->speed);
  2557. }
  2558. }
  2559. MMovementLoop.releasewritelock();
  2560. }
  2561. if (!movementCase && IsRunning() && !IsPauseMovementTimerActive()) {
  2562. CalculateRunningLocation();
  2563. }
  2564. /*else if (IsNPC() && !IsRunning() && !EngagedInCombat() && ((NPC*)this)->GetRunbackLocation()) {
  2565. // Is an npc that is not moving and not engaged in combat but has a run back location set then clear the runback location
  2566. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Clear runback location for %s", GetName());
  2567. ((NPC*)this)->ClearRunback();
  2568. resume_movement = true;
  2569. NeedsToResumeMovement(false);
  2570. }*/
  2571. }
  2572. void Spawn::ResetMovement(bool inFlight){
  2573. if(!inFlight)
  2574. MMovementLoop.writelock();
  2575. vector<MovementData*>::iterator itr;
  2576. for(itr = movement_loop.begin(); itr != movement_loop.end(); itr++){
  2577. safe_delete(*itr);
  2578. }
  2579. movement_loop.clear();
  2580. movement_index = 0;
  2581. resume_movement = true;
  2582. if(!inFlight)
  2583. MMovementLoop.releasewritelock();
  2584. }
  2585. void Spawn::AddMovementLocation(float x, float y, float z, float speed, int16 delay, const char* lua_function){
  2586. LogWrite(LUA__DEBUG, 5, "LUA", "AddMovementLocation: x: %.2f, y: %.2f, z: %.2f, speed: %.2f, delay: %i, lua: %s",
  2587. x, y, z, speed, delay, string(lua_function).c_str());
  2588. MovementData* data = new MovementData;
  2589. data->x = x;
  2590. data->y = y;
  2591. data->z = z;
  2592. data->speed = speed;
  2593. data->delay = delay*1000;
  2594. if(lua_function)
  2595. data->lua_function = string(lua_function);
  2596. MMovementLoop.lock();
  2597. movement_loop.push_back(data);
  2598. MMovementLoop.unlock();
  2599. }
  2600. bool Spawn::IsRunning(){
  2601. if(movement_locations && movement_locations->size() > 0)
  2602. return true;
  2603. else
  2604. return false;
  2605. }
  2606. void Spawn::RunToLocation(float x, float y, float z, float following_x, float following_y, float following_z){
  2607. if(IsPauseMovementTimerActive())
  2608. return;
  2609. if(!IsWidget())
  2610. FaceTarget(x, z);
  2611. SetPos(&appearance.pos.X2, x, false);
  2612. SetPos(&appearance.pos.Z2, z, false);
  2613. SetPos(&appearance.pos.Y2, y, false);
  2614. if(following_x == 0 && following_y == 0 && following_z == 0){
  2615. SetPos(&appearance.pos.X3, x, false);
  2616. SetPos(&appearance.pos.Z3, z, false);
  2617. SetPos(&appearance.pos.Y3, y, false);
  2618. }
  2619. else{
  2620. SetPos(&appearance.pos.X3, following_x, false);
  2621. SetPos(&appearance.pos.Y3, following_y, false);
  2622. SetPos(&appearance.pos.Z3, following_z, false);
  2623. }
  2624. position_changed = true;
  2625. changed = true;
  2626. GetZone()->AddChangedSpawn(this);
  2627. }
  2628. MovementLocation* Spawn::GetCurrentRunningLocation(){
  2629. MovementLocation* ret = 0;
  2630. if(movement_locations && movement_locations->size() > 0){
  2631. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2632. ret = movement_locations->front();
  2633. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2634. }
  2635. return ret;
  2636. }
  2637. MovementLocation* Spawn::GetLastRunningLocation(){
  2638. MovementLocation* ret = 0;
  2639. if(movement_locations && movement_locations->size() > 0){
  2640. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2641. ret = movement_locations->back();
  2642. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2643. }
  2644. return ret;
  2645. }
  2646. 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){
  2647. if(speed == 0)
  2648. return;
  2649. if ( IsEntity() )
  2650. ((Entity*)this)->SetSpeed(speed);
  2651. else
  2652. this->SetSpeed(speed);
  2653. MovementLocation* current_location = 0;
  2654. float distance = GetDistance(x, y, z, distance_away != 0);
  2655. if(distance_away != 0){
  2656. distance -= distance_away;
  2657. x = x - (GetX() - x)*distance_away/distance;
  2658. z = z - (GetZ() - z)*distance_away/distance;
  2659. }
  2660. if(!movement_locations){
  2661. movement_locations = new deque<MovementLocation*>();
  2662. MMovementLocations = new Mutex();
  2663. }
  2664. MovementLocation* data = new MovementLocation;
  2665. data->mapped = isMapped;
  2666. data->x = x;
  2667. data->y = y;
  2668. data->z = z;
  2669. data->speed = speed;
  2670. data->attackable = attackable;
  2671. data->lua_function = lua_function;
  2672. data->gridid = 0; // used for runback defaults
  2673. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2674. if(movement_locations->size() > 0)
  2675. current_location = movement_locations->back();
  2676. if(!current_location){
  2677. SetSpawnOrigX(GetX());
  2678. SetSpawnOrigY(GetY());
  2679. SetSpawnOrigZ(GetZ());
  2680. SetSpawnOrigHeading(GetHeading());
  2681. }
  2682. movement_locations->push_back(data);
  2683. if(!IsPauseMovementTimerActive() && finished_adding_locations){
  2684. current_location = movement_locations->front();
  2685. SetSpeed(current_location->speed);
  2686. if(movement_locations->size() > 1){
  2687. data = movement_locations->at(1);
  2688. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2689. }
  2690. else
  2691. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2692. }
  2693. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2694. }
  2695. bool Spawn::RemoveRunningLocation(){
  2696. bool ret = false;
  2697. if(movement_locations){
  2698. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  2699. if(movement_locations->size() > 0){
  2700. delete movement_locations->front();
  2701. movement_locations->pop_front();
  2702. ret = true;
  2703. }
  2704. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  2705. }
  2706. return ret;
  2707. }
  2708. void Spawn::ClearRunningLocations(){
  2709. while(RemoveRunningLocation()){}
  2710. }
  2711. bool Spawn::CalculateChange(){
  2712. bool remove_needed = false;
  2713. if(movement_locations && MMovementLocations){
  2714. MovementLocation* data = 0;
  2715. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2716. if(movement_locations->size() > 0){
  2717. // Target location
  2718. data = movement_locations->front();
  2719. // If no target or we are at the target location need to remove this point
  2720. if(!data || (data->x == GetX() && data->y == GetY() && data->z == GetZ()))
  2721. remove_needed = true;
  2722. if(data){
  2723. if(NeedsToResumeMovement()){
  2724. resume_movement = true;
  2725. NeedsToResumeMovement(false);
  2726. }
  2727. if(!data->attackable)
  2728. SetHeading(GetSpawnOrigHeading());
  2729. }
  2730. }
  2731. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2732. if(remove_needed) {
  2733. if (data && data->lua_function.length() > 0)
  2734. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2735. RemoveRunningLocation();
  2736. //CalculateChange();
  2737. }
  2738. else if(data){
  2739. // Speed is per second so we need a time_step (amount of time since the last update) to modify movement by
  2740. 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
  2741. // Get current location
  2742. float nx = GetX();
  2743. float ny = GetY();
  2744. float nz = GetZ();
  2745. // Get Forward vecotr
  2746. float tar_vx = data->x - nx;
  2747. float tar_vy = data->y - ny;
  2748. float tar_vz = data->z - nz;
  2749. // Multiply speed by the time_step to get how much should have changed over the last tick
  2750. float speed = GetSpeed() * time_step;
  2751. // Normalize the forward vector and multiply by speed, this gives us our change in coords, just need to add them to our current coords
  2752. float len = sqrtf(tar_vx * tar_vx + tar_vy * tar_vy + tar_vz * tar_vz);
  2753. tar_vx = (tar_vx / len) * speed;
  2754. tar_vy = (tar_vy / len) * speed;
  2755. tar_vz = (tar_vz / len) * speed;
  2756. // Distance less then 0.5 just set the npc to the target location
  2757. if (GetDistance(data->x, data->y, data->z, IsWidget() ? false : true) <= 0.5f) {
  2758. SetX(data->x, false);
  2759. SetZ(data->z, false);
  2760. SetY(data->y, false, true);
  2761. }
  2762. else {
  2763. SetX(nx + tar_vx, false);
  2764. SetZ(nz + tar_vz, false);
  2765. if ( IsWidget() )
  2766. SetY(ny + tar_vy, false, true);
  2767. else
  2768. SetY(ny + tar_vy, false);
  2769. }
  2770. if (GetMap() != nullptr) {
  2771. Cell* newCell = GetMap()->GetGrid()->GetCell(GetX(), GetZ());
  2772. int32 newGrid = GetMap()->GetGrid()->GetGridID(this);
  2773. if (!IsFlyingCreature() && newGrid != 0 && newGrid != appearance.pos.grid_id)
  2774. SetPos(&(appearance.pos.grid_id), newGrid);
  2775. }
  2776. }
  2777. }
  2778. return remove_needed;
  2779. }
  2780. void Spawn::CalculateRunningLocation(bool stop){
  2781. bool pauseTimerEnabled = IsPauseMovementTimerActive();
  2782. if (!pauseTimerEnabled && !stop && (last_location_update + 100) > Timer::GetCurrentTime2())
  2783. return;
  2784. else if (!pauseTimerEnabled && !stop)
  2785. last_location_update = Timer::GetCurrentTime2();
  2786. bool removed = CalculateChange();
  2787. if (stop || pauseTimerEnabled) {
  2788. //following = false;
  2789. SetPos(&appearance.pos.X2, GetX(), false);
  2790. SetPos(&appearance.pos.Y2, GetY(), false);
  2791. SetPos(&appearance.pos.Z2, GetZ(), false);
  2792. SetPos(&appearance.pos.X3, GetX(), false);
  2793. SetPos(&appearance.pos.Y3, GetY(), false);
  2794. SetPos(&appearance.pos.Z3, GetZ(), false);
  2795. }
  2796. else if (removed && movement_locations && movement_locations->size() > 0) {
  2797. if (MMovementLocations)
  2798. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  2799. MovementLocation* current_location = movement_locations->at(0);
  2800. if (movement_locations->size() > 1) {
  2801. MovementLocation* data = movement_locations->at(1);
  2802. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2803. }
  2804. else
  2805. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2806. if (MMovementLocations)
  2807. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  2808. }
  2809. else if (GetZone() && GetTarget() != NULL && EngagedInCombat())
  2810. {
  2811. if (GetDistance(GetTarget()) > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  2812. {
  2813. if ((IsFlyingCreature() || IsWaterCreature()) && CheckLoS(GetTarget()))
  2814. AddRunningLocation(GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ(), GetSpeed(), 0, false);
  2815. else
  2816. GetZone()->movementMgr->NavigateTo((Entity*)this, GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ());
  2817. }
  2818. else
  2819. ((Entity*)this)->HaltMovement();
  2820. }
  2821. }
  2822. float Spawn::GetFaceTarget(float x, float z) {
  2823. float angle;
  2824. double diff_x = x - GetX();
  2825. double diff_z = z - GetZ();
  2826. //If we're very close to the same spot don't bother changing heading
  2827. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  2828. return GetHeading();
  2829. }
  2830. if (diff_z == 0) {
  2831. if (diff_x > 0)
  2832. angle = 90;
  2833. else
  2834. angle = 270;
  2835. }
  2836. else
  2837. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  2838. if (angle < 0)
  2839. angle = angle + 360;
  2840. else
  2841. angle = angle + 180;
  2842. if (diff_x < 0)
  2843. angle = angle + 180;
  2844. if (last_heading_angle == angle) return angle;
  2845. return angle;
  2846. }
  2847. void Spawn::FaceTarget(float x, float z){
  2848. float angle;
  2849. double diff_x = x - GetX();
  2850. double diff_z = z - GetZ();
  2851. //If we're very close to the same spot don't bother changing heading
  2852. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  2853. return;
  2854. }
  2855. if(diff_z==0){
  2856. if(diff_x > 0)
  2857. angle = 90;
  2858. else
  2859. angle = 270;
  2860. }
  2861. else
  2862. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  2863. if(angle < 0)
  2864. angle = angle + 360;
  2865. else
  2866. angle = angle + 180;
  2867. if(diff_x < 0)
  2868. angle = angle + 180;
  2869. if (last_heading_angle == angle) return;
  2870. SetHeading(angle);
  2871. }
  2872. void Spawn::FaceTarget(Spawn* target, bool disable_action_state){
  2873. if(!target)
  2874. return;
  2875. FaceTarget(target->GetX(), target->GetZ());
  2876. if(GetHP() > 0 && target->IsPlayer() && !EngagedInCombat()){
  2877. GetZone()->AddHeadingTimer(this);
  2878. if(disable_action_state)
  2879. SetTempActionState(0);
  2880. }
  2881. }
  2882. bool Spawn::MeetsSpawnAccessRequirements(Player* player){
  2883. bool ret = false;
  2884. Quest* quest = 0;
  2885. //Check if we meet all quest requirements first..
  2886. m_requiredQuests.readlock(__FUNCTION__, __LINE__);
  2887. if (player && required_quests.size() > 0) {
  2888. map<int32, vector<int16>* >::iterator itr;
  2889. for (itr = required_quests.begin(); itr != required_quests.end(); itr++) {
  2890. player->AddQuestRequiredSpawn(this, itr->first);
  2891. vector<int16>* quest_steps = itr->second;
  2892. for (int32 i = 0; i < quest_steps->size(); i++) {
  2893. quest = player->GetQuest(itr->first);
  2894. if (req_quests_continued_access) {
  2895. if (quest) {
  2896. if (quest->GetQuestStepCompleted(quest_steps->at(i))) {
  2897. ret = true;
  2898. break;
  2899. }
  2900. }
  2901. else if (player->GetCompletedQuest(itr->first)) {
  2902. ret = true;
  2903. break;
  2904. }
  2905. }
  2906. if (quest && quest->QuestStepIsActive(quest_steps->at(i))) {
  2907. ret = true;
  2908. break;
  2909. }
  2910. }
  2911. }
  2912. }
  2913. else
  2914. ret = true;
  2915. m_requiredQuests.releasereadlock(__FUNCTION__, __LINE__);
  2916. if (!ret)
  2917. return ret;
  2918. //Now check if the player meets all history requirements
  2919. m_requiredHistory.readlock(__FUNCTION__, __LINE__);
  2920. if (required_history.size() > 0){
  2921. map<int32, LUAHistory*>::iterator itr;
  2922. for (itr = required_history.begin(); itr != required_history.end(); itr++){
  2923. player->AddHistoryRequiredSpawn(this, itr->first);
  2924. LUAHistory* player_history = player->GetLUAHistory(itr->first);
  2925. if (player_history){
  2926. if (player_history->Value != itr->second->Value || player_history->Value2 != itr->second->Value2)
  2927. ret = false;
  2928. }
  2929. else
  2930. ret = false;
  2931. if (!ret)
  2932. break;
  2933. }
  2934. }
  2935. m_requiredHistory.releasereadlock(__FUNCTION__, __LINE__);
  2936. return ret;
  2937. }
  2938. vector<Spawn*>* Spawn::GetSpawnGroup(){
  2939. vector<Spawn*>* ret_list = 0;
  2940. if(spawn_group_list){
  2941. ret_list = new vector<Spawn*>();
  2942. if(MSpawnGroup)
  2943. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  2944. ret_list->insert(ret_list->begin(), spawn_group_list->begin(), spawn_group_list->end());
  2945. if(MSpawnGroup)
  2946. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  2947. }
  2948. return ret_list;
  2949. }
  2950. bool Spawn::HasSpawnGroup() {
  2951. return spawn_group_list && spawn_group_list->size() > 0;
  2952. }
  2953. bool Spawn::IsInSpawnGroup(Spawn* spawn) {
  2954. bool ret = false;
  2955. if (HasSpawnGroup() && spawn) {
  2956. vector<Spawn*>::iterator itr;
  2957. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  2958. if ((*itr) == spawn) {
  2959. ret = true;
  2960. break;
  2961. }
  2962. }
  2963. }
  2964. return ret;
  2965. }
  2966. void Spawn::AddSpawnToGroup(Spawn* spawn){
  2967. if(!spawn)
  2968. return;
  2969. if(!spawn_group_list){
  2970. spawn_group_list = new vector<Spawn*>();
  2971. spawn_group_list->push_back(this);
  2972. safe_delete(MSpawnGroup);
  2973. MSpawnGroup = new Mutex();
  2974. MSpawnGroup->SetName("Spawn::MSpawnGroup");
  2975. }
  2976. vector<Spawn*>::iterator itr;
  2977. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2978. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2979. if((*itr) == spawn){
  2980. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2981. return;
  2982. }
  2983. }
  2984. spawn_group_list->push_back(spawn);
  2985. spawn->SetSpawnGroupList(spawn_group_list, MSpawnGroup);
  2986. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2987. }
  2988. void Spawn::SetSpawnGroupList(vector<Spawn*>* list, Mutex* mutex){
  2989. spawn_group_list = list;
  2990. MSpawnGroup = mutex;
  2991. }
  2992. void Spawn::RemoveSpawnFromGroup(bool erase_all){
  2993. SetSpawnGroupID(0);
  2994. bool del = false;
  2995. if(MSpawnGroup){
  2996. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2997. if(spawn_group_list){
  2998. vector<Spawn*>::iterator itr;
  2999. Spawn* spawn = 0;
  3000. if(spawn_group_list->size() == 1)
  3001. erase_all = true;
  3002. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  3003. spawn = *itr;
  3004. if (spawn) {
  3005. if(!erase_all){
  3006. if(spawn == this){
  3007. spawn_group_list->erase(itr);
  3008. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  3009. spawn_group_list = 0;
  3010. MSpawnGroup = 0;
  3011. return;
  3012. }
  3013. }
  3014. else{
  3015. if (spawn != this)
  3016. spawn->SetSpawnGroupList(0, 0);
  3017. }
  3018. }
  3019. }
  3020. if (erase_all)
  3021. spawn_group_list->clear();
  3022. del = (spawn_group_list->size() == 0);
  3023. }
  3024. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  3025. if (del){
  3026. safe_delete(MSpawnGroup);
  3027. safe_delete(spawn_group_list);
  3028. }
  3029. }
  3030. }
  3031. void Spawn::SetSpawnGroupID(int32 id){
  3032. m_SpawnMutex.writelock();
  3033. group_id = id;
  3034. m_SpawnMutex.releasewritelock();
  3035. }
  3036. int32 Spawn::GetSpawnGroupID(){
  3037. int32 groupid = 0;
  3038. m_SpawnMutex.readlock();
  3039. groupid = group_id;
  3040. m_SpawnMutex.releasereadlock();
  3041. return groupid;
  3042. }
  3043. void Spawn::AddChangedZoneSpawn(){
  3044. if(send_spawn_changes && GetZone())
  3045. GetZone()->AddChangedSpawn(this);
  3046. }
  3047. void Spawn::RemoveSpawnAccess(Spawn* spawn) {
  3048. if (allowed_access.count(spawn->GetID()) > 0) {
  3049. allowed_access.erase(spawn->GetID());
  3050. GetZone()->HidePrivateSpawn(this);
  3051. }
  3052. }
  3053. void Spawn::SetFollowTarget(Spawn* spawn, int32 follow_distance) {
  3054. if (spawn && spawn != this) {
  3055. m_followTarget = spawn->GetID();
  3056. m_followDistance = follow_distance;
  3057. }
  3058. else {
  3059. m_followTarget = 0;
  3060. if (following)
  3061. following = false;
  3062. m_followDistance = 0;
  3063. }
  3064. }
  3065. void Spawn::AddTempVariable(string var, string val) {
  3066. m_tempVariableTypes[var] = 5;
  3067. m_tempVariables[var] = val;
  3068. }
  3069. void Spawn::AddTempVariable(string var, Spawn* val) {
  3070. m_tempVariableTypes[var] = 1;
  3071. m_tempVariableSpawn[var] = val->GetID();
  3072. }
  3073. void Spawn::AddTempVariable(string var, ZoneServer* val) {
  3074. m_tempVariableTypes[var] = 2;
  3075. m_tempVariableZone[var] = val;
  3076. }
  3077. void Spawn::AddTempVariable(string var, Item* val) {
  3078. m_tempVariableTypes[var] = 3;
  3079. m_tempVariableItem[var] = val;
  3080. }
  3081. void Spawn::AddTempVariable(string var, Quest* val) {
  3082. m_tempVariableTypes[var] = 4;
  3083. m_tempVariableQuest[var] = val;
  3084. }
  3085. string Spawn::GetTempVariable(string var) {
  3086. string ret = "";
  3087. if (m_tempVariables.count(var) > 0)
  3088. ret = m_tempVariables[var];
  3089. return ret;
  3090. }
  3091. Spawn* Spawn::GetTempVariableSpawn(string var) {
  3092. Spawn* ret = 0;
  3093. if (m_tempVariableSpawn.count(var) > 0)
  3094. ret = GetZone()->GetSpawnByID(m_tempVariableSpawn[var]);
  3095. return ret;
  3096. }
  3097. ZoneServer* Spawn::GetTempVariableZone(string var) {
  3098. ZoneServer* ret = 0;
  3099. if (m_tempVariableZone.count(var) > 0)
  3100. ret = m_tempVariableZone[var];
  3101. return ret;
  3102. }
  3103. Item* Spawn::GetTempVariableItem(string var) {
  3104. Item* ret = 0;
  3105. if (m_tempVariableItem.count(var) > 0)
  3106. ret = m_tempVariableItem[var];
  3107. return ret;
  3108. }
  3109. Quest* Spawn::GetTempVariableQuest(string var) {
  3110. Quest* ret = 0;
  3111. if (m_tempVariableQuest.count(var) > 0)
  3112. ret = m_tempVariableQuest[var];
  3113. return ret;
  3114. }
  3115. int8 Spawn::GetTempVariableType(string var) {
  3116. int8 ret = 0;
  3117. if (m_tempVariableTypes.count(var) > 0)
  3118. ret = m_tempVariableTypes[var];
  3119. return ret;
  3120. }
  3121. void Spawn::DeleteTempVariable(string var) {
  3122. int8 type = GetTempVariableType(var);
  3123. switch (type) {
  3124. case 1:
  3125. m_tempVariableSpawn.erase(var);
  3126. break;
  3127. case 2:
  3128. m_tempVariableZone.erase(var);
  3129. break;
  3130. case 3:
  3131. m_tempVariableItem.erase(var);
  3132. break;
  3133. case 4:
  3134. m_tempVariableQuest.erase(var);
  3135. break;
  3136. case 5:
  3137. m_tempVariables.erase(var);
  3138. break;
  3139. }
  3140. m_tempVariableTypes.erase(var);
  3141. }
  3142. Spawn* Spawn::GetRunningTo() {
  3143. return GetZone()->GetSpawnByID(running_to);
  3144. }
  3145. Spawn* Spawn::GetFollowTarget() {
  3146. return GetZone()->GetSpawnByID(m_followTarget);
  3147. }
  3148. void Spawn::CopySpawnAppearance(Spawn* spawn){
  3149. if (!spawn)
  3150. return;
  3151. //This function copies the appearace of the provided spawn to this one
  3152. if (spawn->IsEntity() && IsEntity()){
  3153. memcpy(&((Entity*)this)->features, &((Entity*)spawn)->features, sizeof(CharFeatures));
  3154. memcpy(&((Entity*)this)->equipment, &((Entity*)spawn)->equipment, sizeof(EQ2_Equipment));
  3155. }
  3156. SetSize(spawn->GetSize());
  3157. SetModelType(spawn->GetModelType());
  3158. }
  3159. void Spawn::SetY(float y, bool updateFlags, bool disableYMapCheck)
  3160. {
  3161. SetPos(&appearance.pos.Y, y, updateFlags);
  3162. if (!disableYMapCheck)
  3163. FixZ();
  3164. }
  3165. float Spawn::FindDestGroundZ(glm::vec3 dest, float z_offset)
  3166. {
  3167. float best_z = BEST_Z_INVALID;
  3168. if (GetZone() != nullptr && GetMap() != nullptr)
  3169. {
  3170. dest.z += z_offset;
  3171. best_z = GetMap()->FindBestZ(dest, nullptr);
  3172. }
  3173. return best_z;
  3174. }
  3175. float Spawn::GetFixedZ(const glm::vec3& destination, int32 z_find_offset) {
  3176. BenchTimer timer;
  3177. timer.reset();
  3178. float new_z = destination.z;
  3179. if (GetZone() != nullptr && GetMap() != nullptr) {
  3180. /* if (flymode == GravityBehavior::Flying)
  3181. return new_z;
  3182. */
  3183. /* if (zone->HasWaterMap() && zone->watermap->InLiquid(glm::vec3(m_Position)))
  3184. return new_z;
  3185. */
  3186. /*
  3187. * Any more than 5 in the offset makes NPC's hop/snap to ceiling in small corridors
  3188. */
  3189. new_z = this->FindDestGroundZ(destination, z_find_offset);
  3190. if (new_z != BEST_Z_INVALID) {
  3191. if (new_z < -2000) {
  3192. new_z = GetY();
  3193. }
  3194. }
  3195. auto duration = timer.elapsed();
  3196. LogWrite(MAP__DEBUG, 0, "Map", "Mob::GetFixedZ() ([{%s}]) returned [{%f}] at [{%f}], [{%f}], [{%f}] - Took [{%f}]",
  3197. this->GetName(),
  3198. new_z,
  3199. destination.x,
  3200. destination.y,
  3201. destination.z,
  3202. duration);
  3203. }
  3204. return new_z;
  3205. }
  3206. void Spawn::FixZ(bool forceUpdate) {
  3207. if (IsPlayer() || IsFlyingCreature() || !GetZone() || (IsObject() && GetZone()->GetInstanceType() == Instance_Type::PERSONAL_HOUSE_INSTANCE)) {
  3208. return;
  3209. }
  3210. /*
  3211. if (flymode == GravityBehavior::Flying) {
  3212. return;
  3213. }*/
  3214. /*
  3215. if (zone->watermap && zone->watermap->InLiquid(m_Position)) {
  3216. return;
  3217. }*/
  3218. // we do the inwater check here manually to avoid double calling for a Z coordinate
  3219. glm::vec3 current_loc(GetX(), GetZ(), GetY());
  3220. float new_z = GetFixedZ(current_loc, 1);
  3221. if ( region_map != nullptr )
  3222. {
  3223. glm::vec3 targPos(GetX(), GetY(), GetZ());
  3224. if(region_map->InWater(targPos, appearance.pos.grid_id))
  3225. return;
  3226. }
  3227. if (new_z == GetY())
  3228. return;
  3229. if ((new_z > -2000) && new_z != BEST_Z_INVALID) {
  3230. SetY(new_z, forceUpdate, true);
  3231. }
  3232. else {
  3233. LogWrite(MAP__DEBUG, 0, "Map", "[{%s}] is failing to find Z [{%f}]", this->GetName(), std::abs(GetY() - new_z));
  3234. }
  3235. }
  3236. bool Spawn::CheckLoS(Spawn* target)
  3237. {
  3238. float radiusSrc = 2.0f;
  3239. float radiusTarg = 2.0f;
  3240. glm::vec3 targpos(target->GetX(), target->GetZ(), target->GetY()+radiusTarg);
  3241. glm::vec3 pos(GetX(), GetZ(), GetY()+radiusSrc);
  3242. return CheckLoS(pos, targpos);
  3243. }
  3244. bool Spawn::CheckLoS(glm::vec3 myloc, glm::vec3 oloc)
  3245. {
  3246. ZoneServer* zone = GetZone();
  3247. if (zone == NULL || GetMap() == NULL || !GetMap()->IsMapLoaded())
  3248. return true;
  3249. else
  3250. return GetMap()->CheckLoS(myloc, oloc);
  3251. return false;
  3252. }
  3253. void Spawn::CalculateNewFearpoint()
  3254. {
  3255. if (GetZone() && GetZone()->pathing) {
  3256. auto Node = zone->pathing->GetRandomLocation(glm::vec3(GetX(), GetZ(), GetY()));
  3257. if (Node.x != 0.0f || Node.y != 0.0f || Node.z != 0.0f) {
  3258. AddRunningLocation(Node.x, Node.y, Node.z, GetSpeed(), 0, true, true, "", true);
  3259. }
  3260. }
  3261. }
  3262. Item* Spawn::LootItem(int32 id) {
  3263. Item* ret = 0;
  3264. vector<Item*>::iterator itr;
  3265. MLootItems.lock();
  3266. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3267. if ((*itr)->details.item_id == id) {
  3268. ret = *itr;
  3269. loot_items.erase(itr);
  3270. break;
  3271. }
  3272. }
  3273. MLootItems.unlock();
  3274. return ret;
  3275. }
  3276. int32 Spawn::GetLootItemID() {
  3277. int32 ret = 0;
  3278. vector<Item*>::iterator itr;
  3279. MLootItems.lock();
  3280. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3281. ret = (*itr)->details.item_id;
  3282. break;
  3283. }
  3284. MLootItems.unlock();
  3285. return ret;
  3286. }
  3287. bool Spawn::HasLootItemID(int32 id) {
  3288. bool ret = false;
  3289. vector<Item*>::iterator itr;
  3290. MLootItems.readlock(__FUNCTION__, __LINE__);
  3291. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3292. if ((*itr)->details.item_id == id) {
  3293. ret = true;
  3294. break;
  3295. }
  3296. }
  3297. MLootItems.releasereadlock(__FUNCTION__, __LINE__);
  3298. return ret;
  3299. }
  3300. void Spawn::CheckProximities()
  3301. {
  3302. if (!has_spawn_proximities)
  3303. return;
  3304. if (spawn_proximities.size() > 0)
  3305. {
  3306. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3307. while (itr.Next()) {
  3308. SpawnProximity* prox = itr.value;
  3309. map<int32, bool>::iterator spawnsItr;
  3310. for (spawnsItr = prox->spawns_in_proximity.begin(); spawnsItr != prox->spawns_in_proximity.end(); spawnsItr++) {
  3311. Spawn* tmpSpawn = 0;
  3312. if (spawnsItr->first &&
  3313. ((prox->spawn_type == SPAWNPROXIMITY_DATABASE_ID && (tmpSpawn = GetZone()->GetSpawnByDatabaseID(spawnsItr->first)) != 0) ||
  3314. (prox->spawn_type == SPAWNPROXIMITY_LOCATION_ID && (tmpSpawn = GetZone()->GetSpawnByLocationID(spawnsItr->first)) != 0)))
  3315. {
  3316. if (!spawnsItr->second && tmpSpawn->GetDistance(this) <= prox->distance)
  3317. {
  3318. if (prox->in_range_lua_function.size() > 0)
  3319. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->in_range_lua_function.c_str());
  3320. spawnsItr->second = true;
  3321. }
  3322. else if (spawnsItr->second && tmpSpawn->GetDistance(this) > prox->distance)
  3323. {
  3324. if (prox->leaving_range_lua_function.size() > 0)
  3325. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->leaving_range_lua_function.c_str());
  3326. spawnsItr->second = false;
  3327. }
  3328. }
  3329. }
  3330. }
  3331. }
  3332. }
  3333. void Spawn::AddSpawnToProximity(int32 spawnValue, SpawnProximityType type)
  3334. {
  3335. if (!has_spawn_proximities)
  3336. return;
  3337. if (spawn_proximities.size() > 0)
  3338. {
  3339. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3340. while (itr.Next()) {
  3341. SpawnProximity* prox = itr->value;
  3342. if (prox->spawn_value == spawnValue && prox->spawn_type == type)
  3343. prox->spawns_in_proximity.insert(make_pair(spawnValue, false));
  3344. }
  3345. }
  3346. }
  3347. void Spawn::RemoveSpawnFromProximity(int32 spawnValue, SpawnProximityType type)
  3348. {
  3349. if (!has_spawn_proximities)
  3350. return;
  3351. if (spawn_proximities.size() > 0)
  3352. {
  3353. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3354. while (itr.Next()) {
  3355. SpawnProximity* prox = itr->value;
  3356. if (prox->spawn_value == spawnValue && prox->spawn_type == type &&
  3357. prox->spawns_in_proximity.count(spawnValue) > 0)
  3358. prox->spawns_in_proximity.erase(spawnValue);
  3359. }
  3360. }
  3361. }
  3362. 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) {
  3363. EntityCommand* cmd = FindEntityCommand(string(command), true);
  3364. bool newCommand = false;
  3365. if (!cmd)
  3366. {
  3367. newCommand = true;
  3368. cmd = CreateEntityCommand(name, distance, command, error_text, cast_time, spell_visual, !defaultDenyList);
  3369. }
  3370. if (defaultDenyList)
  3371. SetPermissionToEntityCommand(cmd, player, true);
  3372. if (newCommand)
  3373. primary_command_list.push_back(cmd);
  3374. }
  3375. void Spawn::RemovePrimaryEntityCommand(const char* command) {
  3376. vector<EntityCommand*>::iterator itr;
  3377. string tmpStr(command);
  3378. for (itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++) {
  3379. EntityCommand* cmd = *itr;
  3380. if (cmd->command.compare(tmpStr) == 0)
  3381. {
  3382. primary_command_list.erase(itr);
  3383. delete cmd;
  3384. break;
  3385. }
  3386. }
  3387. }
  3388. bool Spawn::SetPermissionToEntityCommand(EntityCommand* command, Player* player, bool permissionValue)
  3389. {
  3390. if(!player)
  3391. return false;
  3392. return SetPermissionToEntityCommandByCharID(command, player->GetCharacterID(), permissionValue);
  3393. }
  3394. bool Spawn::SetPermissionToEntityCommandByCharID(EntityCommand* command, int32 charID, bool permissionValue)
  3395. {
  3396. map<int32, bool>::iterator itr = command->allow_or_deny.find(charID);
  3397. if (itr == command->allow_or_deny.end())
  3398. command->allow_or_deny.insert(make_pair(charID, permissionValue));
  3399. else if (itr->second != permissionValue)
  3400. itr->second = permissionValue;
  3401. return true;
  3402. }
  3403. void Spawn::RemoveSpawnFromPlayer(Player* player)
  3404. {
  3405. m_Update.writelock(__FUNCTION__, __LINE__);
  3406. player->RemoveSpawn(this); // sets it as removed
  3407. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  3408. }
  3409. bool Spawn::InWater()
  3410. {
  3411. bool inWater = false;
  3412. if ( region_map != nullptr )
  3413. {
  3414. glm::vec3 targPos(GetX(), GetY(), GetZ());
  3415. if ( IsGroundSpawn() )
  3416. targPos.y -= .5f;
  3417. if(region_map->InWater(targPos, appearance.pos.grid_id))
  3418. inWater = true;
  3419. }
  3420. return inWater;
  3421. }
  3422. bool Spawn::InLava()
  3423. {
  3424. bool inLava = false;
  3425. if ( region_map != nullptr )
  3426. {
  3427. glm::vec3 targPos(GetX(), GetY(), GetZ());
  3428. if ( IsGroundSpawn() )
  3429. targPos.y -= .5f;
  3430. if(region_map->InLava(targPos, appearance.pos.grid_id))
  3431. inLava = true;
  3432. }
  3433. return inLava;
  3434. }
  3435. void Spawn::DeleteRegion(Region_Node* inNode, ZBSP_Node* rootNode)
  3436. {
  3437. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  3438. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  3439. {
  3440. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  3441. Region_Node* node = actualItr->first;
  3442. ZBSP_Node* BSP_Root = actualItr->second;
  3443. if(inNode == node && rootNode == BSP_Root )
  3444. {
  3445. testitr = Regions.erase(testitr);
  3446. break;
  3447. }
  3448. }
  3449. }
  3450. bool Spawn::InRegion(Region_Node* inNode, ZBSP_Node* rootNode)
  3451. {
  3452. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  3453. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  3454. {
  3455. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  3456. Region_Node* node = actualItr->first;
  3457. ZBSP_Node* BSP_Root = actualItr->second;
  3458. if(inNode == node && rootNode == BSP_Root )
  3459. {
  3460. return testitr->second.inRegion;
  3461. }
  3462. }
  3463. return false;
  3464. }
  3465. int32 Spawn::GetRegionType(Region_Node* inNode, ZBSP_Node* rootNode)
  3466. {
  3467. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  3468. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  3469. {
  3470. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  3471. Region_Node* node = actualItr->first;
  3472. ZBSP_Node* BSP_Root = actualItr->second;
  3473. if(inNode == node && rootNode == BSP_Root )
  3474. {
  3475. return testitr->second.regionType;
  3476. }
  3477. }
  3478. return false;
  3479. }
  3480. float Spawn::SpawnAngle(Spawn* target, float selfx, float selfz)
  3481. {
  3482. if (!target || target == this)
  3483. return 0.0f;
  3484. float angle, lengthb, vectorx, vectorz, dotp;
  3485. float spx = (target->GetX()); // mob xloc (inverse because eq)
  3486. float spz = -(target->GetZ()); // mob yloc
  3487. float heading = target->GetHeading(); // mob heading
  3488. if (heading < 270)
  3489. heading += 90;
  3490. else
  3491. heading -= 270;
  3492. heading = heading * 3.1415f / 180.0f; // convert to radians
  3493. vectorx = spx + (10.0f * std::cos(heading)); // create a vector based on heading
  3494. vectorz = spz + (10.0f * std::sin(heading)); // of spawn length 10
  3495. // length of spawn to player vector
  3496. lengthb = (float) std::sqrt(((selfx - spx) * (selfx - spx)) + ((-selfz - spz) * (-selfz - spz)));
  3497. // calculate dot product to get angle
  3498. // Handle acos domain errors due to floating point rounding errors
  3499. dotp = ((vectorx - spx) * (selfx - spx) +
  3500. (vectorz - spz) * (-selfz - spz)) / (10.0f * lengthb);
  3501. if (dotp > 1)
  3502. return 0.0f;
  3503. else if (dotp < -1)
  3504. return 180.0f;
  3505. angle = std::acos(dotp);
  3506. angle = angle * 180.0f / 3.1415f;
  3507. return angle;
  3508. }
  3509. void Spawn::StopMovement()
  3510. {
  3511. ResetMovement(true);
  3512. }
  3513. bool Spawn::PauseMovement(int32 period_of_time_ms)
  3514. {
  3515. if(period_of_time_ms < 1)
  3516. period_of_time_ms = 1;
  3517. RunToLocation(GetX(),GetY(),GetZ());
  3518. pause_timer.Start(period_of_time_ms, true);
  3519. return true;
  3520. }
  3521. bool Spawn::IsPauseMovementTimerActive()
  3522. {
  3523. if(pause_timer.Check())
  3524. pause_timer.Disable();
  3525. return pause_timer.Enabled();
  3526. }
  3527. bool Spawn::IsFlyingCreature()
  3528. {
  3529. if(!IsEntity())
  3530. return false;
  3531. return ((Entity*)this)->GetInfoStruct()->get_flying_type();
  3532. }
  3533. bool Spawn::IsWaterCreature()
  3534. {
  3535. if(!IsEntity())
  3536. return false;
  3537. return ((Entity*)this)->GetInfoStruct()->get_water_type();
  3538. }
  3539. void Spawn::SetFlyingCreature() {
  3540. if(!IsEntity() || !rule_manager.GetGlobalRule(R_Spawn, UseHardCodeFlyingModelType)->GetInt8())
  3541. return;
  3542. if(((Entity*)this)->GetInfoStruct()->get_flying_type() > 0) // DB spawn npc flag already set
  3543. return;
  3544. switch (GetModelType())
  3545. {
  3546. case 260:
  3547. case 295:
  3548. ((Entity*)this)->GetInfoStruct()->set_flying_type(1);
  3549. is_flying_creature = true;
  3550. break;
  3551. default:
  3552. ((Entity*)this)->GetInfoStruct()->set_flying_type(0);
  3553. break;
  3554. }
  3555. }
  3556. void Spawn::SetWaterCreature() {
  3557. if(!IsEntity() || !rule_manager.GetGlobalRule(R_Spawn, UseHardCodeWaterModelType)->GetInt8())
  3558. return;
  3559. if(((Entity*)this)->GetInfoStruct()->get_water_type() > 0) // DB spawn npc flag already set
  3560. return;
  3561. switch (GetModelType())
  3562. {
  3563. case 194:
  3564. case 204:
  3565. case 210:
  3566. case 241:
  3567. case 242:
  3568. case 254:
  3569. case 10668:
  3570. case 20828:
  3571. ((Entity*)this)->GetInfoStruct()->set_water_type(1);
  3572. break;
  3573. default:
  3574. ((Entity*)this)->GetInfoStruct()->set_water_type(0);
  3575. break;
  3576. }
  3577. }