Skip to content

Commit

Permalink
Fix stacks of items being bulk adjusted on vehicle actors
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosFdez committed Jan 24, 2025
1 parent 23413d7 commit 5486b37
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/module/actor/inventory/bulk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ActorPF2e } from "@actor";
import { PhysicalItemPF2e } from "@item";
import { Bulk } from "@item/physical/bulk.ts";
import { Size } from "@module/data.ts";
import { groupBy } from "@util";

export class InventoryBulk {
Expand Down Expand Up @@ -44,7 +43,7 @@ export class InventoryBulk {
if (this.#value) return this.#value;
this.#value = InventoryBulk.computeTotalBulk(
this.actor.inventory.filter((i) => !i.isInContainer),
this.actor.size,
this.actor,
);
return this.#value;
}
Expand Down Expand Up @@ -80,7 +79,8 @@ export class InventoryBulk {
return this.value.normal;
}

static computeTotalBulk(items: PhysicalItemPF2e[], actorSize: Size): Bulk {
static computeTotalBulk(items: PhysicalItemPF2e[], actor: ActorPF2e | null): Bulk {
const actorSize = actor?.size ?? "med";
items = this.#flattenNonStowing(items);

// Figure out which items have stack groups and which don't
Expand All @@ -98,11 +98,16 @@ export class InventoryBulk {
.reduce((first, second) => first.plus(second), new Bulk());

// Group by stack group, then combine into quantities, then compute bulk from combined quantities
// Only convert to actor-relative size if the actor is a creature
// https://2e.aonprd.com/Rules.aspx?ID=258
const stackingBehaviors = stackingItems.map((item) => ({
per: item.system.bulk.per,
item,
group: item.system.baseItem,
bulk: new Bulk(item.system.bulk.value).convertToSize(item.size, actorSize),
bulk:
!actor || actor.isOfType("creature")
? new Bulk(item.system.bulk.value).convertToSize(item.size, actorSize)
: new Bulk(item.system.bulk.value),
}));
const grouped = groupBy(stackingBehaviors, (d) => `${d.group}-${d.per}-${d.bulk.toLightUnits()}`);
const bulks = [...grouped.values()].map((dataEntries) => {
Expand Down
2 changes: 1 addition & 1 deletion src/module/item/container/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ContainerPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends

get capacity(): { value: Bulk; max: Bulk } {
return {
value: InventoryBulk.computeTotalBulk(this.contents.contents, this.actor?.size ?? "med"),
value: InventoryBulk.computeTotalBulk(this.contents.contents, this.actor ?? null),
max: new Bulk(this.system.bulk.capacity),
};
}
Expand Down

0 comments on commit 5486b37

Please sign in to comment.