NPC.cpp 32 KB

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