1
1

AI.ts 2.5 KB

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