Selaa lähdekoodia

Remember which rooms were visited forever
Correctly position stuff at the closest non-fodder room to avoid stuff being lost to time.

Reddo 5 vuotta sitten
vanhempi
sitoutus
d91f5b4e71
4 muutettua tiedostoa jossa 35 lisäystä ja 27 poistoa
  1. 1 0
      app/World/Classes/Room.ts
  2. 32 26
      app/World/Classes/Thing.ts
  3. 1 1
      app/World/PlayBegins.ts
  4. 1 0
      app/World/WorldState.ts

+ 1 - 0
app/World/Classes/Room.ts

@@ -53,6 +53,7 @@ class Room implements Printable {
     public connections : Array<Room>;
     public description : Say = new Say();
     public fodder : boolean;
+    public visited : boolean = false;
 
     public constructor (id? : string, fodder? : boolean) {
         this.name = id == undefined ? "Room" : id;

+ 32 - 26
app/World/Classes/Thing.ts

@@ -108,38 +108,44 @@ class Thing implements Printable {
 
         // TODO: Make sure you never bring back a fodder Room - these disappear between games and we don't want to lose Things in them.
         this.addGetAlterations((thing : Thing) => {
-            function getClosestRoom (currentRoom : RoomRandom, rooms : Array<RoomRandom>) {
-                if (currentRoom instanceof RoomRandom && rooms.length > 0) {
-                    rooms.sort((a : RoomRandom, b : RoomRandom) => {
-                        if (!(a instanceof RoomRandom)) return -1;
-                        if (!(b instanceof RoomRandom)) return 1;
-                        let da = a.getDistanceTo(<RoomRandom> currentRoom);
-                        let db = b.getDistanceTo(<RoomRandom> currentRoom);
-                        return da - db;
-                    });
-                    return {
-                        Location : rooms[0].getName()
-                    }
-                }
-            }
-
             if (Thing.EnclosedRelation.getLeft(thing) == thing.getRoom() && thing.getRoom() != undefined) {
                 if (thing.getRoom().fodder) {
                     if (thing.isPlayer()) {
                         // put at closest remembered room
-                        let rooms = WorldState.getRememberedRoomsAsRooms();
-                        let currentRoom = thing.getRoom();
-                        return getClosestRoom(<RoomRandom> currentRoom, <Array<RoomRandom>> rooms);
+                        let rooms = WorldState.getRememberedRoomsAsRooms().filter((room : Room) => { return !(room instanceof RoomRandomFodder);});
+                        let room : Room;
+                        if (rooms.length <= 0) {
+                            // Find Closest visited room
+                            rooms = Region.InRelation.getLeft(thing.getRoom()).getRooms().filter(room => {return room.visited && !room.fodder && room.placed;}).sort((rooma, roomb) => {
+                                if (rooma instanceof RoomRandom && roomb instanceof RoomRandom) {
+                                    let da = rooma.getDistanceTo(<RoomRandom> thing.getRoom());
+                                    let db = roomb.getDistanceTo(<RoomRandom> thing.getRoom());
+                                    return da - db;
+                                }
+                                return 0;
+                            });
+
+                        }
+                        if (rooms.length > 0) {
+                            room = rooms[0]; // freshest room
+                        } else {
+                            room = PlayBegins.startingRoom; // back to square one
+                        }
+                        return {
+                            Location : room.getName()
+                        }
                     } else {
                         // put at closest room
-                        let rooms = thing.getRoom().getConnectedRooms();
-                        let currentRoom = thing.getRoom();
-                        let foundRoom = getClosestRoom(<RoomRandom> currentRoom, <Array<RoomRandom>> rooms);
-                        if (foundRoom != undefined) {
-                            return foundRoom;
-                        } else {
-                            rooms = (<Region> Region.InRelation.getLeft(thing.getRoom())).getRooms();
-                            return getClosestRoom(<RoomRandom> currentRoom, <Array<RoomRandom>> rooms);
+                        let rooms = Region.InRelation.getLeft(thing.getRoom()).getRooms().filter(room => {return !room.fodder && room.placed;}).sort((rooma, roomb) => {
+                            if (rooma instanceof RoomRandom && roomb instanceof RoomRandom) {
+                                let da = rooma.getDistanceTo(<RoomRandom> thing.getRoom());
+                                let db = roomb.getDistanceTo(<RoomRandom> thing.getRoom());
+                                return da - db;
+                            }
+                            return 0;
+                        });
+                        return {
+                            Location : rooms[0].getName()
                         }
                     }
                 } else {

+ 1 - 1
app/World/PlayBegins.ts

@@ -4,7 +4,7 @@
 module PlayBegins {
     export var rulebook = new Rulebook<void>("Play Begins");
     export var LOAD_FAILED = false;
-    let startingRoom;
+    export let startingRoom;
 
     export function execute () {
         rulebook.execute({});

+ 1 - 0
app/World/WorldState.ts

@@ -115,6 +115,7 @@ module WorldState {
                     allRememberedRooms.splice(idx, 1);
                     allRememberedRooms.unshift(value);
                 }
+                value.visited = true;
             }
         });
         truncateRooms();