Spawn.cpp 79 KB

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