# HG changeset patch # User Matti Hamalainen # Date 1509070208 -10800 # Node ID 72adabd8e74662dae43cf6ce79709d0b70812b22 # Parent cc59f80b0e784968a1835e17941f99d925732ae7 More cleanups. diff -r cc59f80b0e78 -r 72adabd8e746 map2ppm.c --- a/map2ppm.c Fri Oct 27 04:35:51 2017 +0300 +++ b/map2ppm.c Fri Oct 27 05:10:08 2017 +0300 @@ -145,7 +145,7 @@ int writeImageData(MapBlock *map, void *cbdata, BOOL (*writeRowCB)(void *, uint8_t *, size_t), int scale) { - int x, y, yscale, xscale, res = 0; + int res = 0; uint8_t *row = NULL; // Allocate memory for row buffer @@ -155,11 +155,11 @@ goto done; } - for (y = 0; y < map->height; y++) + for (int y = 0; y < map->height; y++) { uint8_t *ptr = row; - for (x = 0; x < map->width; x++) + for (int x = 0; x < map->width; x++) { int qr, qg, qb, c; qr = 255; qg = qb = 0; @@ -202,7 +202,7 @@ } } - for (xscale = 0; xscale < scale; xscale++) + for (int xscale = 0; xscale < scale; xscale++) { *ptr++ = qr; *ptr++ = qg; @@ -210,7 +210,7 @@ } } - for (yscale = 0; yscale < scale; yscale++) + for (int yscale = 0; yscale < scale; yscale++) { if (!writeRowCB(cbdata, row, map->width * 3 * scale)) { @@ -407,6 +407,5 @@ mapBlockFree(map); - exit(ret); return ret; } diff -r cc59f80b0e78 -r 72adabd8e746 mkcitymap.c --- a/mkcitymap.c Fri Oct 27 04:35:51 2017 +0300 +++ b/mkcitymap.c Fri Oct 27 05:10:08 2017 +0300 @@ -103,14 +103,12 @@ void outputLocationBlockHTML(FILE *outFile, MapLocations *l, int nStart, int nEnd) { - int n; - if (nStart >= l->n || nStart < 0 || nStart > nEnd) return; if (nEnd >= l->n) nEnd = l->n-1; fprintf(outFile, "\n"); - for (n = nStart; n <= nEnd; n++) + for (int n = nStart; n <= nEnd; n++) if (l->locations[n] != NULL) { LocMarker *tmp = l->locations[n]; diff -r cc59f80b0e78 -r 72adabd8e746 mkloc.c --- a/mkloc.c Fri Oct 27 04:35:51 2017 +0300 +++ b/mkloc.c Fri Oct 27 05:10:08 2017 +0300 @@ -1091,10 +1091,9 @@ int main(int argc, char *argv[]) { - FILE *outFile; + FILE *outFile = NULL; MapBlock *worldMap = NULL; MapLocations worldLoc; - int i; memset(&worldLoc, 0, sizeof(worldLoc)); @@ -1105,31 +1104,31 @@ // Parse arguments if (!th_args_process(argc, argv, optList, optListN, argHandleOpt, NULL, OPTH_BAILOUT)) - exit(1); + goto exit; // Check the mode and arguments if (srcFile == NULL && optGetUpdateLoc && optOutput == OUTFMT_LOCFILE) { THERR("Map file required for location update mode!\n"); - exit(0); + goto exit; } if (optOutput == OUTFMT_LOCFILE && nlocFiles < 0 && !optGetUpdateLoc) { THERR("Location file or location update mode required for location file output!\n"); - exit(0); + goto exit; } if ((optOutput == OUTFMT_SCRIPT || optOutput == OUTFMT_MAPLOC) && nlocFiles < 0) { THERR("Location file required for script or MapLoc HTML output!\n"); - exit(0); + goto exit; } if (srcFile == NULL && optOutput == OUTFMT_MAP) { THERR("Mapfile required for map generation.\n"); - exit(0); + goto exit; } // Read initial map @@ -1137,25 +1136,26 @@ { THMSG(2, "Reading map '%s'\n", srcFile); worldMap = mapBlockParseFile(srcFile, FALSE); - if (!worldMap) + if (worldMap == NULL) { THERR("World map could not be loaded!\n"); - exit(1); + goto exit; } THMSG(2, "Initial dimensions (%d x %d)\n", worldMap->width, worldMap->height); } // Read location info - for (i = 0; i <= nlocFiles; i++) + for (int i = 0; i <= nlocFiles; i++) { LocFileInfo *f = &locFiles[i]; FILE *inFile; if (optOutput == OUTFMT_GMAPS && f->continent == NULL) { - THERR("Required continent name not set for #%d '%s'.\n", i, f->filename); - exit(19); + THERR("Required continent name not set for #%d '%s'.\n", + i, f->filename); + goto exit; } THMSG(2, "Reading location info '%s'\n", f->filename); @@ -1163,11 +1163,11 @@ { THERR("Could not open location file '%s' for reading.\n", f->filename); - exit(2); + goto exit; } if (!locParseLocStream(inFile, f, &worldLoc, f->x, f->y)) - exit(1); + goto exit; fclose(inFile); } @@ -1179,11 +1179,9 @@ // Scale locations if (optScale > 0) { - int i; - THMSG(1, "Scaling locations ..\n"); - for (i = 0; i < worldLoc.n; i++) + for (int i = 0; i < worldLoc.n; i++) { LocMarker *tmp = worldLoc.locations[i]; @@ -1200,55 +1198,58 @@ { THERR("Error opening output file '%s'!\n", destFile); - exit(1); + goto exit; } // Output results - switch (optOutput) { - case OUTFMT_SCRIPT: - THMSG(1, "Generating ImageMagick script ...\n"); - outputMagickScript(outFile, &worldLoc); - break; + switch (optOutput) + { + case OUTFMT_SCRIPT: + THMSG(1, "Generating ImageMagick script ...\n"); + outputMagickScript(outFile, &worldLoc); + break; - case OUTFMT_LOCFILE: - THMSG(1, "Outputting generated location list ...\n"); - outputLocationFile(outFile, &worldLoc); - THMSG(2, "%d locations\n", worldLoc.n); - break; + case OUTFMT_LOCFILE: + THMSG(1, "Outputting generated location list ...\n"); + outputLocationFile(outFile, &worldLoc); + THMSG(2, "%d locations\n", worldLoc.n); + break; - case OUTFMT_MAPLOC: - maplocSort(&worldLoc); - THMSG(1, "Outputting MapLoc HTML ...\n"); - outputMapLocHTML(outFile, &worldLoc); - THMSG(2, "%d locations\n", worldLoc.n); - break; + case OUTFMT_MAPLOC: + maplocSort(&worldLoc); + THMSG(1, "Outputting MapLoc HTML ...\n"); + outputMapLocHTML(outFile, &worldLoc); + THMSG(2, "%d locations\n", worldLoc.n); + break; - case OUTFMT_GMAPS: - maplocSort(&worldLoc); - THMSG(1, "Outputting GMaps data (%s) ...\n", gmapsModes[optGMapsMode]); - switch (optGMapsMode) - { - case GMAPS_XML: outputGMapsXML(outFile, &worldLoc); break; - case GMAPS_JSON: outputGMapsJSON(outFile, &worldLoc); break; - } - THMSG(2, "%d locations\n", worldLoc.n); - break; + case OUTFMT_GMAPS: + maplocSort(&worldLoc); + THMSG(1, "Outputting GMaps data (%s) ...\n", gmapsModes[optGMapsMode]); + switch (optGMapsMode) + { + case GMAPS_XML: outputGMapsXML(outFile, &worldLoc); break; + case GMAPS_JSON: outputGMapsJSON(outFile, &worldLoc); break; + } + THMSG(2, "%d locations\n", worldLoc.n); + break; - case OUTFMT_MAP: - THMSG(1, "Outputting generated map of (%d x %d) ...\n", - worldMap->width, worldMap->height); + case OUTFMT_MAP: + THMSG(1, "Outputting generated map of (%d x %d) ...\n", + worldMap->width, worldMap->height); - THMSG(2, "Adjusting location labels ..\n"); - adjustLocInfoCoords(worldMap, &worldLoc); + THMSG(2, "Adjusting location labels ..\n"); + adjustLocInfoCoords(worldMap, &worldLoc); - outputMapCTRL(outFile, worldMap, &worldLoc); - break; + outputMapCTRL(outFile, worldMap, &worldLoc); + break; } - fclose(outFile); +exit: + if (outFile != NULL) + fclose(outFile); + mapBlockFree(worldMap); locFreeMapLocations(&worldLoc); - exit(0); return 0; } diff -r cc59f80b0e78 -r 72adabd8e746 mkspecial.c --- a/mkspecial.c Fri Oct 27 04:35:51 2017 +0300 +++ b/mkspecial.c Fri Oct 27 05:10:08 2017 +0300 @@ -35,7 +35,8 @@ /* Arguments */ -static const th_optarg optList[] = { +static const th_optarg optList[] = +{ { 0, '?', "help", "Show this help", OPT_NONE }, { 1, 'o', "output", "Specify output file", OPT_ARGREQ }, { 2, 'v', "verbose", "Be more verbose", OPT_NONE }, @@ -188,15 +189,18 @@ */ float matchBlock(MapBlock *map, MapBlock *match, int ox, int oy, BOOL hardDrop) { - int x, y, c1, c2, dy, dx, n, k; + int n, k; n = k = 0; - for (y = 0; y < match->height; y++) - for (x = 0; x < match->width; x++) + for (int y = 0; y < match->height; y++) + for (int x = 0; x < match->width; x++) { - dx = (ox + x); - dy = (oy + y); + const int + dx = ox + x, + dy = oy + y; + int c1, c2; + k++; if (dx >= 0 && dx < map->width && dy >= 0 && dy < map->height) @@ -209,12 +213,14 @@ if (c1 == 0 || c2 == 0) { // Both empty ... - } else + } + else if (c1 != 0 && c1 == c2) { // Exact match, increase % n++; - } else + } + else if (hardDrop) { // Mismatch, return failure @@ -231,12 +237,10 @@ BOOL adjustBlockCenter(MapBlock *block, char centerCh) { - int x, y, c; - - for (y = 0; y < block->height; y++) - for (x = 0; x < block->width; x++) + for (int y = 0; y < block->height; y++) + for (int x = 0; x < block->width; x++) { - c = block->data[(y * block->scansize) + x]; + int c = block->data[(y * block->scansize) + x]; if (c == centerCh) { block->x -= x; @@ -273,6 +277,7 @@ return width; } + MapBlock * parseBlock(FILE *inFile) { MapBlock *tmp; @@ -321,8 +326,10 @@ tmpW = getLineWidth(s, sizeof(s)); } - if (optWalkMode) { - switch (tmpW) { + if (optWalkMode) + { + switch (tmpW) + { case 31: tmpH = 17; break; case 13: tmpH = 7; break; case 9: tmpH = 9; break; @@ -331,10 +338,12 @@ return NULL; break; } - } else + } + else tmpH = tmpW - 8; - if ((tmp = mapBlockAlloc(tmpW, tmpH)) == NULL) { + if ((tmp = mapBlockAlloc(tmpW, tmpH)) == NULL) + { THERR("Could not allocate mapblock (%d, %d)\n", tmpW, tmpH); return NULL; } @@ -343,7 +352,8 @@ tmp->y = y; y = 0; - do { + do + { i = getLineWidth(s, tmpW); if (i != (uint_t) tmp->width) @@ -546,7 +556,7 @@ offsetX = -worldX0; offsetY = -worldY0; - if ((worldMap = mapBlockAlloc((worldX1 - worldX0 + 1), (worldY1 - worldY0 + 1))) == NULL) + if ((worldMap = mapBlockAlloc(worldX1 - worldX0 + 1, worldY1 - worldY0 + 1)) == NULL) { THERR("Error allocating world map!\n"); exit(4); @@ -555,7 +565,7 @@ // Place optional initial map if (initialMap) { - if (mapBlockPut(&worldMap, initialMap, offsetX+initialMap->x, offsetY+initialMap->y) < 0) + if (mapBlockPut(&worldMap, initialMap, offsetX + initialMap->x, offsetY + initialMap->y) < 0) { THERR("Initial map mapBlockPut() failed!\n"); exit(9); @@ -564,7 +574,7 @@ else { i = 0; - if (mapBlockPut(&worldMap, mapBlocks[i], offsetX+mapBlocks[i]->x, offsetY+mapBlocks[i]->y) < 0) + if (mapBlockPut(&worldMap, mapBlocks[i], offsetX + mapBlocks[i]->x, offsetY + mapBlocks[i]->y) < 0) { THERR("Initial map mapBlockPut() failed!\n"); exit(9); @@ -653,7 +663,8 @@ if (destFile == NULL) tmpFile = stdout; else - if ((tmpFile = fopen(destFile, "wb")) == NULL) { + if ((tmpFile = fopen(destFile, "wb")) == NULL) + { THERR("Error opening output file '%s'!\n", destFile); exit(1); diff -r cc59f80b0e78 -r 72adabd8e746 patchmap.c --- a/patchmap.c Fri Oct 27 04:35:51 2017 +0300 +++ b/patchmap.c Fri Oct 27 05:10:08 2017 +0300 @@ -181,6 +181,5 @@ fclose(outFile); - exit(0); return 0; }