Skip to content

Commit 251e5e6

Browse files
authored
Incidents (#176)
1 parent 203145c commit 251e5e6

36 files changed

+3844
-111
lines changed

server/src/action.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub(crate) fn execute_custom_phase_action(
172172
on_recruit(game, player_index, r);
173173
}
174174
Incident(i) => {
175-
trigger_incident(game, player_index, i);
175+
trigger_incident(game, i.clone());
176176
}
177177
}
178178
}

server/src/advance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pub(crate) fn gain_advance(game: &mut Game, player_index: usize, info: &AdvanceI
200200
player.incident_tokens -= 1;
201201
if player.incident_tokens == 0 {
202202
player.incident_tokens = 3;
203-
trigger_incident(game, player_index, &IncidentInfo::new(player_index));
203+
trigger_incident(game, IncidentInfo::new(player_index));
204204
}
205205
}
206206

server/src/barbarians.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::content::custom_phase_actions::{
77
new_position_request, ResourceRewardRequest, UnitTypeRequest,
88
};
99
use crate::game::Game;
10-
use crate::incident::{IncidentBuilder, BASE_EFFECT_PRIORITY};
10+
use crate::incident::{play_base_effect, IncidentBuilder, BASE_EFFECT_PRIORITY};
1111
use crate::map::Terrain;
1212
use crate::payment::PaymentOptions;
1313
use crate::player::Player;
@@ -230,7 +230,7 @@ pub(crate) fn barbarians_move(mut builder: IncidentBuilder) -> IncidentBuilder {
230230
builder.add_simple_incident_listener(
231231
IncidentTarget::ActivePlayer,
232232
BASE_EFFECT_PRIORITY,
233-
|game, player, _| {
233+
|game, player, _, _| {
234234
let s = get_barbarian_state(game);
235235
if s.move_units && get_movable_units(game, player, &s).is_empty() {
236236
// after all moves are done
@@ -313,8 +313,8 @@ pub(crate) fn set_info(
313313
builder.add_simple_incident_listener(
314314
IncidentTarget::ActivePlayer,
315315
BASE_EFFECT_PRIORITY + 200,
316-
move |game, player, _| {
317-
if game.current_event().barbarians.is_none() {
316+
move |game, player, _, _| {
317+
if play_base_effect(game) && game.current_event().barbarians.is_none() {
318318
game.add_info_log_item(&format!("Base effect: {name}"));
319319
let mut state = BarbariansEventState::new();
320320
init(&mut state, game, player);
@@ -417,9 +417,9 @@ fn possible_barbarians_reinforcements(game: &Game) -> Vec<Position> {
417417

418418
fn get_barbarian_reinforcement_choices(game: &Game) -> Vec<UnitType> {
419419
let barbarian = get_barbarians_player(game);
420-
let pos = get_barbarian_state(game)
421-
.selected_position
422-
.expect("selected position should exist");
420+
let Some(pos) = get_barbarian_state(game).selected_position else {
421+
return vec![];
422+
};
423423
let possible = if barbarian
424424
.get_units(pos)
425425
.iter()

server/src/content.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod incidents_earthquake;
2222
mod incidents_famine;
2323
mod incidents_good_year;
2424
mod incidents_population_boom;
25+
mod incidents_trade;
2526
mod incidents_trojan;
2627
pub mod trade_routes;
2728
pub mod wonders;

server/src/content/advances_autocracy.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::content::advances::{advance_group_builder, AdvanceGroup};
44
use crate::content::custom_actions::CustomActionType::{AbsolutePower, ForcedLabor};
55
use crate::content::custom_phase_actions::ResourceRewardRequest;
66
use crate::payment::PaymentOptions;
7-
use crate::resource::ResourceType;
87

98
pub(crate) fn autocracy() -> AdvanceGroup {
109
advance_group_builder(
@@ -35,10 +34,7 @@ fn nationalism() -> AdvanceBuilder {
3534
.any(|u| u.is_army_unit() || u.is_ship())
3635
{
3736
Some(ResourceRewardRequest::new(
38-
PaymentOptions::sum(
39-
1,
40-
&[ResourceType::MoodTokens, ResourceType::CultureTokens],
41-
),
37+
PaymentOptions::tokens(1),
4238
"Select token to gain".to_string(),
4339
))
4440
} else {

server/src/content/incidects_civil_war.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::payment::{PaymentConversion, PaymentConversionType, PaymentOptions};
88
use crate::player::Player;
99
use crate::player_events::IncidentTarget;
1010
use crate::position::Position;
11-
use crate::resource::ResourceType;
1211
use crate::resource_pile::ResourcePile;
1312
use crate::status_phase::{add_change_government, can_change_government_for_free};
1413
use crate::unit::UnitType;
@@ -140,7 +139,7 @@ fn revolution() -> Incident {
140139
"Kill a unit to avoid losing an action",
141140
|game, _player| can_loose_action(game),
142141
);
143-
b = b.add_simple_incident_listener(IncidentTarget::ActivePlayer, 2, |game, player, _| {
142+
b = b.add_simple_incident_listener(IncidentTarget::ActivePlayer, 2, |game, player, _, _| {
144143
if can_loose_action(game) && game.current_event_player().sacrifice == 0 {
145144
loose_action(game, player);
146145
}
@@ -229,7 +228,7 @@ fn uprising() -> Incident {
229228
0,
230229
|game, player_index, _incident| {
231230
let player = game.get_player(player_index);
232-
let mut cost = PaymentOptions::sum(4, &[ResourceType::MoodTokens, ResourceType::CultureTokens]);
231+
let mut cost = PaymentOptions::tokens(4);
233232
cost.conversions.push(PaymentConversion::new(
234233
vec![ResourcePile::mood_tokens(1), ResourcePile::culture_tokens(1)],
235234
ResourcePile::empty(),
@@ -258,7 +257,7 @@ fn envoy() -> Incident {
258257
.add_simple_incident_listener(
259258
IncidentTarget::ActivePlayer,
260259
1,
261-
|game, player, player_name | {
260+
|game, player, player_name, _| {
262261
game.add_info_log_item(&format!("{player_name} gained 1 idea and 1 culture token"));
263262
game.get_player_mut(player).gain_resources(
264263
ResourcePile::culture_tokens(1) + ResourcePile::ideas(1));
@@ -274,7 +273,7 @@ fn envoy() -> Incident {
274273
)
275274
.add_incident_player_request(
276275
"Select a player to gain 1 culture token",
277-
|_p| true,
276+
|_p, _| true,
278277
0,
279278
|game, s| {
280279
let p = s.choice;

server/src/content/incidents.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::content::incidents_earthquake::earthquakes;
44
use crate::content::incidents_famine::{epidemics, famines, pestilence};
55
use crate::content::incidents_good_year::{awesome_years, fantastic_years, good_years};
66
use crate::content::incidents_population_boom::population_booms;
7+
use crate::content::incidents_trade::trades;
78
use crate::content::incidents_trojan::trojan_incidents;
89
use crate::incident::Incident;
910
use itertools::Itertools;
@@ -30,6 +31,8 @@ pub(crate) fn get_all() -> Vec<Incident> {
3031
civil_wars(),
3132
// 41+
3233
trojan_incidents(),
34+
// 45+
35+
trades(),
3336
// 51+
3437
successful_year(),
3538
]

server/src/content/incidents_famine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) fn pestilence() -> Vec<Incident> {
3232
});
3333
builder = pestilence_city(builder, 1);
3434
builder =
35-
builder.add_simple_incident_listener(IncidentTarget::ActivePlayer, 0, |game, _, _| {
35+
builder.add_simple_incident_listener(IncidentTarget::ActivePlayer, 0, |game, _, _, _| {
3636
game.permanent_incident_effects
3737
.push(PermanentIncidentEffect::Pestilence);
3838
});

server/src/content/incidents_good_year.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn good_year(mut builder: IncidentBuilder, amount: u32, good_year_type: &GoodYea
141141

142142
builder = builder.add_incident_player_request(
143143
"Select a player to gain 1 food",
144-
|p| p.resources.food < p.resource_limit.food,
144+
|p, _| p.resources.food < p.resource_limit.food,
145145
i as i32,
146146
move |game, c| {
147147
game.add_info_log_item(&format!(

server/src/content/incidents_population_boom.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::content::custom_phase_actions::new_position_request;
2-
use crate::game::Game;
32
use crate::incident::{Incident, IncidentBaseEffect, IncidentBuilder};
4-
use crate::player_events::{IncidentInfo, IncidentTarget};
3+
use crate::player_events::IncidentTarget;
54
use crate::unit::UnitType;
65

76
pub(crate) fn population_booms() -> Vec<Incident> {
@@ -13,16 +12,14 @@ pub(crate) fn population_booms() -> Vec<Incident> {
1312

1413
fn population_boom(id: u8, effect: IncidentBaseEffect) -> Incident {
1514
let mut b = Incident::builder(id, "Population Boom", "-", effect);
16-
b = select_settler(b, 13, |_game, player, i| {
17-
i.is_active(IncidentTarget::ActivePlayer, player)
18-
});
15+
b = select_settler(b, 13, IncidentTarget::ActivePlayer);
1916
select_player_to_gain_settler(b).build()
2017
}
2118

2219
pub(crate) fn select_player_to_gain_settler(mut b: IncidentBuilder) -> IncidentBuilder {
2320
b = b.add_incident_player_request(
2421
"Select a player to gain 1 settler",
25-
|p| p.available_units().settlers > 0 && !p.cities.is_empty(),
22+
|p, _| p.available_units().settlers > 0 && !p.cities.is_empty(),
2623
12,
2724
|game, c| {
2825
game.add_info_log_item(&format!(
@@ -32,22 +29,16 @@ pub(crate) fn select_player_to_gain_settler(mut b: IncidentBuilder) -> IncidentB
3229
game.current_event_mut().selected_player = Some(c.choice);
3330
},
3431
);
35-
select_settler(b, 11, |game, player, _| {
36-
game.current_event().selected_player == Some(player)
37-
})
32+
select_settler(b, 11, IncidentTarget::SelectedPlayer)
3833
}
3934

40-
fn select_settler(
41-
b: IncidentBuilder,
42-
priority: i32,
43-
pred: impl Fn(&Game, usize, &IncidentInfo) -> bool + 'static + Clone,
44-
) -> IncidentBuilder {
35+
fn select_settler(b: IncidentBuilder, priority: i32, target: IncidentTarget) -> IncidentBuilder {
4536
b.add_incident_position_request(
46-
IncidentTarget::AllPlayers,
37+
target,
4738
priority,
48-
move |game, player_index, incident| {
39+
move |game, player_index, _| {
4940
let p = game.get_player(player_index);
50-
if pred(game, player_index, incident) && p.available_units().settlers > 0 {
41+
if p.available_units().settlers > 0 {
5142
Some(new_position_request(
5243
p.cities.iter().map(|c| c.position).collect(),
5344
1..=1,

0 commit comments

Comments
 (0)