screeps/objects/impls/
creep_shared.rs1use js_sys::Object;
2use wasm_bindgen::prelude::*;
3
4use crate::{
5 local::RoomName,
6 objects::{CostMatrix, FindPathOptions, PolyStyle},
7 pathfinder::SingleRoomCostResult,
8};
9
10#[wasm_bindgen]
11extern "C" {
12 #[wasm_bindgen]
13 pub type JsMoveToOptions;
14
15 #[wasm_bindgen(method, setter = reusePath)]
16 pub fn reuse_path(this: &JsMoveToOptions, ticks: u32);
17
18 #[wasm_bindgen(method, setter = serializeMemory)]
19 pub fn serialize_memory(this: &JsMoveToOptions, serialize: bool);
20
21 #[wasm_bindgen(method, setter = noPathFinding)]
22 pub fn no_path_finding(this: &JsMoveToOptions, require: bool);
23
24 #[wasm_bindgen(method, setter = visualizePathStyle)]
25 pub fn visualize_path_style(this: &JsMoveToOptions, style: &JsValue);
26
27 #[wasm_bindgen(method, setter = heuristicWeight)]
30 pub fn find_path_options(this: &JsMoveToOptions, options: &JsValue);
31}
32
33impl JsMoveToOptions {
34 pub fn new() -> JsMoveToOptions {
35 Object::new().unchecked_into()
36 }
37}
38
39impl Default for JsMoveToOptions {
40 fn default() -> Self {
41 Self::new()
42 }
43}
44
45pub struct MoveToOptions<F>
46where
47 F: FnMut(RoomName, CostMatrix) -> SingleRoomCostResult,
48{
49 pub(crate) reuse_path: Option<u32>,
50 pub(crate) serialize_memory: Option<bool>,
51 pub(crate) no_path_finding: Option<bool>,
52 pub(crate) visualize_path_style: Option<PolyStyle>,
53 pub(crate) find_path_options: FindPathOptions<F, SingleRoomCostResult>,
54}
55
56impl Default for MoveToOptions<fn(RoomName, CostMatrix) -> SingleRoomCostResult> {
57 fn default() -> Self {
58 MoveToOptions {
59 reuse_path: None,
60 serialize_memory: None,
61 no_path_finding: None,
62 visualize_path_style: None,
63 find_path_options: FindPathOptions::default(),
64 }
65 }
66}
67
68impl MoveToOptions<fn(RoomName, CostMatrix) -> SingleRoomCostResult> {
69 pub fn new() -> Self {
71 Self::default()
72 }
73}
74
75impl<F> MoveToOptions<F>
76where
77 F: FnMut(RoomName, CostMatrix) -> SingleRoomCostResult,
78{
79 pub fn reuse_path(mut self, n_ticks: u32) -> Self {
81 self.reuse_path = Some(n_ticks);
82 self
83 }
84
85 pub fn serialize_memory(mut self, serialize: bool) -> Self {
87 self.serialize_memory = Some(serialize);
88 self
89 }
90
91 pub fn no_path_finding(mut self, no_finding: bool) -> Self {
93 self.no_path_finding = Some(no_finding);
94 self
95 }
96
97 pub fn visualize_path_style(mut self, style: PolyStyle) -> Self {
100 self.visualize_path_style = Some(style);
101 self
102 }
103
104 pub fn ignore_creeps(mut self, ignore: bool) -> Self {
106 self.find_path_options.ignore_creeps = Some(ignore);
107 self
108 }
109
110 pub fn ignore_destructible_structures(mut self, ignore: bool) -> Self {
113 self.find_path_options.ignore_destructible_structures = Some(ignore);
114 self
115 }
116
117 pub fn cost_callback<F2>(self, cost_callback: F2) -> MoveToOptions<F2>
119 where
120 F2: FnMut(RoomName, CostMatrix) -> SingleRoomCostResult,
121 {
122 let new_options: MoveToOptions<F2> = MoveToOptions {
123 reuse_path: self.reuse_path,
124 serialize_memory: self.serialize_memory,
125 no_path_finding: self.no_path_finding,
126 visualize_path_style: self.visualize_path_style,
127 find_path_options: self.find_path_options.cost_callback(cost_callback),
128 };
129
130 new_options
131 }
132
133 pub fn max_ops(mut self, ops: u32) -> Self {
135 self.find_path_options.max_ops = Some(ops);
136 self
137 }
138
139 pub fn heuristic_weight(mut self, weight: f64) -> Self {
141 self.find_path_options.heuristic_weight = Some(weight);
142 self
143 }
144
145 pub fn serialize(mut self, s: bool) -> Self {
147 self.find_path_options.serialize = Some(s);
148 self
149 }
150
151 pub fn max_rooms(mut self, rooms: u8) -> Self {
153 self.find_path_options.max_rooms = Some(rooms);
154 self
155 }
156
157 pub fn range(mut self, k: u32) -> Self {
158 self.find_path_options.range = Some(k);
159 self
160 }
161
162 pub fn plain_cost(mut self, cost: u8) -> Self {
164 self.find_path_options.plain_cost = Some(cost);
165 self
166 }
167
168 pub fn swamp_cost(mut self, cost: u8) -> Self {
170 self.find_path_options.swamp_cost = Some(cost);
171 self
172 }
173
174 pub fn find_path_options<F2>(
177 self,
178 find_path_options: FindPathOptions<F2, SingleRoomCostResult>,
179 ) -> MoveToOptions<F2>
180 where
181 F2: FnMut(RoomName, CostMatrix) -> SingleRoomCostResult,
182 {
183 MoveToOptions {
184 reuse_path: self.reuse_path,
185 serialize_memory: self.serialize_memory,
186 no_path_finding: self.no_path_finding,
187 visualize_path_style: self.visualize_path_style,
188 find_path_options,
189 }
190 }
191
192 pub(crate) fn into_js_options<CR>(self, callback: impl Fn(&JsMoveToOptions) -> CR) -> CR {
193 let js_options = JsMoveToOptions::new();
198
199 if let Some(reuse_path) = self.reuse_path {
200 js_options.reuse_path(reuse_path);
201 }
202
203 if let Some(serialize_memory) = self.serialize_memory {
204 js_options.serialize_memory(serialize_memory);
205 }
206
207 if let Some(no_path_finding) = self.no_path_finding {
208 js_options.no_path_finding(no_path_finding);
209 }
210
211 if let Some(visualize_path_style) = self.visualize_path_style {
212 let style = serde_wasm_bindgen::to_value(&visualize_path_style)
213 .expect("expected to serialize visualize path style");
214
215 js_options.visualize_path_style(&style);
216 }
217
218 self.find_path_options.into_js_options(|find_path_options| {
219 js_options.find_path_options(find_path_options);
220
221 callback(&js_options)
222 })
223 }
224}