AI.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /// <reference path="Rulebook.ts" />
  2. /// <reference path="Rule.ts" />
  3. /// <reference path="../EveryTurn.ts" />
  4. /// <reference path="../../Functions.ts" />
  5. interface AIOptions {
  6. actor : Thing;
  7. wanderer? : boolean;
  8. wandersOn? : Region;
  9. picksShinies? : boolean;
  10. grudgeRate? : number;
  11. hostileThreshold? : number;
  12. coolOffRate? : number;
  13. retaliates? : boolean;
  14. }
  15. interface DialogueHook {
  16. text : Say;
  17. tree : DialogueTree;
  18. }
  19. interface TalkingHeads {
  20. greeter : Person;
  21. answerer : Person;
  22. options : Array<DialogueHook>;
  23. runFirst : Array<DialogueTree>;
  24. runAndStop : Array<DialogueTree>;
  25. }
  26. class AI {
  27. public actor : Person;
  28. public wanderer = true;
  29. public wandersOn : Region;
  30. public wanderChance = 50;
  31. public picksShinies = true;
  32. public hostilityTargets : Array<Thing> = [];
  33. public hostilityLevels : Array<number> = [];
  34. public hostileTargets : Array<Thing> = []; // Cache hostilities over 100 to reduce processing usage.
  35. public grudgeRate : number = 50; // Multiplies aggression level to determine anger.
  36. public retaliates = false;
  37. public warnedTimes = 0;
  38. public hostileThreshold : number = 100; // Amount of anger at which it becomes entirely hostile
  39. public coolOffRate : number = 5; // Amount, per turn, that rage subdues
  40. public noticed : Array<Thing> = [];
  41. public newNoticed : Array<Thing> = [];
  42. public newArrivals : Array<Thing> = [];
  43. public oldLeavers : Array<Thing> = [];
  44. public static rules = new Rulebook<Thing>("Default AI Rules");
  45. public extraRules : Array<Rulebook<Thing>> = [];
  46. public static combatRules = new Rulebook<Thing>("Default AI Combat Rules");
  47. public extraCombatRules : Array<Rulebook<Thing>> = [];
  48. public static talktoRules = new Rulebook<TalkingHeads>("Default Talk To Rules");
  49. public extraTalktoRules : Array<Rulebook<TalkingHeads>> = [];
  50. public static investigateRules = new Rulebook<TalkingHeads>("Default Ask About Rules");
  51. public extraInvestigateRules : Array<Rulebook<TalkingHeads>> = [];
  52. public static reacttoRules = new Rulebook<Thing>("Default React To Rules");
  53. public extraReacttoRules : Array<Rulebook<Thing>> = [];
  54. public reactingTo : Action;
  55. public storedReaction : Action;
  56. public constructor (options : AIOptions) {
  57. for (let key in options) {
  58. this[key] = options[key];
  59. }
  60. }
  61. /**
  62. * Executing an AI returns an Action. DOESN'T execute the action, just finds it!
  63. * @returns {Promise<Action>}
  64. */
  65. public async execute () : Promise<Action> {
  66. let promise : Promise<Action>;
  67. let inCombat = false;
  68. if (this.storedReaction != undefined) {
  69. let result = this.storedReaction;
  70. this.storedReaction = undefined;
  71. return result;
  72. }
  73. if (this.hostileTargets.length > 0) {
  74. for (let i = this.hostileTargets.length - 1; i >= 0; i--) {
  75. if (this.hostileTargets[i].isVisibleTo(this.actor)) {
  76. inCombat = true;
  77. break;
  78. }
  79. }
  80. } else if (this.actor.reputation < -9) {
  81. this.hostileTargets.push(WorldState.player);
  82. inCombat = WorldState.player.isVisibleTo(this.actor);
  83. }
  84. let result : Action;
  85. if (inCombat) {
  86. // TUNNEL VISION
  87. promise = AI.combatRules.execute({
  88. noun : this.actor
  89. }, ...this.extraCombatRules);
  90. result = await promise;
  91. if (result != undefined) {
  92. return result;
  93. }
  94. // No action was found?
  95. }
  96. this.renoticeThings();
  97. promise = AI.rules.execute({
  98. noun : this.actor
  99. }, ...this.extraRules);
  100. result = await promise;
  101. this.coolOff();
  102. return result;
  103. }
  104. public renoticeThings () {
  105. this.noticed = this.newNoticed;
  106. this.newNoticed = [];
  107. let stuff = this.actor.getRoom().getContainedAndVisibleTo(this.actor);
  108. for (let i = stuff.length - 1; i >= 0; i--) {
  109. this.newNoticed.push(stuff[i]);
  110. }
  111. this.newNoticed = this.newNoticed.filter( ( el ) => !this.noticed.includes( el ) );
  112. this.oldLeavers = this.noticed.filter( ( el ) => !this.newNoticed.includes( el ) );
  113. }
  114. public addRulesBook (...books : Array<Rulebook<Thing>>) {
  115. this.extraRules.push(...books)
  116. arrayUnique(this.extraRules);
  117. }
  118. public addCombatRulesBook (...books : Array<Rulebook<Thing>>) {
  119. this.extraCombatRules.push(...books)
  120. arrayUnique(this.extraCombatRules);
  121. }
  122. public addHostility (target : Thing, amount : number) {
  123. amount = amount * this.grudgeRate;
  124. let index = this.hostilityTargets.indexOf(target);
  125. if (index == -1) {
  126. index = this.hostilityTargets.push(target);
  127. this.hostilityLevels.push(amount);
  128. } else {
  129. this.hostilityLevels[index] += amount;
  130. }
  131. if (this.hostilityLevels[index] >= this.hostileThreshold && !this.hostileTargets.includes(target)) {
  132. this.hostileTargets.push(target);
  133. } else if (this.hostilityLevels[index] <= 0) {
  134. this.removeHostility(index, target);
  135. }
  136. }
  137. public removeHostility (index, target) {
  138. let hostile = this.hostileTargets.indexOf(target);
  139. if (hostile != -1) {
  140. this.hostileTargets.splice(hostile, 1);
  141. }
  142. this.hostilityTargets.splice(index, 1);
  143. this.hostilityLevels.splice(index, 1);
  144. }
  145. public getHostilityTo (target : Thing) {
  146. let index = this.hostilityTargets.indexOf(target);
  147. if (index != -1) {
  148. return this.hostilityLevels[index];
  149. } else {
  150. return 0;
  151. }
  152. }
  153. // TODO: Make this a rulebook.
  154. public coolOff () {
  155. for (let i = this.hostilityTargets.length - 1; i >= 0; i--) {
  156. this.hostilityLevels[i] -= this.coolOffRate;
  157. if (this.hostilityLevels[i] <= 0) {
  158. this.removeHostility(i, this.hostilityTargets[i]);
  159. }
  160. if (this.hostilityLevels[i] < this.hostileThreshold) {
  161. let hostile = this.hostileTargets.indexOf(this.hostilityTargets[i]);
  162. if (hostile != -1) {
  163. this.hostileTargets.splice(hostile, 1);
  164. }
  165. }
  166. }
  167. }
  168. public getPoked (action : Action) {
  169. this.reactingTo = action;
  170. if (this.actor instanceof Person) {
  171. //AIRules.getPoked(this.actor, action);
  172. return AI.reacttoRules.execute({
  173. noun : this.actor
  174. }, ...this.extraReacttoRules);
  175. }
  176. }
  177. public answerTo (noun : TalkingHeads) {
  178. return AI.talktoRules.execute({
  179. noun : noun
  180. }, ...this.extraTalktoRules);
  181. }
  182. public interrogateTo (noun : TalkingHeads) {
  183. return AI.investigateRules.execute({
  184. noun : noun
  185. }, ...this.extraInvestigateRules);
  186. }
  187. }
  188. module AIRules {
  189. /**
  190. * This is or behavioral rules regarding something that is happening RIGHT NOW.
  191. * i.e. Rule for what a monster does when the player has just insulted them, or for when the player triggers an alarm, etc.
  192. * @type {number}
  193. */
  194. export var PRIORITY_ACTING_ON_SITUATION = 5;
  195. /**
  196. * This is for behavioral rules about what the NPC SEES.
  197. * i.e. Is there a shiny on the ground for me to take? Do I see the player and if so how do I feel about it?
  198. * @type {number}
  199. */
  200. export var PRIORITY_ACTING_ON_PLACE = 3;
  201. /**
  202. * This is for rules for when the NPC has nothing better to do.
  203. * i.e. Standard guarding routes, etc.
  204. * @type {number}
  205. */
  206. export var PRIORITY_ACTING_ON_IDLE = 1;
  207. }
  208. module AIDialogueRules {
  209. /**
  210. * This is for when the NPC must talk about something that is currently happening.
  211. * For "Talk To", this means that whatever is happening is much more important than whatever the player might want to talk about.
  212. * Like if the player is currently fighting the NPC, or the player has just killed the NPC's best friend and this is kind of more urgent.
  213. * @type {number}
  214. */
  215. export var PRIORITY_TALKING_ABOUT_SITUATION = 5;
  216. /**
  217. * This is for rules for when the NPC has nothing better to do.
  218. * For "Talk To", this is the default rule for when the player talks to the enemy. We'll probably only have one rule here for each npc type, possibly more than one only if the NPC's demeanor changes, idk.
  219. * @type {number}
  220. */
  221. export var PRIORITY_ACTING_ON_IDLE = 1;
  222. }