Skip to content

Commit

Permalink
Auto move (#131)
Browse files Browse the repository at this point in the history
* auto move

* auto end move

* auto end move

* auto end move

* auto end move

* auto end move

* auto end move

* fix payment
  • Loading branch information
zeitlinger authored Jan 21, 2025
1 parent 6de4d9f commit 3abf26f
Show file tree
Hide file tree
Showing 84 changed files with 428 additions and 1,126 deletions.
25 changes: 14 additions & 11 deletions client/src/action_buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@ pub fn action_buttons(rc: &RenderContext) -> StateUpdate {
if rc.can_play_action(PlayingActionType::MoveUnits)
&& bottom_left_texture(rc, &assets.move_units, icon_pos(0, -3), "Move units")
{
return StateUpdate::MoveUnits(
if rc
.state
.focused_tile
.is_some_and(|t| rc.game.map.is_water(t))
{
MoveIntent::Sea
} else {
MoveIntent::Land
},
);
return global_move(rc);
}

if rc.can_play_action(PlayingActionType::Advance)
Expand Down Expand Up @@ -74,6 +64,19 @@ pub fn action_buttons(rc: &RenderContext) -> StateUpdate {
StateUpdate::None
}

fn global_move(rc: &RenderContext) -> StateUpdate {
let pos = rc.state.focused_tile;
StateUpdate::move_units(
rc,
pos,
if pos.is_some_and(|t| rc.game.map.is_water(t)) {
MoveIntent::Sea
} else {
MoveIntent::Land
},
)
}

fn custom_action_tooltip(custom_action_type: &CustomActionType) -> String {
match custom_action_type {
CustomActionType::ConstructWonder => "Construct a wonder".to_string(),
Expand Down
3 changes: 2 additions & 1 deletion client/src/advance_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::log_ui::break_text;
use crate::payment_ui::{payment_dialog, HasPayment, Payment, ResourcePayment};
use crate::player_ui::player_color;
use crate::render_context::RenderContext;
use crate::resource_ui::new_resource_map;
use crate::select_ui::HasCountSelectableObject;
use crate::tooltip::show_tooltip_for_rect;
use macroquad::color::Color;
Expand All @@ -20,7 +21,7 @@ use server::game::GameState;
use server::payment::PaymentModel;
use server::player::Player;
use server::playing_actions::PlayingAction;
use server::resource::{new_resource_map, ResourceType};
use server::resource::ResourceType;
use server::status_phase::StatusPhaseAction;
use std::cmp::min;
use std::ops::Rem;
Expand Down
2 changes: 1 addition & 1 deletion client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ fn render_active_dialog(rc: &RenderContext) -> StateUpdate {
let state = rc.state;
match &state.active_dialog {
ActiveDialog::None
| ActiveDialog::MoveUnits(_)
| ActiveDialog::WaitingForUpdate
| ActiveDialog::CulturalInfluence
| ActiveDialog::PlaceSettler => StateUpdate::None,
Expand All @@ -145,6 +144,7 @@ fn render_active_dialog(rc: &RenderContext) -> StateUpdate {
influence_ui::cultural_influence_resolution_dialog(rc, r)
}
ActiveDialog::ExploreResolution(r) => explore_dialog(rc, r),
ActiveDialog::MoveUnits(_) => move_ui::move_units_dialog(rc),

//status phase
ActiveDialog::FreeAdvance => show_free_advance_menu(rc),
Expand Down
29 changes: 18 additions & 11 deletions client/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use server::action::Action;
use server::city::{City, MoodState};
use server::combat::{active_attackers, active_defenders, CombatPhase};
use server::content::advances::{NAVIGATION, ROADS};
use server::game::{CulturalInfluenceResolution, Game, GameState};
use server::playing_actions::PlayingAction;
use server::game::{CulturalInfluenceResolution, CurrentMove, Game, GameState};
use server::position::Position;
use server::status_phase::{StatusPhaseAction, StatusPhaseState};

Expand Down Expand Up @@ -223,7 +222,6 @@ pub struct DialogChooser {
pub enum StateUpdate {
None,
OpenDialog(ActiveDialog),
MoveUnits(MoveIntent),
CloseDialog,
Cancel,
ResolvePendingUpdate(bool),
Expand Down Expand Up @@ -306,6 +304,21 @@ impl StateUpdate {
StateUpdate::Execute(Action::StatusPhase(action))
}

pub fn move_units(
rc: &RenderContext,
pos: Option<Position>,
intent: MoveIntent,
) -> StateUpdate {
let game = rc.game;
StateUpdate::OpenDialog(ActiveDialog::MoveUnits(MoveSelection::new(
game.active_player(),
pos,
game,
intent,
&CurrentMove::None,
)))
}

pub fn or(self, other: impl FnOnce() -> StateUpdate) -> StateUpdate {
match self {
StateUpdate::None => other(),
Expand Down Expand Up @@ -364,7 +377,6 @@ pub struct State {
pub mouse_positions: Vec<MousePosition>,
pub log_scroll: f32,
pub focused_tile: Option<Position>,
pub move_intent: MoveIntent,
pub pan_map: bool,
}

Expand All @@ -390,7 +402,6 @@ impl State {
mouse_positions: vec![],
log_scroll: 0.0,
focused_tile: None,
move_intent: MoveIntent::Land, // is set before use
pan_map: false,
}
}
Expand All @@ -415,10 +426,6 @@ impl State {
match update {
StateUpdate::None => GameSyncRequest::None,
StateUpdate::Execute(a) => GameSyncRequest::ExecuteAction(a),
StateUpdate::MoveUnits(intent) => {
self.move_intent = intent;
GameSyncRequest::ExecuteAction(Action::Playing(PlayingAction::MoveUnits))
}
StateUpdate::ExecuteWithWarning(update) => {
self.pending_update = Some(update);
GameSyncRequest::None
Expand Down Expand Up @@ -490,8 +497,8 @@ impl State {
game.active_player(),
self.focused_tile,
game,
&self.move_intent,
move_state,
MoveIntent::Land, // is not used, because no tile is focused
&move_state.current_move,
)),
GameState::CulturalInfluenceResolution(c) => {
ActiveDialog::CulturalInfluenceResolution(c.clone())
Expand Down
4 changes: 2 additions & 2 deletions client/src/collect_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::layout_ui::{
draw_icon, draw_scaled_icon, is_in_circle, left_mouse_button_pressed, ICON_SIZE,
};
use crate::render_context::RenderContext;
use crate::resource_ui::{resource_name, show_resource_pile};
use crate::resource_ui::{new_resource_map, resource_name, show_resource_pile};
use macroquad::color::BLACK;
use macroquad::math::vec2;
use macroquad::prelude::WHITE;
Expand All @@ -22,7 +22,7 @@ use server::content::custom_actions::CustomAction;
use server::game::Game;
use server::playing_actions::{get_total_collection, Collect, PlayingAction};
use server::position::Position;
use server::resource::{new_resource_map, resource_types, ResourceType};
use server::resource::{resource_types, ResourceType};
use server::resource_pile::ResourcePile;

#[derive(Clone)]
Expand Down
3 changes: 2 additions & 1 deletion client/src/construct_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::dialog_ui::OkTooltip;
use crate::payment_ui::{payment_dialog, HasPayment, Payment, ResourcePayment};
use crate::recruit_unit_ui::RecruitSelection;
use crate::render_context::RenderContext;
use crate::resource_ui::new_resource_map;
use crate::select_ui::CountSelector;
use server::action::Action;
use server::city::City;
Expand All @@ -15,7 +16,7 @@ use server::content::custom_actions::CustomAction;
use server::map::Terrain;
use server::playing_actions::{Construct, PlayingAction, Recruit};
use server::position::Position;
use server::resource::{new_resource_map, ResourceType};
use server::resource::ResourceType;
use server::resource_pile::PaymentOptions;
use server::unit::UnitType;

Expand Down
4 changes: 2 additions & 2 deletions client/src/local_client/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ pub fn setup_local_game() -> Game {

let player_index1 = 0;
let player_index2 = 1;
game.players[player_index1].gain_resources(ResourcePile::new(50, 50, 50, 50, 50, 9, 9));
game.players[player_index2].gain_resources(ResourcePile::new(50, 50, 50, 50, 50, 9, 9));
game.players[player_index1].gain_resources(ResourcePile::new(0, 5, 5, 5, 5, 9, 9));
game.players[player_index2].gain_resources(ResourcePile::new(0, 5, 5, 5, 5, 9, 9));
add_city(&mut game, player_index1, "A1");
add_city(&mut game, player_index1, "C2");
add_city(&mut game, player_index1, "B1");
Expand Down
2 changes: 1 addition & 1 deletion client/src/map_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ pub fn move_units_button<'a>(
Some((
move_intent.icon(rc),
move_intent.toolip().to_string(),
Box::new(move || StateUpdate::MoveUnits(move_intent.clone())),
Box::new(move || StateUpdate::move_units(rc, Some(pos), move_intent)),
))
}

Expand Down
25 changes: 17 additions & 8 deletions client/src/move_ui.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use macroquad::math::{u32, Vec2};
use macroquad::prelude::Texture2D;
use server::action::Action;
use server::game::{CurrentMove, Game, MoveState};
use server::game::{CurrentMove, Game, GameState};
use server::player::Player;
use server::position::Position;
use server::unit::{MovementAction, Unit, UnitType};

use crate::client_state::{ActiveDialog, StateUpdate};
use crate::dialog_ui::cancel_button_with_tooltip;
use crate::render_context::RenderContext;
use crate::unit_ui::{click_unit, unit_selection_clicked};

#[derive(Clone)]
#[derive(Clone, Copy)]
pub enum MoveIntent {
Land,
Sea,
Disembark,
}

impl MoveIntent {
pub fn to_predicate(&self) -> impl Fn(&Unit) -> bool {
pub fn to_predicate(self) -> impl Fn(&Unit) -> bool {
match self {
MoveIntent::Land => |u: &Unit| u.unit_type.is_land_based() && !u.is_transported(),
MoveIntent::Sea => |u: &Unit| !u.unit_type.is_land_based(),
Expand All @@ -34,7 +35,7 @@ impl MoveIntent {
}
}

pub fn icon<'a>(&self, rc: &'a RenderContext) -> &'a Texture2D {
pub fn icon<'a>(self, rc: &'a RenderContext) -> &'a Texture2D {
match self {
MoveIntent::Land => &rc.assets().move_units,
MoveIntent::Sea => &rc.assets().units[&UnitType::Ship],
Expand Down Expand Up @@ -177,18 +178,17 @@ pub struct MoveSelection {
pub units: Vec<u32>,
pub start: Option<Position>,
pub destinations: Vec<MoveDestination>,
// pub lo
}

impl MoveSelection {
pub fn new(
player_index: usize,
start: Option<Position>,
game: &Game,
move_intent: &MoveIntent,
move_state: &MoveState,
move_intent: MoveIntent,
current_move: &CurrentMove,
) -> MoveSelection {
if let CurrentMove::Fleet { units } = &move_state.current_move {
if let CurrentMove::Fleet { units } = current_move {
let fleet_pos = game
.get_player(player_index)
.get_unit(units[0])
Expand Down Expand Up @@ -233,3 +233,12 @@ impl MoveSelection {
}
}
}

pub(crate) fn move_units_dialog(rc: &RenderContext) -> StateUpdate {
if matches!(rc.game.state, GameState::Playing)
&& cancel_button_with_tooltip(rc, "Back to playing actions")
{
return StateUpdate::CloseDialog;
}
StateUpdate::None
}
4 changes: 2 additions & 2 deletions client/src/player_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::layout_ui::{
};
use crate::map_ui::terrain_name;
use crate::render_context::RenderContext;
use crate::resource_ui::resource_name;
use crate::resource_ui::{new_resource_map, resource_name};
use crate::tooltip::show_tooltip_for_rect;
use crate::unit_ui;
use macroquad::math::vec2;
Expand All @@ -17,7 +17,7 @@ use server::action::Action;
use server::consts::ARMY_MOVEMENT_REQUIRED_ADVANCE;
use server::game::{CurrentMove, Game, GameState, MoveState};
use server::playing_actions::PlayingAction;
use server::resource::{new_resource_map, resource_types};
use server::resource::resource_types;
use server::unit::MovementAction;

pub fn player_select(rc: &RenderContext) -> StateUpdate {
Expand Down
21 changes: 20 additions & 1 deletion client/src/resource_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::layout_ui::icon_pos;
use crate::player_ui::bottom_icon_with_label;
use crate::render_context::RenderContext;
use macroquad::math::vec2;
use server::resource::{new_resource_map, resource_types, ResourceType};
use server::resource::{resource_types, ResourceType};
use server::resource_pile::ResourcePile;
use std::collections::HashMap;

pub fn resource_name(t: ResourceType) -> &'static str {
match t {
Expand All @@ -18,6 +19,24 @@ 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();
add_resource(&mut m, p.food, ResourceType::Food);
add_resource(&mut m, p.wood, ResourceType::Wood);
add_resource(&mut m, p.ore, ResourceType::Ore);
add_resource(&mut m, p.ideas, ResourceType::Ideas);
add_resource(&mut m, p.gold as u32, ResourceType::Gold);
add_resource(&mut m, p.mood_tokens, ResourceType::MoodTokens);
add_resource(&mut m, p.culture_tokens, ResourceType::CultureTokens);
add_resource(&mut m, 0, ResourceType::Discount);
m
}

fn add_resource(m: &mut HashMap<ResourceType, u32>, amount: u32, resource_type: ResourceType) {
m.insert(resource_type, amount);
}

pub fn show_resource_pile(rc: &RenderContext, p: &ResourcePile, must_show: &[ResourceType]) {
let resource_map = new_resource_map(p);
let show: Vec<ResourceType> = resource_types()
Expand Down
Loading

0 comments on commit 3abf26f

Please sign in to comment.