WorldState.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 rememberedRooms = new StoredVariable<Array<string>>({
  14. id : "Remembered Rooms",
  15. value : []
  16. });
  17. var rememberedFodder : 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 (rememberedRooms.value.indexOf(runner.noun.getName()) != -1 || rememberedFodder.indexOf(runner.noun) != -1) {
  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 async function isRoomRemembered (room : Room) : Promise<boolean> {
  61. let result = await RememberingRoomRulebook.execute({noun : room});
  62. return result == true; // can return "undefined"
  63. }
  64. export function getRememberedRooms () {
  65. return rememberedRooms.value;
  66. }
  67. export function getRememberedRoomsAsRooms () : Array<Room> {
  68. let rooms = [];
  69. rememberedRooms.value.forEach(roomName => {
  70. let room = Room.getRoom(roomName);
  71. if (room != undefined) rooms.push(room);
  72. });
  73. return rooms;
  74. }
  75. export function getMaximumRememberedRooms () {
  76. let intRooms = player.getStat(Attributes.Intelligence) * rememberedRoomsForIntelligence;
  77. let skillRooms = player.getSkill(Skills.Survival) * rememberedRoomsForSurvival;
  78. return intRooms + skillRooms;
  79. }
  80. export function truncateRooms () {
  81. while (rememberedRooms.value.length > getMaximumRememberedRooms()){
  82. rememberedRooms.value.pop();
  83. }
  84. }
  85. export function rememberRoom (...rooms : Array<Room>) {
  86. rooms.forEach((value) => {
  87. if (value instanceof Room) {
  88. if (value.fodder) {
  89. if (rememberedFodder.indexOf(value) == -1) {
  90. rememberedFodder.push(value);
  91. }
  92. } else {
  93. let idx = rememberedRooms.value.indexOf(value.getName());
  94. if (idx != -1) {
  95. rememberedRooms.value.splice(idx, 1);
  96. }
  97. rememberedRooms.value.unshift(value.getName());
  98. }
  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. let allRooms = rememberedRooms.value;
  119. let currentRoom = WorldState.player.getRoom();
  120. if (allRooms.length == 0) {
  121. rememberedRooms.value = [currentRoom.getName()];
  122. return;
  123. }
  124. let newRooms = [];
  125. function recursivelyAddRooms (room : Room) {
  126. if (room instanceof RoomRandom) {
  127. if (room.fodder) {
  128. rememberedFodder.push(room);
  129. room.connections.forEach((newRoom) => {
  130. recursivelyAddRooms(newRoom);
  131. });
  132. } else if (allRooms.indexOf(room.getName()) != -1 && newRooms.indexOf(room.getName()) == -1) {
  133. newRooms.push(room.getName());
  134. room.connections.forEach((newRoom) => {
  135. recursivelyAddRooms(newRoom);
  136. });
  137. }
  138. }
  139. }
  140. recursivelyAddRooms(currentRoom);
  141. rememberedRooms.value = newRooms;
  142. }
  143. })
  144. }