NPC.cpp 30 KB

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