From 353cbde75a19b004ab0c5339684f33ea47ef1244 Mon Sep 17 00:00:00 2001 From: rodri Date: Sat, 7 Oct 2023 17:17:30 +0000 Subject: use a better random source. fix a bug with andys generating oob cell coordinates. --- util.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index da5e8b0..1ff9a42 100644 --- a/util.c +++ b/util.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include #include @@ -33,13 +35,19 @@ static char *statenametab[] = { }; +int +isoob(Point2 cell) +{ + return cell.x < 0 || cell.x >= MAPW || + cell.y < 0 || cell.y >= MAPH; +} + char * cell2coords(Point2 cell) { static char s[3+1]; - assert(cell.x >= 0 && cell.x < MAPW - && cell.y >= 0 && cell.y < MAPH); + assert(!isoob(cell)); snprint(s, sizeof s, "%c%d", rowtab[(int)cell.y], (int)cell.x); return s; @@ -72,6 +80,7 @@ gettile(Map *m, Point2 cell) void settile(Map *m, Point2 cell, int type) { + assert(!isoob(cell)); m->map[(int)cell.x][(int)cell.y] = type; } @@ -84,6 +93,7 @@ settiles(Map *m, Point2 cell, int o, int ncells, int type) sv = o == OH? Vec2(1,0): Vec2(0,1); while(ncells-- > 0){ + assert(!isoob(cell)); settile(m, cell, type); cell = addpt2(cell, sv); } @@ -211,3 +221,17 @@ chanvprint(Channel *c, char *fmt, va_list arg) yield(); /* let recipient handle message immediately */ return n; } + +ulong +getrand(ulong max) +{ + mpint *n, *r; + ulong c; + + n = uitomp(max, nil); + r = mpnrand(n, genrandom, nil); + c = mptoui(r); + mpfree(n); + mpfree(r); + return c; +} -- cgit v1.2.3