tavern_keeper/src/state.rs

185 lines
5.3 KiB
Rust
Raw Normal View History

2022-12-31 07:54:18 +00:00
2022-12-31 14:22:44 +00:00
use std::collections::HashMap;
2022-12-31 07:54:18 +00:00
use std::rc::Rc;
use rand::prelude::*;
2023-01-04 13:37:25 +00:00
use crate::entity::{Location, EntityId};
2023-01-04 13:54:57 +00:00
use crate::creature::Creature;
2022-12-31 07:54:18 +00:00
use crate::time::Time;
2023-01-04 13:37:25 +00:00
use crate::world::{World, Terrain, Town, Tavern};
2022-12-31 13:03:24 +00:00
use crate::events::{FoundTown, WorldGenesis, PersonGenesis, TavernBuilt};
2022-12-30 20:30:12 +00:00
pub struct GameState {
2022-12-31 07:54:18 +00:00
pub time: Time,
2022-12-30 20:30:12 +00:00
pub world: World,
2023-01-04 13:37:25 +00:00
pub creatures: HashMap<u32, Creature>,
2022-12-31 13:03:24 +00:00
pub events: Vec<Box<Event>>,
pub tavern: Option<Rc<Tavern>>,
2022-12-31 07:54:18 +00:00
}
pub struct Event {
pub time: Time,
2023-01-04 13:37:25 +00:00
pub effect: Box<dyn Action>,
2022-12-31 07:54:18 +00:00
}
2023-01-04 13:37:25 +00:00
pub trait Action {
// apply the effect to the game state
2022-12-31 07:54:18 +00:00
fn apply(&self, state: &mut GameState);
2023-01-04 13:37:25 +00:00
// description of the event that will be displayed in the event log
// TODO: thils needs a more complex return type.
// A notable event needs to be related to entities
2022-12-31 07:54:18 +00:00
fn description(&self) -> String;
2023-01-04 13:37:25 +00:00
// if true, the event will be added to the event log
// otherwise, it will only be used for internal purposes
fn notable(&self) -> bool;
2022-12-31 07:54:18 +00:00
}
impl Event {
pub fn description(&self) -> String {
format!("{}: {}", self.time, self.effect.description())
}
}
impl GameState {
pub fn new(world: World) -> GameState {
let mut events = Vec::new();
events.push(Box::new(Event {
time: Time { time: 0 },
effect: Box::new(WorldGenesis),
}));
GameState {
time: Time { time: 0 },
2023-01-04 13:37:25 +00:00
creatures: HashMap::new(),
2022-12-31 07:54:18 +00:00
world: world,
events: events,
2022-12-31 13:03:24 +00:00
tavern: None,
2022-12-31 07:54:18 +00:00
}
}
2022-12-31 13:03:24 +00:00
pub fn set_tavern(&mut self, tavern: Rc<Tavern>) {
self.tavern = Some(tavern);
}
2022-12-31 07:54:18 +00:00
pub fn add_event(&mut self, event: Event) {
event.effect.apply(self);
self.events.push(Box::new(event));
}
2022-12-31 13:03:24 +00:00
/**
* Getters
*/
2023-01-04 13:37:25 +00:00
pub fn guests(&self) -> Vec<EntityId> {
2022-12-31 13:03:24 +00:00
match &self.tavern {
Some(tavern) => {
let loc = self.world.get_tavern_location(tavern);
2023-01-04 13:37:25 +00:00
self.creatures.values().filter(|c| {
c.entity.loc == loc
}).map(|p| p.entity.id).collect::<Vec<_>>()
// Vec::new()
2022-12-31 13:03:24 +00:00
},
None => Vec::new(),
2022-12-31 14:22:44 +00:00
}
}
2023-01-04 13:37:25 +00:00
pub fn get_creature(&self, id: u32) -> Option<&Creature> {
self.creatures.get(&id)
2022-12-31 13:03:24 +00:00
}
2022-12-31 15:04:07 +00:00
2022-12-31 13:03:24 +00:00
/**
* Function to populate the world at genesis
*/
2022-12-31 07:54:18 +00:00
pub fn found_town(&mut self) {
let mut rng = rand::thread_rng();
loop {
let x = rng.gen_range(0..self.world.size);
let y = rng.gen_range(0..self.world.size);
if
self.world.map[x][y].terrain == Terrain::Flats ||
self.world.map[x][y].terrain == Terrain::Hills
{
2022-12-31 08:38:01 +00:00
let town = Rc::new(Town::new());
2022-12-31 07:54:18 +00:00
self.add_event(Event {
time: self.time,
effect: Box::new(FoundTown {
2023-01-04 13:37:25 +00:00
loc: Location{ x: x as i32, y: y as i32 },
2022-12-31 08:38:01 +00:00
town: town.clone(),
2022-12-31 07:54:18 +00:00
})
});
2022-12-31 08:38:01 +00:00
let pop_size = rng.gen_range(20..200);
for _ in 0..pop_size {
self.add_event(Event {
time: self.time,
effect: Box::new(PersonGenesis {
town: town.clone(),
2023-01-04 13:37:25 +00:00
person: Creature::new(
2022-12-31 08:38:01 +00:00
self.time.substract_years(rng.gen_range(18..30)),
self.world.get_town_location(&town)
2022-12-31 14:22:44 +00:00
),
2022-12-31 08:38:01 +00:00
})
});
}
2022-12-31 07:54:18 +00:00
break;
}
}
}
2022-12-31 08:38:01 +00:00
2023-01-04 13:37:25 +00:00
pub fn add_person(&mut self, mut creature: Creature) {
let id = (self.creatures.len() + 1) as u32; // avoid 0 id
creature.entity.id = id;
self.creatures.insert(id, creature);
2022-12-31 08:38:01 +00:00
}
2022-12-31 13:03:24 +00:00
pub fn build_tavern(&mut self) {
let mut rng = rand::thread_rng();
loop {
let x = rng.gen_range(0..self.world.size);
let y = rng.gen_range(0..self.world.size);
if
self.world.map[x][y].terrain == Terrain::Flats ||
self.world.map[x][y].terrain == Terrain::Hills
{
self.add_event(Event {
time: self.time,
effect: Box::new(TavernBuilt {
2023-01-04 13:37:25 +00:00
loc: Location{ x: x as i32, y: y as i32 },
2022-12-31 13:03:24 +00:00
tavern: Rc::new(Tavern::new()),
})
});
2022-12-31 14:22:44 +00:00
break;
2022-12-31 13:03:24 +00:00
}
}
}
2022-12-31 14:22:44 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_found_town() {
let mut state = GameState::new(World::new(1));
state.world.map[0][0].terrain = Terrain::Flats;
2023-01-01 20:11:27 +00:00
state.time = Time { time: 1e6 as i32 };
2022-12-31 14:22:44 +00:00
state.found_town();
2023-01-04 13:37:25 +00:00
assert_ne!(state.creatures.len(), 0);
2022-12-31 14:22:44 +00:00
}
#[test]
fn test_build_tavern() {
let mut state = GameState::new(World::new(2));
state.world.map[0][0].terrain = Terrain::Flats;
state.world.map[0][1].terrain = Terrain::Flats;
state.world.map[1][0].terrain = Terrain::Hills;
state.world.map[1][1].terrain = Terrain::Hills;
state.build_tavern();
assert_eq!(state.tavern.is_some(), true);
}
2023-01-04 13:37:25 +00:00
2022-12-30 20:30:12 +00:00
}