Spawn.cpp 98 KB

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