screeps/objects/impls/
structure_link.rs

1use wasm_bindgen::prelude::*;
2
3use crate::{
4    enums::action_error_codes::structure_link::*,
5    objects::{OwnedStructure, RoomObject, Store, Structure},
6    prelude::*,
7};
8
9#[wasm_bindgen]
10extern "C" {
11    /// An object representing a [`StructureLink`], which can hold energy and
12    /// transfer it to other links within the room.
13    ///
14    /// [Screeps documentation](https://docs.screeps.com/api/#StructureLink)
15    #[wasm_bindgen(extends = RoomObject, extends = Structure, extends = OwnedStructure)]
16    #[derive(Clone, Debug)]
17    pub type StructureLink;
18
19    /// The number of ticks until the [`StructureLink`] can use
20    /// [`StructureLink::transfer`] again.
21    ///
22    /// [Screeps documentation](https://docs.screeps.com/api/#StructureLink.cooldown)
23    #[wasm_bindgen(method, getter)]
24    pub fn cooldown(this: &StructureLink) -> u32;
25
26    /// The [`Store`] of the extension, which contains information about the
27    /// amount of energy in it.
28    ///
29    /// [Screeps documentation](https://docs.screeps.com/api/#StructureLink.store)
30    #[wasm_bindgen(method, getter)]
31    pub fn store(this: &StructureLink) -> Store;
32
33    #[wasm_bindgen(method, js_name = transferEnergy)]
34    fn transfer_energy_internal(
35        this: &StructureLink,
36        target: &StructureLink,
37        amount: Option<u32>,
38    ) -> i8;
39}
40
41impl StructureLink {
42    /// Transfer energy from this [`StructureLink`] to another, losing
43    /// [`LINK_LOSS_RATIO`] percent of the energt and incurring a cooldown of
44    /// [`LINK_COOLDOWN`] tick per range to the target.
45    ///
46    /// [Screeps documentation](https://docs.screeps.com/api/#StructureLink.transferEnergy)
47    ///
48    /// [`LINK_LOSS_RATIO`]: crate::constants::LINK_LOSS_RATIO
49    /// [`LINK_COOLDOWN`]: crate::constants::LINK_COOLDOWN
50    pub fn transfer_energy(
51        &self,
52        target: &StructureLink,
53        amount: Option<u32>,
54    ) -> Result<(), TransferEnergyErrorCode> {
55        TransferEnergyErrorCode::result_from_i8(self.transfer_energy_internal(target, amount))
56    }
57}
58
59impl HasCooldown for StructureLink {
60    fn cooldown(&self) -> u32 {
61        Self::cooldown(self)
62    }
63}
64
65impl HasStore for StructureLink {
66    fn store(&self) -> Store {
67        Self::store(self)
68    }
69}
70
71impl Attackable for StructureLink {}
72impl Dismantleable for StructureLink {}
73impl Repairable for StructureLink {}
74impl Transferable for StructureLink {}
75impl Withdrawable for StructureLink {}