-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_actions.rs
224 lines (215 loc) · 8.98 KB
/
custom_actions.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
use serde::{Deserialize, Serialize};
use crate::action::Action;
use crate::collect::{collect, undo_collect};
use crate::content::advances_culture::{
execute_sports, execute_theaters, undo_sports, undo_theaters,
};
use crate::content::advances_economy::collect_taxes;
use crate::content::wonders::construct_wonder;
use crate::log::{
current_turn_log, format_city_happiness_increase, format_collect_log_item,
format_cultural_influence_attempt_log_item, format_happiness_increase,
};
use crate::player::Player;
use crate::playing_actions::{
increase_happiness, influence_culture_attempt, undo_increase_happiness, Collect,
IncreaseHappiness, InfluenceCultureAttempt, PlayingAction,
};
use crate::{
game::Game, playing_actions::ActionType, position::Position, resource_pile::ResourcePile,
};
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum CustomAction {
ConstructWonder {
city_position: Position,
wonder: String,
payment: ResourcePile,
},
AbsolutePower,
ForcedLabor,
CivilRights,
ArtsInfluenceCultureAttempt(InfluenceCultureAttempt),
VotingIncreaseHappiness(IncreaseHappiness),
FreeEconomyCollect(Collect),
Sports {
city_position: Position,
payment: ResourcePile,
},
Taxes(ResourcePile),
Theaters(ResourcePile),
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Hash)]
pub enum CustomActionType {
ConstructWonder,
AbsolutePower,
ForcedLabor,
CivilRights,
ArtsInfluenceCultureAttempt,
VotingIncreaseHappiness,
FreeEconomyCollect,
Sports,
Taxes,
Theaters,
}
impl CustomAction {
///
///
/// # Panics
///
/// Panics if action is illegal
pub fn execute(self, game: &mut Game, player_index: usize) {
match self {
CustomAction::ConstructWonder {
city_position,
wonder,
payment,
} => construct_wonder(game, player_index, city_position, &wonder, payment),
CustomAction::AbsolutePower => game.actions_left += 1,
CustomAction::ForcedLabor => {
// we check that the action was played
}
CustomAction::CivilRights => {
game.players[player_index].gain_resources(ResourcePile::mood_tokens(3));
}
CustomAction::ArtsInfluenceCultureAttempt(c) => {
influence_culture_attempt(game, player_index, &c);
}
CustomAction::VotingIncreaseHappiness(i) => {
increase_happiness(game, player_index, &i.happiness_increases, Some(i.payment));
}
CustomAction::FreeEconomyCollect(c) => collect(game, player_index, &c),
CustomAction::Sports {
city_position,
payment,
} => {
execute_sports(game, player_index, city_position, &payment);
}
CustomAction::Taxes(r) => collect_taxes(game, player_index, r),
CustomAction::Theaters(r) => execute_theaters(game, player_index, &r),
}
}
#[must_use]
pub fn custom_action_type(&self) -> CustomActionType {
match self {
CustomAction::ConstructWonder { .. } => CustomActionType::ConstructWonder,
CustomAction::AbsolutePower => CustomActionType::AbsolutePower,
CustomAction::ForcedLabor => CustomActionType::ForcedLabor,
CustomAction::CivilRights => CustomActionType::CivilRights,
CustomAction::ArtsInfluenceCultureAttempt(_) => {
CustomActionType::ArtsInfluenceCultureAttempt
}
CustomAction::VotingIncreaseHappiness(_) => CustomActionType::VotingIncreaseHappiness,
CustomAction::FreeEconomyCollect(_) => CustomActionType::FreeEconomyCollect,
CustomAction::Sports { .. } => CustomActionType::Sports,
CustomAction::Taxes(_) => CustomActionType::Taxes,
CustomAction::Theaters(_) => CustomActionType::Theaters,
}
}
pub(crate) fn undo(self, game: &mut Game, player_index: usize) {
let action = self.custom_action_type();
if action.action_type().once_per_turn {
game.players[player_index]
.played_once_per_turn_actions
.retain(|a| a != &action);
}
match self {
CustomAction::ConstructWonder {
city_position,
wonder: _,
payment,
} => {
game.players[player_index].gain_resources_in_undo(payment);
let wonder = game.undo_build_wonder(city_position, player_index);
game.players[player_index].wonder_cards.push(wonder);
}
CustomAction::AbsolutePower => game.actions_left -= 1,
CustomAction::ForcedLabor => {
// we check that the action was played
}
CustomAction::CivilRights => {
game.players[player_index].lose_resources(ResourcePile::mood_tokens(3));
}
CustomAction::ArtsInfluenceCultureAttempt(_) => panic!("Action can't be undone"),
CustomAction::VotingIncreaseHappiness(i) => {
undo_increase_happiness(
game,
player_index,
&i.happiness_increases,
Some(i.payment),
);
}
CustomAction::FreeEconomyCollect(c) => undo_collect(game, player_index, c),
CustomAction::Taxes(r) => {
game.players[player_index].lose_resources(r);
}
CustomAction::Theaters(r) => {
undo_theaters(game, player_index, &r);
}
CustomAction::Sports {
city_position,
payment,
} => {
undo_sports(game, player_index, city_position, &payment);
}
}
}
#[must_use]
pub fn format_log_item(&self, game: &Game, player: &Player, player_name: &str) -> String {
match self {
CustomAction::ConstructWonder { city_position, wonder, payment } =>
format!("{player_name} paid {payment} to construct the {wonder} wonder in the city at {city_position}"),
CustomAction::AbsolutePower =>
format!("{player_name} paid 2 mood tokens to get an extra action using Forced Labor"),
CustomAction::ForcedLabor =>
format!("{player_name} paid 1 mood token to treat Angry cities as neutral"),
CustomAction::CivilRights =>
format!("{player_name} gained 3 mood tokens using Civil Rights"),
CustomAction::ArtsInfluenceCultureAttempt(c) =>
format!("{} using Arts", format_cultural_influence_attempt_log_item(game, player_name, c)),
CustomAction::VotingIncreaseHappiness(i) =>
format!("{} using Voting", format_happiness_increase(player, player_name, i)),
CustomAction::FreeEconomyCollect(c) =>
format!("{} using Free Economy", format_collect_log_item(player, player_name, c)),
CustomAction::Sports { city_position, payment } =>
format!("{player_name} paid {payment} to increase the happiness in {} using Sports",
format_city_happiness_increase(player, *city_position, payment.resource_amount())),
CustomAction::Taxes(r) =>
format!("{player_name} paid 1 mood token to collect {r} using Taxes"),
CustomAction::Theaters(r) =>
format!("{player_name} paid {r} to convert resources using Theaters"),
}
}
}
impl CustomActionType {
#[must_use]
pub fn action_type(&self) -> ActionType {
match self {
CustomActionType::AbsolutePower => {
ActionType::free_and_once_per_turn(ResourcePile::mood_tokens(2))
}
CustomActionType::CivilRights
| CustomActionType::Sports
| CustomActionType::ConstructWonder => ActionType::default(),
CustomActionType::ArtsInfluenceCultureAttempt => {
ActionType::free_and_once_per_turn(ResourcePile::culture_tokens(1))
}
CustomActionType::VotingIncreaseHappiness => {
ActionType::free(ResourcePile::mood_tokens(1))
}
CustomActionType::FreeEconomyCollect | CustomActionType::ForcedLabor => {
ActionType::free_and_once_per_turn(ResourcePile::mood_tokens(1))
}
CustomActionType::Taxes => ActionType::once_per_turn(ResourcePile::mood_tokens(1)),
CustomActionType::Theaters => ActionType::free_and_once_per_turn(ResourcePile::empty()),
}
}
#[must_use]
pub fn is_available(&self, game: &Game, _player_index: usize) -> bool {
match self {
CustomActionType::FreeEconomyCollect => !current_turn_log(game)
.iter()
.any(|item| matches!(item.action, Action::Playing(PlayingAction::Collect(_)))),
_ => true,
}
}
}