NPC.cpp 28 KB

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