NPC.cpp 28 KB

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