Spawn.cpp 79 KB

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