ActionGo.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /// <reference path="../Action.ts" />
  2. /// <reference path="../Rule.ts" />
  3. /// <reference path="../Rulebook.ts" />
  4. class ActionGo extends Action {
  5. public static check = new Rulebook<ActionGo>("Check Going");
  6. public static carry = new Rulebook<ActionGo>("Carry out Going");
  7. public roomGoneFrom : Room;
  8. public roomGoneTo : Room;
  9. public originalTarget : Room | number;
  10. public constructor (actor : Thing, ...nouns : Array<any>) {
  11. super(actor, ...nouns);
  12. this.originalTarget = nouns[0];
  13. this.requiresNoun = false;
  14. this.requiresVisibility = false;
  15. }
  16. /**
  17. * Needs to return a string explaining what the player will do if he does this action.
  18. * For instance, ActionTaking should return something like return "take " + this.nouns[0].getName(),
  19. * which would read as "take thing".
  20. * remember that things implement PRINTABLE interface, so you can get their names.
  21. * @returns {string}
  22. */
  23. public getCommandText () {
  24. let name;
  25. if (typeof this.originalTarget == "number") {
  26. name = DirectionNames[Direction[this.originalTarget]];
  27. } else if (this.originalTarget instanceof Room) {
  28. name = "to " + (<Room> this.originalTarget).getPrintedName();
  29. }
  30. return "go " + name;
  31. }
  32. public static ruleCheckRestoreOriginalNoun = ActionGo.check.createAndAddRule({
  33. firstPriority : Rule.PRIORITY_HIGHEST,
  34. priority : Rule.PRIORITY_HIGHEST,
  35. name : "Check Going - Restore original noun",
  36. code : (rulebook) => {
  37. let action = rulebook.noun;
  38. action.setNoun(0, action.originalTarget);
  39. }
  40. });
  41. public static ruleCheckIsthereactor = ActionGo.check.createAndAddRule({
  42. firstPriority : Rule.PRIORITY_HIGHEST,
  43. priority : Rule.PRIORITY_HIGHEST,
  44. name : "Check Going - Is there an actor?",
  45. code : (rulebook) => {
  46. let action = <ActionGo> rulebook.noun;
  47. if (action.actor == undefined) {
  48. return false; // Rule fails
  49. }
  50. }
  51. });
  52. public static ruleCheckConvertRoomToDirection = ActionGo.check.createAndAddRule({
  53. firstPriority : Rule.PRIORITY_HIGHEST,
  54. priority : Rule.PRIORITY_HIGH,
  55. name : "Change Room to Direction",
  56. code : (rulebook) => {
  57. let action = <ActionGo> rulebook.noun;
  58. // Someone asked for a room...
  59. if (action.getNoun(0) instanceof RoomRandom) {
  60. let actor = action.actor;
  61. let cRoom = actor.getRoom();
  62. if (cRoom instanceof RoomRandom) {
  63. let dRoom = action.getNoun(0);
  64. if (cRoom == dRoom) {
  65. if (actor.isPlayer()) {
  66. action.say.add("You are already there!");
  67. }
  68. action.stop();
  69. return false;
  70. }
  71. let code;
  72. if (actor == WorldState.player) {
  73. code = (room: Room) => {
  74. return WorldState.isRoomRemembered(room);
  75. }
  76. }
  77. let direction = cRoom.getAStarBestDirectionTo(dRoom, code);
  78. if (direction == undefined) {
  79. if (actor.isPlayer()) {
  80. action.say.add("You don't remember how to get there.");
  81. }
  82. return false;
  83. } else {
  84. action.setNoun(0, direction);
  85. }
  86. }
  87. }
  88. }
  89. });
  90. public static ruleCheckIstheredirectionandroom = ActionGo.check.createAndAddRule({
  91. name : "Check Going - is there a direction? Does it lead anywhere?",
  92. code : (rulebook) => {
  93. let action = <ActionGo> rulebook.noun;
  94. let cRoom = action.actor.getRoom();
  95. if (cRoom == undefined) {
  96. return false; // Rule fails, actor is out of world
  97. }
  98. let direction = action.getNoun(0);
  99. if (direction == undefined) {
  100. return false; // Rule fails, no direction
  101. }
  102. let nextRoom = cRoom.connections[direction];
  103. if (nextRoom == undefined) {
  104. return false; // Rule fails, can't go direction that doesn't have a room
  105. }
  106. action.roomGoneFrom = action.actor.getRoom();
  107. action.roomGoneTo = nextRoom;
  108. }
  109. });
  110. public static ruleCarryMove = ActionGo.carry.createAndAddRule({
  111. name : "Going - Move Actor to Next Room",
  112. code : (rulebook) => {
  113. let action = <ActionGo> rulebook.noun;
  114. let roomGoneFrom = action.actor.getRoom();
  115. let direction = action.getNoun(0);
  116. let roomGoneInto = roomGoneFrom.connections[direction];
  117. roomGoneInto.place(action.actor);
  118. let actor = action.actor;
  119. if (actor == WorldState.player) {
  120. action.say.add("You go " + DirectionNames[Direction[direction]].toLowerCase() + ".");
  121. } else {
  122. if (roomGoneFrom == WorldState.player.getRoom()) {
  123. action.say.add(new SayThe(), actor, " goes " + DirectionNames[Direction[direction]].toLowerCase() + ".");
  124. } else {
  125. let oppositeName = DirectionNames[
  126. OppositeDirection[direction]
  127. ];
  128. action.say.add(new SayThe(), actor, " arrives from the " + oppositeName.toLowerCase() + ".");
  129. }
  130. }
  131. }
  132. });
  133. public static ruleCarryRememberRooms = ActionGo.carry.createAndAddRule({
  134. name : "Going - Remember the involved rooms",
  135. code : (rulebook) => {
  136. let action = <ActionGo> rulebook.noun;
  137. let actor = action.actor;
  138. let roomGoneInto = action.actor.getRoom();
  139. let direction = action.getNoun(0);
  140. let roomGoneFrom = roomGoneInto.connections[OppositeDirection[Direction[direction]]];
  141. if (actor.isPlayer()) {
  142. WorldState.rememberRoom(roomGoneFrom, roomGoneInto);
  143. }
  144. }
  145. });
  146. }