remove min modifier (#221)
This commit is contained in:
parent
1a7c0ca040
commit
5bc49280e3
62 changed files with 1056 additions and 838 deletions
107
client/Cargo.lock
generated
107
client/Cargo.lock
generated
|
|
@ -29,21 +29,6 @@ version = "0.2.21"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
|
||||
|
||||
[[package]]
|
||||
name = "android-tzdata"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
|
||||
|
||||
[[package]]
|
||||
name = "android_system_properties"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.97"
|
||||
|
|
@ -266,20 +251,6 @@ version = "0.2.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
|
||||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"wasm-bindgen",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "client"
|
||||
version = "0.1.0"
|
||||
|
|
@ -327,12 +298,6 @@ dependencies = [
|
|||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation-sys"
|
||||
version = "0.8.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||
|
||||
[[package]]
|
||||
name = "cpp_demangle"
|
||||
version = "0.4.4"
|
||||
|
|
@ -806,30 +771,6 @@ dependencies = [
|
|||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.63"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8"
|
||||
dependencies = [
|
||||
"android_system_properties",
|
||||
"core-foundation-sys",
|
||||
"iana-time-zone-haiku",
|
||||
"js-sys",
|
||||
"log",
|
||||
"wasm-bindgen",
|
||||
"windows-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone-haiku"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "icu_collections"
|
||||
version = "1.5.0"
|
||||
|
|
@ -1938,7 +1879,6 @@ name = "server"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-std",
|
||||
"chrono",
|
||||
"console_error_panic_hook",
|
||||
"enumset",
|
||||
"hex2d",
|
||||
|
|
@ -1948,6 +1888,7 @@ dependencies = [
|
|||
"num_cpus",
|
||||
"pathfinding",
|
||||
"readonly",
|
||||
"rustc-hash",
|
||||
"serde",
|
||||
"serde-wasm-bindgen",
|
||||
"serde_json",
|
||||
|
|
@ -2447,41 +2388,6 @@ version = "0.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.61.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980"
|
||||
dependencies = [
|
||||
"windows-implement",
|
||||
"windows-interface",
|
||||
"windows-link",
|
||||
"windows-result",
|
||||
"windows-strings 0.4.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-implement"
|
||||
version = "0.60.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.100",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-interface"
|
||||
version = "0.59.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.100",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.1.1"
|
||||
|
|
@ -2495,7 +2401,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "4286ad90ddb45071efd1a66dfa43eb02dd0dfbae1545ad6cc3c51cf34d7e8ba3"
|
||||
dependencies = [
|
||||
"windows-result",
|
||||
"windows-strings 0.3.1",
|
||||
"windows-strings",
|
||||
"windows-targets 0.53.0",
|
||||
]
|
||||
|
||||
|
|
@ -2517,15 +2423,6 @@ dependencies = [
|
|||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-strings"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a2ba9642430ee452d5a7aa78d72907ebe8cfda358e8cb7918a2050581322f97"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ use server::action::Action;
|
|||
use server::advance::{Advance, AdvanceInfo, Bonus};
|
||||
use server::content::advances;
|
||||
use server::game::GameState;
|
||||
use server::player::Player;
|
||||
use server::player::{CostTrigger, Player};
|
||||
use server::playing_actions::PlayingAction;
|
||||
use server::unit::UnitType;
|
||||
use std::ops::Rem;
|
||||
|
|
@ -31,7 +31,9 @@ pub enum AdvanceState {
|
|||
|
||||
fn new_advance_payment(rc: &RenderContext, a: &AdvanceInfo) -> Payment<Advance> {
|
||||
rc.new_payment(
|
||||
&rc.shown_player.advance_cost(a.advance, None).cost,
|
||||
&rc.shown_player
|
||||
.advance_cost(a.advance, CostTrigger::WithModifiers)
|
||||
.cost,
|
||||
a.advance,
|
||||
&a.name,
|
||||
false,
|
||||
|
|
@ -160,7 +162,9 @@ fn description(rc: &RenderContext, a: &AdvanceInfo) -> Vec<String> {
|
|||
add_tooltip_description(&mut parts, &a.description);
|
||||
parts.push(format!(
|
||||
"Cost: {}",
|
||||
rc.shown_player.advance_cost(a.advance, None).cost
|
||||
rc.shown_player
|
||||
.advance_cost(a.advance, CostTrigger::WithModifiers)
|
||||
.cost
|
||||
));
|
||||
if let Some(r) = &a.required {
|
||||
parts.push(format!("Required: {r}"));
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ use server::construct::{can_construct, new_building_positions};
|
|||
use server::consts::BUILDING_COST;
|
||||
use server::content::persistent_events::Structure;
|
||||
use server::game::Game;
|
||||
use server::player::CostTrigger;
|
||||
use server::playing_actions::PlayingActionType;
|
||||
use server::resource::ResourceType;
|
||||
use server::unit::{UnitType, Units};
|
||||
|
|
@ -117,7 +118,7 @@ fn building_icons<'a>(rc: &'a RenderContext, city: &'a City) -> IconActionVec<'a
|
|||
Building::all()
|
||||
.into_iter()
|
||||
.flat_map(|b| {
|
||||
let can = can_construct(city, b, rc.shown_player, game);
|
||||
let can = can_construct(city, b, rc.shown_player, game, CostTrigger::WithModifiers);
|
||||
|
||||
new_building_positions(game, b, city)
|
||||
.into_iter()
|
||||
|
|
@ -205,6 +206,7 @@ fn collect_resources_button<'a>(rc: &'a RenderContext, city: &'a City) -> Option
|
|||
city.position,
|
||||
city.player_index,
|
||||
&Vec::new(),
|
||||
CostTrigger::WithModifiers,
|
||||
);
|
||||
ActiveDialog::CollectResources(CollectResources::new(
|
||||
city.player_index,
|
||||
|
|
@ -389,7 +391,7 @@ fn draw_buildings(
|
|||
pub fn add_building_description(rc: &RenderContext, parts: &mut Vec<String>, b: Building) {
|
||||
let pile = rc
|
||||
.shown_player
|
||||
.building_cost(rc.game, b, None)
|
||||
.building_cost(rc.game, b, CostTrigger::WithModifiers)
|
||||
.cost
|
||||
.first_valid_payment(&BUILDING_COST)
|
||||
.expect("Building cost should be valid");
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ use server::collect::{
|
|||
CollectInfo, PositionCollection, add_collect, get_total_collection,
|
||||
possible_resource_collections, tiles_used,
|
||||
};
|
||||
use server::player::CostTrigger;
|
||||
use server::playing_actions::{Collect, PlayingAction};
|
||||
use server::position::Position;
|
||||
use server::resource::ResourceType;
|
||||
|
|
@ -82,6 +83,7 @@ pub fn collect_dialog(rc: &RenderContext, collect: &CollectResources) -> StateUp
|
|||
collect.player_index,
|
||||
collect.city_position,
|
||||
&collect.collections,
|
||||
CostTrigger::WithModifiers,
|
||||
);
|
||||
let tooltip = result.as_ref().map_or(
|
||||
OkTooltip::Invalid("Too many resources selected".to_string()),
|
||||
|
|
@ -124,7 +126,13 @@ fn click_collect_option(
|
|||
let c = add_collect(&col.info, p, pile, &col.collections);
|
||||
|
||||
let used = c.clone().into_iter().collect_vec();
|
||||
let i = possible_resource_collections(rc.game, col.info.city, col.player_index, &used);
|
||||
let i = possible_resource_collections(
|
||||
rc.game,
|
||||
col.info.city,
|
||||
col.player_index,
|
||||
&used,
|
||||
CostTrigger::WithModifiers,
|
||||
);
|
||||
let mut new = col.clone();
|
||||
new.info = i;
|
||||
new.collections = c;
|
||||
|
|
@ -173,7 +181,7 @@ pub fn draw_resource_collect_tile(rc: &RenderContext, pos: Position) -> StateUpd
|
|||
}
|
||||
|
||||
let map = new_resource_map(pile);
|
||||
let m: Vec<(ResourceType, &u32)> = ResourceType::all()
|
||||
let m: Vec<(ResourceType, &u8)> = ResourceType::all()
|
||||
.iter()
|
||||
.filter_map(|r| {
|
||||
let a = map.get(r);
|
||||
|
|
@ -196,7 +204,7 @@ pub fn draw_resource_collect_tile(rc: &RenderContext, pos: Position) -> StateUpd
|
|||
fn draw_collect_item(
|
||||
rc: &RenderContext,
|
||||
center: Vec2,
|
||||
resources: &[(ResourceType, &u32)],
|
||||
resources: &[(ResourceType, &u8)],
|
||||
size: f32,
|
||||
) {
|
||||
if resources.iter().len() == 1 {
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ use server::action::Action;
|
|||
use server::city::{City, MoodState};
|
||||
use server::game::Game;
|
||||
use server::happiness::{available_happiness_actions, happiness_cost};
|
||||
use server::player::Player;
|
||||
use server::player::{CostTrigger, Player};
|
||||
use server::player_events::CostInfo;
|
||||
use server::playing_actions::{IncreaseHappiness, PlayingAction, PlayingActionType};
|
||||
use server::position::Position;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct IncreaseHappinessConfig {
|
||||
pub steps: Vec<(Position, u32)>,
|
||||
pub steps: Vec<(Position, u8)>,
|
||||
pub payment: Payment<String>,
|
||||
pub custom: BaseOrCustomDialog,
|
||||
}
|
||||
|
|
@ -32,10 +32,10 @@ impl IncreaseHappinessConfig {
|
|||
|
||||
fn happiness_payment(
|
||||
p: &Player,
|
||||
new_steps: u32,
|
||||
new_steps: u8,
|
||||
custom: &BaseOrCustomDialog,
|
||||
) -> Option<Payment<String>> {
|
||||
let c = happiness_cost(p, new_steps, None).cost;
|
||||
let c = happiness_cost(p, new_steps, CostTrigger::WithModifiers).cost;
|
||||
c.can_afford(&custom.action_type.remaining_resources(p))
|
||||
.then_some(Payment::new(
|
||||
&c,
|
||||
|
|
@ -79,7 +79,7 @@ pub fn add_increase_happiness(
|
|||
city: &City,
|
||||
mut increase_happiness: IncreaseHappinessConfig,
|
||||
) -> Option<IncreaseHappinessConfig> {
|
||||
let new_steps: Vec<(Position, u32)> = increase_happiness
|
||||
let new_steps: Vec<(Position, u8)> = increase_happiness
|
||||
.steps
|
||||
.iter()
|
||||
.map(|(p, steps)| {
|
||||
|
|
@ -100,8 +100,8 @@ pub fn add_increase_happiness(
|
|||
|
||||
let step_sum = new_steps
|
||||
.iter()
|
||||
.map(|(p, steps)| rc.shown_player.get_city(*p).size() as u32 * steps)
|
||||
.sum::<u32>();
|
||||
.map(|(p, steps)| rc.shown_player.get_city(*p).size() as u8 * steps)
|
||||
.sum::<u8>();
|
||||
|
||||
IncreaseHappinessConfig::happiness_payment(
|
||||
rc.shown_player,
|
||||
|
|
@ -118,9 +118,9 @@ pub fn add_increase_happiness(
|
|||
fn increase_happiness_steps(
|
||||
rc: &RenderContext,
|
||||
city: &City,
|
||||
old_steps: u32,
|
||||
old_steps: u8,
|
||||
action_type: &PlayingActionType,
|
||||
) -> Option<u32> {
|
||||
) -> Option<u8> {
|
||||
if let Some(value) = increase_happiness_new_steps(rc, city, old_steps + 1, action_type) {
|
||||
return Some(value);
|
||||
}
|
||||
|
|
@ -133,9 +133,9 @@ fn increase_happiness_steps(
|
|||
fn increase_happiness_new_steps(
|
||||
rc: &RenderContext,
|
||||
city: &City,
|
||||
new_steps: u32,
|
||||
new_steps: u8,
|
||||
action_type: &PlayingActionType,
|
||||
) -> Option<u32> {
|
||||
) -> Option<u8> {
|
||||
increase_happiness_cost(rc.shown_player, city, new_steps, action_type).map(|_| new_steps)
|
||||
}
|
||||
|
||||
|
|
@ -143,11 +143,15 @@ fn increase_happiness_new_steps(
|
|||
pub fn increase_happiness_cost(
|
||||
player: &Player,
|
||||
city: &City,
|
||||
steps: u32,
|
||||
steps: u8,
|
||||
action_type: &PlayingActionType,
|
||||
) -> Option<CostInfo> {
|
||||
let total_cost = happiness_cost(player, steps * city.size() as u32, None);
|
||||
let max_steps = 2 - city.mood_state.clone() as u32;
|
||||
let total_cost = happiness_cost(
|
||||
player,
|
||||
steps * city.size() as u8,
|
||||
CostTrigger::WithModifiers,
|
||||
);
|
||||
let max_steps = 2 - city.mood_state.clone() as u8;
|
||||
(total_cost
|
||||
.cost
|
||||
.can_afford(&action_type.remaining_resources(player))
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use server::action::execute_action;
|
|||
use server::advance::{Advance, do_advance};
|
||||
use server::ai::AI;
|
||||
use server::city::City;
|
||||
use server::game::{Game, GameData};
|
||||
use server::game::{Game, GameData, GameState};
|
||||
use server::game_setup::setup_game;
|
||||
use server::map::Terrain;
|
||||
use server::player::add_unit;
|
||||
|
|
@ -133,14 +133,14 @@ async fn run(mut game: Game, features: &mut Features) {
|
|||
}
|
||||
}
|
||||
|
||||
fn ai_autoplay(game: Game, f: &mut Features, state: &mut State) -> Game {
|
||||
fn ai_autoplay(mut game: Game, f: &mut Features, state: &mut State) -> Game {
|
||||
if let Some(ai) = &mut f.ai {
|
||||
if state.ai_autoplay {
|
||||
while state.ai_autoplay && game.state != GameState::Finished {
|
||||
// todo does this block the ui?
|
||||
// state.ai_autoplay = false;
|
||||
let action = ai.next_action(&game);
|
||||
let player_index = game.active_player();
|
||||
return execute_action(game, action, player_index);
|
||||
game = execute_action(game, action, player_index);
|
||||
}
|
||||
}
|
||||
game
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ pub struct ResourcePayment {
|
|||
}
|
||||
|
||||
impl ResourcePayment {
|
||||
pub fn new(resource: ResourceType, current: u32, min: u32, max: u32) -> ResourcePayment {
|
||||
pub fn new(resource: ResourceType, current: u8, min: u8, max: u8) -> ResourcePayment {
|
||||
ResourcePayment {
|
||||
resource,
|
||||
selectable: CountSelector { current, min, max },
|
||||
|
|
@ -94,7 +94,7 @@ where
|
|||
.unwrap_or_else(|| panic!("Resource {r:?} not found in payment"))
|
||||
}
|
||||
|
||||
fn current(r: &[ResourcePayment], resource_type: ResourceType) -> u32 {
|
||||
fn current(r: &[ResourcePayment], resource_type: ResourceType) -> u8 {
|
||||
r.iter()
|
||||
.find(|p| p.resource == resource_type)
|
||||
.map_or(0, |p| p.selectable.current)
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use itertools::Itertools;
|
|||
use macroquad::prelude::*;
|
||||
|
||||
use server::game::Game;
|
||||
use server::player::Player;
|
||||
use server::player::{CostTrigger, Player};
|
||||
use server::player_events::CostInfo;
|
||||
use server::position::Position;
|
||||
use server::recruit::{recruit_cost, recruit_cost_without_replaced};
|
||||
|
|
@ -85,18 +85,14 @@ fn selectable_unit(
|
|||
&all,
|
||||
city_position,
|
||||
unit.leader_name.as_ref().or(leader_name),
|
||||
None,
|
||||
CostTrigger::WithModifiers,
|
||||
);
|
||||
let max = if cost.is_ok() {
|
||||
u32::from(current + 1)
|
||||
} else {
|
||||
u32::from(current)
|
||||
};
|
||||
let max = if cost.is_ok() { current + 1 } else { current };
|
||||
SelectableUnit {
|
||||
unit_type: unit.unit_type,
|
||||
cost,
|
||||
selectable: CountSelector {
|
||||
current: u32::from(current),
|
||||
current,
|
||||
min: 0,
|
||||
max,
|
||||
},
|
||||
|
|
@ -174,7 +170,7 @@ impl RecruitSelection {
|
|||
self.amount.city_position,
|
||||
self.amount.leader_name.as_ref(),
|
||||
self.replaced_units.as_slice(),
|
||||
None,
|
||||
CostTrigger::WithModifiers,
|
||||
)
|
||||
.map_or_else(
|
||||
|_| OkTooltip::Invalid("Replace exact amount of units".to_string()),
|
||||
|
|
@ -279,7 +275,7 @@ fn open_dialog(rc: &RenderContext, city: Position, sel: RecruitSelection) -> Sta
|
|||
city,
|
||||
sel.amount.leader_name.as_ref(),
|
||||
&sel.replaced_units,
|
||||
None,
|
||||
CostTrigger::WithModifiers,
|
||||
)
|
||||
.unwrap();
|
||||
StateUpdate::OpenDialog(ActiveDialog::ConstructionPayment(ConstructionPayment::new(
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ pub fn resource_name(t: ResourceType) -> &'static str {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn new_resource_map(p: &ResourcePile) -> HashMap<ResourceType, u32> {
|
||||
let mut m: HashMap<ResourceType, u32> = HashMap::new();
|
||||
pub fn new_resource_map(p: &ResourcePile) -> HashMap<ResourceType, u8> {
|
||||
let mut m: HashMap<ResourceType, u8> = HashMap::new();
|
||||
add_resource(&mut m, p.food, ResourceType::Food);
|
||||
add_resource(&mut m, p.wood, ResourceType::Wood);
|
||||
add_resource(&mut m, p.ore, ResourceType::Ore);
|
||||
|
|
@ -31,7 +31,7 @@ pub fn new_resource_map(p: &ResourcePile) -> HashMap<ResourceType, u32> {
|
|||
m
|
||||
}
|
||||
|
||||
fn add_resource(m: &mut HashMap<ResourceType, u32>, amount: u32, resource_type: ResourceType) {
|
||||
fn add_resource(m: &mut HashMap<ResourceType, u8>, amount: u8, resource_type: ResourceType) {
|
||||
m.insert(resource_type, amount);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,9 +8,9 @@ use macroquad::prelude::{GRAY, RED};
|
|||
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
pub struct CountSelector {
|
||||
pub current: u32,
|
||||
pub min: u32,
|
||||
pub max: u32,
|
||||
pub current: u8,
|
||||
pub min: u8,
|
||||
pub max: u8,
|
||||
}
|
||||
|
||||
pub trait HasCountSelectableObject {
|
||||
|
|
|
|||
135
server/Cargo.lock
generated
135
server/Cargo.lock
generated
|
|
@ -17,21 +17,6 @@ version = "2.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
||||
|
||||
[[package]]
|
||||
name = "android-tzdata"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
|
||||
|
||||
[[package]]
|
||||
name = "android_system_properties"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-channel"
|
||||
version = "1.9.0"
|
||||
|
|
@ -203,35 +188,12 @@ version = "1.10.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e3a13707ac958681c13b39b458c073d0d9bc8a22cb1b2f4c8e55eb72c13f362"
|
||||
dependencies = [
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
|
||||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"wasm-bindgen",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "concurrent-queue"
|
||||
version = "2.5.0"
|
||||
|
|
@ -251,12 +213,6 @@ dependencies = [
|
|||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation-sys"
|
||||
version = "0.8.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.21"
|
||||
|
|
@ -472,30 +428,6 @@ dependencies = [
|
|||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.63"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8"
|
||||
dependencies = [
|
||||
"android_system_properties",
|
||||
"core-foundation-sys",
|
||||
"iana-time-zone-haiku",
|
||||
"js-sys",
|
||||
"log",
|
||||
"wasm-bindgen",
|
||||
"windows-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone-haiku"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ident_case"
|
||||
version = "1.0.1"
|
||||
|
|
@ -948,7 +880,6 @@ name = "server"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-std",
|
||||
"chrono",
|
||||
"console_error_panic_hook",
|
||||
"enumset",
|
||||
"hex2d",
|
||||
|
|
@ -958,6 +889,7 @@ dependencies = [
|
|||
"num_cpus",
|
||||
"pathfinding",
|
||||
"readonly",
|
||||
"rustc-hash",
|
||||
"serde",
|
||||
"serde-wasm-bindgen",
|
||||
"serde_json",
|
||||
|
|
@ -966,12 +898,6 @@ dependencies = [
|
|||
"wasm-bindgen-futures",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.2"
|
||||
|
|
@ -1201,65 +1127,6 @@ dependencies = [
|
|||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.61.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980"
|
||||
dependencies = [
|
||||
"windows-implement",
|
||||
"windows-interface",
|
||||
"windows-link",
|
||||
"windows-result",
|
||||
"windows-strings",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-implement"
|
||||
version = "0.60.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-interface"
|
||||
version = "0.59.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38"
|
||||
|
||||
[[package]]
|
||||
name = "windows-result"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c64fd11a4fd95df68efcfee5f44a294fe71b8bc6a91993e2791938abcc712252"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-strings"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a2ba9642430ee452d5a7aa78d72907ebe8cfda358e8cb7918a2050581322f97"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
|
|
|
|||
|
|
@ -30,5 +30,5 @@ pathfinding = "4.14.0"
|
|||
tokio = { version = "1.44.2", features = ["full"] }
|
||||
num_cpus = "1.16.0"
|
||||
readonly = "0.2.13"
|
||||
chrono = "0.4.40"
|
||||
enumset = { version = "1.1.5" }
|
||||
rustc-hash = "2.1.1"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::advance::Advance;
|
||||
use crate::ai_collect::reset_collection_stats;
|
||||
use crate::card::HandCard;
|
||||
use crate::collect::reset_collection_stats;
|
||||
use crate::combat::{Combat, update_combat_strength};
|
||||
use crate::combat_listeners::CombatStrength;
|
||||
use crate::content::persistent_events::{
|
||||
|
|
@ -211,24 +211,6 @@ pub(crate) trait AbilityInitializerSetup: Sized {
|
|||
)
|
||||
}
|
||||
|
||||
fn add_once_per_turn_listener<T, U, V, E, F>(
|
||||
self,
|
||||
event: E,
|
||||
priority: i32,
|
||||
listener: F,
|
||||
get_info: impl Fn(&mut T) -> &mut HashMap<String, String> + 'static + Clone + Sync + Send,
|
||||
) -> Self
|
||||
where
|
||||
T: Clone + PartialEq,
|
||||
E: Fn(&mut TransientEvents) -> &mut Event<T, U, V, ()> + 'static + Clone + Sync + Send,
|
||||
F: Fn(&mut T, &U, &V) + 'static + Clone + Sync + Send,
|
||||
{
|
||||
let id = self.get_key().id();
|
||||
self.add_transient_event_listener(event, priority, move |value, u, v| {
|
||||
do_once_per_turn(&id, value, u, v, get_info.clone(), listener.clone());
|
||||
})
|
||||
}
|
||||
|
||||
fn add_persistent_event_listener<E, V>(
|
||||
self,
|
||||
event: E,
|
||||
|
|
@ -953,8 +935,9 @@ where
|
|||
.add_ability_deinitializer(deinitializer)
|
||||
}
|
||||
|
||||
pub(crate) fn do_once_per_turn<F, T, U, V>(
|
||||
id: &str,
|
||||
#[allow(clippy::map_entry)]
|
||||
pub(crate) fn once_per_turn_advance<F, T, U, V>(
|
||||
id: Advance,
|
||||
value: &mut T,
|
||||
u: &U,
|
||||
v: &V,
|
||||
|
|
@ -963,8 +946,9 @@ pub(crate) fn do_once_per_turn<F, T, U, V>(
|
|||
) where
|
||||
F: Fn(&mut T, &U, &V) + 'static + Clone + Sync + Send,
|
||||
{
|
||||
if !get_info(value).contains_key(id) {
|
||||
let key = id.to_string();
|
||||
if !get_info(value).contains_key(&key) {
|
||||
listener(value, u, v);
|
||||
get_info(value).insert(id.to_string(), "used".to_string());
|
||||
get_info(value).insert(key, "used".to_string());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
use crate::action_card::on_play_action_card;
|
||||
use crate::advance::on_advance;
|
||||
use crate::ai_collect::set_city_collections;
|
||||
use crate::city::{MoodState, on_found_city};
|
||||
use crate::collect::{on_collect, set_city_collections};
|
||||
use crate::collect::on_collect;
|
||||
use crate::combat::{
|
||||
combat_loop, move_with_possible_combat, on_capture_undefended_position, start_combat,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
extern crate num_cpus;
|
||||
use std::time::Duration;
|
||||
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
use crate::ai_actions::AiActions;
|
||||
use crate::{
|
||||
action::{self, Action, ActionType},
|
||||
ai_actions,
|
||||
game::{Game, GameData, GameState},
|
||||
playing_actions::PlayingAction,
|
||||
position::Position,
|
||||
|
|
@ -13,7 +14,6 @@ use crate::{
|
|||
|
||||
const ACTION_SCORE_WEIGHTING: f64 = 1.0;
|
||||
const ADAPTIVE_DIFFICULTY_SCORE_THRESHOLD: f64 = 10.0;
|
||||
extern crate num_cpus;
|
||||
|
||||
pub struct AI {
|
||||
rng: Rng,
|
||||
|
|
@ -21,6 +21,7 @@ pub struct AI {
|
|||
pub thinking_time: Duration,
|
||||
pub adaptive_difficulty: bool,
|
||||
active_missions: Vec<Mission>,
|
||||
pub ai_actions: AiActions,
|
||||
}
|
||||
|
||||
impl AI {
|
||||
|
|
@ -44,6 +45,7 @@ impl AI {
|
|||
thinking_time,
|
||||
adaptive_difficulty,
|
||||
active_missions: Vec::new(),
|
||||
ai_actions: AiActions::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -59,7 +61,7 @@ impl AI {
|
|||
/// May panic if the game is in an invalid state or if `ai_actions` provides an invalid action.
|
||||
pub fn next_action(&mut self, game: &Game) -> Action {
|
||||
//todo: handle movement phase actions
|
||||
let actions = ai_actions::get_available_actions(game);
|
||||
let actions = self.ai_actions.get_available_actions(game);
|
||||
let actions = actions
|
||||
.into_iter()
|
||||
.flat_map(|(action_group, actions)| {
|
||||
|
|
@ -217,9 +219,10 @@ async fn evaluate_action(
|
|||
}
|
||||
|
||||
fn monte_carlo_score(mut rng: Rng, player_index: usize, game_data: GameData) -> f64 {
|
||||
let mut ai = AiActions::new();
|
||||
let mut game = Game::from_data(game_data);
|
||||
game.supports_undo = false;
|
||||
let new_game = monte_carlo_run(game, &mut rng);
|
||||
let new_game = monte_carlo_run(&mut ai, game, &mut rng);
|
||||
let ai_score = new_game.players[player_index].victory_points(&new_game) as f64;
|
||||
let mut max_opponent_score = 0.0;
|
||||
for (i, player) in new_game.players.iter().enumerate() {
|
||||
|
|
@ -233,19 +236,19 @@ fn monte_carlo_score(mut rng: Rng, player_index: usize, game_data: GameData) ->
|
|||
ai_score - max_opponent_score
|
||||
}
|
||||
|
||||
fn monte_carlo_run(mut game: Game, rng: &mut Rng) -> Game {
|
||||
fn monte_carlo_run(ai: &mut AiActions, mut game: Game, rng: &mut Rng) -> Game {
|
||||
loop {
|
||||
if matches!(game.state, GameState::Finished) {
|
||||
return game;
|
||||
}
|
||||
let current_player = game.active_player();
|
||||
let action = choose_monte_carlo_action(&game, rng);
|
||||
let action = choose_monte_carlo_action(ai, &game, rng);
|
||||
game = action::execute_action(game, action, current_player);
|
||||
}
|
||||
}
|
||||
|
||||
fn choose_monte_carlo_action(game: &Game, rng: &mut Rng) -> Action {
|
||||
let action_groups = ai_actions::get_available_actions(game);
|
||||
fn choose_monte_carlo_action(ai: &mut AiActions, game: &Game, rng: &mut Rng) -> Action {
|
||||
let action_groups = ai.get_available_actions(game);
|
||||
if action_groups.is_empty() {
|
||||
return Action::Playing(PlayingAction::EndTurn);
|
||||
}
|
||||
|
|
@ -357,13 +360,13 @@ enum MissionType {
|
|||
///
|
||||
/// Panics if the game is in an invalid state.
|
||||
#[must_use]
|
||||
pub fn evaluate_position(game: &Game, evaluation_time: Duration) -> Vec<f64> {
|
||||
pub fn evaluate_position(ai: &mut AiActions, game: &Game, evaluation_time: Duration) -> Vec<f64> {
|
||||
let mut rng = Rng::new();
|
||||
let start_time = std::time::Instant::now();
|
||||
let mut wins = vec![0; game.players.len()];
|
||||
let mut iterations = 0;
|
||||
loop {
|
||||
let new_game = monte_carlo_run(game.clone(), &mut rng);
|
||||
let new_game = monte_carlo_run(ai, game.clone(), &mut rng);
|
||||
let max_score = new_game
|
||||
.players
|
||||
.iter()
|
||||
|
|
@ -392,8 +395,8 @@ pub fn evaluate_position(game: &Game, evaluation_time: Duration) -> Vec<f64> {
|
|||
///
|
||||
/// Panics if the game is in an invalid state.
|
||||
#[must_use]
|
||||
pub fn rate_action(game: &Game, action: &Action, evaluation_time: Duration) -> f64 {
|
||||
let all_actions = ai_actions::get_available_actions(game);
|
||||
pub fn rate_action(ai: &mut AI, game: &Game, action: &Action, evaluation_time: Duration) -> f64 {
|
||||
let all_actions = ai.ai_actions.get_available_actions(game);
|
||||
let all_actions = all_actions
|
||||
.into_iter()
|
||||
.flat_map(|(action_group, actions)| {
|
||||
|
|
|
|||
|
|
@ -17,16 +17,18 @@ use crate::events::EventOrigin;
|
|||
use crate::game::Game;
|
||||
use crate::happiness::{available_happiness_actions, happiness_cost};
|
||||
use crate::payment::PaymentOptions;
|
||||
use crate::player::Player;
|
||||
use crate::player::{CostTrigger, Player};
|
||||
use crate::playing_actions::{
|
||||
IncreaseHappiness, PlayingAction, PlayingActionType, Recruit, base_and_custom_action,
|
||||
};
|
||||
use crate::position::Position;
|
||||
use crate::recruit::recruit_cost;
|
||||
use crate::resource::ResourceType;
|
||||
use crate::resource_pile::ResourcePile;
|
||||
use crate::status_phase::{ChangeGovernment, ChangeGovernmentType, government_advances};
|
||||
use crate::unit::{UnitType, Units};
|
||||
use itertools::Itertools;
|
||||
use rustc_hash::FxHashMap;
|
||||
use std::vec;
|
||||
//todo
|
||||
//nicht nur maximale anzahl rekrutieren
|
||||
|
|
@ -34,35 +36,69 @@ use std::vec;
|
|||
//Siedler: nur von stadt wegbewegen wo er gebaut wurde
|
||||
//militär
|
||||
|
||||
///
|
||||
/// Returns a list of available actions for the current player.
|
||||
///
|
||||
/// Some simplifications are made to help AI implementations:
|
||||
/// - custom actions are preferred over basic actions if available.
|
||||
/// - always pay default payment
|
||||
/// - collect and select as much as possible (which is not always the best choice,
|
||||
/// e.g. selecting to sacrifice a unit for an incident)
|
||||
/// - move actions are not returned at all - this required special handling
|
||||
///
|
||||
#[must_use]
|
||||
pub fn get_available_actions(game: &Game) -> Vec<(ActionType, Vec<Action>)> {
|
||||
if let Some(event) = game.events.last() {
|
||||
vec![(
|
||||
ActionType::Response,
|
||||
responses(event, game.player(game.active_player()), game)
|
||||
.into_iter()
|
||||
.map(Action::Response)
|
||||
.collect(),
|
||||
)]
|
||||
} else {
|
||||
base_actions(game)
|
||||
struct PaymentCache {
|
||||
options: FxHashMap<PaymentOptions, FxHashMap<ResourcePile, Option<ResourcePile>>>,
|
||||
}
|
||||
|
||||
impl PaymentCache {
|
||||
fn new() -> Self {
|
||||
PaymentCache {
|
||||
options: FxHashMap::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ActionFactory = (PlayingActionType, fn(&Player, &Game) -> Vec<Action>);
|
||||
pub struct AiActions {
|
||||
payment_cache: PaymentCache,
|
||||
}
|
||||
|
||||
impl AiActions {
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
AiActions {
|
||||
payment_cache: PaymentCache::new(),
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a list of available actions for the current player.
|
||||
///
|
||||
/// Some simplifications are made to help AI implementations:
|
||||
/// - custom actions are preferred over basic actions if available.
|
||||
/// - always pay default payment
|
||||
/// - collect and select as much as possible (which is not always the best choice,
|
||||
/// e.g. selecting to sacrifice a unit for an incident)
|
||||
/// - move actions are not returned at all - this required special handling
|
||||
///
|
||||
#[must_use]
|
||||
pub fn get_available_actions(&mut self, game: &Game) -> Vec<(ActionType, Vec<Action>)> {
|
||||
if let Some(event) = game.events.last() {
|
||||
vec![(
|
||||
ActionType::Response,
|
||||
responses(event, game.player(game.active_player()), game)
|
||||
.into_iter()
|
||||
.map(Action::Response)
|
||||
.collect(),
|
||||
)]
|
||||
} else {
|
||||
base_actions(self, game)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AiActions {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
type ActionFactory = (
|
||||
PlayingActionType,
|
||||
fn(&mut AiActions, &Player, &Game) -> Vec<Action>,
|
||||
);
|
||||
|
||||
#[must_use]
|
||||
fn base_actions(game: &Game) -> Vec<(ActionType, Vec<Action>)> {
|
||||
fn base_actions(ai: &mut AiActions, game: &Game) -> Vec<(ActionType, Vec<Action>)> {
|
||||
let p = game.player(game.current_player_index);
|
||||
|
||||
let factories: Vec<ActionFactory> = vec![
|
||||
|
|
@ -79,7 +115,7 @@ fn base_actions(game: &Game) -> Vec<(ActionType, Vec<Action>)> {
|
|||
return None;
|
||||
}
|
||||
|
||||
let a = f(p, game);
|
||||
let a = f(ai, p, game);
|
||||
(!a.is_empty()).then_some((ActionType::Playing(t.clone()), a))
|
||||
})
|
||||
.collect_vec();
|
||||
|
|
@ -210,9 +246,8 @@ fn available_action_cards(game: &Game, p: &Player) -> Vec<Action> {
|
|||
action_cards
|
||||
}
|
||||
|
||||
fn payment(o: &PaymentOptions, p: &Player) -> ResourcePile {
|
||||
o.first_valid_payment(&p.resources)
|
||||
.expect("expected payment")
|
||||
fn payment(ai_actions: &mut AiActions, o: &PaymentOptions, p: &Player) -> ResourcePile {
|
||||
try_payment(ai_actions, o, p).expect("expected payment")
|
||||
}
|
||||
|
||||
fn payment_with_action(
|
||||
|
|
@ -224,11 +259,36 @@ fn payment_with_action(
|
|||
.expect("expected payment")
|
||||
}
|
||||
|
||||
fn try_payment(o: &PaymentOptions, p: &Player) -> Option<ResourcePile> {
|
||||
o.first_valid_payment(&p.resources)
|
||||
fn try_payment(ai_actions: &mut AiActions, o: &PaymentOptions, p: &Player) -> Option<ResourcePile> {
|
||||
let sum = o.default.amount();
|
||||
|
||||
let mut max = p.resources.clone();
|
||||
for r in ResourceType::all() {
|
||||
let t = max.get_mut(&r);
|
||||
if *t > sum {
|
||||
*t = sum;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(e) = ai_actions.payment_cache.options.get_mut(o) {
|
||||
// here we don't need to clone the payment options
|
||||
return e
|
||||
.entry(max)
|
||||
.or_insert_with_key(|available| o.first_valid_payment(available))
|
||||
.clone();
|
||||
}
|
||||
|
||||
ai_actions
|
||||
.payment_cache
|
||||
.options
|
||||
.entry(o.clone())
|
||||
.or_default()
|
||||
.entry(max)
|
||||
.or_insert_with_key(|available| o.first_valid_payment(available))
|
||||
.clone()
|
||||
}
|
||||
|
||||
fn advances(p: &Player, _game: &Game) -> Vec<Action> {
|
||||
fn advances(ai_actions: &mut AiActions, p: &Player, _game: &Game) -> Vec<Action> {
|
||||
advances::get_all()
|
||||
.iter()
|
||||
.filter_map(|info| {
|
||||
|
|
@ -240,7 +300,12 @@ fn advances(p: &Player, _game: &Game) -> Vec<Action> {
|
|||
if !p.can_advance_free(a) {
|
||||
return None;
|
||||
}
|
||||
try_payment(&p.advance_cost(a, None).cost, p).map(|r| {
|
||||
try_payment(
|
||||
ai_actions,
|
||||
&p.advance_cost(a, CostTrigger::NoModifiers).cost,
|
||||
p,
|
||||
)
|
||||
.map(|r| {
|
||||
Action::Playing(PlayingAction::Advance {
|
||||
advance: a,
|
||||
payment: r,
|
||||
|
|
@ -281,7 +346,7 @@ fn collect_actions(p: &Player, game: &Game) -> Vec<Action> {
|
|||
.collect_vec()
|
||||
}
|
||||
|
||||
fn found_city(p: &Player, game: &Game) -> Vec<Action> {
|
||||
fn found_city(_ai_actions: &mut AiActions, p: &Player, game: &Game) -> Vec<Action> {
|
||||
p.units
|
||||
.iter()
|
||||
.filter_map(|u| {
|
||||
|
|
@ -301,12 +366,12 @@ fn recruit_strategies() -> Vec<Vec<UnitType>> {
|
|||
]
|
||||
}
|
||||
|
||||
fn recruit(p: &Player, _game: &Game) -> Vec<Action> {
|
||||
fn recruit(ai_actions: &mut AiActions, p: &Player, _game: &Game) -> Vec<Action> {
|
||||
p.cities
|
||||
.iter()
|
||||
.flat_map(|city| {
|
||||
if city.can_activate() {
|
||||
recruit_actions(p, city)
|
||||
recruit_actions(ai_actions, p, city)
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
|
|
@ -314,7 +379,7 @@ fn recruit(p: &Player, _game: &Game) -> Vec<Action> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn recruit_actions(player: &Player, city: &City) -> Vec<Action> {
|
||||
fn recruit_actions(ai_actions: &mut AiActions, player: &Player, city: &City) -> Vec<Action> {
|
||||
recruit_strategies()
|
||||
.iter()
|
||||
.map(|strategy| {
|
||||
|
|
@ -329,9 +394,16 @@ fn recruit_actions(player: &Player, city: &City) -> Vec<Action> {
|
|||
|
||||
let mut next = units.clone();
|
||||
next += &unit_type;
|
||||
match recruit_cost(player, &next, city.position, None, &[], None) {
|
||||
match recruit_cost(
|
||||
player,
|
||||
&next,
|
||||
city.position,
|
||||
None,
|
||||
&[],
|
||||
CostTrigger::NoModifiers,
|
||||
) {
|
||||
Ok(c) => {
|
||||
cost = payment(&c.cost, player);
|
||||
cost = payment(ai_actions, &c.cost, player);
|
||||
units = next;
|
||||
}
|
||||
Err(_) => {
|
||||
|
|
@ -359,7 +431,7 @@ fn calculate_increase_happiness(
|
|||
action_type: &PlayingActionType,
|
||||
) -> Option<IncreaseHappiness> {
|
||||
// try to make the biggest cities happy - that's usually the best choice
|
||||
let mut all_steps: Vec<(Position, u32)> = vec![];
|
||||
let mut all_steps: Vec<(Position, u8)> = vec![];
|
||||
let mut step_sum = 0;
|
||||
let mut cost = PaymentOptions::free();
|
||||
let available = action_type.remaining_resources(player);
|
||||
|
|
@ -375,9 +447,9 @@ fn calculate_increase_happiness(
|
|||
MoodState::Neutral => 1,
|
||||
MoodState::Happy => 0,
|
||||
};
|
||||
let new_steps_sum = step_sum + steps * c.size() as u32;
|
||||
let new_steps_sum = step_sum + steps * c.size() as u8;
|
||||
|
||||
let info = happiness_cost(player, new_steps_sum, None);
|
||||
let info = happiness_cost(player, new_steps_sum, CostTrigger::NoModifiers);
|
||||
if !info.cost.can_afford(&available) {
|
||||
break;
|
||||
}
|
||||
|
|
@ -571,19 +643,24 @@ fn calculate_influence(
|
|||
.map(|(s, _, _)| s)
|
||||
}
|
||||
|
||||
fn construct(p: &Player, game: &Game) -> Vec<Action> {
|
||||
fn construct(ai_actions: &mut AiActions, p: &Player, game: &Game) -> Vec<Action> {
|
||||
p.cities
|
||||
.iter()
|
||||
.flat_map(|city| {
|
||||
if !city.can_activate() {
|
||||
return vec![];
|
||||
}
|
||||
get_construct_actions(game, p, city)
|
||||
get_construct_actions(ai_actions, game, p, city)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn get_construct_actions(game: &Game, p: &Player, city: &City) -> Vec<Action> {
|
||||
pub(crate) fn get_construct_actions(
|
||||
ai_actions: &mut AiActions,
|
||||
game: &Game,
|
||||
p: &Player,
|
||||
city: &City,
|
||||
) -> Vec<Action> {
|
||||
available_buildings(game, p.index, city.position)
|
||||
.iter()
|
||||
.flat_map(|(building, cost)| {
|
||||
|
|
@ -591,8 +668,12 @@ pub(crate) fn get_construct_actions(game: &Game, p: &Player, city: &City) -> Vec
|
|||
.iter()
|
||||
.map(|port| {
|
||||
Action::Playing(PlayingAction::Construct(
|
||||
Construct::new(city.position, *building, payment(&cost.cost, p))
|
||||
.with_port_position(*port),
|
||||
Construct::new(
|
||||
city.position,
|
||||
*building,
|
||||
payment(ai_actions, &cost.cost, p),
|
||||
)
|
||||
.with_port_position(*port),
|
||||
))
|
||||
})
|
||||
.collect_vec()
|
||||
|
|
|
|||
170
server/src/ai_collect.rs
Normal file
170
server/src/ai_collect.rs
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
use crate::ability_initializer::AbilityInitializerSetup;
|
||||
use crate::advance::Advance;
|
||||
use crate::city::City;
|
||||
use crate::collect::{
|
||||
CollectInfo, PositionCollection, add_collect, apply_total_collect,
|
||||
possible_resource_collections, tiles_used,
|
||||
};
|
||||
use crate::content::builtin::Builtin;
|
||||
use crate::game::Game;
|
||||
use crate::player::{CostTrigger, Player};
|
||||
use crate::playing_actions::{Collect, PlayingActionType};
|
||||
use crate::position::Position;
|
||||
use crate::resource::ResourceType;
|
||||
use crate::resource_pile::ResourcePile;
|
||||
use itertools::Itertools;
|
||||
|
||||
pub fn set_city_collections(game: &mut Game, city_position: Position) {
|
||||
let city = game.get_any_city(city_position);
|
||||
let player = city.player_index;
|
||||
let p = city_collections_uncached(game, game.player(player), city);
|
||||
game.player_mut(player)
|
||||
.get_city_mut(city_position)
|
||||
.possible_collections
|
||||
.clone_from(&p);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn city_collections_uncached(game: &Game, player: &Player, city: &City) -> Vec<Collect> {
|
||||
let info = possible_resource_collections(
|
||||
game,
|
||||
city.position,
|
||||
player.index,
|
||||
&[],
|
||||
CostTrigger::NoModifiers,
|
||||
);
|
||||
|
||||
let all = ResourceType::all()
|
||||
.into_iter()
|
||||
.filter(|r| {
|
||||
info.choices
|
||||
.iter()
|
||||
.any(|(_, choices)| choices.iter().any(|pile| pile.get(r) > 0))
|
||||
})
|
||||
.collect_vec();
|
||||
let l = all.len();
|
||||
all.into_iter()
|
||||
.permutations(l)
|
||||
.filter_map(|priority| city_collection_uncached(game, player, city, &priority))
|
||||
.unique_by(|c| c.total.clone())
|
||||
.collect_vec()
|
||||
}
|
||||
|
||||
fn city_collection_uncached(
|
||||
game: &Game,
|
||||
player: &Player,
|
||||
city: &City,
|
||||
priority: &[ResourceType],
|
||||
) -> Option<Collect> {
|
||||
let mut c: Vec<PositionCollection> = vec![];
|
||||
|
||||
loop {
|
||||
let info = possible_resource_collections(
|
||||
game,
|
||||
city.position,
|
||||
player.index,
|
||||
&c,
|
||||
CostTrigger::NoModifiers,
|
||||
);
|
||||
|
||||
let Some((pos, pile)) = pick_resource(&info, &c, priority) else {
|
||||
return apply_total_collect(&c, player, info, game)
|
||||
.ok()
|
||||
.map(|i| Collect::new(city.position, c, i.total, PlayingActionType::Collect));
|
||||
};
|
||||
c = add_collect(&info, pos, &pile, &c);
|
||||
}
|
||||
}
|
||||
|
||||
fn pick_resource(
|
||||
info: &CollectInfo,
|
||||
collected: &[PositionCollection],
|
||||
priority: &[ResourceType],
|
||||
) -> Option<(Position, ResourcePile)> {
|
||||
if info.max_selection == tiles_used(collected) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let used = collected
|
||||
.iter()
|
||||
.chunk_by(|c| c.position)
|
||||
.into_iter()
|
||||
.map(|(p, group)| (p, group.map(|c| c.times).sum::<u8>()))
|
||||
.collect_vec();
|
||||
|
||||
let available = info
|
||||
.choices
|
||||
.iter()
|
||||
// .sorted_by_key(|(pos, _)| **pos)
|
||||
.filter(|(pos, _)| {
|
||||
let u = used
|
||||
.iter()
|
||||
.find_map(|(p, u)| (*p == **pos).then_some(*u))
|
||||
.unwrap_or(0);
|
||||
|
||||
u < info.max_per_tile
|
||||
})
|
||||
.collect_vec();
|
||||
|
||||
priority.iter().find_map(|r| {
|
||||
available.iter().find_map(|(pos, choices)| {
|
||||
choices
|
||||
.iter()
|
||||
.find_map(|pile| (pile.get(r) > 0).then_some((**pos, pile.clone())))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn invalidate_collect_cache() -> Builtin {
|
||||
Builtin::builder("InvalidateCollectCache", "-")
|
||||
.add_simple_persistent_event_listener(
|
||||
|event| &mut event.found_city,
|
||||
1,
|
||||
|game, player, _, p| {
|
||||
reset_collect_within_range(game.player_mut(player), *p);
|
||||
},
|
||||
)
|
||||
.add_simple_persistent_event_listener(
|
||||
|event| &mut event.combat_end,
|
||||
0,
|
||||
|game, _player, _, p| {
|
||||
reset_collect_within_range_for_all(game, p.combat.defender_position);
|
||||
},
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
pub(crate) fn reset_collect_within_range(p: &mut Player, position: Position) {
|
||||
let has_husbandry = p.has_advance(Advance::Husbandry);
|
||||
let range = if has_husbandry { 2 } else { 1 };
|
||||
for c in &mut p.cities {
|
||||
if c.position.distance(position) <= range {
|
||||
c.possible_collections.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn reset_collect_within_range_for_all(game: &mut Game, pos: Position) {
|
||||
for p in &mut game.players {
|
||||
reset_collect_within_range(p, pos);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn reset_collect_within_range_for_all_except(
|
||||
game: &mut Game,
|
||||
pos: Position,
|
||||
player: usize,
|
||||
) {
|
||||
for p in &mut game.players {
|
||||
if p.index == player {
|
||||
continue;
|
||||
}
|
||||
reset_collect_within_range(p, pos);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn reset_collection_stats(p: &mut Player) {
|
||||
for c in &mut p.cities {
|
||||
c.possible_collections.clear();
|
||||
}
|
||||
}
|
||||
|
|
@ -226,10 +226,10 @@ pub enum MoodState {
|
|||
Angry = 0,
|
||||
}
|
||||
|
||||
impl Add<u32> for MoodState {
|
||||
impl Add<u8> for MoodState {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: u32) -> Self::Output {
|
||||
fn add(self, rhs: u8) -> Self::Output {
|
||||
match rhs {
|
||||
0 => self,
|
||||
1 => match self {
|
||||
|
|
@ -241,10 +241,10 @@ impl Add<u32> for MoodState {
|
|||
}
|
||||
}
|
||||
|
||||
impl Sub<u32> for MoodState {
|
||||
impl Sub<u8> for MoodState {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: u32) -> Self::Output {
|
||||
fn sub(self, rhs: u8) -> Self::Output {
|
||||
match rhs {
|
||||
0 => self,
|
||||
1 => match self {
|
||||
|
|
|
|||
|
|
@ -1,18 +1,14 @@
|
|||
use crate::ability_initializer::AbilityInitializerSetup;
|
||||
use crate::advance::Advance;
|
||||
use crate::city::City;
|
||||
use crate::content::builtin::Builtin;
|
||||
use crate::content::custom_actions::CustomActionType;
|
||||
use crate::content::persistent_events::PersistentEventType;
|
||||
use crate::events::EventOrigin;
|
||||
use crate::game::Game;
|
||||
use crate::map::Terrain;
|
||||
use crate::map::Terrain::{Fertile, Forest, Mountain};
|
||||
use crate::player::Player;
|
||||
use crate::player::{CostTrigger, Player};
|
||||
use crate::player_events::ActionInfo;
|
||||
use crate::playing_actions::{Collect, PlayingActionType, base_or_custom_available};
|
||||
use crate::position::Position;
|
||||
use crate::resource::ResourceType;
|
||||
use crate::resource_pile::ResourcePile;
|
||||
use itertools::Itertools;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
|
@ -23,7 +19,7 @@ use std::iter;
|
|||
pub struct PositionCollection {
|
||||
pub position: Position,
|
||||
pub pile: ResourcePile,
|
||||
pub times: u32,
|
||||
pub times: u8,
|
||||
}
|
||||
|
||||
impl PositionCollection {
|
||||
|
|
@ -37,7 +33,7 @@ impl PositionCollection {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn times(&self, times: u32) -> PositionCollection {
|
||||
pub fn times(&self, times: u8) -> PositionCollection {
|
||||
PositionCollection {
|
||||
position: self.position,
|
||||
pile: self.pile.clone(),
|
||||
|
|
@ -60,6 +56,7 @@ pub fn get_total_collection(
|
|||
player_index: usize,
|
||||
city_position: Position,
|
||||
collections: &[PositionCollection],
|
||||
trigger: CostTrigger,
|
||||
) -> Result<CollectInfo, String> {
|
||||
let player = &game.players[player_index];
|
||||
let city = player.get_city(city_position);
|
||||
|
|
@ -68,7 +65,7 @@ pub fn get_total_collection(
|
|||
return Err("Not your city".to_string());
|
||||
}
|
||||
|
||||
let i = possible_resource_collections(game, city_position, player_index, &Vec::new());
|
||||
let i = possible_resource_collections(game, city_position, player_index, &Vec::new(), trigger);
|
||||
if i.max_selection < tiles_used(collections) {
|
||||
return Err(format!(
|
||||
"You can only collect {} resources at {city_position} - got {}",
|
||||
|
|
@ -78,7 +75,7 @@ pub fn get_total_collection(
|
|||
}
|
||||
|
||||
for (_, group) in &collections.iter().chunk_by(|c| c.position) {
|
||||
let used = group.map(|c| c.times).sum::<u32>();
|
||||
let used = group.map(|c| c.times).sum::<u8>();
|
||||
|
||||
if used > i.max_per_tile {
|
||||
return Err(format!(
|
||||
|
|
@ -101,7 +98,7 @@ pub fn get_total_collection(
|
|||
apply_total_collect(collections, player, i, game)
|
||||
}
|
||||
|
||||
fn apply_total_collect(
|
||||
pub(crate) fn apply_total_collect(
|
||||
collections: &[PositionCollection],
|
||||
player: &Player,
|
||||
mut i: CollectInfo,
|
||||
|
|
@ -122,12 +119,18 @@ fn apply_total_collect(
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn tiles_used(collections: &[PositionCollection]) -> u32 {
|
||||
pub fn tiles_used(collections: &[PositionCollection]) -> u8 {
|
||||
collections.iter().map(|c| c.times).sum()
|
||||
}
|
||||
|
||||
pub(crate) fn collect(game: &mut Game, player_index: usize, c: &Collect) -> Result<(), String> {
|
||||
let i = get_total_collection(game, player_index, c.city_position, &c.collections)?;
|
||||
let mut i = get_total_collection(
|
||||
game,
|
||||
player_index,
|
||||
c.city_position,
|
||||
&c.collections,
|
||||
game.execute_cost_trigger(),
|
||||
)?;
|
||||
let city = game.players[player_index].get_city_mut(c.city_position);
|
||||
if !city.can_activate() {
|
||||
return Err("City can't be activated".to_string());
|
||||
|
|
@ -135,6 +138,18 @@ pub(crate) fn collect(game: &mut Game, player_index: usize, c: &Collect) -> Resu
|
|||
city.activate();
|
||||
game.players[player_index].gain_resources(i.total.clone());
|
||||
|
||||
let key = Advance::Husbandry.to_string();
|
||||
if i.info.info.contains_key(&key) {
|
||||
// check if husbandry is used
|
||||
let used = c
|
||||
.collections
|
||||
.iter()
|
||||
.any(|p| p.position.distance(c.city_position) > 1);
|
||||
if !used {
|
||||
i.info.info.remove(&key);
|
||||
}
|
||||
}
|
||||
|
||||
on_collect(game, player_index, i);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -165,8 +180,8 @@ pub struct CollectInfo {
|
|||
pub modifiers: Vec<EventOrigin>,
|
||||
pub city: Position,
|
||||
pub total: ResourcePile,
|
||||
pub max_per_tile: u32,
|
||||
pub max_selection: u32,
|
||||
pub max_per_tile: u8,
|
||||
pub max_selection: u8,
|
||||
pub(crate) info: ActionInfo,
|
||||
}
|
||||
|
||||
|
|
@ -183,7 +198,7 @@ impl CollectInfo {
|
|||
info: ActionInfo::new(player),
|
||||
city,
|
||||
max_per_tile: 1,
|
||||
max_selection: player.get_city(city).mood_modified_size(player) as u32,
|
||||
max_selection: player.get_city(city).mood_modified_size(player) as u8,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -197,6 +212,7 @@ pub fn possible_resource_collections(
|
|||
city_pos: Position,
|
||||
player_index: usize,
|
||||
used: &[PositionCollection],
|
||||
trigger: CostTrigger,
|
||||
) -> CollectInfo {
|
||||
let set = [
|
||||
(Mountain, HashSet::from([ResourcePile::ore(1)])),
|
||||
|
|
@ -209,9 +225,10 @@ pub fn possible_resource_collections(
|
|||
.events
|
||||
.transient
|
||||
.terrain_collect_options;
|
||||
let modifiers = event
|
||||
.get()
|
||||
.trigger_with_modifiers(&mut terrain_options, &(), &(), &mut ());
|
||||
let modifiers =
|
||||
event
|
||||
.get()
|
||||
.trigger_with_modifiers(&mut terrain_options, &(), &(), &mut (), trigger);
|
||||
|
||||
let collect_options = city_pos
|
||||
.neighbors()
|
||||
|
|
@ -308,146 +325,3 @@ pub fn available_collect_actions(game: &Game, player: usize) -> Vec<PlayingActio
|
|||
&CustomActionType::FreeEconomyCollect,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn set_city_collections(game: &mut Game, city_position: Position) {
|
||||
let city = game.get_any_city(city_position);
|
||||
let player = city.player_index;
|
||||
let p = city_collections_uncached(game, game.player(player), city);
|
||||
game.player_mut(player)
|
||||
.get_city_mut(city_position)
|
||||
.possible_collections
|
||||
.clone_from(&p);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn city_collections_uncached(game: &Game, player: &Player, city: &City) -> Vec<Collect> {
|
||||
let info = possible_resource_collections(game, city.position, player.index, &[]);
|
||||
|
||||
let all = ResourceType::all()
|
||||
.into_iter()
|
||||
.filter(|r| {
|
||||
info.choices
|
||||
.iter()
|
||||
.any(|(_, choices)| choices.iter().any(|pile| pile.get(r) > 0))
|
||||
})
|
||||
.collect_vec();
|
||||
let l = all.len();
|
||||
all.into_iter()
|
||||
.permutations(l)
|
||||
.filter_map(|priority| city_collection_uncached(game, player, city, &priority))
|
||||
.unique_by(|c| c.total.clone())
|
||||
.collect_vec()
|
||||
}
|
||||
|
||||
fn city_collection_uncached(
|
||||
game: &Game,
|
||||
player: &Player,
|
||||
city: &City,
|
||||
priority: &[ResourceType],
|
||||
) -> Option<Collect> {
|
||||
let mut c: Vec<PositionCollection> = vec![];
|
||||
|
||||
loop {
|
||||
let info = possible_resource_collections(game, city.position, player.index, &c);
|
||||
|
||||
let Some((pos, pile)) = pick_resource(&info, &c, priority) else {
|
||||
return apply_total_collect(&c, player, info, game)
|
||||
.ok()
|
||||
.map(|i| Collect::new(city.position, c, i.total, PlayingActionType::Collect));
|
||||
};
|
||||
c = add_collect(&info, pos, &pile, &c);
|
||||
}
|
||||
}
|
||||
|
||||
fn pick_resource(
|
||||
info: &CollectInfo,
|
||||
collected: &[PositionCollection],
|
||||
priority: &[ResourceType],
|
||||
) -> Option<(Position, ResourcePile)> {
|
||||
if info.max_selection == tiles_used(collected) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let used = collected
|
||||
.iter()
|
||||
.chunk_by(|c| c.position)
|
||||
.into_iter()
|
||||
.map(|(p, group)| (p, group.map(|c| c.times).sum::<u32>()))
|
||||
.collect_vec();
|
||||
|
||||
let available = info
|
||||
.choices
|
||||
.iter()
|
||||
// .sorted_by_key(|(pos, _)| **pos)
|
||||
.filter(|(pos, _)| {
|
||||
let u = used
|
||||
.iter()
|
||||
.find_map(|(p, u)| (*p == **pos).then_some(*u))
|
||||
.unwrap_or(0);
|
||||
|
||||
u < info.max_per_tile
|
||||
})
|
||||
.collect_vec();
|
||||
|
||||
priority.iter().find_map(|r| {
|
||||
available.iter().find_map(|(pos, choices)| {
|
||||
choices
|
||||
.iter()
|
||||
.find_map(|pile| (pile.get(r) > 0).then_some((**pos, pile.clone())))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn invalidate_collect_cache() -> Builtin {
|
||||
Builtin::builder("InvalidateCollectCache", "-")
|
||||
.add_simple_persistent_event_listener(
|
||||
|event| &mut event.found_city,
|
||||
1,
|
||||
|game, player, _, p| {
|
||||
reset_collect_within_range(game.player_mut(player), *p);
|
||||
},
|
||||
)
|
||||
.add_simple_persistent_event_listener(
|
||||
|event| &mut event.combat_end,
|
||||
0,
|
||||
|game, _player, _, p| {
|
||||
reset_collect_within_range_for_all(game, p.combat.defender_position);
|
||||
},
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
pub(crate) fn reset_collect_within_range(p: &mut Player, position: Position) {
|
||||
let has_husbandry = p.has_advance(Advance::Husbandry);
|
||||
let range = if has_husbandry { 2 } else { 1 };
|
||||
for c in &mut p.cities {
|
||||
if c.position.distance(position) <= range {
|
||||
c.possible_collections.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn reset_collect_within_range_for_all(game: &mut Game, pos: Position) {
|
||||
for p in &mut game.players {
|
||||
reset_collect_within_range(p, pos);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn reset_collect_within_range_for_all_except(
|
||||
game: &mut Game,
|
||||
pos: Position,
|
||||
player: usize,
|
||||
) {
|
||||
for p in &mut game.players {
|
||||
if p.index == player {
|
||||
continue;
|
||||
}
|
||||
reset_collect_within_range(p, pos);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn reset_collection_stats(p: &mut Player) {
|
||||
for c in &mut p.cities {
|
||||
c.possible_collections.clear();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -337,7 +337,7 @@ pub(crate) fn conquer_city(
|
|||
let attacker_is_human = game.player(new_player_index).is_human();
|
||||
let size = city.mood_modified_size(&game.players[new_player_index]);
|
||||
if attacker_is_human {
|
||||
game.players[new_player_index].gain_resources(ResourcePile::gold(size as u32));
|
||||
game.players[new_player_index].gain_resources(ResourcePile::gold(size as u8));
|
||||
}
|
||||
let take_over = game.player(new_player_index).is_city_available();
|
||||
|
||||
|
|
@ -370,7 +370,7 @@ pub(crate) fn conquer_city(
|
|||
}
|
||||
game.players[new_player_index].cities.push(city);
|
||||
} else {
|
||||
game.players[new_player_index].gain_resources(ResourcePile::gold(city.size() as u32));
|
||||
game.players[new_player_index].gain_resources(ResourcePile::gold(city.size() as u8));
|
||||
city.raze(game, old_player_index);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use crate::consts::MAX_CITY_PIECES;
|
|||
use crate::content::persistent_events::PersistentEventType;
|
||||
use crate::game::Game;
|
||||
use crate::map::Terrain;
|
||||
use crate::player::Player;
|
||||
use crate::player::{CostTrigger, Player};
|
||||
use crate::player_events::CostInfo;
|
||||
use crate::position::Position;
|
||||
use crate::resource_pile::ResourcePile;
|
||||
|
|
@ -48,6 +48,7 @@ pub fn can_construct(
|
|||
building: Building,
|
||||
player: &Player,
|
||||
game: &Game,
|
||||
trigger: CostTrigger,
|
||||
) -> Result<CostInfo, String> {
|
||||
let advance = cache::get().get_building_advance(building);
|
||||
if !player.has_advance(advance) {
|
||||
|
|
@ -67,7 +68,7 @@ pub fn can_construct(
|
|||
if !player.is_building_available(building, game) {
|
||||
return Err("All non-destroyed buildings are built".to_string());
|
||||
}
|
||||
let cost_info = player.building_cost(game, building, None);
|
||||
let cost_info = player.building_cost(game, building, trigger);
|
||||
if !player.can_afford(&cost_info.cost) {
|
||||
// construct cost event listener?
|
||||
return Err("Not enough resources".to_string());
|
||||
|
|
@ -92,7 +93,13 @@ pub(crate) fn can_construct_anything(city: &City, player: &Player) -> Result<(),
|
|||
pub(crate) fn construct(game: &mut Game, player_index: usize, c: &Construct) -> Result<(), String> {
|
||||
let player = &game.players[player_index];
|
||||
let city = player.get_city(c.city_position);
|
||||
let cost = can_construct(city, c.city_piece, player, game)?;
|
||||
let cost = can_construct(
|
||||
city,
|
||||
c.city_piece,
|
||||
player,
|
||||
game,
|
||||
game.execute_cost_trigger(),
|
||||
)?;
|
||||
if matches!(c.city_piece, Building::Port) {
|
||||
let port_position = c.port_position.as_ref().expect("Illegal action");
|
||||
assert!(
|
||||
|
|
@ -132,7 +139,11 @@ pub fn available_buildings(
|
|||
let city = player.get_city(city);
|
||||
Building::all()
|
||||
.into_iter()
|
||||
.filter_map(|b| can_construct(city, b, player, game).ok().map(|i| (b, i)))
|
||||
.filter_map(|b| {
|
||||
can_construct(city, b, player, game, CostTrigger::NoModifiers)
|
||||
.ok()
|
||||
.map(|i| (b, i))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use crate::{resource_pile::ResourcePile, unit::Units};
|
|||
|
||||
pub const MAX_CITY_PIECES: usize = 4; // i.e. city size 5
|
||||
pub const AGES: u32 = 6;
|
||||
pub const ADVANCE_COST: u32 = 2;
|
||||
pub const ADVANCE_COST: u8 = 2;
|
||||
pub const BUILDING_VICTORY_POINTS: f32 = 1.0;
|
||||
pub const ADVANCE_VICTORY_POINTS: f32 = 0.5;
|
||||
pub const OBJECTIVE_VICTORY_POINTS: f32 = 2.0;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::ability_initializer::AbilityInitializerSetup;
|
||||
use crate::action_card::ActionCard;
|
||||
use crate::collect::reset_collection_stats;
|
||||
use crate::ai_collect::reset_collection_stats;
|
||||
use crate::content::action_cards::cultural_takeover::cultural_takeover;
|
||||
use crate::content::action_cards::mercenaries::mercenaries;
|
||||
use crate::content::builtin::Builtin;
|
||||
|
|
|
|||
|
|
@ -255,12 +255,12 @@ fn ideas(id: u8, tactics_card: TacticsCardFactory) -> ActionCard {
|
|||
.build()
|
||||
}
|
||||
|
||||
fn academies(player: &Player) -> u32 {
|
||||
fn academies(player: &Player) -> u8 {
|
||||
player
|
||||
.cities
|
||||
.iter()
|
||||
.filter(|c| c.pieces.academy.is_some())
|
||||
.count() as u32
|
||||
.count() as u8
|
||||
}
|
||||
|
||||
fn great_ideas(id: u8, tactics_card: TacticsCardFactory) -> ActionCard {
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ pub(crate) fn mercenaries(id: u8, tactics_card: TacticsCardFactory) -> ActionCar
|
|||
|_game, _player, a| {
|
||||
Some(vec![PaymentRequest::new(
|
||||
PaymentOptions::sum(
|
||||
a.selected_positions.len() as u32,
|
||||
a.selected_positions.len() as u8,
|
||||
&[
|
||||
ResourceType::Food,
|
||||
ResourceType::Wood,
|
||||
|
|
@ -189,5 +189,5 @@ fn barbarian_army_positions_in_range2(game: &Game, player: &Player) -> Vec<Posit
|
|||
|
||||
fn max_mercenary_payment(player: &Player) -> u8 {
|
||||
let pile = &player.resources;
|
||||
(pile.food + pile.wood + pile.ore + pile.culture_tokens + pile.gold) as u8
|
||||
pile.food + pile.wood + pile.ore + pile.culture_tokens + pile.gold
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use crate::ability_initializer::AbilityInitializerSetup;
|
||||
use crate::action::Action;
|
||||
use crate::action_card::ActionCard;
|
||||
use crate::ai_collect::reset_collection_stats;
|
||||
use crate::city::City;
|
||||
use crate::collect::reset_collection_stats;
|
||||
use crate::content::builtin::Builtin;
|
||||
use crate::content::effects::{CollectEffect, PermanentEffect};
|
||||
use crate::content::incidents::great_diplomat::{DiplomaticRelations, Negotiations};
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ use crate::content::tactics_cards::{
|
|||
TacticsCardFactory, archers, defensive_formation, flanking, high_ground, high_morale, surprise,
|
||||
wedge_formation,
|
||||
};
|
||||
use crate::game::Game;
|
||||
use crate::player::{Player, add_unit};
|
||||
use crate::playing_actions::ActionCost;
|
||||
use crate::resource_pile::ResourcePile;
|
||||
|
|
@ -43,7 +44,7 @@ fn synergies(id: u8, tactics_card: TacticsCardFactory) -> ActionCard {
|
|||
"Gain 2 advances from the same category without changing the Game Event counter. \
|
||||
Pay the price as usual.",
|
||||
ActionCost::regular(),
|
||||
move |_game, p, _| !categories_with_2_affordable_advances(p).is_empty(),
|
||||
move |game, p, _| !categories_with_2_affordable_advances(p, game).is_empty(),
|
||||
)
|
||||
.tactics_card(tactics_card)
|
||||
.add_advance_request(
|
||||
|
|
@ -52,6 +53,7 @@ fn synergies(id: u8, tactics_card: TacticsCardFactory) -> ActionCard {
|
|||
|game, p, _| {
|
||||
Some(AdvanceRequest::new(categories_with_2_affordable_advances(
|
||||
game.player(p),
|
||||
game,
|
||||
)))
|
||||
},
|
||||
|game, sel, i| {
|
||||
|
|
@ -103,7 +105,7 @@ fn pay_for_advance(b: ActionCardBuilder, priority: i32) -> ActionCardBuilder {
|
|||
let p = game.player(player_index);
|
||||
let advance = i.selected_advance.expect("advance not found");
|
||||
Some(vec![PaymentRequest::new(
|
||||
p.advance_cost(advance, None).cost,
|
||||
p.advance_cost(advance, game.execute_cost_trigger()).cost,
|
||||
&format!("Pay for {}", advance.info().name),
|
||||
false,
|
||||
)])
|
||||
|
|
@ -119,7 +121,7 @@ fn pay_for_advance(b: ActionCardBuilder, priority: i32) -> ActionCardBuilder {
|
|||
)
|
||||
}
|
||||
|
||||
fn categories_with_2_affordable_advances(p: &Player) -> Vec<Advance> {
|
||||
fn categories_with_2_affordable_advances(p: &Player, game: &Game) -> Vec<Advance> {
|
||||
advances::get_groups()
|
||||
.iter()
|
||||
.flat_map(|g| {
|
||||
|
|
@ -136,8 +138,11 @@ fn categories_with_2_affordable_advances(p: &Player) -> Vec<Advance> {
|
|||
.filter(|pair| {
|
||||
let a = pair[0];
|
||||
let b = pair[1];
|
||||
let mut cost = p.advance_cost(a.advance, None).cost;
|
||||
cost.default += p.advance_cost(b.advance, None).cost.default;
|
||||
let mut cost = p.advance_cost(a.advance, game.execute_cost_trigger()).cost;
|
||||
cost.default += p
|
||||
.advance_cost(b.advance, game.execute_cost_trigger())
|
||||
.cost
|
||||
.default;
|
||||
p.can_afford(&cost)
|
||||
&& p.can_advance_free(a.advance)
|
||||
&& p.can_advance_free(b.advance)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::ability_initializer::AbilityInitializerSetup;
|
||||
use crate::ability_initializer::{AbilityInitializerSetup, once_per_turn_advance};
|
||||
use crate::advance::Bonus::MoodToken;
|
||||
use crate::advance::{Advance, AdvanceBuilder, AdvanceInfo};
|
||||
use crate::collect::{CollectContext, CollectInfo};
|
||||
|
|
@ -63,12 +63,19 @@ fn husbandry() -> AdvanceBuilder {
|
|||
spaces away. This Advance can only be used once per turn.",
|
||||
)
|
||||
.with_advance_bonus(MoodToken)
|
||||
.with_reset_collect_stats()
|
||||
.add_once_per_turn_listener(
|
||||
.add_transient_event_listener(
|
||||
|event| &mut event.collect_options,
|
||||
0,
|
||||
husbandry_collect,
|
||||
|i| &mut i.info.info,
|
||||
|i, c, game| {
|
||||
once_per_turn_advance(
|
||||
Advance::Husbandry,
|
||||
i,
|
||||
c,
|
||||
game,
|
||||
|i| &mut i.info.info,
|
||||
husbandry_collect,
|
||||
);
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -129,7 +129,7 @@ pub fn tax_options(player: &Player) -> PaymentOptions {
|
|||
if player.has_advance(Advance::Currency) {
|
||||
c.insert(0, ResourceType::Gold);
|
||||
}
|
||||
PaymentOptions::sum(player.cities.len() as u32, &c)
|
||||
PaymentOptions::sum(player.cities.len() as u8, &c)
|
||||
}
|
||||
|
||||
fn trade_routes() -> AdvanceBuilder {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::ability_initializer::{AbilityInitializerSetup, do_once_per_turn};
|
||||
use crate::ability_initializer::{AbilityInitializerSetup, once_per_turn_advance};
|
||||
use crate::action_card::gain_action_card_from_pile;
|
||||
use crate::advance::Bonus::{CultureToken, MoodToken};
|
||||
use crate::advance::{Advance, AdvanceBuilder, AdvanceInfo};
|
||||
|
|
@ -58,8 +58,8 @@ fn public_education() -> AdvanceBuilder {
|
|||
|i, game, ()| {
|
||||
let city = game.get_any_city(i.city);
|
||||
if city.pieces.academy.is_some() {
|
||||
do_once_per_turn(
|
||||
"Public Education",
|
||||
once_per_turn_advance(
|
||||
Advance::PublicEducation,
|
||||
i,
|
||||
&(),
|
||||
&(),
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::ability_initializer::AbilityInitializerSetup;
|
||||
use crate::ability_initializer::{AbilityInitializerSetup, once_per_turn_advance};
|
||||
use crate::advance::Bonus::{CultureToken, MoodToken};
|
||||
use crate::advance::{Advance, AdvanceBuilder, AdvanceInfo};
|
||||
use crate::city_pieces::Building::Temple;
|
||||
|
|
@ -84,7 +84,7 @@ fn priesthood() -> AdvanceBuilder {
|
|||
"Priesthood",
|
||||
"Once per turn, a science advance is free",
|
||||
)
|
||||
.add_once_per_turn_listener(
|
||||
.add_transient_event_listener(
|
||||
|event| &mut event.advance_cost,
|
||||
2,
|
||||
|i, &advance, ()| {
|
||||
|
|
@ -93,13 +93,21 @@ fn priesthood() -> AdvanceBuilder {
|
|||
.iter()
|
||||
.any(|a| a.advance == advance)
|
||||
{
|
||||
i.set_zero();
|
||||
i.info
|
||||
.log
|
||||
.push("Priesthood reduced the cost to 0".to_string());
|
||||
once_per_turn_advance(
|
||||
Advance::Priesthood,
|
||||
i,
|
||||
&(),
|
||||
&(),
|
||||
|i| &mut i.info.info,
|
||||
|i, (), ()| {
|
||||
i.set_zero();
|
||||
i.info
|
||||
.log
|
||||
.push("Priesthood reduced the cost to 0".to_string());
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
|i| &mut i.info.info,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -110,21 +118,29 @@ fn state_religion() -> AdvanceBuilder {
|
|||
"Once per turn, when constructing a Temple, do not pay any Food.",
|
||||
)
|
||||
.with_advance_bonus(MoodToken)
|
||||
.add_once_per_turn_listener(
|
||||
.add_transient_event_listener(
|
||||
|event| &mut event.construct_cost,
|
||||
0,
|
||||
|i, b, _| {
|
||||
|i, &b, _| {
|
||||
if matches!(b, Temple) {
|
||||
i.cost.conversions.push(PaymentConversion::limited(
|
||||
ResourcePile::of(ResourceType::Food, 1),
|
||||
ResourcePile::empty(),
|
||||
1,
|
||||
));
|
||||
i.info
|
||||
.log
|
||||
.push("State Religion reduced the food cost to 0".to_string());
|
||||
once_per_turn_advance(
|
||||
Advance::StateReligion,
|
||||
i,
|
||||
&b,
|
||||
&(),
|
||||
|i| &mut i.info.info,
|
||||
|i, _, ()| {
|
||||
i.cost.conversions.push(PaymentConversion::limited(
|
||||
ResourcePile::of(ResourceType::Food, 1),
|
||||
ResourcePile::empty(),
|
||||
1,
|
||||
));
|
||||
i.info
|
||||
.log
|
||||
.push("State Religion reduced the food cost to 0".to_string());
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
|i| &mut i.info.info,
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,11 +26,11 @@ pub fn trade_route_reward(game: &Game) -> Option<(PaymentOptions, Vec<TradeRoute
|
|||
Some((
|
||||
if game.players[p].has_advance(Advance::Currency) {
|
||||
PaymentOptions::sum(
|
||||
trade_routes.len() as u32,
|
||||
trade_routes.len() as u8,
|
||||
&[ResourceType::Gold, ResourceType::Food],
|
||||
)
|
||||
} else {
|
||||
PaymentOptions::sum(trade_routes.len() as u32, &[ResourceType::Food])
|
||||
PaymentOptions::sum(trade_routes.len() as u8, &[ResourceType::Food])
|
||||
},
|
||||
trade_routes,
|
||||
))
|
||||
|
|
|
|||
|
|
@ -174,7 +174,7 @@ fn draft() -> AdvanceBuilder {
|
|||
)
|
||||
}
|
||||
|
||||
pub(crate) fn draft_cost(player: &Player) -> u32 {
|
||||
pub(crate) fn draft_cost(player: &Player) -> u8 {
|
||||
if player.has_advance(Advance::CivilLiberties) {
|
||||
2
|
||||
} else {
|
||||
|
|
@ -197,7 +197,7 @@ fn steel_weapons_cost(game: &Game, combat: &Combat, player_index: usize) -> Paym
|
|||
let defender = &game.players[combat.defender];
|
||||
let both_steel_weapons =
|
||||
attacker.has_advance(Advance::SteelWeapons) && defender.has_advance(Advance::SteelWeapons);
|
||||
let cost = u32::from(!player.has_advance(Advance::Metallurgy) || both_steel_weapons);
|
||||
let cost = u8::from(!player.has_advance(Advance::Metallurgy) || both_steel_weapons);
|
||||
PaymentOptions::sum(cost, &[ResourceType::Ore, ResourceType::Gold])
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use crate::ability_initializer::AbilityInitializerSetup;
|
||||
use crate::ability_initializer::{AbilityInitializerBuilder, AbilityListeners};
|
||||
use crate::ai_collect::invalidate_collect_cache;
|
||||
use crate::barbarians::barbarians_bonus;
|
||||
use crate::cache;
|
||||
use crate::collect::invalidate_collect_cache;
|
||||
use crate::combat_listeners::{
|
||||
choose_fighter_casualties, combat_stats, offer_retreat, place_settler,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ fn pestilence() -> Incident {
|
|||
2
|
||||
} else {
|
||||
1
|
||||
} - i.player.payment.amount() as u8;
|
||||
} - i.player.payment.amount();
|
||||
|
||||
(non_angry_cites(p), needed)
|
||||
},
|
||||
|
|
@ -207,9 +207,9 @@ pub(crate) fn famine(
|
|||
}
|
||||
|
||||
let needed = amount(p, game);
|
||||
let lost = needed.min(p.resources.food as u8) as u32;
|
||||
let lost = needed.min(p.resources.food);
|
||||
|
||||
if lost == needed as u32 {
|
||||
if lost == needed {
|
||||
// only avoid anger if full amount is paid
|
||||
i.player.payment = ResourcePile::food(lost);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -127,7 +127,7 @@ fn fantastic_years() -> Vec<Incident> {
|
|||
]
|
||||
}
|
||||
|
||||
fn good_year(mut builder: IncidentBuilder, amount: u32, good_year_type: &GoodYearType) -> Incident {
|
||||
fn good_year(mut builder: IncidentBuilder, amount: u8, good_year_type: &GoodYearType) -> Incident {
|
||||
let n = builder.name.clone();
|
||||
let role = match good_year_type {
|
||||
GoodYearType::AllPlayers => IncidentTarget::AllPlayers,
|
||||
|
|
@ -264,7 +264,7 @@ pub(crate) fn successful_year() -> Incident {
|
|||
let cities = game.players[player_index].cities.len();
|
||||
if cities == *min_cities {
|
||||
Some(ResourceRewardRequest::new(
|
||||
PaymentOptions::sum((max_cities - min_cities) as u32, &[ResourceType::Food]),
|
||||
PaymentOptions::sum((max_cities - min_cities) as u8, &[ResourceType::Food]),
|
||||
"-".to_string(),
|
||||
))
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -298,7 +298,9 @@ fn great_prophet() -> ActionCard {
|
|||
}
|
||||
|
||||
fn temple_cost(game: &Game, player: &Player) -> PaymentOptions {
|
||||
player.building_cost(game, Building::Temple, None).cost
|
||||
player
|
||||
.building_cost(game, Building::Temple, game.execute_cost_trigger())
|
||||
.cost
|
||||
}
|
||||
|
||||
pub(crate) fn great_person_description<S: AsRef<str>>(free_advance_groups: &[S]) -> String {
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ fn pandemics() -> Incident {
|
|||
}
|
||||
|
||||
Some(vec![PaymentRequest::new(
|
||||
PaymentOptions::sum(needed as u32, &ResourceType::all()),
|
||||
PaymentOptions::sum(needed, &ResourceType::all()),
|
||||
"Select resources to lose",
|
||||
false,
|
||||
)])
|
||||
|
|
@ -126,7 +126,7 @@ impl PandemicsContributions {
|
|||
max: vec![
|
||||
player.units.len() as u8,
|
||||
player.action_cards.len() as u8,
|
||||
player.resources.amount() as u8,
|
||||
player.resources.amount(),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ fn scientific_trade() -> Incident {
|
|||
ideas = ideas.max(2);
|
||||
}
|
||||
|
||||
let pile = ResourcePile::ideas(ideas as u32);
|
||||
let pile = ResourcePile::ideas(ideas as u8);
|
||||
player.gain_resources(pile.clone());
|
||||
game.add_info_log_item(&format!("{name} gained {pile}"));
|
||||
})
|
||||
|
|
@ -71,7 +71,7 @@ fn flourishing_trade() -> Incident {
|
|||
gold = gold.max(1);
|
||||
}
|
||||
|
||||
let pile = ResourcePile::gold(gold as u32);
|
||||
let pile = ResourcePile::gold(gold as u8);
|
||||
player.gain_resources(pile.clone());
|
||||
game.add_info_log_item(&format!("{name} gained {pile}"));
|
||||
})
|
||||
|
|
@ -107,7 +107,7 @@ fn era_of_stability() -> Incident {
|
|||
tokens = tokens.max(1);
|
||||
}
|
||||
Some(ResourceRewardRequest::new(
|
||||
PaymentOptions::tokens(tokens as u32),
|
||||
PaymentOptions::tokens(tokens as u8),
|
||||
"Select token to gain".to_string(),
|
||||
))
|
||||
},
|
||||
|
|
|
|||
|
|
@ -205,7 +205,7 @@ pub(crate) fn cultural_influence_resolution() -> Builtin {
|
|||
.build()
|
||||
}
|
||||
|
||||
fn influence_distance(game: &Game, src: Position, dst: Position) -> u32 {
|
||||
fn influence_distance(game: &Game, src: Position, dst: Position) -> u8 {
|
||||
astar(
|
||||
&src,
|
||||
|p| {
|
||||
|
|
@ -217,7 +217,7 @@ fn influence_distance(game: &Game, src: Position, dst: Position) -> u32 {
|
|||
|p| p.distance(dst),
|
||||
|&p| p == dst,
|
||||
)
|
||||
.map_or(u32::MAX, |(_path, len)| len)
|
||||
.map_or(u8::MAX, |(_path, len)| len as u8)
|
||||
}
|
||||
|
||||
///
|
||||
|
|
@ -379,7 +379,7 @@ fn affordable_start_city(
|
|||
player_index: usize,
|
||||
target_city: &City,
|
||||
action_type: &PlayingActionType,
|
||||
) -> Result<(Position, u32), String> {
|
||||
) -> Result<(Position, u8), String> {
|
||||
if target_city.player_index == player_index {
|
||||
Ok((target_city.position, 0))
|
||||
} else {
|
||||
|
|
@ -397,14 +397,14 @@ fn affordable_start_city(
|
|||
let min_cost = c
|
||||
.position
|
||||
.distance(target_city.position)
|
||||
.saturating_sub(c.size() as u32);
|
||||
.saturating_sub(c.size() as u32) as u8;
|
||||
if min_cost > available.culture_tokens {
|
||||
// avoid unnecessary calculations
|
||||
return None;
|
||||
}
|
||||
|
||||
let boost_cost = influence_distance(game, c.position, target_city.position)
|
||||
.saturating_sub(c.size() as u32);
|
||||
.saturating_sub(c.size() as u8);
|
||||
if boost_cost > available.culture_tokens {
|
||||
return None;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ impl EventOrigin {
|
|||
match self {
|
||||
EventOrigin::Advance(name)
|
||||
// can't call to_string, because cache is not constructed
|
||||
| EventOrigin::SpecialAdvance(name) => format!("{name:?}"),
|
||||
| EventOrigin::SpecialAdvance(name) => format!("{name:?}"),
|
||||
| EventOrigin::Wonder(name)
|
||||
| EventOrigin::Leader(name)
|
||||
| EventOrigin::Objective(name)
|
||||
|
|
@ -49,8 +49,8 @@ use crate::advance::Advance;
|
|||
use crate::content::action_cards::get_civil_card;
|
||||
use crate::content::incidents;
|
||||
use crate::content::tactics_cards::get_tactics_card;
|
||||
use crate::player::CostTrigger;
|
||||
use incidents::get_incident;
|
||||
use itertools::Itertools;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
type Listener<T, U, V, W> = (
|
||||
|
|
@ -120,8 +120,14 @@ where
|
|||
info: &U,
|
||||
details: &V,
|
||||
extra_value: &mut W,
|
||||
trigger: CostTrigger,
|
||||
) -> Vec<EventOrigin> {
|
||||
self.trigger_with_exclude(value, info, details, extra_value, &[])
|
||||
if trigger == CostTrigger::WithModifiers {
|
||||
self.trigger_with_exclude(value, info, details, extra_value, &[])
|
||||
} else {
|
||||
self.trigger(value, info, details, extra_value);
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn trigger(&self, value: &mut T, info: &U, details: &V, extra_value: &mut W) {
|
||||
|
|
@ -153,45 +159,6 @@ where
|
|||
}
|
||||
modifiers
|
||||
}
|
||||
|
||||
pub(crate) fn trigger_with_minimal_modifiers(
|
||||
&self,
|
||||
value: &T,
|
||||
info: &U,
|
||||
details: &V,
|
||||
extra_value: &mut W,
|
||||
mut is_ok: impl FnMut(&T) -> bool,
|
||||
set_modifiers: impl Fn(&mut T, Vec<EventOrigin>),
|
||||
) -> T
|
||||
where
|
||||
T: Clone + PartialEq,
|
||||
{
|
||||
let mut initial_value = value.clone();
|
||||
let initial_modifiers =
|
||||
self.trigger_with_modifiers(&mut initial_value, info, details, extra_value);
|
||||
// to see what's possible
|
||||
is_ok(&initial_value);
|
||||
|
||||
initial_modifiers
|
||||
.iter()
|
||||
.powerset()
|
||||
.find_map(|try_modifiers| {
|
||||
let mut v = value.clone();
|
||||
let mut exclude = initial_modifiers.clone();
|
||||
exclude.retain(|origin| !try_modifiers.contains(&origin));
|
||||
let m = self.trigger_with_exclude(&mut v, info, details, extra_value, &exclude);
|
||||
if is_ok(&v) {
|
||||
set_modifiers(&mut v, m);
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
set_modifiers(&mut initial_value, initial_modifiers);
|
||||
initial_value
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Event<T, U = (), V = (), W = ()> {
|
||||
|
|
@ -227,6 +194,7 @@ impl<T, U, V, W> Event<T, U, V, W> {
|
|||
mod tests {
|
||||
use super::{EventMut, EventOrigin};
|
||||
use crate::advance::Advance;
|
||||
use crate::player::CostTrigger;
|
||||
|
||||
#[test]
|
||||
fn mutable_event() {
|
||||
|
|
@ -256,7 +224,13 @@ mod tests {
|
|||
let mut item = 0;
|
||||
let addend = 2;
|
||||
let multiplier = 3;
|
||||
let modifiers = event.trigger_with_modifiers(&mut item, &addend, &multiplier, &mut ());
|
||||
let modifiers = event.trigger_with_modifiers(
|
||||
&mut item,
|
||||
&addend,
|
||||
&multiplier,
|
||||
&mut (),
|
||||
CostTrigger::WithModifiers,
|
||||
);
|
||||
assert_eq!(6, item);
|
||||
assert_eq!(
|
||||
vec![
|
||||
|
|
@ -269,55 +243,14 @@ mod tests {
|
|||
event.remove_listener_mut_by_key(&EventOrigin::Advance(multiply_value));
|
||||
let mut item = 0;
|
||||
let addend = 3;
|
||||
let modifiers = event.trigger_with_modifiers(&mut item, &addend, &0, &mut ());
|
||||
let modifiers = event.trigger_with_modifiers(
|
||||
&mut item,
|
||||
&addend,
|
||||
&0,
|
||||
&mut (),
|
||||
CostTrigger::WithModifiers,
|
||||
);
|
||||
assert_eq!(3, item);
|
||||
assert_eq!(vec![EventOrigin::Advance(add_constant)], modifiers);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_minimal_modifiers() {
|
||||
#[derive(Clone, PartialEq)]
|
||||
struct Info {
|
||||
pub value: i32,
|
||||
pub modifiers: Vec<EventOrigin>,
|
||||
}
|
||||
|
||||
let a = Advance::Arts;
|
||||
let b = Advance::Bartering;
|
||||
let c = Advance::Cartography;
|
||||
|
||||
let mut event = EventMut::new("test");
|
||||
event.add_listener_mut(
|
||||
|value: &mut Info, (), (), ()| value.value += 1,
|
||||
0,
|
||||
EventOrigin::Advance(a),
|
||||
);
|
||||
event.add_listener_mut(
|
||||
|value: &mut Info, (), (), ()| value.value += 2,
|
||||
1,
|
||||
EventOrigin::Advance(b),
|
||||
);
|
||||
event.add_listener_mut(
|
||||
|value: &mut Info, (), (), ()| value.value += 4,
|
||||
2,
|
||||
EventOrigin::Advance(c),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
vec![EventOrigin::Advance(c), EventOrigin::Advance(a)],
|
||||
event
|
||||
.trigger_with_minimal_modifiers(
|
||||
&Info {
|
||||
value: 0,
|
||||
modifiers: Vec::new(),
|
||||
},
|
||||
&(),
|
||||
&(),
|
||||
&mut (),
|
||||
|i| i.value == 5,
|
||||
|v, m| v.modifiers = m
|
||||
)
|
||||
.modifiers
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::ability_initializer::AbilityInitializerSetup;
|
||||
use crate::advance::Advance;
|
||||
use crate::collect::reset_collect_within_range_for_all;
|
||||
use crate::ai_collect::reset_collect_within_range_for_all;
|
||||
use crate::content::builtin::Builtin;
|
||||
use crate::content::persistent_events::{
|
||||
EventResponse, PersistentEventRequest, PersistentEventType,
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ use crate::log::{
|
|||
use crate::movement::MoveState;
|
||||
use crate::objective_card::present_instant_objective_cards;
|
||||
use crate::pirates::get_pirates_player;
|
||||
use crate::player::CostTrigger;
|
||||
use crate::player_events::{
|
||||
PersistentEvent, PersistentEventInfo, PersistentEvents, PlayerEvents, TransientEvents,
|
||||
};
|
||||
|
|
@ -45,7 +46,7 @@ pub struct Game {
|
|||
pub action_log_index: usize,
|
||||
pub log: Vec<Vec<String>>,
|
||||
pub undo_limit: usize,
|
||||
pub supports_undo: bool,
|
||||
pub supports_undo: bool, // if false: optimizend AI mode
|
||||
pub actions_left: u32,
|
||||
pub successful_cultural_influence: bool,
|
||||
pub round: u32, // starts at 1
|
||||
|
|
@ -398,6 +399,15 @@ impl Game {
|
|||
event(&mut self.players[player_index].events).set(e);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub(crate) fn execute_cost_trigger(&self) -> CostTrigger {
|
||||
if self.supports_undo {
|
||||
CostTrigger::WithModifiers
|
||||
} else {
|
||||
CostTrigger::NoModifiers
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn can_undo(&self) -> bool {
|
||||
self.supports_undo && self.undo_limit < self.action_log_index
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use crate::city::MoodState;
|
|||
use crate::content::custom_actions::CustomActionType;
|
||||
use crate::game::Game;
|
||||
use crate::payment::PaymentOptions;
|
||||
use crate::player::Player;
|
||||
use crate::player::{CostTrigger, Player};
|
||||
use crate::player_events::CostInfo;
|
||||
use crate::playing_actions::{PlayingActionType, base_or_custom_available};
|
||||
use crate::position::Position;
|
||||
|
|
@ -22,9 +22,10 @@ pub fn available_happiness_actions(game: &Game, player: usize) -> Vec<PlayingAct
|
|||
pub(crate) fn increase_happiness(
|
||||
game: &mut Game,
|
||||
player_index: usize,
|
||||
happiness_increases: &[(Position, u32)],
|
||||
happiness_increases: &[(Position, u8)],
|
||||
payment: Option<ResourcePile>,
|
||||
) {
|
||||
let trigger = game.execute_cost_trigger();
|
||||
let player = &mut game.players[player_index];
|
||||
let mut angry_activations = vec![];
|
||||
let mut step_sum = 0;
|
||||
|
|
@ -33,7 +34,7 @@ pub(crate) fn increase_happiness(
|
|||
if steps == 0 {
|
||||
continue;
|
||||
}
|
||||
step_sum += steps * city.size() as u32;
|
||||
step_sum += steps * city.size() as u8;
|
||||
|
||||
if city.mood_state == MoodState::Angry {
|
||||
angry_activations.push(city_position);
|
||||
|
|
@ -45,15 +46,15 @@ pub(crate) fn increase_happiness(
|
|||
}
|
||||
|
||||
if let Some(r) = payment {
|
||||
happiness_cost(player, step_sum, Some(&r)).pay(game, &r);
|
||||
happiness_cost(player, step_sum, trigger).pay(game, &r);
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn happiness_cost(
|
||||
p: &Player,
|
||||
city_size_steps: u32, // for each city: size * steps in that city
|
||||
execute: Option<&ResourcePile>,
|
||||
city_size_steps: u8, // for each city: size * steps in that city
|
||||
execute: CostTrigger,
|
||||
) -> CostInfo {
|
||||
let payment_options = PaymentOptions::sum(city_size_steps, &[ResourceType::MoodTokens]);
|
||||
p.trigger_cost_event(|e| &e.happiness_cost, &payment_options, &(), &(), execute)
|
||||
|
|
|
|||
|
|
@ -2,10 +2,10 @@ use crate::ability_initializer::{AbilityInitializerBuilder, AbilityListeners};
|
|||
use crate::ability_initializer::{AbilityInitializerSetup, SelectedChoice};
|
||||
use crate::action_card::ActionCard;
|
||||
use crate::advance::Advance;
|
||||
use crate::ai_collect::reset_collect_within_range_for_all;
|
||||
use crate::barbarians::{barbarians_move, barbarians_spawn};
|
||||
use crate::card::{HandCard, draw_card_from_pile};
|
||||
use crate::city::{MoodState, is_valid_city_terrain};
|
||||
use crate::collect::reset_collect_within_range_for_all;
|
||||
use crate::content::incidents;
|
||||
use crate::content::incidents::great_persons::GREAT_PERSON_OFFSET;
|
||||
use crate::content::persistent_events::{
|
||||
|
|
@ -469,17 +469,15 @@ impl IncidentBuilder {
|
|||
+ Send,
|
||||
) -> Self {
|
||||
let cities2 = cities.clone();
|
||||
self.add_myths_payment(target, mood_modifier, move |g, p, i| {
|
||||
cities(p, g, i).1 as u32
|
||||
})
|
||||
.decrease_mood(target, mood_modifier, cities2)
|
||||
self.add_myths_payment(target, mood_modifier, move |g, p, i| cities(p, g, i).1)
|
||||
.decrease_mood(target, mood_modifier, cities2)
|
||||
}
|
||||
|
||||
fn add_myths_payment(
|
||||
self,
|
||||
target: IncidentTarget,
|
||||
mood_modifier: MoodModifier,
|
||||
amount: impl Fn(&Game, &Player, &IncidentInfo) -> u32 + 'static + Clone + Sync + Send,
|
||||
amount: impl Fn(&Game, &Player, &IncidentInfo) -> u8 + 'static + Clone + Sync + Send,
|
||||
) -> Self {
|
||||
self.add_incident_payment_request(
|
||||
target,
|
||||
|
|
@ -514,7 +512,7 @@ impl IncidentBuilder {
|
|||
},
|
||||
move |game, s, i| {
|
||||
let pile = &s.choice[0];
|
||||
i.player.myths_payment = pile.amount() as u8;
|
||||
i.player.myths_payment = pile.amount();
|
||||
game.add_info_log_item(&format!(
|
||||
"{} paid {pile} to avoid the mood change using Myths",
|
||||
s.player_name
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ pub mod action_card;
|
|||
pub mod advance;
|
||||
pub mod ai;
|
||||
pub mod ai_actions;
|
||||
pub mod ai_collect;
|
||||
mod barbarians;
|
||||
pub mod cache;
|
||||
pub mod card;
|
||||
|
|
|
|||
|
|
@ -222,7 +222,7 @@ pub fn format_happiness_increase(
|
|||
pub(crate) fn format_city_happiness_increase(
|
||||
player: &Player,
|
||||
position: Position,
|
||||
steps: u32,
|
||||
steps: u8,
|
||||
) -> String {
|
||||
format!(
|
||||
"the city at {position} by {steps} steps, making it {:?}",
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@ use serde::{Deserialize, Serialize};
|
|||
use std::fmt::Display;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
|
||||
pub enum PaymentConversionType {
|
||||
Unlimited,
|
||||
MayOverpay(u32),
|
||||
MayNotOverpay(u32),
|
||||
MayOverpay(u8),
|
||||
MayNotOverpay(u8),
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
|
||||
pub struct PaymentConversion {
|
||||
pub from: Vec<ResourcePile>, // alternatives
|
||||
pub to: ResourcePile,
|
||||
|
|
@ -28,7 +28,7 @@ impl PaymentConversion {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn limited(from: ResourcePile, to: ResourcePile, limit: u32) -> Self {
|
||||
pub fn limited(from: ResourcePile, to: ResourcePile, limit: u8) -> Self {
|
||||
PaymentConversion::new(vec![from], to, PaymentConversionType::MayNotOverpay(limit))
|
||||
}
|
||||
|
||||
|
|
@ -46,7 +46,7 @@ impl PaymentConversion {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
|
||||
pub struct PaymentOptions {
|
||||
pub default: ResourcePile,
|
||||
#[serde(default)]
|
||||
|
|
@ -74,7 +74,7 @@ impl PaymentOptions {
|
|||
None
|
||||
}
|
||||
})
|
||||
.sum::<u32>();
|
||||
.sum::<u8>();
|
||||
if discount_left == 0 && available.has_at_least(&self.default) {
|
||||
return Some(self.default.clone());
|
||||
}
|
||||
|
|
@ -112,7 +112,7 @@ impl PaymentOptions {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub(crate) fn sum(cost: u32, types_by_preference: &[ResourceType]) -> Self {
|
||||
pub(crate) fn sum(cost: u8, types_by_preference: &[ResourceType]) -> Self {
|
||||
let mut conversions = vec![];
|
||||
types_by_preference.windows(2).for_each(|pair| {
|
||||
conversions.push(PaymentConversion::unlimited(
|
||||
|
|
@ -128,7 +128,7 @@ impl PaymentOptions {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub(crate) fn single_type(t: ResourceType, r: RangeInclusive<u32>) -> PaymentOptions {
|
||||
pub(crate) fn single_type(t: ResourceType, r: RangeInclusive<u8>) -> PaymentOptions {
|
||||
let max = r.clone().max().expect("range empty");
|
||||
let d = max - r.min().expect("range empty");
|
||||
PaymentOptions {
|
||||
|
|
@ -143,7 +143,7 @@ impl PaymentOptions {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub(crate) fn tokens(cost: u32) -> Self {
|
||||
pub(crate) fn tokens(cost: u8) -> Self {
|
||||
Self::sum(
|
||||
cost,
|
||||
&[ResourceType::MoodTokens, ResourceType::CultureTokens],
|
||||
|
|
@ -242,7 +242,7 @@ pub fn can_convert(
|
|||
current: &ResourcePile,
|
||||
conversions: &[&PaymentConversion],
|
||||
skip_from: usize,
|
||||
discount_left: u32,
|
||||
discount_left: u8,
|
||||
may_overpay: bool,
|
||||
) -> Option<ResourcePile> {
|
||||
if available.has_at_least(current) && (discount_left == 0 || may_overpay) {
|
||||
|
|
@ -266,7 +266,7 @@ pub fn can_convert(
|
|||
let from = &conversion.from[skip_from];
|
||||
|
||||
let upper_limit = match conversion.payment_conversion_type {
|
||||
PaymentConversionType::Unlimited => u32::MAX,
|
||||
PaymentConversionType::Unlimited => u8::MAX,
|
||||
PaymentConversionType::MayOverpay(i) | PaymentConversionType::MayNotOverpay(i) => i,
|
||||
};
|
||||
|
||||
|
|
@ -334,8 +334,8 @@ mod tests {
|
|||
};
|
||||
let available = ResourcePile::wood(1) + ResourcePile::ore(1);
|
||||
assert_eq!(
|
||||
cost.first_valid_payment(&available),
|
||||
Some(ResourcePile::wood(1)),
|
||||
cost.first_valid_payment(&available)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ pub(crate) fn pirates_round_bonus() -> Builtin {
|
|||
{
|
||||
let hits = r.casualties(CombatRole::Defender).fighters as u32;
|
||||
Some(ResourceRewardRequest::new(
|
||||
PaymentOptions::sum(hits, &[ResourceType::Gold]),
|
||||
PaymentOptions::sum(hits as u8, &[ResourceType::Gold]),
|
||||
"-".to_string(),
|
||||
))
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::advance::Advance;
|
||||
use crate::ai_collect::reset_collect_within_range_for_all_except;
|
||||
use crate::city_pieces::{DestroyedStructures, DestroyedStructuresData};
|
||||
use crate::collect::reset_collect_within_range_for_all_except;
|
||||
use crate::consts::{UNIT_LIMIT_BARBARIANS, UNIT_LIMIT_PIRATES};
|
||||
use crate::content::builtin;
|
||||
use crate::events::{Event, EventOrigin};
|
||||
|
|
@ -90,6 +90,12 @@ impl PartialEq for Player {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Copy, Clone)]
|
||||
pub enum CostTrigger {
|
||||
WithModifiers,
|
||||
NoModifiers,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
///
|
||||
///
|
||||
|
|
@ -425,7 +431,7 @@ impl Player {
|
|||
self.resources -= resources;
|
||||
}
|
||||
|
||||
pub(crate) fn can_gain_resource(&self, r: ResourceType, amount: u32) -> bool {
|
||||
pub(crate) fn can_gain_resource(&self, r: ResourceType, amount: u8) -> bool {
|
||||
match r {
|
||||
ResourceType::MoodTokens | ResourceType::CultureTokens => true,
|
||||
_ => self.resources.get(&r) + amount <= self.resource_limit.get(&r),
|
||||
|
|
@ -465,7 +471,8 @@ impl Player {
|
|||
|
||||
#[must_use]
|
||||
pub fn can_advance(&self, advance: Advance) -> bool {
|
||||
self.can_afford(&self.advance_cost(advance, None).cost) && self.can_advance_free(advance)
|
||||
self.can_afford(&self.advance_cost(advance, CostTrigger::NoModifiers).cost)
|
||||
&& self.can_advance_free(advance)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
|
@ -606,12 +613,7 @@ impl Player {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn building_cost(
|
||||
&self,
|
||||
game: &Game,
|
||||
building: Building,
|
||||
execute: Option<&ResourcePile>,
|
||||
) -> CostInfo {
|
||||
pub fn building_cost(&self, game: &Game, building: Building, execute: CostTrigger) -> CostInfo {
|
||||
self.trigger_cost_event(
|
||||
|e| &e.construct_cost,
|
||||
&PaymentOptions::resources(BUILDING_COST),
|
||||
|
|
@ -622,7 +624,7 @@ impl Player {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn advance_cost(&self, advance: Advance, execute: Option<&ResourcePile>) -> CostInfo {
|
||||
pub fn advance_cost(&self, advance: Advance, execute: CostTrigger) -> CostInfo {
|
||||
self.trigger_cost_event(
|
||||
|e| &e.advance_cost,
|
||||
&PaymentOptions::sum(
|
||||
|
|
@ -773,34 +775,21 @@ impl Player {
|
|||
value: &PaymentOptions,
|
||||
info: &U,
|
||||
details: &V,
|
||||
execute: Option<&ResourcePile>,
|
||||
trigger: CostTrigger,
|
||||
) -> CostInfo {
|
||||
let event = get_event(&self.events.transient).get();
|
||||
let mut cost_info = CostInfo::new(self, value.clone());
|
||||
let mut can_avoid_activate = false;
|
||||
if let Some(execute) = execute {
|
||||
event.trigger_with_minimal_modifiers(
|
||||
&cost_info,
|
||||
info,
|
||||
details,
|
||||
&mut (),
|
||||
|i| {
|
||||
if can_avoid_activate && i.activate_city {
|
||||
return false;
|
||||
}
|
||||
if !i.activate_city {
|
||||
can_avoid_activate = true;
|
||||
}
|
||||
|
||||
i.cost.is_valid_payment(execute)
|
||||
},
|
||||
|i, m| i.cost.modifiers = m,
|
||||
)
|
||||
} else {
|
||||
let m = event.trigger_with_modifiers(&mut cost_info, info, details, &mut ());
|
||||
cost_info.cost.modifiers = m;
|
||||
cost_info
|
||||
match trigger {
|
||||
CostTrigger::WithModifiers => {
|
||||
let m =
|
||||
event.trigger_with_modifiers(&mut cost_info, info, details, &mut (), trigger);
|
||||
cost_info.cost.modifiers = m;
|
||||
}
|
||||
CostTrigger::NoModifiers => {
|
||||
event.trigger(&mut cost_info, info, details, &mut ());
|
||||
}
|
||||
}
|
||||
cost_info
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ impl Recruit {
|
|||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
|
||||
pub struct IncreaseHappiness {
|
||||
pub happiness_increases: Vec<(Position, u32)>,
|
||||
pub happiness_increases: Vec<(Position, u8)>,
|
||||
pub payment: ResourcePile,
|
||||
pub action_type: PlayingActionType,
|
||||
}
|
||||
|
|
@ -91,7 +91,7 @@ pub struct IncreaseHappiness {
|
|||
impl IncreaseHappiness {
|
||||
#[must_use]
|
||||
pub fn new(
|
||||
happiness_increases: Vec<(Position, u32)>,
|
||||
happiness_increases: Vec<(Position, u8)>,
|
||||
payment: ResourcePile,
|
||||
action_type: PlayingActionType,
|
||||
) -> Self {
|
||||
|
|
@ -271,7 +271,7 @@ impl PlayingAction {
|
|||
return Err("Cannot advance".to_string());
|
||||
}
|
||||
game.player(player_index)
|
||||
.advance_cost(advance, Some(&payment))
|
||||
.advance_cost(advance, game.execute_cost_trigger())
|
||||
.pay(game, &payment);
|
||||
gain_advance_without_payment(game, advance, player_index, payment, true);
|
||||
}
|
||||
|
|
@ -413,7 +413,7 @@ impl ActionCost {
|
|||
}
|
||||
|
||||
pub(crate) fn roll_boost_cost(roll: u8) -> ResourcePile {
|
||||
ResourcePile::culture_tokens(5 - roll as u32)
|
||||
ResourcePile::culture_tokens(5 - roll)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
|
|
|||
|
|
@ -3,11 +3,10 @@ use crate::consts::STACK_LIMIT;
|
|||
use crate::content::persistent_events::PersistentEventType;
|
||||
use crate::game::Game;
|
||||
use crate::payment::PaymentOptions;
|
||||
use crate::player::{Player, add_unit};
|
||||
use crate::player::{CostTrigger, Player, add_unit};
|
||||
use crate::player_events::CostInfo;
|
||||
use crate::playing_actions::Recruit;
|
||||
use crate::position::Position;
|
||||
use crate::resource_pile::ResourcePile;
|
||||
use crate::unit::{UnitType, Units, kill_units, set_unit_position};
|
||||
use itertools::Itertools;
|
||||
|
||||
|
|
@ -18,7 +17,7 @@ pub(crate) fn recruit(game: &mut Game, player_index: usize, r: Recruit) -> Resul
|
|||
r.city_position,
|
||||
r.leader_name.as_ref(),
|
||||
&r.replaced_units,
|
||||
Some(&r.payment),
|
||||
game.execute_cost_trigger(),
|
||||
)?;
|
||||
cost.pay(game, &r.payment);
|
||||
for unit in &r.replaced_units {
|
||||
|
|
@ -126,7 +125,7 @@ pub fn recruit_cost(
|
|||
city_position: Position,
|
||||
leader_name: Option<&String>,
|
||||
replaced_units: &[u32],
|
||||
execute: Option<&ResourcePile>,
|
||||
execute: CostTrigger,
|
||||
) -> Result<CostInfo, String> {
|
||||
let mut require_replace = units.clone();
|
||||
for t in player.available_units().to_vec() {
|
||||
|
|
@ -154,7 +153,7 @@ pub fn recruit_cost_without_replaced(
|
|||
units: &Units,
|
||||
city_position: Position,
|
||||
leader_name: Option<&String>,
|
||||
execute: Option<&ResourcePile>,
|
||||
execute: CostTrigger,
|
||||
) -> Result<CostInfo, String> {
|
||||
let city = player.get_city(city_position);
|
||||
if (units.cavalry > 0 || units.elephants > 0) && city.pieces.market.is_none() {
|
||||
|
|
|
|||
|
|
@ -11,38 +11,38 @@ use std::{
|
|||
#[derive(Default, Clone, Serialize, Deserialize, PartialEq, Eq, Debug, Hash)]
|
||||
pub struct ResourcePile {
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "u32::is_zero")]
|
||||
pub food: u32,
|
||||
#[serde(skip_serializing_if = "u8::is_zero")]
|
||||
pub food: u8,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "u32::is_zero")]
|
||||
pub wood: u32,
|
||||
#[serde(skip_serializing_if = "u8::is_zero")]
|
||||
pub wood: u8,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "u32::is_zero")]
|
||||
pub ore: u32,
|
||||
#[serde(skip_serializing_if = "u8::is_zero")]
|
||||
pub ore: u8,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "u32::is_zero")]
|
||||
pub ideas: u32,
|
||||
#[serde(skip_serializing_if = "u8::is_zero")]
|
||||
pub ideas: u8,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "u32::is_zero")]
|
||||
pub gold: u32,
|
||||
#[serde(skip_serializing_if = "u8::is_zero")]
|
||||
pub gold: u8,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "u32::is_zero")]
|
||||
pub mood_tokens: u32,
|
||||
#[serde(skip_serializing_if = "u8::is_zero")]
|
||||
pub mood_tokens: u8,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "u32::is_zero")]
|
||||
pub culture_tokens: u32,
|
||||
#[serde(skip_serializing_if = "u8::is_zero")]
|
||||
pub culture_tokens: u8,
|
||||
}
|
||||
|
||||
impl ResourcePile {
|
||||
#[must_use]
|
||||
pub const fn new(
|
||||
food: u32,
|
||||
wood: u32,
|
||||
ore: u32,
|
||||
ideas: u32,
|
||||
gold: u32,
|
||||
mood_tokens: u32,
|
||||
culture_tokens: u32,
|
||||
food: u8,
|
||||
wood: u8,
|
||||
ore: u8,
|
||||
ideas: u8,
|
||||
gold: u8,
|
||||
mood_tokens: u8,
|
||||
culture_tokens: u8,
|
||||
) -> Self {
|
||||
Self {
|
||||
food,
|
||||
|
|
@ -56,14 +56,14 @@ impl ResourcePile {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn of(resource_type: ResourceType, amount: u32) -> Self {
|
||||
pub const fn of(resource_type: ResourceType, amount: u8) -> Self {
|
||||
let mut p = Self::empty();
|
||||
p.add_type(resource_type, amount as i32);
|
||||
p
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get(&self, resource_type: &ResourceType) -> u32 {
|
||||
pub fn get(&self, resource_type: &ResourceType) -> u8 {
|
||||
match resource_type {
|
||||
ResourceType::Food => self.food,
|
||||
ResourceType::Wood => self.wood,
|
||||
|
|
@ -75,13 +75,26 @@ impl ResourcePile {
|
|||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get_mut(&mut self, resource_type: &ResourceType) -> &mut u8 {
|
||||
match resource_type {
|
||||
ResourceType::Food => &mut self.food,
|
||||
ResourceType::Wood => &mut self.wood,
|
||||
ResourceType::Ore => &mut self.ore,
|
||||
ResourceType::Ideas => &mut self.ideas,
|
||||
ResourceType::Gold => &mut self.gold,
|
||||
ResourceType::MoodTokens => &mut self.mood_tokens,
|
||||
ResourceType::CultureTokens => &mut self.culture_tokens,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn has_at_least(&self, other: &ResourcePile) -> bool {
|
||||
self.has_at_least_times(other, 1)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn has_at_least_times(&self, other: &ResourcePile, times: u32) -> bool {
|
||||
pub fn has_at_least_times(&self, other: &ResourcePile, times: u8) -> bool {
|
||||
self.food >= other.food * times
|
||||
&& self.wood >= other.wood * times
|
||||
&& self.ore >= other.ore * times
|
||||
|
|
@ -96,52 +109,52 @@ impl ResourcePile {
|
|||
/// Panics if `resource_type` is `Discount`
|
||||
pub const fn add_type(&mut self, resource_type: ResourceType, amount: i32) {
|
||||
match resource_type {
|
||||
ResourceType::Food => self.food = (self.food as i32 + amount) as u32,
|
||||
ResourceType::Wood => self.wood = (self.wood as i32 + amount) as u32,
|
||||
ResourceType::Ore => self.ore = (self.ore as i32 + amount) as u32,
|
||||
ResourceType::Ideas => self.ideas = (self.ideas as i32 + amount) as u32,
|
||||
ResourceType::Gold => self.gold = (self.gold as i32 + amount) as u32,
|
||||
ResourceType::Food => self.food = (self.food as i32 + amount) as u8,
|
||||
ResourceType::Wood => self.wood = (self.wood as i32 + amount) as u8,
|
||||
ResourceType::Ore => self.ore = (self.ore as i32 + amount) as u8,
|
||||
ResourceType::Ideas => self.ideas = (self.ideas as i32 + amount) as u8,
|
||||
ResourceType::Gold => self.gold = (self.gold as i32 + amount) as u8,
|
||||
ResourceType::MoodTokens => {
|
||||
self.mood_tokens = (self.mood_tokens as i32 + amount) as u32;
|
||||
self.mood_tokens = (self.mood_tokens as i32 + amount) as u8;
|
||||
}
|
||||
ResourceType::CultureTokens => {
|
||||
self.culture_tokens = (self.culture_tokens as i32 + amount) as u32;
|
||||
self.culture_tokens = (self.culture_tokens as i32 + amount) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn food(amount: u32) -> Self {
|
||||
pub const fn food(amount: u8) -> Self {
|
||||
Self::new(amount, 0, 0, 0, 0, 0, 0)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn wood(amount: u32) -> Self {
|
||||
pub const fn wood(amount: u8) -> Self {
|
||||
Self::new(0, amount, 0, 0, 0, 0, 0)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn ore(amount: u32) -> Self {
|
||||
pub const fn ore(amount: u8) -> Self {
|
||||
Self::new(0, 0, amount, 0, 0, 0, 0)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn ideas(amount: u32) -> Self {
|
||||
pub const fn ideas(amount: u8) -> Self {
|
||||
Self::new(0, 0, 0, amount, 0, 0, 0)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn gold(amount: u32) -> Self {
|
||||
pub const fn gold(amount: u8) -> Self {
|
||||
Self::new(0, 0, 0, 0, amount, 0, 0)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn mood_tokens(amount: u32) -> Self {
|
||||
pub const fn mood_tokens(amount: u8) -> Self {
|
||||
Self::new(0, 0, 0, 0, 0, amount, 0)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn culture_tokens(amount: u32) -> Self {
|
||||
pub const fn culture_tokens(amount: u8) -> Self {
|
||||
Self::new(0, 0, 0, 0, 0, 0, amount)
|
||||
}
|
||||
|
||||
|
|
@ -188,7 +201,7 @@ impl ResourcePile {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn amount(&self) -> u32 {
|
||||
pub fn amount(&self) -> u8 {
|
||||
self.food
|
||||
+ self.wood
|
||||
+ self.ore
|
||||
|
|
@ -231,7 +244,7 @@ impl ResourcePile {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn times(&self, t: u32) -> Self {
|
||||
pub fn times(&self, t: u8) -> Self {
|
||||
ResourcePile::new(
|
||||
self.food * t,
|
||||
self.wood * t,
|
||||
|
|
@ -284,10 +297,10 @@ impl SubAssign for ResourcePile {
|
|||
}
|
||||
}
|
||||
|
||||
impl Mul<u32> for ResourcePile {
|
||||
impl Mul<u8> for ResourcePile {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, rhs: u32) -> Self::Output {
|
||||
fn mul(self, rhs: u8) -> Self::Output {
|
||||
Self::new(
|
||||
self.food * rhs,
|
||||
self.wood * rhs,
|
||||
|
|
@ -311,7 +324,7 @@ impl Sum for ResourcePile {
|
|||
}
|
||||
|
||||
impl IntoIterator for ResourcePile {
|
||||
type Item = (ResourceType, u32);
|
||||
type Item = (ResourceType, u8);
|
||||
type IntoIter = ResourceIntoIterator;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
|
|
@ -328,7 +341,7 @@ pub struct ResourceIntoIterator {
|
|||
}
|
||||
|
||||
impl Iterator for ResourceIntoIterator {
|
||||
type Item = (ResourceType, u32);
|
||||
type Item = (ResourceType, u8);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let index = self.index;
|
||||
|
|
@ -399,7 +412,7 @@ impl Display for ResourcePile {
|
|||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
|
||||
pub struct CostWithDiscount {
|
||||
pub cost: ResourcePile,
|
||||
pub discount: u32,
|
||||
pub discount: u8,
|
||||
}
|
||||
|
||||
impl CostWithDiscount {
|
||||
|
|
@ -430,7 +443,7 @@ mod tests {
|
|||
use super::ResourcePile;
|
||||
use crate::payment::{PaymentConversionType, PaymentOptions};
|
||||
|
||||
fn assert_can_afford(name: &str, cost: &ResourcePile, discount: u32) {
|
||||
fn assert_can_afford(name: &str, cost: &ResourcePile, discount: u8) {
|
||||
let player_has = ResourcePile::new(1, 2, 3, 4, 5, 6, 7);
|
||||
let can_afford = PaymentOptions::resources_with_discount(
|
||||
cost.clone(),
|
||||
|
|
@ -440,7 +453,7 @@ mod tests {
|
|||
assert!(can_afford, "{name}");
|
||||
}
|
||||
|
||||
fn assert_cannot_afford(name: &str, cost: &ResourcePile, discount: u32) {
|
||||
fn assert_cannot_afford(name: &str, cost: &ResourcePile, discount: u8) {
|
||||
let player_has = ResourcePile::new(1, 2, 3, 4, 5, 6, 7);
|
||||
let can_afford = PaymentOptions::resources_with_discount(
|
||||
cost.clone(),
|
||||
|
|
|
|||
|
|
@ -418,9 +418,9 @@ mod tests {
|
|||
|
||||
fn assert_next_player(
|
||||
name: &str,
|
||||
player0_mood: u32,
|
||||
player1_mood: u32,
|
||||
player2_mood: u32,
|
||||
player0_mood: u8,
|
||||
player1_mood: u8,
|
||||
player2_mood: u8,
|
||||
expected_player_index: usize,
|
||||
) {
|
||||
let mut player0 = Player::new(civ::get_test_civilization(), 0);
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ use std::{
|
|||
};
|
||||
|
||||
use crate::ability_initializer::AbilityInitializerSetup;
|
||||
use crate::ai_collect::reset_collect_within_range_for_all_except;
|
||||
use crate::city::is_valid_city_terrain;
|
||||
use crate::collect::reset_collect_within_range_for_all_except;
|
||||
use crate::combat_roll::COMBAT_DIE_SIDES;
|
||||
use crate::consts::SHIP_CAPACITY;
|
||||
use crate::content::builtin::Builtin;
|
||||
|
|
|
|||
|
|
@ -248,12 +248,12 @@ pub(crate) fn on_play_wonder_card(game: &mut Game, player_index: usize, i: Wonde
|
|||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
|
||||
pub struct WonderDiscount {
|
||||
ignore_required_advances: bool,
|
||||
culture_tokens: u32,
|
||||
culture_tokens: u8,
|
||||
}
|
||||
|
||||
impl WonderDiscount {
|
||||
#[must_use]
|
||||
pub const fn new(ignore_required_advances: bool, discount_culture_tokens: u32) -> Self {
|
||||
pub const fn new(ignore_required_advances: bool, discount_culture_tokens: u8) -> Self {
|
||||
Self {
|
||||
ignore_required_advances,
|
||||
culture_tokens: discount_culture_tokens,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::common::{JsonTest, TestAction, illegal_action_test, influence_action};
|
||||
use server::action::{Action, execute_action};
|
||||
use server::city_pieces::Building::{Academy, Fortress, Temple};
|
||||
use server::collect::PositionCollection;
|
||||
use server::collect::{PositionCollection, possible_resource_collections};
|
||||
use server::consts::BUILDING_COST;
|
||||
use server::content::advances::trade_routes::find_trade_routes;
|
||||
use server::content::custom_actions::{CustomActionType, CustomEventAction};
|
||||
|
|
@ -11,6 +11,7 @@ use server::events::EventOrigin;
|
|||
use server::game::Game;
|
||||
use server::movement::MovementAction::Move;
|
||||
use server::movement::{MoveUnits, move_units_destinations};
|
||||
use server::player::CostTrigger;
|
||||
use server::playing_actions::PlayingAction::{
|
||||
Advance, Collect, Construct, Custom, EndTurn, Recruit, WonderCard,
|
||||
};
|
||||
|
|
@ -29,6 +30,8 @@ const JSON: JsonTest = JsonTest::new("advances");
|
|||
#[test]
|
||||
fn test_sanitation_and_draft() {
|
||||
// we should figure out that sanitation or draft are used, but not both
|
||||
// actually that code turns out to be too complicated and slow, so the log now says you
|
||||
// used both
|
||||
let units = Units::new(1, 1, 0, 0, 0, 0);
|
||||
let city_position = Position::from_offset("A1");
|
||||
JSON.test(
|
||||
|
|
@ -48,7 +51,7 @@ fn test_sanitation_and_draft() {
|
|||
&units,
|
||||
city_position,
|
||||
None,
|
||||
None,
|
||||
CostTrigger::WithModifiers,
|
||||
)
|
||||
.unwrap()
|
||||
.cost;
|
||||
|
|
@ -697,3 +700,58 @@ fn test_overpay() {
|
|||
)],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_husbandry() {
|
||||
JSON.test(
|
||||
"husbandry",
|
||||
vec![
|
||||
TestAction::undoable(
|
||||
0,
|
||||
Action::Playing(Collect(playing_actions::Collect::new(
|
||||
Position::from_offset("C2"),
|
||||
vec![PositionCollection::new(
|
||||
Position::from_offset("D1"),
|
||||
ResourcePile::food(1),
|
||||
)],
|
||||
ResourcePile::food(1),
|
||||
PlayingActionType::Collect,
|
||||
))),
|
||||
)
|
||||
.without_json_comparison()
|
||||
.with_post_assert(|game| {
|
||||
// but not again
|
||||
assert!(has_husbandry_field(game))
|
||||
}),
|
||||
// can use husbandry - because it was not used in the previous action
|
||||
TestAction::undoable(
|
||||
0,
|
||||
Action::Playing(Collect(playing_actions::Collect::new(
|
||||
Position::from_offset("C2"),
|
||||
vec![PositionCollection::new(
|
||||
Position::from_offset("E2"),
|
||||
ResourcePile::food(1),
|
||||
)],
|
||||
ResourcePile::food(1),
|
||||
PlayingActionType::Collect,
|
||||
))),
|
||||
)
|
||||
.without_json_comparison()
|
||||
.with_post_assert(|game| {
|
||||
// but not again
|
||||
assert!(!has_husbandry_field(game))
|
||||
}),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
fn has_husbandry_field(game: &Game) -> bool {
|
||||
let info = possible_resource_collections(
|
||||
game,
|
||||
Position::from_offset("C2"),
|
||||
0,
|
||||
&[],
|
||||
CostTrigger::NoModifiers,
|
||||
);
|
||||
info.choices.contains_key(&Position::from_offset("E2"))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
use crate::common::JsonTest;
|
||||
use itertools::Itertools;
|
||||
use server::action::{Action, ActionType};
|
||||
use server::ai_actions::get_available_actions;
|
||||
use server::collect::{PositionCollection, city_collections_uncached};
|
||||
use server::ai_actions::AiActions;
|
||||
use server::ai_collect::city_collections_uncached;
|
||||
use server::collect::PositionCollection;
|
||||
use server::playing_actions::{
|
||||
Collect, IncreaseHappiness, PlayingAction, PlayingActionType, Recruit,
|
||||
};
|
||||
|
|
@ -39,7 +40,8 @@ fn collect_city() {
|
|||
#[test]
|
||||
fn all_actions() {
|
||||
let game = &JSON.load_game("start");
|
||||
let mut all = get_available_actions(game);
|
||||
let mut actions = AiActions::new();
|
||||
let mut all = actions.get_available_actions(game);
|
||||
let (_, advances) = remove_element_by(&mut all, |(t, _)| {
|
||||
matches!(t, ActionType::Playing(PlayingActionType::Advance))
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,15 +1,13 @@
|
|||
use crate::common::{GamePath, to_json, write_result};
|
||||
use async_std::task;
|
||||
use server::ai_actions::AiActions;
|
||||
use server::game::Game;
|
||||
use server::{
|
||||
action::{self, Action},
|
||||
ai_actions,
|
||||
game::GameState,
|
||||
game_setup,
|
||||
playing_actions::PlayingAction,
|
||||
utils::{Rng, Shuffle},
|
||||
};
|
||||
use std::panic::{AssertUnwindSafe, catch_unwind};
|
||||
|
||||
mod common;
|
||||
|
||||
|
|
@ -44,13 +42,14 @@ fn random_actions_iterations(mut rng: Rng) {
|
|||
let mut game = game_setup::setup_game(2, seed, true);
|
||||
game.supports_undo = false;
|
||||
build_adjacent_cities(&mut game);
|
||||
let mut ai_actions = AiActions::new();
|
||||
loop {
|
||||
if matches!(game.state, GameState::Finished) {
|
||||
break;
|
||||
}
|
||||
let player_index = game.active_player();
|
||||
//todo: include movement action
|
||||
let actions = ai_actions::get_available_actions(&game);
|
||||
let actions = ai_actions.get_available_actions(&game);
|
||||
let actions = actions
|
||||
.into_iter()
|
||||
.flat_map(|(_, actions)| actions)
|
||||
|
|
@ -59,18 +58,7 @@ fn random_actions_iterations(mut rng: Rng) {
|
|||
.take_random_element(&mut rng)
|
||||
.unwrap_or(Action::Playing(PlayingAction::EndTurn));
|
||||
|
||||
game = catch_unwind(AssertUnwindSafe(|| {
|
||||
action::execute_action(game.clone(), action.clone(), player_index)
|
||||
}))
|
||||
.unwrap_or_else(move |e| {
|
||||
use chrono::Utc;
|
||||
let rfc_format = Utc::now().to_rfc3339();
|
||||
let file = format!("failure{rfc_format}");
|
||||
|
||||
write_result(&to_json(&game), &GamePath::new(".", &file));
|
||||
|
||||
panic!("action {action:?}\nresult stored in {file}.json: {e:?}")
|
||||
});
|
||||
game = action::execute_action(game.clone(), action.clone(), player_index)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
293
server/tests/test_games/advances/husbandry.json
Normal file
293
server/tests/test_games/advances/husbandry.json
Normal file
|
|
@ -0,0 +1,293 @@
|
|||
{
|
||||
"state": "Playing",
|
||||
"players": [
|
||||
{
|
||||
"id": 0,
|
||||
"resources": {
|
||||
"food": 7,
|
||||
"wood": 5,
|
||||
"ore": 5,
|
||||
"ideas": 1,
|
||||
"gold": 5,
|
||||
"mood_tokens": 11,
|
||||
"culture_tokens": 10
|
||||
},
|
||||
"resource_limit": {
|
||||
"food": 7,
|
||||
"wood": 7,
|
||||
"ore": 7,
|
||||
"ideas": 7,
|
||||
"gold": 7
|
||||
},
|
||||
"cities": [
|
||||
{
|
||||
"city_pieces": {
|
||||
"market": 1
|
||||
},
|
||||
"mood_state": "Happy",
|
||||
"position": "A1"
|
||||
},
|
||||
{
|
||||
"city_pieces": {
|
||||
"academy": 1,
|
||||
"port": 1
|
||||
},
|
||||
"mood_state": "Happy",
|
||||
"activations": 6,
|
||||
"position": "C2",
|
||||
"port_position": "C3"
|
||||
},
|
||||
{
|
||||
"city_pieces": {
|
||||
"obelisk": 1,
|
||||
"observatory": 1,
|
||||
"fortress": 1,
|
||||
"temple": 1
|
||||
},
|
||||
"mood_state": "Neutral",
|
||||
"position": "B1"
|
||||
},
|
||||
{
|
||||
"city_pieces": {},
|
||||
"mood_state": "Angry",
|
||||
"activations": 4,
|
||||
"angry_activation": true,
|
||||
"position": "B3"
|
||||
}
|
||||
],
|
||||
"units": [
|
||||
{
|
||||
"position": "C2",
|
||||
"unit_type": "Infantry",
|
||||
"id": 0
|
||||
},
|
||||
{
|
||||
"position": "B3",
|
||||
"unit_type": "Settler",
|
||||
"id": 5
|
||||
},
|
||||
{
|
||||
"position": "B3",
|
||||
"unit_type": "Settler",
|
||||
"id": 6
|
||||
},
|
||||
{
|
||||
"position": "C3",
|
||||
"unit_type": "Ship",
|
||||
"id": 7,
|
||||
"carried_units": [
|
||||
{
|
||||
"unit_type": "Cavalry",
|
||||
"id": 1
|
||||
},
|
||||
{
|
||||
"unit_type": "Elephant",
|
||||
"id": 2
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"position": "C3",
|
||||
"unit_type": "Ship",
|
||||
"id": 8
|
||||
},
|
||||
{
|
||||
"position": "C3",
|
||||
"unit_type": "Ship",
|
||||
"id": 9
|
||||
}
|
||||
],
|
||||
"civilization": "Maya",
|
||||
"available_leaders": [
|
||||
"Kʼinich Janaab Pakal I"
|
||||
],
|
||||
"advances": [
|
||||
"Draft",
|
||||
"Farming",
|
||||
"Husbandry"
|
||||
],
|
||||
"incident_tokens": 2,
|
||||
"wonder_cards": [
|
||||
"Great Gardens"
|
||||
],
|
||||
"next_unit_id": 10
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"resources": {
|
||||
"food": 2,
|
||||
"wood": 5,
|
||||
"ore": 5,
|
||||
"ideas": 5,
|
||||
"gold": 5,
|
||||
"mood_tokens": 9,
|
||||
"culture_tokens": 9
|
||||
},
|
||||
"resource_limit": {
|
||||
"food": 2,
|
||||
"wood": 7,
|
||||
"ore": 7,
|
||||
"ideas": 7,
|
||||
"gold": 7
|
||||
},
|
||||
"cities": [
|
||||
{
|
||||
"city_pieces": {},
|
||||
"mood_state": "Angry",
|
||||
"activations": 2,
|
||||
"position": "C1"
|
||||
},
|
||||
{
|
||||
"city_pieces": {
|
||||
"port": 1
|
||||
},
|
||||
"mood_state": "Neutral",
|
||||
"position": "B2",
|
||||
"port_position": "C3"
|
||||
}
|
||||
],
|
||||
"units": [
|
||||
{
|
||||
"position": "C1",
|
||||
"unit_type": "Infantry",
|
||||
"id": 0
|
||||
},
|
||||
{
|
||||
"position": "C1",
|
||||
"unit_type": "Infantry",
|
||||
"id": 1
|
||||
}
|
||||
],
|
||||
"civilization": "test0",
|
||||
"available_leaders": [
|
||||
"Alexander",
|
||||
"Kleopatra"
|
||||
],
|
||||
"advances": [
|
||||
"Farming",
|
||||
"Mining"
|
||||
],
|
||||
"incident_tokens": 3,
|
||||
"next_unit_id": 2
|
||||
}
|
||||
],
|
||||
"map": {
|
||||
"tiles": [
|
||||
[
|
||||
"A1",
|
||||
"Fertile"
|
||||
],
|
||||
[
|
||||
"A2",
|
||||
"Water"
|
||||
],
|
||||
[
|
||||
"A3",
|
||||
{
|
||||
"Exhausted": "Forest"
|
||||
}
|
||||
],
|
||||
[
|
||||
"A4",
|
||||
"Mountain"
|
||||
],
|
||||
[
|
||||
"B1",
|
||||
"Mountain"
|
||||
],
|
||||
[
|
||||
"B2",
|
||||
"Forest"
|
||||
],
|
||||
[
|
||||
"B3",
|
||||
"Fertile"
|
||||
],
|
||||
[
|
||||
"B4",
|
||||
"Fertile"
|
||||
],
|
||||
[
|
||||
"B5",
|
||||
"Fertile"
|
||||
],
|
||||
[
|
||||
"B6",
|
||||
"Fertile"
|
||||
],
|
||||
[
|
||||
"C1",
|
||||
"Barren"
|
||||
],
|
||||
[
|
||||
"C2",
|
||||
"Forest"
|
||||
],
|
||||
[
|
||||
"C3",
|
||||
"Water"
|
||||
],
|
||||
[
|
||||
"C4",
|
||||
"Water"
|
||||
],
|
||||
[
|
||||
"C5",
|
||||
"Water"
|
||||
],
|
||||
[
|
||||
"D1",
|
||||
"Fertile"
|
||||
],
|
||||
[
|
||||
"D2",
|
||||
"Water"
|
||||
],
|
||||
[
|
||||
"E2",
|
||||
"Fertile"
|
||||
]
|
||||
]
|
||||
},
|
||||
"starting_player_index": 0,
|
||||
"current_player_index": 0,
|
||||
"action_log": [
|
||||
{
|
||||
"rounds": [
|
||||
{
|
||||
"players": [
|
||||
{
|
||||
"index": 0,
|
||||
"items": []
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"action_log_index": 0,
|
||||
"log": [],
|
||||
"undo_limit": 0,
|
||||
"actions_left": 3,
|
||||
"round": 6,
|
||||
"age": 1,
|
||||
"messages": [
|
||||
"The game has started"
|
||||
],
|
||||
"dice_roll_outcomes": [
|
||||
1,
|
||||
1,
|
||||
10,
|
||||
10,
|
||||
10,
|
||||
10,
|
||||
10,
|
||||
10,
|
||||
10,
|
||||
10
|
||||
],
|
||||
"rng": "46312381643103681595563341886777350953",
|
||||
"wonders_left": [
|
||||
"Pyramids"
|
||||
]
|
||||
}
|
||||
|
|
@ -339,7 +339,8 @@
|
|||
"log": [
|
||||
[
|
||||
"Player1 paid 2 gold and 1 mood token to recruit 1 settler and 1 infantry in the city at A1",
|
||||
"Sanitation reduced the cost of 1 Settler to 1 mood token"
|
||||
"Sanitation reduced the cost of 1 Settler to 1 mood token",
|
||||
"Draft reduced the cost of 1 Infantry to 1 mood token"
|
||||
]
|
||||
],
|
||||
"undo_limit": 0,
|
||||
|
|
|
|||
|
|
@ -149,7 +149,7 @@
|
|||
"incident_tokens": 3,
|
||||
"next_unit_id": 8,
|
||||
"event_info": {
|
||||
"StateReligion": "used"
|
||||
"State Religion": "used"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue