1
1

WorldState.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /// <reference path="Classes/Things/Person.ts" />
  2. /// <reference path="Classes/Things/MapNote.ts" />
  3. /// <reference path="Classes/Things/Humanoid/Humanoid.ts" />
  4. /// <reference path="Classes/Rulebook.ts" />
  5. /// <reference path="Classes/Rule.ts" />
  6. /// <reference path="Classes/Save/StoredVariable.ts" />
  7. /// <reference path="PlayBegins.ts" />
  8. module WorldState {
  9. var worldTurn : number = 0;
  10. var playerTurn : number = 0;
  11. export var player : Humanoid = new Humanoid({isMale : false});
  12. player.description = Humanoid.getPlayerDescription;
  13. var memoryRooms = new StoredVariable<Array<string>>({
  14. id : "Remembered Rooms",
  15. value : []
  16. });
  17. let allRememberedRooms : Array<Room> = [];
  18. let rememberedRoomsForIntelligence = 2;
  19. let rememberedRoomsForSurvival = 1;
  20. export var RememberingRoomRulebook : Rulebook<Room> = new Rulebook("Remembering Room something");
  21. export function isTurnWaiting () {
  22. return worldTurn < playerTurn;
  23. }
  24. export function incrementWorldTurn () {
  25. worldTurn++;
  26. }
  27. export function incrementPlayerTurn () {
  28. playerTurn++;
  29. }
  30. export function getCurrentTurn () {
  31. return playerTurn;
  32. }
  33. export function setCurrentTurn (turn : number) {
  34. playerTurn = turn;
  35. worldTurn = turn;
  36. }
  37. export let RememberingRoomInList = RememberingRoomRulebook.createAndAddRule({
  38. name : "Is room listed in remembered rooms?",
  39. firstPriority : Rule.PRIORITY_LOWEST,
  40. priority : Rule.PRIORITY_LOW,
  41. code : runner => {
  42. if (allRememberedRooms.includes(runner.noun)) {
  43. return true;
  44. }
  45. }
  46. });
  47. export let RememberingRoomInMap = RememberingRoomRulebook.createAndAddRule({
  48. name : "Is room shown on a map?",
  49. firstPriority : Rule.PRIORITY_LOWEST,
  50. priority : Rule.PRIORITY_LOWEST,
  51. code : runner => {
  52. let maps = <Array<MapNote>> Thing.CarryRelation.getRightType(player, MapNote);
  53. for (let i = 0; i < maps.length; i++) {
  54. if (maps[i].contains(runner.noun)) {
  55. return true;
  56. }
  57. }
  58. }
  59. });
  60. export function isPlayer (thing : any) {
  61. return player === thing;
  62. }
  63. export async function isRoomRemembered (room : Room) : Promise<boolean> {
  64. if (room == WorldState.player.getRoom()) return true;
  65. let result = await RememberingRoomRulebook.execute({noun : room});
  66. return result == true; // can return "undefined"
  67. }
  68. export function getRememberedRooms () {
  69. let roomNames = [];
  70. allRememberedRooms.forEach(room => {
  71. roomNames.push(room.getName());
  72. });
  73. return roomNames;
  74. }
  75. export function getRememberedRoomsAsRooms () : Array<Room> {
  76. return [...allRememberedRooms];
  77. }
  78. export function getMaximumRememberedRooms () {
  79. let intRooms = player.getStat(Attributes.Intelligence) * rememberedRoomsForIntelligence;
  80. let skillRooms = player.getSkill(Skills.Survival) * rememberedRoomsForSurvival;
  81. return intRooms + skillRooms;
  82. }
  83. export function truncateRooms () {
  84. while (allRememberedRooms.length > getMaximumRememberedRooms()){
  85. allRememberedRooms.pop();
  86. }
  87. }
  88. export function rememberRoom (...rooms : Array<Room>) {
  89. rooms.forEach((value) => {
  90. if (value instanceof Room) {
  91. let idx = allRememberedRooms.indexOf(value);
  92. if (idx == -1) {
  93. allRememberedRooms.unshift(value);
  94. } else {
  95. allRememberedRooms.splice(idx, 1);
  96. allRememberedRooms.unshift(value);
  97. }
  98. value.visited = true;
  99. }
  100. });
  101. truncateRooms();
  102. }
  103. export function saveState () {
  104. let things = {
  105. carried: [],
  106. worn: [],
  107. wielded: []
  108. };
  109. return {
  110. Things : things
  111. };
  112. }
  113. export let CorrectRememberedRoomsRule = PlayBegins.rulebook.createAndAddRule({
  114. name : "Correct remembered rooms",
  115. firstPriority : Rule.PRIORITY_LOWEST,
  116. priority : Rule.PRIORITY_MEDIUM,
  117. code : () => {
  118. if (memoryRooms.value.length > 0 && allRememberedRooms.length == 0) { // loading a save
  119. memoryRooms.value.forEach(roomName => {
  120. let room = Room.getRoom(roomName);
  121. allRememberedRooms.push(room); // Already ordered
  122. });
  123. truncateRooms();
  124. }
  125. if (!isRoomRemembered(WorldState.player.getRoom())) {
  126. rememberRoom(WorldState.player.getRoom());
  127. }
  128. let cleanRoomNames = [];
  129. allRememberedRooms.forEach(room => {
  130. if (!room.fodder) {
  131. cleanRoomNames.push(room.getName());
  132. }
  133. });
  134. memoryRooms.value = cleanRoomNames;
  135. // let allRooms = rememberedRooms.value;
  136. // let currentRoom = WorldState.player.getRoom();
  137. // if (allRooms.length == 0) {
  138. // rememberedRooms.value = [currentRoom.getName()];
  139. // return;
  140. // }
  141. // let newRooms = [];
  142. //
  143. // let tentativeFodder = [];
  144. // function recursivelyAddRooms (room : Room) {
  145. // if (room instanceof RoomRandom) {
  146. // if (room.fodder && !tentativeFodder.includes(room)) {
  147. // tentativeFodder.push(room);
  148. // room.connections.forEach((newRoom) => {
  149. // recursivelyAddRooms(newRoom);
  150. // });
  151. // } else if (allRooms.indexOf(room.getName()) != -1 && newRooms.indexOf(room.getName()) == -1) {
  152. // tentativeFodder.forEach(fodder => {
  153. // rememberedFodder.push(fodder);
  154. // });
  155. // tentativeFodder = [];
  156. // newRooms.push(room.getName());
  157. // room.connections.forEach((newRoom) => {
  158. // recursivelyAddRooms(newRoom);
  159. // });
  160. // }
  161. // }
  162. // }
  163. //
  164. // recursivelyAddRooms(currentRoom);
  165. // rememberedRooms.value = newRooms;
  166. }
  167. })
  168. }