Spawn.cpp 113 KB

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