@adatechnology/game-items 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -580,6 +580,8 @@ interface Inventory {
580
580
  take(params: TakeParams): InventoryResult<{
581
581
  slotIds: readonly number[];
582
582
  }>;
583
+ /** Whether `take` would put the item in the bag now (its weight, volume and length against what the bag already holds); changes nothing. */
584
+ canTake(params: TakeParams): InventoryResult;
583
585
  /** Puts an item from the world straight into the hand (it takes no slot and no bag volume, only weight). */
584
586
  hold(params: Omit<TakeParams, 'count'>): InventoryResult<{
585
587
  held: ItemStack;
@@ -650,14 +650,17 @@ ${problems.map((problem) => ` - ${problem}`).join("\n")}`);
650
650
  return slotIds;
651
651
  };
652
652
  const fitsInBag = (item, stack, plan, rest) => canTake(item, bag, { slots: slotList.length, loadKg: rest.kg, litres: rest.litres }, { count: stack.count, slotsNeeded: plan.newStacks.length, weightFactor: 1 - weightReduction });
653
- const putInBag = (stack, rest) => {
653
+ const planBag = (stack, rest) => {
654
654
  const item = items.get(stack.itemId);
655
655
  if (!item) return { ok: false, reason: "unknown_item" };
656
656
  if (!Number.isInteger(stack.count) || stack.count < 1 || stack.state !== void 0 && stack.count !== 1) return { ok: false, reason: "bad_count" };
657
657
  const plan = planSlots(item, stack);
658
658
  const verdict = fitsInBag(item, stack, plan, rest);
659
- if (verdict !== "ok") return { ok: false, reason: verdict };
660
- return { ok: true, slotIds: commit(stack, plan) };
659
+ return verdict === "ok" ? { ok: true, plan } : { ok: false, reason: verdict };
660
+ };
661
+ const putInBag = (stack, rest) => {
662
+ const planned = planBag(stack, rest);
663
+ return planned.ok ? { ok: true, slotIds: commit(stack, planned.plan) } : planned;
661
664
  };
662
665
  const findSlot = (slotId) => slotList.findIndex((slot) => slot.slotId === slotId);
663
666
  return {
@@ -666,6 +669,11 @@ ${problems.map((problem) => ` - ${problem}`).join("\n")}`);
666
669
  const now = totals();
667
670
  return putInBag(stack, { kg: now.kg, litres: now.litres });
668
671
  },
672
+ canTake: ({ itemId, count = 1, state }) => {
673
+ const now = totals();
674
+ const planned = planBag(state === void 0 ? { itemId, count } : { itemId, count, state }, { kg: now.kg, litres: now.litres });
675
+ return planned.ok ? { ok: true } : planned;
676
+ },
669
677
  hold: ({ itemId, state }) => {
670
678
  const item = items.get(itemId);
671
679
  if (!item) return { ok: false, reason: "unknown_item" };