Spawn.cpp 112 KB

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