shithub: battleship

Download patch

ref: ca3289c29673b914d8ca64b08954ef0afccdf3c9
parent: 3d638eb8e9f753d7766cba6efb17320597b3394b
author: rodri <rgl@antares-labs.eu>
date: Mon Oct 9 17:01:52 EDT 2023

replace the andy layout algorithm for a better one.

--- a/andy.c
+++ b/andy.c
@@ -68,33 +68,20 @@
 }
 
 static int
-between(double n, double min, double max)
-{
-	return n >= min && n < max;
-}
-
-static int
 lineXline(Point2 min0, Point2 max0, Point2 min1, Point2 max1)
 {
-	double a₁, b₁;
-	double a₂, b₂;
-	double det;
+	if(min0.x == max0.x)
+		max0.x++;
+	else if(min0.y == max0.y)
+		max0.y++;
 
-	a₁ = max0.y - min0.y;
-	b₁ = min0.x - max0.x;
+	if(min1.x == max1.x)
+		max1.x++;
+	else if(min1.y == max1.y)
+		max1.y++;
 
-	a₂ = max1.y - min1.y;
-	b₂ = min1.x - max1.x;
-
-	det = a₁*b₂ - a₂*b₁;
-	if(det == 0){
-		/* do they overlap? */
-		if((min0.x == min1.x && (between(min0.y, min1.y, max1.y) || between(max0.y, min1.y, max1.y))) ||
-			(min0.y == min1.y && (between(min0.x, min1.x, max1.x) || between(max0.x, min1.x, max1.x))))
-			return 1;
-		return 0;
-	}
-	return 1;
+	return min0.x < max1.x && min1.x < max0.x &&
+	       min0.y < max1.y && min1.y < max0.y;
 }
 
 static void
@@ -107,7 +94,7 @@
 	for(i = 0; i < NSHIPS; i++){
 Retry:
 		cells[i] = Pt2(getrand(MAPW-shiplen(i)), getrand(MAPH-shiplen(i)), 1);
-		o[i] = getrand(1)? OH: OV;
+		o[i] = i > 1 && o[i-1] != OH? OH: OV;
 		sv[i] = o[i] == OH? Vec2(1,0): Vec2(0,1);
 		for(j = 0; j < i; j++)
 			if(lineXline(cells[i], addpt2(cells[i], mulpt2(sv[i], shiplen(i))),
--- a/bts.c
+++ b/bts.c
@@ -641,7 +641,6 @@
 
 		audio_play(playlist[SCANNON]);
 		cell = toboard(&alienboard, mc->xy);
-		/* TODO check if we already shot at that cell */
 		cell2coords(buf, sizeof buf, cell);
 		if(gettile(&alienboard, cell) == Twater){
 			chanprint(egress, "shoot %s\n", buf);
@@ -986,7 +985,7 @@
 soundproc(void *)
 {
 	Biobuf *aout;
-	uchar adata[512];
+	uchar buf[1024];
 
 	threadsetname("soundproc");
 
@@ -995,8 +994,8 @@
 		sysfatal("Bopen: %r");
 
 	for(;;){
-		audio_process((void*)adata, sizeof(adata)/2);
-		Bwrite(aout, adata, sizeof adata);
+		audio_process((void*)buf, sizeof(buf)/2);
+		Bwrite(aout, buf, sizeof buf);
 	}
 }