NPC.cpp 32 KB

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