Spawn.cpp 113 KB

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