screeps/objects/impls/source.rs
1use js_sys::JsString;
2use wasm_bindgen::prelude::*;
3
4use crate::{objects::RoomObject, prelude::*};
5
6#[wasm_bindgen]
7extern "C" {
8 /// A [`Source`], which can be harvested for energy.
9 ///
10 /// [Screeps documentation](https://docs.screeps.com/api/#Source)
11 #[wasm_bindgen(extends = RoomObject)]
12 #[derive(Clone, Debug)]
13 pub type Source;
14
15 /// Amount of energy available to be harvested from the source.
16 ///
17 /// [Screeps documentation](https://docs.screeps.com/api/#Source.energy)
18 #[wasm_bindgen(method, getter)]
19 pub fn energy(this: &Source) -> u32;
20
21 /// Amount of energy this source will regenerate to after
22 /// [`Source::ticks_to_regeneration`] reaches 0.
23 ///
24 /// Value depends on the type of room the source is in:
25 ///
26 /// - Owned and reserved rooms: [`SOURCE_ENERGY_CAPACITY`]
27 /// - Neutral rooms: [`SOURCE_ENERGY_NEUTRAL_CAPACITY`]
28 /// - Source Keeper rooms: [`SOURCE_ENERGY_KEEPER_CAPACITY`]
29 ///
30 /// [Screeps documentation](https://docs.screeps.com/api/#Source.energy)
31 ///
32 /// [`SOURCE_ENERGY_CAPACITY`]: crate::constants::SOURCE_ENERGY_CAPACITY
33 /// [`SOURCE_ENERGY_NEUTRAL_CAPACITY`]:
34 /// crate::constants::SOURCE_ENERGY_NEUTRAL_CAPACITY
35 /// [`SOURCE_ENERGY_KEEPER_CAPACITY`]:
36 /// crate::constants::SOURCE_ENERGY_KEEPER_CAPACITY
37 #[wasm_bindgen(method, getter = energyCapacity)]
38 pub fn energy_capacity(this: &Source) -> u32;
39
40 /// Object ID of the source, which can be used to efficiently fetch a fresh
41 /// reference to the object on subsequent ticks.
42 ///
43 /// [Screeps documentation](https://docs.screeps.com/api/#Source.id)
44 #[wasm_bindgen(method, getter = id)]
45 fn id_internal(this: &Source) -> JsString;
46
47 /// The number of ticks until this source regenerates to its
48 /// [`Source::energy_capacity`], or `None` if the source has not started to
49 /// regenerate.
50 ///
51 /// [Screeps documentation](https://docs.screeps.com/api/#Source.ticksToRegeneration)
52 #[wasm_bindgen(method, getter = ticksToRegeneration)]
53 pub fn ticks_to_regeneration(this: &Source) -> Option<u32>;
54}
55
56impl HasId for Source {
57 fn js_raw_id(&self) -> JsString {
58 Self::id_internal(self)
59 }
60}
61
62impl Harvestable for Source {}