Skip to content

Commit

Permalink
fix waste (#130)
Browse files Browse the repository at this point in the history
* fix waste

* fmt
  • Loading branch information
zeitlinger authored Jan 21, 2025
1 parent f141fae commit 6de4d9f
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 9 deletions.
12 changes: 11 additions & 1 deletion server/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::combat::{self, Combat, CombatDieRoll, CombatPhase, COMBAT_DIE_SIDES};
use crate::explore::{explore_resolution, move_to_unexplored_tile, undo_explore_resolution};
use crate::map::UnexploredBlock;
use crate::movement::terrain_movement_restriction;
use crate::resource::check_for_waste;
use crate::unit::{carried_units, get_current_move, MovementRestriction};
use crate::utils::Rng;
use crate::utils::Shuffle;
Expand Down Expand Up @@ -354,6 +355,7 @@ impl Game {
}
Finished => panic!("actions can't be executed when the game is finished"),
}
check_for_waste(self, player_index);
}

fn can_auto_redo(&mut self, action: &Action) -> bool {
Expand Down Expand Up @@ -388,6 +390,10 @@ impl Game {
}
self.action_log_index -= 1;
self.log.remove(self.log.len() - 1);
if let Some(UndoContext::WastedResources { resources }) = self.undo_context_stack.last() {
self.players[player_index].gain_resources(resources.clone());
self.undo_context_stack.pop();
}
}

fn redo(&mut self, player_index: usize) {
Expand Down Expand Up @@ -430,6 +436,7 @@ impl Game {
Action::Redo => panic!("redo action can't be redone"),
}
self.action_log_index += 1;
check_for_waste(self, player_index);
}

#[must_use]
Expand Down Expand Up @@ -485,7 +492,7 @@ impl Game {
.find(|route| route.destination == destination)
.expect("destination should be a valid destination")
.cost;
if c.resource_amount() > 0 {
if !c.is_empty() {
self.players[player_index].loose_resources(c.clone());
cost = Some(c.clone());
}
Expand Down Expand Up @@ -1669,6 +1676,9 @@ pub enum UndoContext {
cost: Option<ResourcePile>,
},
ExploreResolution(ExploreResolutionState),
WastedResources {
resources: ResourcePile,
},
}

#[derive(Serialize, Deserialize)]
Expand Down
7 changes: 6 additions & 1 deletion server/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub struct Player {
pub index: usize,
pub resources: ResourcePile,
pub resource_limit: ResourcePile,
// transient, only for the current turn, only the active player can gain resources
pub wasted_resources: ResourcePile,
pub events: Option<PlayerEvents>,
pub cities: Vec<City>,
pub units: Vec<Unit>,
Expand Down Expand Up @@ -172,6 +174,7 @@ impl Player {
index: data.id,
resources: data.resources,
resource_limit: data.resource_limit,
wasted_resources: ResourcePile::empty(),
events: Some(PlayerEvents::default()),
cities: data.cities.into_iter().map(City::from_data).collect(),
units,
Expand Down Expand Up @@ -307,6 +310,7 @@ impl Player {
index,
resources: ResourcePile::food(2),
resource_limit: ResourcePile::new(2, 7, 7, 7, 7, 0, 0),
wasted_resources: ResourcePile::empty(),
events: Some(PlayerEvents::new()),
cities: Vec::new(),
units: Vec::new(),
Expand Down Expand Up @@ -367,7 +371,8 @@ impl Player {

pub fn gain_resources(&mut self, resources: ResourcePile) {
self.resources += resources;
self.resources.apply_resource_limit(&self.resource_limit);
let waste = self.resources.apply_resource_limit(&self.resource_limit);
self.wasted_resources += waste;
}

///
Expand Down
3 changes: 1 addition & 2 deletions server/src/playing_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ impl PlayingAction {
let player = &mut game.players[player_index];
let city = City::new(player_index, settler.position);
player.cities.push(city);
game.undo_context_stack
.push(UndoContext::FoundCity { settler });
game.push_undo_context(UndoContext::FoundCity { settler });
}
Construct(c) => {
let player = &game.players[player_index];
Expand Down
22 changes: 22 additions & 0 deletions server/src/resource.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::game::{Game, UndoContext};
use crate::resource_pile::ResourcePile;
use std::collections::HashMap;
use std::fmt;
Expand Down Expand Up @@ -49,3 +50,24 @@ pub fn new_resource_map(p: &ResourcePile) -> HashMap<ResourceType, u32> {
fn add_resource(m: &mut HashMap<ResourceType, u32>, amount: u32, resource_type: ResourceType) {
m.insert(resource_type, amount);
}

pub(crate) fn check_for_waste(game: &mut Game, player_index: usize) {
let mut wasted_resources = ResourcePile::empty();
for p in &mut game.players {
if p.wasted_resources.is_empty() {
continue;
}
assert_eq!(
p.index, player_index,
"non-active player {} has wasted resources: {:?}",
p.index, p.wasted_resources
);
wasted_resources = p.wasted_resources.clone();
p.wasted_resources = ResourcePile::empty();
}
if !wasted_resources.is_empty() {
game.push_undo_context(UndoContext::WastedResources {
resources: wasted_resources,
});
}
}
18 changes: 16 additions & 2 deletions server/src/resource_pile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,30 @@ impl ResourcePile {
&& self.culture_tokens >= other.culture_tokens
}

pub fn apply_resource_limit(&mut self, limit: &ResourcePile) {
#[must_use]
pub fn apply_resource_limit(&mut self, limit: &ResourcePile) -> ResourcePile {
let mut waste = ResourcePile::empty();
if self.food > limit.food {
waste.food = self.food - limit.food;
self.food = limit.food;
}
if self.wood > limit.wood {
waste.wood = self.wood - limit.wood;
self.wood = limit.wood;
}
if self.ore > limit.ore {
waste.ore = self.ore - limit.ore;
self.ore = limit.ore;
}
if self.ideas > limit.ideas {
waste.ideas = self.ideas - limit.ideas;
self.ideas = limit.ideas;
}
if self.gold > limit.gold {
waste.gold = self.gold - limit.gold;
self.gold = limit.gold;
}
waste
}

//this function assumes that `self` can afford `cost`
Expand Down Expand Up @@ -278,6 +286,11 @@ impl ResourcePile {
+ self.mood_tokens
+ self.culture_tokens
}

#[must_use]
pub fn is_empty(&self) -> bool {
self.resource_amount() == 0
}
}

impl AddAssign for ResourcePile {
Expand Down Expand Up @@ -479,8 +492,9 @@ mod tests {
#[test]
fn resource_limit_test() {
let mut resources = ResourcePile::new(3, 6, 9, 9, 0, 10, 6);
resources.apply_resource_limit(&ResourcePile::new(7, 5, 7, 10, 3, 7, 6));
let waste = resources.apply_resource_limit(&ResourcePile::new(7, 5, 7, 10, 3, 7, 6));
assert_eq!(ResourcePile::new(3, 5, 7, 9, 0, 10, 6), resources);
assert_eq!(ResourcePile::new(0, 1, 2, 0, 0, 0, 0), waste);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion server/tests/test_games/collect.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"resources": {
"food": 1,
"wood": 6,
"ore": 6,
"ore": 7,
"ideas": 5,
"gold": 7,
"mood_tokens": 6,
Expand Down
7 changes: 7 additions & 0 deletions server/tests/test_games/collect.outcome.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@
},
{
"Recruit": {}
},
{
"WastedResources": {
"resources": {
"ore": 1
}
}
}
]
}
9 changes: 8 additions & 1 deletion server/tests/test_games/direct_capture_city.outcome.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,13 @@
"starting_position": "C2",
"movement_actions_left": 3
}
},
{
"WastedResources": {
"resources": {
"gold": 1
}
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@
"starting_position": "C2",
"movement_actions_left": 3
}
},
{
"WastedResources": {
"resources": {
"gold": 1
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@
"starting_position": "C2",
"movement_actions_left": 3
}
},
{
"WastedResources": {
"resources": {
"gold": 1
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,13 @@
},
{
"Recruit": {}
},
{
"WastedResources": {
"resources": {
"gold": 1
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@
}
]
}
},
{
"WastedResources": {
"resources": {
"gold": 2
}
}
}
]
}
}

0 comments on commit 6de4d9f

Please sign in to comment.