Skip to content

Commit aa072ba

Browse files
authored
cleanup (#178)
1 parent 9f7b2af commit aa072ba

File tree

277 files changed

+1579
-2239
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

277 files changed

+1579
-2239
lines changed

client/src/action_buttons.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub fn base_or_custom_available(
164164
custom: &CustomActionType,
165165
) -> bool {
166166
rc.can_play_action(action)
167-
|| (rc.game.state() == &GameState::Playing
167+
|| (rc.game.state == GameState::Playing
168168
&& rc
169169
.game
170170
.is_custom_action_available(rc.shown_player.index, custom))

client/src/advance_ui.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ pub fn show_paid_advance_menu(rc: &RenderContext) -> StateUpdate {
3838
|a, p| {
3939
if p.has_advance(&a.name) {
4040
AdvanceState::Owned
41-
} else if game.state() == &GameState::Playing
42-
&& game.actions_left > 0
43-
&& p.can_advance(a)
41+
} else if game.state == GameState::Playing && game.actions_left > 0 && p.can_advance(a)
4442
{
4543
AdvanceState::Available
4644
} else {

client/src/client_state.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ impl State {
552552
}
553553
};
554554
}
555-
match &game.state() {
555+
match &game.state {
556556
GameState::Playing | GameState::Finished => ActiveDialog::None,
557557
GameState::Movement(move_state) => ActiveDialog::MoveUnits(MoveSelection::new(
558558
game.active_player(),
@@ -561,7 +561,6 @@ impl State {
561561
MoveIntent::Land, // is not used, because no tile is focused
562562
&move_state.current_move,
563563
)),
564-
GameState::Combat(_) | GameState::StatusPhase(_) => panic!("should be in custom phase"),
565564
}
566565
}
567566

client/src/map_ui.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::client_state::{ActiveDialog, State, StateUpdate, MAX_OFFSET, MIN_OFFS
33
use crate::dialog_ui::{cancel_button_pos, ok_button, OkTooltip};
44
use crate::layout_ui::{bottom_center_texture, bottom_right_texture, icon_pos};
55
use crate::move_ui::{movable_units, MoveDestination, MoveIntent};
6+
use crate::player_ui::get_combat;
67
use crate::render_context::RenderContext;
78
use crate::select_ui::HighlightType;
89
use crate::{collect_ui, hex_ui, unit_ui};
@@ -11,7 +12,6 @@ use macroquad::prelude::*;
1112
use server::action::Action;
1213
use server::combat::Combat;
1314
use server::content::custom_phase_actions::CurrentEventResponse;
14-
use server::game::GameState;
1515
use server::map::{Rotation, Terrain, UnexploredBlock};
1616
use server::playing_actions::{PlayingAction, PlayingActionType};
1717
use server::position::Position;
@@ -74,7 +74,8 @@ pub fn draw_map(rc: &RenderContext) -> StateUpdate {
7474
return update;
7575
}
7676
}
77-
if let GameState::Combat(c) = &game.state() {
77+
// get from current event
78+
if let Some(c) = get_combat(game) {
7879
draw_combat_arrow(c);
7980
}
8081
let state = &rc.state;

client/src/move_ui.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl MoveSelection {
265265
}
266266

267267
pub(crate) fn move_units_dialog(rc: &RenderContext) -> StateUpdate {
268-
if matches!(rc.game.state(), GameState::Playing)
268+
if matches!(rc.game.state, GameState::Playing)
269269
&& cancel_button_with_tooltip(rc, "Back to playing actions")
270270
{
271271
return StateUpdate::CloseDialog;

client/src/player_ui.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ use crate::unit_ui;
1515
use macroquad::math::vec2;
1616
use macroquad::prelude::*;
1717
use server::action::Action;
18+
use server::combat::Combat;
1819
use server::consts::ARMY_MOVEMENT_REQUIRED_ADVANCE;
19-
use server::game::GameState::Movement;
20+
use server::content::custom_phase_actions::CurrentEventType;
2021
use server::game::{Game, GameState};
21-
use server::movement::{CurrentMove, MoveState};
22+
use server::movement::CurrentMove;
2223
use server::playing_actions::PlayingAction;
2324
use server::resource::ResourceType;
25+
use server::status_phase::get_status_phase;
2426
use server::unit::MovementAction;
2527

2628
pub fn player_select(rc: &RenderContext) -> StateUpdate {
@@ -156,13 +158,14 @@ pub fn show_top_left(rc: &RenderContext) {
156158

157159
let game = rc.game;
158160

159-
match &game.state() {
161+
match &game.state {
160162
GameState::Finished => label("Finished"),
161163
_ => label(&format!("Age {}", game.age)),
162164
}
163-
match &game.state() {
164-
GameState::StatusPhase(ref p) => label(&format!("Status Phase: {p:?}")),
165-
_ => label(&format!("Round {}", game.round)),
165+
if let Some(s) = get_status_phase(game) {
166+
label(&format!("Status Phase: {s:?}"));
167+
} else {
168+
label(&format!("Round {}", game.round));
166169
}
167170

168171
let player = rc.shown_player;
@@ -181,11 +184,10 @@ pub fn show_top_left(rc: &RenderContext) {
181184
));
182185

183186
if game.current_player_index == player.index {
184-
match &game.state() {
185-
GameState::StatusPhase(_) | GameState::Finished => {}
186-
_ => label(&format!("{} actions left", game.actions_left)),
187+
if get_status_phase(game).is_none() && game.state != GameState::Finished {
188+
label(&format!("{} actions left", game.actions_left));
187189
}
188-
if let Some(moves) = move_state(game) {
190+
if let GameState::Movement(moves) = &game.state {
189191
let movement_actions_left = moves.movement_actions_left;
190192
label(&format!("Move units: {movement_actions_left} moves left"));
191193
match moves.current_move {
@@ -201,7 +203,7 @@ pub fn show_top_left(rc: &RenderContext) {
201203
}
202204
}
203205

204-
if let GameState::Combat(c) = &game.state() {
206+
if let Some(c) = get_combat(game) {
205207
if c.attacker == player.index {
206208
label(&format!("Attack - combat round {}", c.round));
207209
} else if c.defender == player.index {
@@ -247,10 +249,15 @@ pub fn show_top_left(rc: &RenderContext) {
247249
}
248250
}
249251

250-
pub fn move_state(game: &Game) -> Option<&MoveState> {
251-
game.state_stack
252-
.iter()
253-
.find_map(|s| if let Movement(m) = s { Some(m) } else { None })
252+
pub fn get_combat(game: &Game) -> Option<&Combat> {
253+
game.current_events
254+
.last()
255+
.and_then(|e| match &e.event_type {
256+
CurrentEventType::CombatStart(c) => Some(c),
257+
CurrentEventType::CombatRoundEnd(e) => Some(&e.combat),
258+
CurrentEventType::CombatEnd(e) => Some(&e.combat),
259+
_ => None,
260+
})
254261
}
255262

256263
pub fn show_global_controls(rc: &RenderContext, features: &Features) -> StateUpdate {
@@ -291,15 +298,15 @@ pub fn show_global_controls(rc: &RenderContext, features: &Features) -> StateUpd
291298
}
292299

293300
fn can_end_move(game: &Game) -> Option<&str> {
294-
match game.state() {
301+
match game.state {
295302
GameState::Movement { .. } => Some("End movement"),
296303
GameState::Playing => Some("End turn"),
297-
_ => None,
304+
GameState::Finished => None,
298305
}
299306
}
300307

301308
fn end_move(game: &Game) -> StateUpdate {
302-
if let GameState::Movement(m) = &game.state() {
309+
if let GameState::Movement(m) = &game.state {
303310
let movement_actions_left = m.movement_actions_left;
304311
return StateUpdate::execute_with_warning(
305312
Action::Movement(MovementAction::Stop),

client/src/render_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl RenderContext<'_> {
6565

6666
pub fn can_play_action(&self, action: PlayingActionType) -> bool {
6767
self.can_control()
68-
&& self.game.state() == &GameState::Playing
68+
&& self.game.state == GameState::Playing
6969
&& self.game.actions_left > 0
7070
&& action.is_available(self.game, self.shown_player.index)
7171
}

server/src/action.rs

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use crate::advance::gain_advance;
22
use crate::combat::{
3-
combat_loop, combat_round_end, end_combat, move_with_possible_combat, start_combat, take_combat,
3+
combat_loop, combat_round_end, end_combat, move_with_possible_combat, start_combat,
44
};
55
use crate::content::custom_phase_actions::{CurrentEventResponse, CurrentEventType};
66
use crate::cultural_influence::ask_for_cultural_influence_payment;
77
use crate::explore::{ask_explore_resolution, move_to_unexplored_tile};
8-
use crate::game::GameState::{Combat, Finished, Movement, Playing, StatusPhase};
8+
use crate::game::GameState::{Finished, Movement, Playing};
99
use crate::game::{ActionLogItem, Game, GameState};
1010
use crate::incident::trigger_incident;
1111
use crate::log;
1212
use crate::map::Terrain::Unexplored;
1313
use crate::movement::{
14-
has_movable_units, move_units_destinations, take_move_state, CurrentMove, MoveState,
14+
get_move_state, has_movable_units, move_units_destinations, CurrentMove, MoveState,
1515
};
1616
use crate::playing_actions::PlayingAction;
1717
use crate::recruit::on_recruit;
@@ -148,19 +148,18 @@ pub(crate) fn execute_custom_phase_action(
148148
InfluenceCultureResolution(r) => {
149149
ask_for_cultural_influence_payment(game, player_index, r);
150150
}
151-
CombatStart => {
152-
start_combat(game);
151+
CombatStart(c) => {
152+
start_combat(game, c.clone());
153153
}
154154
CombatRoundEnd(r) => {
155155
if let Some(c) = combat_round_end(game, r) {
156156
combat_loop(game, c);
157157
}
158158
}
159159
CombatEnd(r) => {
160-
let c = take_combat(game);
161-
end_combat(game, c, r.clone());
160+
end_combat(game, r);
162161
}
163-
StatusPhase => play_status_phase(game),
162+
StatusPhase(s) => play_status_phase(game, s.clone()),
164163
TurnStart => game.start_turn(),
165164
Advance(a) => {
166165
gain_advance(game, player_index, a);
@@ -182,12 +181,16 @@ pub(crate) fn add_log_item_from_action(game: &mut Game, action: &Action) {
182181
}
183182

184183
fn execute_regular_action(game: &mut Game, action: Action, player_index: usize) {
185-
match game.state() {
184+
match game.state {
186185
Playing => {
187186
if let Some(m) = action.clone().movement() {
187+
if let MovementAction::Move(_) = m {
188+
} else {
189+
panic!("Illegal action");
190+
}
188191
assert_ne!(game.actions_left, 0, "Illegal action");
189192
game.actions_left -= 1;
190-
game.push_state(GameState::Movement(MoveState::new()));
193+
game.state = GameState::Movement(MoveState::new());
191194
execute_movement_action(game, m, player_index);
192195
} else {
193196
let action = action.playing().expect("action should be a playing action");
@@ -200,12 +203,6 @@ fn execute_regular_action(game: &mut Game, action: Action, player_index: usize)
200203
.expect("action should be a movement action");
201204
execute_movement_action(game, action, player_index);
202205
}
203-
Combat(_) => {
204-
panic!("actions can't be executed when the game is in a combat state");
205-
}
206-
StatusPhase(_) => {
207-
panic!("actions can't be executed when the game is in a status state");
208-
}
209206
Finished => panic!("actions can't be executed when the game is finished"),
210207
}
211208
}
@@ -247,16 +244,16 @@ pub(crate) fn execute_movement_action(
247244
}
248245
}
249246

250-
let mut move_state = take_move_state(game);
251-
move_state.moved_units.extend(m.units.iter());
252-
move_state.moved_units = move_state.moved_units.iter().unique().copied().collect();
253247
let current_move = get_current_move(
254248
game,
255249
&m.units,
256250
starting_position,
257251
m.destination,
258252
m.embark_carrier_id,
259253
);
254+
let move_state = get_move_state(game);
255+
move_state.moved_units.extend(m.units.iter());
256+
move_state.moved_units = move_state.moved_units.iter().unique().copied().collect();
260257

261258
if matches!(current_move, CurrentMove::None) || move_state.current_move != current_move
262259
{
@@ -267,7 +264,6 @@ pub(crate) fn execute_movement_action(
267264
// roads move ends the current move
268265
move_state.current_move = CurrentMove::None;
269266
}
270-
game.push_state(GameState::Movement(move_state));
271267

272268
let dest_terrain = game
273269
.map
@@ -290,16 +286,15 @@ pub(crate) fn execute_movement_action(
290286
}
291287
}
292288
Stop => {
293-
game.pop_state();
289+
game.state = GameState::Playing;
294290
return;
295291
}
296292
};
297293

298-
let state = take_move_state(game);
294+
let state = get_move_state(game);
299295
let all_moves_used =
300296
state.movement_actions_left == 0 && state.current_move == CurrentMove::None;
301297
if all_moves_used || !has_movable_units(game, game.get_player(game.current_player_index)) {
302-
return;
298+
game.state = GameState::Playing;
303299
}
304-
game.push_state(GameState::Movement(state));
305300
}

server/src/advance.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,16 @@ pub(crate) fn advance_with_incident_token(
187187
}
188188

189189
pub(crate) fn gain_advance(game: &mut Game, player_index: usize, info: &AdvanceInfo) {
190-
if game.trigger_current_event(
191-
&[player_index],
192-
|e| &mut e.on_advance,
193-
info,
194-
CurrentEventType::Advance,
195-
None,
196-
) {
190+
if game
191+
.trigger_current_event(
192+
&[player_index],
193+
|e| &mut e.on_advance,
194+
info,
195+
CurrentEventType::Advance,
196+
None,
197+
)
198+
.is_none()
199+
{
197200
return;
198201
}
199202
let player = &mut game.players[player_index];

0 commit comments

Comments
 (0)