1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use std::marker::PhantomData;

use std::{mem, f64};

use stdweb::{Array, Object, Reference, UnsafeTypedArray, unstable::TryInto, web::TypedArray};

use {HasPosition, LocalRoomPosition, RoomPosition};

#[derive(Clone, Debug)]
pub struct LocalCostMatrix {
    /// Length should be 2500.
    bits: Vec<u8>,
}

#[inline(always)]
fn pos_as_idx(x: u8, y: u8) -> usize {
    (x as usize) * 50 + (y as usize)
}

impl LocalCostMatrix {
    pub fn new() -> Self {
        LocalCostMatrix {
            bits: vec![0; 2500],
        }
    }

    pub fn set(&mut self, x: u8, y: u8, val: u8) {
        self.bits[pos_as_idx(x, y)] = val;
    }

    pub fn get(&self, x: u8, y: u8) -> u8 {
        self.bits[pos_as_idx(x, y)]
    }

    /// Copies all data into an JavaScript CostMatrix for use.
    ///
    /// This is slower than [`as_uploaded`], but much safer.
    ///
    /// [`as_uploaded`]: #method.as_uploaded
    pub fn upload(&self) -> CostMatrix<'static> {
        let bits: TypedArray<u8> = self.bits[..].into();

        CostMatrix {
            inner: (js! {
                var matrix = Object.create(CostMatrix.prototype);
                matrix._bits = @{bits};
                return matrix;
            }).try_into()
                .expect("expected function returning CostMatrix to return a Reference"),
            lifetime: PhantomData,
        }
    }

    /// Temporarily exposes the bits of this matrix as a cost matrix.
    ///
    /// # Unsafety
    ///
    /// There are two main invariants you must uphold after using this function:
    ///
    /// 1. The `CostMatrix` can only be used in JS code as long as this `LocalCostMatrix` is alive.
    ///    Doing otherwise will result in undefined behavior, mainly JS being allowed to read/
    ///    manipulate uninitialized rust memory or rust memory that's been repurposed.
    ///
    /// 2. The `set` method of the cost matrix must not be used - it must be read only. This takes
    ///    &self, but technically allows mutation of the inner Vec via JavaScript access. You
    ///    should not use this method, or you will invoke Rust undefined behavior.
    ///
    /// The CostMatrix returned will _reference the internal data of this `LocalCostMatrix`_.
    pub unsafe fn as_uploaded<'a>(&'a self) -> CostMatrix<'a> {
        let bits: UnsafeTypedArray<u8> = UnsafeTypedArray::new(&self.bits);

        CostMatrix {
            inner: (js! {
                // using this first is necessary in order to uphold the invariant of
                // `UnsafeTypedArray`.
                var bits = @{bits};

                var matrix = Object.create(CostMatrix.prototype);
                matrix._bits = bits;

                return matrix;
            }).try_into()
                .expect("expected function returning CostMatrix to return a Reference"),
            lifetime: PhantomData,
        }
    }
}

impl Into<Vec<u8>> for LocalCostMatrix {
    /// Returns a vector of bits length 2500, where each position is
    /// `idx = ((x * 50) + y)`.
    fn into(self) -> Vec<u8> {
        self.bits
    }
}

/// A `CostMatrix` that's valid to pass as a result from a `PathFinder.search` room callback.
///
/// Lives as long as `'a` lifetime. It's unsound to leak to JS past this lifetime if this matrix
/// was created by [`LocalCostMatrix::as_uploaded`].
///
/// [`LocalCostMatrix::as_uploaded`]: struct.LocalCostMatrix.html#method.as_uploaded
pub struct CostMatrix<'a> {
    pub(crate) inner: Reference,
    pub(crate) lifetime: PhantomData<&'a ()>,
}

impl Default for CostMatrix<'static> {
    fn default() -> Self {
        CostMatrix {
            inner: js_unwrap!(new PathFinder.CostMatrix()),
            lifetime: PhantomData,
        }
    }
}

// need custom implementation in order to ensure length of 'bits' is always 2500
mod serde_impls {
    use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error as DeError};

    use super::LocalCostMatrix;

    impl Serialize for LocalCostMatrix {
        fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            self.bits.serialize(s)
        }
    }

    impl<'de> Deserialize<'de> for LocalCostMatrix {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            let bits: Vec<u8> = Vec::deserialize(deserializer)?;

            if bits.len() != 2500 {
                return Err(DeError::invalid_length(bits.len(), &"a vec of length 2500"));
            }

            Ok(LocalCostMatrix { bits })
        }
    }
}

pub struct SearchOptions<'a, F>
where
    F: Fn(String) -> CostMatrix<'a>,
{
    room_callback: F,
    plain_cost: u8,
    swamp_cost: u8,
    flee: bool,
    max_ops: i32,
    max_rooms: i32,
    max_cost: f64,
    heuristic_weight: f64,
}

impl Default for SearchOptions<'static, fn(String) -> CostMatrix<'static>> {
    fn default() -> Self {
        fn cost_matrix(_: String) -> CostMatrix<'static> {
            CostMatrix::default()
        }

        // TODO: should we fall back onto the game's default values, or is
        // it alright to copy them here?
        SearchOptions {
            room_callback: cost_matrix,
            plain_cost: 1,
            swamp_cost: 5,
            flee: false,
            max_ops: 2000,
            max_rooms: 16,
            max_cost: f64::INFINITY,
            heuristic_weight: 1.2,
        }
    }
}

impl SearchOptions<'static, fn(String) -> CostMatrix<'static>> {
    /// Creates default SearchOptions
    pub fn new() -> Self {
        Self::default()
    }
}

impl<'a, F> SearchOptions<'a, F>
where
    F: Fn(String) -> CostMatrix<'a>,
{
    /// Sets room callback - default `|_| { CostMatrix::default() }`.
    pub fn room_callback<'b, F2>(self, room_callback: F2) -> SearchOptions<'b, F2>
    where
        F2: Fn(String) -> CostMatrix<'b>,
    {
        let SearchOptions {
            room_callback: _,
            plain_cost,
            swamp_cost,
            flee,
            max_ops,
            max_rooms,
            max_cost,
            heuristic_weight,
        } = self;
        SearchOptions {
            room_callback,
            plain_cost,
            swamp_cost,
            flee,
            max_ops,
            max_rooms,
            max_cost,
            heuristic_weight,
        }
    }

    /// Sets plain cost - default `1`.
    pub fn plain_cost(mut self, cost: u8) -> Self {
        self.plain_cost = cost;
        self
    }

    /// Sets swamp cost - default `5`.
    pub fn swamp_cost(mut self, cost: u8) -> Self {
        self.swamp_cost = cost;
        self
    }

    /// Sets whether this is a flee search - default `false`.
    pub fn flee(mut self, flee: bool) -> Self {
        self.flee = flee;
        self
    }

    /// Sets maximum ops - default `2000`.
    pub fn max_ops(mut self, ops: i32) -> Self {
        self.max_ops = ops;
        self
    }

    /// Sets maximum rooms - default `16`, max `16`.
    pub fn max_rooms(mut self, rooms: i32) -> Self {
        self.max_rooms = rooms;
        self
    }

    /// Sets maximum path cost - default `f64::Infinity`.
    pub fn max_cost(mut self, cost: f64) -> Self {
        self.max_cost = cost;
        self
    }

    /// Sets heuristic weight - default `1.2`.
    pub fn heuristic_weight(mut self, weight: f64) -> Self {
        self.heuristic_weight = weight;
        self
    }
}

pub struct SearchResults {
    path: Array,
    pub ops: i32,
    pub cost: i32,
    pub incomplete: bool,
}

impl SearchResults {
    pub fn opaque_path(&self) -> &Array {
        &self.path
    }
    pub fn load_local_path(&self) -> Vec<LocalRoomPosition> {
        self.path
            .clone()
            .try_into()
            .expect("expected PathFinder.search path result to be an array of RoomPositions")
    }
    pub fn load_semi_local_path(&self) -> Vec<RoomPosition> {
        self.path
            .clone()
            .try_into()
            .expect("expected PathFinder.search path result to be an array of RoomPositions")
    }
}

/// Searches between a single origin and single goal.
pub fn search<'a, O, G, F>(
    origin: &O,
    goal: &G,
    range: i32,
    opts: SearchOptions<'a, F>,
) -> SearchResults
where
    O: HasPosition,
    G: HasPosition,
    F: Fn(String) -> CostMatrix<'a> + 'a,
{
    let pos = goal.pos();
    search_real(
        &origin.pos(),
        &js_unwrap!({pos: @{pos.as_ref()}, range: @{range}}),
        opts,
    )
}

/// Searches between a single origin and multiple goals.
pub fn search_many<'a, O, G, I, F>(origin: &O, goal: G, opts: SearchOptions<'a, F>) -> SearchResults
where
    O: HasPosition,
    G: IntoIterator<Item = (I, i32)>,
    I: HasPosition,
    F: Fn(String) -> CostMatrix<'a> + 'a,
{
    let goals: Vec<Object> = goal.into_iter()
        .map(|(target, range)| {
            let pos = target.pos();
            js_unwrap!({pos: @{pos.as_ref()}, range: @{range}})
        })
        .collect();
    let goals_js: Reference = js_unwrap!(@{goals});
    search_real(&origin.pos(), &goals_js, opts)
}

scoped_thread_local!(static PF_CALLBACK: Box<(Fn(String) -> Reference)>);

fn search_real<'a, F>(
    origin: &RoomPosition,
    goal: &Reference,
    opts: SearchOptions<'a, F>,
) -> SearchResults
where
    F: Fn(String) -> CostMatrix<'a> + 'a,
{
    // TODO: should we just accept `fn()` and force the user
    // to do synchronization? it would... greatly simplify all of this.
    fn callback(input: String) -> Reference {
        PF_CALLBACK.with(|callback| callback(input))
    }

    let raw_callback = opts.room_callback;

    let callback_unboxed = move |input| raw_callback(input).inner;
    let callback_type_erased: Box<Fn(String) -> Reference + 'a> = Box::new(callback_unboxed);
    let callback_lifetime_erased: Box<Fn(String) -> Reference + 'static> =
        unsafe { mem::transmute(callback_type_erased) };

    let SearchOptions {
        plain_cost,
        swamp_cost,
        flee,
        max_ops,
        max_rooms,
        heuristic_weight,
        ..
    } = opts;

    PF_CALLBACK.set(&callback_lifetime_erased, || {
        let res: ::stdweb::Reference = js_unwrap!{
            PathFinder.search(@{origin.as_ref()}, @{goal}, {
                roomCallback: @{callback},
                plainCost: @{plain_cost},
                swampCost: @{swamp_cost},
                flee: @{flee},
                maxOps: @{max_ops},
                maxRooms: @{max_rooms},
                heuristicWeight: @{heuristic_weight}
            })
        };

        SearchResults {
            path: js_unwrap!(@{&res}.path),
            ops: js_unwrap!(@{&res}.ops),
            cost: js_unwrap!(@{&res}.cost),
            incomplete: js_unwrap!(@{&res}.incomplete),
        }
    })
}