9
3

NPC.cpp 30 KB

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