Spawn.cpp 78 KB

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