Mercurial > hg > batmud > maputils
changeset 45:df05fc552cc3
Cleanups
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 12 Dec 2006 11:08:00 +0000 |
parents | 4192ed8b2d6b |
children | 6be1c559a6ff |
files | maputils.c maputils.h mkbcmap.c mkloc.c mkmap.c mkspecial.c |
diffstat | 6 files changed, 193 insertions(+), 178 deletions(-) [+] |
line wrap: on
line diff
--- a/maputils.c Tue Dec 12 09:55:43 2006 +0000 +++ b/maputils.c Tue Dec 12 11:08:00 2006 +0000 @@ -325,6 +325,22 @@ } +/* Clean given block from position markers and whitespaces + */ +void cleanBlock(mapblock_t *map, DCHAR *symbols) +{ + DINT x, y; + size_t o; + + o = 0; + for (y = 0; y < map->h; y++) + for (x = 0; x < map->w; x++) { + if (strchr(symbols, map->d[o]) != NULL) + map->d[o] = 0; + o++; + } +} + DCHAR * mydup(DCHAR *s) { if (s)
--- a/maputils.h Tue Dec 12 09:55:43 2006 +0000 +++ b/maputils.h Tue Dec 12 11:08:00 2006 +0000 @@ -79,6 +79,7 @@ void printBlock(FILE *, mapblock_t *); DINT putBlockDo(mapblock_t *, mapblock_t *, DINT, DINT); DINT putBlock(mapblock_t **, mapblock_t *, DINT, DINT, DINT); +void cleanBlock(mapblock_t *, DCHAR *); DCHAR * mydup(DCHAR *);
--- a/mkbcmap.c Tue Dec 12 09:55:43 2006 +0000 +++ b/mkbcmap.c Tue Dec 12 11:08:00 2006 +0000 @@ -324,10 +324,7 @@ return -1; } - while (!feof(inFile)) { - s[0] = 0; - fgets(s, sizeof(s), inFile); - + while (!fgets(s, sizeof(s), inFile)) { if (strlen(s) > 3) { i = 1;
--- a/mkloc.c Tue Dec 12 09:55:43 2006 +0000 +++ b/mkloc.c Tue Dec 12 11:08:00 2006 +0000 @@ -24,6 +24,7 @@ LOCF_MARK, }; + typedef struct { DINT x, y, ox, oy; DINT dir, flags; @@ -31,6 +32,12 @@ } locinfo_t; +typedef struct { + DCHAR *filename, *inLine; + size_t lineNum, linePos; +} fileinfo_t; + + /* Variables */ DCHAR *progName = NULL; @@ -40,6 +47,9 @@ DBOOL optGetLoc = FALSE; +DINT numLocations = 0; +locinfo_t **locations = NULL; + /* Arguments */ t_opt optList[] = { @@ -120,8 +130,6 @@ } } -DINT numLocations = 0; -locinfo_t **locations = NULL; DBOOL addLocation(DINT x, DINT y, DINT dir, DCHAR *desc, DINT flags) { @@ -164,6 +172,125 @@ return -1; } +void parseElement(fileinfo_t *f, DCHAR *ts, size_t tmax) +{ + size_t i = 0; + + while (i < tmax && f->inLine[f->linePos] && th_isdigit(f->inLine[f->linePos])) + ts[i++] = f->inLine[f->linePos++]; + ts[i] = 0; +} + +DBOOL parseValue(fileinfo_t *f, DINT *val) +{ + DCHAR tmpStr[NMAX+1]; + + if (!th_isdigit(f->inLine[f->linePos])) { + THERR("Expected decimal value on column %d line #%d in '%s'\n", + f->linePos, f->lineNum, f->filename); + return FALSE; + } + + parseElement(f, tmpStr, NMAX); + *val = atoi(tmpStr); + + return TRUE; +} + + +DBOOL parseLocationFile(DCHAR *filename) +{ + FILE *inFile; + size_t i; + DCHAR inLine[NMAX+1]; + fileinfo_t f; + + f.filename = filename; + f.inLine = inLine; + f.lineNum = 0; + + THMSG(2, "Reading location info '%s'\n", f.filename); + + /* Open file */ + if ((inFile = fopen(f.filename, "rb")) == NULL) { + THERR("Could not open location file '%s' for reading.\n", + f.filename); + return FALSE; + } + + /* Parse data */ + while (!feof(inFile) && fgets(inLine, sizeof(inLine), inFile)) { + for (i = 0; i < sizeof(inLine) && inLine[i] && inLine[i] != '\r' && inLine[i] != '\n'; i++); + inLine[i] = 0; + f.linePos = 0; + f.lineNum++; + + th_findnext(inLine, &f.linePos); + + if (th_isdigit(inLine[f.linePos])) { + DINT tx, ty, td, tf; + + /* Get X coordinate */ + if (!parseValue(&f, &tx)) + return FALSE; + + if (!th_isspace(inLine[f.linePos])) { + THERR("Expected whitespace after X coord on line #%d in '%s'\n", + f.lineNum, f.filename); + return FALSE; + } + + th_findnext(inLine, &f.linePos); + + /* Get Y coordinate */ + if (!parseValue(&f, &ty)) + return FALSE; + + if (!th_isspace(inLine[f.linePos])) { + THERR("Expected whitespace after Y coord on line #%d in '%s'\n", + f.lineNum, f.filename); + return FALSE; + } + + th_findnext(inLine, &f.linePos); + + /* Get dir */ + if (!parseValue(&f, &td)) + return FALSE; + + /* Get flags */ + tf = LOCF_NONE; + switch (inLine[f.linePos++]) { + case '+': tf = LOCF_MARK; break; + case ' ': case 9: break; + default: + THERR("Expected flag or whitespace after direction on line #%d in '%s'\n", + f.lineNum, f.filename); + return FALSE; + } + + th_findnext(inLine, &f.linePos); + + /* Get name */ + if (inLine[f.linePos] == 0) { + THERR("No location name? on line #%d in '%s'\n", + f.lineNum, f.filename); + return FALSE; + } + + /* Add location */ + addLocation(tx, ty, td, &(inLine[f.linePos]), tf); + + } else if (inLine[f.linePos] != '#' && inLine[f.linePos] != 0) { + THERR("Invalid line #%d in '%s'\n", + f.lineNum, f.filename); + } + } + + fclose(inFile); + return TRUE; +} + int main(int argc, char *argv[]) { @@ -196,103 +323,10 @@ /* Read location info */ if (locFile) { - FILE *inFile; - size_t i, linePos, lineNum; - DCHAR inLine[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); + if (!parseLocationFile(locFile)) { + THERR("Error reading location file!\n"); + exit(1); } - - /* 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])) { - DINT tx, ty, td, tf; - DCHAR ts[NMAX+1]; - - /* 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; - - td = atoi(ts); - - /* Get flags */ - tf = LOCF_NONE; - switch (inLine[linePos++]) { - case '+': tf = LOCF_MARK; break; - case ' ': case 9: break; - default: - THERR("Expected flag or whitespace after direction on line #%d in '%s'\n", - lineNum, locFile); - exit(3); - } - - th_findnext(inLine, &linePos); - - /* 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]), tf); - - } else if (inLine[linePos] != '#') { - THERR("Invalid line #%d in '%s'\n", - lineNum, locFile); - } - } - } - - fclose(inFile); } /* What mode are we in? */ @@ -376,8 +410,21 @@ 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); + fprintf(outFile, "%d\t%d\t%d", + tmp->ox, tmp->oy, tmp->dir); + + switch (tmp->flags) { + case LOCF_MARK: + fprintf(outFile, "+"); + break; + + case LOCF_NONE: + default: + /* Do nothing */ + break; + } + + fprintf(outFile, "\t%s\n", tmp->desc); } THMSG(2, "%d locations\n", numLocations);
--- a/mkmap.c Tue Dec 12 09:55:43 2006 +0000 +++ b/mkmap.c Tue Dec 12 11:08:00 2006 +0000 @@ -31,6 +31,7 @@ DCHAR *optPattern = "You quack 'pfs.'"; DCHAR *optInitialMap = NULL; + /* Arguments */ t_opt optList[] = { @@ -199,32 +200,6 @@ } -/* Clean given block from position markers and whitespaces - * TODO: specify position marker(s) somehow instead of hardcoding - */ -void cleanBlock(mapblock_t *map) -{ - DINT x, y, o; - - o = 0; - for (y = 0; y < map->h; y++) - for (x = 0; x < map->w; x++) { - switch (map->d[o]) { - case ' ': - case '*': -// case '@': - map->d[o] = 0; - break; - case 'X': - map->d[o] = '.'; - break; - } - - o++; - } -} - - /* Parse next block (marked by string optPattern) from * input stream into a mapblock, return NULL if not found or error. */ @@ -236,8 +211,7 @@ DCHAR s[4096]; isFound = FALSE; - while (!feof(inFile) && !isFound) { - fgets(s, sizeof(s), inFile); + while (!feof(inFile) && !isFound && fgets(s, sizeof(s), inFile)) { if (!strncmp(s, inPattern, strlen(inPattern))) isFound = TRUE; } @@ -253,14 +227,13 @@ } o = y = x = 0; - while (!feof(inFile) && (y < tmp->h)) { - DUINT i; - fgets(s, sizeof(s), inFile); + while (!feof(inFile) && (y < tmp->h) && fgets(s, sizeof(s), inFile)) { + size_t 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) { + if (i != (DUINT) tmp->w) { THERR("Broken block, line width %d < %d!\n", strlen(s), tmp->w, s); freeBlock(tmp); @@ -375,7 +348,7 @@ THERR("Could not allocate/extend mapblock pointer structure (#%d)\n", nmapBlocks); exit(3); } - cleanBlock(tmp); + cleanBlock(tmp, " *X"); mapBlocks[nmapBlocks - 1] = tmp; } }
--- a/mkspecial.c Tue Dec 12 09:55:43 2006 +0000 +++ b/mkspecial.c Tue Dec 12 11:08:00 2006 +0000 @@ -199,29 +199,6 @@ } -/* Clean given block from position markers and whitespaces - * TODO: specify position marker(s) somehow instead of hardcoding - */ -void cleanBlock(mapblock_t *map) -{ - DINT x, y, o; - - o = 0; - for (y = 0; y < map->h; y++) - for (x = 0; x < map->w; x++) { - switch (map->d[o]) { - case ' ': - case '*': -// case '%': - map->d[o] = 0; - break; - } - - o++; - } -} - - /* Parse next block (marked by string optPattern) from * input stream into a mapblock, return NULL if not found or error. */ @@ -234,9 +211,7 @@ DCHAR s[4096]; isFound = FALSE; - while (!feof(inFile) && !isFound) { - fgets(s, sizeof(s), inFile); - + while (!feof(inFile) && !isFound && fgets(s, sizeof(s), inFile)) { if (!strncmp(s, "!!PLR: ", 7)) isFound = TRUE; } @@ -261,8 +236,7 @@ o = y = x = 0; - while (!feof(inFile) && (y < tmp->h)) { - fgets(s, sizeof(s), inFile); + while (!feof(inFile) && (y < tmp->h) && fgets(s, sizeof(s), inFile)) { for (i = 0; i < sizeof(s) && s[i] && s[i] != '\r' && s[i] != '\n'; i++); s[i] = 0; @@ -292,29 +266,36 @@ } -/* Bruteforce search / block matching .. +/* Bruteforce search / block matching .. Try to match the + * block on another block (map) by sliding the block + * [-bw...mapw+bw, -bh...maph+bw] */ -DBOOL blockSearchBrute(mapblock_t *map, mapblock_t *b, DINT *x, DINT *y, DFLOAT *m) +DBOOL blockSearchBrute(mapblock_t *map, mapblock_t *block, + DINT *x, DINT *y, DFLOAT *matchValue) { DINT ox, oy; - DFLOAT v; - 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, FALSE); - if (v >= *m) { - *m = v; + for (oy = -(block->h); oy < (map->h + block->h); oy++) + for (ox = -(block->w); ox < (map->w + block->w); ox++) { + DFLOAT v; + v = matchBlock(map, block, ox, oy, FALSE); + if (v >= *matchValue) { + *matchValue = v; *x = ox; *y = oy; return TRUE; } } - + return FALSE; } -void getWorldSize(mapblock_t *tmp, DINT *worldX0, DINT *worldY0, DINT *worldX1, DINT *worldY1) +/* + */ +void findWorldSize(mapblock_t *tmp, + DINT *worldX0, DINT *worldY0, + DINT *worldX1, DINT *worldY1) { if (tmp->x < *worldX0) *worldX0 = tmp->x; if ((tmp->x + tmp->w) > *worldX1) *worldX1 = (tmp->x + tmp->w); @@ -380,7 +361,7 @@ THERR("Could not allocate/extend mapblock pointer structure (#%d)\n", nmapBlocks); exit(3); } - cleanBlock(tmp); + cleanBlock(tmp, " *"); mapBlocks[nmapBlocks - 1] = tmp; } } @@ -487,10 +468,10 @@ /* Get world dimensions */ worldX0 = worldY0 = worldX1 = worldY1 = 0; for (i = 0; i < nmapBlocks; i++) - getWorldSize(mapBlocks[i], &worldX0, &worldY0, &worldX1, &worldY1); + findWorldSize(mapBlocks[i], &worldX0, &worldY0, &worldX1, &worldY1); if (initialMap) - getWorldSize(initialMap, &worldX0, &worldY0, &worldX1, &worldY1); + findWorldSize(initialMap, &worldX0, &worldY0, &worldX1, &worldY1); /* Compute offsets, allocate world map */ /* FIXME: check dimensions */