Spawn.cpp 80 KB

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