changeset 38:6c7c42a135fa

Updates
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 11 Dec 2006 13:51:11 +0000
parents a1a1d64382de
children fa68caaf3c94
files coloransi.c colormap.c maputils.c maputils.h mkbcmap.c mkloc.c
diffstat 6 files changed, 282 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/coloransi.c	Mon Dec 11 07:47:42 2006 +0000
+++ b/coloransi.c	Mon Dec 11 13:51:11 2006 +0000
@@ -32,8 +32,8 @@
 			c = -1;
 		} else if (k == 0xff) {
 			/* Location title mode */
-			c = -1;
 			putANSI(outFile, "0;47;30m");
+			c = p = -2;
 			
 			while ((k = fgetc(inFile)) != EOF) {
 				if (k == 0xfe)
@@ -41,7 +41,6 @@
 				else
 					fprintf(outFile, "%c", k);			
 			}
-
 		} else {
 			c = mcGetColor(k);
 			if (c != p) {
--- a/colormap.c	Mon Dec 11 07:47:42 2006 +0000
+++ b/colormap.c	Mon Dec 11 13:51:11 2006 +0000
@@ -25,10 +25,10 @@
 			fprintf(outFile, "\n");
 		} else if (k == 0xff) {
 			/* Location title mode */
-			c = col_light_white;
 			if (p != -1) fprintf(outFile, "</i>");
+			c = p = -2;
 			
-			fprintf(outFile, "<i class='%c'>", 'a'+c);
+			fprintf(outFile, "<i style='background:white;color:black'>");
 			
 			while ((k = fgetc(inFile)) != EOF) {
 				if (k == 0xfe)
--- a/maputils.c	Mon Dec 11 07:47:42 2006 +0000
+++ b/maputils.c	Mon Dec 11 13:51:11 2006 +0000
@@ -3,6 +3,7 @@
  * Programmed by Matti 'ccr' Hämäläinen (Ggr Pupunen)
  * (C) Copyright 2006 Tecnic Software productions (TNSP)
  */
+#include <string.h>
 #include "maputils.h"
 #include "th_util.h"
 
@@ -324,3 +325,11 @@
 }
 
 
+DCHAR * mydup(DCHAR *s)
+{
+	if (s)
+		return strdup(s);
+	else
+		return NULL;
+}
+
--- a/maputils.h	Mon Dec 11 07:47:42 2006 +0000
+++ b/maputils.h	Mon Dec 11 13:51:11 2006 +0000
@@ -80,4 +80,6 @@
 DINT		putBlockDo(mapblock_t *, mapblock_t *, DINT, DINT);
 DINT		putBlock(mapblock_t **, mapblock_t *, DINT, DINT, DINT);
 
+DCHAR *		mydup(DCHAR *);
+
 #endif
--- a/mkbcmap.c	Mon Dec 11 07:47:42 2006 +0000
+++ b/mkbcmap.c	Mon Dec 11 13:51:11 2006 +0000
@@ -20,14 +20,6 @@
 location_t locations[MAX_LOC];
 
 
-DCHAR * mydup(DCHAR *s)
-{
-	if (s)
-		return strdup(s);
-	else
-		return NULL;
-}
-
 void addLoc(DCHAR c, DCHAR *desc, DCHAR *extra)
 {
 	if (nlocations < MAX_LOC) {
--- a/mkloc.c	Mon Dec 11 07:47:42 2006 +0000
+++ b/mkloc.c	Mon Dec 11 13:51:11 2006 +0000
@@ -10,6 +10,22 @@
 #include "th_args.h"
 #include "th_string.h"
 
+#define NMAX 1024
+	
+enum {
+	DIR_NONE = 0,
+	DIR_LEFT,
+	DIR_LEFTDOWN,
+	DIR_DOWN
+};
+
+typedef struct {
+	DINT x, y, ox, oy;
+	DINT dir;
+	DCHAR * desc;
+} locinfo_t;
+
+
 /* Variables
  */
 DCHAR	*progName = NULL;
@@ -37,7 +53,7 @@
 void argShowHelp()
 {
 	th_args_help(stdout, optList, optListN, progName,
-		"[options] <inputfile> [inputfile#2..]");
+		"[options]");
 }
 
 
@@ -70,7 +86,7 @@
 	case 5:
 		if (optArg) {
 			srcFile = optArg;
-			THMSG(1, "Map file '%s'\n", destFile);
+			THMSG(1, "Map file '%s'\n", srcFile);
 		} else {
 			THERR("No map filename argument provided.\n");
 			exit(1);
@@ -79,8 +95,8 @@
 
 	case 6:
 		if (optArg) {
-			destFile = optArg;
-			THMSG(1, "Location info file '%s'\n", destFile);
+			locFile = optArg;
+			THMSG(1, "Location info file '%s'\n", locFile);
 		} else {
 			THERR("No location info filename argument provided.\n");
 			exit(1);
@@ -99,11 +115,56 @@
 	}
 }
 
+DINT numLocations = 0;
+locinfo_t **locations = NULL;
+
+DBOOL addLocation(DINT x, DINT y, DINT dir, DCHAR *desc)
+{
+	locinfo_t *tmp;
+	
+	/* Allocate location struct */
+	if ((tmp = th_calloc(1, sizeof(locinfo_t))) == NULL)
+		return FALSE;
+	
+	tmp->x = tmp->ox = x;
+	tmp->y = tmp->oy = y;
+	tmp->dir = dir;
+	tmp->desc = mydup(desc);
+
+	/* Add new location */	
+	numLocations++;
+	locations = (locinfo_t **) th_realloc(locations, sizeof(locinfo_t*) * numLocations);
+	if (!locations) return FALSE;
+	locations[numLocations - 1] = tmp;
+
+	return TRUE;
+}
+
+
+DINT searchLocation(DINT x, DINT y, DBOOL locTrue)
+{
+	DINT i;
+	locinfo_t *tmp;
+
+	for (i = 0; i < numLocations; i++) {
+		tmp = locations[i];
+		if (locTrue) {
+			if (tmp->ox == x && tmp->oy == y) return i;
+		} else {
+			if (tmp->x == x && tmp->y == y) return i;
+		}
+	}
+	
+	return -1;
+}
+
 
 int main(int argc, char *argv[])
 {
+	FILE *outFile;
 	mapblock_t *worldMap = NULL;
 
+	/* Initialize */
 	progName = argv[0];
 	th_init("mkloc", "Add locations to ASCII map", "0.1", NULL, NULL);
 	th_verbosityLevel = 1;
@@ -120,36 +181,220 @@
 	/* Read initial map */
 	THMSG(1, "Reading map '%s'\n", srcFile);
 	worldMap = parseFile(srcFile);
-	if (worldMap) {
+	if (!worldMap) {
 		THERR("World map could not be loaded!\n");
 		exit(1);
 	}
 	
 	THMSG(2, "Initial dimensions (%d x %d)\n", worldMap->w, worldMap->h);
-	
+
+	/* Read location info */
+	if (locFile) {
+		FILE *inFile;
+		int tx, ty, td;
+		size_t i, linePos, lineNum;
+		DCHAR inLine[NMAX+1], ts[NMAX+1];
+		
+		THMSG(2, "Reading location info '%s'\n", locFile);
+
+		/* Open file */
+		if ((inFile = fopen(locFile, "rb")) == NULL) {
+			THERR("Could not open location file '%s' for reading.\n",
+				locFile);
+			exit(2);
+		}
+		
+		/* Parse data */
+		lineNum = 0;
+		while (!feof(inFile)) {
+			lineNum++;
+			fgets(inLine, sizeof(inLine), inFile);
+			for (i = 0; i < sizeof(inLine) && inLine[i] && inLine[i] != '\r' && inLine[i] != '\n'; i++);
+			inLine[i] = 0;
+			linePos = 0;
+			
+			th_findnext(inLine, &linePos);
+			
+			if (inLine[linePos] != 0) {
+			if (th_isdigit(inLine[linePos])) {
+				/* Get X */
+				for (i = 0; i < NMAX && th_isdigit(inLine[linePos]); linePos++,i++)
+					ts[i] = inLine[linePos];
+				ts[i] = 0;
+				
+				if (!th_isspace(inLine[linePos])) {
+					THERR("Expected whitespace after X coord on line #%d in '%s'\n",
+						lineNum, locFile);
+					exit(3);
+				}
+				
+				tx = atoi(ts);
+				
+				th_findnext(inLine, &linePos);
+				
+				/* Get Y */
+				for (i = 0; i < NMAX && th_isdigit(inLine[linePos]); linePos++,i++)
+					ts[i] = inLine[linePos];
+				ts[i] = 0;
+				
+				if (!th_isspace(inLine[linePos])) {
+					THERR("Expected whitespace after Y coord on line #%d in '%s'\n",
+						lineNum, locFile);
+					exit(3);
+				}
+				
+				ty = atoi(ts);
+
+				th_findnext(inLine, &linePos);
+
+				/* Get dir */
+				for (i = 0; i < NMAX && th_isdigit(inLine[linePos]); linePos++,i++)
+					ts[i] = inLine[linePos];
+				ts[i] = 0;
+				
+				if (!th_isspace(inLine[linePos])) {
+					THERR("Expected whitespace after direction on line #%d in '%s'\n",
+						lineNum, locFile);
+					exit(3);
+				}
+				
+				td = atoi(ts);
+				
+				th_findnext(inLine, &linePos);
 
-	/* Output generated map
-	 */
-	if (worldMap) {
-		FILE *tmpFile;
+				/* Get name */
+				if (inLine[linePos] == 0) {
+					THERR("No location name? on line #%d in '%s'\n",
+						lineNum, locFile);
+					exit(3);
+				}
+				
+				/* Add location */
+				addLocation(tx, ty, td, &(inLine[linePos]));
+				
+			} else if (inLine[linePos] != '#') {
+				THERR("Invalid line #%d in '%s'\n",
+					lineNum, locFile);
+			}
+			}
+		}
+		
+		fclose(inFile);		
+	}
+	
+	/* What mode are we in? */
+	if (optGetLoc) {
+		DINT x, y, cloc = 0;
+		DCHAR *d = worldMap->d;
+		
+		THMSG(1, "Updating location information ..\n");
+		
+		/* Find new, unknown locations */
+		for (y = 0; y < worldMap->h; y++)
+		for (x = 0; x < worldMap->w; x++) {
+			if (*d == '?' || *d == '%') {
+				if (searchLocation(x, y, TRUE) < 0) {
+					DCHAR tmps[512];
+					snprintf(tmps, sizeof(tmps),
+						"UNK #%d", cloc);
+					addLocation(x, y, DIR_NONE, tmps);
+					cloc++;
+				}
+			}
+			d++;
+		}
+	} else {
+		DINT i;
 		
-		THMSG(1, "Outputting generated map of (%d x %d) ...\n",
+		THMSG(1, "Checking locations ..\n");
+
+		for (i = 0; i < numLocations; i++) {
+			DINT y, x0, x1;
+			locinfo_t *tmp = locations[i];
+			
+			/* Compute text location */
+			y = tmp->y - 1;
+
+			switch (tmp->dir) {
+			case DIR_LEFTDOWN:
+				y += 2;
+			case DIR_LEFT:
+				x0 = tmp->x - strlen(tmp->desc);
+				break;
+
+			case DIR_DOWN:
+				y += 2;
+			case DIR_NONE:
+			default:
+				x0 = tmp->x + 1;
+				break;
+			}
+			
+			x1 = x0 + strlen(tmp->desc) + 1;
+			
+			if (x1 >= worldMap->w)
+				x0 -= (x1 - worldMap->w + 1);
+			if (x0 < 0) x0 = 0;
+			
+			if (y < 0) y += 2;
+			if (y >= worldMap->h) y -= 2;
+			
+			/* Update location info */
+			tmp->x = x0;
+			tmp->y = y;
+		}
+	}
+	
+	/* Open output file */
+	if (destFile == NULL)
+		outFile = stdout;
+	else if ((outFile = fopen(destFile, "wb")) == NULL) {
+		THERR("Error opening output file '%s'!\n",
+			destFile);
+		exit(1);
+	}
+
+	/* Output results */
+	if (optGetLoc) {
+		DINT i;
+
+		THMSG(2, "Outputting generated location list ...\n");
+		
+		for (i = 0; i < numLocations; i++) {
+			locinfo_t *tmp = locations[i];
+			
+			fprintf(outFile, "%d\t%d\t%d\t%s\n",
+				tmp->ox, tmp->oy, tmp->dir, tmp->desc);
+		}
+		
+		THMSG(2, "%d locations\n", numLocations);
+	} else {
+		DCHAR *d;
+		DINT x, y, n;
+
+		THMSG(2, "Outputting generated map of (%d x %d) ...\n",
 			worldMap->w, worldMap->h);
 
-		if (destFile == NULL)
-			tmpFile = stdout;
-		else if ((tmpFile = fopen(destFile, "wb")) == NULL) {
-			THERR("Error opening output file '%s'!\n",
-				destFile);
-			exit(1);
+		d = worldMap->d;
+		
+		for (y = 0; y < worldMap->h; y++) {
+			for (x = 0; x < worldMap->w; x++) {
+				n = searchLocation(x, y, FALSE);
+				if (n >= 0) {
+					fputc(0xff, outFile);
+					fprintf(outFile, locations[n]->desc);
+					fputc(0xfe, outFile);
+					x += strlen(locations[n]->desc) - 1;
+				} else {
+					fputc(d[(worldMap->w * y) + x], outFile);
+				}
+
+			}
+			fprintf(outFile, "\n");
 		}
-	
-		printBlock(tmpFile, worldMap);
-	
-		fclose(tmpFile);
-	} else {
-		THERR("No map generated?\n");
 	}
 	
+	fclose(outFile);
+
 	return 0;
 }