Skip to content

Commit

Permalink
Merge branch 'depth_syntax' into godot4
Browse files Browse the repository at this point in the history
  • Loading branch information
oganm committed Jun 3, 2024
2 parents 8b49d70 + f771023 commit 22266ec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion addons/dice_syntax/dice_syntax.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static func dice_parser(dice:String,regex:RegEx = RegEx.new(), base_instance = n
var dh = preload('dice_helpers.gd')
var error = false
var msg = []
var dice_regex = '[0-9]*d[0-9]+[dksfro!<=>0-9lh]*'
var dice_regex = '[0-9]*d[0-9]+[dksfro/!<=>0-9lh]*'


regex.compile(dice_regex)
Expand Down
47 changes: 43 additions & 4 deletions addons/dice_syntax/single_dice_funs.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ static func base_dice_parser(dice_string:String,regex:RegEx = RegEx.new())->Dict
'dice_count':0,
'success': [],
'fail': [],
'explode_depth': 0,
'sort':false
}
var valid_tokens = '[dksfr!<=>hl]'
var valid_tokens = '[dksfr/!<=>hl]'
dice_string = dice_string.to_lower()
var used_tokens:PackedInt64Array

Expand Down Expand Up @@ -182,15 +183,23 @@ static func base_dice_parser(dice_string:String,regex:RegEx = RegEx.new())->Dict
# new explode rules
regex.compile('^![0-9]*$')
var explode_rules = sm.strs_detect_rg(tokens,regex)

regex.compile("/")
var end_rules = sm.strs_detect_rg(tokens,regex)

var explode:Array = []
var compound: Array = []
var compound_flag:bool = false
var depth_flag:bool = false
var explode_depth:Array = []



for i in explode_rules:
used_tokens.append(i)
if tokens[i] == "!" and i + 1 in explode_rules and not compound_flag:
compound_flag = true
elif not compound_flag:
elif not compound_flag and i-1 not in end_rules:
var explode_token = tokens[i]
if i+1 in ranges and !sm.str_detect(explode_token,"[0-9]",regex):
used_tokens.append(i+1)
Expand All @@ -201,6 +210,22 @@ static func base_dice_parser(dice_string:String,regex:RegEx = RegEx.new())->Dict
rolling_rules['dice_side'],
regex,rolling_rules,
rolling_rules['dice_side']))
elif not compound_flag and i-1 in end_rules:
# /![number]
used_tokens.append(i-1)
var depth_token = tokens[i]
if i+1 in ranges and !sm.str_detect(depth_token,"[0-9]",regex):
used_tokens.append(i+1)
depth_token = depth_token + tokens[i+1]

var depths = dh.range_determine(depth_token,
rolling_rules['dice_side'],
regex,rolling_rules,
rolling_rules['dice_side'])

dh.dice_error(depths.size()==1,"Explode depth must be set to a single integer", rolling_rules)
explode_depth.append_array(depths)

elif compound_flag:
compound_flag = false
var compound_token = tokens[i]
Expand All @@ -214,6 +239,8 @@ static func base_dice_parser(dice_string:String,regex:RegEx = RegEx.new())->Dict
regex,rolling_rules,
rolling_rules['dice_side']))

if explode_depth.size()>0:
rolling_rules['explode_depth'] = explode_depth.min()
rolling_rules['explode'] = explode
rolling_rules['compound'] = compound

Expand Down Expand Up @@ -268,13 +295,17 @@ static func base_rule_roller(rolling_rules:Dictionary,rng:RandomNumberGenerator
for i in to_reroll:
dice[i] = al.sample(possible_dice,1,rng)[0]



if rolling_rules.explode.size()>0:
var exploded_dice = []
for d in dice:
var x = d
exploded_dice.append(d)
while x in rolling_rules.explode:
var explode_count = 1
while x in rolling_rules.explode and \
(rolling_rules.explode_depth == 0 or explode_count<rolling_rules.explode_depth):
explode_count += 1
x = al.sample(possible_dice,1,rng)[0]
# if new roll is in the reroll once list, reroll
if x in rolling_rules.reroll_once:
Expand All @@ -287,7 +318,10 @@ static func base_rule_roller(rolling_rules:Dictionary,rng:RandomNumberGenerator
for d in dice:
var com_result = d
var x = d
while x in rolling_rules.compound:
var explode_count = 1
while x in rolling_rules.compound and \
(rolling_rules.explode_depth == 0 or explode_count<rolling_rules.explode_depth):
explode_count += 1
x = al.sample(possible_dice,1,rng)[0]
if x in rolling_rules.reroll_once:
x = al.sample(possible_dice,1,rng)[0]
Expand Down Expand Up @@ -373,6 +407,11 @@ static func base_calc_rule_probs(rules:Dictionary,explode_depth:int = 3)->Dictio
new_probs[[x]] = probs[x]
probs = new_probs

if rules.explode_depth != 0:
print(explode_depth)
print(rules.explode_depth)
print(rules)
explode_depth = min(explode_depth,rules.explode_depth)

if rules.explode.size()>0:
probs = dh.blow_up(probs,rules.explode, explode_depth)
Expand Down
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ config_version=5
[application]

config/name="dice_syntax_gd"
config/features=PackedStringArray("4.1")
config/features=PackedStringArray("4.2")
config/icon="res://icon.png"

[editor_plugins]
Expand Down
9 changes: 9 additions & 0 deletions tests/test_dice.gd
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func test_dice_mean():

m_roll = mean_tester("1d2s2f1")
assert_between(m_roll,-0.1,.1,'success fail')

m_roll = mean_tester("1d2!/!3")
assert_between(m_roll,2.6,2.7,'success fail')


func test_probs():
Expand All @@ -90,3 +93,9 @@ func test_probs():

probs = dice_syntax.dice_probs('1d2s2f1')
assert_true(dice_syntax.expected_value(probs) == 0)

probs = dice_syntax.dice_probs('1d2!/!3',10)
assert_true(dice_syntax.expected_value(probs) == 2.625)

probs = dice_syntax.dice_probs('1d2!/!10',10)
assert_true(dice_syntax.expected_value(probs) > 2.9)

0 comments on commit 22266ec

Please sign in to comment.