game_features/faction.rs
1use crate::user_group::*;
2
3/// A team with the ability to claim ownership over terrain.
4/// WIP
5pub struct Faction {
6 /// The group of users that this faction is composed of.
7 pub users: UserGroup,
8 /// The claiming power of this faction. Limits the number of claims it can have and maintain.
9 // TODO calculate power from users.
10 pub power: f32,
11 /// All the claims owned by this faction.
12 pub claims: Vec<(i32, i32, i32)>,
13 /// A value added to the calculated power value.
14 pub power_boost: f32,
15}
16
17impl Faction {
18 /// Claim terrain from another faction.
19 pub fn claim_from(
20 &mut self,
21 _other: &mut Faction,
22 _settings: &FactionSettings,
23 ) -> FactionResult {
24 // TODO
25 Ok(())
26 }
27}
28
29/// Settings of the faction module.
30pub struct FactionSettings {
31 /// The settings related to users.
32 pub user_settings: UserGroupSettings,
33 /// The maximum player-generated claim power.
34 pub maximum_player_power: f32,
35 /// The flags that apply to claimed terrain and faction behavior.
36 pub flags: FactionFlags,
37}
38
39/// Fags that modify how a faction behaves and how the claimed terrain behaves.
40pub struct FactionFlags {
41 /// You can steal terrain from this faction.
42 pub claimable: bool,
43 /// Player attacks are enabled in claimed terrain.
44 pub pvp_enabled: bool,
45 /// You lose power on death in claimed terrain.
46 pub power_loss_in_territory: bool,
47 /// You can gain power in claimed terrain.
48 pub power_gain_in_territory: bool,
49 /// If true, will not destroy the faction once all players leaved the faction.
50 pub permanent: bool,
51}
52
53/// Alias type. List of all known factions.
54pub type FactionRepository = Vec<Faction>;
55
56/// Alias type. Result of faction methods that can fail.
57pub type FactionResult = std::result::Result<(), FactionError>;
58
59/// Errors that can occur while using factions.
60#[derive(Debug)]
61pub enum FactionError {
62 /// You don't have enough power to claim terrain.
63 NotEnoughPower,
64 /// You cannot claim this terrain.
65 Unclaimable,
66 /// The pvp is not allowed in this terrain.
67 PvpDenied,
68 /// You cannot use an item in this terrain.
69 UseDenied,
70}
71
72/// The settings related to terrain claiming and how the world is divided into claimable chunks.
73pub struct LandClaimSettings {
74 /// The size of the claimable chunks.
75 pub claim_size: [f32; 3],
76}
77
78impl LandClaimSettings {
79 /// Get the three dimensional ID of this claim area.
80 pub fn claim_id_from_position(&self, pos: &[f32; 3]) -> (i32, i32, i32) {
81 let x = pos[0] / self.claim_size[0];
82 let y = pos[1] / self.claim_size[1];
83 let z = if self.claim_size[2] != 0.0 {
84 pos[2] / self.claim_size[2]
85 } else {
86 0.0
87 };
88 (x as i32, y as i32, z as i32)
89 }
90}