Spawn.cpp 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  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, true);
  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, bool bSpawnUpdate) {
  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. bool bSendSpeed = true;
  1179. if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
  1180. Widget* widget = (Widget*)this;
  1181. float x;
  1182. float y;
  1183. float z;
  1184. if (IsRunning()){
  1185. x = appearance.pos.X2 - widget->GetWidgetX();
  1186. y = appearance.pos.Y2 - widget->GetWidgetY();
  1187. z = appearance.pos.Z2- widget->GetWidgetZ();
  1188. }
  1189. else {
  1190. x = appearance.pos.X - widget->GetWidgetX();
  1191. y = appearance.pos.Y - widget->GetWidgetY();
  1192. z = appearance.pos.Z - widget->GetWidgetZ();
  1193. }
  1194. packet->setDataByName("pos_next_x", x);
  1195. packet->setDataByName("pos_next_y", y);
  1196. packet->setDataByName("pos_next_z", z);
  1197. packet->setDataByName("pos_x3", x);
  1198. packet->setDataByName("pos_y3", y);
  1199. packet->setDataByName("pos_z3", z);
  1200. }
  1201. //If this is a spawn update or this spawn is currently moving we can send these values, otherwise set speed and next_xyz to 0
  1202. //This fixes the bug where spawns with movement scripts face south when initially spawning if they are at their target location.
  1203. else if (bSpawnUpdate || memcmp(&appearance.pos.X, &appearance.pos.X2, sizeof(float) * 3) != 0) {
  1204. packet->setDataByName("pos_next_x", appearance.pos.X2);
  1205. packet->setDataByName("pos_next_y", appearance.pos.Y2);
  1206. packet->setDataByName("pos_next_z", appearance.pos.Z2);
  1207. packet->setDataByName("pos_x3", appearance.pos.X3);
  1208. packet->setDataByName("pos_y3", appearance.pos.Y3);
  1209. packet->setDataByName("pos_z3", appearance.pos.Z3);
  1210. }
  1211. else
  1212. {
  1213. bSendSpeed = false;
  1214. }
  1215. //packet->setDataByName("pos_unknown2", 4, 2);
  1216. int16 speed_multiplier = rule_manager.GetGlobalRule(R_Spawn, SpeedMultiplier)->GetInt16(); // was 1280, 600 and now 300... investigating why
  1217. //float speed_ratio = rule_manager.GetGlobalRule(R_Spawn, SpeedRatio)->GetFloat(); // was 7.5 for 1280 and 600, then 0 for 300... investigating why
  1218. if (IsPlayer()) {
  1219. Player* player = static_cast<Player*>(this);
  1220. packet->setDataByName("pos_speed", player->GetPosPacketSpeed() * speed_multiplier);
  1221. packet->setDataByName("pos_side_speed", player->GetSideSpeed() * speed_multiplier);
  1222. }
  1223. else if (bSendSpeed) {
  1224. packet->setDataByName("pos_speed", GetSpeed() * speed_multiplier);
  1225. }
  1226. if (IsNPC() || IsPlayer()) {
  1227. packet->setDataByName("pos_move_type", 25);
  1228. }
  1229. else if (IsWidget() || IsSign()) {
  1230. packet->setDataByName("pos_move_type", 11);
  1231. }
  1232. else if(IsGroundSpawn()) {
  1233. packet->setDataByName("pos_move_type", 16);
  1234. }
  1235. if (!IsPlayer())
  1236. packet->setDataByName("pos_movement_mode", 2);
  1237. if(version <= 910)
  1238. packet->setDataByName("pos_unknown10", 0xFFFF, 1);
  1239. else
  1240. packet->setDataByName("pos_unknown10", 0xFFFF);
  1241. if(version <= 910)
  1242. packet->setDataByName("pos_unknown10", 0xFFFF, 2);
  1243. else
  1244. packet->setDataByName("pos_unknown10", 0XFFFF, 1);
  1245. packet->setDataByName("pos_pitch1", appearance.pos.Pitch1);
  1246. packet->setDataByName("pos_pitch2", appearance.pos.Pitch2);
  1247. packet->setDataByName("pos_roll", appearance.pos.Roll);
  1248. }
  1249. void Spawn::InitializeInfoPacketData(Player* spawn, PacketStruct* packet){
  1250. int16 version = packet->GetVersion();
  1251. if(appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1){
  1252. appearance.locked_no_loot = 1; //for now
  1253. if(!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign()){
  1254. int8 percent = 0;
  1255. if(GetHP() > 0)
  1256. percent = (int8)(((float)GetHP()/GetTotalHP()) * 100);
  1257. if(percent < 100){
  1258. packet->setDataByName("hp_remaining", 100 ^ percent);
  1259. }
  1260. else
  1261. packet->setDataByName("hp_remaining", 0);
  1262. if(GetTotalPower() > 0){
  1263. percent = (int8)(((float)GetPower()/GetTotalPower()) * 100);
  1264. if(percent > 0)
  1265. packet->setDataByName("power_percent", percent);
  1266. else
  1267. packet->setDataByName("power_percent", 0);
  1268. }
  1269. }
  1270. }
  1271. packet->setDataByName("level", (int8)GetLevel());
  1272. packet->setDataByName("unknown4", (int8)GetLevel());
  1273. packet->setDataByName("difficulty", appearance.encounter_level); //6);
  1274. packet->setDataByName("heroic_flag", appearance.heroic_flag);
  1275. if(!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign())
  1276. packet->setDataByName("interaction_flag", 12); //this makes NPCs head turn to look at you
  1277. if (version >= 1188 && (IsPlayer() || IsBot()))
  1278. packet->setDataByName("spawn_type", 0);
  1279. else
  1280. packet->setDataByName("spawn_type", spawn_type);
  1281. packet->setDataByName("class", appearance.adventure_class);
  1282. int16 model_type = appearance.model_type;
  1283. if (GetIllusionModel() != 0) {
  1284. if (IsPlayer()) {
  1285. if (((Player*)this)->get_character_flag(CF_SHOW_ILLUSION)) {
  1286. model_type = GetIllusionModel();
  1287. }
  1288. }
  1289. else
  1290. model_type = GetIllusionModel();
  1291. }
  1292. packet->setDataByName("model_type", model_type);
  1293. if(appearance.soga_model_type == 0)
  1294. packet->setDataByName("soga_model_type", model_type);
  1295. else
  1296. packet->setDataByName("soga_model_type", appearance.soga_model_type);
  1297. if(GetTempActionState() >= 0)
  1298. packet->setDataByName("action_state", GetTempActionState());
  1299. else
  1300. packet->setDataByName("action_state", appearance.action_state);
  1301. if(GetTempVisualState() >= 0)
  1302. packet->setDataByName("visual_state", GetTempVisualState());
  1303. else
  1304. packet->setDataByName("visual_state", appearance.visual_state);
  1305. packet->setDataByName("emote_state", appearance.emote_state);
  1306. packet->setDataByName("mood_state", appearance.mood_state);
  1307. packet->setDataByName("gender", appearance.gender);
  1308. packet->setDataByName("race", appearance.race);
  1309. packet->setDataByName("gender", appearance.gender);
  1310. if(IsEntity()){
  1311. Entity* entity = ((Entity*)this);
  1312. packet->setDataByName("combat_voice", entity->GetCombatVoice());
  1313. packet->setDataByName("emote_voice", entity->GetEmoteVoice());
  1314. for(int i=0;i<25;i++){
  1315. if(i == 2){ //don't send helm if hidden flag
  1316. if(IsPlayer()){
  1317. if(((Player*)this)->get_character_flag(CF_HIDE_HELM)){
  1318. packet->setDataByName("equipment_types", 0, i);
  1319. packet->setColorByName("equipment_colors", 0, i);
  1320. packet->setColorByName("equipment_highlights", 0, i);
  1321. continue;
  1322. }
  1323. }
  1324. if (IsBot()) {
  1325. if (!((Bot*)this)->ShowHelm) {
  1326. packet->setDataByName("equipment_types", 0, i);
  1327. packet->setColorByName("equipment_colors", 0, i);
  1328. packet->setColorByName("equipment_highlights", 0, i);
  1329. continue;
  1330. }
  1331. }
  1332. }
  1333. else if(i == 19){ //don't send cloak if hidden
  1334. if(IsPlayer()){
  1335. if(!((Player*)this)->get_character_flag(CF_SHOW_CLOAK)){
  1336. packet->setDataByName("equipment_types", 0, i);
  1337. packet->setColorByName("equipment_colors", 0, i);
  1338. packet->setColorByName("equipment_highlights", 0, i);
  1339. continue;
  1340. }
  1341. }
  1342. if (IsBot()) {
  1343. if (!((Bot*)this)->ShowCloak) {
  1344. packet->setDataByName("equipment_types", 0, i);
  1345. packet->setColorByName("equipment_colors", 0, i);
  1346. packet->setColorByName("equipment_highlights", 0, i);
  1347. continue;
  1348. }
  1349. }
  1350. }
  1351. packet->setDataByName("equipment_types", entity->equipment.equip_id[i], i);
  1352. packet->setColorByName("equipment_colors", entity->equipment.color[i], i);
  1353. packet->setColorByName("equipment_highlights", entity->equipment.highlight[i], i);
  1354. }
  1355. packet->setDataByName("mount_type", entity->GetMount());
  1356. // find the visual flags
  1357. int8 vis_flag = 0;
  1358. //Invis + crouch flag check
  1359. if (entity->IsStealthed())
  1360. vis_flag += (INFO_VIS_FLAG_INVIS + INFO_VIS_FLAG_CROUCH);
  1361. //Invis flag check
  1362. else if (entity->IsInvis())
  1363. vis_flag += INFO_VIS_FLAG_INVIS;
  1364. //Mount flag check
  1365. if (entity->GetMount() > 0)
  1366. vis_flag += INFO_VIS_FLAG_MOUNTED;
  1367. //Hide hood check
  1368. if ((IsPlayer() && ((Player*)this)->get_character_flag(CF_HIDE_HOOD)) || appearance.hide_hood)
  1369. vis_flag += INFO_VIS_FLAG_HIDE_HOOD;
  1370. packet->setDataByName("visual_flag", vis_flag);
  1371. packet->setColorByName("mount_saddle_color", entity->GetMountSaddleColor());
  1372. packet->setColorByName("mount_color", entity->GetMountColor());
  1373. packet->setDataByName("hair_type_id", entity->features.hair_type);
  1374. packet->setDataByName("chest_type_id", entity->features.chest_type);
  1375. packet->setDataByName("wing_type_id", entity->features.wing_type);
  1376. packet->setDataByName("legs_type_id", entity->features.legs_type);
  1377. packet->setDataByName("soga_hair_type_id", entity->features.soga_hair_type);
  1378. packet->setDataByName("facial_hair_type_id", entity->features.hair_face_type);
  1379. packet->setDataByName("soga_facial_hair_type_id", entity->features.soga_hair_face_type);
  1380. for(int i=0;i<3;i++){
  1381. packet->setDataByName("eye_type", entity->features.eye_type[i], i);
  1382. packet->setDataByName("ear_type", entity->features.ear_type[i], i);
  1383. packet->setDataByName("eye_brow_type", entity->features.eye_brow_type[i], i);
  1384. packet->setDataByName("cheek_type", entity->features.cheek_type[i], i);
  1385. packet->setDataByName("lip_type", entity->features.lip_type[i], i);
  1386. packet->setDataByName("chin_type", entity->features.chin_type[i], i);
  1387. packet->setDataByName("nose_type", entity->features.nose_type[i], i);
  1388. packet->setDataByName("soga_eye_type", entity->features.soga_eye_type[i], i);
  1389. packet->setDataByName("soga_ear_type", entity->features.soga_ear_type[i], i);
  1390. packet->setDataByName("soga_eye_brow_type", entity->features.soga_eye_brow_type[i], i);
  1391. packet->setDataByName("soga_cheek_type", entity->features.soga_cheek_type[i], i);
  1392. packet->setDataByName("soga_lip_type", entity->features.soga_lip_type[i], i);
  1393. packet->setDataByName("soga_chin_type", entity->features.soga_chin_type[i], i);
  1394. packet->setDataByName("soga_nose_type", entity->features.soga_nose_type[i], i);
  1395. }
  1396. packet->setColorByName("skin_color", entity->features.skin_color);
  1397. packet->setColorByName("eye_color", entity->features.eye_color);
  1398. packet->setColorByName("hair_type_color", entity->features.hair_type_color);
  1399. packet->setColorByName("hair_type_highlight_color", entity->features.hair_type_highlight_color);
  1400. packet->setColorByName("hair_face_color", entity->features.hair_face_color);
  1401. packet->setColorByName("hair_face_highlight_color", entity->features.hair_face_highlight_color);
  1402. packet->setColorByName("hair_highlight", entity->features.hair_highlight_color);
  1403. packet->setColorByName("wing_color1", entity->features.wing_color1);
  1404. packet->setColorByName("wing_color2", entity->features.wing_color2);
  1405. packet->setColorByName("hair_color1", entity->features.hair_color1);
  1406. packet->setColorByName("hair_color2", entity->features.hair_color2);
  1407. packet->setColorByName("soga_skin_color", entity->features.soga_skin_color);
  1408. packet->setColorByName("soga_eye_color", entity->features.soga_eye_color);
  1409. packet->setColorByName("soga_hair_color1", entity->features.soga_hair_color1);
  1410. packet->setColorByName("soga_hair_color2", entity->features.soga_hair_color2);
  1411. packet->setColorByName("soga_hair_type_color", entity->features.soga_hair_type_color);
  1412. packet->setColorByName("soga_hair_type_highlight_color", entity->features.soga_hair_type_highlight_color);
  1413. packet->setColorByName("soga_hair_face_color", entity->features.soga_hair_face_color);
  1414. packet->setColorByName("soga_hair_face_highlight_color", entity->features.soga_hair_face_highlight_color);
  1415. packet->setColorByName("soga_hair_highlight", entity->features.soga_hair_highlight_color);
  1416. packet->setDataByName("body_age", entity->features.body_age);
  1417. }
  1418. else{
  1419. EQ2_Color empty;
  1420. empty.red = 255;
  1421. empty.blue = 255;
  1422. empty.green = 255;
  1423. packet->setColorByName("skin_color", empty);
  1424. packet->setColorByName("eye_color", empty);
  1425. packet->setColorByName("soga_skin_color", empty);
  1426. packet->setColorByName("soga_eye_color", empty);
  1427. }
  1428. if(appearance.icon == 0){
  1429. if(appearance.attackable == 1)
  1430. appearance.icon = 0;
  1431. else if(appearance.encounter_level > 0)
  1432. appearance.icon = 4;
  1433. else
  1434. appearance.icon = 6;
  1435. }
  1436. // If Coe+ clients modify the values before we send
  1437. // if not then just send the value we have.
  1438. int8 temp_icon = appearance.icon;
  1439. //Check if we need to add the hand icon..
  1440. if ((temp_icon & 6) != 6 && appearance.display_hand_icon) {
  1441. temp_icon |= 6;
  1442. }
  1443. //Icon value 28 for boats, set this without modifying the value
  1444. if (version >= 1188 && temp_icon != 28) {
  1445. if ((temp_icon & 64) > 0) {
  1446. temp_icon -= 64; // remove the DoV value;
  1447. temp_icon += 128; // add the CoE value
  1448. }
  1449. if ((temp_icon & 32) > 0) {
  1450. temp_icon -= 32; // remove the DoV value;
  1451. temp_icon += 64; // add the CoE value
  1452. }
  1453. if ((temp_icon & 4) > 0) {
  1454. temp_icon -= 4; // remove DoV value
  1455. temp_icon += 8; // add the CoE icon
  1456. }
  1457. if ((temp_icon & 6) > 0) {
  1458. temp_icon -= 10; // remove DoV value
  1459. temp_icon += 12; // add the CoE icon
  1460. }
  1461. }
  1462. packet->setDataByName("icon", temp_icon);//appearance.icon);
  1463. int16 temp_activity_status = 0;
  1464. if (version >= 1188) {
  1465. if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
  1466. temp_activity_status += ACTIVITY_STATUS_ROLEPLAYING_1188;
  1467. if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
  1468. temp_activity_status += ACTIVITY_STATUS_ANONYMOUS_1188;
  1469. if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
  1470. temp_activity_status += ACTIVITY_STATUS_LINKDEAD_1188;
  1471. if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
  1472. temp_activity_status += ACTIVITY_STATUS_CAMPING_1188;
  1473. if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
  1474. temp_activity_status += ACTIVITY_STATUS_LFG_1188;
  1475. if ((appearance.activity_status & ACTIVITY_STATUS_LFW) > 0)
  1476. temp_activity_status += ACTIVITY_STATUS_LFW_1188;
  1477. if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
  1478. temp_activity_status += ACTIVITY_STATUS_SOLID_1188;
  1479. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_GAINED) > 0)
  1480. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_GAINED_1188;
  1481. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_REMAINING) > 0)
  1482. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_REMAINING_1188;
  1483. }
  1484. else
  1485. temp_activity_status = appearance.activity_status;
  1486. packet->setDataByName("activity_status", temp_activity_status); //appearance.activity_status);
  1487. // If player and player has a follow target
  1488. if (IsPlayer()) {
  1489. if (((Player*)this)->GetFollowTarget())
  1490. packet->setDataByName("follow_target", ((((Player*)this)->GetIDWithPlayerSpawn(((Player*)this)->GetFollowTarget()) * -1) - 1));
  1491. else
  1492. packet->setDataByName("follow_target", 0);
  1493. }
  1494. if (GetTarget() && GetTarget()->GetTargetable())
  1495. packet->setDataByName("target_id", ((spawn->GetIDWithPlayerSpawn(GetTarget()) * -1) - 1));
  1496. else
  1497. packet->setDataByName("target_id", 0);
  1498. //Send spell effects for target window
  1499. if(IsEntity()){
  1500. InfoStruct* info = ((Entity*)this)->GetInfoStruct();
  1501. int8 i = 0;
  1502. int16 backdrop = 0;
  1503. int16 spell_icon = 0;
  1504. int32 spell_id = 0;
  1505. LuaSpell* spell = 0;
  1506. ((Entity*)this)->GetSpellEffectMutex()->readlock(__FUNCTION__, __LINE__);
  1507. while(i < 30){
  1508. //Change value of spell id for this packet if spell exists
  1509. spell_id = info->spell_effects[i].spell_id;
  1510. if(spell_id > 0)
  1511. spell_id = 0xFFFFFFFF - spell_id;
  1512. else
  1513. spell_id = 0;
  1514. packet->setSubstructDataByName("spell_effects", "spell_id", spell_id, i);
  1515. //Change value of spell icon for this packet if spell exists
  1516. spell_icon = info->spell_effects[i].icon;
  1517. if(spell_icon > 0){
  1518. if(!(spell_icon == 0xFFFF))
  1519. spell_icon = 0xFFFF - spell_icon;
  1520. }
  1521. else
  1522. spell_icon = 0;
  1523. packet->setSubstructDataByName("spell_effects", "spell_icon", spell_icon, i);
  1524. //Change backdrop values to match values in this packet
  1525. backdrop = info->spell_effects[i].icon_backdrop;
  1526. switch(backdrop){
  1527. case 312:
  1528. backdrop = 33080;
  1529. break;
  1530. case 313:
  1531. backdrop = 33081;
  1532. break;
  1533. case 314:
  1534. backdrop = 33082;
  1535. break;
  1536. case 315:
  1537. backdrop = 33083;
  1538. break;
  1539. case 316:
  1540. backdrop = 33084;
  1541. break;
  1542. case 317:
  1543. backdrop = 33085;
  1544. break;
  1545. case (318 || 319):
  1546. backdrop = 33086;
  1547. break;
  1548. default:
  1549. break;
  1550. }
  1551. packet->setSubstructDataByName("spell_effects", "spell_icon_backdrop", backdrop, i);
  1552. spell = info->spell_effects[i].spell;
  1553. if (spell)
  1554. packet->setSubstructDataByName("spell_effects", "spell_triggercount", spell->num_triggers, i);
  1555. i++;
  1556. }
  1557. ((Entity*)this)->GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
  1558. }
  1559. }
  1560. void Spawn::MoveToLocation(Spawn* spawn, float distance, bool immediate){
  1561. if(!spawn)
  1562. return;
  1563. SetRunningTo(spawn);
  1564. FaceTarget(spawn);
  1565. if(immediate)
  1566. ClearRunningLocations();
  1567. AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance);
  1568. }
  1569. void Spawn::ProcessMovement(){
  1570. if(IsPlayer()){
  1571. //Check if player is riding a boat, if so update pos (boat's current location + XYZ offsets)
  1572. Player* player = ((Player*)this);
  1573. int32 boat_id = player->GetBoatSpawn();
  1574. Spawn* boat = 0;
  1575. if(boat_id > 0)
  1576. boat = GetZone()->GetSpawnByID(boat_id);
  1577. if(boat){
  1578. SetX(boat->GetX() + player->GetBoatX());
  1579. SetY(boat->GetY() + player->GetBoatY());
  1580. SetZ(boat->GetZ() + player->GetBoatZ());
  1581. }
  1582. return;
  1583. }
  1584. if (GetHP() <= 0 && !IsWidget())
  1585. return;
  1586. MMovementLoop.lock();
  1587. Spawn* followTarget = GetZone()->GetSpawnByID(m_followTarget);
  1588. if (!followTarget && m_followTarget > 0)
  1589. m_followTarget = 0;
  1590. if (following && followTarget) {
  1591. // Need to clear m_followTarget before the zoneserver deletes it
  1592. if (followTarget->GetHP() <= 0) {
  1593. followTarget = 0;
  1594. MMovementLoop.unlock();
  1595. return;
  1596. }
  1597. if (!IsEntity() || (!((Entity*)this)->IsCasting() && !((Entity*)this)->IsMezzedOrStunned() && !((Entity*)this)->IsRooted())) {
  1598. if (GetBaseSpeed() > 0) {
  1599. CalculateRunningLocation();
  1600. }
  1601. else {
  1602. float speed = 4.0f;
  1603. if (IsEntity())
  1604. speed = ((Entity*)this)->GetMaxSpeed();
  1605. SetSpeed(speed);
  1606. }
  1607. MovementLocation* loc = GetCurrentRunningLocation();
  1608. if (GetDistance(followTarget, true) <= MAX_COMBAT_RANGE || (loc && loc->x == GetX() && loc->y == GetY() && loc->z == GetZ())) {
  1609. ClearRunningLocations();
  1610. CalculateRunningLocation(true);
  1611. }
  1612. else if (loc) {
  1613. float distance = GetDistance(loc->x, loc->y, loc->z, followTarget->GetX(), followTarget->GetY(), followTarget->GetZ());
  1614. if (distance > MAX_COMBAT_RANGE) {
  1615. MoveToLocation(followTarget, MAX_COMBAT_RANGE);
  1616. CalculateRunningLocation();
  1617. }
  1618. }
  1619. else {
  1620. MoveToLocation(followTarget, MAX_COMBAT_RANGE);
  1621. CalculateRunningLocation();
  1622. }
  1623. }
  1624. }
  1625. // Movement loop is only for scripted paths
  1626. else if(!EngagedInCombat() && !NeedsToResumeMovement() && movement_loop.size() > 0 && movement_index < movement_loop.size() && (!IsNPC() || !((NPC*)this)->m_runningBack)){
  1627. // Get the target location
  1628. MovementData* data = movement_loop[movement_index];
  1629. // need to resume our movement
  1630. if(resume_movement){
  1631. if (movement_locations){
  1632. while (movement_locations->size()){
  1633. safe_delete(movement_locations->front());
  1634. movement_locations->pop_front();
  1635. }
  1636. movement_locations->clear();
  1637. }
  1638. data = movement_loop[movement_index];
  1639. SetSpeed(data->speed);
  1640. if(!IsWidget())
  1641. FaceTarget(data->x, data->z);
  1642. // 0 delay at target location, need to set multiple locations
  1643. if(data->delay == 0 && movement_loop.size() > 0) {
  1644. int16 tmp_index = movement_index+1;
  1645. MovementData* data2 = 0;
  1646. if(tmp_index < movement_loop.size())
  1647. data2 = movement_loop[tmp_index];
  1648. else
  1649. data2 = movement_loop[0];
  1650. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false);
  1651. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed);
  1652. }
  1653. // delay at target location, only need to set 1 location
  1654. else
  1655. AddRunningLocation(data->x, data->y, data->z, data->speed);
  1656. movement_start_time = 0;
  1657. resume_movement = false;
  1658. }
  1659. // If we are not moving or we have arrived at our destination
  1660. else if(!IsRunning() || (data && data->x == GetX() && data->y == GetY() && data->z == GetZ())){
  1661. // If we were moving remove the last running location (the point we just arrived at)
  1662. if(IsRunning())
  1663. RemoveRunningLocation();
  1664. // If this waypoint has a delay and we just arrived here (movement_start_time == 0)
  1665. if(data->delay > 0 && movement_start_time == 0){
  1666. // Set the current time
  1667. movement_start_time = Timer::GetCurrentTime2();
  1668. // If this waypoint had a lua function then call it
  1669. if(data->lua_function.length() > 0)
  1670. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  1671. }
  1672. // If this waypoint has no delay or we have waited the required time (current time >= delay + movement_start_time)
  1673. else if(data->delay == 0 || (data->delay > 0 && Timer::GetCurrentTime2() >= (data->delay+movement_start_time))) {
  1674. // if no delay at this waypoint but a lua function for it then call the function
  1675. if(data->delay == 0 && data->lua_function.length() > 0)
  1676. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  1677. // Advance the current movement loop index
  1678. if((int16)(movement_index+1) < movement_loop.size())
  1679. movement_index++;
  1680. else
  1681. movement_index = 0;
  1682. // Get the next target location
  1683. data = movement_loop[movement_index];
  1684. // set the speed for that location
  1685. SetSpeed(data->speed);
  1686. if(!IsWidget())
  1687. // turn towards the location
  1688. FaceTarget(data->x, data->z);
  1689. // If 0 delay at location get and set data for the point after it
  1690. if(data->delay == 0 && movement_loop.size() > 0){
  1691. while (movement_locations->size()){
  1692. safe_delete(movement_locations->front());
  1693. movement_locations->pop_front();
  1694. }
  1695. // clear current target locations
  1696. movement_locations->clear();
  1697. // get the data for the location after out new location
  1698. int16 tmp_index = movement_index+1;
  1699. MovementData* data2 = 0;
  1700. if(tmp_index < movement_loop.size())
  1701. data2 = movement_loop[tmp_index];
  1702. else
  1703. data2 = movement_loop[0];
  1704. // set the first location (adds it to movement_locations that we just cleared)
  1705. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false);
  1706. // set the location after that
  1707. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed);
  1708. }
  1709. // there is a delay at the next location so we only need to set it
  1710. else
  1711. AddRunningLocation(data->x, data->y, data->z, data->speed);
  1712. // reset this timer to 0 now that we are moving again
  1713. movement_start_time = 0;
  1714. }
  1715. }
  1716. // moving and not at target location yet
  1717. else if(GetBaseSpeed() > 0)
  1718. CalculateRunningLocation();
  1719. // not moving, have a target location but not at it yet
  1720. else if (data) {
  1721. SetSpeed(data->speed);
  1722. AddRunningLocation(data->x, data->y, data->z, data->speed);
  1723. }
  1724. }
  1725. else if (IsRunning()) {
  1726. CalculateRunningLocation();
  1727. }
  1728. else if (IsNPC() && !IsRunning() && !EngagedInCombat() && ((NPC*)this)->GetRunbackLocation()) {
  1729. // Is an npc that is not moving and not engaged in combat but has a run back location set then clear the runback location
  1730. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Clear runback location for %s", GetName());
  1731. SetPos(&appearance.pos.Dir1, ((NPC*)this)->m_runbackHeading, false);
  1732. SetPos(&appearance.pos.Dir2, ((NPC*)this)->m_runbackHeading, true);
  1733. ((NPC*)this)->ClearRunback();
  1734. resume_movement = true;
  1735. NeedsToResumeMovement(false);
  1736. }
  1737. MMovementLoop.unlock();
  1738. }
  1739. void Spawn::ResetMovement(){
  1740. MMovementLoop.lock();
  1741. vector<MovementData*>::iterator itr;
  1742. for(itr = movement_loop.begin(); itr != movement_loop.end(); itr++){
  1743. safe_delete(*itr);
  1744. }
  1745. MMovementLoop.unlock();
  1746. resume_movement = true;
  1747. movement_index = 0;
  1748. }
  1749. void Spawn::AddMovementLocation(float x, float y, float z, float speed, int16 delay, const char* lua_function){
  1750. LogWrite(LUA__DEBUG, 5, "LUA", "AddMovementLocation: x: %.2f, y: %.2f, z: %.2f, speed: %.2f, delay: %i, lua: %s",
  1751. x, y, z, speed, delay, string(lua_function).c_str());
  1752. MovementData* data = new MovementData;
  1753. data->x = x;
  1754. data->y = y;
  1755. data->z = z;
  1756. data->speed = speed;
  1757. data->delay = delay*1000;
  1758. if(lua_function)
  1759. data->lua_function = string(lua_function);
  1760. MMovementLoop.lock();
  1761. movement_loop.push_back(data);
  1762. MMovementLoop.unlock();
  1763. }
  1764. bool Spawn::IsRunning(){
  1765. if(movement_locations && movement_locations->size() > 0)
  1766. return true;
  1767. else
  1768. return false;
  1769. }
  1770. void Spawn::RunToLocation(float x, float y, float z, float following_x, float following_y, float following_z){
  1771. if(!IsWidget())
  1772. FaceTarget(x, z);
  1773. SetPos(&appearance.pos.X2, x);
  1774. SetPos(&appearance.pos.Y2, y);
  1775. SetPos(&appearance.pos.Z2, z);
  1776. if(following_x == 0 && following_y == 0 && following_z == 0){
  1777. SetPos(&appearance.pos.X3, x);
  1778. SetPos(&appearance.pos.Y3, y);
  1779. SetPos(&appearance.pos.Z3, z);
  1780. }
  1781. else{
  1782. SetPos(&appearance.pos.X3, following_x);
  1783. SetPos(&appearance.pos.Y3, following_y);
  1784. SetPos(&appearance.pos.Z3, following_z);
  1785. }
  1786. }
  1787. MovementLocation* Spawn::GetCurrentRunningLocation(){
  1788. MovementLocation* ret = 0;
  1789. if(movement_locations && movement_locations->size() > 0){
  1790. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  1791. ret = movement_locations->front();
  1792. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  1793. }
  1794. return ret;
  1795. }
  1796. MovementLocation* Spawn::GetLastRunningLocation(){
  1797. MovementLocation* ret = 0;
  1798. if(movement_locations && movement_locations->size() > 0){
  1799. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  1800. ret = movement_locations->back();
  1801. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  1802. }
  1803. return ret;
  1804. }
  1805. void Spawn::AddRunningLocation(float x, float y, float z, float speed, float distance_away, bool attackable, bool finished_adding_locations, string lua_function){
  1806. if(speed == 0)
  1807. return;
  1808. MovementLocation* current_location = 0;
  1809. float distance = GetDistance(x, y, z, distance_away != 0);
  1810. if(distance_away != 0){
  1811. distance -= distance_away;
  1812. x = x - (GetX() - x)*distance_away/distance;
  1813. z = z - (GetZ() - z)*distance_away/distance;
  1814. }
  1815. if(!movement_locations){
  1816. movement_locations = new deque<MovementLocation*>();
  1817. MMovementLocations = new Mutex();
  1818. }
  1819. MovementLocation* data = new MovementLocation;
  1820. data->x = x;
  1821. data->y = y;
  1822. data->z = z;
  1823. data->speed = speed;
  1824. data->attackable = attackable;
  1825. data->lua_function = lua_function;
  1826. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  1827. if(movement_locations->size() > 0)
  1828. current_location = movement_locations->back();
  1829. if(!current_location){
  1830. SetSpawnOrigX(GetX());
  1831. SetSpawnOrigY(GetY());
  1832. SetSpawnOrigZ(GetZ());
  1833. SetSpawnOrigHeading(GetHeading());
  1834. }
  1835. movement_locations->push_back(data);
  1836. if(finished_adding_locations){
  1837. current_location = movement_locations->front();
  1838. SetSpeed(current_location->speed);
  1839. if(movement_locations->size() > 1){
  1840. data = movement_locations->at(1);
  1841. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  1842. }
  1843. else
  1844. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  1845. }
  1846. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  1847. }
  1848. bool Spawn::RemoveRunningLocation(){
  1849. bool ret = false;
  1850. if(movement_locations){
  1851. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  1852. if(movement_locations->size() > 0){
  1853. delete movement_locations->front();
  1854. movement_locations->pop_front();
  1855. ret = true;
  1856. }
  1857. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  1858. }
  1859. return ret;
  1860. }
  1861. void Spawn::ClearRunningLocations(){
  1862. while(RemoveRunningLocation()){}
  1863. }
  1864. bool Spawn::CalculateChange(){
  1865. bool remove_needed = false;
  1866. if(movement_locations && MMovementLocations){
  1867. MovementLocation* data = 0;
  1868. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  1869. if(movement_locations->size() > 0){
  1870. // Target location
  1871. data = movement_locations->front();
  1872. // If no target or we are at the target location need to remove this point
  1873. if(!data || (data->x == GetX() && data->y == GetY() && data->z == GetZ()))
  1874. remove_needed = true;
  1875. if(data){
  1876. if(NeedsToResumeMovement()){
  1877. resume_movement = true;
  1878. NeedsToResumeMovement(false);
  1879. }
  1880. if(!data->attackable)
  1881. SetHeading(GetSpawnOrigHeading());
  1882. }
  1883. }
  1884. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  1885. if(remove_needed) {
  1886. if (data && data->lua_function.length() > 0)
  1887. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  1888. RemoveRunningLocation();
  1889. //CalculateChange();
  1890. }
  1891. else if(data){
  1892. // Speed is per second so we need a time_step (amount of time since the last update) to modify movement by
  1893. 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
  1894. // Get current location
  1895. float nx = GetX();
  1896. float ny = GetY();
  1897. float nz = GetZ();
  1898. // Get Forward vecotr
  1899. float tar_vx = data->x - nx;
  1900. float tar_vy = data->y - ny;
  1901. float tar_vz = data->z - nz;
  1902. // Multiply speed by the time_step to get how much should have changed over the last tick
  1903. float speed = GetSpeed() * time_step;
  1904. // Normalize the forward vector and multiply by speed, this gives us our change in coords, just need to add them to our current coords
  1905. float len = sqrtf(tar_vx * tar_vx + tar_vy * tar_vy + tar_vz * tar_vz);
  1906. tar_vx = (tar_vx / len) * speed;
  1907. tar_vy = (tar_vy / len) * speed;
  1908. tar_vz = (tar_vz / len) * speed;
  1909. // Distance less then 0.5 just set the npc to the target location
  1910. if (GetDistance(data->x, data->y, data->z, IsWidget() ? false : true) <= 0.5f) {
  1911. SetX(data->x, false);
  1912. SetY(data->y, false);
  1913. SetZ(data->z, false);
  1914. }
  1915. else {
  1916. SetX(nx + tar_vx, false);
  1917. SetY(ny + tar_vy, false);
  1918. SetZ(nz + tar_vz, false);
  1919. }
  1920. if (GetZone()->Grid != nullptr) {
  1921. Cell* newCell = GetZone()->Grid->GetCell(GetX(), GetZ());
  1922. if (newCell != Cell_Info.CurrentCell) {
  1923. GetZone()->Grid->RemoveSpawnFromCell(this);
  1924. GetZone()->Grid->AddSpawn(this, newCell);
  1925. }
  1926. int32 newGrid = GetZone()->Grid->GetGridID(this);
  1927. if (newGrid != appearance.pos.grid_id)
  1928. SetPos(&(appearance.pos.grid_id), newGrid);
  1929. }
  1930. }
  1931. }
  1932. return remove_needed;
  1933. }
  1934. void Spawn::CalculateRunningLocation(bool stop){
  1935. bool removed = CalculateChange();
  1936. if(stop) {
  1937. //following = false;
  1938. SetPos(&appearance.pos.X2, GetX());
  1939. SetPos(&appearance.pos.Y2, GetY());
  1940. SetPos(&appearance.pos.Z2, GetZ());
  1941. SetPos(&appearance.pos.X3, GetX());
  1942. SetPos(&appearance.pos.Y3, GetY());
  1943. SetPos(&appearance.pos.Z3, GetZ());
  1944. }
  1945. else if (removed && movement_locations->size() > 0) {
  1946. MovementLocation* current_location = movement_locations->at(0);
  1947. if (movement_locations->size() > 1) {
  1948. MovementLocation* data = movement_locations->at(1);
  1949. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  1950. }
  1951. else
  1952. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  1953. }
  1954. }
  1955. void Spawn::FaceTarget(float x, float z){
  1956. float angle;
  1957. double diff_x = x - GetX();
  1958. double diff_z = z - GetZ();
  1959. //If we're very close to the same spot don't bother changing heading
  1960. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  1961. return;
  1962. }
  1963. if(diff_z==0){
  1964. if(diff_x > 0)
  1965. angle = 90;
  1966. else
  1967. angle = 270;
  1968. }
  1969. else
  1970. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  1971. if(angle < 0)
  1972. angle = angle + 360;
  1973. else
  1974. angle = angle + 180;
  1975. if(diff_x < 0)
  1976. angle = angle + 180;
  1977. if (last_heading_angle == angle) return;
  1978. SetHeading(angle);
  1979. }
  1980. void Spawn::FaceTarget(Spawn* target){
  1981. if(!target)
  1982. return;
  1983. FaceTarget(target->GetX(), target->GetZ());
  1984. if(GetHP() > 0 && target->IsPlayer() && !EngagedInCombat()){
  1985. GetZone()->AddHeadingTimer(this);
  1986. SetTempActionState(0);
  1987. }
  1988. }
  1989. bool Spawn::MeetsSpawnAccessRequirements(Player* player){
  1990. bool ret = false;
  1991. Quest* quest = 0;
  1992. //Check if we meet all quest requirements first..
  1993. m_requiredQuests.readlock(__FUNCTION__, __LINE__);
  1994. if (player && required_quests.size() > 0) {
  1995. map<int32, vector<int16>* >::iterator itr;
  1996. for (itr = required_quests.begin(); itr != required_quests.end(); itr++) {
  1997. player->AddQuestRequiredSpawn(this, itr->first);
  1998. vector<int16>* quest_steps = itr->second;
  1999. for (int32 i = 0; i < quest_steps->size(); i++) {
  2000. quest = player->GetQuest(itr->first);
  2001. if (req_quests_continued_access) {
  2002. if (quest) {
  2003. if (quest->GetQuestStepCompleted(quest_steps->at(i))) {
  2004. ret = true;
  2005. break;
  2006. }
  2007. }
  2008. else if (player->GetCompletedQuest(itr->first)) {
  2009. ret = true;
  2010. break;
  2011. }
  2012. }
  2013. if (quest && quest->QuestStepIsActive(quest_steps->at(i))) {
  2014. ret = true;
  2015. break;
  2016. }
  2017. }
  2018. }
  2019. }
  2020. else
  2021. ret = true;
  2022. m_requiredQuests.releasereadlock(__FUNCTION__, __LINE__);
  2023. if (!ret)
  2024. return ret;
  2025. //Now check if the player meets all history requirements
  2026. m_requiredHistory.readlock(__FUNCTION__, __LINE__);
  2027. if (required_history.size() > 0){
  2028. map<int32, LUAHistory>::iterator itr;
  2029. for (itr = required_history.begin(); itr != required_history.end(); itr++){
  2030. player->AddHistoryRequiredSpawn(this, itr->first);
  2031. LUAHistory* player_history = player->GetLUAHistory(itr->first);
  2032. if (player_history){
  2033. if (player_history->Value != itr->second.Value || player_history->Value2 != itr->second.Value2)
  2034. ret = false;
  2035. }
  2036. else
  2037. ret = false;
  2038. if (!ret)
  2039. break;
  2040. }
  2041. }
  2042. m_requiredHistory.releasereadlock(__FUNCTION__, __LINE__);
  2043. return ret;
  2044. }
  2045. vector<Spawn*>* Spawn::GetSpawnGroup(){
  2046. vector<Spawn*>* ret_list = 0;
  2047. if(spawn_group_list){
  2048. ret_list = new vector<Spawn*>();
  2049. if(MSpawnGroup)
  2050. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  2051. ret_list->insert(ret_list->begin(), spawn_group_list->begin(), spawn_group_list->end());
  2052. if(MSpawnGroup)
  2053. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  2054. }
  2055. return ret_list;
  2056. }
  2057. bool Spawn::HasSpawnGroup() {
  2058. return spawn_group_list && spawn_group_list->size() > 0;
  2059. }
  2060. bool Spawn::IsInSpawnGroup(Spawn* spawn) {
  2061. bool ret = false;
  2062. if (HasSpawnGroup() && spawn) {
  2063. vector<Spawn*>::iterator itr;
  2064. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  2065. if ((*itr) == spawn) {
  2066. ret = true;
  2067. break;
  2068. }
  2069. }
  2070. }
  2071. return ret;
  2072. }
  2073. void Spawn::AddSpawnToGroup(Spawn* spawn){
  2074. if(!spawn)
  2075. return;
  2076. if(!spawn_group_list){
  2077. spawn_group_list = new vector<Spawn*>();
  2078. spawn_group_list->push_back(this);
  2079. safe_delete(MSpawnGroup);
  2080. MSpawnGroup = new Mutex();
  2081. MSpawnGroup->SetName("Spawn::MSpawnGroup");
  2082. }
  2083. vector<Spawn*>::iterator itr;
  2084. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2085. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2086. if((*itr) == spawn){
  2087. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2088. return;
  2089. }
  2090. }
  2091. spawn_group_list->push_back(spawn);
  2092. spawn->SetSpawnGroupList(spawn_group_list, MSpawnGroup);
  2093. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2094. }
  2095. void Spawn::SetSpawnGroupList(vector<Spawn*>* list, Mutex* mutex){
  2096. spawn_group_list = list;
  2097. MSpawnGroup = mutex;
  2098. }
  2099. void Spawn::RemoveSpawnFromGroup(bool erase_all){
  2100. SetSpawnGroupID(0);
  2101. bool del = false;
  2102. if(MSpawnGroup){
  2103. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2104. if(spawn_group_list){
  2105. vector<Spawn*>::iterator itr;
  2106. Spawn* spawn = 0;
  2107. if(spawn_group_list->size() == 1)
  2108. erase_all = true;
  2109. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2110. spawn = *itr;
  2111. if (spawn) {
  2112. if(!erase_all){
  2113. if(spawn == this){
  2114. spawn_group_list->erase(itr);
  2115. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2116. spawn_group_list = 0;
  2117. MSpawnGroup = 0;
  2118. return;
  2119. }
  2120. }
  2121. else{
  2122. if (spawn != this)
  2123. spawn->SetSpawnGroupList(0, 0);
  2124. }
  2125. }
  2126. }
  2127. if (erase_all)
  2128. spawn_group_list->clear();
  2129. del = (spawn_group_list->size() == 0);
  2130. }
  2131. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2132. if (del){
  2133. safe_delete(MSpawnGroup);
  2134. safe_delete(spawn_group_list);
  2135. }
  2136. }
  2137. }
  2138. void Spawn::SetSpawnGroupID(int32 id){
  2139. group_id = id;
  2140. }
  2141. int32 Spawn::GetSpawnGroupID(){
  2142. return group_id;
  2143. }
  2144. void Spawn::AddChangedZoneSpawn(){
  2145. if(send_spawn_changes && GetZone())
  2146. GetZone()->AddChangedSpawn(this);
  2147. }
  2148. void Spawn::RemoveSpawnAccess(Spawn* spawn) {
  2149. if (allowed_access.count(spawn->GetID()) > 0) {
  2150. allowed_access.erase(spawn->GetID());
  2151. GetZone()->HidePrivateSpawn(this);
  2152. }
  2153. }
  2154. void Spawn::SetFollowTarget(Spawn* spawn) {
  2155. if (spawn && spawn != this) {
  2156. m_followTarget = spawn->GetID();
  2157. }
  2158. else {
  2159. m_followTarget = 0;
  2160. if (following)
  2161. following = false;
  2162. }
  2163. }
  2164. void Spawn::AddTempVariable(string var, string val) {
  2165. m_tempVariableTypes[var] = 5;
  2166. m_tempVariables[var] = val;
  2167. }
  2168. void Spawn::AddTempVariable(string var, Spawn* val) {
  2169. m_tempVariableTypes[var] = 1;
  2170. m_tempVariableSpawn[var] = val->GetID();
  2171. }
  2172. void Spawn::AddTempVariable(string var, ZoneServer* val) {
  2173. m_tempVariableTypes[var] = 2;
  2174. m_tempVariableZone[var] = val;
  2175. }
  2176. void Spawn::AddTempVariable(string var, Item* val) {
  2177. m_tempVariableTypes[var] = 3;
  2178. m_tempVariableItem[var] = val;
  2179. }
  2180. void Spawn::AddTempVariable(string var, Quest* val) {
  2181. m_tempVariableTypes[var] = 4;
  2182. m_tempVariableQuest[var] = val;
  2183. }
  2184. string Spawn::GetTempVariable(string var) {
  2185. string ret = "";
  2186. if (m_tempVariables.count(var) > 0)
  2187. ret = m_tempVariables[var];
  2188. return ret;
  2189. }
  2190. Spawn* Spawn::GetTempVariableSpawn(string var) {
  2191. Spawn* ret = 0;
  2192. if (m_tempVariableSpawn.count(var) > 0)
  2193. ret = GetZone()->GetSpawnByID(m_tempVariableSpawn[var]);
  2194. return ret;
  2195. }
  2196. ZoneServer* Spawn::GetTempVariableZone(string var) {
  2197. ZoneServer* ret = 0;
  2198. if (m_tempVariableZone.count(var) > 0)
  2199. ret = m_tempVariableZone[var];
  2200. return ret;
  2201. }
  2202. Item* Spawn::GetTempVariableItem(string var) {
  2203. Item* ret = 0;
  2204. if (m_tempVariableItem.count(var) > 0)
  2205. ret = m_tempVariableItem[var];
  2206. return ret;
  2207. }
  2208. Quest* Spawn::GetTempVariableQuest(string var) {
  2209. Quest* ret = 0;
  2210. if (m_tempVariableQuest.count(var) > 0)
  2211. ret = m_tempVariableQuest[var];
  2212. return ret;
  2213. }
  2214. int8 Spawn::GetTempVariableType(string var) {
  2215. int8 ret = 0;
  2216. if (m_tempVariableTypes.count(var) > 0)
  2217. ret = m_tempVariableTypes[var];
  2218. return ret;
  2219. }
  2220. void Spawn::DeleteTempVariable(string var) {
  2221. int8 type = GetTempVariableType(var);
  2222. switch (type) {
  2223. case 1:
  2224. m_tempVariableSpawn.erase(var);
  2225. break;
  2226. case 2:
  2227. m_tempVariableZone.erase(var);
  2228. break;
  2229. case 3:
  2230. m_tempVariableItem.erase(var);
  2231. break;
  2232. case 4:
  2233. m_tempVariableQuest.erase(var);
  2234. break;
  2235. case 5:
  2236. m_tempVariables.erase(var);
  2237. break;
  2238. }
  2239. m_tempVariableTypes.erase(var);
  2240. }
  2241. Spawn* Spawn::GetRunningTo() {
  2242. return GetZone()->GetSpawnByID(running_to);
  2243. }
  2244. Spawn* Spawn::GetFollowTarget() {
  2245. return GetZone()->GetSpawnByID(m_followTarget);
  2246. }
  2247. void Spawn::CopySpawnAppearance(Spawn* spawn){
  2248. if (!spawn)
  2249. return;
  2250. //This function copies the appearace of the provided spawn to this one
  2251. if (spawn->IsEntity() && IsEntity()){
  2252. memcpy(&((Entity*)this)->features, &((Entity*)spawn)->features, sizeof(CharFeatures));
  2253. memcpy(&((Entity*)this)->equipment, &((Entity*)spawn)->equipment, sizeof(EQ2_Equipment));
  2254. }
  2255. SetSize(spawn->GetSize());
  2256. SetModelType(spawn->GetModelType());
  2257. }