Spawn.cpp 91 KB

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