From b1944fc9205c72ac94cc87271bd1d4a16318f4a7 Mon Sep 17 00:00:00 2001 From: gumi Date: Sun, 29 Jul 2018 13:13:56 -0400 Subject: fix some edge cases in relative_array_random() --- npc/functions/RNGesus.txt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'npc') diff --git a/npc/functions/RNGesus.txt b/npc/functions/RNGesus.txt index 7d2ff7c7..6883f8f1 100644 --- a/npc/functions/RNGesus.txt +++ b/npc/functions/RNGesus.txt @@ -44,9 +44,9 @@ function script relative_array_random { for (.@i = .@initial_index + 1; .@i < .@size; .@i += 2) { if (.@is_str) { - .@total_prob += atoi(getelementofarray(getarg(0), .@i)); + .@total_prob += max(1, atoi(getelementofarray(getarg(0), .@i))); } else { - .@total_prob += getelementofarray(getarg(0), .@i); + .@total_prob += max(1, getelementofarray(getarg(0), .@i)); } } @@ -54,7 +54,7 @@ function script relative_array_random { set(getelementofarray(getarg(0), 0), .@total_prob); } - .@target_sum = rand(.@total_prob); + .@target_sum = rand(0, .@total_prob); for (.@i = .@initial_index; .@sum < .@target_sum; .@i += 2) { if (.@is_str) { @@ -62,6 +62,10 @@ function script relative_array_random { } else { .@sum += getelementofarray(getarg(0), .@i + 1); } + + if (.@sum >= .@target_sum) { + break; + } } freeloop(false); -- cgit v1.2.3-70-g09d2