shithub: battleship

Download patch

ref: 2723bb6ff3e339f7f56a6276fa1c7c9f28d63871
parent: a764a5069421930c9261f87f02b0b4db3e80783b
author: rodri <rgl@antares-labs.eu>
date: Fri Sep 1 17:41:09 EDT 2023

obtain each player's username and show it during battle.

--- a/bts.c
+++ b/bts.c
@@ -65,6 +65,7 @@
 };
 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 @@
 	}
 	curship = nil;
 	layoutdone = 0;
+	oid[0] = 0;
 }
 
 Point
@@ -272,11 +274,15 @@
 	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 @@
 		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 @@
 			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 @@
 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 @@
 	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);
--- a/btsd.c
+++ b/btsd.c
@@ -139,11 +139,8 @@
 	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 @@
 			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 @@
 						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);
--- a/dat.h
+++ b/dat.h
@@ -71,6 +71,7 @@
 struct Player
 {
 	Map;
+	char name[8+1];
 	int fd;
 	int sfd;
 	int state;