-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcity.rs
292 lines (264 loc) · 7.13 KB
/
city.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::ops::{Add, Sub};
use serde::{Deserialize, Serialize};
use crate::consts::MAX_CITY_SIZE;
use crate::{
city_pieces::{Building, CityPieces, CityPiecesData},
game::Game,
player::Player,
position::Position,
wonder::Wonder,
};
use MoodState::*;
pub struct City {
pub pieces: CityPieces,
pub mood_state: MoodState,
pub activations: u32,
pub angry_activation: bool,
pub player_index: usize,
pub position: Position,
pub port_position: Option<Position>,
}
impl City {
#[must_use]
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,
position: data.position,
port_position: data.port_position,
}
}
#[must_use]
pub fn data(self) -> CityData {
CityData::new(
self.pieces.data(),
self.mood_state,
self.activations,
self.angry_activation,
self.position,
self.port_position,
)
}
#[must_use]
pub fn cloned_data(&self) -> CityData {
CityData::new(
self.pieces.cloned_data(),
self.mood_state.clone(),
self.activations,
self.angry_activation,
self.position,
self.port_position,
)
}
#[must_use]
pub fn new(player_index: usize, position: Position) -> Self {
Self {
pieces: CityPieces::default(),
mood_state: Neutral,
activations: 0,
angry_activation: false,
player_index,
position,
port_position: None,
}
}
#[must_use]
pub fn can_activate(&self) -> bool {
!self.angry_activation
}
pub fn activate(&mut self) {
if self.mood_state == Angry {
self.angry_activation = true;
}
if self.is_activated() {
self.decrease_mood_state();
}
self.activations += 1;
}
pub fn deactivate(&mut self) {
self.activations = 0;
self.angry_activation = false;
}
pub fn undo_activate(&mut self) {
self.activations -= 1;
self.angry_activation = false;
if self.is_activated() {
self.increase_mood_state();
}
}
#[must_use]
pub fn is_activated(&self) -> bool {
self.activations > 0
}
#[must_use]
pub fn can_construct(&self, building: Building, player: &Player, game: &Game) -> bool {
if self.player_index != player.index {
return false;
}
if self.pieces.amount() == MAX_CITY_SIZE {
return false;
}
if matches!(self.mood_state, Angry) {
return false;
}
if !self.pieces.can_add_building(building) {
return false;
}
let size = self.pieces.amount() + 1;
if size >= player.cities.len() {
return false;
}
if !player
.advances
.iter()
.any(|a| a.unlocked_building == Some(building))
{
return false;
}
if !player.is_building_available(building, game) {
return false;
}
player.can_afford(&player.construct_cost(building, self))
}
#[must_use]
pub fn can_build_wonder(&self, wonder: &Wonder, player: &Player, game: &Game) -> bool {
if self.player_index != player.index {
return false;
}
if self.pieces.amount() == MAX_CITY_SIZE {
return false;
}
if self.pieces.amount() >= player.cities.len() {
return false;
}
if !matches!(self.mood_state, Happy) {
return false;
}
if !player.can_afford(&player.wonder_cost(wonder, self)) {
return false;
}
for advance in &wonder.required_advances {
if !player.has_advance(advance) {
return false;
}
}
if let Some(placement_requirement) = &wonder.placement_requirement {
return placement_requirement(self.position, game);
}
true
}
///
///
/// # Panics
///
/// Panics if the city does not have a builder
pub fn raze(self, game: &mut Game, player_index: usize) {
for wonder in &self.pieces.wonders {
(wonder.player_deinitializer)(game, player_index);
}
for wonder in &self.pieces.wonders {
for p in &mut game.players {
p.remove_wonder(wonder);
}
}
}
#[must_use]
pub fn size(&self) -> usize {
self.pieces.amount() + 1
}
#[must_use]
pub fn mood_modified_size(&self) -> usize {
match self.mood_state {
Happy => self.size() + 1,
Neutral => self.size(),
Angry => 1,
}
}
pub fn increase_mood_state(&mut self) {
self.mood_state = match self.mood_state {
Happy | Neutral => Happy,
Angry => Neutral,
};
self.angry_activation = false;
}
pub fn decrease_mood_state(&mut self) {
self.mood_state = match self.mood_state {
Happy => Neutral,
Neutral | Angry => Angry,
}
}
#[must_use]
fn uninfluenced_buildings(&self) -> u32 {
self.pieces.buildings(Some(self.player_index)).len() as u32
}
#[must_use]
pub fn influenced(&self) -> bool {
self.uninfluenced_buildings() as usize != self.pieces.amount()
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct CityData {
city_pieces: CityPiecesData,
mood_state: MoodState,
activations: u32,
angry_activation: bool,
position: Position,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
port_position: Option<Position>,
}
impl CityData {
#[must_use]
pub fn new(
city_pieces: CityPiecesData,
mood_state: MoodState,
activations: u32,
angry_activation: bool,
position: Position,
port_position: Option<Position>,
) -> Self {
Self {
city_pieces,
mood_state,
activations,
angry_activation,
position,
port_position,
}
}
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum MoodState {
Happy = 2,
Neutral = 1,
Angry = 0,
}
impl Add<u32> for MoodState {
type Output = Self;
fn add(self, rhs: u32) -> Self::Output {
match rhs {
0 => self,
1 => match self {
Happy | Neutral => Happy,
Angry => Neutral,
},
2.. => Happy,
}
}
}
impl Sub<u32> for MoodState {
type Output = Self;
fn sub(self, rhs: u32) -> Self::Output {
match rhs {
0 => self,
1 => match self {
Angry | Neutral => Angry,
Happy => Neutral,
},
2.. => Angry,
}
}
}