Spawn.cpp 121 KB

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