Spawn.cpp 96 KB

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