diff options
author | rodri <rgl@antares-labs.eu> | 2023-09-01 21:41:09 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2023-09-01 21:41:09 +0000 |
commit | 2723bb6ff3e339f7f56a6276fa1c7c9f28d63871 (patch) | |
tree | 5398980304f82a0332fb7f846a4d9f9e90785f5a | |
parent | a764a5069421930c9261f87f02b0b4db3e80783b (diff) | |
download | battleship-2723bb6ff3e339f7f56a6276fa1c7c9f28d63871.tar.gz battleship-2723bb6ff3e339f7f56a6276fa1c7c9f28d63871.tar.bz2 battleship-2723bb6ff3e339f7f56a6276fa1c7c9f28d63871.zip |
obtain each player's username and show it during battle.
-rw-r--r-- | bts.c | 23 | ||||
-rw-r--r-- | btsd.c | 20 | ||||
-rw-r--r-- | dat.h | 1 |
3 files changed, 35 insertions, 9 deletions
@@ -65,6 +65,7 @@ Cursor aimcursor = { }; char deffont[] = "/lib/font/bit/pelm/unicode.9.font"; char winspec[32]; +char uid[8+1], oid[8+1]; Channel *drawchan; Channel *ingress, *egress; Mousectl *mctl; /* only used to update the cursor */ @@ -172,6 +173,7 @@ resetgame(void) } curship = nil; layoutdone = 0; + oid[0] = 0; } Point @@ -272,11 +274,15 @@ drawinfo(Image *dst) s = "TARGET"; p = subpt(alienboard.bbox.min, Pt(font->width+2,0)); vstring(dst, p, display->white, ZP, font, s); - s = "LOCAL"; p = Pt(localboard.bbox.max.x+2, localboard.bbox.min.y); vstring(dst, p, display->white, ZP, font, s); + p = Pt(alienboard.bbox.max.x+2, alienboard.bbox.min.y); + vstring(dst, p, display->white, ZP, font, oid); + p = subpt(localboard.bbox.min, Pt(font->width+2,0)); + vstring(dst, p, display->white, ZP, font, uid); + if(game.state == Outlaying){ if(c == nil) c = eallocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellow); @@ -716,7 +722,8 @@ processcmd(char *cmd) if(strcmp(cmd, "layout") == 0){ game.state = Outlaying; curship = &armada[0]; - } + }else if(strcmp(cmd, "id") == 0) + chanprint(egress, "id %s\n", uid); csetcursor(mctl, &patrolcursor); break; case Outlaying: @@ -725,6 +732,8 @@ processcmd(char *cmd) csetcursor(mctl, &waitcursor); }else if(strcmp(cmd, "play") == 0) game.state = Playing; + else if(strncmp(cmd, "oid", 3) == 0) + snprint(oid, sizeof oid, "%s", cmd+4); break; case Playing: if(strcmp(cmd, "wait") == 0){ @@ -812,6 +821,13 @@ void threadmain(int argc, char *argv[]) { char *addr; + /* + * TODO + * if it's not static it messes with in.mc->xy later, probably + * because of an stack overflow somewhere. have to investigate + * with wpset("w", &in.mc->xy, sizeof(Point*)); in acid(1) + */ + static char *user; int fd; Input in; @@ -849,6 +865,9 @@ threadmain(int argc, char *argv[]) unlockdisplay(display); mctl = in.mc; + if((user = getenv("user")) == nil) + user = getuser(); + snprint(uid, sizeof uid, "%s", user); screenb = eallocimage(display, rectsubpt(screen->r, screen->r.min), screen->chan, 0, DNofill); worldrf.p = Pt2(0,0,1); @@ -139,11 +139,8 @@ battleproc(void *arg) threadcreate(netrecvthread, &cp[0], mainstacksize); threadcreate(netrecvthread, &cp[1], mainstacksize); - /* TODO ask for the username */ - write(m->pl[0]->fd, "layout\n", 7); - write(m->pl[1]->fd, "layout\n", 7); - m->pl[0]->state = Outlaying; - m->pl[1]->state = Outlaying; + write(m->pl[0]->fd, "id\n", 3); + write(m->pl[1]->fd, "id\n", 3); while((i = alt(a)) >= 0){ p = m->pl[i]; @@ -161,6 +158,17 @@ battleproc(void *arg) fprint(2, "[%d] said '%s'\n", i, s); switch(p->state){ + case Waiting0: + if(strncmp(s, "id", 2) == 0){ + snprint(p->name, sizeof p->name, "%s", strlen(s) > 3? s+3: "???"); + write(p->fd, "layout\n", 7); + p->state = Outlaying; + if(op->state == Outlaying){ + fprint(p->fd, "oid %s\n", op->name); + fprint(op->fd, "oid %s\n", p->name); + } + } + break; case Outlaying: if(strncmp(s, "layout", 6) == 0) if(gettokens(s+7, coords, nelem(coords), ",") == nelem(coords)){ @@ -173,8 +181,6 @@ battleproc(void *arg) settiles(p, cell, orient, shiplen(j), Tship); } p->state = Waiting; - if(debug) - fprint(2, "curstates [%d] %d / [%d] %d\n", i, p->state, i^1, op->state); if(op->state == Waiting){ if(debug){ fprint(2, "map%d:\n", i); @@ -71,6 +71,7 @@ struct Board struct Player { Map; + char name[8+1]; int fd; int sfd; int state; |