diff options
author | rodri <rgl@antares-labs.eu> | 2023-10-07 22:33:25 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2023-10-07 22:33:25 +0000 |
commit | 71c2cd61f6e53a455140f5920cff88cdfd4b4d33 (patch) | |
tree | 14983dd6f41f359cbe143dd3dc2bb55691037e99 | |
parent | c5ca61912a88aeb8ae727c0d217b7dc41efeb00d (diff) | |
download | battleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.tar.gz battleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.tar.bz2 battleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.zip |
make cell2coords thread-safe.
-rw-r--r-- | andy.c | 4 | ||||
-rw-r--r-- | bts.c | 8 | ||||
-rw-r--r-- | btsd.c | 11 | ||||
-rw-r--r-- | fns.h | 2 | ||||
-rw-r--r-- | util.c | 16 |
5 files changed, 22 insertions, 19 deletions
@@ -61,6 +61,7 @@ static void andy_shoot(Andy *a, Msg *m) { Point2 cell; + char buf[3+1]; Retry: switch(a->state){ @@ -86,7 +87,8 @@ Retry: } break; } - m->body = smprint("shoot %s", cell2coords(cell)); + cell2coords(buf, sizeof buf, cell); + m->body = smprint("shoot %s", buf); sendp(a->ego->battle->data, m); a->lastshot = cell; } @@ -617,6 +617,7 @@ void lmb(Mousectl *mc) { Point2 cell; + char buf[3+1]; if(conclusion.s != nil) return; @@ -641,7 +642,8 @@ lmb(Mousectl *mc) audio_play(playlist[SCANNON]); cell = toboard(&alienboard, mc->xy); /* TODO check if we already shot at that cell */ - chanprint(egress, "shoot %s\n", cell2coords(cell)); + cell2coords(buf, sizeof buf, cell); + chanprint(egress, "shoot %s\n", buf); lastshot = cell; break; } @@ -721,8 +723,8 @@ rmb(Mousectl *mc) assert(sizeof buf - n > 1+3+1); if(i != 0) buf[n++] = ','; - n += snprint(buf+n, sizeof buf - n, "%s%c", - cell2coords(armada[i].p), armada[i].orient == OH? 'h': 'v'); + n += cell2coords(buf+n, sizeof buf - n, armada[i].p); + buf[n++] = armada[i].orient == OH? 'h': 'v'; } chanprint(egress, "layout %s\n", buf); layoutdone++; @@ -422,6 +422,7 @@ battleproc(void *arg) Player *p, *op; Stands stands; /* TODO make this a member of Match */ uchar buf[BY2MAP]; + char cbuf[3+1]; uint n0; Point2 cell; @@ -499,8 +500,9 @@ battleproc(void *arg) case Tship: settile(op, cell, Thit); chanprint(p->io.out, "hit\n"); - chanprint(op->io.out, "hit %s\n", cell2coords(cell)); - broadcast(&stands, "hit %d %s\n", p == m->pl[0]? 0: 1, cell2coords(cell)); + cell2coords(cbuf, sizeof cbuf, cell); + chanprint(op->io.out, "hit %s\n", cbuf); + broadcast(&stands, "hit %d %s\n", p == m->pl[0]? 0: 1, cbuf); if(countshipcells(op) < (debug? 12: 1)){ chanprint(p->io.out, "win\n"); chanprint(op->io.out, "lose\n"); @@ -516,8 +518,9 @@ battleproc(void *arg) case Twater: settile(op, cell, Tmiss); chanprint(p->io.out, "miss\n"); - chanprint(op->io.out, "miss %s\n", cell2coords(cell)); - broadcast(&stands, "miss %d %s\n", p == m->pl[0]? 0: 1, cell2coords(cell)); + cell2coords(cbuf, sizeof cbuf, cell); + chanprint(op->io.out, "miss %s\n", cbuf); + broadcast(&stands, "miss %d %s\n", p == m->pl[0]? 0: 1, cbuf); Swapturn: chanprint(p->io.out, "wait\n"); chanprint(op->io.out, "play\n"); @@ -12,7 +12,7 @@ Image *eallocimage(Display*, Rectangle, ulong, int, ulong); * util */ int isoob(Point2); -char *cell2coords(Point2); +int cell2coords(char*, ulong, Point2); Point2 coords2cell(char*); int gettile(Map*, Point2); void settile(Map*, Point2, int); @@ -10,7 +10,7 @@ #include "dat.h" #include "fns.h" -static char rowtab[] = "abcdefghijklmnopq"; +static char rowtab[] = "abcdefghijklmnopq"; /* |rowtab| = MAPH */ static int shiplentab[] = { [Scarrier] 5, [Sbattleship] 4, @@ -42,15 +42,11 @@ isoob(Point2 cell) cell.y < 0 || cell.y >= MAPH; } -char * -cell2coords(Point2 cell) +int +cell2coords(char *buf, ulong len, Point2 cell) { - static char s[3+1]; - - assert(!isoob(cell)); - - snprint(s, sizeof s, "%c%d", rowtab[(int)cell.y], (int)cell.x); - return s; + assert(len >= 3+1 && !isoob(cell)); + return snprint(buf, len, "%c%d", rowtab[(int)cell.y], (int)cell.x); } Point2 @@ -66,7 +62,7 @@ coords2cell(char *s) cell.y = p-rowtab; cell.x = strtol(s+1, nil, 10); - assert(cell.x >= 0 && cell.x < MAPW); + assert(!isoob(cell)); return cell; } |