123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- /// <reference path="Classes/Things/Person.ts" />
- /// <reference path="Classes/Things/MapNote.ts" />
- /// <reference path="Classes/Things/Humanoid/Humanoid.ts" />
- /// <reference path="Classes/Rulebook.ts" />
- /// <reference path="Classes/Rule.ts" />
- /// <reference path="Classes/Save/StoredVariable.ts" />
- /// <reference path="PlayBegins.ts" />
- module WorldState {
- var worldTurn : number = 0;
- var playerTurn : number = 0;
- export var player : Humanoid = new Humanoid({isMale : false, name : "Player", unique : true});
- player.description = Humanoid.getPlayerDescription;
- var memoryRooms = new StoredVariable<Array<string>>({
- id : "Remembered Rooms",
- value : []
- });
- let allRememberedRooms : Array<Room> = [];
- let rememberedRoomsForIntelligence = 2;
- let rememberedRoomsForSurvival = 1;
- export var RememberingRoomRulebook : Rulebook<Room> = new Rulebook("Remembering Room something");
- export function isTurnWaiting () {
- return worldTurn < playerTurn;
- }
- export function incrementWorldTurn () {
- worldTurn++;
- }
- export function incrementPlayerTurn () {
- playerTurn++;
- }
- export function getCurrentTurn () {
- return playerTurn;
- }
- export function setCurrentTurn (turn : number) {
- playerTurn = turn;
- worldTurn = turn;
- }
- export let RememberingRoomInList = RememberingRoomRulebook.createAndAddRule({
- name : "Is room listed in remembered rooms?",
- firstPriority : Rule.PRIORITY_LOWEST,
- priority : Rule.PRIORITY_LOW,
- code : runner => {
- if (allRememberedRooms.includes(runner.noun)) {
- return true;
- }
- }
- });
- export let RememberingRoomInMap = RememberingRoomRulebook.createAndAddRule({
- name : "Is room shown on a map?",
- firstPriority : Rule.PRIORITY_LOWEST,
- priority : Rule.PRIORITY_LOWEST,
- code : runner => {
- let maps = <Array<MapNote>> Thing.CarryRelation.getRightType(player, MapNote);
- for (let i = 0; i < maps.length; i++) {
- if (maps[i].contains(runner.noun)) {
- return true;
- }
- }
- }
- });
- export function isPlayer (thing : any) {
- return player === thing;
- }
- export async function isRoomRemembered (room : Room) : Promise<boolean> {
- if (room == WorldState.player.getRoom()) return true;
- let result = await RememberingRoomRulebook.execute({noun : room});
- return result == true; // can return "undefined"
- }
- export function getRememberedRooms () {
- let roomNames = [];
- allRememberedRooms.forEach(room => {
- roomNames.push(room.getName());
- });
- return roomNames;
- }
- export function getRememberedRoomsAsRooms () : Array<Room> {
- return [...allRememberedRooms];
- }
- export function getMaximumRememberedRooms () {
- let intRooms = player.getStat(Attributes.Intelligence) * rememberedRoomsForIntelligence;
- let skillRooms = player.getSkill(Skills.Survival) * rememberedRoomsForSurvival;
- return intRooms + skillRooms;
- }
- export function truncateRooms () {
- while (allRememberedRooms.length > getMaximumRememberedRooms()){
- allRememberedRooms.pop();
- }
- }
- export function rememberRoom (...rooms : Array<Room>) {
- rooms.forEach((value) => {
- if (value instanceof Room) {
- let idx = allRememberedRooms.indexOf(value);
- if (idx == -1) {
- allRememberedRooms.unshift(value);
- } else {
- allRememberedRooms.splice(idx, 1);
- allRememberedRooms.unshift(value);
- }
- value.visited = true;
- }
- });
- truncateRooms();
- }
- export function saveState () {
- let things = {
- carried: [],
- worn: [],
- wielded: []
- };
- return {
- Things : things
- };
- }
- export let CorrectRememberedRoomsRule = PlayBegins.rulebook.createAndAddRule({
- name : "Correct remembered rooms",
- firstPriority : Rule.PRIORITY_LOWEST,
- priority : Rule.PRIORITY_MEDIUM,
- code : () => {
- if (memoryRooms.value.length > 0 && allRememberedRooms.length == 0) { // loading a save
- memoryRooms.value.forEach(roomName => {
- let room = Room.getRoom(roomName);
- allRememberedRooms.push(room); // Already ordered
- });
- truncateRooms();
- }
- if (!isRoomRemembered(WorldState.player.getRoom())) {
- rememberRoom(WorldState.player.getRoom());
- }
- let cleanRoomNames = [];
- allRememberedRooms.forEach(room => {
- if (!room.fodder) {
- cleanRoomNames.push(room.getName());
- }
- });
- memoryRooms.value = cleanRoomNames;
- // let allRooms = rememberedRooms.value;
- // let currentRoom = WorldState.player.getRoom();
- // if (allRooms.length == 0) {
- // rememberedRooms.value = [currentRoom.getName()];
- // return;
- // }
- // let newRooms = [];
- //
- // let tentativeFodder = [];
- // function recursivelyAddRooms (room : Room) {
- // if (room instanceof RoomRandom) {
- // if (room.fodder && !tentativeFodder.includes(room)) {
- // tentativeFodder.push(room);
- // room.connections.forEach((newRoom) => {
- // recursivelyAddRooms(newRoom);
- // });
- // } else if (allRooms.indexOf(room.getName()) != -1 && newRooms.indexOf(room.getName()) == -1) {
- // tentativeFodder.forEach(fodder => {
- // rememberedFodder.push(fodder);
- // });
- // tentativeFodder = [];
- // newRooms.push(room.getName());
- // room.connections.forEach((newRoom) => {
- // recursivelyAddRooms(newRoom);
- // });
- // }
- // }
- // }
- //
- // recursivelyAddRooms(currentRoom);
- // rememberedRooms.value = newRooms;
- }
- })
- }
|