Spawn.cpp 98 KB

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