screeps_utils/math/
gpl.rs

1use screeps::constants::*;
2
3/// Provides the total number of processed power needed to achieve a given
4/// Global Power Level
5///
6/// Calculates the total number of power that need to be processed to achieve a
7/// given Global Power Level. The game's API only exposes current level plus
8/// progress toward the next level; this allows you to see how much you
9/// processed to achieve your current level
10///
11/// [Code reference](https://github.com/screeps/engine/blob/6d498f2f0db4e0744fa6bf8563836d36b49b6a29/src/game/game.js#L120)
12pub const fn power_for_gpl(level: u32) -> u128 {
13    (level as u128).pow(POWER_LEVEL_POW) * POWER_LEVEL_MULTIPLY as u128
14}
15
16#[cfg(test)]
17mod test {
18    use super::power_for_gpl;
19
20    #[test]
21    fn gpl_formula() {
22        // the sanity of these values has been validated up to GCL 48
23        // on the MMO game server
24        assert_eq!(power_for_gpl(0), 0);
25        assert_eq!(power_for_gpl(1), 1_000);
26        assert_eq!(power_for_gpl(2), 4_000);
27        assert_eq!(power_for_gpl(3), 9_000);
28        assert_eq!(power_for_gpl(4), 16_000);
29        assert_eq!(power_for_gpl(5), 25_000);
30        assert_eq!(power_for_gpl(6), 36_000);
31        assert_eq!(power_for_gpl(7), 49_000);
32        assert_eq!(power_for_gpl(8), 64_000);
33        assert_eq!(power_for_gpl(9), 81_000);
34        assert_eq!(power_for_gpl(10), 100_000);
35        assert_eq!(power_for_gpl(50), 2_500_000);
36        assert_eq!(power_for_gpl(100), 10_000_000);
37        assert_eq!(power_for_gpl(1_000), 1_000_000_000);
38        assert_eq!(power_for_gpl(5_000), 25_000_000_000);
39        assert_eq!(power_for_gpl(10_000), 100_000_000_000);
40        assert_eq!(power_for_gpl(50_000), 2_500_000_000_000);
41        assert_eq!(power_for_gpl(100_000), 10_000_000_000_000);
42        assert_eq!(power_for_gpl(1_000_000), 1_000_000_000_000_000);
43        assert_eq!(power_for_gpl(5_000_000), 25_000_000_000_000_000);
44        assert_eq!(power_for_gpl(10_000_000), 100_000_000_000_000_000);
45        assert_eq!(power_for_gpl(100_000_000), 10_000_000_000_000_000_000);
46        // beyond this value the return overflows a u64
47        assert_eq!(power_for_gpl(135_818_791), 18_446_743_988_701_681_000);
48        // must be u128 return to fit this one!
49        assert_eq!(power_for_gpl(135_818_792), 18_446_744_260_339_264_000);
50        assert_eq!(power_for_gpl(1_000_000_000), 1_000_000_000_000_000_000_000);
51        assert_eq!(power_for_gpl(4_000_000_000), 16_000_000_000_000_000_000_000);
52        assert_eq!(power_for_gpl(u32::MAX), 18_446_744_065_119_617_025_000);
53    }
54}