NPC.cpp 28 KB

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