NPC.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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 "NPC.h"
  17. #include "WorldDatabase.h"
  18. #include <math.h>
  19. #include "client.h"
  20. #include "World.h"
  21. #include "races.h"
  22. #include "../common/Log.h"
  23. #include "NPC_AI.h"
  24. #include "Appearances.h"
  25. #include "SpellProcess.h"
  26. #include "Skills.h"
  27. extern MasterSpellList master_spell_list;
  28. extern ConfigReader configReader;
  29. extern WorldDatabase database;
  30. extern World world;
  31. extern Races races;
  32. extern Appearance master_appearance_list;
  33. extern MasterSkillList master_skill_list;
  34. NPC::NPC(){
  35. Initialize();
  36. if (GetMaxSpeed() > 0)
  37. SetSpeed(GetMaxSpeed());
  38. }
  39. NPC::NPC(NPC* old_npc){
  40. Initialize();
  41. if(old_npc){
  42. if(old_npc->GetSizeOffset() > 0){
  43. int8 offset = old_npc->GetSizeOffset()+1;
  44. sint32 tmp_size = old_npc->size + (rand()%offset - rand()%offset);
  45. if(tmp_size < 0)
  46. tmp_size = 1;
  47. else if(tmp_size >= 0xFFFF)
  48. tmp_size = 0xFFFF;
  49. size = (int16)tmp_size;
  50. }
  51. else
  52. size = old_npc->size;
  53. SetCollector(old_npc->IsCollector());
  54. SetMerchantID(old_npc->GetMerchantID());
  55. SetMerchantType(old_npc->GetMerchantType());
  56. SetMerchantLevelRange(old_npc->GetMerchantMinLevel(), old_npc->GetMerchantMaxLevel());
  57. SetPrimaryCommands(&old_npc->primary_command_list);
  58. SetSecondaryCommands(&old_npc->secondary_command_list);
  59. appearance_id = old_npc->appearance_id;
  60. database_id = old_npc->database_id;
  61. primary_command_list_id = old_npc->primary_command_list_id;
  62. secondary_command_list_id = old_npc->secondary_command_list_id;
  63. this->SetInfoStruct(old_npc->GetInfoStruct());
  64. //memcpy(GetInfoStruct(), old_npc->GetInfoStruct(), sizeof(InfoStruct));
  65. memcpy(&appearance, &old_npc->appearance, sizeof(AppearanceData));
  66. memcpy(&features, &old_npc->features, sizeof(CharFeatures));
  67. memcpy(&equipment, &old_npc->equipment, sizeof(EQ2_Equipment));
  68. if(appearance.min_level < appearance.max_level)
  69. SetLevel(appearance.min_level + rand()%((appearance.max_level - appearance.min_level)+1));
  70. target = 0;
  71. SetTotalHPBase(old_npc->GetTotalHPBase());
  72. SetTotalPowerBase(old_npc->GetTotalPowerBase());
  73. faction_id = old_npc->faction_id;
  74. movement_interrupted = false;
  75. old_npc->SetQuestsRequired(this);
  76. SetTransporterID(old_npc->GetTransporterID());
  77. SetAttackType(old_npc->GetAttackType());
  78. SetAIStrategy(old_npc->GetAIStrategy());
  79. SetPrimarySpellList(old_npc->GetPrimarySpellList());
  80. SetSecondarySpellList(old_npc->GetSecondarySpellList());
  81. SetPrimarySkillList(old_npc->GetPrimarySkillList());
  82. SetSecondarySkillList(old_npc->GetSecondarySkillList());
  83. SetEquipmentListID(old_npc->GetEquipmentListID());
  84. SetAggroRadius(old_npc->GetBaseAggroRadius());
  85. SetCastPercentage(old_npc->GetCastPercentage());
  86. SetRandomize(old_npc->GetRandomize());
  87. if(appearance.randomize > 0)
  88. Randomize(this, appearance.randomize);
  89. CalculateBonuses();
  90. SetHP(GetTotalHP());
  91. SetPower(GetTotalPower());
  92. ChangePrimaryWeapon();
  93. ChangeSecondaryWeapon();
  94. SetSoundsDisabled(old_npc->IsSoundsDisabled());
  95. SetFlyingCreature();
  96. SetWaterCreature();
  97. SetOmittedByDBFlag(old_npc->IsOmittedByDBFlag());
  98. SetLootTier(old_npc->GetLootTier());
  99. }
  100. }
  101. NPC::~NPC(){
  102. ResetMovement();
  103. if(skills){
  104. map<string, Skill*>::iterator skill_itr;
  105. for(skill_itr = skills->begin(); skill_itr != skills->end(); skill_itr++){
  106. safe_delete(skill_itr->second);
  107. }
  108. safe_delete(skills);
  109. }
  110. safe_delete(spells);
  111. MutexMap<int32, SkillBonus*>::iterator sb_itr = skill_bonus_list.begin();
  112. while (sb_itr.Next())
  113. RemoveSkillBonus(sb_itr.first);
  114. safe_delete(runback);
  115. safe_delete(m_brain);
  116. }
  117. void NPC::Initialize(){
  118. ai_strategy = 0;
  119. attack_type = 0;
  120. movement_index = 0;
  121. resume_movement = true;
  122. movement_start_time = 0;
  123. spawn_type = 2;
  124. movement_interrupted = false;
  125. attack_resume_needed = false;
  126. MMovementLoop.SetName("NPC::MMovementLoop");
  127. last_movement_update = Timer::GetCurrentTime2();
  128. aggro_radius = 0.0f;
  129. base_aggro_radius = 0.0f;
  130. skills = 0;
  131. spells = 0;
  132. runback = 0;
  133. m_brain = new ::Brain(this);
  134. MBrain.SetName("NPC::m_brain");
  135. m_runningBack = false;
  136. m_runbackHeadingDir1 = m_runbackHeadingDir2 = 0;
  137. following = false;
  138. SetFollowTarget(0);
  139. m_petDismissing = false;
  140. m_ShardID = 0;
  141. m_ShardCharID = 0;
  142. m_ShardCreatedTimestamp = 0;
  143. }
  144. EQ2Packet* NPC::serialize(Player* player, int16 version){
  145. return spawn_serialize(player, version);
  146. }
  147. void NPC::SetSkills(map<string, Skill*>* in_skills){
  148. if (skills) {
  149. map<string, Skill*>::iterator skill_itr;
  150. for(skill_itr = skills->begin(); skill_itr != skills->end(); skill_itr++){
  151. safe_delete(skill_itr->second);
  152. }
  153. safe_delete(skills);
  154. }
  155. skills = in_skills;
  156. }
  157. void NPC::SetSpells(vector<Spell*>* in_spells){
  158. safe_delete(spells);
  159. spells = in_spells;
  160. }
  161. void NPC::SetRunbackLocation(float x, float y, float z, int32 gridid, bool set_hp_runback){
  162. safe_delete(runback);
  163. runback = new MovementLocation;
  164. runback->x = x;
  165. runback->y = y;
  166. runback->z = z;
  167. runback->gridid = gridid;
  168. runback->stage = 0;
  169. runback->reset_hp_on_runback = set_hp_runback;
  170. }
  171. MovementLocation* NPC::GetRunbackLocation(){
  172. return runback;
  173. }
  174. float NPC::GetRunbackDistance(){
  175. if(!runback)
  176. return 0;
  177. return GetDistance(runback->x, runback->y, runback->z);
  178. }
  179. void NPC::Runback(float distance, bool stopFollowing){
  180. if(!runback)
  181. return;
  182. if ( distance == 0.0f )
  183. distance = GetRunbackDistance(); // gotta make sure its true, lua doesn't send the distance
  184. if(stopFollowing)
  185. following = false;
  186. if (!m_runningBack)
  187. {
  188. ClearRunningLocations();
  189. GetZone()->movementMgr->StopNavigation((Entity*)this);
  190. }
  191. m_runningBack = true;
  192. SetSpeed(GetMaxSpeed()*2);
  193. if (CheckLoS(glm::vec3(runback->x, runback->z, runback->y + 1.0f), glm::vec3(GetX(), GetZ(), GetY() + 1.0f)))
  194. {
  195. FaceTarget(runback->x, runback->z);
  196. ClearRunningLocations();
  197. GetZone()->movementMgr->DisruptNavigation((Entity*)this);
  198. if (GetRunbackLocation()->gridid > 0)
  199. SetLocation(GetRunbackLocation()->gridid);
  200. AddRunningLocation(runback->x, runback->y, runback->z, GetSpeed(), 0, true, true, "", true);
  201. }
  202. else
  203. GetZone()->movementMgr->NavigateTo((Entity*)this, runback->x, runback->y, runback->z);
  204. //AddRunningLocation(runback->x, runback->y, runback->z, GetSpeed(), 0, false);
  205. last_movement_update = Timer::GetCurrentTime2();
  206. }
  207. void NPC::ClearRunback(){
  208. safe_delete(runback);
  209. m_runningBack = false;
  210. m_runbackHeadingDir1 = m_runbackHeadingDir2 = 0;
  211. resume_movement = true;
  212. NeedsToResumeMovement(false);
  213. }
  214. void NPC::StartRunback(bool reset_hp_on_runback)
  215. {
  216. if(GetRunbackLocation())
  217. return;
  218. SetRunbackLocation(GetX(), GetY(), GetZ(), GetLocation(), reset_hp_on_runback);
  219. m_runbackHeadingDir1 = appearance.pos.Dir1;
  220. m_runbackHeadingDir2 = appearance.pos.Dir2;
  221. }
  222. bool NPC::PauseMovement(int32 period_of_time_ms)
  223. {
  224. if(period_of_time_ms < 1)
  225. period_of_time_ms = 1;
  226. if(HasMovementLoop() || HasMovementLocations())
  227. StartRunback();
  228. RunToLocation(GetX(),GetY(),GetZ());
  229. pause_timer.Start(period_of_time_ms, true);
  230. return true;
  231. }
  232. bool NPC::IsPauseMovementTimerActive()
  233. {
  234. if(pause_timer.Check())
  235. {
  236. pause_timer.Disable();
  237. Runback();
  238. }
  239. return pause_timer.Enabled();
  240. }
  241. void NPC::InCombat(bool val){
  242. if (in_combat == val)
  243. return;
  244. if(in_combat == false && val && GetZone()){
  245. LogWrite(NPC__DEBUG, 3, "NPC", "'%s' engaged in combat with '%s'", this->GetName(), ( GetTarget() ) ? GetTarget()->GetName() : "Unknown" );
  246. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_ATTACKED, GetTarget());
  247. SetTempActionState(0); // disable action states in combat
  248. }
  249. if(!in_combat && val){
  250. // if not a pet and no current run back location set then set one to the current location
  251. bool hadRunback = (GetRunbackLocation() != nullptr);
  252. if(hadRunback) {
  253. pause_timer.Disable();
  254. if(!GetRunbackLocation()->reset_hp_on_runback) // if we aren't resetting hp it isn't a real reset point, just face target swings
  255. ClearRunback();
  256. }
  257. if(!IsPet()) {
  258. StartRunback(true);
  259. }
  260. }
  261. in_combat = val;
  262. if(val){
  263. LogWrite(NPC__DEBUG, 3, "NPC", "'%s' engaged in combat with '%s'", this->GetName(), ( GetTarget() ) ? GetTarget()->GetName() : "Unknown" );
  264. SetLockedNoLoot(ENCOUNTER_STATE_LOCKED);
  265. AddIconValue(64);
  266. // In combat so lets set the NPC's speed to its max speed
  267. if (GetMaxSpeed() > 0)
  268. SetSpeed(GetMaxSpeed());
  269. }
  270. else{
  271. SetLockedNoLoot(ENCOUNTER_STATE_AVAILABLE);
  272. RemoveIconValue(64);
  273. if (GetHP() > 0){
  274. SetTempActionState(-1); //re-enable action states on exiting combat
  275. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_COMBAT_RESET);
  276. // Stop all HO's attached to this NPC
  277. GetZone()->GetSpellProcess()->KillHOBySpawnID(GetID());
  278. }
  279. }
  280. if(!MovementInterrupted() && val && GetSpeed() > 0 && movement_loop.size() > 0){
  281. CalculateRunningLocation(true);
  282. }
  283. MovementInterrupted(val);
  284. }
  285. bool NPC::HandleUse(Client* client, string type){
  286. if(!client || type.length() == 0 || (appearance.show_command_icon == 0 && appearance.display_hand_icon == 0))
  287. return false;
  288. EntityCommand* entity_command = FindEntityCommand(type);
  289. if (entity_command) {
  290. client->GetCurrentZone()->ProcessEntityCommand(entity_command, client->GetPlayer(), client->GetPlayer()->GetTarget());
  291. return true;
  292. }
  293. return false;
  294. /*Spell* spell = master_spell_list.GetSpellByName((char*)type.c_str());
  295. if(spell)
  296. client->GetCurrentZone()->ProcessSpell(spell, client->GetPlayer(), client->GetPlayer()->GetTarget());
  297. else if(GetSpawnScript())
  298. client->GetCurrentZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), (char*)type.c_str());
  299. else
  300. return false;
  301. return true;*/
  302. }
  303. bool NPC::CheckSameAppearance(string name, int16 id)
  304. {
  305. // need to iterate through master_appearance_list finding if id contains name
  306. return true;
  307. }
  308. void NPC::Randomize(NPC* npc, int32 flags)
  309. {
  310. int8 random = 0;
  311. int8 min_val = 0;
  312. int8 max_val = 255;
  313. /* We need to check if gender is going to be randomized first because if the race is going to be
  314. * randomized, we need to know its gender so we can choose the proper model.
  315. * We also need to make sure the model gets set properly if the player chooses to randomize the gender
  316. * and not the race. */
  317. int8 old_gender = npc->GetGender();
  318. if (flags & RANDOMIZE_GENDER)
  319. {
  320. random = MakeRandomInt(1, 2);
  321. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Gender: %i", random);
  322. npc->SetGender(random);
  323. }
  324. if ((flags & RANDOMIZE_RACE) || (flags & RANDOMIZE_GENDER && old_gender != npc->GetGender()) || (flags & RANDOMIZE_MODEL_TYPE))
  325. {
  326. string race_string = "";
  327. int8 gender = npc->GetGender();
  328. if (gender == 1 || gender == 2)
  329. {
  330. if (flags & RANDOMIZE_RACE)
  331. {
  332. if(npc->GetAlignment() == 1) // Good
  333. random = races.GetRaceNameGood();
  334. else if(npc->GetAlignment() < 0) // Evil
  335. random = races.GetRaceNameEvil();
  336. else // All
  337. random = MakeRandomInt(0, 20);
  338. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Race: %s (%i)", races.GetRaceNameCase(random), random);
  339. npc->SetRace(random);
  340. }
  341. switch (npc->GetRace())
  342. {
  343. case BARBARIAN:
  344. // JA: If randomize has "4" (model) and race=0, barbarians show up in place of the real models.
  345. // Started working on a solution (CheckSameAppearance) but cannot get it to work yet.
  346. // Solution for now is to not randomize models for race=0. Set to race=255, or turn off model randomize
  347. race_string = "/barbarian/barbarian";
  348. break;
  349. case DARK_ELF:
  350. race_string = "/darkelf/darkelf";
  351. break;
  352. case DWARF:
  353. race_string = "/dwarf/dwarf";
  354. break;
  355. case ERUDITE:
  356. race_string = "/erudite/erudite";
  357. break;
  358. case FROGLOK:
  359. race_string = "/froglok/froglok";
  360. break;
  361. case GNOME:
  362. race_string = "/gnome/gnome";
  363. break;
  364. case HALF_ELF:
  365. race_string = "/halfelf/halfelf";
  366. break;
  367. case HALFLING:
  368. race_string = "/halfling/halfling";
  369. break;
  370. case HIGH_ELF:
  371. race_string = "/highelf/highelf";
  372. break;
  373. case HUMAN:
  374. race_string = "/human/human";
  375. break;
  376. case IKSAR:
  377. race_string = "/iksar/iksar";
  378. break;
  379. case KERRA:
  380. race_string = "/kerra/kerra";
  381. break;
  382. case OGRE:
  383. race_string = "/ogre/ogre";
  384. break;
  385. case RATONGA:
  386. race_string = "/ratonga/ratonga";
  387. break;
  388. case TROLL:
  389. race_string = "/troll/troll";
  390. break;
  391. case WOOD_ELF:
  392. race_string = "/woodelf/woodelf";
  393. break;
  394. case FAE:
  395. race_string = "/fae/fae_light";
  396. break;
  397. case ARASAI:
  398. race_string = "/fae/fae_dark";
  399. break;
  400. case SARNAK:
  401. gender == 1 ? race_string = "01/sarnak_male/sarnak" : race_string = "01/sarnak_female/sarnak";
  402. break;
  403. case VAMPIRE:
  404. race_string = "/vampire/vampire";
  405. break;
  406. case AERAKYN:
  407. race_string = "/aerakyn/aerakyn";
  408. break;
  409. }
  410. if (race_string.length() > 0)
  411. {
  412. string gender_string;
  413. gender == 1 ? gender_string = "male" : gender_string = "female";
  414. vector<int16>* id_list = database.GetAppearanceIDsLikeName("ec/pc" + race_string + "_" + gender_string);
  415. if (id_list)
  416. {
  417. int32 index = MakeRandomInt(0, id_list->size() - 1);
  418. npc->SetModelType(id_list->at(index));
  419. npc->SetSogaModelType(id_list->at(index));
  420. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Model Type: %i", npc->GetModelType());
  421. int16 wing_type = 0;
  422. if (npc->GetRace() == FAE)
  423. {
  424. vector<int16>* id_list_wings = database.GetAppearanceIDsLikeName("ec/pc/fae_wings/fae_wing");
  425. if (id_list_wings) {
  426. wing_type = id_list_wings->at(MakeRandomInt(0, id_list_wings->size() - 1));
  427. safe_delete(id_list_wings);
  428. }
  429. }
  430. else if (npc->GetRace() == ARASAI)
  431. {
  432. vector<int16>* id_list_wings = database.GetAppearanceIDsLikeName("ec/pc/fae_wings/fae_d_wing");
  433. if (id_list_wings) {
  434. wing_type = id_list_wings->at(MakeRandomInt(0, id_list_wings->size() - 1));
  435. safe_delete(id_list_wings);
  436. }
  437. }
  438. else if (npc->GetRace() == AERAKYN)
  439. {
  440. vector<int16>* id_list_wings = database.GetAppearanceIDsLikeName("ec/pc/aerakyn/aerakyn_male_wings");
  441. if (id_list_wings) {
  442. wing_type = id_list_wings->at(MakeRandomInt(0, id_list_wings->size() - 1));
  443. safe_delete(id_list_wings);
  444. }
  445. }
  446. if (wing_type > 0)
  447. {
  448. EQ2_Color color1;
  449. EQ2_Color color2;
  450. color1.red = MakeRandomInt(0, 255);
  451. color1.green = MakeRandomInt(0, 255);
  452. color1.blue = MakeRandomInt(0, 255);
  453. color2.red = MakeRandomInt(0, 255);
  454. color2.green = MakeRandomInt(0, 255);
  455. color2.blue = MakeRandomInt(0, 255);
  456. npc->SetWingColor1(color1);
  457. npc->SetWingColor2(color2);
  458. }
  459. npc->SetWingType(wing_type);
  460. safe_delete(id_list);
  461. }
  462. }
  463. }
  464. }
  465. if (flags & RANDOMIZE_FACIAL_HAIR_TYPE) {
  466. vector<int16>* id_list = database.GetAppearanceIDsLikeName("accessories/hair/face");
  467. if (id_list) {
  468. int32 index = MakeRandomInt(0, id_list->size() - 1);
  469. npc->SetFacialHairType(id_list->at(index));
  470. npc->SetSogaFacialHairType(id_list->at(index));
  471. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Facial Hair: %i", npc->GetFacialHairType());
  472. safe_delete(id_list);
  473. }
  474. }
  475. if (flags & RANDOMIZE_HAIR_TYPE) {
  476. vector<int16>* id_list = database.GetAppearanceIDsLikeName("accessories/hair/hair");
  477. if (id_list) {
  478. int32 index = MakeRandomInt(0, id_list->size() - 1);
  479. npc->SetHairType(id_list->at(index));
  480. npc->SetSogaHairType(id_list->at(index));
  481. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Hair: %i", npc->GetHairType());
  482. safe_delete(id_list);
  483. }
  484. }
  485. if (flags & RANDOMIZE_WING_TYPE) {
  486. int16 wing_type = 0;
  487. if (npc->GetRace() == FAE) {
  488. vector<int16>* id_list_wings = database.GetAppearanceIDsLikeName("ec/pc/fae_wings/fae_wing");
  489. if (id_list_wings) {
  490. wing_type = id_list_wings->at(MakeRandomInt(0, id_list_wings->size() - 1));
  491. safe_delete(id_list_wings);
  492. }
  493. }
  494. else if (npc->GetRace() == ARASAI) {
  495. vector<int16>* id_list_wings = database.GetAppearanceIDsLikeName("ec/pc/fae_wings/fae_d_wing");
  496. if (id_list_wings) {
  497. wing_type = id_list_wings->at(MakeRandomInt(0, id_list_wings->size() - 1));
  498. safe_delete(id_list_wings);
  499. }
  500. }
  501. else if (npc->GetRace() == AERAKYN)
  502. {
  503. vector<int16>* id_list_wings = database.GetAppearanceIDsLikeName("ec/pc/aerakyn/aerakyn_male_wings");
  504. if (id_list_wings) {
  505. wing_type = id_list_wings->at(MakeRandomInt(0, id_list_wings->size() - 1));
  506. safe_delete(id_list_wings);
  507. }
  508. }
  509. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Wing Type: %i", wing_type);
  510. npc->SetWingType(wing_type);
  511. }
  512. if (flags & RANDOMIZE_CHEEK_TYPE) {
  513. for(int i=0;i<3;i++) {
  514. random = MakeRandomFloat(-100, 100);
  515. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Cheek[%i]: %i", i, random);
  516. npc->features.cheek_type[i] = random;
  517. }
  518. }
  519. if (flags & RANDOMIZE_CHIN_TYPE) {
  520. for(int i=0;i<3;i++) {
  521. random = MakeRandomFloat(-100, 100);
  522. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Chin[%i]: %i", i, random);
  523. npc->features.chin_type[i] = MakeRandomFloat(-100, 100);
  524. }
  525. }
  526. if (flags & RANDOMIZE_EAR_TYPE) {
  527. for(int i=0;i<3;i++) {
  528. random = MakeRandomFloat(-100, 100);
  529. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Ear[%i]: %i", i, random);
  530. npc->features.ear_type[i] = MakeRandomFloat(-100, 100);
  531. }
  532. }
  533. if (flags & RANDOMIZE_EYE_BROW_TYPE) {
  534. for(int i=0;i<3;i++) {
  535. random = MakeRandomFloat(-100, 100);
  536. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Eyebrow[%i]: %i", i, random);
  537. npc->features.eye_brow_type[i] = MakeRandomFloat(-100, 100);
  538. }
  539. }
  540. if (flags & RANDOMIZE_EYE_TYPE) {
  541. for(int i=0;i<3;i++) {
  542. random = MakeRandomFloat(-100, 100);
  543. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Eye[%i]: %i", i, random);
  544. npc->features.eye_type[i] = MakeRandomFloat(-100, 100);
  545. }
  546. }
  547. if (flags & RANDOMIZE_LIP_TYPE) {
  548. for(int i=0;i<3;i++) {
  549. random = MakeRandomFloat(-100, 100);
  550. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Lip[%i]: %i", i, random);
  551. npc->features.lip_type[i] = MakeRandomFloat(-100, 100);
  552. }
  553. }
  554. if (flags & RANDOMIZE_NOSE_TYPE) {
  555. for(int i=0;i<3;i++) {
  556. random = MakeRandomFloat(-100, 100);
  557. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Nose[%i]: %i", i, random);
  558. npc->features.nose_type[i] = MakeRandomFloat(-100, 100);
  559. }
  560. }
  561. /* Randomize Colors */
  562. random = MakeRandomInt(0, 255);
  563. if(random > 30) {
  564. min_val = random - MakeRandomInt(0, 30);
  565. max_val = random + MakeRandomInt(0, 30);
  566. }
  567. if(max_val > 255)
  568. max_val = 255;
  569. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Color Ranges, random: %i, min: %i, max: %i", random, min_val, max_val);
  570. if (flags & RANDOMIZE_EYE_COLOR) {
  571. npc->features.eye_color.red = MakeRandomInt(min_val, max_val);
  572. npc->features.eye_color.green = MakeRandomInt(min_val, max_val);
  573. npc->features.eye_color.blue = MakeRandomInt(min_val, max_val);
  574. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Eye Color - R: %i, G: %i, B: %i", npc->features.eye_color.red, npc->features.eye_color.green, npc->features.eye_color.blue);
  575. }
  576. if (flags & RANDOMIZE_HAIR_COLOR1) {
  577. npc->features.hair_color1.red = MakeRandomInt(min_val, max_val);
  578. npc->features.hair_color1.green = MakeRandomInt(min_val, max_val);
  579. npc->features.hair_color1.blue = MakeRandomInt(min_val, max_val);
  580. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Hair Color 1 - R: %i, G: %i, B: %i", npc->features.hair_color1.red, npc->features.hair_color1.green, npc->features.hair_color1.blue);
  581. }
  582. if (flags & RANDOMIZE_HAIR_COLOR2) {
  583. npc->features.hair_color2.red = MakeRandomInt(min_val, max_val);
  584. npc->features.hair_color2.green = MakeRandomInt(min_val, max_val);
  585. npc->features.hair_color2.blue = MakeRandomInt(min_val, max_val);
  586. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Hair Color 2 - R: %i, G: %i, B: %i", npc->features.hair_color2.red, npc->features.hair_color2.green, npc->features.hair_color2.blue);
  587. }
  588. if (flags & RANDOMIZE_HAIR_HIGHLIGHT) {
  589. npc->features.hair_highlight_color.red = MakeRandomInt(min_val, max_val);
  590. npc->features.hair_highlight_color.green = MakeRandomInt(min_val, max_val);
  591. npc->features.hair_highlight_color.blue = MakeRandomInt(min_val, max_val);
  592. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Hair Highlight - R: %i, G: %i, B: %i", npc->features.hair_highlight_color.red, npc->features.hair_highlight_color.green, npc->features.hair_highlight_color.blue);
  593. }
  594. if (flags & RANDOMIZE_HAIR_FACE_COLOR) {
  595. EQ2_Color color1;
  596. color1.red = MakeRandomInt(min_val, max_val);
  597. color1.green = MakeRandomInt(min_val, max_val);
  598. color1.blue = MakeRandomInt(min_val, max_val);
  599. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Facial Hair Color - R: %i, G: %i, B: %i", color1.red, color1.green, color1.blue);
  600. npc->SetFacialHairColor(color1);
  601. }
  602. if (flags & RANDOMIZE_HAIR_FACE_HIGHLIGHT_COLOR) {
  603. EQ2_Color color1;
  604. color1.red = MakeRandomInt(min_val, max_val);
  605. color1.green = MakeRandomInt(min_val, max_val);
  606. color1.blue = MakeRandomInt(min_val, max_val);
  607. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Facial Hair Highlight - R: %i, G: %i, B: %i", color1.red, color1.green, color1.blue);
  608. npc->SetFacialHairHighlightColor(color1);
  609. }
  610. if (flags & RANDOMIZE_HAIR_TYPE_COLOR) {
  611. EQ2_Color color1;
  612. color1.red = MakeRandomInt(min_val, max_val);
  613. color1.green = MakeRandomInt(min_val, max_val);
  614. color1.blue = MakeRandomInt(min_val, max_val);
  615. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Hair Type Color - R: %i, G: %i, B: %i", color1.red, color1.green, color1.blue);
  616. npc->SetHairColor(color1);
  617. }
  618. if (flags & RANDOMIZE_HAIR_TYPE_HIGHLIGHT_COLOR) {
  619. EQ2_Color color1;
  620. color1.red = MakeRandomInt(min_val, max_val);
  621. color1.green = MakeRandomInt(min_val, max_val);
  622. color1.blue = MakeRandomInt(min_val, max_val);
  623. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Hair Type Highlight - R: %i, G: %i, B: %i", color1.red, color1.green, color1.blue);
  624. npc->SetHairTypeHighlightColor(color1);
  625. }
  626. if (flags & RANDOMIZE_SKIN_COLOR) {
  627. npc->features.skin_color.red = MakeRandomInt(min_val, max_val);
  628. npc->features.skin_color.green = MakeRandomInt(min_val, max_val);
  629. npc->features.skin_color.blue = MakeRandomInt(min_val, max_val);
  630. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Skin Color - R: %i, G: %i, B: %i", npc->features.eye_color.red, npc->features.eye_color.green, npc->features.eye_color.blue);
  631. }
  632. if (flags & RANDOMIZE_WING_COLOR1) {
  633. EQ2_Color color1;
  634. color1.red = MakeRandomInt(min_val, max_val);
  635. color1.green = MakeRandomInt(min_val, max_val);
  636. color1.blue = MakeRandomInt(min_val, max_val);
  637. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Wing Color 1 - R: %i, G: %i, B: %i", color1.red, color1.green, color1.blue);
  638. npc->SetWingColor1(color1);
  639. }
  640. if (flags & RANDOMIZE_WING_COLOR2) {
  641. EQ2_Color color1;
  642. color1.red = MakeRandomInt(min_val, max_val);
  643. color1.green = MakeRandomInt(min_val, max_val);
  644. color1.blue = MakeRandomInt(min_val, max_val);
  645. LogWrite(NPC__DEBUG, 5, "NPCs", "Randomizing Wing Color 2 - R: %i, G: %i, B: %i", color1.red, color1.green, color1.blue);
  646. npc->SetWingColor2(color1);
  647. }
  648. }
  649. Skill* NPC::GetSkillByName(const char* name, bool check_update){
  650. if(skills && skills->count(name) > 0){
  651. Skill* ret = (*skills)[name];
  652. if(ret && check_update && ret->current_val < ret->max_val && (rand()%100) >= 90)
  653. ret->current_val++;
  654. return ret;
  655. }
  656. return 0;
  657. }
  658. Skill* NPC::GetSkillByID(int32 id, bool check_update){
  659. Skill* skill = master_skill_list.GetSkill(id);
  660. if(skill && skills && skills->count(skill->name.data) > 0){
  661. Skill* ret = (*skills)[skill->name.data];
  662. if(ret && check_update && ret->current_val < ret->max_val && (rand()%100) >= 90)
  663. ret->current_val++;
  664. return ret;
  665. }
  666. return 0;
  667. }
  668. void NPC::SetAttackType(int8 type){
  669. attack_type = type;
  670. }
  671. int8 NPC::GetAttackType(){
  672. return attack_type;
  673. }
  674. void NPC::SetAIStrategy(int8 strategy){
  675. ai_strategy = strategy;
  676. }
  677. int8 NPC::GetAIStrategy(){
  678. return ai_strategy;
  679. }
  680. void NPC::SetPrimarySpellList(int32 id){
  681. primary_spell_list = id;
  682. }
  683. int32 NPC::GetPrimarySpellList(){
  684. return primary_spell_list;
  685. }
  686. void NPC::SetSecondarySpellList(int32 id){
  687. secondary_spell_list = id;
  688. }
  689. int32 NPC::GetSecondarySpellList(){
  690. return secondary_spell_list;
  691. }
  692. void NPC::SetPrimarySkillList(int32 id){
  693. primary_skill_list = id;
  694. }
  695. int32 NPC::GetPrimarySkillList(){
  696. return primary_skill_list;
  697. }
  698. void NPC::SetSecondarySkillList(int32 id){
  699. secondary_skill_list = id;
  700. }
  701. int32 NPC::GetSecondarySkillList(){
  702. return secondary_skill_list;
  703. }
  704. void NPC::SetEquipmentListID(int32 id){
  705. equipment_list_id = id;
  706. }
  707. int32 NPC::GetEquipmentListID(){
  708. return equipment_list_id;
  709. }
  710. Spell* NPC::GetNextSpell(float distance){
  711. int8 val = rand()%100;
  712. if(ai_strategy == AI_STRATEGY_OFFENSIVE){
  713. if(val >= 20)//80% chance to cast offensive spell if Offensive AI
  714. return GetNextSpell(distance, AI_STRATEGY_OFFENSIVE);
  715. return GetNextSpell(distance, AI_STRATEGY_DEFENSIVE);
  716. }
  717. else if(ai_strategy == AI_STRATEGY_DEFENSIVE){
  718. if(val >= 20)//80% chance to cast defensive spell if Defensive AI
  719. return GetNextSpell(distance, AI_STRATEGY_DEFENSIVE);
  720. return GetNextSpell(distance, AI_STRATEGY_OFFENSIVE);
  721. }
  722. return GetNextSpell(distance, AI_STRATEGY_BALANCED);
  723. }
  724. Spell* NPC::GetNextSpell(float distance, int8 type){
  725. Spell* ret = 0;
  726. if(spells){
  727. if(distance < 0)
  728. distance = 0;
  729. Spell* tmpSpell = 0;
  730. vector<Spell*>::iterator itr;
  731. for(itr = spells->begin(); itr != spells->end(); itr++){
  732. tmpSpell = *itr;
  733. if(!tmpSpell || (type == AI_STRATEGY_OFFENSIVE && tmpSpell->GetSpellData()->friendly_spell > 0))
  734. continue;
  735. if (tmpSpell->GetSpellData()->cast_type == SPELL_CAST_TYPE_TOGGLE)
  736. continue;
  737. if(type == AI_STRATEGY_DEFENSIVE && tmpSpell->GetSpellData()->friendly_spell == 0)
  738. continue;
  739. if(distance <= tmpSpell->GetSpellData()->range && distance >= tmpSpell->GetSpellData()->min_range && GetPower() >= tmpSpell->GetPowerRequired(this)){
  740. ret = tmpSpell;
  741. if((rand()%100) >= 70) //30% chance to stop after finding the first match, this will give the appearance of the NPC randomly choosing a spell to cast
  742. break;
  743. }
  744. }
  745. if(!ret && type != AI_STRATEGY_BALANCED)
  746. ret = GetNextSpell(distance, AI_STRATEGY_BALANCED); //wasnt able to find a valid match, so find any spell that the NPC has
  747. }
  748. return ret;
  749. }
  750. Spell* NPC::GetNextBuffSpell() {
  751. Spell* ret = 0;
  752. if (spells && GetZone()->GetSpellProcess()) {
  753. Spell* tmpSpell = 0;
  754. vector<Spell*>::iterator itr;
  755. for (itr = spells->begin(); itr != spells->end(); itr++) {
  756. tmpSpell = *itr;
  757. if (tmpSpell && tmpSpell->GetSpellData() && tmpSpell->GetSpellData()->cast_type == SPELL_CAST_TYPE_TOGGLE) {
  758. SpellEffects* effect = GetSpellEffect(tmpSpell->GetSpellID());
  759. if (effect) {
  760. if (effect->tier < tmpSpell->GetSpellTier()) {
  761. ret = tmpSpell;
  762. break;
  763. }
  764. }
  765. else {
  766. ret = tmpSpell;
  767. break;
  768. }
  769. }
  770. }
  771. }
  772. return ret;
  773. }
  774. void NPC::SetAggroRadius(float radius, bool overrideBaseValue){
  775. if (base_aggro_radius == 0.0f || overrideBaseValue)
  776. base_aggro_radius = radius;
  777. aggro_radius = radius;
  778. }
  779. float NPC::GetAggroRadius(){
  780. return aggro_radius;
  781. }
  782. void NPC::SetCastPercentage(int8 percentage){
  783. cast_percentage = percentage;
  784. }
  785. int8 NPC::GetCastPercentage(){
  786. return cast_percentage;
  787. }
  788. void NPC::AddSkillBonus(int32 spell_id, int32 skill_id, float value) {
  789. if (value != 0) {
  790. SkillBonus* sb;
  791. if (skill_bonus_list.count(spell_id) == 0) {
  792. sb = new SkillBonus;
  793. sb->spell_id = spell_id;
  794. skill_bonus_list.Put(spell_id, sb);
  795. }
  796. else
  797. sb = skill_bonus_list.Get(spell_id);
  798. if (sb->skills[skill_id] == 0) {
  799. SkillBonusValue* sbv = new SkillBonusValue;
  800. sbv->skill_id = skill_id;
  801. sbv->value = value;
  802. sb->skills[skill_id] = sbv;
  803. if (skills) {
  804. map<string, Skill*>::iterator itr;
  805. for (itr = skills->begin(); itr != skills->end(); itr++) {
  806. Skill* skill = itr->second;
  807. if (skill->skill_id == sbv->skill_id) {
  808. skill->current_val += (int16)sbv->value;
  809. skill->max_val += (int16)sbv->value;
  810. }
  811. }
  812. }
  813. }
  814. }
  815. }
  816. void NPC::RemoveSkillBonus(int32 spell_id) {
  817. if (skill_bonus_list.count(spell_id) > 0) {
  818. SkillBonus* sb = skill_bonus_list.Get(spell_id);
  819. skill_bonus_list.erase(spell_id);
  820. map<int32, SkillBonusValue*>::iterator itr;
  821. for (itr = sb->skills.begin(); itr != sb->skills.end(); itr++) {
  822. SkillBonusValue* sbv = itr->second;
  823. if (skills) {
  824. map<string, Skill*>::iterator skill_itr;
  825. for (skill_itr = skills->begin(); skill_itr != skills->end(); skill_itr++) {
  826. Skill* skill = skill_itr->second;
  827. if (sbv->skill_id == skill->skill_id) {
  828. skill->current_val -= (int16)sbv->value;
  829. skill->max_val -= (int16)sbv->value;
  830. }
  831. }
  832. }
  833. safe_delete(sbv);
  834. }
  835. safe_delete(sb);
  836. }
  837. }
  838. void NPC::SetBrain(::Brain* brain) {
  839. // Again, had to use the '::' to refer to the Brain class and not the function defined in the NPC class
  840. MBrain.writelock(__FUNCTION__, __LINE__);
  841. // Check to make sure the NPC the brain controls matches this npc
  842. if (brain && brain->GetBody() != this) {
  843. LogWrite(NPC_AI__ERROR, 0, "NPC_AI", "Brain body does not match the npc we tried to assign the brain to.");
  844. MBrain.releasewritelock(__FUNCTION__, __LINE__);
  845. return;
  846. }
  847. // Store the old brain in a temp pointer so we can delete it later
  848. ::Brain* old_brain = m_brain;
  849. // Set the brain for this NPC to the new brain
  850. m_brain = brain;
  851. // Release the lock
  852. MBrain.releasewritelock(__FUNCTION__, __LINE__);
  853. // Delete the old brain
  854. safe_delete(old_brain);
  855. }
  856. void NPC::SetZone(ZoneServer* in_zone, int32 version) {
  857. Spawn::SetZone(in_zone, version);
  858. if (in_zone){
  859. GetZone()->SetNPCEquipment(this);
  860. SetSkills(GetZone()->GetNPCSkills(primary_skill_list, secondary_skill_list));
  861. SetSpells(GetZone()->GetNPCSpells(primary_spell_list, secondary_spell_list));
  862. }
  863. }