summary refs log tree commit diff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig
index f6a2353..8cdfb6e 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -16,6 +16,7 @@
 // You should have received a copy of the GNU General Public License
 // along with Black Shades.  If not, see <https://www.gnu.org/licenses/>.
 
+const DefaultPrng = std.rand.DefaultPrng;
 const allocator = std.heap.c_allocator;
 const free = std.c.free;
 const std = @import("std");
@@ -33,6 +34,7 @@ const loadScores = misc.loadScores;
 const saveScores = misc.saveScores;
 
 var game: *c.Game = undefined;
+var prng: DefaultPrng = undefined;
 
 fn resizeWindow(window: gf.Window, width: c_int, height: c_int) void {
     c.resizeWindow(game, width, height);
@@ -52,6 +54,23 @@ fn click(window: gf.Window, button: gf.MouseButton,
     c.click(game, @enumToInt(button), @enumToInt(action), mods.toInt());
 }
 
+/// Return a floating point value evenly distributed in the range [0, 1).
+export fn randFloat() f32 {
+    return prng.random.float(f32);
+}
+
+/// Return a random integer i where at_least <= i <= at_most.
+/// The results of this function may be biased.
+export fn randInt(at_least: i32, at_most: i32) i32 {
+    return prng.random.intRangeAtMostBiased(i32, at_least, at_most);
+}
+
+/// Return a random integer i where 0 <= i < less_than.
+/// The results of this function may be biased.
+export fn randUint(less_than: u32) u32 {
+    return prng.random.uintLessThanBiased(u32, less_than);
+}
+
 pub fn main() !void {
     const loca = try Loca.init(allocator, .{});
     defer loca.deinit();
@@ -64,6 +83,7 @@ pub fn main() !void {
                                         "Black Shades", .{}, .{});
     try window.makeCurrent();
 
+    prng = DefaultPrng.init(@floatToInt(u64, try gf.getTime()));
     game = c.makeGame(@bitCast(c.Config, config),
                       @bitCast(c.Scores, try loadScores(loca.user_data))).?;
     defer saveScores(loca.user_data, @bitCast(Scores, c.getScores(game)))