|
| 1 | +use crate::game::Game; |
| 2 | +use crate::map::Terrain::{Fertile, Forest, Mountain}; |
| 3 | +use crate::playing_actions::Collect; |
| 4 | +use crate::position::Position; |
| 5 | +use crate::resource_pile::ResourcePile; |
| 6 | +use std::collections::HashMap; |
| 7 | +use std::iter; |
| 8 | +use std::ops::Add; |
| 9 | + |
| 10 | +/// |
| 11 | +/// # Panics |
| 12 | +/// |
| 13 | +/// Panics if the action is illegal |
| 14 | +#[must_use] |
| 15 | +pub fn get_total_collection( |
| 16 | + game: &Game, |
| 17 | + player_index: usize, |
| 18 | + city_position: Position, |
| 19 | + collections: &[(Position, ResourcePile)], |
| 20 | +) -> Option<ResourcePile> { |
| 21 | + let player = &game.players[player_index]; |
| 22 | + let city = player.get_city(city_position)?; |
| 23 | + if city.mood_modified_size() < collections.len() || city.player_index != player_index { |
| 24 | + return None; |
| 25 | + } |
| 26 | + let choices = possible_resource_collections(game, city_position, player_index, &HashMap::new()); |
| 27 | + let possible = collections.iter().all(|(position, collect)| { |
| 28 | + choices |
| 29 | + .get(position) |
| 30 | + .is_some_and(|options| options.contains(collect)) |
| 31 | + }); |
| 32 | + |
| 33 | + if possible { |
| 34 | + collections |
| 35 | + .iter() |
| 36 | + .map(|(_, collect)| collect.clone()) |
| 37 | + .reduce(|a, b| a.clone().add(b.clone())) |
| 38 | + } else { |
| 39 | + None |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +pub(crate) fn collect(game: &mut Game, player_index: usize, c: &Collect) { |
| 44 | + let total_collect = get_total_collection(game, player_index, c.city_position, &c.collections) |
| 45 | + .expect("Illegal action"); |
| 46 | + let city = game.players[player_index] |
| 47 | + .get_city_mut(c.city_position) |
| 48 | + .expect("Illegal action"); |
| 49 | + assert!(city.can_activate(), "Illegal action"); |
| 50 | + city.activate(); |
| 51 | + game.players[player_index].gain_resources(total_collect); |
| 52 | +} |
| 53 | + |
| 54 | +pub(crate) fn undo_collect(game: &mut Game, player_index: usize, c: Collect) { |
| 55 | + game.players[player_index] |
| 56 | + .get_city_mut(c.city_position) |
| 57 | + .expect("city should be owned by the player") |
| 58 | + .undo_activate(); |
| 59 | + let total_collect = c.collections.into_iter().map(|(_, collect)| collect).sum(); |
| 60 | + game.players[player_index].loose_resources(total_collect); |
| 61 | +} |
| 62 | + |
| 63 | +pub(crate) struct CollectContext { |
| 64 | + pub city_position: Position, |
| 65 | + #[allow(dead_code)] // will need for other advances |
| 66 | + pub used: HashMap<Position, ResourcePile>, |
| 67 | +} |
| 68 | + |
| 69 | +/// |
| 70 | +/// # Panics |
| 71 | +/// Panics if the action is illegal |
| 72 | +#[must_use] |
| 73 | +pub fn possible_resource_collections( |
| 74 | + game: &Game, |
| 75 | + city_pos: Position, |
| 76 | + player_index: usize, |
| 77 | + used: &HashMap<Position, ResourcePile>, |
| 78 | +) -> HashMap<Position, Vec<ResourcePile>> { |
| 79 | + let terrain_options = HashMap::from([ |
| 80 | + (Mountain, vec![ResourcePile::ore(1)]), |
| 81 | + (Fertile, vec![ResourcePile::food(1)]), |
| 82 | + (Forest, vec![ResourcePile::wood(1)]), |
| 83 | + ]); |
| 84 | + |
| 85 | + let mut collect_options = city_pos |
| 86 | + .neighbors() |
| 87 | + .into_iter() |
| 88 | + .chain(iter::once(city_pos)) |
| 89 | + .filter_map(|pos| { |
| 90 | + if let Some(t) = game.map.get(pos) { |
| 91 | + if let Some(option) = terrain_options.get(t) { |
| 92 | + return Some((pos, option.clone())); |
| 93 | + } |
| 94 | + } |
| 95 | + None |
| 96 | + }) |
| 97 | + .collect(); |
| 98 | + game.players[player_index] |
| 99 | + .events |
| 100 | + .as_ref() |
| 101 | + .expect("events should be set") |
| 102 | + .collect_options |
| 103 | + .trigger( |
| 104 | + &mut collect_options, |
| 105 | + &CollectContext { |
| 106 | + city_position: city_pos, |
| 107 | + used: used.clone(), |
| 108 | + }, |
| 109 | + game, |
| 110 | + ); |
| 111 | + collect_options.retain(|p, _| { |
| 112 | + game.get_any_city(*p).is_none_or(|c| c.position == city_pos) |
| 113 | + && game.enemy_player(player_index, *p).is_none() |
| 114 | + }); |
| 115 | + collect_options |
| 116 | +} |
0 commit comments