changeset 31:25e473a82ce3

Joo
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 10 Dec 2006 20:10:38 +0000
parents 2eceda1c86ab
children c41ec03eac99
files maputils.c maputils.h mkmap.c mkspecial.c
diffstat 4 files changed, 116 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/maputils.c	Sun Dec 10 02:38:23 2006 +0000
+++ b/maputils.c	Sun Dec 10 20:10:38 2006 +0000
@@ -286,7 +286,7 @@
 }
 
 
-DINT putBlock(mapblock_t **map, mapblock_t *b, DINT ox, DINT oy)
+DINT putBlock(mapblock_t **map, mapblock_t *b, DINT ox, DINT oy, DINT q)
 {
 	mapblock_t *tmp;
 	DINT x0, y0, x1, y1, mx, my;
--- a/maputils.h	Sun Dec 10 02:38:23 2006 +0000
+++ b/maputils.h	Sun Dec 10 20:10:38 2006 +0000
@@ -66,6 +66,6 @@
 mapblock_t *	parseFile(DCHAR *);
 void		printBlock(FILE *, mapblock_t *);
 DINT		putBlockDo(mapblock_t *, mapblock_t *, DINT, DINT);
-DINT		putBlock(mapblock_t **, mapblock_t *, DINT, DINT);
+DINT		putBlock(mapblock_t **, mapblock_t *, DINT, DINT, DINT);
 
 #endif
--- a/mkmap.c	Sun Dec 10 02:38:23 2006 +0000
+++ b/mkmap.c	Sun Dec 10 20:10:38 2006 +0000
@@ -419,7 +419,7 @@
 		if (!mapBlocks[i]->mark) {
 			if (blockSearch(map, mapBlocks[i], &ox, &oy, mv)) {
 				THPRINT(2, "X");
-				if (putBlock(&map, mapBlocks[i], ox, oy) < 0) {
+				if (putBlock(&map, mapBlocks[i], ox, oy, 15) < 0) {
 					THERR("putBlock(%d, %d, %d) failed!\n",
 						ox, oy, i);
 				}
--- a/mkspecial.c	Sun Dec 10 02:38:23 2006 +0000
+++ b/mkspecial.c	Sun Dec 10 20:10:38 2006 +0000
@@ -23,10 +23,11 @@
 
 DINT	optBlockW = 21,
 	optBlockH = 21,
-	optMapFactor = 2,
-	optRounds = 100;
+	optMapFactor = 10,
+	optRounds = 50,
+	optReset = 32;
 DBOOL	optHardDrop = FALSE;
-DFLOAT	optMatch = 90.0;
+DFLOAT	optMatch = 30.0, optMatchInit = 70.0;
 DCHAR	*optInitialMap = NULL;
 
 /* Arguments
@@ -37,7 +38,9 @@
 	{ 2, 'v', "verbose",	"Be more verbose", 0 },
 	{ 3, 'q', "quiet",	"Be quiet", 0 },
 	{ 4, 'm', "match",	"Match percentage", 1 },
+	{ 6, 'M', "minit",	"Initial block match percentage", 1 },
 	{ 5, 'r', "rounds",	"Processing timeout # rounds", 1 },
+	{ 12,'R', "reset",	"Round reset after", 1 },
 	{ 11,'d', "drop",	"Use hard dropping (bailout on first mismatch)", 0 },
 	{ 8, 'w', "width",	"Block width", 1 },
 	{ 9, 'h', "height",	"Block height", 1 },
@@ -87,6 +90,13 @@
 		}
 		break;
 
+	case 6:
+		if (optArg) {
+			optMatchInit = atof(optArg);
+			THMSG(1, "Initial Block Match at %1.4f%%\n", optMatchInit);
+		}
+		break;
+
 	case 5:
 		if (optArg) {
 			optRounds = atoi(optArg);
@@ -94,6 +104,13 @@
 		}
 		break;
 
+	case 12:
+		if (optArg) {
+			optReset = atoi(optArg);
+			THMSG(1, "Round reset after %d blocks\n", optReset);
+		}
+		break;
+
 	case 8:
 		if (optArg) {
 			optBlockW = atoi(optArg);
@@ -143,7 +160,7 @@
 /* Calculate matching percentage of given block against a map,
  * with using specified x/y offsets.
  */
-DFLOAT matchBlock(mapblock_t *map, mapblock_t *match, DINT ox, DINT oy)
+DFLOAT matchBlock(mapblock_t *map, mapblock_t *match, DINT ox, DINT oy, DBOOL matchEmpty)
 {
 	DINT x, y, c1, c2, dy, dx, n, k;
 
@@ -155,24 +172,23 @@
 		dy = (oy + y);
 		k++;
 		
-		if (dx >= 0 && dx < map->w && dy >= 0 && dy < map->h) {
-			c1 = match->d[(y * match->w) + x];
+		if (dx >= 0 && dx < map->w && dy >= 0 && dy < map->h)
 			c2 = map->d[(dy * map->w) + dx];
-			
-			if (c1 == 0 && c2 == 0) {
-				/* Both are zero (nothing) */
-			} else
-			if (c1 == c2) {
-				/* Exact match, increase % */
-				n++;
-			} else
-			if (c1 == 0 || c2 == 0) {
-				/* Either is zero */
-			} else 
-			if (optHardDrop) {
-				/* Mismatch, return failure */
-				return -1;
-			}
+		else
+			c2 = 0;
+		
+		c1 = match->d[(y * match->w) + x];
+
+		if (c1 == c2) {
+			/* Exact match, increase % */
+			n++;
+		} else
+		if (c2 == 0 || c1 == 0) {
+			if (matchEmpty) n++;
+		} else 
+		if (optHardDrop) {
+			/* Mismatch, return failure */
+			return -1;
 		}
 	}
 	
@@ -196,7 +212,7 @@
 		switch (map->d[o]) {
 		case ' ':
 		case '*':
-//		case '@':
+//		case '%':
 			map->d[o] = 0;
 			break;
 		}
@@ -251,9 +267,9 @@
 		for (i = 0; i < sizeof(s) && s[i] && s[i] != '\r' && s[i] != '\n'; i++);
 		s[i] = 0;
 		
-		if (strlen(s) != (DUINT) tmp->w) {
+		if (i != (DUINT) tmp->w) {
 			THERR("Broken block, line width %d < %d!\n",
-				strlen(s), tmp->w, s);
+				i, tmp->w, s);
 			freeBlock(tmp);
 			return NULL;
 		}
@@ -285,26 +301,7 @@
 	
 	for (oy = -(b->h); oy < (map->h + b->h); oy++)
 	for (ox = -(b->w); ox < (map->w + b->w); ox++) {
-		v = matchBlock(map, b, ox, oy);
-		if (v >= *m) {
-			*m = v;
-			*x = ox;
-			*y = oy;
-			return TRUE;
-		}
-	}
-	
-	return FALSE;
-}
-
-DBOOL blockSearch(mapblock_t *map, mapblock_t *b, DINT *x, DINT *y, DFLOAT *m, DINT q)
-{
-	DINT ox, oy;
-	DFLOAT v;
-	
-	for (oy = (*y - q); oy < (*y + q); oy++)
-	for (ox = (*x - q); ox < (*x + q); ox++) {
-		v = matchBlock(map, b, ox, oy);
+		v = matchBlock(map, b, ox, oy, TRUE);
 		if (v >= *m) {
 			*m = v;
 			*x = ox;
@@ -320,8 +317,8 @@
 int main(int argc, char *argv[])
 {
 	DBOOL isOK;
-	DINT i, offsetX, offsetY, currRounds, nmapBlocks;
-	mapblock_t *map = NULL, *tmp = NULL, *initialMap = NULL;
+	DINT i, offsetX, offsetY, currRounds, nmapBlocks, currBlocks;
+	mapblock_t *map = NULL, *initialMap = NULL;
 	mapblock_t **mapBlocks = NULL;
 
 	progName = argv[0];
@@ -362,6 +359,7 @@
 		}
 		
 		while (!feof(tmpFile)) {
+			mapblock_t *tmp;
 			if ((tmp = parseBlock(tmpFile, optBlockW, optBlockH)) != NULL) {
 				nmapBlocks++;
 				mapBlocks = (mapblock_t **) th_realloc(mapBlocks, sizeof(mapblock_t *) * nmapBlocks);
@@ -399,22 +397,42 @@
 	 	if (all blocks placed) break
 	 }
 	 */
+	/* Get dimensions */
 	
 	offsetX = offsetY = 0;
 	
 	if (initialMap) {
-		DFLOAT mv = optMatch;
+		DFLOAT mv = optMatchInit;
 		map = initialMap;
+		isOK = FALSE;
+		
+		THMSG(1, "Matching with initial map ...\n");
 		
-		for (i = 0; i < nmapBlocks; i++)
-		if (blockSearchBrute(map, mapBlocks[i], &offsetX, &offsetY, &mv)) {
-			THMSG(1, "Found init point at %d,%d (%1.3f%%)\n",
-				offsetX, offsetY, mv);
-			break;
+		for (i = 0; i < nmapBlocks && !isOK; i++) {
+			THPRINT(1, "%d%%..", (i * 100) / nmapBlocks);
+			
+			if (blockSearchBrute(map, mapBlocks[i], &offsetX, &offsetY, &mv)) {
+				THMSG(1, "Found init point at [%d] %d,%d (%1.3f%%)\n",
+				i, offsetX, offsetY, mv);
+				
+				if (putBlock(&map, mapBlocks[i], offsetX, offsetY, 15 /* FIXME */) < 0) {
+					THERR("putBlock(%d, %d, %d) failed!\n",
+						offsetX, offsetY, i);
+					exit(9);
+				}
+					
+				isOK = TRUE;
+			}
 		}
-			
+		
+		if (!isOK) {
+			THERR("No matches initmap <-> blocks. Need more data.\n");
+			exit(6);
+		}
+		
 	} else if (nmapBlocks > 0) {
-		/* No initial map file given, let's start from these coordinates */
+		mapblock_t *tmp;
+		
 		tmp = mapBlocks[0];
 		
 		if ((map = allocBlock(tmp->w * optMapFactor, tmp->h * optMapFactor)) == NULL) {
@@ -422,12 +440,14 @@
 			exit(5);
 		}
 		
-		offsetX = tmp->x;
-		offsetY = tmp->y;
+		offsetX = (map->w / 2) - (tmp->w / 2);
+		offsetY = (map->h / 2) - (tmp->h / 2);
 		
-		putBlockDo(map, tmp, 
-			(map->w / 2) - (tmp->w / 2),
-			(map->h / 2) - (tmp->h / 2));
+		putBlockDo(map, tmp, offsetX, offsetY);
+/*		
+		offsetX -= tmp->x;
+		offsetY -= tmp->y;
+*/
 	}
 	
 	if (!map) {
@@ -442,7 +462,7 @@
 	while ((currRounds++ < optRounds) && !isOK) {
 		DINT usedBlocks;
 		
-		/* Get number of marked blocks */
+		/* Get number of used blocks */
 		for (usedBlocks = i = 0; i < nmapBlocks; i++)
 			if (mapBlocks[i]->mark) usedBlocks++;
 		
@@ -452,14 +472,43 @@
 		
 		/* Search and match blocks */
 		isOK = TRUE;
-		for (i = 0; i < nmapBlocks; i++)
-		if (!mapBlocks[i]->mark) {
-			/* Pim */
+		currBlocks = 0;
+		for (i = 0; i < nmapBlocks && currBlocks < optReset; i++) {
+			mapblock_t *tmp = mapBlocks[i];
+			
+			if (!tmp->mark) {
+				DINT	qx = offsetX + tmp->x,
+					qy = offsetY + tmp->y;
+
+				isOK = FALSE;
+
+				if (matchBlock(map, tmp, qx, qy, TRUE) >= optMatch) {
+					if (putBlock(&map, tmp, qx, qy, 15) < 0) {
+						THERR("putBlock(%d, %d, %d) failed!\n",
+							offsetX, offsetY, i);
+						exit(9);
+					}
+					
+					currBlocks++;
+					THPRINT(2, "X");
+				} else {
+					THPRINT(2, ".");
+#if 1
+				DCHAR mysti[512];
+				snprintf(mysti, sizeof(mysti),
+					"[%d]: %d,%d (%d,%d)",
+					i, qx, qy, tmp->x, tmp->y);
+				fprintf(stderr, "\n--- %.30s ---\n", mysti);
+				printBlock(stderr, tmp);
+				fprintf(stderr, "---------\n");
+				printBlock(stderr, map);
+#endif
+				}
+			}
 		}
 		
 		THPRINT(2, "\n");
 	}
-	
 
 	/* Output generated map
 	 */