9
3

NPC.cpp 28 KB

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