enshwick 1.0.0-alpha.11

The Enshwick Loader's modding API for Enshrouded: the one crate mods are written against
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
use crate::abi::{InventoryOp, InventoryRequest};
use crate::{Entity, ItemId};

use super::slots::{Fill, SlotRef};
use super::{MAX_INVENTORIES, MAX_OPS, OP_LOG, inventories};

/// One change in a [`Batch`].
///
/// # Examples
///
/// ```
/// use enshwick::Entity;
/// use enshwick::inventory::Op;
///
/// let loot = Op::MoveAll { from: Entity(40), to: Entity(2), include_special: false };
/// let _ = loot;
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Op {
    /// New items: merged into stacks of the same item, then free slots.
    Give {
        /// The inventory.
        to: Entity,
        /// The item id.
        item: ItemId,
        /// How many.
        count: u32,
        /// How full each new item's meter starts; `None` for its
        /// template's start. Items without a meter ignore it.
        fill: Option<Fill>,
    },
    /// Removes items, from whichever stacks hold them.
    Take {
        /// The inventory.
        from: Entity,
        /// The item id.
        item: ItemId,
        /// How many.
        count: u32,
    },
    /// Destroys a stack.
    DestroySlot(SlotRef),
    /// Moves a stack, or part of it, into an inventory's free or matching
    /// slots. What does not fit stays in the source.
    MoveSlot {
        /// The stack.
        slot: SlotRef,
        /// The inventory.
        to: Entity,
        /// How many; `None` for the whole stack.
        amount: Option<u16>,
    },
    /// Replaces the stack in a slot with new items, each with its own entity
    /// and its meter at `fill`, asked for the same slot: gives an item that
    /// lost its entity, such as one from before its mod gave it a
    /// [`FuelSetup`](crate::content::items::FuelSetup), a fresh one.
    Renew {
        /// The stack.
        slot: SlotRef,
        /// The inventory the slot belongs to, at its root
        /// ([`Reader::root`](super::Reader::root)).
        root: Entity,
        /// The new items' id.
        item: ItemId,
        /// How many.
        count: u32,
        /// Their meter's fill.
        fill: Fill,
    },
    /// Moves every stack of one inventory into another, as "take all" does.
    /// What does not fit stays in the source.
    MoveAll {
        /// The source inventory.
        from: Entity,
        /// The destination inventory.
        to: Entity,
        /// Also the source's action bar, equipment and locked slots.
        include_special: bool,
    },
}

impl Op {
    pub(super) fn raw(&self) -> InventoryOp {
        let entity = |e: Entity| u64::from(e.0);
        match *self {
            Self::Give {
                to,
                item,
                count,
                fill,
            } => InventoryOp {
                kind: InventoryOp::GIVE,
                flags: if fill.is_some() {
                    InventoryOp::FILLED
                } else {
                    0
                },
                target: entity(to),
                other: fill.map_or(0, |f| u64::from(f.fraction().to_bits())),
                item,
                count,
            },
            Self::Take { from, item, count } => InventoryOp {
                kind: InventoryOp::TAKE,
                target: entity(from),
                item,
                count,
                ..InventoryOp::default()
            },
            Self::DestroySlot(slot) => InventoryOp {
                kind: InventoryOp::DESTROY_SLOT,
                target: slot.0,
                ..InventoryOp::default()
            },
            Self::MoveSlot { slot, to, amount } => InventoryOp {
                kind: InventoryOp::MOVE_SLOT,
                target: slot.0,
                other: entity(to),
                count: amount.map_or(0, u32::from),
                ..InventoryOp::default()
            },
            Self::Renew {
                slot,
                root,
                item,
                count,
                fill,
            } => InventoryOp {
                kind: InventoryOp::RENEW,
                flags: InventoryOp::FILLED,
                target: slot.0,
                other: entity(root) | u64::from(fill.fraction().to_bits()) << 32,
                item,
                count,
            },
            Self::MoveAll {
                from,
                to,
                include_special,
            } => InventoryOp {
                kind: InventoryOp::MOVE_ALL,
                flags: if include_special {
                    InventoryOp::INCLUDE_SPECIAL
                } else {
                    0
                },
                target: entity(from),
                other: entity(to),
                ..InventoryOp::default()
            },
        }
    }

    /// Op log records the operation may write, at most.
    const fn cost(&self) -> u32 {
        match self {
            Self::Give { .. } | Self::Take { .. } => 16,
            Self::Renew { .. } => 17,
            Self::DestroySlot(_) => 1,
            Self::MoveSlot { .. } => 4,
            Self::MoveAll { .. } => 256,
        }
    }

    /// The inventories the operation names.
    pub(super) const fn inventories(&self) -> [Entity; 2] {
        match *self {
            Self::Give { to, .. } => [to, Entity::NONE],
            Self::Take { from, .. } => [from, Entity::NONE],
            Self::DestroySlot(slot) => [slot.inventory(), Entity::NONE],
            Self::Renew { root, .. } => [root, Entity::NONE],
            Self::MoveSlot { slot, to, .. } => [slot.inventory(), to],
            Self::MoveAll { from, to, .. } => [from, to],
        }
    }

    /// Whether the operation names what it needs.
    fn is_valid(&self) -> bool {
        match *self {
            Self::Give {
                to: e, item, count, ..
            }
            | Self::Take {
                from: e,
                item,
                count,
            } => !e.is_none() && item.get() != 0 && count != 0 && i32::try_from(count).is_ok(),
            Self::DestroySlot(slot) => !slot.inventory().is_none(),
            Self::Renew {
                slot,
                root,
                item,
                count,
                ..
            } => {
                !slot.inventory().is_none()
                    && !root.is_none()
                    && item.get() != 0
                    && count != 0
                    && i32::try_from(count).is_ok()
            }
            Self::MoveSlot { slot, to, amount } => {
                !slot.inventory().is_none() && !to.is_none() && amount != Some(0)
            }
            Self::MoveAll { from, to, .. } => !from.is_none() && !to.is_none() && from != to,
        }
    }
}

/// Why a [`Batch`] cannot run as one transaction.
///
/// # Examples
///
/// ```
/// let empty = enshwick::inventory::Batch::new();
/// assert_eq!(empty.check(), Err(enshwick::inventory::BatchError::Empty));
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, thiserror::Error)]
#[non_exhaustive]
pub enum BatchError {
    /// No operations.
    #[error("the batch is empty")]
    Empty,
    /// An operation names no inventory or item, or a count of 0.
    #[error("an operation is malformed")]
    Malformed,
    /// More than [`MAX_OPS`] operations.
    #[error("more than {MAX_OPS} operations")]
    TooManyOps,
    /// More than [`MAX_INVENTORIES`] inventories.
    #[error("more than {MAX_INVENTORIES} inventories")]
    TooManyInventories,
    /// The operations could overflow the game's op log.
    #[error("too much work for one transaction")]
    TooLarge,
}

/// Operations to run as one transaction: every one succeeds, or nothing
/// changes.
///
/// # Examples
///
/// ```
/// use enshwick::Entity;
/// use enshwick::inventory::{Batch, Op};
///
/// // A trade: 5 coins out, a sword in, or neither.
/// let player = Entity(2);
/// let trade = Batch::new()
///     .with(Op::Take { from: player, item: enshwick::ItemId::new(0x0c01), count: 5 })
///     .with(Op::Give { to: player, item: enshwick::ItemId::new(0x5e01), count: 1, fill: None });
/// assert_eq!(trade.check(), Ok(()));
/// ```
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Batch {
    pub(super) ops: Vec<Op>,
    pub(super) allow_partial: bool,
    notify: bool,
    pub(super) actor: Entity,
}

impl Batch {
    /// An empty batch.
    ///
    /// # Examples
    ///
    /// See [`Batch`].
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// The batch with `op` appended.
    ///
    /// # Examples
    ///
    /// See [`Batch`].
    #[must_use]
    pub fn with(mut self, op: Op) -> Self {
        self.ops.push(op);
        self
    }

    /// Appends `op`.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut batch = enshwick::inventory::Batch::new();
    /// batch.push(enshwick::inventory::Op::DestroySlot(enshwick::inventory::SlotRef(0x41)));
    /// assert_eq!(batch.ops().len(), 1);
    /// ```
    pub fn push(&mut self, op: Op) {
        self.ops.push(op);
    }

    /// Commits even when a give or take leaves some of its count undone;
    /// otherwise that rolls the batch back.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::Entity;
    /// use enshwick::inventory::{Batch, Op};
    ///
    /// // As many as fit.
    /// let _ = Batch::new().with(Op::Give { to: Entity(2), item: enshwick::ItemId::new(7), count: 500, fill: None }).allow_partial();
    /// ```
    #[must_use]
    pub const fn allow_partial(mut self) -> Self {
        self.allow_partial = true;
        self
    }

    /// Shows the actor the game's pickup message for each stack it gets and
    /// its inventory-full message for what does not fit, as the game's own
    /// looting does. Batches with a give always do; moves are silent without
    /// this.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::Entity;
    /// use enshwick::inventory::{Batch, Op};
    ///
    /// let (corpse, player) = (Entity(40), Entity(2));
    /// let _ = Batch::new()
    ///     .with(Op::MoveAll { from: corpse, to: player, include_special: false })
    ///     .actor(player)
    ///     .notify();
    /// ```
    #[must_use]
    pub const fn notify(mut self) -> Self {
        self.notify = true;
        self
    }

    /// The player the game credits with the change, for its pickup and
    /// inventory-full messages. By default the destination of the first
    /// give or move.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::Entity;
    /// use enshwick::inventory::{Batch, Op};
    ///
    /// let _ = Batch::new().with(Op::MoveAll { from: Entity(40), to: Entity(41), include_special: false }).actor(Entity(2));
    /// ```
    #[must_use]
    pub const fn actor(mut self, player: Entity) -> Self {
        self.actor = player;
        self
    }

    /// The operations, in order.
    ///
    /// # Examples
    ///
    /// See [`Batch::push`].
    #[must_use]
    pub fn ops(&self) -> &[Op] {
        &self.ops
    }

    /// Whether the batch fits one transaction.
    ///
    /// # Errors
    ///
    /// The first limit the batch breaks.
    ///
    /// # Examples
    ///
    /// See [`Batch`].
    pub fn check(&self) -> Result<(), BatchError> {
        if self.ops.is_empty() {
            return Err(BatchError::Empty);
        }
        if !self.ops.iter().all(Op::is_valid) {
            return Err(BatchError::Malformed);
        }
        if self.ops.len() > MAX_OPS {
            return Err(BatchError::TooManyOps);
        }
        if inventories(&self.ops) > MAX_INVENTORIES {
            return Err(BatchError::TooManyInventories);
        }
        if self.ops.iter().map(Op::cost).sum::<u32>() > OP_LOG {
            return Err(BatchError::TooLarge);
        }
        Ok(())
    }

    /// Splits the batch into batches that each fit one transaction, in
    /// order. Each part is all or nothing on its own; together they are not.
    /// Malformed operations are kept, for [`Batch::check`] to name.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::Entity;
    /// use enshwick::inventory::{Batch, Op};
    ///
    /// let mut everything = Batch::new();
    /// for chest in 100..120 {
    ///     everything.push(Op::MoveAll { from: Entity(chest), to: Entity(2), include_special: false });
    /// }
    /// let parts = everything.split();
    /// assert!(parts.len() > 1);
    /// assert!(parts.iter().all(|part| part.check().is_ok()));
    /// ```
    #[must_use]
    pub fn split(self) -> Vec<Self> {
        let mut parts: Vec<Self> = Vec::new();
        let mut part = Self {
            ops: Vec::new(),
            ..self.clone()
        };
        for op in self.ops {
            part.ops.push(op);
            if part.ops.len() > 1 && part.check().is_err_and(|e| e != BatchError::Malformed) {
                part.ops.pop();
                let full = std::mem::replace(&mut part.ops, vec![op]);
                parts.push(Self {
                    ops: full,
                    ..part.clone()
                });
            }
        }
        if !part.ops.is_empty() {
            parts.push(part);
        }
        parts
    }

    pub(super) fn request_flags(&self) -> u32 {
        let mut flags = 0;
        if self.allow_partial {
            flags |= InventoryRequest::ALLOW_PARTIAL;
        }
        if self.notify || self.ops.iter().any(|op| matches!(op, Op::Give { .. })) {
            flags |= InventoryRequest::NOTIFY;
        }
        flags
    }

    pub(super) fn actor_entity(&self) -> Entity {
        if !self.actor.is_none() {
            return self.actor;
        }
        self.ops
            .iter()
            .find_map(|op| match *op {
                Op::Give { to, .. } | Op::MoveSlot { to, .. } | Op::MoveAll { to, .. } => Some(to),
                _ => None,
            })
            .unwrap_or(Entity::NONE)
    }
}