Skip to content

Commit

Permalink
Allow Strike RE dice property to be resolvable (#8566)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonward committed Jul 10, 2023
1 parent 80add9c commit 55567ec
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/module/rules/rule-element/strike.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
StringField,
} from "types/foundry/common/data/fields.d.ts";
import { RuleElementOptions, RuleElementPF2e, RuleElementSchema, RuleElementSource } from "./index.ts";
import { ResolvableValueField } from "./data.ts";

/**
* Create an ephemeral strike on an actor
Expand Down Expand Up @@ -121,14 +122,7 @@ class StrikeRuleElement extends RuleElementPF2e<StrikeSchema> {
damage: new fields.SchemaField({
base: new fields.SchemaField({
damageType: new fields.StringField({ required: true, blank: false, initial: "bludgeoning" }),
dice: new fields.NumberField({
required: true,
nullable: false,
integer: true,
min: 0,
max: 8,
initial: 1,
}),
dice: new ResolvableValueField({ required: true, nullable: false, initial: 1 }),
die: new fields.StringField({ required: true, choices: CONFIG.PF2E.damageDie, initial: "d4" }),
modifier: new fields.NumberField({ nullable: false, min: 0, initial: 0 }),
}),
Expand Down Expand Up @@ -236,8 +230,16 @@ class StrikeRuleElement extends RuleElementPF2e<StrikeSchema> {
return this.failValidation("Unrecognized damage type");
}

const dice = ((): number => {
const resolvedDice = Number(this.resolveValue(this.damage.base.dice));
return Math.clamped(Math.trunc(resolvedDice), 0, 8);
})();
if (Number.isNaN(dice)) {
return this.failValidation("dice does not resolve to a number");
}

if (predicatePassed) {
const weapon = this.#constructWeapon(damageType);
const weapon = this.#constructWeapon(damageType, dice);
const slug = weapon.slug ?? sluggify(weapon.name);
this.actor.synthetics.strikes.set(slug, weapon);
}
Expand All @@ -262,7 +264,7 @@ class StrikeRuleElement extends RuleElementPF2e<StrikeSchema> {
* Construct a `WeaponPF2e` instance for use as the synthetic strike
* @param damageType The resolved damage type for the strike
*/
#constructWeapon(damageType: DamageType): WeaponPF2e<ActorPF2e> {
#constructWeapon(damageType: DamageType, dice: number): WeaponPF2e<ActorPF2e> {
const actorIsNPC = this.actor.isOfType("npc");
const source: PreCreate<WeaponSource> = deepClone({
_id: this.item.id,
Expand All @@ -287,6 +289,7 @@ class StrikeRuleElement extends RuleElementPF2e<StrikeSchema> {
},
damage: {
...this.damage.base,
dice,
damageType,
},
range: (this.range?.increment ?? null) as WeaponRangeIncrement | null,
Expand Down Expand Up @@ -378,7 +381,7 @@ type StrikeSchema = RuleElementSchema & {
damage: SchemaField<{
base: SchemaField<{
damageType: StringField<string, string, true, false, true>;
dice: NumberField<number, number, true, false, true>;
dice: ResolvableValueField<true, false, true>;
die: StringField<DamageDieSize, DamageDieSize, true, false, true>;
modifier: NumberField<number, number, false, false, true>;
}>;
Expand Down

0 comments on commit 55567ec

Please sign in to comment.