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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
use js_sys::{Array, JsString};
use wasm_bindgen::{prelude::*, JsCast};

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

#[cfg(feature = "seasonal-season-5")]
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;

    // explicitly structural getters here, as these may have varying
    // getter methods (notably on the first tick a creep is spawning):
    // https://github.com/screeps/engine/blob/c6c4fc9e656f160e0e0174b0dd9a817d2dd18976/src/game/structures.js#L1134-L1213
    #[wasm_bindgen(structural, method, getter = body)]
    fn body_internal(this: &Creep) -> Array;

    #[wasm_bindgen(structural, method, getter = fatigue)]
    fn fatigue_internal(this: &Creep) -> u32;

    #[wasm_bindgen(structural, method, getter = hits)]
    fn hits_internal(this: &Creep) -> u32;

    #[wasm_bindgen(structural, method, getter = hitsMax)]
    fn hits_max_internal(this: &Creep) -> u32;

    #[wasm_bindgen(structural, method, getter = id)]
    fn id_internal(this: &Creep) -> Option<JsString>;

    #[wasm_bindgen(structural, method, getter = memory)]
    fn memory_internal(this: &Creep) -> JsValue;

    #[wasm_bindgen(structural, method, setter = memory)]
    fn set_memory_internal(this: &Creep, val: &JsValue);

    #[wasm_bindgen(structural, method, getter = my)]
    fn my_internal(this: &Creep) -> bool;

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

    #[wasm_bindgen(structural, method, getter = owner)]
    fn owner_internal(this: &Creep) -> Owner;

    #[wasm_bindgen(structural, method, getter = saying)]
    fn saying_internal(this: &Creep) -> Option<JsString>;

    #[wasm_bindgen(structural, method, getter = spawning)]
    fn spawning_internal(this: &Creep) -> bool;

    #[wasm_bindgen(structural, method, getter = store)]
    fn store_internal(this: &Creep) -> Store;

    #[wasm_bindgen(structural, method, getter = ticksToLive)]
    fn ticks_to_live_internal(this: &Creep) -> Option<u32>;

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

    #[wasm_bindgen(final, method, js_name = attackController)]
    fn attack_controller_internal(this: &Creep, target: &StructureController) -> i8;

    #[wasm_bindgen(final, method, js_name = build)]
    fn build_internal(this: &Creep, target: &ConstructionSite) -> i8;

    #[wasm_bindgen(final, method, js_name = cancelOrder)]
    fn cancel_order_internal(this: &Creep, target: &JsString) -> i8;

    #[wasm_bindgen(final, method, js_name = claimController)]
    fn claim_controller_internal(this: &Creep, target: &StructureController) -> i8;

    #[cfg(feature = "seasonal-season-5")]
    #[wasm_bindgen(final, method, js_name = claimReactor)]
    fn claim_reactor_internal(this: &Creep, target: &Reactor) -> i8;

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

    #[wasm_bindgen(final, method, js_name = drop)]
    fn drop_internal(this: &Creep, ty: ResourceType, amount: Option<u32>) -> i8;

    #[wasm_bindgen(final, method, js_name = generateSafeMode)]
    fn generate_safe_mode_internal(this: &Creep, target: &StructureController) -> i8;

    #[wasm_bindgen(final, method, js_name = getActiveBodyparts)]
    fn get_active_bodyparts_internal(this: &Creep, ty: Part) -> u8;

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

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

    #[wasm_bindgen(final, method, js_name = move)]
    fn move_direction_internal(this: &Creep, direction: Direction) -> i8;

    #[wasm_bindgen(final, method, js_name = move)]
    fn move_pulled_by_internal(this: &Creep, target: &Creep) -> i8;

    #[wasm_bindgen(final, method, js_name = moveByPath)]
    fn move_by_path_internal(this: &Creep, path: &JsValue) -> i8;

    #[wasm_bindgen(final, method, js_name = moveTo)]
    fn move_to_internal(this: &Creep, target: &JsValue, options: &JsValue) -> i8;

    #[wasm_bindgen(final, method, js_name = notifyWhenAttacked)]
    fn notify_when_attacked_internal(this: &Creep, enabled: bool) -> i8;

    #[wasm_bindgen(final, method, js_name = pickup)]
    fn pickup_internal(this: &Creep, target: &Resource) -> i8;

    #[wasm_bindgen(final, method, js_name = pull)]
    fn pull_internal(this: &Creep, target: &Creep) -> i8;

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

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

    #[wasm_bindgen(final, method, js_name = rangedMassAttack)]
    fn ranged_mass_attack_internal(this: &Creep) -> i8;

    #[wasm_bindgen(final, method, js_name = repair)]
    fn repair_internal(this: &Creep, target: &RoomObject) -> i8;

    #[wasm_bindgen(final, method, js_name = reserveController)]
    fn reserve_controller_internal(this: &Creep, target: &StructureController) -> i8;

    #[wasm_bindgen(final, method, js_name = say)]
    fn say_internal(this: &Creep, message: &str, public: bool) -> i8;

    #[wasm_bindgen(final, method, js_name = signController)]
    fn sign_controller_internal(this: &Creep, target: &StructureController, text: &str) -> i8;

    #[wasm_bindgen(final, method, js_name = suicide)]
    fn suicide_internal(this: &Creep) -> i8;

    #[wasm_bindgen(final, method, js_name = transfer)]
    fn transfer_internal(
        this: &Creep,
        target: &RoomObject,
        ty: ResourceType,
        amount: Option<u32>,
    ) -> i8;

    #[wasm_bindgen(final, method, js_name = upgradeController)]
    fn upgrade_controller_internal(this: &Creep, target: &StructureController) -> i8;

    #[wasm_bindgen(final, method, js_name = withdraw)]
    fn withdraw_internal(
        this: &Creep,
        target: &RoomObject,
        ty: ResourceType,
        amount: Option<u32>,
    ) -> i8;
}

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()
    }

    /// 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)
    pub fn fatigue(&self) -> u32 {
        self.fatigue_internal()
    }

    /// Retrieve the current hits of this creep.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.hits)
    pub fn hits(&self) -> u32 {
        self.hits_internal()
    }

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

    /// A shortcut to `Memory.creeps[creep.name]`.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.memory)
    pub fn memory(&self) -> JsValue {
        self.memory_internal()
    }

    /// Sets a new value to `Memory.creeps[creep.name]`.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.memory)
    pub fn set_memory(&self, val: &JsValue) {
        self.set_memory_internal(val)
    }

    /// Whether this creep is owned by the player.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.my)
    pub fn my(&self) -> bool {
        self.my_internal()
    }

    /// The [`Owner`] of this creep that contains the owner's username.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.owner)
    pub fn owner(&self) -> Owner {
        self.owner_internal()
    }

    /// What the creep said last tick.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.saying)
    pub fn saying(&self) -> Option<JsString> {
        self.saying_internal()
    }

    /// Whether the creep is still spawning.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.spawning)
    pub fn spawning(&self) -> bool {
        self.spawning_internal()
    }

    /// The [`Store`] of the creep, which contains information about what
    /// resources it is it carrying.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.store)
    pub fn store(&self) -> Store {
        self.store_internal()
    }

    /// The number of ticks the creep has left to live
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.ticksToLive)
    pub fn ticks_to_live(&self) -> Option<u32> {
        self.ticks_to_live_internal()
    }

    /// 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) -> Result<(), ErrorCode>
    where
        T: ?Sized + Attackable,
    {
        ErrorCode::result_from_i8(self.attack_internal(target.as_ref()))
    }

    /// Attack a [`StructureController`] in melee range using a creep's claim
    /// parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.attackController)
    pub fn attack_controller(&self, target: &StructureController) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.attack_controller_internal(target))
    }

    /// 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)
    pub fn build(&self, target: &ConstructionSite) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.build_internal(target))
    }

    /// 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)
    pub fn cancel_order(&self, target: &JsString) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.cancel_order_internal(target))
    }

    /// 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)
    pub fn claim_controller(&self, target: &StructureController) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.claim_controller_internal(target))
    }

    /// 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 = "seasonal-season-5")]
    pub fn claim_reactor(&self, target: &Reactor) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.claim_reactor_internal(target))
    }

    /// 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) -> Result<(), ErrorCode>
    where
        T: ?Sized + Dismantleable,
    {
        ErrorCode::result_from_i8(self.dismantle_internal(target.as_ref()))
    }

    /// Drop a resource on the ground from the creep's [`Store`].
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.drop)
    pub fn drop(&self, ty: ResourceType, amount: Option<u32>) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.drop_internal(ty, amount))
    }

    /// 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
    pub fn generate_safe_mode(&self, target: &StructureController) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.generate_safe_mode_internal(target))
    }

    /// 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)
    pub fn get_active_bodyparts(&self, ty: Part) -> u8 {
        self.get_active_bodyparts_internal(ty)
    }

    /// 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) -> Result<(), ErrorCode>
    where
        T: ?Sized + Harvestable,
    {
        ErrorCode::result_from_i8(self.harvest_internal(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) -> Result<(), ErrorCode>
    where
        T: ?Sized + Healable,
    {
        ErrorCode::result_from_i8(self.heal_internal(target.as_ref()))
    }

    /// Move one square in the specified direction.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.move)
    pub fn move_direction(&self, direction: Direction) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.move_direction_internal(direction))
    }

    /// Accept an attempt by another creep to pull this one.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.move)
    pub fn move_pulled_by(&self, target: &Creep) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.move_pulled_by_internal(target))
    }

    /// 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)
    pub fn move_by_path(&self, path: &JsValue) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.move_by_path_internal(path))
    }

    /// Whether to send an email notification when this creep is attacked.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.notifyWhenAttacked)
    pub fn notify_when_attacked(&self, enabled: bool) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.notify_when_attacked_internal(enabled))
    }

    /// Pick up a [`Resource`] in melee range (or at the same position as the
    /// creep).
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.pickup)
    pub fn pickup(&self, target: &Resource) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.pickup_internal(target))
    }

    /// Help another creep to move by pulling, if the second creep accepts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.pull)
    pub fn pull(&self, target: &Creep) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.pull_internal(target))
    }

    /// 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) -> Result<(), ErrorCode>
    where
        T: ?Sized + Attackable,
    {
        ErrorCode::result_from_i8(self.ranged_attack_internal(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) -> Result<(), ErrorCode>
    where
        T: ?Sized + Healable,
    {
        ErrorCode::result_from_i8(self.ranged_heal_internal(target.as_ref()))
    }

    /// 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)
    pub fn ranged_mass_attack(&self) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.ranged_mass_attack_internal())
    }

    /// Repair a target in range 3 using carried energy and the creep's work
    /// parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.repair)
    pub fn repair<T>(&self, target: &T) -> Result<(), ErrorCode>
    where
        T: ?Sized + Repairable,
    {
        ErrorCode::result_from_i8(self.repair_internal(target.as_ref()))
    }

    /// Reserve an unowned [`StructureController`] in melee range using a
    /// creep's claim parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.reserveController)
    pub fn reserve_controller(&self, target: &StructureController) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.reserve_controller_internal(target))
    }

    /// Display a string in a bubble above the creep next tick. 10 character
    /// limit.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.say)
    pub fn say(&self, message: &str, public: bool) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.say_internal(message, public))
    }

    /// Add (or remove, using an empty string) a sign to a
    /// [`StructureController`] in melee range.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.signController)
    pub fn sign_controller(
        &self,
        target: &StructureController,
        text: &str,
    ) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.sign_controller_internal(target, text))
    }

    /// Immediately kill the creep.
    ///
    /// Actions taken by the creep earlier in the tick may be cancelled.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.suicide)
    pub fn suicide(&self) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.suicide_internal())
    }

    /// Upgrade a [`StructureController`] in range 3 using carried energy and
    /// the creep's work parts.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.upgradeController)
    pub fn upgrade_controller(&self, target: &StructureController) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.upgrade_controller_internal(target))
    }
}

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

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

    fn hits_max(&self) -> u32 {
        self.hits_max()
    }
}

impl MaybeHasId for Creep {
    /// The Object ID of the [`Creep`], or `None` if it began spawning this
    /// tick.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Creep.id)
    fn try_js_raw_id(&self) -> Option<JsString> {
        self.id_internal()
    }
}

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

impl Attackable for Creep {}
impl Healable for Creep {}
impl Transferable for Creep {}

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

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

    fn my(&self) -> bool {
        self.my()
    }

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

    fn owner(&self) -> Owner {
        self.owner()
    }

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

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

    fn cancel_order(&self, target: &JsString) -> Result<(), ErrorCode> {
        self.cancel_order(target)
    }

    fn drop(&self, ty: ResourceType, amount: Option<u32>) -> Result<(), ErrorCode> {
        self.drop(ty, amount)
    }

    fn move_direction(&self, direction: Direction) -> Result<(), ErrorCode> {
        self.move_direction(direction)
    }

    fn move_by_path(&self, path: &JsValue) -> Result<(), ErrorCode> {
        self.move_by_path(path)
    }

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

    fn move_to_with_options<T, F>(
        &self,
        target: T,
        options: Option<MoveToOptions<F>>,
    ) -> Result<(), ErrorCode>
    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| {
                ErrorCode::result_from_i8(self.move_to_internal(&target, js_options))
            })
        } else {
            ErrorCode::result_from_i8(self.move_to_internal(&target, &JsValue::UNDEFINED))
        }
    }

    fn notify_when_attacked(&self, enabled: bool) -> Result<(), ErrorCode> {
        self.notify_when_attacked(enabled)
    }

    fn pickup(&self, target: &Resource) -> Result<(), ErrorCode> {
        self.pickup(target)
    }

    fn say(&self, message: &str, public: bool) -> Result<(), ErrorCode> {
        self.say(message, public)
    }

    fn suicide(&self) -> Result<(), ErrorCode> {
        self.suicide()
    }

    fn transfer<T>(
        &self,
        target: &T,
        ty: ResourceType,
        amount: Option<u32>,
    ) -> Result<(), ErrorCode>
    where
        T: Transferable + ?Sized,
    {
        ErrorCode::result_from_i8(self.transfer_internal(target.as_ref(), ty, amount))
    }

    fn withdraw<T>(
        &self,
        target: &T,
        ty: ResourceType,
        amount: Option<u32>,
    ) -> Result<(), ErrorCode>
    where
        T: Withdrawable + ?Sized,
    {
        ErrorCode::result_from_i8(self.withdraw_internal(target.as_ref(), ty, amount))
    }
}

#[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;
}