1
1

ActionRetrace.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /// <reference path="../Action.ts" />
  2. /// <reference path="../Rule.ts" />
  3. /// <reference path="../Rulebook.ts" />
  4. class ActionRetrace extends Action {
  5. public static check = new Rulebook<ActionRetrace>("Check Retracing");
  6. public static carry = new Rulebook<ActionRetrace>("Carry out Retracing");
  7. public constructor (actor : Thing, ...nouns : Array<any>) {
  8. super(actor, ...nouns);
  9. this.requiresNoun = false;
  10. this.requiresVisibility = false;
  11. this.requiresTurn = false;
  12. }
  13. /**
  14. * Needs to return a string explaining what the player will do if he does this action.
  15. * For instance, ActionTaking should return something like return "take " + this.nouns[0].getName(),
  16. * which would read as "take thing".
  17. * remember that things implement PRINTABLE interface, so you can get their names.
  18. * @returns {string}
  19. */
  20. public getCommandText () {
  21. let name;
  22. if (typeof this.getNoun(0) == "number") {
  23. name = DirectionNames[Direction[this.getNoun(0)]];
  24. } else if (this.getNoun(0) instanceof Room) {
  25. name = (<Room> this.getNoun(0)).getPrintedName();
  26. }
  27. return "think about how to get to " + name;
  28. }
  29. }
  30. ActionRetrace.check.addRule(new Rule({
  31. firstPriority : Rule.PRIORITY_HIGHEST,
  32. priority : Rule.PRIORITY_HIGH,
  33. name : "Change Room to Direction",
  34. code : (rulebook : RulebookRunner<ActionRetrace>) => {
  35. let action = <ActionGo> rulebook.noun;
  36. // Someone asked for a room...
  37. if (action.getNoun(0) instanceof Room) {
  38. let actor = action.actor;
  39. let cRoom = actor.getRoom();
  40. if (cRoom == undefined) {
  41. return false;
  42. }
  43. let dRoom = action.getNoun(0);
  44. if (cRoom == dRoom) {
  45. if (actor.isPlayer()) {
  46. action.say.add("You are already there!");
  47. }
  48. action.stop();
  49. return false;
  50. }
  51. let code;
  52. if (actor == WorldState.player) {
  53. code = (room : Room) => {
  54. return WorldState.isRoomRemembered(room);
  55. }
  56. }
  57. let direction = cRoom.bestDirectionTo(dRoom, code);
  58. if (direction == undefined) {
  59. if (actor.isPlayer()) {
  60. action.say.add("You don't remember how to get there.");
  61. }
  62. return false;
  63. } else {
  64. action.setNoun(0, direction);
  65. }
  66. }
  67. }
  68. }));
  69. ActionRetrace.carry.addRule(new Rule({
  70. name : "Retracing - Find Direction",
  71. code : (rulebook : RulebookRunner<ActionRetrace>) => {
  72. let action = <ActionRetrace> rulebook.noun;
  73. action.say.add("To get there, you should go " + DirectionNames[Direction[action.getNoun(0)]] + ".");
  74. }
  75. }));