Skip to content

Commit 7ebd466

Browse files
committed
payment options
1 parent 5c218dc commit 7ebd466

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

server/src/content/advances.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::combat::{Combat, CombatModifier, CombatStrength};
88
use crate::content::custom_phase_actions::{CustomPhasePaymentRequest, CustomPhaseRewardRequest};
99
use crate::content::trade_routes::{gain_trade_route_reward, trade_route_reward};
1010
use crate::game::GameState;
11-
use crate::payment::PaymentOptions;
11+
use crate::payment::{PaymentConversion, PaymentOptions};
1212
use crate::playing_actions::{PlayingAction, PlayingActionType};
1313
use crate::position::Position;
1414
use crate::resource::ResourceType;
@@ -27,7 +27,6 @@ pub const NAVIGATION: &str = "Navigation";
2727
pub const ROADS: &str = "Roads";
2828
pub const STEEL_WEAPONS: &str = "Steel Weapons";
2929
pub const METALLURGY: &str = "Metallurgy";
30-
pub const RITUALS: &str = "Rituals";
3130
pub const TACTICS: &str = "Tactics";
3231
pub const BARTERING: &str = "Bartering";
3332
pub const CURRENCY: &str = "Currency";
@@ -464,8 +463,23 @@ fn spirituality() -> Vec<Advance> {
464463
Advance::builder("Myths", "not implemented")
465464
.with_advance_bonus(MoodToken)
466465
.with_unlocked_building(Temple),
467-
Advance::builder(RITUALS, "When you perform the Increase Happiness Action you may spend any Resources as a substitute for mood tokens. This is done at a 1:1 ratio")
468-
.with_advance_bonus(CultureToken),
466+
Advance::builder("Rituals", "When you perform the Increase Happiness Action you may spend any Resources as a substitute for mood tokens. This is done at a 1:1 ratio")
467+
.with_advance_bonus(CultureToken)
468+
.add_player_event_listener(
469+
|event| &mut event.happiness_cost,
470+
|cost, (), ()| {
471+
for r in &[
472+
ResourceType::Food,
473+
ResourceType::Wood,
474+
ResourceType::Ore,
475+
ResourceType::Ideas,
476+
ResourceType::Gold,
477+
] {
478+
cost.conversions.push(PaymentConversion::unlimited(vec![ResourcePile::mood_tokens(1)],ResourcePile::of(*r, 1)));
479+
}
480+
},
481+
0,
482+
),
469483
],
470484
)
471485
}

server/src/payment.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ pub struct PaymentConversion {
1313
pub limit: Option<u32>,
1414
}
1515

16+
impl PaymentConversion {
17+
#[must_use]
18+
pub fn unlimited(from: Vec<ResourcePile>, to: ResourcePile) -> Self {
19+
PaymentConversion {
20+
from,
21+
to,
22+
limit: None,
23+
}
24+
}
25+
}
26+
1627
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
1728
pub struct PaymentOptions {
1829
pub default: ResourcePile,
@@ -55,11 +66,10 @@ impl PaymentOptions {
5566
pub fn sum(cost: u32, types_by_preference: &[ResourceType]) -> Self {
5667
let mut conversions = vec![];
5768
types_by_preference.windows(2).for_each(|pair| {
58-
conversions.push(PaymentConversion {
59-
from: vec![ResourcePile::of(pair[0], 1)],
60-
to: ResourcePile::of(pair[1], 1),
61-
limit: None,
62-
});
69+
conversions.push(PaymentConversion::unlimited(
70+
vec![ResourcePile::of(pair[0], 1)],
71+
ResourcePile::of(pair[1], 1),
72+
));
6373
});
6474
PaymentOptions {
6575
default: ResourcePile::of(types_by_preference[0], cost),
@@ -160,7 +170,9 @@ pub fn can_convert(
160170

161171
let upper_limit = conversion.limit.unwrap_or(u32::MAX);
162172
for amount in 1..=upper_limit {
163-
if !current.has_at_least(from, amount) || (conversion.to.is_empty() && amount > discount_left) {
173+
if !current.has_at_least(from, amount)
174+
|| (conversion.to.is_empty() && amount > discount_left)
175+
{
164176
return can_convert(
165177
available,
166178
current,

server/src/player.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::advance::Advance;
2-
use crate::content::advances::{get_advance_by_name, RITUALS};
2+
use crate::content::advances::get_advance_by_name;
33
use crate::game::CurrentMove;
44
use crate::game::GameState::Movement;
55
use crate::movement::move_routes;
@@ -604,21 +604,12 @@ impl Player {
604604
let cost = city.size() as u32 * steps;
605605
if steps > max_steps {
606606
None
607-
} else if self.has_advance(RITUALS) {
608-
// todo can call event to add conversions
609-
Some(PaymentOptions::sum(
610-
cost,
611-
&[
612-
ResourceType::Food,
613-
ResourceType::Wood,
614-
ResourceType::Ore,
615-
ResourceType::Ideas,
616-
ResourceType::MoodTokens,
617-
ResourceType::Gold,
618-
],
619-
))
620607
} else {
621-
Some(PaymentOptions::sum(cost, &[ResourceType::MoodTokens]))
608+
let mut options = PaymentOptions::sum(cost, &[ResourceType::MoodTokens]);
609+
self.get_events()
610+
.happiness_cost
611+
.trigger(&mut options, &(), &());
612+
Some(options)
622613
}
623614
}
624615

server/src/player_events.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub(crate) struct PlayerEvents {
2929
pub after_execute_action: EventMut<Player, Action, ()>,
3030
pub before_undo_action: EventMut<Player, Action, ()>,
3131
pub advance_cost: EventMut<u32, String>,
32+
pub happiness_cost: EventMut<PaymentOptions, (), ()>,
3233
pub is_playing_action_available: EventMut<bool, PlayingActionType, Player>,
3334
pub terrain_collect_options: EventMut<HashMap<Terrain, HashSet<ResourcePile>>, (), ()>,
3435
pub collect_options: EventMut<HashMap<Position, HashSet<ResourcePile>>, CollectContext, Game>,

0 commit comments

Comments
 (0)