Skip to content

Commit

Permalink
Added int32_abs () to Utils.
Browse files Browse the repository at this point in the history
Made Dice.roll () use uint32 as it's return value. It can never be negative, so...
  • Loading branch information
gegoxaren committed Jun 1, 2022
1 parent b25d47f commit a12a0cc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
10 changes: 4 additions & 6 deletions src/libvqdr/dice.vala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class VQDR.Expression.Dice {
this.modifier = 0;
}

public int32 roll () {
public uint32 roll () {
if (faces <= 0) {
return modifier;
}
Expand All @@ -51,14 +51,12 @@ public class VQDR.Expression.Dice {
}

if (faces == 1) {
return ((int32) times) + modifier;
return times + modifier;
}

int32 retval = modifier;
uint32 retval = modifier;
for (size_t i = 1; i <= times; i++) {
// we have to do this ugly mess, as int32 is missing the abs () method.
// bug: https://gitlab.gnome.org/GNOME/vala/-/issues/1328
int32 r = ((int)(Utils.Random.get_static_int () % (int)faces)).abs ();
uint32 r = Utils.Random.get_static_int () % faces;

retval += r;
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/
[CCode (cname = "V", cprefix = "v_")]
namespace Utils {
// int32 is missinng the abs method. This will have to do for the time being.
// BUG: https://gitlab.gnome.org/GNOME/vala/-/issues/1328
static int32 int32_abs (int32 N) { return ((N < 0) ? ( -N ) : (N)); }

[CCode (cname = "v_str_cmp")]
public int str_cmp (string a, string b) {
return a.collate (b);
Expand Down
4 changes: 2 additions & 2 deletions tests/random-d6.vala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void d6_test () {
int32 count[7] = {0};
int32 rolls = 1000000;
for (size_t i = 0; i < rolls; i++) {
int32 r = d.roll ();
uint32 r = d.roll ();

count[r] += 1;
}
Expand All @@ -60,7 +60,7 @@ void d6_test () {
int32 count[7] = {0};
int32 rolls = 1000000;
for (size_t i = 0; i < rolls; i++) {
int32 r = d.roll ();
uint32 r = d.roll ();

count[r] += 1;
}
Expand Down

0 comments on commit a12a0cc

Please sign in to comment.