changeset 29:007cbc05d04f

Shit
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 10 Dec 2006 02:37:25 +0000
parents 81a329b34878
children 2eceda1c86ab
files Makefile.gen mkspecial.c
diffstat 2 files changed, 63 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.gen	Sun Dec 10 01:37:38 2006 +0000
+++ b/Makefile.gen	Sun Dec 10 02:37:25 2006 +0000
@@ -36,7 +36,7 @@
 $(MKMAP_BIN): mkmap.c maputils.o th_args.o th_util.o th_string.o
 	$(COMP) -o $@ $+ $(LDFLAGS)
 
-$(MKSPECIAL_BIN): mkspecial.c maputils.o th_args.o th_util.o
+$(MKSPECIAL_BIN): mkspecial.c maputils.o th_args.o th_util.o th_string.o
 	$(COMP) -o $@ $+ $(LDFLAGS)
 
 $(COLORMAP_BIN): colormap.c maputils.o th_util.o
--- a/mkspecial.c	Sun Dec 10 01:37:38 2006 +0000
+++ b/mkspecial.c	Sun Dec 10 02:37:25 2006 +0000
@@ -213,6 +213,7 @@
 {
 	mapblock_t *tmp;
 	DINT x, y, o;
+	DUINT i;
 	DBOOL isFound;
 	DCHAR s[4096];
 	
@@ -235,7 +236,7 @@
 	}
 	
 	
-	for (i = 0; i < sizeof(s) && s[i] && s[i] != '\n'; i++);
+	for (i = 0; i < sizeof(s) && s[i] && s[i] != '\r' && s[i] != '\n'; i++);
 	s[i] = 0;
 	if (sscanf(s, "!!PLR: %d,%d", &(tmp->x), &(tmp->y)) < 2) {
 		THERR("Could not location coordinates\n");
@@ -245,10 +246,9 @@
 	
 	o = y = x = 0;
 	while (!feof(inFile) && (y < tmp->h)) {
-		DINT i;
 		fgets(s, sizeof(s), inFile);
 		
-		for (i = 0; i < sizeof(s) && s[i] && s[i] != '\n'; i++);
+		for (i = 0; i < sizeof(s) && s[i] && s[i] != '\r' && s[i] != '\n'; i++);
 		s[i] = 0;
 		
 		if (strlen(s) != (DUINT) tmp->w) {
@@ -278,7 +278,7 @@
 
 /* Bruteforce search / block matching ..
  */
-DBOOL blockSearchBrute(mapblock_t *map, mapblock_t *b, DINT *x, DINT *y, DFLOAT m)
+DBOOL blockSearchBrute(mapblock_t *map, mapblock_t *b, DINT *x, DINT *y, DFLOAT *m)
 {
 	DINT ox, oy;
 	DFLOAT v;
@@ -286,7 +286,8 @@
 	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) {
+		if (v >= *m) {
+			*m = v;
 			*x = ox;
 			*y = oy;
 			return TRUE;
@@ -296,7 +297,7 @@
 	return FALSE;
 }
 
-DBOOL blockSearch(mapblock_t *map, mapblock_t *b, DINT *x, DINT *y, DFLOAT m, DINT q)
+DBOOL blockSearch(mapblock_t *map, mapblock_t *b, DINT *x, DINT *y, DFLOAT *m, DINT q)
 {
 	DINT ox, oy;
 	DFLOAT v;
@@ -304,7 +305,8 @@
 	for (oy = (*y - q); oy < (*y + q); oy++)
 	for (ox = (*x - q); ox < (*x + q); ox++) {
 		v = matchBlock(map, b, ox, oy);
-		if (v >= m) {
+		if (v >= *m) {
+			*m = v;
 			*x = ox;
 			*y = oy;
 			return TRUE;
@@ -317,10 +319,9 @@
 
 int main(int argc, char *argv[])
 {
-	FILE *tmpFile;
 	DBOOL isOK;
-	DINT i, n, ox, oy, nmapBlocks = 0;
-	mapblock_t *map = NULL, *tmp = NULL, *initial = NULL;
+	DINT i, offsetX, offsetY, currRounds, nmapBlocks;
+	mapblock_t *map = NULL, *tmp = NULL, *initialMap = NULL;
 	mapblock_t **mapBlocks = NULL;
 
 	progName = argv[0];
@@ -339,10 +340,10 @@
 	/* Read initial map */
 	if (optInitialMap) {
 		THMSG(1, "Reading initial map '%s'\n", optInitialMap);
-		initial = parseFile(optInitialMap);
-		if (initial) {
+		initialMap = parseFile(optInitialMap);
+		if (initialMap) {
 			THMSG(2, "Initial dimensions %d x %d\n",
-				initial->w, initial->h);
+				initialMap->w, initialMap->h);
 		} else {
 			THERR("Initial map could not be loaded!\n");
 			exit(1);
@@ -351,7 +352,9 @@
 	
 	/* Read in continuous mapdata and parse it into map blocks
 	 */
-	for (i = 0; i < nsrcFiles; i++) {
+	for (nmapBlocks = i = 0; i < nsrcFiles; i++) {
+		FILE *tmpFile;
+		
 		if ((tmpFile = fopen(srcFiles[i], "rb")) == NULL) {
 			THERR("Error opening input file '%s'!\n",
 				srcFiles[i]);
@@ -359,10 +362,11 @@
 		}
 		
 		while (!feof(tmpFile)) {
-			if ((tmp = parseBlock(tmpFile, optBlockW, optBlockH, optPattern)) != NULL) {
+			if ((tmp = parseBlock(tmpFile, optBlockW, optBlockH)) != NULL) {
 				nmapBlocks++;
 				mapBlocks = (mapblock_t **) th_realloc(mapBlocks, sizeof(mapblock_t *) * nmapBlocks);
 				if (!mapBlocks) {
+					fclose(tmpFile);
 					THERR("Could not allocate/extend mapblock pointer structure (#%d)\n", nmapBlocks);
 					exit(3);
 				}
@@ -395,54 +399,62 @@
 	 	if (all blocks placed) break
 	 }
 	 */
-
-	if (initial) {
-		map = initial;
+	
+	offsetX = offsetY = 0;
+	
+	if (initialMap) {
+		DFLOAT mv = optMatch;
+		map = initialMap;
+		
+		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;
+		}
+			
 	} else if (nmapBlocks > 0) {
-		if (optInverse)
-			tmp = mapBlocks[nmapBlocks - 1];
-		else
-			tmp = mapBlocks[0];
+		/* No initial map file given, let's start from these coordinates */
+		tmp = mapBlocks[0];
 		
 		if ((map = allocBlock(tmp->w * optMapFactor, tmp->h * optMapFactor)) == NULL) {
 			THERR("Could not allocate initial map.\n");
-			return 3;
+			exit(5);
 		}
 		
-		putBlockDo(map, tmp,
+		offsetX = tmp->x;
+		offsetY = tmp->y;
+		
+		putBlockDo(map, tmp, 
 			(map->w / 2) - (tmp->w / 2),
 			(map->h / 2) - (tmp->h / 2));
 	}
+	
+	if (!map) {
+		THERR("Initial map could not be formed, no input.\n");
+		exit(4);
+	}
 
 	THMSG(1, "Initialized initial map of (%d x %d)\n", map->w, map->h);
 
-	n = 0;
+	currRounds = 0;
 	isOK = FALSE;
-	while ((n < optRounds) && !isOK) {
-		DINT q;
+	while ((currRounds++ < optRounds) && !isOK) {
+		DINT usedBlocks;
 		
 		/* Get number of marked blocks */
-		for (q = i = 0; i < nmapBlocks; i++)
-		if (!mapBlocks[i]->mark) q++;
+		for (usedBlocks = i = 0; i < nmapBlocks; i++)
+			if (mapBlocks[i]->mark) usedBlocks++;
 		
 		/* Print out status information */
-		n++;
-		THPRINT(2, "#%d [%d/%d]: ", n, q, nmapBlocks);
+		THPRINT(2, "#%d [%d/%d]: ",
+			currRounds, usedBlocks, nmapBlocks);
 		
 		/* Search and match blocks */
 		isOK = TRUE;
 		for (i = 0; i < nmapBlocks; i++)
 		if (!mapBlocks[i]->mark) {
-			if (blockSearch(map, mapBlocks[i], &ox, &oy, mv)) {
-				THPRINT(2, "X");
-				if (putBlock(&map, mapBlocks[i], ox, oy) < 0) {
-					THERR("putBlock(%d, %d, %d) failed!\n",
-						ox, oy, i);
-				}
-				isOK = FALSE;
-			} else {
-				THPRINT(2, ".");
-			}
+			/* Pim */
 		}
 		
 		THPRINT(2, "\n");
@@ -452,7 +464,11 @@
 	/* Output generated map
 	 */
 	if (map) {
+		DINT unusedBlocks;
+		FILE *tmpFile;
+		
 		THMSG(1, "Outputting generated map ...\n");
+
 		if (destFile == NULL)
 			tmpFile = stdout;
 		else if ((tmpFile = fopen(destFile, "wb")) == NULL) {
@@ -464,11 +480,12 @@
 		printBlock(tmpFile, map);
 	
 		fclose(tmpFile);
-
-		for (n = i = 0; i < nmapBlocks; i++)
-		if (!mapBlocks[i]->mark) n++;
 		
-		THMSG(1, "%d mapblocks unused/discarded\n", n);
+		/* Compute number of unused blocks */
+		for (unusedBlocks = i = 0; i < nmapBlocks; i++)
+			if (!mapBlocks[i]->mark) unusedBlocks++;
+		
+		THMSG(1, "%d mapblocks unused/discarded\n", unusedBlocks);
 	} else {
 		THERR("No map generated?\n");
 	}