Skip to content

Commit c5b1b89

Browse files
authored
Custom event phase (#146)
1 parent 6fc5a65 commit c5b1b89

28 files changed

+732
-548
lines changed

client/src/client.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ fn render_active_dialog(rc: &RenderContext) -> StateUpdate {
164164
ActiveDialog::PlayActionCard => combat_ui::play_action_card_dialog(rc),
165165
ActiveDialog::Retreat => combat_ui::retreat_dialog(rc),
166166
ActiveDialog::RemoveCasualties(s) => combat_ui::remove_casualties_dialog(rc, s),
167-
ActiveDialog::SiegecraftPayment(p) => combat_ui::pay_siegecraft_dialog(p, rc),
168-
ActiveDialog::SteelWeaponPayment(p) => combat_ui::pay_steel_weapons_dialog(rc, p),
167+
168+
ActiveDialog::CustomPhasePaymentRequest(c) => {
169+
custom_actions_ui::custom_phase_payment_dialog(rc, c)
170+
}
169171
}
170172
}
171173

client/src/client_state.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
use macroquad::prelude::*;
2+
use server::ability_initializer::EventOrigin;
23
use server::action::Action;
34
use server::city::{City, MoodState};
45
use server::combat::{active_attackers, active_defenders, CombatPhase};
5-
use server::content::advances::{NAVIGATION, ROADS, SIEGECRAFT, STEEL_WEAPONS};
6-
use server::content::custom_phase_actions::CustomPhaseState;
6+
use server::content::advances::{NAVIGATION, ROADS};
7+
use server::content::custom_phase_actions::{CustomPhaseRequest, CustomPhaseState};
78
use server::game::{CulturalInfluenceResolution, CurrentMove, Game, GameState};
89
use server::position::Position;
910
use server::status_phase::{StatusPhaseAction, StatusPhaseState};
1011

1112
use crate::assets::Assets;
1213
use crate::client::{Features, GameSyncRequest};
1314
use crate::collect_ui::CollectResources;
14-
use crate::combat_ui::{
15-
steel_weapons_dialog, RemoveCasualtiesSelection, SiegecraftPaymentDialog, SteelWeaponDialog,
16-
};
15+
use crate::combat_ui::RemoveCasualtiesSelection;
1716
use crate::construct_ui::ConstructionPayment;
1817
use crate::custom_actions_ui::trade_route_dialog;
1918
use crate::happiness_ui::IncreaseHappinessConfig;
@@ -59,9 +58,9 @@ pub enum ActiveDialog {
5958
PlaceSettler,
6059
Retreat,
6160
RemoveCasualties(RemoveCasualtiesSelection),
62-
SiegecraftPayment(SiegecraftPaymentDialog),
63-
SteelWeaponPayment(SteelWeaponDialog),
6461
TradeRouteSelection(Payment), // it's actually a gain
62+
63+
CustomPhasePaymentRequest(Vec<Payment>),
6564
}
6665

6766
impl ActiveDialog {
@@ -93,9 +92,8 @@ impl ActiveDialog {
9392
ActiveDialog::PlaceSettler => "place settler",
9493
ActiveDialog::Retreat => "retreat",
9594
ActiveDialog::RemoveCasualties(_) => "remove casualties",
96-
ActiveDialog::SiegecraftPayment(_) => "siegecraft payment",
97-
ActiveDialog::SteelWeaponPayment(_) => "steel weapon payment",
9895
ActiveDialog::TradeRouteSelection(_) => "trade route selection",
96+
ActiveDialog::CustomPhasePaymentRequest(_) => "custom phase payment request",
9997
}
10098
}
10199

@@ -180,16 +178,13 @@ impl ActiveDialog {
180178
r.needed
181179
)],
182180
ActiveDialog::WaitingForUpdate => vec!["Waiting for server update".to_string()],
183-
ActiveDialog::SiegecraftPayment(_) => advance_help(rc, SIEGECRAFT),
184-
ActiveDialog::SteelWeaponPayment(p) => {
185-
let mut result = vec![format!(
186-
"{}: ",
187-
if p.attacker { "Attacker" } else { "Defender" },
188-
)];
189-
add_advance_help(rc, &mut result, STEEL_WEAPONS);
190-
result
191-
}
192181
ActiveDialog::TradeRouteSelection(_) => vec!["Select trade route reward".to_string()],
182+
ActiveDialog::CustomPhasePaymentRequest(_r) => {
183+
match &rc.game.custom_phase_state.current.as_ref().unwrap().origin {
184+
EventOrigin::Advance(a) => advance_help(rc, a),
185+
_ => vec![], // TODO
186+
}
187+
}
193188
}
194189
}
195190

@@ -507,6 +502,22 @@ impl State {
507502

508503
#[must_use]
509504
pub fn game_state_dialog(&self, game: &Game) -> ActiveDialog {
505+
if let Some(e) = &game.custom_phase_state.current {
506+
return match &e.request {
507+
CustomPhaseRequest::Payment(r) => ActiveDialog::CustomPhasePaymentRequest(
508+
r.iter()
509+
.map(|p| {
510+
Payment::new(
511+
&p.model,
512+
&game.get_player(game.active_player()).resources,
513+
&p.name,
514+
p.optional,
515+
)
516+
})
517+
.collect(),
518+
),
519+
};
520+
}
510521
match &game.state {
511522
GameState::Playing | GameState::Finished => ActiveDialog::None,
512523
GameState::Movement(move_state) => ActiveDialog::MoveUnits(MoveSelection::new(
@@ -558,15 +569,6 @@ impl State {
558569
})
559570
}
560571
GameState::CustomPhase(c) => match c {
561-
CustomPhaseState::SiegecraftPayment(_) => {
562-
ActiveDialog::SiegecraftPayment(SiegecraftPaymentDialog::new(game))
563-
}
564-
CustomPhaseState::SteelWeaponsAttacker(c) => {
565-
steel_weapons_dialog(game, c, c.attacker)
566-
}
567-
CustomPhaseState::SteelWeaponsDefender(c) => {
568-
steel_weapons_dialog(game, c, c.defender)
569-
}
570572
CustomPhaseState::TradeRouteSelection => trade_route_dialog(game),
571573
},
572574
}

client/src/combat_ui.rs

Lines changed: 1 addition & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
use crate::client_state::{ActiveDialog, StateUpdate};
1+
use crate::client_state::StateUpdate;
22
use crate::dialog_ui::{cancel_button_with_tooltip, ok_button, OkTooltip};
3-
use crate::payment_ui::{multi_payment_dialog, payment_dialog, Payment};
43
use crate::render_context::RenderContext;
54
use crate::select_ui::ConfirmSelection;
65
use crate::unit_ui;
76
use crate::unit_ui::UnitSelection;
87
use server::action::{Action, CombatAction, PlayActionCard};
9-
use server::combat::Combat;
10-
use server::content::custom_phase_actions::{
11-
steel_weapons_cost, CustomPhaseAction, SiegecraftPayment, SIEGECRAFT_EXTRA_DIE,
12-
SIEGECRAFT_IGNORE_HIT,
13-
};
148
use server::game::Game;
159
use server::position::Position;
1610
use server::unit::Unit;
@@ -98,97 +92,3 @@ pub fn play_action_card_dialog(rc: &RenderContext) -> StateUpdate {
9892
}
9993
StateUpdate::None
10094
}
101-
102-
#[derive(Clone)]
103-
pub struct SiegecraftPaymentDialog {
104-
extra_die: Payment,
105-
ignore_hit: Payment,
106-
}
107-
108-
impl SiegecraftPaymentDialog {
109-
pub fn new(game: &Game) -> SiegecraftPaymentDialog {
110-
let available = game.get_player(game.active_player()).resources.clone();
111-
SiegecraftPaymentDialog {
112-
extra_die: Payment::new(
113-
&SIEGECRAFT_EXTRA_DIE,
114-
&available,
115-
"Cancel fortress extra die in first round of combat",
116-
true,
117-
),
118-
ignore_hit: Payment::new(
119-
&SIEGECRAFT_IGNORE_HIT,
120-
&available,
121-
"Cancel fortress ignore hit in first round of combat",
122-
true,
123-
),
124-
}
125-
}
126-
}
127-
128-
pub fn pay_siegecraft_dialog(p: &SiegecraftPaymentDialog, rc: &RenderContext) -> StateUpdate {
129-
multi_payment_dialog(
130-
rc,
131-
&[p.extra_die.clone(), p.ignore_hit.clone()],
132-
|p| {
133-
ActiveDialog::SiegecraftPayment(SiegecraftPaymentDialog {
134-
extra_die: p[0].clone(),
135-
ignore_hit: p[1].clone(),
136-
})
137-
},
138-
false,
139-
|p| {
140-
StateUpdate::Execute(Action::CustomPhase(
141-
CustomPhaseAction::SiegecraftPaymentAction(SiegecraftPayment {
142-
extra_die: p[0].clone(),
143-
ignore_hit: p[1].clone(),
144-
}),
145-
))
146-
},
147-
)
148-
}
149-
150-
#[derive(Clone)]
151-
pub struct SteelWeaponDialog {
152-
pub attacker: bool,
153-
pub payment: Payment,
154-
pub combat: Combat,
155-
}
156-
157-
pub fn steel_weapons_dialog(game: &Game, c: &Combat, player_index: usize) -> ActiveDialog {
158-
let model = &steel_weapons_cost(game, c, player_index);
159-
let available = &game.get_player(player_index).resources;
160-
let payment = Payment::new(model, available, "Use steel weapons", true);
161-
ActiveDialog::SteelWeaponPayment(SteelWeaponDialog {
162-
attacker: player_index == c.attacker,
163-
payment,
164-
combat: c.clone(),
165-
})
166-
}
167-
168-
pub(crate) fn pay_steel_weapons_dialog(
169-
rc: &RenderContext,
170-
dialog: &SteelWeaponDialog,
171-
) -> StateUpdate {
172-
let attacker = dialog.attacker;
173-
174-
payment_dialog(
175-
rc,
176-
&dialog.payment.clone(),
177-
|p| {
178-
let mut n = dialog.clone();
179-
n.payment = p;
180-
ActiveDialog::SteelWeaponPayment(n)
181-
},
182-
|p| {
183-
if attacker {
184-
StateUpdate::Execute(Action::CustomPhase(
185-
CustomPhaseAction::SteelWeaponsAttackerAction(p),
186-
))
187-
} else {
188-
StateUpdate::Execute(Action::CustomPhase(
189-
CustomPhaseAction::SteelWeaponsDefenderAction(p),
190-
))
191-
}
192-
},
193-
)
194-
}

client/src/custom_actions_ui.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::client_state::{ActiveDialog, StateUpdate};
2-
use crate::payment_ui::{payment_dialog, Payment};
2+
use crate::payment_ui::{multi_payment_dialog, payment_dialog, Payment};
33
use crate::render_context::RenderContext;
44
use server::action::Action;
5-
use server::content::custom_phase_actions::CustomPhaseAction;
5+
use server::content::custom_phase_actions::{CustomPhaseAction, CustomPhaseEventAction};
66
use server::content::trade_routes::trade_route_reward;
77
use server::game::Game;
88

@@ -23,3 +23,17 @@ pub fn trade_route_selection_dialog(rc: &RenderContext, payment: &Payment) -> St
2323
},
2424
)
2525
}
26+
27+
pub fn custom_phase_payment_dialog(rc: &RenderContext, payments: &[Payment]) -> StateUpdate {
28+
multi_payment_dialog(
29+
rc,
30+
payments,
31+
|p| ActiveDialog::CustomPhasePaymentRequest(p.clone()),
32+
false,
33+
|p| {
34+
StateUpdate::Execute(Action::CustomPhaseEvent(CustomPhaseEventAction::Payment(
35+
p.clone(),
36+
)))
37+
},
38+
)
39+
}

client/src/payment_ui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fn resource_payment(options: &PaymentOptions) -> Vec<ResourcePayment> {
307307
#[must_use]
308308
pub fn show_types(model: &PaymentModel) -> Vec<ResourceType> {
309309
match model {
310-
PaymentModel::Sum(options) => options.types_by_preference.to_vec(),
310+
PaymentModel::Sum(options) => options.types_by_preference.clone(),
311311
PaymentModel::Resources(options) => options.cost.types(),
312312
}
313313
}

0 commit comments

Comments
 (0)