shithub: battleship

Download patch

ref: d9752f9750402f01386dceb3606b6cba7a675155
parent: ca4af600fdad77b3c4603a62c81eee51187fafcf
author: rodri <rgl@antares-labs.eu>
date: Sat Aug 26 16:16:06 EDT 2023

show the current state and keep ships from overlapping.

--- a/bts.c
+++ b/bts.c
@@ -59,6 +59,17 @@
 	return np;
 }
 
+int
+rectXarmada(Rectangle r)
+{
+	int i;
+
+	for(i = 0; i < nelem(armada); i++)
+		if(curship != &armada[i] && rectXrect(r, armada[i].bbox))
+			return 1;
+	return 0;
+}
+
 Rectangle
 mkshipbbox(Point2 p, int o, int ncells)
 {
@@ -114,6 +125,9 @@
 	Point2 p, sv;
 	int i;
 
+	if(!rectinrect(s->bbox, localboard.bbox))
+		return;
+
 	p = s->p;
 	switch(s->orient){
 	case OH: sv = Vec2(1,0); break;
@@ -147,6 +161,25 @@
 }
 
 void
+drawinfo(Image *dst)
+{
+	Point p;
+	char *s;
+
+	s = nil;
+	switch(game.state){
+	case Waiting0: s = "looking for players"; break;
+	case Outlaying: s = "place the fleet"; break;
+	case Waiting: s = "wait for your turn"; break;
+	case Playing: s = "your turn"; break;
+	}
+	if(s == nil)
+		return;
+	p = Pt(SCRW/2 - stringwidth(font, s)/2, SCRH-Boardmargin);
+	stringbg(dst, p, display->white, ZP, font, s, display->black, ZP);
+}
+
+void
 redraw(void)
 {
 	lockdisplay(display);
@@ -155,6 +188,7 @@
 	drawboard(screenb, &alienboard);
 	drawboard(screenb, &localboard);
 	drawships(screenb);
+	drawinfo(screenb);
 
 	draw(screen, screen->r, screenb, nil, ZP);
 
@@ -261,7 +295,7 @@
 		case Sdestroyer: s->ncells = 2; break;
 		default: sysfatal("initarmada: unknown ship: %d", i);
 		}
-		s->orient = OH;
+		s->orient = OV;
 		s->hit = emalloc(s->ncells*sizeof(int));
 		memset(s->hit, 0, s->ncells*sizeof(int));
 		s->sunk = 0;
@@ -386,7 +420,6 @@
 		if(!confirmdone(mc))
 			break;
 
-		buf[0] = 0;
 		n = 0;
 		for(i = 0; i < nelem(armada); i++){
 			assert(sizeof buf - n > 1+3+1);
@@ -411,7 +444,7 @@
 	if(game.state == Outlaying && curship != nil){
 		newbbox = mkshipbbox(toboard(&localboard, mc->xy), curship->orient, curship->ncells);
 
-		if(rectinrect(newbbox, localboard.bbox)){
+		if(rectinrect(newbbox, localboard.bbox) && !rectXarmada(newbbox)){
 			curship->p = toboard(&localboard, mc->xy);
 			curship->bbox = newbbox;
 		}
@@ -442,7 +475,7 @@
 }
 
 void
-painter(void *)
+bobross(void *)
 {
 	while(recv(drawchan, nil) > 0)
 		redraw();
@@ -486,8 +519,6 @@
 	char buf[256], *coords[2];
 	int n, fd;
 
-	threadsetname("netrecvthread");
-
 	fd = *(int*)arg;
 	io = ioproc();
 
@@ -542,8 +573,6 @@
 	char *s;
 	int fd;
 
-	threadsetname("netsendthread");
-
 	fd = *(int*)arg;
 
 	while(recv(egress, &s) > 0){
@@ -616,7 +645,7 @@
 	drawchan = chancreate(sizeof(void*), 0);
 	ingress = chancreate(sizeof(char*), 16);
 	egress = chancreate(sizeof(char*), 16);
-	threadcreate(painter, nil, mainstacksize);
+	threadcreate(bobross, nil, mainstacksize);
 	threadcreate(inputthread, &in, mainstacksize);
 	threadcreate(netrecvthread, &fd, mainstacksize);
 	threadcreate(netsendthread, &fd, mainstacksize);