Skip to content

Commit

Permalink
Merge pull request #322 from shanemadden/screeps_4_2
Browse files Browse the repository at this point in the history
Screeps decoration update new features and deprecations
  • Loading branch information
ASalvail authored Nov 18, 2020
2 parents 56d7548 + 435c018 commit 3bd3c5f
Show file tree
Hide file tree
Showing 11 changed files with 560 additions and 452 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Unreleased
- Add the `parse_display::Display` trait to type constant enums which currently implement
`parse_display::FromStr`, allowing reversal of the conversion from native to constant string
- Fixed `Position::get_direction_to` which always returned the opposite of what it should
- Fixed deserialization of `EVENT_POWER` and `EVENT_TRANSFER` events
- Add new `IntershardResourceType::CPUUnlock`, `IntershardResourceType::Pixel`, and
`IntershardResourceType::AccessKey` resources
- Add `game::cpu::generate_pixel` and `constants::PIXEL_COST`

0.8.0 (2020-05-30)
==================
Expand Down
6 changes: 6 additions & 0 deletions javascript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ function __resource_type_num_to_str(num) {
case 83: return RESOURCE_EMANATION;
case 84: return RESOURCE_ESSENCE;
case 1001: return SUBSCRIPTION_TOKEN;
case 1002: return CPU_UNLOCK;
case 1003: return PIXEL;
case 1004: return ACCESS_KEY;
default: throw new Error("unknown resource type integer encoding " + num);
}
}
Expand Down Expand Up @@ -298,6 +301,9 @@ function __resource_type_str_to_num(str) {
case RESOURCE_EMANATION: return 83;
case RESOURCE_ESSENCE: return 84;
case SUBSCRIPTION_TOKEN: return 1001;
case CPU_UNLOCK: return 1002;
case PIXEL: return 1003;
case ACCESS_KEY: return 1004;
default: throw new Error("unknown resource type " + str);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/constants/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,13 @@ pub const MARKET_ORDER_LIFE_TIME: u32 = 30 * 24 * 3600 * 1000;
/// Maximum number of total flags a player is allowed to have on a shard.
pub const FLAGS_LIMIT: u32 = 10_000;

/// Cost, paid from [`game::cpu::bucket`], to generate a pixel using
/// [`game::cpu::generate_pixel`]
///
/// [`game::cpu::bucket`]: crate::game::cpu::bucket
/// [`game::cpu::generate_pixel`]: crate::game::cpu::generate_pixel
pub const PIXEL_CPU_COST: u32 = 5000;

// Resources defined in `types.rs`

// REACTIONS defined in `recipes.rs`
Expand Down
188 changes: 101 additions & 87 deletions src/constants/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ pub enum IntershardResourceType {
/// `"token"`
#[display("token")]
SubscriptionToken = 1001,
/// `"cpuUnlock"`
#[display("cpuUnlock")]
CPUUnlock = 1002,
/// `"pixel"`
#[display("pixel")]
Pixel = 1003,
/// `"accessKey"`
#[display("accessKey")]
AccessKey = 1004,
}

impl IntershardResourceType {
Expand Down Expand Up @@ -712,95 +721,100 @@ impl<'de> Deserialize<'de> for MarketResourceType {
where
D: Deserializer<'de>,
{
use IntershardResourceType::*;
use MarketResourceType::*;
use ResourceType::*;

let resource = u16::deserialize(deserializer)?;
let resource_type = match resource {
1 => MarketResourceType::Resource(ResourceType::Energy),
2 => MarketResourceType::Resource(ResourceType::Power),
3 => MarketResourceType::Resource(ResourceType::Hydrogen),
4 => MarketResourceType::Resource(ResourceType::Oxygen),
5 => MarketResourceType::Resource(ResourceType::Utrium),
6 => MarketResourceType::Resource(ResourceType::Lemergium),
7 => MarketResourceType::Resource(ResourceType::Keanium),
8 => MarketResourceType::Resource(ResourceType::Zynthium),
9 => MarketResourceType::Resource(ResourceType::Catalyst),
10 => MarketResourceType::Resource(ResourceType::Ghodium),
11 => MarketResourceType::Resource(ResourceType::Hydroxide),
12 => MarketResourceType::Resource(ResourceType::ZynthiumKeanite),
13 => MarketResourceType::Resource(ResourceType::UtriumLemergite),
14 => MarketResourceType::Resource(ResourceType::UtriumHydride),
15 => MarketResourceType::Resource(ResourceType::UtriumOxide),
16 => MarketResourceType::Resource(ResourceType::KeaniumHydride),
17 => MarketResourceType::Resource(ResourceType::KeaniumOxide),
18 => MarketResourceType::Resource(ResourceType::LemergiumHydride),
19 => MarketResourceType::Resource(ResourceType::LemergiumOxide),
20 => MarketResourceType::Resource(ResourceType::ZynthiumHydride),
21 => MarketResourceType::Resource(ResourceType::ZynthiumOxide),
22 => MarketResourceType::Resource(ResourceType::GhodiumHydride),
23 => MarketResourceType::Resource(ResourceType::GhodiumOxide),
24 => MarketResourceType::Resource(ResourceType::UtriumAcid),
25 => MarketResourceType::Resource(ResourceType::UtriumAlkalide),
26 => MarketResourceType::Resource(ResourceType::KeaniumAcid),
27 => MarketResourceType::Resource(ResourceType::KeaniumAlkalide),
28 => MarketResourceType::Resource(ResourceType::LemergiumAcid),
29 => MarketResourceType::Resource(ResourceType::LemergiumAlkalide),
30 => MarketResourceType::Resource(ResourceType::ZynthiumAcid),
31 => MarketResourceType::Resource(ResourceType::ZynthiumAlkalide),
32 => MarketResourceType::Resource(ResourceType::GhodiumAcid),
33 => MarketResourceType::Resource(ResourceType::GhodiumAlkalide),
34 => MarketResourceType::Resource(ResourceType::CatalyzedUtriumAcid),
35 => MarketResourceType::Resource(ResourceType::CatalyzedUtriumAlkalide),
36 => MarketResourceType::Resource(ResourceType::CatalyzedKeaniumAcid),
37 => MarketResourceType::Resource(ResourceType::CatalyzedKeaniumAlkalide),
38 => MarketResourceType::Resource(ResourceType::CatalyzedLemergiumAcid),
39 => MarketResourceType::Resource(ResourceType::CatalyzedLemergiumAlkalide),
40 => MarketResourceType::Resource(ResourceType::CatalyzedZynthiumAcid),
41 => MarketResourceType::Resource(ResourceType::CatalyzedZynthiumAlkalide),
42 => MarketResourceType::Resource(ResourceType::CatalyzedGhodiumAcid),
43 => MarketResourceType::Resource(ResourceType::CatalyzedGhodiumAlkalide),
44 => MarketResourceType::Resource(ResourceType::Ops),
45 => MarketResourceType::Resource(ResourceType::Silicon),
46 => MarketResourceType::Resource(ResourceType::Metal),
47 => MarketResourceType::Resource(ResourceType::Biomass),
48 => MarketResourceType::Resource(ResourceType::Mist),
49 => MarketResourceType::Resource(ResourceType::UtriumBar),
50 => MarketResourceType::Resource(ResourceType::LemergiumBar),
51 => MarketResourceType::Resource(ResourceType::ZynthiumBar),
52 => MarketResourceType::Resource(ResourceType::KeaniumBar),
53 => MarketResourceType::Resource(ResourceType::GhodiumMelt),
54 => MarketResourceType::Resource(ResourceType::Oxidant),
55 => MarketResourceType::Resource(ResourceType::Reductant),
56 => MarketResourceType::Resource(ResourceType::Purifier),
57 => MarketResourceType::Resource(ResourceType::Battery),
58 => MarketResourceType::Resource(ResourceType::Composite),
59 => MarketResourceType::Resource(ResourceType::Crystal),
60 => MarketResourceType::Resource(ResourceType::Liquid),
61 => MarketResourceType::Resource(ResourceType::Wire),
62 => MarketResourceType::Resource(ResourceType::Switch),
63 => MarketResourceType::Resource(ResourceType::Transistor),
64 => MarketResourceType::Resource(ResourceType::Microchip),
65 => MarketResourceType::Resource(ResourceType::Circuit),
66 => MarketResourceType::Resource(ResourceType::Device),
67 => MarketResourceType::Resource(ResourceType::Cell),
68 => MarketResourceType::Resource(ResourceType::Phlegm),
69 => MarketResourceType::Resource(ResourceType::Tissue),
70 => MarketResourceType::Resource(ResourceType::Muscle),
71 => MarketResourceType::Resource(ResourceType::Organoid),
72 => MarketResourceType::Resource(ResourceType::Organism),
73 => MarketResourceType::Resource(ResourceType::Alloy),
74 => MarketResourceType::Resource(ResourceType::Tube),
75 => MarketResourceType::Resource(ResourceType::Fixtures),
76 => MarketResourceType::Resource(ResourceType::Frame),
77 => MarketResourceType::Resource(ResourceType::Hydraulics),
78 => MarketResourceType::Resource(ResourceType::Machine),
79 => MarketResourceType::Resource(ResourceType::Condensate),
80 => MarketResourceType::Resource(ResourceType::Concentrate),
81 => MarketResourceType::Resource(ResourceType::Extract),
82 => MarketResourceType::Resource(ResourceType::Spirit),
83 => MarketResourceType::Resource(ResourceType::Emanation),
84 => MarketResourceType::Resource(ResourceType::Essence),
1001 => {
MarketResourceType::IntershardResource(IntershardResourceType::SubscriptionToken)
}
1 => Resource(Energy),
2 => Resource(Power),
3 => Resource(Hydrogen),
4 => Resource(Oxygen),
5 => Resource(Utrium),
6 => Resource(Lemergium),
7 => Resource(Keanium),
8 => Resource(Zynthium),
9 => Resource(Catalyst),
10 => Resource(Ghodium),
11 => Resource(Hydroxide),
12 => Resource(ZynthiumKeanite),
13 => Resource(UtriumLemergite),
14 => Resource(UtriumHydride),
15 => Resource(UtriumOxide),
16 => Resource(KeaniumHydride),
17 => Resource(KeaniumOxide),
18 => Resource(LemergiumHydride),
19 => Resource(LemergiumOxide),
20 => Resource(ZynthiumHydride),
21 => Resource(ZynthiumOxide),
22 => Resource(GhodiumHydride),
23 => Resource(GhodiumOxide),
24 => Resource(UtriumAcid),
25 => Resource(UtriumAlkalide),
26 => Resource(KeaniumAcid),
27 => Resource(KeaniumAlkalide),
28 => Resource(LemergiumAcid),
29 => Resource(LemergiumAlkalide),
30 => Resource(ZynthiumAcid),
31 => Resource(ZynthiumAlkalide),
32 => Resource(GhodiumAcid),
33 => Resource(GhodiumAlkalide),
34 => Resource(CatalyzedUtriumAcid),
35 => Resource(CatalyzedUtriumAlkalide),
36 => Resource(CatalyzedKeaniumAcid),
37 => Resource(CatalyzedKeaniumAlkalide),
38 => Resource(CatalyzedLemergiumAcid),
39 => Resource(CatalyzedLemergiumAlkalide),
40 => Resource(CatalyzedZynthiumAcid),
41 => Resource(CatalyzedZynthiumAlkalide),
42 => Resource(CatalyzedGhodiumAcid),
43 => Resource(CatalyzedGhodiumAlkalide),
44 => Resource(Ops),
45 => Resource(Silicon),
46 => Resource(Metal),
47 => Resource(Biomass),
48 => Resource(Mist),
49 => Resource(UtriumBar),
50 => Resource(LemergiumBar),
51 => Resource(ZynthiumBar),
52 => Resource(KeaniumBar),
53 => Resource(GhodiumMelt),
54 => Resource(Oxidant),
55 => Resource(Reductant),
56 => Resource(Purifier),
57 => Resource(Battery),
58 => Resource(Composite),
59 => Resource(Crystal),
60 => Resource(Liquid),
61 => Resource(Wire),
62 => Resource(Switch),
63 => Resource(Transistor),
64 => Resource(Microchip),
65 => Resource(Circuit),
66 => Resource(Device),
67 => Resource(Cell),
68 => Resource(Phlegm),
69 => Resource(Tissue),
70 => Resource(Muscle),
71 => Resource(Organoid),
72 => Resource(Organism),
73 => Resource(Alloy),
74 => Resource(Tube),
75 => Resource(Fixtures),
76 => Resource(Frame),
77 => Resource(Hydraulics),
78 => Resource(Machine),
79 => Resource(Condensate),
80 => Resource(Concentrate),
81 => Resource(Extract),
82 => Resource(Spirit),
83 => Resource(Emanation),
84 => Resource(Essence),
1001 => IntershardResource(SubscriptionToken),
1002 => IntershardResource(CPUUnlock),
1003 => IntershardResource(Pixel),
1004 => IntershardResource(AccessKey),
_ => {
return Err(D::Error::invalid_value(
Unexpected::Unsigned(resource as u64),
Expand Down
64 changes: 52 additions & 12 deletions src/game/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,26 @@ pub fn shard_limits() -> collections::HashMap<String, u32> {
js_unwrap!(Game.cpu.shardLimits)
}

/// See [http://docs.screeps.com/api/#Game.getHeapStatistics]
/// Whether you have an active subscription and are able to use your full CPU
/// limit. See [http://docs.screeps.com/api/#Game.cpu]
///
/// [http://docs.screeps.com/api/#Game.getHeapStatistics]: http://docs.screeps.com/api/#Game.getHeapStatistics
/// [http://docs.screeps.com/api/#Game.cpu]: http://docs.screeps.com/api/#Game.cpu
pub fn unlocked() -> bool {
// undefined on private servers; return true in that case
js_unwrap!(Game.cpu.unlocked || Game.cpu.unlocked === undefined)
}

/// Time of expiration of your current CPU subscription in milliseconds since
/// epoch, or None when locked, or unlocked via subscription. See [http://docs.screeps.com/api/#Game.cpu]
///
/// [http://docs.screeps.com/api/#Game.cpu]: http://docs.screeps.com/api/#Game.cpu
pub fn unlocked_time() -> Option<u64> {
js_unwrap!(Game.cpu.unlockedTime)
}

/// See [https://docs.screeps.com/api/#Game.cpu.getHeapStatistics]
///
/// [https://docs.screeps.com/api/#Game.cpu.getHeapStatistics]: https://docs.screeps.com/api/#Game.cpu.getHeapStatistics
///
/// Returns object with all 0 values if heap statistics are not available.
pub fn get_heap_statistics() -> HeapStatistics {
Expand All @@ -73,25 +90,48 @@ pub fn get_heap_statistics() -> HeapStatistics {
}
}

/// See [http://docs.screeps.com/api/#Game.getUsed]
/// See [https://docs.screeps.com/api/#Game.cpu.getUsed]
///
/// [http://docs.screeps.com/api/#Game.getUsed]: http://docs.screeps.com/api/#Game.getUsed
/// [https://docs.screeps.com/api/#Game.cpu.getUsed]: https://docs.screeps.com/api/#Game.cpu.getUsed
pub fn get_used() -> f64 {
js_unwrap!(Game.cpu.getUsed())
}

/// See [http://docs.screeps.com/api/#Game.setShardLimits]
///
/// [http://docs.screeps.com/api/#Game.setShardLimits]: http://docs.screeps.com/api/#Game.setShardLimits
pub fn set_shard_limits(limits: collections::HashMap<String, u32>) -> ReturnCode {
js_unwrap!(Game.cpu.setShardLimits(@{limits}))
}

/// Reset your runtime environment and wipe all data in heap memory.
///
/// See [Game.cpu.halt()](https://docs.screeps.com/api/#Game.halt).
/// See [`Game.cpu.halt`](https://docs.screeps.com/api/#Game.cpu.halt).
pub fn halt() {
js! {
Game.cpu.halt();
}
}

/// See [https://docs.screeps.com/api/#Game.cpu.setShardLimits]
///
/// [https://docs.screeps.com/api/#Game.cpu.setShardLimits]: https://docs.screeps.com/api/#Game.cpu.setShardLimits
pub fn set_shard_limits(limits: collections::HashMap<String, u32>) -> ReturnCode {
js_unwrap!(Game.cpu.setShardLimits(@{limits}))
}

/// Spend a [`CPUUnlock`] from your intershard resource inventory to unlock your
/// full CPU limit for 24 hours
///
/// See [`Game.cpu.unlock`](https://docs.screeps.com/api/#Game.cpu.unlock).
///
/// [`CPUUnlock`]: crate::constants::types::IntershardResourceType::CPUUnlock
pub fn unlock() -> ReturnCode {
// undefined on private servers, return OK in that case
js_unwrap!(typeof(Game.cpu.unlock) == "function" && Game.cpu.unlock() || 0)
}

/// Generate a [`Pixel`], spending [`PIXEL_CPU_COST`] from [`game::cpu::bucket`]
///
/// See [`Game.cpu.generatePixel`](https://docs.screeps.com/api/#Game.cpu.generatePixel).
///
/// [`Pixel`]: crate::constants::IntershardResourceType::Pixel
/// [`PIXEL_CPU_COST`]: crate::constants::PIXEL_CPU_COST
/// [`game::cpu::bucket`]: crate::game::cpu::bucket
pub fn generate_pixel() -> ReturnCode {
// undefined on private servers, return OK in that case
js_unwrap!(typeof(Game.cpu.generatePixel) == "function" && Game.cpu.generatePixel() || 0)
}
14 changes: 6 additions & 8 deletions src/game/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,12 @@ pub fn find_exit_with_callback(

// Type erased and boxed callback: no longer a type specific to the closure
// passed in, now unified as &Fn
let callback_type_erased: &mut (dyn FnMut(RoomName, RoomName) -> f64) =
&mut callback_boxed;
let callback_type_erased: &mut (dyn FnMut(RoomName, RoomName) -> f64) = &mut callback_boxed;

// Overwrite lifetime of reference so it can be passed to javascript.
// Overwrite lifetime of reference so it can be passed to javascript.
// It's now pretending to be static data. This should be entirely safe
// because we control the only use of it and it remains valid during the
// pathfinder callback. This transmute is necessary because "some lifetime
// pathfinder callback. This transmute is necessary because "some lifetime
// above the current scope but otherwise unknown" is not a valid lifetime.
let callback_lifetime_erased: &'static mut dyn FnMut(RoomName, RoomName) -> f64 =
unsafe { mem::transmute(callback_type_erased) };
Expand Down Expand Up @@ -164,13 +163,12 @@ pub fn find_route_with_callback(

// Type erased and boxed callback: no longer a type specific to the closure
// passed in, now unified as &Fn
let callback_type_erased: &mut (dyn FnMut(RoomName, RoomName) -> f64) =
&mut callback_boxed;
let callback_type_erased: &mut (dyn FnMut(RoomName, RoomName) -> f64) = &mut callback_boxed;

// Overwrite lifetime of reference so it can be passed to javascript.
// Overwrite lifetime of reference so it can be passed to javascript.
// It's now pretending to be static data. This should be entirely safe
// because we control the only use of it and it remains valid during the
// pathfinder callback. This transmute is necessary because "some lifetime
// pathfinder callback. This transmute is necessary because "some lifetime
// above the current scope but otherwise unknown" is not a valid lifetime.
let callback_lifetime_erased: &'static mut dyn FnMut(RoomName, RoomName) -> f64 =
unsafe { mem::transmute(callback_type_erased) };
Expand Down
Loading

0 comments on commit 3bd3c5f

Please sign in to comment.