NPC.cpp 28 KB

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