NPC.cpp 33 KB

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