1
1

Action.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /// <reference path="Rulebook.ts" />
  2. /// <reference path="Rule.ts" />
  3. /// <reference path="../../Elements/Classes/Say.ts" />
  4. class Action {
  5. public static check = new Rulebook<Action>("Check any Action");
  6. public static carry = new Rulebook<Action>("Carry out any Action");
  7. public extraChecks : Array<Rulebook<Action>> = [];
  8. public extraCarries : Array<Rulebook<Action>> = [];
  9. public _actor : Thing;
  10. public nouns : Array<Thing> = [];
  11. public say : Say = new Say();
  12. public actingAgressively = false;
  13. public actingSubmissively = false;
  14. public requiresTurn = true;
  15. public requiresNoun = true;
  16. public requiresVisibility = true; // First noun must be visible and in the same room
  17. public constructor (actor : Thing, ...nouns : Array<any>) {
  18. this.actor = actor;
  19. nouns.forEach((value, index, array) => {
  20. this.setNoun(index, value);
  21. });
  22. }
  23. public async execute () : Promise<Say> {
  24. this.say = new Say();
  25. let checkRulebooks = [];
  26. let carryRulebooks = [];
  27. let cClass = this.constructor;
  28. while (cClass != Action) {
  29. if ((<typeof Action> cClass).check != undefined) {
  30. checkRulebooks.push((<typeof Action> cClass).check);
  31. }
  32. if ((<typeof Action> cClass).carry != undefined) {
  33. carryRulebooks.push((<typeof Action> cClass).carry);
  34. }
  35. cClass = Object.getPrototypeOf(cClass);
  36. }
  37. /**
  38. * Check if action goes through
  39. */
  40. let result = await Action.check.execute({
  41. noun : this
  42. }, ...checkRulebooks);
  43. // There are now multiple results! A false result means a fail Check! But it can also return a new action!
  44. if (result == false) {
  45. return;
  46. } else if(result instanceof Action) {
  47. console.debug(Rulebook.getIndentation() + "[ACTION] Instead of...");
  48. let originalNouns = this.nouns;
  49. await result.execute();
  50. this.say.add(result.say);
  51. this.nouns = result.nouns;
  52. // Reset to initial state
  53. this.nouns = originalNouns;
  54. return;
  55. }
  56. /**
  57. * Carry Out
  58. */
  59. await Action.carry.execute({
  60. noun : this
  61. }, ...carryRulebooks);
  62. return this.say;
  63. }
  64. get actor(): Thing {
  65. return this._actor;
  66. }
  67. set actor(value: Thing) {
  68. this._actor = value;
  69. }
  70. public getNoun (n : number) : any {
  71. if (this.nouns.length > n) {
  72. return this.nouns[n];
  73. }
  74. return undefined;
  75. }
  76. public setNoun (n : number, noun : any) {
  77. while (this.nouns.length < n) {
  78. this.nouns.push(undefined);
  79. }
  80. this.nouns[n] = noun;
  81. }
  82. /**
  83. * Needs to return a string explaining what the player will do if he does this action.
  84. * For instance, ActionTaking should return something like return "take " + this.nouns[0].getName(),
  85. * which would read as "take thing".
  86. * remember that things implement PRINTABLE interface, so you can get their names.
  87. * @returns {string}
  88. */
  89. public getCommandText () {
  90. return "do";
  91. }
  92. /**
  93. * If an action is stopped, it means it failed so spectacularly that it didn't even begin.
  94. * Which means if the player is doing it, it'll not take a turn.
  95. */
  96. public stop () {
  97. this.requiresTurn = false;
  98. }
  99. }
  100. // Action.addCarryRule(new Rule({
  101. // name : "Testing say in actions rule",
  102. // priority : Rule.PRIORITY_LOWEST,
  103. // firstPriority : Rule.PRIORITY_LOWEST,
  104. // code : (rule, rulebook) => {
  105. // let action = <Action> rulebook.noun;
  106. // action.say.add("You do nothing all turn. What was the point, really?");
  107. // }
  108. // }))
  109. Action.check.addRule(
  110. new Rule({
  111. name : "Check any Action - Requires Noun",
  112. firstPriority : Rule.PRIORITY_HIGHEST,
  113. code : (rulebook : RulebookRunner<Action>) => {
  114. let action = <Action> rulebook.noun;
  115. if (action.getNoun(0) == undefined) {
  116. return false;
  117. }
  118. },
  119. conditions : runner => {
  120. return runner.noun.requiresNoun;
  121. }
  122. })
  123. );
  124. Action.check.addRule(
  125. new Rule({
  126. name : "Check any Action - Requires Visibility",
  127. code : (rulebook : RulebookRunner<Action>) => {
  128. let action = <Action> rulebook.noun;
  129. let actor = action.actor;
  130. if (!action.getNoun(0).isVisibleTo(actor)) {
  131. return false;
  132. }
  133. },
  134. conditions : runner => {
  135. return runner.noun.requiresVisibility;
  136. }
  137. })
  138. );