Skip to content

Commit 6fc5a65

Browse files
authored
Cleanup (#143)
1 parent b03a007 commit 6fc5a65

File tree

102 files changed

+402
-1443
lines changed

Some content is hidden

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

102 files changed

+402
-1443
lines changed

client/src/advance_ui.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ fn description(p: &Player, a: &Advance) -> Vec<String> {
202202
parts.push(format!("Government: {g}"));
203203
}
204204
if let Some(u) = &a.unlocked_building {
205-
parts.push(format!("Unlocks: {u}"));
205+
parts.push(format!("Unlocks: {}", u.name()));
206206
}
207207

208208
parts

client/src/city_ui.rs

+6-29
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ fn building_icons<'a>(rc: &'a RenderContext, city: &'a City) -> IconActionVec<'a
106106
return vec![];
107107
}
108108
let owner = rc.shown_player;
109-
building_names()
109+
Building::all()
110110
.iter()
111-
.filter_map(|(b, _)| {
111+
.filter_map(|b| {
112112
if city.can_construct(*b, owner, rc.game) {
113113
Some(*b)
114114
} else {
@@ -117,7 +117,7 @@ fn building_icons<'a>(rc: &'a RenderContext, city: &'a City) -> IconActionVec<'a
117117
})
118118
.flat_map(|b| new_building_positions(b, rc, city))
119119
.map(|(b, pos)| {
120-
let name = building_name(b);
120+
let name = b.name();
121121
let tooltip = format!(
122122
"Built {}{} for {}",
123123
name,
@@ -216,13 +216,9 @@ pub fn city_labels(game: &Game, city: &City) -> Vec<String> {
216216
.filter_map(|(b, o)| {
217217
o.as_ref().map(|o| {
218218
if city.player_index == *o {
219-
building_name(*b).to_string()
219+
b.name().to_string()
220220
} else {
221-
format!(
222-
"{} (owned by {})",
223-
building_name(*b),
224-
game.get_player(*o).get_name()
225-
)
221+
format!("{} (owned by {})", b.name(), game.get_player(*o).get_name())
226222
}
227223
})
228224
})
@@ -296,7 +292,7 @@ pub fn draw_city(rc: &RenderContext, city: &City) {
296292
let tooltip = if matches!(state.active_dialog, ActiveDialog::CulturalInfluence) {
297293
""
298294
} else {
299-
building_name(*b)
295+
b.name()
300296
};
301297
draw_scaled_icon(
302298
rc,
@@ -322,22 +318,3 @@ pub fn building_position(city: &City, center: Point, i: usize, building: Buildin
322318
hex_ui::rotate_around(center, 25.0, 90 * i)
323319
}
324320
}
325-
326-
pub fn building_name(b: Building) -> &'static str {
327-
building_names()
328-
.iter()
329-
.find_map(|(b2, n)| if &b == b2 { Some(n) } else { None })
330-
.unwrap()
331-
}
332-
333-
fn building_names() -> [(Building, &'static str); 7] {
334-
[
335-
(Building::Academy, "Academy"),
336-
(Building::Market, "Market"),
337-
(Building::Obelisk, "Obelisk"),
338-
(Building::Observatory, "Observatory"),
339-
(Building::Fortress, "Fortress"),
340-
(Building::Port, "Port"),
341-
(Building::Temple, "Temple"),
342-
]
343-
}

client/src/client_state.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use server::position::Position;
99
use server::status_phase::{StatusPhaseAction, StatusPhaseState};
1010

1111
use crate::assets::Assets;
12-
use crate::city_ui::building_name;
1312
use crate::client::{Features, GameSyncRequest};
1413
use crate::collect_ui::CollectResources;
1514
use crate::combat_ui::{
@@ -150,7 +149,7 @@ impl ActiveDialog {
150149
ActiveDialog::CulturalInfluenceResolution(c) => vec![format!(
151150
"Pay {} culture tokens to influence {}",
152151
c.roll_boost_cost,
153-
building_name(c.city_piece)
152+
c.city_piece.name()
154153
)],
155154
ActiveDialog::ExploreResolution(_) => {
156155
vec!["Click on the new tile to rotate it".to_string()]

client/src/influence_ui.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::city_ui::{building_name, building_position, BUILDING_SIZE};
1+
use crate::city_ui::{building_position, BUILDING_SIZE};
22
use crate::client_state::{CameraMode, StateUpdate};
33
use crate::dialog_ui::{cancel_button_with_tooltip, ok_button, OkTooltip};
44
use crate::hex_ui;
@@ -31,7 +31,7 @@ pub fn cultural_influence_resolution_dialog(
3131
rc: &RenderContext,
3232
r: &CulturalInfluenceResolution,
3333
) -> StateUpdate {
34-
let name = building_name(r.city_piece);
34+
let name = r.city_piece.name();
3535
let pile = ResourcePile::culture_tokens(r.roll_boost_cost);
3636
show_resource_pile(rc, &pile, &[ResourceType::CultureTokens]);
3737
if ok_button(rc, OkTooltip::Valid(format!("Influence {name} for {pile}"))) {
@@ -78,7 +78,7 @@ fn show_city(rc: &RenderContext, mouse_pos: Vec2, city: &City) -> Option<StateUp
7878
*b,
7979
) {
8080
if player.can_afford_resources(&cost) {
81-
let name = building_name(*b);
81+
let name = b.name();
8282
let _ = rc.with_camera(CameraMode::World, |rc| {
8383
draw_circle_lines(center.x, center.y, BUILDING_SIZE, 1., WHITE);
8484
show_tooltip_for_circle(

client/src/local_client/bin/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn setup_local_game() -> Game {
8080
vec![unit_type],
8181
Position::from_offset(pos),
8282
None,
83-
vec![],
83+
&[],
8484
);
8585
};
8686

client/src/unit_ui.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ pub fn click_unit(
117117
.find_map(|(i, u)| {
118118
let place = unit_place(rc, i, pos);
119119
if can_select_carried_units {
120-
let carried_units = carried_units(rc.game, player.index, u.id);
120+
let game = rc.game;
121+
let player_index = player.index;
122+
let carried_units = carried_units(u.id, &game.players[player_index]);
121123
for (j, carried) in carried_units.iter().enumerate() {
122124
let carried_place = carried_unit_place(&place, j);
123125
if is_in_circle(mouse_pos, carried_place.center, carried_place.radius) {
@@ -185,7 +187,10 @@ pub fn draw_units(rc: &RenderContext, tooltip: bool) {
185187
draw_unit(rc, tooltip, &highlighted_units, *p, u, &place);
186188

187189
let player = rc.game.get_player(*p);
188-
let carried = carried_units(rc.game, *p, u.id);
190+
let game = rc.game;
191+
let player_index = *p;
192+
let carrier = u.id;
193+
let carried = carried_units(carrier, &game.players[player_index]);
189194
carried.iter().enumerate().for_each(|(j, u)| {
190195
draw_unit(
191196
rc,

server/src/advance.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
resource_pile::ResourcePile,
55
};
66

7+
use crate::city_pieces::Building;
78
use Bonus::*;
89

910
pub struct Advance {
@@ -12,7 +13,7 @@ pub struct Advance {
1213
pub bonus: Option<Bonus>,
1314
pub required: Option<String>,
1415
pub contradicting: Vec<String>,
15-
pub unlocked_building: Option<String>,
16+
pub unlocked_building: Option<Building>,
1617
pub government: Option<String>,
1718
pub player_initializer: AbilityInitializer,
1819
pub player_deinitializer: AbilityInitializer,
@@ -39,7 +40,7 @@ pub(crate) struct AdvanceBuilder {
3940
advance_bonus: Option<Bonus>,
4041
pub required_advance: Option<String>,
4142
contradicting_advance: Vec<String>,
42-
unlocked_building: Option<String>,
43+
unlocked_building: Option<Building>,
4344
government: Option<String>,
4445
player_initializers: Vec<AbilityInitializer>,
4546
player_deinitializers: Vec<AbilityInitializer>,
@@ -86,8 +87,8 @@ impl AdvanceBuilder {
8687
}
8788

8889
#[must_use]
89-
pub fn with_unlocked_building(mut self, unlocked_building: &str) -> Self {
90-
self.unlocked_building = Some(unlocked_building.to_string());
90+
pub fn with_unlocked_building(mut self, unlocked_building: Building) -> Self {
91+
self.unlocked_building = Some(unlocked_building);
9192
self
9293
}
9394

server/src/city.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub struct City {
2424

2525
impl City {
2626
#[must_use]
27-
pub fn from_data(data: CityData) -> Self {
27+
pub fn from_data(data: CityData, player_index: usize) -> Self {
2828
Self {
2929
pieces: CityPieces::from_data(&data.city_pieces),
3030
mood_state: data.mood_state,
3131
activations: data.activations,
3232
angry_activation: data.angry_activation,
33-
player_index: data.player_index,
33+
player_index,
3434
position: data.position,
3535
port_position: data.port_position,
3636
}
@@ -43,7 +43,6 @@ impl City {
4343
self.mood_state,
4444
self.activations,
4545
self.angry_activation,
46-
self.player_index,
4746
self.position,
4847
self.port_position,
4948
)
@@ -56,7 +55,6 @@ impl City {
5655
self.mood_state.clone(),
5756
self.activations,
5857
self.angry_activation,
59-
self.player_index,
6058
self.position,
6159
self.port_position,
6260
)
@@ -126,7 +124,11 @@ impl City {
126124
if size >= player.cities.len() {
127125
return false;
128126
}
129-
if !player.has_advance(&building.required_advance()) {
127+
if !player
128+
.advances
129+
.iter()
130+
.any(|a| a.unlocked_building == Some(building))
131+
{
130132
return false;
131133
}
132134
if !player.is_building_available(building, game) {
@@ -225,7 +227,6 @@ pub struct CityData {
225227
mood_state: MoodState,
226228
activations: u32,
227229
angry_activation: bool,
228-
player_index: usize,
229230
position: Position,
230231
#[serde(skip_serializing_if = "Option::is_none")]
231232
#[serde(default)]
@@ -239,7 +240,6 @@ impl CityData {
239240
mood_state: MoodState,
240241
activations: u32,
241242
angry_activation: bool,
242-
player_index: usize,
243243
position: Position,
244244
port_position: Option<Position>,
245245
) -> Self {
@@ -248,7 +248,6 @@ impl CityData {
248248
mood_state,
249249
activations,
250250
angry_activation,
251-
player_index,
252251
position,
253252
port_position,
254253
}

server/src/city_pieces.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use serde::{Deserialize, Serialize};
22

3-
use crate::content::advances::{BARTERING, TACTICS};
43
use crate::{content::wonders, wonder::Wonder};
54
use Building::*;
65

@@ -220,15 +219,28 @@ impl Building {
220219
}
221220

222221
#[must_use]
223-
pub fn required_advance(&self) -> String {
224-
String::from(match self {
225-
Self::Academy => "Writing",
226-
Self::Market => BARTERING,
227-
Self::Obelisk => "Arts",
228-
Self::Observatory => "Math",
229-
Self::Fortress => TACTICS,
230-
Self::Port => "Fishing",
231-
Self::Temple => "Myths",
232-
})
222+
pub fn all() -> Vec<Building> {
223+
vec![
224+
Academy,
225+
Market,
226+
Obelisk,
227+
Observatory,
228+
Fortress,
229+
Port,
230+
Temple,
231+
]
232+
}
233+
234+
#[must_use]
235+
pub fn name(&self) -> &'static str {
236+
match self {
237+
Academy => "Academy",
238+
Market => "Market",
239+
Obelisk => "Obelisk",
240+
Observatory => "Observatory",
241+
Fortress => "Fortress",
242+
Port => "Port",
243+
Temple => "Temple",
244+
}
233245
}
234246
}

0 commit comments

Comments
 (0)