Spawn.cpp 90 KB

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