import DFManualRolls from "./DFManualRolls" ; import DFRollPrompt from "./DFRollPrompt" ; import SETTINGS from "../../common/Settings" ; /***** Pathfinder1 Roller Declaration *****/ declare class RollPF { static safeRoll ( p1 , p2 ) ; } export default class DFManualRollsLegacy { static PREF_USE_LEGACY = 'use-legacy' ; static get useLegacy ( ) : boolean { return SETTINGS . get ( DFManualRollsLegacy . PREF_USE_LEGACY ) ; } private static pf1HelpersPatched = false ; static patch ( ) { libWrapper . register ( SETTINGS . MOD_NAME , 'DiceTerm.prototype._evaluateSync' , this . _DiceTerm_evaluateSync , 'MIXED' ) ; if ( this . pf1HelpersPatched ) return ; this . pf1HelpersPatched = true ; /*******************************************************/ /************** This Code Copied From PF1 **************/ /*******************************************************/ Handlebars . registerHelper ( "itemDamage" , ( item , rollData ) => { if ( ! item . hasDamage ) return null ; const actorData = item . document . parentActor . data . data , itemData = item . data ; const rv = [ ] ; const reduceFormula = ( formula ) => { /******** MODIFIED PORTION START ********/ let roll ; try { DFManualRolls . tempDisable = true ; roll = RollPF . safeRoll ( formula , rollData ) ; } finally { DFManualRolls . tempDisable = false ; } /******** MODIFIED PORTION END ********/ formula = roll . formula . replace ( /\[[^\]]+\]/g , "" ) ; return [ roll , formula ] ; } ; const handleParts = ( parts ) => { for ( const [ formula , _ ] of parts ) { const [ roll , newformula ] = reduceFormula ( formula ) ; if ( roll . total == 0 ) continue ; rv . push ( newformula ) ; } } ; handleParts ( itemData . damage . parts ) ; const dmgAbl = itemData . ability . damage ; const dmgAblMod = Math . floor ( ( actorData . abilities [ dmgAbl ] ?. mod ?? 0 ) * ( itemData . ability . damageMult || 1 ) ) ; if ( dmgAblMod != 0 ) rv . push ( dmgAblMod ) ; handleParts ( itemData . damage . nonCritParts ) ; const sources = item . document . allDamageSources ; for ( const s of sources ) rv . push ( s . formula ) ; if ( rv . length === 0 ) rv . push ( "NaN" ) ; return rv . join ( "+" ) . replace ( /\s+/g , "" ) . replace ( /\+-/ , "-" ) . replace ( /--/g , "+" ) . replace ( /\+\++/ , "+" ) ; } ) ; } static unpatch ( ) { libWrapper . unregister ( SETTINGS . MOD_NAME , 'DiceTerm.prototype.roll' , false ) ; } static _DiceTerm_evaluateSync ( this , wrapper , { minimize = false , maximize = false } = { } ) { if ( ( this . number > 999 ) ) { throw new Error ( `You may not evaluate a DiceTerm with more than 999 requested results` ) ; } if ( ! DFManualRolls . shouldRollManually || minimize || maximize ) return wrapper ( { minimize , maximize } ) ; if ( this . modifiers . length == 0 ) { const total = DFManualRollsLegacy . prompt ( this . number , this . faces , this . flavor ) ; const results = DFRollPrompt . distributeRoll ( total [ 0 ] , this . number ) ; this . results = results . map ( x => { return { result : x , active : true } ; } ) ; if ( DFManualRolls . flagged && total [ 1 ] ) { this . options . flavor = ( this . options . flavor || '' ) + '[MRT]' ; ( < any > this . options ) . isManualRoll = true ; } } else { const flags = [ ] ; for ( let n = 1 ; n <= this . number ; n ++ ) { const roll = { result : undefined , active : true } ; if ( minimize ) { roll . result = Math . min ( 1 , this . faces ) ; flags . push ( 'RN' ) ; } else if ( maximize ) { roll . result = this . faces ; flags . push ( 'RN' ) ; } else { const result = DFManualRollsLegacy . prompt ( 1 , this . faces , this . flavor ) ; roll . result = result [ 0 ] ; flags . push ( result [ 1 ] ? 'MR' : 'RN' ) ; } this . results . push ( roll ) ; } if ( DFManualRolls . flagged && flags . some ( x => x === 'MR' ) ) { this . options . flavor = ( this . options . flavor || '' ) + '[' + flags . join ( ',' ) + ']' ; ( < any > this . options ) . isManualRoll = true ; } } this . _evaluateModifiers ( ) ; return this ; } static prompt ( number , faces , flavour ) { let failed = false ; let result = [ 0 , false ] ; const promptText = game . i18n . localize ( 'DF_MANUAL_ROLLS.Prompt.Legacy' ) . dfmr_replaceAll ( '{0}' , number . toString ( ) ) . dfmr_replaceAll ( '{1}' , faces . toString ( ) ) . dfmr_replaceAll ( '{2}' , ( number * faces ) . toString ( ) ) ; const invalidText = game . i18n . localize ( 'DF_MANUAL_ROLLS.Prompt.Legacy_Invalid' ) ; while ( true ) { let value = prompt ( promptText + ( flavour ? `\n${ flavour }` : '' ) + ( failed ? '\n' + invalidText : '' ) , '' ) ; if ( value === '' || value === null ) result = [ Math . ceil ( CONFIG . Dice . randomUniform ( ) * faces ) , false ] ; else { value = parseInt ( value ) ; result = [ value , true ] ; if ( isNaN ( < number > value ) || value < number || value > number * faces ) { failed = true ; continue ; } } return result ; } } }