From 9d61814eeb7873a1c21d0f521dcb3649f3b4baf8 Mon Sep 17 00:00:00 2001 From: troido Date: Wed, 1 Apr 2020 21:25:18 +0200 Subject: room unloading works now --- content/encyclopediae/default_encyclopedia.json | 4 ++-- src/componentwrapper.rs | 1 + src/main.rs | 1 + src/room.rs | 4 ++++ src/world.rs | 23 ++++++++++++++++++++++- todo.md | 4 +--- 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index 19d57d0..28d834d 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -202,7 +202,7 @@ "name": "seed", "components": [ ["Grow", { - "delay": ["int", 100], + "delay": ["int", 600], "target_time": ["arg", "target_time"], "into": ["template", "radishseedling"] }] @@ -218,7 +218,7 @@ "name": "seedling", "components": [ ["Grow", { - "delay": ["int", 200], + "delay": ["int", 600], "target_time": ["arg", "target_time"], "into": ["template", "radishplant"] }] diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 589da15..42eccff 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -196,6 +196,7 @@ components!( into, delay, target_time: if target_time == 0 { None } else { Some(Timestamp(target_time)) } + // please forgive me for using 0 as null }; Equipment () {panic!("equipment from parameters not implemented")}; CreationTime (time: Int) {CreationTime{time: Timestamp(time)}}; diff --git a/src/main.rs b/src/main.rs index 4732d77..68e0f50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,6 +125,7 @@ fn main() -> Result<()>{ world.update(); if count % 50 == 0 { world.save(); + world.unload_rooms(); } let messages = world.view(); for (player, mut message) in messages { diff --git a/src/room.rs b/src/room.rs index 9df131a..805f94e 100644 --- a/src/room.rs +++ b/src/room.rs @@ -228,6 +228,10 @@ impl <'a, 'b>Room<'a, 'b> { } } + pub fn has_players(&self) -> bool { + !self.world.read_component::().is_empty() + } + pub fn save_players(&self) -> HashMap { let mut states = HashMap::new(); let players = self.world.read_component::(); diff --git a/src/world.rs b/src/world.rs index ecdc540..50a3a4b 100644 --- a/src/world.rs +++ b/src/world.rs @@ -24,6 +24,7 @@ pub struct World<'a, 'b> { default_room: RoomId, players: HashMap, rooms: HashMap>, + room_age: HashMap, encyclopedia: Encyclopedia, time: Timestamp } @@ -38,11 +39,13 @@ impl <'a, 'b>World<'a, 'b> { default_room, encyclopedia: encyclopedia.clone(), players: HashMap::new(), - rooms: hashmap!(purgatory::purgatory_id() => purgatory::create_purgatory(encyclopedia)) + rooms: hashmap!(purgatory::purgatory_id() => purgatory::create_purgatory(encyclopedia)), + room_age: HashMap::new() } } fn get_room_mut(&mut self, id: &RoomId) -> Result<&mut Room<'a, 'b>> { + self.room_age.insert(id.clone(), 0); let result = self.get_room_mut_(id); if let Err(err) = &result { println!("Failed to load room {:?}: {:?}", id, err); @@ -169,6 +172,24 @@ impl <'a, 'b>World<'a, 'b> { } } + pub fn unload_rooms(&mut self){ + let mut to_remove = Vec::new(); + for roomid in self.rooms.keys() { + if self.rooms[roomid].has_players() { + self.room_age.insert(roomid.clone(), 0); + } else { + let age = *self.room_age.get(&roomid).unwrap_or(&0) + 1; + self.room_age.insert(roomid.clone(), age); + if age > 2 { + to_remove.push(roomid.clone()); + } + } + } + for roomid in to_remove { + self.rooms.remove(&roomid); + } + } + pub fn view(&self) -> HashMap { let mut views = HashMap::new(); for room in self.rooms.values() { diff --git a/todo.md b/todo.md index cee0b84..ec698be 100644 --- a/todo.md +++ b/todo.md @@ -2,11 +2,9 @@ # TODO - make readme -- save time -- timer resource +- timer resource? - log world events to player - draw new entities -- room unloading - relative room locations? - improve error handling - doors -- cgit