game logic fixes (#213)
This commit is contained in:
parent
02b90c2b53
commit
aef62b6b1e
10 changed files with 124 additions and 49 deletions
|
|
@ -1,3 +1,6 @@
|
|||
[env]
|
||||
RUST_BACKTRACE = '1'
|
||||
|
||||
[tasks.test]
|
||||
description = "Run server tests"
|
||||
run = 'cargo test {{arg(name="test", default="")}}'
|
||||
|
|
|
|||
|
|
@ -450,7 +450,8 @@ fn change_government(p: &Player, c: &ChangeGovernmentRequest) -> Vec<EventRespon
|
|||
let advances = new
|
||||
.advances
|
||||
.iter()
|
||||
.take(government_advances(p).len())
|
||||
.dropping(1) // is taken implicitly
|
||||
.take(government_advances(p).len() - 1)
|
||||
.map(|a| a.name.clone())
|
||||
.collect_vec();
|
||||
|
||||
|
|
|
|||
|
|
@ -229,12 +229,27 @@ impl CombatRoundEnd {
|
|||
combat: Combat,
|
||||
game: &Game,
|
||||
) -> Self {
|
||||
let attackers_dead =
|
||||
combat.active_attackers(game).len() - attacker_casualties.fighters as usize == 0;
|
||||
let defenders_dead =
|
||||
combat.active_defenders(game).len() - defender_casualties.fighters as usize == 0;
|
||||
let mut combat_round_end = Self {
|
||||
attacker_casualties,
|
||||
defender_casualties,
|
||||
can_retreat,
|
||||
final_result: None,
|
||||
combat,
|
||||
phase: CombatEventPhase::TacticsCard,
|
||||
};
|
||||
combat_round_end.set_final_result(game);
|
||||
combat_round_end
|
||||
}
|
||||
|
||||
let final_result = if attackers_dead && defenders_dead {
|
||||
pub(crate) fn set_final_result(&mut self, game: &Game) {
|
||||
let attackers_dead = self.combat.active_attackers(game).len()
|
||||
- self.attacker_casualties.fighters as usize
|
||||
== 0;
|
||||
let defenders_dead = self.combat.active_defenders(game).len()
|
||||
- self.defender_casualties.fighters as usize
|
||||
== 0;
|
||||
|
||||
self.final_result = if attackers_dead && defenders_dead {
|
||||
Some(CombatResult::Draw)
|
||||
} else if attackers_dead {
|
||||
Some(CombatResult::DefenderWins)
|
||||
|
|
@ -243,15 +258,6 @@ impl CombatRoundEnd {
|
|||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Self {
|
||||
attacker_casualties,
|
||||
defender_casualties,
|
||||
can_retreat,
|
||||
final_result,
|
||||
combat,
|
||||
phase: CombatEventPhase::TacticsCard,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use crate::ability_initializer::AbilityInitializerSetup;
|
||||
use crate::advance::remove_advance;
|
||||
use crate::combat::{Combat, CombatModifier, CombatRetreatState};
|
||||
use crate::combat_listeners::CombatResult;
|
||||
use crate::content::advances::get_advance;
|
||||
|
|
@ -11,6 +12,7 @@ use crate::payment::PaymentOptions;
|
|||
use crate::player_events::IncidentTarget;
|
||||
use crate::resource_pile::ResourcePile;
|
||||
use crate::utils::remove_and_map_element_by;
|
||||
use itertools::Itertools;
|
||||
|
||||
pub(crate) fn trojan_incidents() -> Vec<Incident> {
|
||||
vec![trojan_horse(), solar_eclipse(), anarchy()]
|
||||
|
|
@ -157,14 +159,24 @@ fn anarchy() -> Incident {
|
|||
IncidentTarget::ActivePlayer,
|
||||
0,
|
||||
|game, player_index, player_name, _| {
|
||||
let old = game.player(player_index).advances.len();
|
||||
|
||||
let remove = game
|
||||
.player(player_index)
|
||||
.advances
|
||||
.iter()
|
||||
.filter_map(|a| a.government.is_some().then_some(a.name.clone()))
|
||||
.collect_vec();
|
||||
for a in remove {
|
||||
remove_advance(game, &get_advance(&a), player_index);
|
||||
}
|
||||
|
||||
let p = game.player_mut(player_index);
|
||||
let old = p.advances.len();
|
||||
p.advances.retain(|a| a.government.is_none());
|
||||
let lost = old - p.advances.len();
|
||||
p.event_victory_points += lost as f32;
|
||||
if lost > 0 {
|
||||
game.add_info_log_item(&format!(
|
||||
"{player_name} lost {lost} government advances due to Anarchy -\
|
||||
"{player_name} lost {lost} government advances due to Anarchy - \
|
||||
adding {lost} victory points",
|
||||
));
|
||||
|
||||
|
|
|
|||
|
|
@ -77,6 +77,7 @@ pub(crate) fn encircled(id: u8) -> TacticsCard {
|
|||
"Encircled rolled a 5 or 6 and added a hit that cannot be ignored",
|
||||
);
|
||||
e.casualties_mut(opponent_role).fighters += 1;
|
||||
e.set_final_result(game);
|
||||
} else {
|
||||
game.add_info_log_item("Encircled rolled no 5 or 6");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@ use serde::{Deserialize, Serialize};
|
|||
type Listener<T, U, V, W> = (Box<dyn Fn(&mut T, &U, &V, &mut W)>, i32, usize, EventOrigin);
|
||||
|
||||
pub struct EventMut<T, U = (), V = (), W = ()> {
|
||||
name: String, // for debugging
|
||||
listeners: Vec<Listener<T, U, V, W>>,
|
||||
next_id: usize,
|
||||
}
|
||||
|
|
@ -64,8 +65,9 @@ where
|
|||
T: Clone + PartialEq,
|
||||
W: Clone + PartialEq,
|
||||
{
|
||||
fn new() -> Self {
|
||||
fn new(name: &str) -> Self {
|
||||
Self {
|
||||
name: name.to_string(),
|
||||
listeners: Vec::new(),
|
||||
next_id: 0,
|
||||
}
|
||||
|
|
@ -82,10 +84,12 @@ where
|
|||
F: Fn(&mut T, &U, &V, &mut W) + 'static,
|
||||
{
|
||||
let id = self.next_id;
|
||||
assert!(
|
||||
!self.listeners.iter().any(|(_, p, _, _)| &priority == p),
|
||||
"Priority {priority} already used by listener with key {key:?}"
|
||||
);
|
||||
if let Some(old) = self.listeners.iter().find(|(_, p, _, _)| &priority == p) {
|
||||
panic!(
|
||||
"Event {}: Priority {priority} already used by listener with key {:?} when adding {key:?}",
|
||||
self.name, old.3
|
||||
)
|
||||
}
|
||||
self.listeners
|
||||
.push((Box::new(new_listener), priority, id, key));
|
||||
self.listeners.sort_by_key(|(_, priority, _, _)| *priority);
|
||||
|
|
@ -184,19 +188,22 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Clone + PartialEq, U, V, W: Clone + PartialEq> Default for Event<T, U, V, W> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
inner: Some(EventMut::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Event<T, U = (), V = (), W = ()> {
|
||||
pub inner: Option<EventMut<T, U, V, W>>,
|
||||
}
|
||||
|
||||
impl<T, U, V, W> Event<T, U, V, W> {
|
||||
#[must_use]
|
||||
pub fn new(name: &str) -> Self
|
||||
where
|
||||
T: Clone + PartialEq,
|
||||
W: Clone + PartialEq,
|
||||
{
|
||||
Self {
|
||||
inner: Some(EventMut::new(name)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get(&self) -> &EventMut<T, U, V, W> {
|
||||
self.inner.as_ref().expect("Event should be initialized")
|
||||
}
|
||||
|
|
@ -216,7 +223,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn mutable_event() {
|
||||
let mut event = EventMut::new();
|
||||
let mut event = EventMut::new("test");
|
||||
event.add_listener_mut(
|
||||
|item, constant, _, ()| *item += constant,
|
||||
0,
|
||||
|
|
@ -268,7 +275,7 @@ mod tests {
|
|||
pub modifiers: Vec<EventOrigin>,
|
||||
}
|
||||
|
||||
let mut event = EventMut::new();
|
||||
let mut event = EventMut::new("test");
|
||||
event.add_listener_mut(
|
||||
|value: &mut Info, (), (), ()| value.value += 1,
|
||||
0,
|
||||
|
|
|
|||
|
|
@ -100,7 +100,6 @@ pub fn setup_game(player_amount: usize, seed: String, setup: bool) -> Game {
|
|||
}
|
||||
|
||||
game.next_age();
|
||||
game.start_turn();
|
||||
game
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,13 +30,20 @@ use std::collections::{HashMap, HashSet};
|
|||
|
||||
pub(crate) type PersistentEvent<V = ()> = Event<Game, PersistentEventInfo, (), V>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct PlayerEvents {
|
||||
pub persistent: PersistentEvents,
|
||||
pub transient: TransientEvents,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
impl PlayerEvents {
|
||||
pub fn new() -> PlayerEvents {
|
||||
Self {
|
||||
persistent: PersistentEvents::new(),
|
||||
transient: TransientEvents::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct TransientEvents {
|
||||
pub on_influence_culture_attempt: Event<Result<InfluenceCultureInfo, String>, City, Game>,
|
||||
pub on_influence_culture_resolve: Event<Game, InfluenceCultureOutcome>,
|
||||
|
|
@ -54,7 +61,27 @@ pub(crate) struct TransientEvents {
|
|||
pub collect_total: Event<CollectInfo>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
impl TransientEvents {
|
||||
pub fn new() -> TransientEvents {
|
||||
TransientEvents {
|
||||
on_influence_culture_attempt: Event::new("on_influence_culture_attempt"),
|
||||
on_influence_culture_resolve: Event::new("on_influence_culture_resolve"),
|
||||
before_move: Event::new("before_move"),
|
||||
|
||||
construct_cost: Event::new("construct_cost"),
|
||||
advance_cost: Event::new("advance_cost"),
|
||||
happiness_cost: Event::new("happiness_cost"),
|
||||
recruit_cost: Event::new("recruit_cost"),
|
||||
|
||||
is_playing_action_available: Event::new("is_playing_action_available"),
|
||||
|
||||
terrain_collect_options: Event::new("terrain_collect_options"),
|
||||
collect_options: Event::new("collect_options"),
|
||||
collect_total: Event::new("collect_total"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct PersistentEvents {
|
||||
pub collect: PersistentEvent<CollectInfo>,
|
||||
pub construct: PersistentEvent<Building>,
|
||||
|
|
@ -82,9 +109,35 @@ pub(crate) struct PersistentEvents {
|
|||
pub select_objective_cards: PersistentEvent<SelectObjectivesInfo>,
|
||||
}
|
||||
|
||||
impl PlayerEvents {
|
||||
pub fn new() -> PlayerEvents {
|
||||
Self::default()
|
||||
impl PersistentEvents {
|
||||
#[must_use]
|
||||
pub fn new() -> PersistentEvents {
|
||||
PersistentEvents {
|
||||
collect: Event::new("collect"),
|
||||
construct: Event::new("construct"),
|
||||
draw_wonder_card: Event::new("draw_wonder_card"),
|
||||
advance: Event::new("advance"),
|
||||
recruit: Event::new("recruit"),
|
||||
found_city: Event::new("found_city"),
|
||||
influence_culture_resolution: Event::new("influence_culture_resolution"),
|
||||
explore_resolution: Event::new("explore_resolution"),
|
||||
play_action_card: Event::new("play_action_card"),
|
||||
play_wonder_card: Event::new("play_wonder_card"),
|
||||
|
||||
status_phase: Event::new("status_phase"),
|
||||
turn_start: Event::new("turn_start"),
|
||||
incident: Event::new("incident"),
|
||||
combat_start: Event::new("combat_start"),
|
||||
combat_round_start: Event::new("combat_round_start"),
|
||||
combat_round_start_reveal_tactics: Event::new("combat_round_start_reveal_tactics"),
|
||||
combat_round_start_tactics: Event::new("combat_round_start_tactics"),
|
||||
combat_round_end: Event::new("combat_round_end"),
|
||||
combat_round_end_tactics: Event::new("combat_round_end_tactics"),
|
||||
combat_end: Event::new("combat_end"),
|
||||
capture_undefended_position: Event::new("capture_undefended_position"),
|
||||
units_killed: Event::new("units_killed"),
|
||||
select_objective_cards: Event::new("select_objective_cards"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -384,10 +384,6 @@
|
|||
"rounds": [
|
||||
{
|
||||
"players": [
|
||||
{
|
||||
"index": 0,
|
||||
"items": []
|
||||
},
|
||||
{
|
||||
"index": 0,
|
||||
"items": []
|
||||
|
|
@ -418,9 +414,6 @@
|
|||
[
|
||||
"Round 1/3"
|
||||
],
|
||||
[
|
||||
"It's Player1's turn"
|
||||
],
|
||||
[
|
||||
"It's Player1's turn"
|
||||
]
|
||||
|
|
|
|||
|
|
@ -223,7 +223,7 @@
|
|||
"infantry": 2
|
||||
}
|
||||
},
|
||||
"result": "DefenderWins"
|
||||
"result": "Draw"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
@ -253,7 +253,7 @@
|
|||
"Player1 removed 1 infantry",
|
||||
"Player2 has to remove all of their defending units",
|
||||
"Player2 removed 2 infantry",
|
||||
"Defender wins"
|
||||
"Battle ends in a draw"
|
||||
]
|
||||
],
|
||||
"undo_limit": 2,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue