1
1

AI.ts 2.8 KB

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