engineering advance

This commit is contained in:
AlphaCodingPilot 2023-01-21 20:00:59 +01:00
commit 8da59b9dd1
5 changed files with 68 additions and 15 deletions

View file

@ -20,9 +20,8 @@ impl Advance {
pub fn builder(
name: &str,
description: &str,
advance_bonus: Option<AdvanceBonus>,
) -> AdvanceBuilder {
AdvanceBuilder::new(name.to_string(), description.to_string(), advance_bonus)
AdvanceBuilder::new(name.to_string(), description.to_string())
}
}
@ -44,11 +43,11 @@ pub struct AdvanceBuilder {
}
impl AdvanceBuilder {
fn new(name: String, description: String, advance_bonus: Option<AdvanceBonus>) -> Self {
fn new(name: String, description: String) -> Self {
Self {
name,
description,
advance_bonus,
advance_bonus: None,
required_advance: None,
contradicting_advance: None,
unlocked_building: None,
@ -57,6 +56,11 @@ impl AdvanceBuilder {
}
}
pub fn with_advance_bonus(mut self, advance_bonus: AdvanceBonus) -> Self {
self.advance_bonus = Some(advance_bonus);
self
}
pub fn with_required_advance(mut self, required_advance: String) -> Self {
self.required_advance = Some(required_advance);
self

View file

@ -57,6 +57,9 @@ impl City {
}
pub fn can_increase_size(&self, building: &Building, player: &Player) -> bool {
if self.player != player.id {
return false;
}
if self.city_pieces.amount() == MAX_CITY_SIZE {
return false;
}
@ -80,6 +83,9 @@ impl City {
}
pub fn can_build_wonder(&self, wonder: &Wonder, player: &Player) -> bool {
if self.player != player.id {
return false;
}
if self.city_pieces.amount() == MAX_CITY_SIZE {
return false;
}
@ -189,7 +195,7 @@ impl City {
}
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub struct CityData {
city_pieces: CityPiecesData,
mood_state: MoodState,
@ -398,7 +404,7 @@ impl CityPieces {
}
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub struct CityPiecesData {
academy: Option<usize>,
market: Option<usize>,
@ -410,7 +416,7 @@ pub struct CityPiecesData {
wonders: Vec<String>,
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum MoodState {
Happy,
Neutral,
@ -420,7 +426,6 @@ pub enum MoodState {
#[cfg(test)]
mod tests {
use super::{Building, City, MoodState};
use crate::civilization::{self, Civilization};
use crate::content::civilizations;
use crate::game;
use crate::hexagon::Position;

View file

@ -1,7 +1,13 @@
use crate::advance::Advance;
use crate::{ability_initializer::AbilityInitializerSetup, advance::Advance};
pub fn get_technologies() -> Vec<Advance> {
vec![]
vec![Advance::builder(
"Engineering",
"● Immediately draw 1 wonder\n● May Construct wonder happy cities",
)
.add_ability_initializer(Box::new(|game, player| game.draw_wonder_card(player)))
.add_custom_action("Construct wonder")
.build()]
}
pub fn get_advance_by_name(name: &str) -> Option<Advance> {

View file

@ -1,5 +1,43 @@
use crate::playing_actions::CustomAction;
use serde::{Serialize, Deserialize};
use crate::{playing_actions::{CustomAction, ActionType}, city::{CityData, City}, resource_pile::ResourcePile, game::Game};
use super::wonders;
pub fn get_custom_action(name: &str, contents: &str) -> Box<dyn CustomAction> {
panic!("Invalid name");
Box::new(match name {
"Construct wonder" => serde_json::from_str::<ConstructWonder>(contents),
_ => panic!("Invalid action name"),
}.unwrap_or_else(|_| panic!("Invalid {} action name", name)))
}
#[derive(Serialize, Deserialize)]
struct ConstructWonder {
city: CityData,
wonder: String,
payment: ResourcePile,
}
impl CustomAction for ConstructWonder {
fn execute(&self, game: &mut Game, player: usize) {
let city = City::from_data(self.city.clone());
let wonder = wonders::get_wonder_by_name(&self.wonder).expect("construct wonder data should include a valid wonder name");
if !city.can_build_wonder(&wonder, &game.players[player]) || !self.payment.can_afford(&wonder.cost) {
panic!("Illegal action");
}
game.with_player(player, |player, game| {
player.loose_resources(self.payment.clone());
player.with_city(&city.position, |player, city| {
city.build_wonder(wonder, game, player.id);
});
});
}
fn action_type(&self) -> ActionType {
ActionType::default()
}
fn name(&self) -> String {
String::from("Construct wonder")
}
}

View file

@ -64,8 +64,8 @@ impl PlayingAction {
let city = City::from_data(city);
let building = Building::from_data(&city_piece);
let cost = player.building_cost(&building, &city);
if city.player != player.id
|| !city.can_increase_size(&building, player)
if
!city.can_increase_size(&building, player)
|| !payment.can_afford(&cost)
{
panic!("Illegal action");
@ -76,7 +76,7 @@ impl PlayingAction {
if building_bonus != ResourcePile::mood_tokens(1)
&& building_bonus != ResourcePile::culture_tokens(1)
{
panic!("Illegal action");
panic!("Invalid temple bonus");
}
player.gain_resources(building_bonus);
}