nil_core/world/cheat/
city.rs1use crate::bail_if_cheats_are_not_allowed;
5use crate::city::City;
6use crate::city::stability::Stability;
7use crate::continent::field::Field;
8use crate::continent::index::ContinentKey;
9use crate::error::{Error, Result};
10use crate::ruler::Ruler;
11use crate::world::World;
12
13pub fn get_city(world: &World, key: impl ContinentKey) -> Result<&City> {
14 bail_if_cheats_are_not_allowed!(world);
15 world.continent.city(key)
16}
17
18pub fn set_stability(
19 world: &mut World,
20 key: impl ContinentKey,
21 stability: Stability,
22) -> Result<()> {
23 bail_if_cheats_are_not_allowed!(world);
24
25 let coord = key.into_coord(world.continent.size())?;
26 let city = world.city_mut(coord)?;
27 *city.stability_mut() = stability;
28
29 world.emit_city(coord)?;
30
31 Ok(())
32}
33
34pub fn spawn_city(world: &mut World, ruler: &Ruler, key: impl ContinentKey) -> Result<()> {
35 bail_if_cheats_are_not_allowed!(world);
36
37 let coord = key.into_coord(world.continent.size())?;
38 let city = City::builder(coord)
39 .name(<Ruler as AsRef<str>>::as_ref(ruler))
40 .owner(ruler.clone())
41 .build();
42
43 let field = world.continent.field_mut(coord)?;
44 if field.is_empty() {
45 *field = Field::City { city: Box::new(city) };
46 world.emit_public_city(coord)?;
47
48 if let Some(player) = ruler.player() {
49 world.emit_player(player.clone())?;
50 }
51 } else {
52 return Err(Error::FieldNotEmpty(coord));
53 }
54
55 Ok(())
56}