@@ -34,6 +34,7 @@ export class DamageData extends foundry.abstract.DataModel {
3434 enabled : new BooleanField ( ) ,
3535 formula : new FormulaField ( )
3636 } ) ,
37+ modifiers : new SetField ( new StringField ( ) ) ,
3738 scaling : new SchemaField ( {
3839 mode : new StringField ( ) ,
3940 number : new NumberField ( { initial : 1 , min : 0 , integer : true } ) ,
@@ -51,7 +52,7 @@ export class DamageData extends foundry.abstract.DataModel {
5152 * @type {string }
5253 */
5354 get formula ( ) {
54- if ( this . custom . enabled ) return this . custom . formula ?? "" ;
55+ if ( this . custom . enabled ) return this . _manualFormula ( ) ;
5556 return this . _automaticFormula ( ) ;
5657 }
5758
@@ -60,44 +61,65 @@ export class DamageData extends foundry.abstract.DataModel {
6061 /* -------------------------------------------- */
6162
6263 /**
63- * Produce the auto-generated formula from the `number`, `denomination`, and `bonus`.
64- * @param {number } [increase=0] Amount to increase the die count.
64+ * Produce the auto-generated formula from the `number`, `denomination`, `modifiers`, and `bonus`.
65+ * @param {number } [increase=0] Amount to increase the die count.
66+ * @param {object } [options={}]
67+ * @param {Set<string> } [options.modifiers] Additional modifiers to apply to the formula, if possible.
6568 * @returns {string }
6669 * @protected
6770 */
68- _automaticFormula ( increase = 0 ) {
71+ _automaticFormula ( increase = 0 , { modifiers } = { } ) {
6972 let formula ;
7073 const number = ( this . number ?? 0 ) + increase ;
71- if ( number && this . denomination ) formula = `${ number } d${ this . denomination } ` ;
74+ if ( number && this . denomination ) {
75+ formula = `${ number } d${ this . denomination } ${ Array . from ( this . modifiers ) . concat ( modifiers ?? [ ] ) . join ( "" ) } ` ;
76+ }
7277 if ( this . bonus ) formula = formula ? `${ formula } + ${ this . bonus } ` : this . bonus ;
7378 return formula ?? "" ;
7479 }
7580
7681 /* -------------------------------------------- */
7782
83+ /**
84+ * Produce the manual formula from the `custom.formula` and `modifiers` (if possible).
85+ * @param {object } [options={}]
86+ * @param {Set<string> } [options.modifiers] Additional modifiers to apply to the formula, if possible.
87+ * @returns {string }
88+ * @protected
89+ */
90+ _manualFormula ( { modifiers } = { } ) {
91+ if ( ! this . custom . formula ) return "" ;
92+ modifiers = Array . from ( this . modifiers ) . concat ( modifiers ?? [ ] ) . join ( "" ) ;
93+ return this . custom . formula . replace ( / (?: \d | \) ) d (?: \d + \w * | \( .+ \) \d * \w * ) / , `$&${ modifiers } ` ) ;
94+ }
95+
96+ /* -------------------------------------------- */
97+
7898 /**
7999 * Scale the damage by a number of steps using its configured scaling configuration.
80- * @param {number|Scaling } increase Number of steps above base damage to scaling.
100+ * @param {number|Scaling } increase Number of steps above base damage to scaling.
101+ * @param {object } [options={}]
102+ * @param {Set<string> } [options.modifiers] Additional modifiers to apply to the formula, if possible.
81103 * @returns {string }
82104 */
83- scaledFormula ( increase ) {
105+ scaledFormula ( increase , { modifiers } = { } ) {
84106 if ( increase instanceof Scaling ) increase = increase . increase ;
85107
86108 switch ( this . scaling . mode ) {
87109 case "whole" : break ;
88110 case "half" : increase = Math . floor ( increase * .5 ) ; break ;
89111 default : increase = 0 ; break ;
90112 }
91- if ( ! increase ) return this . formula ;
113+ if ( ! increase ) return this . custom . enabled ? this . _manualFormula ( ) : this . _automaticFormula ( 0 , { modifiers } ) ;
92114 let formula ;
93115
94116 // If dice count scaling, increase the count on the first die rolled
95117 const dieIncrease = ( this . scaling . number ?? 0 ) * increase ;
96118 if ( this . custom . enabled ) {
97- formula = this . custom . formula ;
119+ formula = this . _manualFormula ( { modifiers } ) ;
98120 formula = formula . replace ( / ^ ( \d ) + d / , ( match , number ) => `${ Number ( number ) + dieIncrease } d` ) ;
99121 } else {
100- formula = this . _automaticFormula ( dieIncrease ) ;
122+ formula = this . _automaticFormula ( dieIncrease , { modifiers } ) ;
101123 }
102124
103125 // If custom scaling included, modify to match increase and append for formula
0 commit comments