Action.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. await result.execute();
  49. this.say.add(result.say);
  50. this.nouns = result.nouns;
  51. return;
  52. }
  53. /**
  54. * Carry Out
  55. */
  56. await Action.carry.execute({
  57. noun : this
  58. }, ...carryRulebooks);
  59. return this.say;
  60. }
  61. get actor(): Thing {
  62. return this._actor;
  63. }
  64. set actor(value: Thing) {
  65. this._actor = value;
  66. }
  67. public getNoun (n : number) : any {
  68. if (this.nouns.length > n) {
  69. return this.nouns[n];
  70. }
  71. return undefined;
  72. }
  73. public setNoun (n : number, noun : any) {
  74. while (this.nouns.length < n) {
  75. this.nouns.push(undefined);
  76. }
  77. this.nouns[n] = noun;
  78. }
  79. /**
  80. * Needs to return a string explaining what the player will do if he does this action.
  81. * For instance, ActionTaking should return something like return "take " + this.nouns[0].getName(),
  82. * which would read as "take thing".
  83. * remember that things implement PRINTABLE interface, so you can get their names.
  84. * @returns {string}
  85. */
  86. public getCommandText () {
  87. return "do";
  88. }
  89. /**
  90. * If an action is stopped, it means it failed so spectacularly that it didn't even begin.
  91. * Which means if the player is doing it, it'll not take a turn.
  92. */
  93. public stop () {
  94. this.requiresTurn = false;
  95. }
  96. }
  97. // Action.addCarryRule(new Rule({
  98. // name : "Testing say in actions rule",
  99. // priority : Rule.PRIORITY_LOWEST,
  100. // firstPriority : Rule.PRIORITY_LOWEST,
  101. // code : (rule, rulebook) => {
  102. // let action = <Action> rulebook.noun;
  103. // action.say.add("You do nothing all turn. What was the point, really?");
  104. // }
  105. // }))
  106. Action.check.addRule(
  107. new Rule({
  108. name : "Check any Action - Requires Noun",
  109. firstPriority : Rule.PRIORITY_HIGHEST,
  110. code : (rulebook : RulebookRunner<Action>) => {
  111. let action = <Action> rulebook.noun;
  112. if (action.getNoun(0) == undefined) {
  113. return false;
  114. }
  115. },
  116. conditions : runner => {
  117. return runner.noun.requiresNoun;
  118. }
  119. })
  120. );
  121. Action.check.addRule(
  122. new Rule({
  123. name : "Check any Action - Requires Visibility",
  124. code : (rulebook : RulebookRunner<Action>) => {
  125. let action = <Action> rulebook.noun;
  126. let actor = action.actor;
  127. if (!action.getNoun(0).isVisibleTo(actor)) {
  128. return false;
  129. }
  130. },
  131. conditions : runner => {
  132. return runner.noun.requiresVisibility;
  133. }
  134. })
  135. );