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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use js_sys::{Array, JsString};
use wasm_bindgen::{prelude::*, JsCast};

use crate::{
    constants::{Direction, Part, ResourceType, ReturnCode},
    js_collections::JsCollectionFromValue,
    objects::{
        ConstructionSite, Owner, Resource, RoomObject, Store, Structure, StructureController,
    },
    pathfinder::SingleRoomCostResult,
    prelude::*,
    CostMatrix, MoveToOptions, RoomName, RoomPosition,
};

#[cfg(feature = "thorium")]
use crate::objects::Reactor;

#[wasm_bindgen]
extern "C" {
    /// A [`Creep`] unit in the game world.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep)
    #[wasm_bindgen(extends = RoomObject)]
    #[derive(Clone, Debug)]
    pub type Creep;

    #[wasm_bindgen(method, getter = body)]
    fn body_internal(this: &Creep) -> Array;

    /// The amount of fatigue the creep has. If greater than 0, it cannot move
    /// this tick without being pulled.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.fatigue)
    #[wasm_bindgen(method, getter)]
    pub fn fatigue(this: &Creep) -> u32;

    /// Retrieve the current hits of this creep.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.hits)
    #[wasm_bindgen(method, getter)]
    pub fn hits(this: &Creep) -> u32;

    /// Retrieve the maximum hits of this creep, which generally equals 50 per
    /// body part.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.hitsMax)
    #[wasm_bindgen(method, getter = hitsMax)]
    pub fn hits_max(this: &Creep) -> u32;

    /// Object ID of the creep, which can be used to efficiently fetch a fresh
    /// reference to the object on subsequent ticks.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.id)
    #[wasm_bindgen(method, getter = id)]
    fn id_internal(this: &Creep) -> Option<JsString>;

    /// A shortcut to `Memory.creeps[creep.name]`.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.memory)
    #[wasm_bindgen(method, getter)]
    pub fn memory(this: &Creep) -> JsValue;

    /// Sets a new value to `Memory.creeps[creep.name]`.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.memory)
    #[wasm_bindgen(method, setter)]
    pub fn set_memory(this: &Creep, val: &JsValue);

    /// Whether this creep is owned by the player.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.my)
    #[wasm_bindgen(method, getter)]
    pub fn my(this: &Creep) -> bool;

    #[wasm_bindgen(method, getter = name)]
    fn name_internal(this: &Creep) -> JsString;

    /// The [`Owner`] of this creep that contains the owner's username.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.owner)
    #[wasm_bindgen(method, getter)]
    pub fn owner(this: &Creep) -> Owner;

    /// What the creep said last tick.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.saying)
    #[wasm_bindgen(method, getter)]
    pub fn saying(this: &Creep) -> Option<JsString>;

    /// Whether the creep is still spawning.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.spawning)
    #[wasm_bindgen(method, getter)]
    pub fn spawning(this: &Creep) -> bool;

    /// The [`Store`] of the creep, which contains information about what
    /// resources it is it carrying.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.store)
    #[wasm_bindgen(final, method, getter)]
    pub fn store(this: &Creep) -> Store;

    /// The number of ticks the creep has left to live
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.ticksToLive)
    #[wasm_bindgen(method, getter = ticksToLive)]
    pub fn ticks_to_live(this: &Creep) -> Option<u32>;

    #[wasm_bindgen(final, method, js_name = attack)]
    fn attack_internal(this: &Creep, target: &RoomObject) -> ReturnCode;

    /// Attack a [`StructureController`] in melee range using a creep's claim
    /// parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.attackController)
    #[wasm_bindgen(final, method, js_name = attackController)]
    pub fn attack_controller(this: &Creep, target: &StructureController) -> ReturnCode;

    /// Use a creep's work parts to consume carried energy, putting it toward
    /// progress in a [`ConstructionSite`] in range 3.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.build)
    #[wasm_bindgen(final, method)]
    pub fn build(this: &Creep, target: &ConstructionSite) -> ReturnCode;

    /// Cancel an a successfully called creep function from earlier in the tick,
    /// with a [`JsString`] that must contain the JS version of the function
    /// name.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.cancelOrder)
    #[wasm_bindgen(final, method, js_name = cancelOrder)]
    pub fn cancel_order(this: &Creep, target: &JsString) -> ReturnCode;

    /// Claim an unowned [`StructureController`] in melee range as your own
    /// using a creep's claim parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.claimController)
    #[wasm_bindgen(final, method, js_name = claimController)]
    pub fn claim_controller(this: &Creep, target: &StructureController) -> ReturnCode;

    /// Claim a [`Reactor`] in melee range as your own using a creep's claim
    /// parts.
    ///
    /// [Screeps documentation](https://docs-season.screeps.com/api/#Creep.claimReactor)
    #[cfg(feature = "thorium")]
    #[wasm_bindgen(final, method, js_name = claimReactor)]
    pub fn claim_reactor(this: &Creep, target: &Reactor) -> ReturnCode;

    #[wasm_bindgen(final, method, js_name = dismantle)]
    fn dismantle_internal(this: &Creep, target: &Structure) -> ReturnCode;

    /// Drop a resource on the ground from the creep's [`Store`].
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.drop)
    #[wasm_bindgen(final, method)]
    pub fn drop(this: &Creep, ty: ResourceType, amount: Option<u32>) -> ReturnCode;

    /// Consume [`ResourceType::Ghodium`] (in the amount of [`SAFE_MODE_COST`])
    /// from the creep's [`Store`] to add a safe mode activation to a
    /// [`StructureController`] in melee range.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.generateSafeMode)
    ///
    /// [`SAFE_MODE_COST`]: crate::constants::SAFE_MODE_COST
    #[wasm_bindgen(final, method, js_name = generateSafeMode)]
    pub fn generate_safe_mode(this: &Creep, target: &StructureController) -> ReturnCode;

    /// Get the number of parts of the given type the creep has in its body,
    /// excluding fully damaged parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.getActiveBodyparts)
    #[wasm_bindgen(final, method, js_name = getActiveBodyparts)]
    pub fn get_active_bodyparts(this: &Creep, ty: Part) -> u8;

    #[wasm_bindgen(final, method, js_name = harvest)]
    fn harvest_internal(this: &Creep, target: &RoomObject) -> ReturnCode;

    #[wasm_bindgen(final, method, js_name = heal)]
    fn heal_internal(this: &Creep, target: &RoomObject) -> ReturnCode;

    /// Move one square in the specified direction.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.move)
    #[wasm_bindgen(final, method, js_name = move)]
    pub fn move_direction(this: &Creep, direction: Direction) -> ReturnCode;

    /// Accept an attempt by another creep to pull this one.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.move)
    #[wasm_bindgen(final, method, js_name = move)]
    pub fn move_pulled_by(this: &Creep, target: &Creep) -> ReturnCode;

    /// Move the creep along a previously determined path returned from a
    /// pathfinding function, in array or serialized string form.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.moveByPath)
    #[wasm_bindgen(final, method, js_name = moveByPath)]
    pub fn move_by_path(this: &Creep, path: &JsValue) -> ReturnCode;

    /// Move the creep toward the specified goal, either a [`RoomPosition`] or
    /// [`RoomObject`]. Note that using this function will store data in
    /// `Memory.creeps[creep_name]` and enable the default serialization
    /// behavior of the `Memory` object, which may hamper attempts to directly
    /// use `RawMemory`.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.moveByPath)
    #[wasm_bindgen(final, method, js_name = moveTo)]
    fn move_to_internal(this: &Creep, target: &JsValue, options: &JsValue) -> ReturnCode;

    /// Whether to send an email notification when this creep is attacked.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.notifyWhenAttacked)
    #[wasm_bindgen(final, method, js_name = notifyWhenAttacked)]
    pub fn notify_when_attacked(this: &Creep, enabled: bool) -> ReturnCode;

    /// Pick up a [`Resource`] in melee range (or at the same position as the
    /// creep).
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.pickup)
    #[wasm_bindgen(final, method)]
    pub fn pickup(this: &Creep, target: &Resource) -> ReturnCode;

    /// Help another creep to move by pulling, if the second creep accepts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.pull)
    #[wasm_bindgen(final, method)]
    pub fn pull(this: &Creep, target: &Creep) -> ReturnCode;

    #[wasm_bindgen(final, method, js_name = rangedAttack)]
    fn ranged_attack_internal(this: &Creep, target: &RoomObject) -> ReturnCode;

    #[wasm_bindgen(final, method, js_name = rangedHeal)]
    fn ranged_heal_internal(this: &Creep, target: &RoomObject) -> ReturnCode;

    /// Attack all enemy targets in range using a creep's ranged attack parts,
    /// with lower damage depending on range.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.rangedMassAttack)
    #[wasm_bindgen(final, method, js_name = rangedMassAttack)]
    pub fn ranged_mass_attack(this: &Creep) -> ReturnCode;

    /// Repair a target in range 3 using carried energy and the creep's work
    /// parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.repair)
    #[wasm_bindgen(final, method)]
    pub fn repair(this: &Creep, target: &RoomObject) -> ReturnCode;

    /// Reserve an unowned [`StructureController`] in melee range using a
    /// creep's claim parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.reserveController)
    #[wasm_bindgen(final, method, js_name = reserveController)]
    pub fn reserve_controller(this: &Creep, target: &StructureController) -> ReturnCode;

    /// Display a string in a bubble above the creep next tick. 10 character
    /// limit.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.say)
    #[wasm_bindgen(final, method)]
    pub fn say(this: &Creep, message: &str, public: bool) -> ReturnCode;

    /// Add (or remove, using an empty string) a sign to a
    /// [`StructureController`] in melee range.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.signController)
    #[wasm_bindgen(final, method, js_name = signController)]
    pub fn sign_controller(this: &Creep, target: &StructureController, text: &str) -> ReturnCode;

    /// Immediately kill the creep.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.suicide)
    #[wasm_bindgen(final, method)]
    pub fn suicide(this: &Creep) -> ReturnCode;

    /// Transfer a resource from the creep's store to [`Structure`],
    /// [`PowerCreep`], or another [`Creep`].
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.transfer)
    #[wasm_bindgen(final, method, js_name = transfer)]
    fn transfer_internal(
        this: &Creep,
        target: &RoomObject,
        ty: ResourceType,
        amount: Option<u32>,
    ) -> ReturnCode;

    /// Upgrade a [`StructureController`] in range 3 using carried energy and
    /// the creep's work parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.upgradeController)
    #[wasm_bindgen(final, method, js_name = upgradeController)]
    pub fn upgrade_controller(this: &Creep, target: &StructureController) -> ReturnCode;

    /// Withdraw a resource from a [`Structure`], [`Tombstone`], or [`Ruin`].
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.withdraw)
    #[wasm_bindgen(final, method, js_name = withdraw)]
    fn withdraw_internal(
        this: &Creep,
        target: &RoomObject,
        ty: ResourceType,
        amount: Option<u32>,
    ) -> ReturnCode;
}

#[wasm_bindgen]
extern "C" {
    /// A [`BodyPart`] of a creep.
    ///
    /// [Screeps documentation](https://docs-ptr.screeps.com/api/#Creep.body)
    #[wasm_bindgen]
    pub type BodyPart;

    #[wasm_bindgen(method, getter)]
    pub fn boost(this: &BodyPart) -> Option<ResourceType>;

    #[wasm_bindgen(method, getter = type)]
    pub fn part(this: &BodyPart) -> Part;

    #[wasm_bindgen(method, getter)]
    pub fn hits(this: &BodyPart) -> u32;
}

impl Creep {
    /// Retrieve a [`Vec<BodyPart>`] containing details about the creep's body
    /// parts and boosts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.body)
    pub fn body(&self) -> Vec<BodyPart> {
        self.body_internal().iter().map(BodyPart::from).collect()
    }

    /// Attack a target in melee range using a creep's attack parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.attack)
    pub fn attack<T>(&self, target: &T) -> ReturnCode
    where
        T: ?Sized + Attackable,
    {
        Self::attack_internal(self, target.as_ref())
    }

    /// Dismantle a [`Structure`] in melee range, removing [`DISMANTLE_POWER`]
    /// hits per effective work part, giving the creep energy equivalent to half
    /// of the cost to repair the same hits. Can only be used against types
    /// of structures that can be constructed; if
    /// [`StructureType::construction_cost`] is `None`, dismantling is
    /// impossible.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.dismantle)
    ///
    /// [`DISMANTLE_POWER`]: crate::constants::DISMANTLE_POWER
    /// [`StructureType::construction_cost`]: crate::constants::StructureType::construction_cost
    pub fn dismantle<T>(&self, target: &T) -> ReturnCode
    where
        T: ?Sized + Dismantleable,
    {
        Self::dismantle_internal(self, target.as_ref())
    }

    /// Harvest from a [`Source`], [`Mineral`], or [`Deposit`] in melee range.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.harvest)
    ///
    /// [`Source`]: crate::objects::Source
    /// [`Mineral`]: crate::objects::Mineral
    /// [`Deposit`]: crate::objects::Deposit
    pub fn harvest<T>(&self, target: &T) -> ReturnCode
    where
        T: ?Sized + Harvestable,
    {
        Self::harvest_internal(self, target.as_ref())
    }

    /// Heal a [`Creep`] or [`PowerCreep`] in melee range, including itself.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.heal)
    ///
    /// [`PowerCreep`]: crate::objects::PowerCreep
    pub fn heal<T>(&self, target: &T) -> ReturnCode
    where
        T: ?Sized + Healable,
    {
        Self::heal_internal(self, target.as_ref())
    }

    /// Attack a target in range 3 using a creep's ranged attack parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.rangedAttack)
    pub fn ranged_attack<T>(&self, target: &T) -> ReturnCode
    where
        T: ?Sized + Attackable,
    {
        Self::ranged_attack_internal(self, target.as_ref())
    }

    /// Heal a target in range 3 using a creep's heal parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.rangedHeal)
    pub fn ranged_heal<T>(&self, target: &T) -> ReturnCode
    where
        T: ?Sized + Healable,
    {
        Self::ranged_heal_internal(self, target.as_ref())
    }
}

impl JsCollectionFromValue for Creep {
    fn from_value(val: JsValue) -> Self {
        val.unchecked_into()
    }
}

impl HasHits for Creep {
    fn hits(&self) -> u32 {
        Self::hits(self)
    }

    fn hits_max(&self) -> u32 {
        Self::hits_max(self)
    }
}

impl MaybeHasNativeId for Creep {
    fn try_native_id(&self) -> Option<JsString> {
        Self::id_internal(self)
    }
}

impl HasStore for Creep {
    fn store(&self) -> Store {
        Self::store(self)
    }
}

impl SharedCreepProperties for Creep {
    fn memory(&self) -> JsValue {
        Self::memory(self)
    }

    fn set_memory(&self, val: &JsValue) {
        Self::set_memory(self, val)
    }

    fn my(&self) -> bool {
        Self::my(self)
    }

    fn name(&self) -> String {
        Self::name_internal(self).into()
    }

    fn owner(&self) -> Owner {
        Self::owner(self)
    }

    fn saying(&self) -> Option<JsString> {
        Self::saying(self)
    }

    fn ticks_to_live(&self) -> Option<u32> {
        Self::ticks_to_live(self)
    }

    fn cancel_order(&self, target: &JsString) -> ReturnCode {
        Self::cancel_order(self, target)
    }

    fn drop(&self, ty: ResourceType, amount: Option<u32>) -> ReturnCode {
        Self::drop(self, ty, amount)
    }

    fn move_direction(&self, direction: Direction) -> ReturnCode {
        Self::move_direction(self, direction)
    }

    fn move_by_path(&self, path: &JsValue) -> ReturnCode {
        Self::move_by_path(self, path)
    }

    fn move_to<T>(&self, target: T) -> ReturnCode
    where
        T: HasPosition,
    {
        let target: RoomPosition = target.pos().into();
        Self::move_to_internal(self, &target, &JsValue::UNDEFINED)
    }

    fn move_to_with_options<T, F>(&self, target: T, options: Option<MoveToOptions<F>>) -> ReturnCode
    where
        T: HasPosition,
        F: FnMut(RoomName, CostMatrix) -> SingleRoomCostResult,
    {
        let target: RoomPosition = target.pos().into();

        if let Some(options) = options {
            options.into_js_options(|js_options| Self::move_to_internal(self, &target, js_options))
        } else {
            Self::move_to_internal(self, &target, &JsValue::UNDEFINED)
        }
    }

    fn notify_when_attacked(&self, enabled: bool) -> ReturnCode {
        Self::notify_when_attacked(self, enabled)
    }

    fn pickup(&self, target: &Resource) -> ReturnCode {
        Self::pickup(self, target)
    }

    fn say(&self, message: &str, public: bool) -> ReturnCode {
        Self::say(self, message, public)
    }

    fn suicide(&self) -> ReturnCode {
        Self::suicide(self)
    }

    fn transfer<T>(&self, target: &T, ty: ResourceType, amount: Option<u32>) -> ReturnCode
    where
        T: Transferable,
    {
        Self::transfer_internal(self, target.as_ref(), ty, amount)
    }

    fn withdraw<T>(&self, target: &T, ty: ResourceType, amount: Option<u32>) -> ReturnCode
    where
        T: Withdrawable,
    {
        Self::withdraw_internal(self, target.as_ref(), ty, amount)
    }
}