AI.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. }
  11. class AI {
  12. public actor : Thing;
  13. public wanderer = true;
  14. public wandersOn : Region;
  15. public wanderChance = 50;
  16. public picksShinies = true;
  17. public constructor (options : AIOptions) {
  18. for (let key in options) {
  19. this[key] = options[key];
  20. }
  21. }
  22. /**
  23. * Executing an AI returns an Action. DOESN'T execute the action, just finds it!
  24. * @returns {Promise<Action>}
  25. */
  26. public async execute () : Promise<Action> {
  27. let promise : Promise<Action>;
  28. if (promise != undefined) {
  29. promise = AI.combatRules.execute({
  30. noun : this.actor
  31. }, ...this.extraCombatRules);
  32. } else {
  33. promise = AI.rules.execute({
  34. noun : this.actor
  35. }, ...this.extraRules);
  36. }
  37. let result : Action = await promise;
  38. return result;
  39. }
  40. public addRulesBook (...books : Array<Rulebook<Thing>>) {
  41. this.extraRules.push(...books)
  42. arrayUnique(this.extraRules);
  43. }
  44. public addCombatRulesBook (...books : Array<Rulebook<Thing>>) {
  45. this.extraCombatRules.push(...books)
  46. arrayUnique(this.extraCombatRules);
  47. }
  48. public static rules = new Rulebook<Thing>("Default AI Rules");
  49. public extraRules : Array<Rulebook<Thing>> = [];
  50. public static combatRules = new Rulebook<Thing>("Default AI Combat Rules");
  51. public extraCombatRules : Array<Rulebook<Thing>> = [];
  52. }
  53. module AIRules {
  54. /**
  55. * This is or behavioral rules regarding something that is happening RIGHT NOW.
  56. * i.e. Rule for what a monster does when the player has just insulted them, or for when the player triggers an alarm, etc.
  57. * @type {number}
  58. */
  59. export var PRIORITY_ACTING_ON_SITUATION = 5;
  60. /**
  61. * This is for behavioral rules about what the NPC SEES.
  62. * 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?
  63. * @type {number}
  64. */
  65. export var PRIORITY_ACTING_ON_PLACE = 3;
  66. /**
  67. * This is for rules for when the NPC has nothing better to do.
  68. * i.e. Standard guarding routes, etc.
  69. * @type {number}
  70. */
  71. export var PRIORITY_ACTING_ON_IDLE = 1;
  72. }