screeps/objects/impls/
structure_terminal.rs

1use js_sys::JsString;
2use wasm_bindgen::prelude::*;
3
4use crate::{
5    constants::{ErrorCode, ResourceType},
6    local::RoomName,
7    objects::{OwnedStructure, RoomObject, Store, Structure},
8    prelude::*,
9};
10
11#[wasm_bindgen]
12extern "C" {
13    /// An object representing a [`StructureTerminal`], which can send resources
14    /// to distant rooms and participate in the market.
15    ///
16    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal)
17    #[wasm_bindgen(extends = RoomObject, extends = Structure, extends = OwnedStructure)]
18    #[derive(Clone, Debug)]
19    pub type StructureTerminal;
20
21    /// The number of ticks until the [`StructureTerminal`] can use
22    /// [`StructureTerminal::send`] or be used in a market transaction again.
23    ///
24    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal.cooldown)
25    #[wasm_bindgen(method, getter)]
26    pub fn cooldown(this: &StructureTerminal) -> u32;
27
28    /// The [`Store`] of the terminal, which contains information about what
29    /// resources it is it holding.
30    ///
31    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal.store)
32    #[wasm_bindgen(method, getter)]
33    pub fn store(this: &StructureTerminal) -> Store;
34
35    #[wasm_bindgen(method, js_name = send)]
36    fn send_internal(
37        this: &StructureTerminal,
38        resource_type: ResourceType,
39        amount: u32,
40        destination: &JsString,
41        description: Option<&JsString>,
42    ) -> i8;
43}
44
45impl StructureTerminal {
46    /// Send resources to another room's terminal.
47    ///
48    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal.send)
49    pub fn send(
50        &self,
51        resource_type: ResourceType,
52        amount: u32,
53        destination: RoomName,
54        description: Option<&str>,
55    ) -> Result<(), ErrorCode> {
56        let desination = destination.into();
57        let description = description.map(JsString::from);
58
59        ErrorCode::result_from_i8(self.send_internal(
60            resource_type,
61            amount,
62            &desination,
63            description.as_ref(),
64        ))
65    }
66}
67
68impl HasCooldown for StructureTerminal {
69    fn cooldown(&self) -> u32 {
70        Self::cooldown(self)
71    }
72}
73
74impl HasStore for StructureTerminal {
75    fn store(&self) -> Store {
76        Self::store(self)
77    }
78}
79
80impl Attackable for StructureTerminal {}
81impl Dismantleable for StructureTerminal {}
82impl Repairable for StructureTerminal {}
83impl Transferable for StructureTerminal {}
84impl Withdrawable for StructureTerminal {}