shithub: battleship

Download patch

ref: be547a2dba10292fb3191bc5fec42212d2391660
parent: 3d93e42d0e854a3bb7c3a380a0635cf927d59ab7
author: rodri <rgl@antares-labs.eu>
date: Wed Aug 30 18:07:50 EDT 2023

forbid placing ships after being done with the layout.
show the current ship and its length during placement.
also made changes to some of the info banners.

--- a/bts.c
+++ b/bts.c
@@ -80,6 +80,7 @@
 
 struct {
 	int state;
+	int layoutdone;
 } game;
 
 struct {
@@ -158,6 +159,23 @@
 	oc = c;
 }
 
+void
+resetgame(void)
+{
+	int i;
+
+	memset(localboard.map, Twater, MAPW*MAPH);
+	memset(alienboard.map, Twater, MAPW*MAPH);
+	for(i = 0; i < nelem(armada); i++){
+		armada[i].bbox = ZR;
+		memset(armada[i].hit, 0, armada[i].ncells*sizeof(int));
+		armada[i].sunk = 0;
+	}
+	curship = nil;
+	game.state = Waiting0;
+	game.layoutdone = 0;
+}
+
 Image *
 gettileimage(int type)
 {
@@ -224,8 +242,9 @@
 void
 drawinfo(Image *dst)
 {
+	static Image *c;
 	Point p;
-	char *s;
+	char *s, aux[32];
 
 	s = nil;
 	switch(game.state){
@@ -236,8 +255,22 @@
 	}
 	if(s == nil)
 		return;
-	p = Pt(SCRW/2 - stringwidth(font, s)/2, SCRH-Boardmargin);
+	p = Pt(SCRW/2 - stringwidth(font, s)/2, 0);
 	string(dst, p, display->white, ZP, font, s);
+
+	if(game.state == Outlaying){
+		if(c == nil)
+			c = eallocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellow);
+		if(curship != nil){
+			snprint(aux, sizeof aux, "%s (%d)", shipname(curship-armada), curship->ncells);
+			p = Pt(SCRW/2 - stringwidth(font, aux)/2, SCRH-Boardmargin);
+			string(dst, p, c, ZP, font, aux);
+		}else{
+			s = "done with the layout?";
+			p = Pt(SCRW/2 - stringwidth(font, s)/2, SCRH-Boardmargin);
+			string(dst, p, c, ZP, font, s);
+		}
+	}
 }
 
 void
@@ -252,7 +285,7 @@
 	drawinfo(screenb);
 
 	if(conclusion.s != nil)
-		string(screenb, Pt(SCRW/2 - stringwidth(font, conclusion.s)/2, 0), conclusion.c, ZP, font, conclusion.s);
+		string(screenb, Pt(SCRW/2 - stringwidth(font, conclusion.s)/2, font->height+5), conclusion.c, ZP, font, conclusion.s);
 
 	draw(screen, screen->r, screenb, nil, ZP);
 
@@ -260,8 +293,9 @@
 	unlockdisplay(display);
 
 	if(conclusion.s != nil){
-		sleep(5000);
+		resetgame();
 		conclusion.s = nil;
+		sleep(5000);
 		redraw();
 	}
 }
@@ -374,21 +408,6 @@
 	}
 }
 
-void
-resetgame(void)
-{
-	int i;
-
-	memset(localboard.map, Twater, MAPW*MAPH);
-	memset(alienboard.map, Twater, MAPW*MAPH);
-	for(i = 0; i < nelem(armada); i++){
-		armada[i].bbox = ZR;
-		memset(armada[i].hit, 0, armada[i].ncells*sizeof(int));
-		armada[i].sunk = 0;
-	}
-	curship = nil;
-}
-
 int
 confirmdone(Mousectl *mc)
 {
@@ -527,10 +546,11 @@
 	mc->xy = addpt(mc->xy, screen->r.min);
 	switch(menuhit(3, mc, &menu, _screen)){
 	case PLACESHIP:
-		curship = &armada[0];
+		if(!game.layoutdone)
+			curship = &armada[0];
 		break;
 	case DONE:
-		if(curship != nil)
+		if(curship != nil || game.layoutdone)
 			break;
 
 		if(!confirmdone(mc))
@@ -545,6 +565,7 @@
 				cell2coords(armada[i].p), armada[i].orient == OH? 'h': 'v');
 		}
 		chanprint(egress, "layout %s\n", buf);
+		game.layoutdone++;
 		break;
 	}
 	send(drawchan, nil);
@@ -672,15 +693,10 @@
 	if(debug)
 		fprint(2, "rcvd '%s'\n", cmd);
 
-	if(strcmp(cmd, "win") == 0){
+	if(strcmp(cmd, "win") == 0)
 		celebrate();
-		resetgame();
-		game.state = Waiting0;
-	}else if(strcmp(cmd, "lose") == 0){
+	else if(strcmp(cmd, "lose") == 0)
 		keelhaul();
-		resetgame();
-		game.state = Waiting0;
-	}
 
 	switch(game.state){
 	case Waiting0:
--- a/btsd.c
+++ b/btsd.c
@@ -252,6 +252,10 @@
 	threadsetname("matchmaker");
 
 	for(;;){
+		/*
+		 * TODO make fairer matches
+		 * locking playerq while checking nplayers and popping the couple.
+		 */
 		if(playerq.nplayers < 2){
 			sleep(100);
 			continue;
--- a/fns.h
+++ b/fns.h
@@ -18,3 +18,4 @@
 void fprintmap(int, Map*);
 int countshipcells(Map*);
 int shiplen(int);
+char *shipname(int);
--- a/util.c
+++ b/util.c
@@ -16,6 +16,13 @@
  [Ssubmarine]	3,
  [Sdestroyer]	2,
 };
+static char *shipnametab[] = {
+ [Scarrier]	"carrier",
+ [Sbattleship]	"battleship",
+ [Scruiser]	"cruiser",
+ [Ssubmarine]	"submarine",
+ [Sdestroyer]	"destroyer",
+};
 
 
 char *
@@ -109,6 +116,15 @@
 int
 shiplen(int stype)
 {
-	assert(stype >= 0 && stype < NSHIPS);
+	if(stype < 0 || stype >= NSHIPS)
+		return -1;
 	return shiplentab[stype];
+}
+
+char *
+shipname(int stype)
+{
+	if(stype < 0 || stype >= NSHIPS)
+		return nil;
+	return shipnametab[stype];
 }