screeps-game-api 0.23.3

WASM bindings to the in-game Screeps API
Documentation
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
use std::{error::Error, fmt};

use num_derive::FromPrimitive;
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::{constants::ErrorCode, FromReturnCode};

/// Error codes used by
/// [SharedCreepProperties::drop](crate::SharedCreepProperties::drop).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.drop).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L404)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum DropErrorCode {
    NotOwner = -1,
    Busy = -4,
    NotEnoughResources = -6,
    InvalidArgs = -10,
}

impl FromReturnCode for DropErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(DropErrorCode::NotOwner)),
            -4 => Some(Err(DropErrorCode::Busy)),
            -6 => Some(Err(DropErrorCode::NotEnoughResources)),
            -10 => Some(Err(DropErrorCode::InvalidArgs)),
            _ => None,
        }
    }
}

impl fmt::Display for DropErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            DropErrorCode::NotOwner => "you are not the owner of this creep",
            DropErrorCode::Busy => "the creep is still being spawned",
            DropErrorCode::NotEnoughResources => {
                "the creep does not have the given amount of resources"
            }
            DropErrorCode::InvalidArgs => "the resourcetype is not a valid resource_* constants",
        };

        write!(f, "{}", msg)
    }
}

impl Error for DropErrorCode {}

impl From<DropErrorCode> for ErrorCode {
    fn from(value: DropErrorCode) -> Self {
        // Safety: DropErrorCode is repr(i8), so we can cast it to get the discriminant
        // value, which will match the raw return code value that ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
        // Safety: DropErrorCode discriminants are always error code values, and thus
        // the Result returned here will always be an `Err` variant, so we can always
        // extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [SharedCreepProperties::notify_when_attacked](crate::SharedCreepProperties::notify_when_attacked).
///
///
/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.notifyWhenAttacked).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L988)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum NotifyWhenAttackedErrorCode {
    NotOwner = -1,
    Busy = -4,
    InvalidArgs = -10,
}

impl FromReturnCode for NotifyWhenAttackedErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(NotifyWhenAttackedErrorCode::NotOwner)),
            -4 => Some(Err(NotifyWhenAttackedErrorCode::Busy)),
            -10 => Some(Err(NotifyWhenAttackedErrorCode::InvalidArgs)),
            _ => None,
        }
    }
}

impl fmt::Display for NotifyWhenAttackedErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            NotifyWhenAttackedErrorCode::NotOwner => "you are not the owner of this creep",
            NotifyWhenAttackedErrorCode::Busy => "the creep is still being spawned",
            NotifyWhenAttackedErrorCode::InvalidArgs => "enable argument is not a boolean value",
        };

        write!(f, "{}", msg)
    }
}

impl Error for NotifyWhenAttackedErrorCode {}

impl From<NotifyWhenAttackedErrorCode> for ErrorCode {
    fn from(value: NotifyWhenAttackedErrorCode) -> Self {
        // Safety: NotifyWhenAttackedErrorCode is repr(i8), so we can cast it to get the
        // discriminant value, which will match the raw return code value that ErrorCode
        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
        // Safety: NotifyWhenAttackedErrorCode discriminants are always error code
        // values, and thus the Result returned here will always be an `Err` variant, so
        // we can always extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [SharedCreepProperties::pickup](crate::SharedCreepProperties::pickup).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.pickup).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L566)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum PickupErrorCode {
    NotOwner = -1,
    Busy = -4,
    InvalidTarget = -7,
    Full = -8,
    NotInRange = -9,
}

impl FromReturnCode for PickupErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(PickupErrorCode::NotOwner)),
            -4 => Some(Err(PickupErrorCode::Busy)),
            -7 => Some(Err(PickupErrorCode::InvalidTarget)),
            -8 => Some(Err(PickupErrorCode::Full)),
            -9 => Some(Err(PickupErrorCode::NotInRange)),
            _ => None,
        }
    }
}

impl fmt::Display for PickupErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            PickupErrorCode::NotOwner => "you are not the owner of this creep",
            PickupErrorCode::Busy => "the creep is still being spawned",
            PickupErrorCode::InvalidTarget => "the target is not a valid object to pick up",
            PickupErrorCode::Full => "the creep cannot receive any more resource",
            PickupErrorCode::NotInRange => "the target is too far away",
        };

        write!(f, "{}", msg)
    }
}

impl Error for PickupErrorCode {}

impl From<PickupErrorCode> for ErrorCode {
    fn from(value: PickupErrorCode) -> Self {
        // Safety: PickupErrorCode is repr(i8), so we can cast it to get the
        // discriminant value, which will match the raw return code value that ErrorCode
        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
        // Safety: PickupErrorCode discriminants are always error code values, and thus
        // the Result returned here will always be an `Err` variant, so we can always
        // extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [SharedCreepProperties::say](crate::SharedCreepProperties::say).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.say).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L826)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum SayErrorCode {
    NotOwner = -1,
    Busy = -4,
}

impl FromReturnCode for SayErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(SayErrorCode::NotOwner)),
            -4 => Some(Err(SayErrorCode::Busy)),
            _ => None,
        }
    }
}

impl fmt::Display for SayErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            SayErrorCode::NotOwner => "you are not the owner of this creep",
            SayErrorCode::Busy => "the creep is still being spawned",
        };

        write!(f, "{}", msg)
    }
}

impl Error for SayErrorCode {}

impl From<SayErrorCode> for ErrorCode {
    fn from(value: SayErrorCode) -> Self {
        // Safety: SayErrorCode is repr(i8), so we can cast it to get the discriminant
        // value, which will match the raw return code value that ErrorCode expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
        // Safety: SayErrorCode discriminants are always error code values, and thus the
        // Result returned here will always be an `Err` variant, so we can always
        // extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [SharedCreepProperties::suicide](crate::SharedCreepProperties::suicide).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.suicide).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L813)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum SuicideErrorCode {
    NotOwner = -1,
    Busy = -4,
}

impl FromReturnCode for SuicideErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(SuicideErrorCode::NotOwner)),
            -4 => Some(Err(SuicideErrorCode::Busy)),
            _ => None,
        }
    }
}

impl fmt::Display for SuicideErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            SuicideErrorCode::NotOwner => "you are not the owner of this creep",
            SuicideErrorCode::Busy => "the creep is still being spawned",
        };

        write!(f, "{}", msg)
    }
}

impl Error for SuicideErrorCode {}

impl From<SuicideErrorCode> for ErrorCode {
    fn from(value: SuicideErrorCode) -> Self {
        // Safety: SuicideErrorCode is repr(i8), so we can cast it to get the
        // discriminant value, which will match the raw return code value that ErrorCode
        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
        // Safety: SuicideErrorCode discriminants are always error code values, and thus
        // the Result returned here will always be an `Err` variant, so we can always
        // extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [SharedCreepProperties::transfer](crate::SharedCreepProperties::transfer).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.transfer).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L428)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum TransferErrorCode {
    NotOwner = -1,
    Busy = -4,
    NotEnoughResources = -6,
    InvalidTarget = -7,
    Full = -8,
    NotInRange = -9,
    InvalidArgs = -10,
}

impl FromReturnCode for TransferErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(TransferErrorCode::NotOwner)),
            -4 => Some(Err(TransferErrorCode::Busy)),
            -6 => Some(Err(TransferErrorCode::NotEnoughResources)),
            -7 => Some(Err(TransferErrorCode::InvalidTarget)),
            -8 => Some(Err(TransferErrorCode::Full)),
            -9 => Some(Err(TransferErrorCode::NotInRange)),
            -10 => Some(Err(TransferErrorCode::InvalidArgs)),
            _ => None,
        }
    }
}

impl fmt::Display for TransferErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            TransferErrorCode::NotOwner => "you are not the owner of this creep",
            TransferErrorCode::Busy => "the creep is still being spawned",
            TransferErrorCode::NotEnoughResources => "the creep does not have the given amount of resources",
            TransferErrorCode::InvalidTarget => "the target is not a valid object which can contain the specified resource",
            TransferErrorCode::Full => "the target cannot receive any more resources",
            TransferErrorCode::NotInRange => "the target is too far away",
            TransferErrorCode::InvalidArgs => "the resourcetype is not one of the resource_* constants, or the amount is incorrect",
        };

        write!(f, "{}", msg)
    }
}

impl Error for TransferErrorCode {}

impl From<TransferErrorCode> for ErrorCode {
    fn from(value: TransferErrorCode) -> Self {
        // Safety: TransferErrorCode is repr(i8), so we can cast it to get the
        // discriminant value, which will match the raw return code value that ErrorCode
        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
        // Safety: TransferErrorCode discriminants are always error code values, and
        // thus the Result returned here will always be an `Err` variant, so we can
        // always extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}

/// Error codes used by
/// [SharedCreepProperties::withdraw](crate::SharedCreepProperties::withdraw).
///
/// [Screeps API Docs](https://docs.screeps.com/api/#Creep.withdraw).
///
/// [Screeps Engine Source Code](https://github.com/screeps/engine/blob/97c9d12385fed686655c13b09f5f2457dd83a2bf/src/game/creeps.js#L493)
#[derive(
    Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum WithdrawErrorCode {
    NotOwner = -1,
    Busy = -4,
    NotEnoughResources = -6,
    InvalidTarget = -7,
    Full = -8,
    NotInRange = -9,
    InvalidArgs = -10,
}

impl FromReturnCode for WithdrawErrorCode {
    type Error = Self;

    fn result_from_i8(val: i8) -> Result<(), Self::Error> {
        let maybe_result = Self::try_result_from_i8(val);
        #[cfg(feature = "unsafe-return-conversion")]
        unsafe {
            maybe_result.unwrap_unchecked()
        }
        #[cfg(not(feature = "unsafe-return-conversion"))]
        maybe_result.unwrap()
    }

    fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
        match val {
            0 => Some(Ok(())),
            -1 => Some(Err(WithdrawErrorCode::NotOwner)),
            -4 => Some(Err(WithdrawErrorCode::Busy)),
            -6 => Some(Err(WithdrawErrorCode::NotEnoughResources)),
            -7 => Some(Err(WithdrawErrorCode::InvalidTarget)),
            -8 => Some(Err(WithdrawErrorCode::Full)),
            -9 => Some(Err(WithdrawErrorCode::NotInRange)),
            -10 => Some(Err(WithdrawErrorCode::InvalidArgs)),
            _ => None,
        }
    }
}

impl fmt::Display for WithdrawErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg: &'static str = match self {
            WithdrawErrorCode::NotOwner => "you are not the owner of this creep, or there is a hostile rampart on top of the target",
            WithdrawErrorCode::Busy => "the creep is still being spawned",
            WithdrawErrorCode::NotEnoughResources => "the target does not have the given amount of resources",
            WithdrawErrorCode::InvalidTarget => "the target is not a valid object which can contain the specified resource",
            WithdrawErrorCode::Full => "the creep's carry is full",
            WithdrawErrorCode::NotInRange => "the target is too far away",
            WithdrawErrorCode::InvalidArgs => "the resourcetype is not one of the resource_* constants, or the amount is incorrect",
        };

        write!(f, "{}", msg)
    }
}

impl Error for WithdrawErrorCode {}

impl From<WithdrawErrorCode> for ErrorCode {
    fn from(value: WithdrawErrorCode) -> Self {
        // Safety: WithdrawErrorCode is repr(i8), so we can cast it to get the
        // discriminant value, which will match the raw return code value that ErrorCode
        // expects.   Ref: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.coercion.intro
        // Safety: WithdrawErrorCode discriminants are always error code values, and
        // thus the Result returned here will always be an `Err` variant, so we can
        // always extract the error without panicking
        Self::result_from_i8(value as i8).unwrap_err()
    }
}