aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-10-07 22:33:25 +0000
committerrodri <rgl@antares-labs.eu>2023-10-07 22:33:25 +0000
commit71c2cd61f6e53a455140f5920cff88cdfd4b4d33 (patch)
tree14983dd6f41f359cbe143dd3dc2bb55691037e99
parentc5ca61912a88aeb8ae727c0d217b7dc41efeb00d (diff)
downloadbattleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.tar.gz
battleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.tar.bz2
battleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.zip
make cell2coords thread-safe.
-rw-r--r--andy.c4
-rw-r--r--bts.c8
-rw-r--r--btsd.c11
-rw-r--r--fns.h2
-rw-r--r--util.c16
5 files changed, 22 insertions, 19 deletions
diff --git a/andy.c b/andy.c
index 17184a2..468968e 100644
--- a/andy.c
+++ b/andy.c
@@ -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;
}
diff --git a/bts.c b/bts.c
index f446c2f..ca951dc 100644
--- a/bts.c
+++ b/bts.c
@@ -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++;
diff --git a/btsd.c b/btsd.c
index 4fb4019..f804c39 100644
--- a/btsd.c
+++ b/btsd.c
@@ -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");
diff --git a/fns.h b/fns.h
index f8473fd..38aa481 100644
--- a/fns.h
+++ b/fns.h
@@ -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);
diff --git a/util.c b/util.c
index 1ff9a42..cedd89e 100644
--- a/util.c
+++ b/util.c
@@ -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;
}