Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #143

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/advance_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn description(p: &Player, a: &Advance) -> Vec<String> {
parts.push(format!("Government: {g}"));
}
if let Some(u) = &a.unlocked_building {
parts.push(format!("Unlocks: {u}"));
parts.push(format!("Unlocks: {}", u.name()));
}

parts
Expand Down
35 changes: 6 additions & 29 deletions client/src/city_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ fn building_icons<'a>(rc: &'a RenderContext, city: &'a City) -> IconActionVec<'a
return vec![];
}
let owner = rc.shown_player;
building_names()
Building::all()
.iter()
.filter_map(|(b, _)| {
.filter_map(|b| {
if city.can_construct(*b, owner, rc.game) {
Some(*b)
} else {
Expand All @@ -117,7 +117,7 @@ fn building_icons<'a>(rc: &'a RenderContext, city: &'a City) -> IconActionVec<'a
})
.flat_map(|b| new_building_positions(b, rc, city))
.map(|(b, pos)| {
let name = building_name(b);
let name = b.name();
let tooltip = format!(
"Built {}{} for {}",
name,
Expand Down Expand Up @@ -216,13 +216,9 @@ pub fn city_labels(game: &Game, city: &City) -> Vec<String> {
.filter_map(|(b, o)| {
o.as_ref().map(|o| {
if city.player_index == *o {
building_name(*b).to_string()
b.name().to_string()
} else {
format!(
"{} (owned by {})",
building_name(*b),
game.get_player(*o).get_name()
)
format!("{} (owned by {})", b.name(), game.get_player(*o).get_name())
}
})
})
Expand Down Expand Up @@ -296,7 +292,7 @@ pub fn draw_city(rc: &RenderContext, city: &City) {
let tooltip = if matches!(state.active_dialog, ActiveDialog::CulturalInfluence) {
""
} else {
building_name(*b)
b.name()
};
draw_scaled_icon(
rc,
Expand All @@ -322,22 +318,3 @@ pub fn building_position(city: &City, center: Point, i: usize, building: Buildin
hex_ui::rotate_around(center, 25.0, 90 * i)
}
}

pub fn building_name(b: Building) -> &'static str {
building_names()
.iter()
.find_map(|(b2, n)| if &b == b2 { Some(n) } else { None })
.unwrap()
}

fn building_names() -> [(Building, &'static str); 7] {
[
(Building::Academy, "Academy"),
(Building::Market, "Market"),
(Building::Obelisk, "Obelisk"),
(Building::Observatory, "Observatory"),
(Building::Fortress, "Fortress"),
(Building::Port, "Port"),
(Building::Temple, "Temple"),
]
}
3 changes: 1 addition & 2 deletions client/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use server::position::Position;
use server::status_phase::{StatusPhaseAction, StatusPhaseState};

use crate::assets::Assets;
use crate::city_ui::building_name;
use crate::client::{Features, GameSyncRequest};
use crate::collect_ui::CollectResources;
use crate::combat_ui::{
Expand Down Expand Up @@ -150,7 +149,7 @@ impl ActiveDialog {
ActiveDialog::CulturalInfluenceResolution(c) => vec![format!(
"Pay {} culture tokens to influence {}",
c.roll_boost_cost,
building_name(c.city_piece)
c.city_piece.name()
)],
ActiveDialog::ExploreResolution(_) => {
vec!["Click on the new tile to rotate it".to_string()]
Expand Down
6 changes: 3 additions & 3 deletions client/src/influence_ui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::city_ui::{building_name, building_position, BUILDING_SIZE};
use crate::city_ui::{building_position, BUILDING_SIZE};
use crate::client_state::{CameraMode, StateUpdate};
use crate::dialog_ui::{cancel_button_with_tooltip, ok_button, OkTooltip};
use crate::hex_ui;
Expand Down Expand Up @@ -31,7 +31,7 @@ pub fn cultural_influence_resolution_dialog(
rc: &RenderContext,
r: &CulturalInfluenceResolution,
) -> StateUpdate {
let name = building_name(r.city_piece);
let name = r.city_piece.name();
let pile = ResourcePile::culture_tokens(r.roll_boost_cost);
show_resource_pile(rc, &pile, &[ResourceType::CultureTokens]);
if ok_button(rc, OkTooltip::Valid(format!("Influence {name} for {pile}"))) {
Expand Down Expand Up @@ -78,7 +78,7 @@ fn show_city(rc: &RenderContext, mouse_pos: Vec2, city: &City) -> Option<StateUp
*b,
) {
if player.can_afford_resources(&cost) {
let name = building_name(*b);
let name = b.name();
let _ = rc.with_camera(CameraMode::World, |rc| {
draw_circle_lines(center.x, center.y, BUILDING_SIZE, 1., WHITE);
show_tooltip_for_circle(
Expand Down
2 changes: 1 addition & 1 deletion client/src/local_client/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn setup_local_game() -> Game {
vec![unit_type],
Position::from_offset(pos),
None,
vec![],
&[],
);
};

Expand Down
9 changes: 7 additions & 2 deletions client/src/unit_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ pub fn click_unit(
.find_map(|(i, u)| {
let place = unit_place(rc, i, pos);
if can_select_carried_units {
let carried_units = carried_units(rc.game, player.index, u.id);
let game = rc.game;
let player_index = player.index;
let carried_units = carried_units(u.id, &game.players[player_index]);
for (j, carried) in carried_units.iter().enumerate() {
let carried_place = carried_unit_place(&place, j);
if is_in_circle(mouse_pos, carried_place.center, carried_place.radius) {
Expand Down Expand Up @@ -185,7 +187,10 @@ pub fn draw_units(rc: &RenderContext, tooltip: bool) {
draw_unit(rc, tooltip, &highlighted_units, *p, u, &place);

let player = rc.game.get_player(*p);
let carried = carried_units(rc.game, *p, u.id);
let game = rc.game;
let player_index = *p;
let carrier = u.id;
let carried = carried_units(carrier, &game.players[player_index]);
carried.iter().enumerate().for_each(|(j, u)| {
draw_unit(
rc,
Expand Down
9 changes: 5 additions & 4 deletions server/src/advance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
resource_pile::ResourcePile,
};

use crate::city_pieces::Building;
use Bonus::*;

pub struct Advance {
Expand All @@ -12,7 +13,7 @@ pub struct Advance {
pub bonus: Option<Bonus>,
pub required: Option<String>,
pub contradicting: Vec<String>,
pub unlocked_building: Option<String>,
pub unlocked_building: Option<Building>,
pub government: Option<String>,
pub player_initializer: AbilityInitializer,
pub player_deinitializer: AbilityInitializer,
Expand All @@ -39,7 +40,7 @@ pub(crate) struct AdvanceBuilder {
advance_bonus: Option<Bonus>,
pub required_advance: Option<String>,
contradicting_advance: Vec<String>,
unlocked_building: Option<String>,
unlocked_building: Option<Building>,
government: Option<String>,
player_initializers: Vec<AbilityInitializer>,
player_deinitializers: Vec<AbilityInitializer>,
Expand Down Expand Up @@ -86,8 +87,8 @@ impl AdvanceBuilder {
}

#[must_use]
pub fn with_unlocked_building(mut self, unlocked_building: &str) -> Self {
self.unlocked_building = Some(unlocked_building.to_string());
pub fn with_unlocked_building(mut self, unlocked_building: Building) -> Self {
self.unlocked_building = Some(unlocked_building);
self
}

Expand Down
15 changes: 7 additions & 8 deletions server/src/city.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ pub struct City {

impl City {
#[must_use]
pub fn from_data(data: CityData) -> Self {
pub fn from_data(data: CityData, player_index: usize) -> Self {
Self {
pieces: CityPieces::from_data(&data.city_pieces),
mood_state: data.mood_state,
activations: data.activations,
angry_activation: data.angry_activation,
player_index: data.player_index,
player_index,
position: data.position,
port_position: data.port_position,
}
Expand All @@ -43,7 +43,6 @@ impl City {
self.mood_state,
self.activations,
self.angry_activation,
self.player_index,
self.position,
self.port_position,
)
Expand All @@ -56,7 +55,6 @@ impl City {
self.mood_state.clone(),
self.activations,
self.angry_activation,
self.player_index,
self.position,
self.port_position,
)
Expand Down Expand Up @@ -126,7 +124,11 @@ impl City {
if size >= player.cities.len() {
return false;
}
if !player.has_advance(&building.required_advance()) {
if !player
.advances
.iter()
.any(|a| a.unlocked_building == Some(building))
{
return false;
}
if !player.is_building_available(building, game) {
Expand Down Expand Up @@ -225,7 +227,6 @@ pub struct CityData {
mood_state: MoodState,
activations: u32,
angry_activation: bool,
player_index: usize,
position: Position,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
Expand All @@ -239,7 +240,6 @@ impl CityData {
mood_state: MoodState,
activations: u32,
angry_activation: bool,
player_index: usize,
position: Position,
port_position: Option<Position>,
) -> Self {
Expand All @@ -248,7 +248,6 @@ impl CityData {
mood_state,
activations,
angry_activation,
player_index,
position,
port_position,
}
Expand Down
34 changes: 23 additions & 11 deletions server/src/city_pieces.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use serde::{Deserialize, Serialize};

use crate::content::advances::{BARTERING, TACTICS};
use crate::{content::wonders, wonder::Wonder};
use Building::*;

Expand Down Expand Up @@ -220,15 +219,28 @@ impl Building {
}

#[must_use]
pub fn required_advance(&self) -> String {
String::from(match self {
Self::Academy => "Writing",
Self::Market => BARTERING,
Self::Obelisk => "Arts",
Self::Observatory => "Math",
Self::Fortress => TACTICS,
Self::Port => "Fishing",
Self::Temple => "Myths",
})
pub fn all() -> Vec<Building> {
vec![
Academy,
Market,
Obelisk,
Observatory,
Fortress,
Port,
Temple,
]
}

#[must_use]
pub fn name(&self) -> &'static str {
match self {
Academy => "Academy",
Market => "Market",
Obelisk => "Obelisk",
Observatory => "Observatory",
Fortress => "Fortress",
Port => "Port",
Temple => "Temple",
}
}
}
Loading
Loading