Spawn.cpp 88 KB

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