Mercurial > hg > batmud > maputils
changeset 51:6c51042147c1
Lots of code cleanups, moved stuff from main() to other functions
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 13 Dec 2006 06:29:24 +0000 |
parents | cbc291055e15 |
children | bed7ae9be468 |
files | mkloc.c |
diffstat | 1 files changed, 176 insertions(+), 130 deletions(-) [+] |
line wrap: on
line diff
--- a/mkloc.c Wed Dec 13 02:52:49 2006 +0000 +++ b/mkloc.c Wed Dec 13 06:29:24 2006 +0000 @@ -33,7 +33,13 @@ typedef struct { - DCHAR *filename, *inLine; + DINT numLocations; + locinfo_t **locations; +} locations_t; + + +typedef struct { + DCHAR *fileName, *inLine; size_t lineNum, linePos; } fileinfo_t; @@ -47,8 +53,6 @@ DBOOL optGetLoc = FALSE; -DINT numLocations = 0; -locinfo_t **locations = NULL; /* Arguments */ @@ -59,7 +63,7 @@ { 1, 'o', "output", "Output file (default stdout)", 1 }, { 5, 'm', "map", "Input map file", 1 }, { 6, 'l', "locinfo", "Input location info file", 1 }, - { 4, 'g', "getloc", "Get/generate location information", 0 }, + { 4, 'g', "getloc", "Generate/update location info", 0 }, }; const DINT optListN = (sizeof(optList) / sizeof(t_opt)); @@ -93,7 +97,7 @@ destFile = optArg; THMSG(1, "Output file '%s'\n", destFile); } else { - THERR("No output filename argument provided.\n"); + THERR("No output fileName argument provided.\n"); exit(1); } break; @@ -103,7 +107,7 @@ srcFile = optArg; THMSG(1, "Map file '%s'\n", srcFile); } else { - THERR("No map filename argument provided.\n"); + THERR("No map fileName argument provided.\n"); exit(1); } break; @@ -113,7 +117,7 @@ locFile = optArg; THMSG(1, "Location info file '%s'\n", locFile); } else { - THERR("No location info filename argument provided.\n"); + THERR("No location info fileName argument provided.\n"); exit(1); } break; @@ -131,7 +135,8 @@ } -DBOOL addLocation(DINT x, DINT y, DINT dir, DCHAR *desc, DINT flags) +DBOOL addLocation(locations_t *l, + DINT x, DINT y, DINT dir, DCHAR *desc, DINT flags) { locinfo_t *tmp; @@ -146,22 +151,23 @@ tmp->flags = flags; /* Add new location */ - numLocations++; - locations = (locinfo_t **) th_realloc(locations, sizeof(locinfo_t*) * numLocations); - if (!locations) return FALSE; - locations[numLocations - 1] = tmp; + l->locations = (locinfo_t **) th_realloc(l->locations, + sizeof(locinfo_t*) * (l->numLocations+1)); + if (!l->locations) return FALSE; + l->locations[l->numLocations] = tmp; + l->numLocations++; return TRUE; } -DINT searchLocation(DINT x, DINT y, DBOOL locTrue) +DINT searchLocation(locations_t *l, DINT x, DINT y, DBOOL locTrue) { DINT i; locinfo_t *tmp; - for (i = 0; i < numLocations; i++) { - tmp = locations[i]; + for (i = 0; i < l->numLocations; i++) { + tmp = l->locations[i]; if (locTrue) { if (tmp->ox == x && tmp->oy == y) return i; } else { @@ -187,7 +193,7 @@ 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); + f->linePos, f->lineNum, f->fileName); return FALSE; } @@ -198,23 +204,21 @@ } -DBOOL parseLocationFile(DCHAR *filename) +DBOOL parseLocationFile(DCHAR *fileName, locations_t *l) { FILE *inFile; size_t i; DCHAR inLine[NMAX+1]; fileinfo_t f; - f.filename = filename; + 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) { + if ((inFile = fopen(f.fileName, "rb")) == NULL) { THERR("Could not open location file '%s' for reading.\n", - f.filename); + f.fileName); return FALSE; } @@ -228,7 +232,7 @@ th_findnext(inLine, &f.linePos); if (th_isdigit(inLine[f.linePos])) { - DINT tx, ty, td, tf; + DINT tx, ty, td, tf, tn; /* Get X coordinate */ if (!parseValue(&f, &tx)) @@ -236,7 +240,7 @@ if (!th_isspace(inLine[f.linePos])) { THERR("Expected whitespace after X coord on line #%d in '%s'\n", - f.lineNum, f.filename); + f.lineNum, f.fileName); return FALSE; } @@ -248,7 +252,7 @@ if (!th_isspace(inLine[f.linePos])) { THERR("Expected whitespace after Y coord on line #%d in '%s'\n", - f.lineNum, f.filename); + f.lineNum, f.fileName); return FALSE; } @@ -265,7 +269,7 @@ case ' ': case 9: break; default: THERR("Expected flag or whitespace after direction on line #%d in '%s'\n", - f.lineNum, f.filename); + f.lineNum, f.fileName); return FALSE; } @@ -274,16 +278,27 @@ /* Get name */ if (inLine[f.linePos] == 0) { THERR("No location name? on line #%d in '%s'\n", - f.lineNum, f.filename); + f.lineNum, f.fileName); return FALSE; } /* Add location */ - addLocation(tx, ty, td, &(inLine[f.linePos]), tf); + tn = searchLocation(l, tx, ty, TRUE); + if (tn >= 0) { + locinfo_t *tloc = l->locations[tn]; + + THERR("Warning, location already in list!\n" + "(%d,%d) '%s' <-> (%d,%d) '%s'\n", + tloc->x, tloc->y, tloc->desc, + tx, ty, &(inLine[f.linePos]) + ); + return FALSE; + } + addLocation(l, 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); + f.lineNum, f.fileName); } } @@ -292,10 +307,124 @@ } +void outputMapCTRL(FILE *outFile, mapblock_t *map, locations_t *l) +{ + DCHAR *d; + DINT x, y, n; + + d = map->d; + + for (y = 0; y < map->h; y++) { + for (x = 0; x < map->w; x++) { + n = searchLocation(l, x, y, TRUE); + if ((n = searchLocation(l, x, y, TRUE)) >= 0) { + locinfo_t *tloc = l->locations[n]; + + switch (tloc->flags) { + case LOCF_MARK: + fputc('?', outFile); + break; + + case LOCF_NONE: + default: + fputc(d[(map->w * y) + x], outFile); + break; + } + } else + if ((n = searchLocation(l, x, y, FALSE)) >= 0) { + locinfo_t *tloc = l->locations[n]; + + fputc(0xff, outFile); + fprintf(outFile, tloc->desc); + fputc(0xfe, outFile); + + x += strlen(tloc->desc) - 1; + } else { + fputc(d[(map->w * y) + x], outFile); + } + } + fprintf(outFile, "\n"); + } +} + + +void outputLocationFile(FILE *outFile, locations_t *l) +{ + DINT i; + + fprintf(outFile, + "# Format of this file is one location per line:\n" + "# X\tY\textra\tLocation name\n" + ); + + for (i = 0; i < l->numLocations; i++) { + locinfo_t *tmp = l->locations[i]; + + 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); + } +} + +void adjustLocInfoCoords(mapblock_t *map, locations_t *l) +{ + DINT i; + + for (i = 0; i < l->numLocations; i++) { + DINT y, x0, x1; + locinfo_t *tmp = l->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 >= map->w) + x0 -= (x1 - map->w + 1); + if (x0 < 0) x0 = 0; + + if (y < 0) y += 2; + if (y >= map->h) y -= 2; + + /* Update location info */ + tmp->x = x0; + tmp->y = y; + } +} + + int main(int argc, char *argv[]) { FILE *outFile; mapblock_t *worldMap = NULL; + locations_t worldLocations; /* Initialize */ progName = argv[0]; @@ -323,7 +452,8 @@ /* Read location info */ if (locFile) { - if (!parseLocationFile(locFile)) { + THMSG(1, "Reading location info '%s'\n", locFile); + if (!parseLocationFile(locFile, &worldLocations)) { THERR("Error reading location file!\n"); exit(1); } @@ -334,62 +464,24 @@ DINT x, y, cloc = 0; DCHAR *d = worldMap->d; - THMSG(1, "Updating location information ..\n"); + THMSG(2, "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) { + if (searchLocation(&worldLocations, x, y, TRUE) < 0) { DCHAR tmps[512]; - snprintf(tmps, sizeof(tmps), - "UNK #%d", cloc); - addLocation(x, y, DIR_NONE, tmps, LOCF_NONE); + snprintf(tmps, sizeof(tmps), "UNK #%d", cloc); + + addLocation(&worldLocations, + x, y, DIR_NONE, tmps, LOCF_NONE); + cloc++; } } d++; } - } else { - DINT i; - - 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 */ @@ -403,67 +495,21 @@ /* 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", - tmp->ox, tmp->oy, tmp->dir); - - switch (tmp->flags) { - case LOCF_MARK: - fprintf(outFile, "+"); - break; + /* Output location list */ + THMSG(1, "Outputting generated location list ...\n"); - case LOCF_NONE: - default: - /* Do nothing */ - break; - } - - fprintf(outFile, "\t%s\n", tmp->desc); - } + outputLocationFile(outFile, &worldLocations); - THMSG(2, "%d locations\n", numLocations); + THMSG(2, "%d locations\n", worldLocations.numLocations); } else { - DCHAR *d; - DINT x, y, n; - - THMSG(2, "Outputting generated map of (%d x %d) ...\n", + /* Output map in selected format */ + THMSG(1, "Outputting generated map of (%d x %d) ...\n", worldMap->w, worldMap->h); - d = worldMap->d; + THMSG(2, "Adjusting location labels ..\n"); + adjustLocInfoCoords(worldMap, &worldLocations); - for (y = 0; y < worldMap->h; y++) { - for (x = 0; x < worldMap->w; x++) { - n = searchLocation(x, y, TRUE); - if ((n = searchLocation(x, y, TRUE)) >= 0) { - switch (locations[n]->flags) { - case LOCF_MARK: - fputc('?', outFile); - break; - - case LOCF_NONE: - default: - fputc(d[(worldMap->w * y) + x], outFile); - break; - } - } else - if ((n = searchLocation(x, y, FALSE)) >= 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"); - } + outputMapCTRL(outFile, worldMap, &worldLocations); } fclose(outFile);