ref: 71c2cd61f6e53a455140f5920cff88cdfd4b4d33
parent: c5ca61912a88aeb8ae727c0d217b7dc41efeb00d
author: rodri <rgl@antares-labs.eu>
date: Sat Oct 7 18:33:25 EDT 2023
make cell2coords thread-safe.
--- a/andy.c
+++ b/andy.c
@@ -61,6 +61,7 @@
andy_shoot(Andy *a, Msg *m)
{
Point2 cell;
+ char buf[3+1];
Retry:
switch(a->state){
@@ -86,7 +87,8 @@
}
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;
}
--- a/bts.c
+++ b/bts.c
@@ -617,6 +617,7 @@
lmb(Mousectl *mc)
{
Point2 cell;
+ char buf[3+1];
if(conclusion.s != nil)
return;
@@ -641,7 +642,8 @@
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 @@
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++;
--- a/btsd.c
+++ b/btsd.c
@@ -422,6 +422,7 @@
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 @@
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 @@
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");
--- a/fns.h
+++ b/fns.h
@@ -12,7 +12,7 @@
* util
*/
int isoob(Point2);
-char *cell2coords(Point2);
+int cell2coords(char*, ulong, Point2);
Point2 coords2cell(char*);
int gettile(Map*, Point2);
void settile(Map*, Point2, int);
--- 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 @@
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 @@
cell.y = p-rowtab;
cell.x = strtol(s+1, nil, 10);
- assert(cell.x >= 0 && cell.x < MAPW);
+ assert(!isoob(cell));
return cell;
}