WorldState.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. }
  99. });
  100. truncateRooms();
  101. }
  102. export function saveState () {
  103. let things = {
  104. carried: [],
  105. worn: [],
  106. wielded: []
  107. };
  108. return {
  109. Things : things
  110. };
  111. }
  112. export let CorrectRememberedRoomsRule = PlayBegins.rulebook.createAndAddRule({
  113. name : "Correct remembered rooms",
  114. firstPriority : Rule.PRIORITY_LOWEST,
  115. priority : Rule.PRIORITY_MEDIUM,
  116. code : () => {
  117. if (memoryRooms.value.length > 0 && allRememberedRooms.length == 0) { // loading a save
  118. memoryRooms.value.forEach(roomName => {
  119. let room = Room.getRoom(roomName);
  120. allRememberedRooms.push(room); // Already ordered
  121. });
  122. truncateRooms();
  123. }
  124. if (!isRoomRemembered(WorldState.player.getRoom())) {
  125. rememberRoom(WorldState.player.getRoom());
  126. }
  127. let cleanRoomNames = [];
  128. allRememberedRooms.forEach(room => {
  129. if (!room.fodder) {
  130. cleanRoomNames.push(room.getName());
  131. }
  132. });
  133. memoryRooms.value = cleanRoomNames;
  134. // let allRooms = rememberedRooms.value;
  135. // let currentRoom = WorldState.player.getRoom();
  136. // if (allRooms.length == 0) {
  137. // rememberedRooms.value = [currentRoom.getName()];
  138. // return;
  139. // }
  140. // let newRooms = [];
  141. //
  142. // let tentativeFodder = [];
  143. // function recursivelyAddRooms (room : Room) {
  144. // if (room instanceof RoomRandom) {
  145. // if (room.fodder && !tentativeFodder.includes(room)) {
  146. // tentativeFodder.push(room);
  147. // room.connections.forEach((newRoom) => {
  148. // recursivelyAddRooms(newRoom);
  149. // });
  150. // } else if (allRooms.indexOf(room.getName()) != -1 && newRooms.indexOf(room.getName()) == -1) {
  151. // tentativeFodder.forEach(fodder => {
  152. // rememberedFodder.push(fodder);
  153. // });
  154. // tentativeFodder = [];
  155. // newRooms.push(room.getName());
  156. // room.connections.forEach((newRoom) => {
  157. // recursivelyAddRooms(newRoom);
  158. // });
  159. // }
  160. // }
  161. // }
  162. //
  163. // recursivelyAddRooms(currentRoom);
  164. // rememberedRooms.value = newRooms;
  165. }
  166. })
  167. }