2026-11-10 19:53:07 -06:00
extends TestBase
2026-11-09 10:01:10 -06:00
## Headless checks for the roll modifier on ranged roll tables (dice_mod,
## RollRange): results past the dice, using the nearest result past the ends,
## chances with a modifier, the file format, plain words and the Why line,
## and the ModifierEditor's plain forms.
## Run: godot --headless --path . -s res://tests/test_modifier.gd
func _init ( ) - > void :
_test_model ( )
_test_chances ( )
_test_rolls ( )
_test_live_mod ( )
_test_words ( )
_test_problems ( )
_test_editor_forms ( )
2026-11-10 19:53:07 -06:00
finish ( )
2026-11-09 10:01:10 -06:00
## A 1d20 + @luck event table with results from -3 to 24:
## -3 Disaster · -2– 5 Bad · 6– 15 Quiet · 16– 23 Good · 24 Windfall.
func _events ( mod : String = " @luck " ) - > RollTable :
var t : = RollTable . new ( " events " , " Events " )
t . dice = " 1d20 "
t . dice_mod = mod
for r : Array in [ [ " Disaster " , - 3 , - 3 ] , [ " Bad " , - 2 , 5 ] , [ " Quiet " , 6 , 15 ] , [ " Good " , 16 , 23 ] , [ " Windfall " , 24 , 24 ] ] :
var e : = TableEntry . new ( " e %d " % t . entries . size ( ) , r [ 0 ] )
e . has_range = true
e . range_lo = r [ 1 ]
e . range_hi = r [ 2 ]
t . entries . append ( e )
return t
func _board ( t : RollTable ) - > TowerBoard :
var b : = TowerBoard . new ( )
b . id = " modifier "
b . tables . append ( t )
var luck : = RollTable . create ( RollTable . KIND_VARIABLE , " v_luck " , " Luck " )
luck . var_name = " luck "
b . tables . append ( luck )
return b
func _luck ( n : int ) - > Dictionary :
return { " luck " : { " value " : n , " source " : " " } }
func _test_model ( ) - > void :
print ( " Model and file format " )
var t : = _events ( )
_check ( t . has_mod ( ) and t . clamps ( ) , " a modifier makes the table use the nearest result past its ends " )
_eq ( t . roll_text ( ) , " 1d20 + @luck " , " roll text " )
_eq ( RollRange . natural ( t ) , Vector2i ( 1 , 20 ) , " the dice alone roll 1– 20 " )
_eq ( RollRange . span ( t ) , Vector2i ( - 3 , 24 ) , " the ranges span -3– 24 " )
_check ( t . min_value ( ) == - 3 and t . die_size ( ) == 24 , " the card shows -3 to 24 " )
_eq ( t . num_label ( t . entries [ 0 ] , 0 ) , " ≤-3 " , " the low end reads ≤-3 " )
_eq ( t . num_label ( t . entries [ 4 ] , 4 ) , " 24+ " , " the high end reads 24+ " )
_eq ( t . num_label ( t . entries [ 2 ] , 2 ) , " 6– 15 " , " middle ranges read as written " )
var back : = RollTable . from_dict ( t . to_dict ( ) )
_eq ( back . dice_mod , " @luck " , " dice_mod survives a save " )
_check ( not RollTable . from_dict ( _events ( " " ) . to_dict ( ) ) . has_mod ( ) , " no modifier, no key " )
_check ( not _events ( " " ) . to_dict ( ) . has ( " dice_mod " ) , " an empty modifier isn ' t written " )
_eq ( RollRange . join ( " 2d6 " , " -1 " ) , " 2d6 - 1 " , " a negative modifier joins with minus " )
_eq ( RollRange . join ( " 2d6 " , " " ) , " 2d6 " , " a blank modifier is no modifier " )
func _test_chances ( ) - > void :
print ( " Chances " )
var t : = _events ( )
for mod in [ 0 , 3 , - 5 , 30 ] :
var sum : = 0.0
for e in t . entries :
sum += t . chance ( e , mod )
_check ( absf ( sum - 1.0 ) < 1e-9 , " chances add up to 1 with the modifier at %d " % mod )
_check ( absf ( t . chance ( t . entries [ 2 ] , 0 ) - 0.5 ) < 1e-9 , " Quiet (6– 15) is 10/20 with no modifier " )
_eq ( t . chance ( t . entries [ 0 ] , 0 ) , 0.0 , " Disaster can ' t come up with no modifier " )
_check ( absf ( t . chance ( t . entries [ 0 ] , - 5 ) - 0.1 ) < 1e-9 , " -5: a 2 gives -3 and a 1 gives -4, which counts as -3 " )
_check ( absf ( t . chance ( t . entries [ 4 ] , 4 ) - 0.05 ) < 1e-9 , " +4: a 20 gives 24 " )
_check ( absf ( t . chance ( t . entries [ 4 ] , 30 ) - 1.0 ) < 1e-9 , " +30: every roll counts as 24 " )
_check ( not RollRange . natural_reach ( t , t . entries [ 0 ] ) and RollRange . natural_reach ( t , t . entries [ 1 ] ) , " only a modifier reaches -3 " )
_check ( t . can_come_up ( t . entries [ 0 ] ) , " -3 can still come up (with a modifier) " )
func _test_rolls ( ) - > void :
print ( " Rolling " )
var t : = _events ( )
var b : = _board ( t )
for seed_value in range ( 1 , 40 ) :
var rng : = RandomNumberGenerator . new ( )
rng . seed = seed_value
var ctx : = Roller . new_context ( b , rng , _luck ( 3 ) )
var tree : = Roller . roll ( b , " events " , ctx . rng , 0 , { } , ctx )
var f : Dictionary = tree [ " why " ] [ 0 ]
_eq ( int ( f [ " total " ] ) , int ( f [ " nat " ] ) + 3 , " total is the dice plus Luck (seed %d ) " % seed_value )
_eq ( int ( f [ " mod " ] ) , 3 , " the fact ' s mod is Luck " )
_eq ( f [ " mod_expr " ] , " @luck " , " the fact names the modifier " )
_check ( t . entries [ tree [ " index " ] ] . range_lo < = int ( f [ " total " ] ) and int ( f [ " total " ] ) < = t . entries [ tree [ " index " ] ] . range_hi , " the total picks its range " )
# Luck +10 pushes a high roll past 24: it counts as 24.
var high : = false
for seed_value in range ( 1 , 80 ) :
var rng : = RandomNumberGenerator . new ( )
rng . seed = seed_value
var ctx : = Roller . new_context ( b , rng , _luck ( 10 ) )
var tree : = Roller . roll ( b , " events " , ctx . rng , 0 , { } , ctx )
var f : Dictionary = tree [ " why " ] [ 0 ]
if int ( f [ " total " ] ) > 24 :
high = true
_eq ( int ( f [ " counts_as " ] ) , 24 , " past the top counts as 24 " )
_eq ( tree [ " index " ] , 4 , " and takes Windfall " )
_eq ( ctx . get_value ( " nat " ) , f [ " nat " ] , " nat stays the dice alone " )
break
_check ( high , " some roll went past 24 " )
# A modifier that fails stops the card with a readable message.
t . dice_mod = " @luck + "
var ctx2 : = Roller . new_context ( b , RandomNumberGenerator . new ( ) , _luck ( 1 ) )
var bad : = Roller . roll ( b , " events " , ctx2 . rng , 0 , { } , ctx2 )
_check ( str ( bad . get ( " stop " , " " ) ) . begins_with ( " Can ' t roll 1d20 + @luck +: the modifier " ) , " a broken modifier stops the card and names it: %s " % str ( bad . get ( " stop " , " " ) ) )
func _test_live_mod ( ) - > void :
print ( " The modifier ' s value now " )
var t : = _events ( )
var b : = _board ( t )
var lm : = RollRange . live_mod ( t , Roller . live_context ( b , _luck ( 2 ) ) )
_check ( lm [ " known " ] and lm [ " value " ] == 2 , " @luck reads as 2 " )
_eq ( RollRange . live_mod ( _events ( " " ) , Roller . live_context ( b , { } ) ) [ " value " ] , 0 , " no modifier is 0 " )
_check ( not RollRange . live_mod ( _events ( " 1d4 " ) , Roller . live_context ( b , { } ) ) [ " known " ] , " dice in the modifier: unknown " )
_check ( not RollRange . live_mod ( _events ( " tier " ) , Roller . live_context ( b , { } ) ) [ " known " ] , " an unset roll value: unknown " )
var inp : = _events ( " bonus " )
inp . inputs = [ { " name " : " bonus " , " label " : " Bonus " , " default " : " 3 " } ]
var il : = RollRange . live_mod ( inp , Roller . live_context ( b , { } ) )
_check ( il [ " known " ] and il [ " value " ] == 3 , " a card input reads as its default " )
func _test_words ( ) - > void :
print ( " Plain words " )
var t : = _events ( )
var b : = _board ( t )
_eq ( Describe . roll ( t , b ) , " Roll d20 + Luck " , " the roll in words " )
_eq ( Describe . roll ( _events ( " -@luck " ) , b ) , " Roll d20 − Luck " , " subtracting reads as minus " )
_eq ( RulesView . roll_line ( t , b , false ) , " 1d20 + @luck " , " text view " )
var f : = { " type " : " roll " , " pick " : " dice " , " dice " : " 1d20 + @luck " , " faces " : [ 20 ] , " nat " : 20 , " mod " : 7 , " total " : 27 ,
" mod_expr " : " @luck " , " counts_as " : 24 }
_eq ( Describe . why ( f , b ) , " Rolled 20, +7 for Luck = 27, past the top of the table, so it counts as 24 " , " why, past the top " )
f = { " type " : " roll " , " pick " : " dice " , " dice " : " 1d20 + @luck " , " faces " : [ 3 ] , " nat " : 3 , " mod " : - 2 , " total " : 1 , " mod_expr " : " @luck " }
_eq ( Describe . why ( f , b ) , " Rolled 3, − 2 for Luck = 1 " , " why, a negative modifier " )
_check ( Describe . why ( f , b , false ) . begins_with ( " roll 1d20 + @luck: 3 -2 = 1 " ) , " why in text view: %s " % Describe . why ( f , b , false ) )
func _test_problems ( ) - > void :
print ( " Range problems " )
_eq ( _events ( ) . range_problems ( ) , " " , " a modifier table from -3 to 24 is clean " )
var plain : = _events ( " " )
_check ( plain . range_problems ( ) . contains ( " outside 1– 20 " ) and plain . range_problems ( ) . contains ( " modifier " ) ,
" without a modifier, -3 and 24 are outside the dice: %s " % plain . range_problems ( ) )
var gap : = _events ( )
gap . entries [ 2 ] . range_lo = 8
_eq ( gap . range_problems ( ) , " No result for 6– 7. " , " a gap is still reported " )
_check ( TableSettingsRules . mod_error ( " @luck + " ) != " " and TableSettingsRules . mod_error ( " @luck " ) == " " , " a broken modifier is refused " )
var t : = _events ( " " )
var r : = TableSettingsRules . apply_settings ( t , { " ranged " : true , " dice " : " 1d20 " , " dice_mod " : " @luck " } )
_check ( r [ " ok " ] and t . dice_mod == " @luck " , " settings apply the modifier " )
r = TableSettingsRules . apply_settings ( t , { " ranged " : true , " dice " : " 1d20 " , " dice_mod " : " @luck + " } )
_check ( not r [ " ok " ] and t . dice_mod == " @luck " , " a broken modifier changes nothing " )
func _test_editor_forms ( ) - > void :
print ( " Modifier editor forms " )
var W : = ModifierEditor . Way
for c : Array in [ [ " " , W . NONE , " " ] , [ " @luck " , W . ADD , " @luck " ] , [ " tier " , W . ADD , " tier " ] , [ " -@fatigue " , W . SUBTRACT , " @fatigue " ] ,
[ " - @fatigue " , W . SUBTRACT , " @fatigue " ] , [ " dm(@str) " , W . DM , " @str " ] , [ " 2 " , W . NUMBER , " 2 " ] , [ " -1 " , W . NUMBER , " -1 " ] ,
[ " @luck + 1 " , W . CUSTOM , " @luck + 1 " ] , [ " 1d4 " , W . CUSTOM , " 1d4 " ] , [ " max(@a, @b) " , W . CUSTOM , " max(@a, @b) " ] ] :
var p : = ModifierEditor . parse ( c [ 0 ] )
_eq ( [ p [ " way " ] , p [ " value " ] ] , [ c [ 1 ] , c [ 2 ] ] , " parse %s " % c [ 0 ] )
_eq ( ModifierEditor . compose ( W . SUBTRACT , " @fatigue " ) , " -@fatigue " , " compose subtract " )
_eq ( ModifierEditor . compose ( W . DM , " @str " ) , " dm(@str) " , " compose dm " )
_eq ( ModifierEditor . compose ( W . ADD , " " ) , " " , " no value yet, no text " )