Spawn.cpp 93 KB

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