screeps/objects/impls/cost_matrix.rs
1use js_sys::{Array, Object, Uint8Array};
2use wasm_bindgen::prelude::*;
3
4use crate::{
5 local::{LocalCostMatrix, RoomXY},
6 prototypes::COST_MATRIX_PROTOTYPE,
7 traits::{CostMatrixGet, CostMatrixSet},
8};
9
10#[wasm_bindgen]
11extern "C" {
12 /// A reference to a matrix of pathing costs for a room, stored in
13 /// JavaScript memory.
14 ///
15 /// Use [`LocalCostMatrix`] to store and access the same data in Rust
16 /// memory.
17 ///
18 /// [Screeps documentation](https://docs.screeps.com/api/#PathFinder-CostMatrix)
19 #[wasm_bindgen(js_namespace = PathFinder)]
20 pub type CostMatrix;
21
22 /// Create a new reference to a CostMatrix, containing 0s in all positions,
23 /// using the normal constructor.
24 ///
25 /// [Screeps documentation](https://docs.screeps.com/api/#PathFinder.CostMatrix.constructor)
26 #[wasm_bindgen(constructor, js_namespace = PathFinder)]
27 pub fn new() -> CostMatrix;
28
29 /// Gets a reference to the [`Uint8Array`] underlying this [`CostMatrix`].
30 #[wasm_bindgen(method, getter = _bits)]
31 pub fn get_bits(this: &CostMatrix) -> Uint8Array;
32
33 /// Sets a [`Uint8Array`] to this [`CostMatrix`], overwriting any current
34 /// contents.
35 #[wasm_bindgen(method, setter = _bits)]
36 pub fn set_bits(this: &CostMatrix, arr: &Uint8Array);
37
38 /// Sets a new value for a specific position in this [`CostMatrix`].
39 ///
40 /// [Screeps documentation](https://docs.screeps.com/api/#PathFinder.CostMatrix.set)
41 #[wasm_bindgen(method)]
42 pub fn set(this: &CostMatrix, x: u8, y: u8, cost: u8);
43
44 /// Get the value of a specific position in this [`CostMatrix`].
45 ///
46 /// [Screeps documentation](https://docs.screeps.com/api/#PathFinder.CostMatrix.get)
47 #[wasm_bindgen(method)]
48 pub fn get(this: &CostMatrix, x: u8, y: u8) -> u8;
49
50 /// Get a new [`CostMatrix`] with data copied from the current one
51 ///
52 /// [Screeps documentation](https://docs.screeps.com/api/#PathFinder.CostMatrix.clone)
53 #[wasm_bindgen(method)]
54 pub fn clone(this: &CostMatrix) -> CostMatrix;
55
56 /// Get an [`Array`] of numbers representing the [`CostMatrix`] that's
57 /// appropriate for memory serialization.
58 ///
59 /// [Screeps documentation](https://docs.screeps.com/api/#PathFinder.CostMatrix.serialize)
60 #[wasm_bindgen(method)]
61 pub fn serialize(this: &CostMatrix) -> Array;
62
63 /// Get a new [`CostMatrix`] using the array representation from
64 /// [`CostMatrix::serialize`].
65 ///
66 /// [Screeps documentation](https://docs.screeps.com/api/#PathFinder.CostMatrix.deserialize)
67 #[wasm_bindgen(static_method_of = CostMatrix, js_namespace = PathFinder)]
68 pub fn deserialize(val: Array) -> CostMatrix;
69}
70
71impl CostMatrix {
72 /// Create a new [`CostMatrix`], taking a u8 slice with 2500 members such as
73 /// that returned from [`LocalCostMatrix::get_bits`] which will be copied
74 /// across the memory boundary.
75 ///
76 /// [`LocalCostMatrix::get_bits`]: crate::local::LocalCostMatrix
77 pub fn new_from_bits(bits: &[u8]) -> CostMatrix {
78 let matrix = CostMatrix::from(JsValue::from(Object::create(&COST_MATRIX_PROTOTYPE)));
79 matrix.set_bits(&Uint8Array::from(bits));
80 matrix
81 }
82
83 // todo also a function that takes the unsafe view into wasm linear mem with
84 // view for a matrix that'll easily go bad
85}
86
87impl From<LocalCostMatrix> for CostMatrix {
88 fn from(matrix: LocalCostMatrix) -> Self {
89 CostMatrix::new_from_bits(matrix.get_bits())
90 }
91}
92
93impl CostMatrixSet for CostMatrix {
94 fn set_xy(&mut self, xy: RoomXY, cost: u8) {
95 CostMatrix::set(self, xy.x.u8(), xy.y.u8(), cost);
96 }
97}
98
99impl CostMatrixGet for CostMatrix {
100 fn get_xy(&mut self, xy: RoomXY) -> u8 {
101 CostMatrix::get(self, xy.x.u8(), xy.y.u8())
102 }
103}