Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Husbandry #140

Merged
merged 10 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions client/src/action_buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ pub fn base_or_custom_action(
action: PlayingActionType,
title: &str,
custom: &[(&str, CustomActionType)],
f: impl Fn(BaseOrCustomDialog) -> ActiveDialog,
execute: impl Fn(BaseOrCustomDialog) -> ActiveDialog,
) -> StateUpdate {
let base = if rc.can_play_action(action) {
Some(f(BaseOrCustomDialog {
Some(execute(BaseOrCustomDialog {
custom: BaseOrCustomAction::Base,
title: title.to_string(),
}))
Expand All @@ -133,7 +133,7 @@ pub fn base_or_custom_action(
.find(|a| custom.iter().any(|(_, b)| **a == *b))
.map(|a| {
let advance = custom.iter().find(|(_, b)| *b == *a).unwrap().0;
let dialog = f(BaseOrCustomDialog {
let dialog = execute(BaseOrCustomDialog {
custom: BaseOrCustomAction::Custom {
custom: a.clone(),
advance: advance.to_string(),
Expand Down
7 changes: 4 additions & 3 deletions client/src/collect_ui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::collections::HashSet;

use crate::client_state::{ActiveDialog, StateUpdate};
use crate::dialog_ui::{
Expand Down Expand Up @@ -28,7 +29,7 @@ use server::resource_pile::ResourcePile;
pub struct CollectResources {
player_index: usize,
city_position: Position,
possible_collections: HashMap<Position, Vec<ResourcePile>>,
possible_collections: HashMap<Position, HashSet<ResourcePile>>,
collections: Vec<(Position, ResourcePile)>,
custom: BaseOrCustomDialog,
}
Expand All @@ -37,7 +38,7 @@ impl CollectResources {
pub fn new(
player_index: usize,
city_position: Position,
possible_collections: HashMap<Position, Vec<ResourcePile>>,
possible_collections: HashMap<Position, HashSet<ResourcePile>>,
custom: BaseOrCustomDialog,
) -> CollectResources {
CollectResources {
Expand Down Expand Up @@ -134,7 +135,7 @@ fn click_collect_option(
new.collections.push((p, pile.clone()));
}

let used = col.collections.clone().into_iter().collect();
let used = new.collections.clone().into_iter().collect();
new.possible_collections =
possible_resource_collections(rc.game, col.city_position, col.player_index, &used);

Expand Down
3 changes: 3 additions & 0 deletions client/src/local_client/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ pub fn setup_local_game() -> Game {
add_terrain(&mut game, "C4", Terrain::Water);
add_terrain(&mut game, "C5", Terrain::Water);
add_terrain(&mut game, "D1", Terrain::Fertile);
add_terrain(&mut game, "E2", Terrain::Fertile);
add_terrain(&mut game, "B5", Terrain::Fertile);
add_terrain(&mut game, "B6", Terrain::Fertile);
add_terrain(&mut game, "D2", Terrain::Water);

add_unit(&mut game, "C2", player_index1, UnitType::Infantry);
Expand Down
28 changes: 28 additions & 0 deletions server/src/ability_initializer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::action::Action;
use crate::{
content::custom_actions::CustomActionType, events::EventMut, game::Game,
player_events::PlayerEvents,
Expand Down Expand Up @@ -51,6 +52,33 @@ pub trait AbilityInitializerSetup: Sized {
.add_ability_deinitializer(deinitializer)
}

fn add_once_per_turn_effect<P>(self, name: &str, pred: P) -> Self
where
P: Fn(&Action) -> bool + 'static + Clone,
{
let pred2 = pred.clone();
let name2 = name.to_string();
let name3 = name.to_string();
self.add_player_event_listener(
|event| &mut event.after_execute_action,
move |player, action, ()| {
if pred2(action) {
player.played_once_per_turn_effects.push(name2.to_string());
}
},
0,
)
.add_player_event_listener(
|event| &mut event.before_undo_action,
move |player, action, ()| {
if pred(action) {
player.played_once_per_turn_effects.retain(|a| a != &name3);
}
},
0,
)
}

fn add_custom_action(self, action: CustomActionType) -> Self {
let deinitializer_action = action.clone();
self.add_ability_initializer(move |game, player_index| {
Expand Down
36 changes: 28 additions & 8 deletions server/src/collect.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::game::Game;
use crate::map::Terrain;
use crate::map::Terrain::{Fertile, Forest, Mountain};
use crate::playing_actions::Collect;
use crate::position::Position;
use crate::resource_pile::ResourcePile;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::iter;
use std::ops::Add;

Expand Down Expand Up @@ -61,9 +62,10 @@ pub(crate) fn undo_collect(game: &mut Game, player_index: usize, c: Collect) {
}

pub(crate) struct CollectContext {
pub player_index: usize,
pub city_position: Position,
#[allow(dead_code)] // will need for other advances
pub used: HashMap<Position, ResourcePile>,
pub terrain_options: HashMap<Terrain, HashSet<ResourcePile>>,
}

///
Expand All @@ -75,12 +77,19 @@ pub fn possible_resource_collections(
city_pos: Position,
player_index: usize,
used: &HashMap<Position, ResourcePile>,
) -> HashMap<Position, Vec<ResourcePile>> {
let terrain_options = HashMap::from([
(Mountain, vec![ResourcePile::ore(1)]),
(Fertile, vec![ResourcePile::food(1)]),
(Forest, vec![ResourcePile::wood(1)]),
]);
) -> HashMap<Position, HashSet<ResourcePile>> {
let set = [
(Mountain, HashSet::from([ResourcePile::ore(1)])),
(Fertile, HashSet::from([ResourcePile::food(1)])),
(Forest, HashSet::from([ResourcePile::wood(1)])),
];
let mut terrain_options = HashMap::from(set);
game.players[player_index]
.events
.as_ref()
.expect("events should be set")
.terrain_collect_options
.trigger(&mut terrain_options, &(), &());

let mut collect_options = city_pos
.neighbors()
Expand All @@ -95,6 +104,7 @@ pub fn possible_resource_collections(
None
})
.collect();

game.players[player_index]
.events
.as_ref()
Expand All @@ -103,11 +113,21 @@ pub fn possible_resource_collections(
.trigger(
&mut collect_options,
&CollectContext {
player_index,
city_position: city_pos,
used: used.clone(),
terrain_options,
},
game,
);
for (pos, pile) in used {
collect_options
.entry(*pos)
.or_default()
.insert(pile.clone());
// collect_options.insert(*pos, vec![pile.clone()]);
}

collect_options.retain(|p, _| {
game.get_any_city(*p).is_none_or(|c| c.position == city_pos)
&& game.enemy_player(player_index, *p).is_none()
Expand Down
159 changes: 108 additions & 51 deletions server/src/content/advances.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use super::custom_actions::CustomActionType::*;
use crate::action::Action;
use crate::advance::AdvanceBuilder;
use crate::playing_actions::PlayingActionType;
use crate::collect::CollectContext;
use crate::playing_actions::{PlayingAction, PlayingActionType};
use crate::position::Position;
use crate::{
ability_initializer::AbilityInitializerSetup,
advance::{Advance, Bonus::*},
game::Game,
map::Terrain::*,
resource_pile::ResourcePile,
};
use std::collections::{HashMap, HashSet};

//names of advances that need special handling
pub const NAVIGATION: &str = "Navigation";
Expand Down Expand Up @@ -74,36 +78,85 @@ fn agriculture() -> Vec<Advance> {
"Storage",
"Your maximum food limit is increased from 2 to 7",
)
.add_one_time_ability_initializer(|game, player_index| {
game.players[player_index].resource_limit.food = 7;
})
.add_ability_undo_deinitializer(|game, player_index| {
game.players[player_index].resource_limit.food = 2;
})
.with_advance_bonus(MoodToken),
.add_one_time_ability_initializer(|game, player_index| {
game.players[player_index].resource_limit.food = 7;
})
.add_ability_undo_deinitializer(|game, player_index| {
game.players[player_index].resource_limit.food = 2;
})
.with_advance_bonus(MoodToken),
Advance::builder(
"Irrigation",
"Your cities may Collect food from Barren spaces, Ignore Famine events",
)
.add_player_event_listener(
|event| &mut event.collect_options,
|options, c, game| {
c.city_position
.neighbors()
.iter()
.chain(std::iter::once(&c.city_position))
.filter(|pos| game.map.get(**pos) == Some(&Barren))
.for_each(|pos| {
options.insert(*pos, vec![ResourcePile::food(1)]);
});
},
0,
.add_player_event_listener(
|event| &mut event.terrain_collect_options,
|m,(),()| {
m.insert(Barren, HashSet::from([ResourcePile::food(1)]));
},
0,
)
.with_advance_bonus(MoodToken),
Advance::builder(
"Husbandry",
"During a Collect Resources Action, you may collect from a Land space that is 2 Land spaces away, rather than 1. If you have the Roads Advance you may collect from two Land spaces that are 2 Land spaces away. This Advance can only be used once per turn.",
)
.with_advance_bonus(MoodToken),
.with_advance_bonus(MoodToken)
.add_player_event_listener(
|event| &mut event.collect_options,
husbandry_collect,
0,
)
.add_once_per_turn_effect("Husbandry", is_husbandry_action)
],
)
}

fn is_husbandry_action(action: &Action) -> bool {
match action {
Action::Playing(PlayingAction::Collect(collect)) => collect
.collections
.iter()
.any(|c| c.0.distance(collect.city_position) > 1),
_ => false,
}
}

fn husbandry_collect(
options: &mut HashMap<Position, HashSet<ResourcePile>>,
c: &CollectContext,
game: &Game,
) {
let player = &game.players[c.player_index];
let allowed = if player
.played_once_per_turn_effects
.contains(&"Husbandry".to_string())
{
0
} else if player.has_advance(ROADS) {
2
} else {
1
};

if c.used
.iter()
.filter(|(pos, _)| pos.distance(c.city_position) == 2)
.count()
== allowed
{
return;
}

game.map
.tiles
.iter()
.filter(|(pos, t)| pos.distance(c.city_position) == 2 && t.is_land())
.for_each(|(pos, t)| {
options.insert(*pos, c.terrain_options.get(t).cloned().unwrap_or_default());
});
}

fn construction() -> Vec<Advance> {
advance_group(
"Mining",
Expand All @@ -125,35 +178,7 @@ fn seafaring() -> Vec<Advance> {
"Fishing",
vec![
Advance::builder("Fishing", "Your cities may Collect food from one Sea space")
.add_player_event_listener(
|event| &mut event.collect_options,
|options, c, game| {
let city = game
.get_any_city(c.city_position)
.expect("city should exist");
let port = city.port_position;
if let Some(position) = port.or_else(|| {
c.city_position
.neighbors()
.into_iter()
.find(|pos| game.map.is_water(*pos))
}) {
options.insert(
position,
if port.is_some() {
vec![
ResourcePile::food(1),
ResourcePile::gold(1),
ResourcePile::mood_tokens(1),
]
} else {
vec![ResourcePile::food(1)]
},
);
}
},
0,
)
.add_player_event_listener(|event| &mut event.collect_options, fishing_collect, 0)
.with_advance_bonus(MoodToken),
Advance::builder(
NAVIGATION,
Expand All @@ -163,6 +188,38 @@ fn seafaring() -> Vec<Advance> {
)
}

fn fishing_collect(
options: &mut HashMap<Position, HashSet<ResourcePile>>,
c: &CollectContext,
game: &Game,
) {
let city = game
.get_any_city(c.city_position)
.expect("city should exist");
let port = city.port_position;
if let Some(position) =
port.filter(|p| game.enemy_player(c.player_index, *p).is_none())
.or_else(|| {
c.city_position.neighbors().into_iter().find(|p| {
game.map.is_water(*p) && game.enemy_player(c.player_index, *p).is_none()
})
})
{
options.insert(
position,
if Some(position) == port {
HashSet::from([
ResourcePile::food(1),
ResourcePile::gold(1),
ResourcePile::mood_tokens(1),
])
} else {
HashSet::from([ResourcePile::food(1)])
},
);
}
}

fn education() -> Vec<Advance> {
advance_group(
"Philosophy",
Expand Down
Loading
Loading