changeset 1770:cc59f80b0e78

Various cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 27 Oct 2017 04:35:51 +0300
parents 57276229a8c8
children 72adabd8e746
files combine.c diffmap.c liblocfile.c liblocfile.h libmaputils.c libmaputils.h map2ppm.c mapstats.c mkcitymap.c mkloc.c mkspecial.c patchmap.c
diffstat 12 files changed, 248 insertions(+), 251 deletions(-) [+]
line wrap: on
line diff
--- a/combine.c	Thu Oct 26 22:20:25 2017 +0300
+++ b/combine.c	Fri Oct 27 04:35:51 2017 +0300
@@ -143,7 +143,7 @@
             return FALSE;
         }
 
-        /* Get filename component */
+        // Get filename component
         for (q = c = tmpArg; *c && (*c != ':'); c++);
         if (*c != ':')
         {
@@ -156,7 +156,7 @@
         *(c++) = 0;
         srcFiles[nsrcFiles].fileName = th_strdup(q);
 
-        /* Get X offset */
+        // Get X offset
         if (!th_isdigit(*c) && *c != '-')
         {
             th_free(tmpArg);
@@ -176,7 +176,7 @@
         *(c++) = 0;
         srcFiles[nsrcFiles].x = atoi(q);
 
-        /* Get Y offset */
+        // Get Y offset
         if (!th_isdigit(*c) && *c != '-')
         {
             th_free(tmpArg);
@@ -233,7 +233,7 @@
     th_init("combine", "Combine several maps into one", "0.1", NULL, NULL);
     th_verbosityLevel = 0;
 
-    /* Parse arguments */
+    // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
         exit(1);
@@ -270,7 +270,7 @@
     }
 
 
-    /* Get world dimensions */
+    // Get world dimensions
     worldX0 = worldY0 = worldX1 = worldY1 = 0;
     for (i = 0; i < nsrcFiles; i++)
         findWorldSize(srcFiles[i].b, &worldX0, &worldY0, &worldX1, &worldY1);
@@ -281,7 +281,7 @@
     THMSG(2, "Initial dimensions <%d, %d> - <%d, %d> (%d x %d)\n",
         worldX0, worldY0, worldX1, worldY1, worldW, worldH);
 
-    /* Adjust to origo <0, 0> */
+    // Adjust to origo <0, 0>
     worldX0 = -worldX0;
     worldY0 = -worldY0;
     worldX1 = worldX0 + worldW - 1;
@@ -290,7 +290,7 @@
     THMSG(2, "Adjusted dimensions <%d, %d> - <%d, %d> (%d x %d)\n",
         worldX0, worldY0, worldX1, worldY1, worldW, worldH);
 
-    /* Adjust for requested minimum dimensions */
+    // Adjust for requested minimum dimensions
     if (worldW < optWorldMinW)
     {
         int tmp = (optWorldMinW - worldW) / 2;
@@ -306,7 +306,7 @@
         worldH = optWorldMinH;
     }
 
-    /* Allocate world map */
+    // Allocate world map
     THMSG(1, "Initializing world map of <%d, %d> - <%d, %d> (%d x %d)\n",
         worldX0, worldY0, worldX1, worldY1,
         worldW, worldH);
--- a/diffmap.c	Thu Oct 26 22:20:25 2017 +0300
+++ b/diffmap.c	Fri Oct 27 04:35:51 2017 +0300
@@ -115,9 +115,7 @@
 
 MapBlock * diffBlocks(MapBlock *map1, MapBlock *map2, BOOL useOld, char *filter1, char *filter2, size_t *count)
 {
-    int x, y;
     MapBlock *res;
-    unsigned char *p1, *p2, *pd;
 
     if (map1->width != map2->width || map1->height != map2->height)
     {
@@ -135,13 +133,14 @@
     }
 
     *count = 0;
-    for (y = 0; y < map1->height; y++)
+    for (int y = 0; y < map1->height; y++)
     {
-        p1 = map1->data + (map1->scansize * y);
-        p2 = map2->data + (map2->scansize * y);
-        pd = res->data + (res->scansize * y);
+        unsigned char
+            *p1 = map1->data + (map1->scansize * y),
+            *p2 = map2->data + (map2->scansize * y),
+            *pd = res->data + (res->scansize * y);
 
-        for (x = 0; x < map1->width; x++)
+        for (int x = 0; x < map1->width; x++)
         {
             int c;
 
@@ -164,7 +163,9 @@
                     *pd = c;
             }
 
-            p1++; p2++; pd++;
+            p1++;
+            p2++;
+            pd++;
         }
     }
 
@@ -178,11 +179,11 @@
     FILE *outFile;
     size_t count;
 
-    /* Initialize */
+    // Initialize
     th_init("diffmap", "Create a diff between two ASCII mapfiles", "0.5", NULL, NULL);
     th_verbosityLevel = 1;
 
-    /* Parse arguments */
+    // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, TRUE))
         exit(1);
@@ -193,14 +194,14 @@
         exit(0);
     }
 
-    /* Read mapfiles */
+    // Read mapfiles
     if ((map1 = mapBlockParseFile(srcFilename1, FALSE)) == NULL)
         exit(2);
 
     if ((map2 = mapBlockParseFile(srcFilename2, FALSE)) == NULL)
         exit(2);
 
-    /* Compute and output data */
+    // Compute and output data
     count = 0;
     if ((res = diffBlocks(map1, map2, optUseOldFormat, optFilter1, optFilter2, &count)) == NULL)
     {
@@ -209,7 +210,7 @@
     }
     THMSG(1, "%ld differences.\n", count);
 
-    /* Open output file */
+    // Open output file
     if (optAlwaysDiff || count > 0)
     {
         THMSG(2, "Outputting map %d x %d...\n", res->width, res->height);
--- a/liblocfile.c	Thu Oct 26 22:20:25 2017 +0300
+++ b/liblocfile.c	Fri Oct 27 04:35:51 2017 +0300
@@ -87,7 +87,7 @@
     LocMarker *tmp;
     int i;
 
-    /* Allocate location struct */
+    // Allocate location struct
     if ((tmp = th_malloc0(sizeof(LocMarker))) == NULL)
         return FALSE;
 
@@ -120,7 +120,7 @@
     tmp->uri = th_strdup(uri);
     tmp->freeform = th_strdup(freeform);
 
-    /* Add new location */
+    // Add new location
     l->locations = (LocMarker **) th_realloc(l->locations,
         sizeof(LocMarker*) * (l->n+1));
 
@@ -294,7 +294,7 @@
             return NULL;
 
         case '\\':
-            /* Enable continuation via '\' at EOL */
+            // Enable continuation via '\' at EOL
             i = locFGetc(f);
             if (i == EOF)
             {
@@ -341,7 +341,7 @@
     {
         switch (f->ch)
         {
-            /* Scenic marker flags */
+            // Scenic marker flags
         case '?':
             if (!locCheckMutex(f, flags, LOCF_M_MASK, LOCF_M_SCENIC1))
                 return FALSE;
@@ -359,7 +359,7 @@
                 return FALSE;
             break;
 
-            /* Marker type flags */
+            // Marker type flags
         case 'S':
             if (!locCheckMutex(f, flags, LOCF_T_MASK, LOCF_T_SHRINE))
                 return FALSE;
@@ -385,7 +385,7 @@
                 return FALSE;
             break;
 
-            /* Additional flags */
+            // Additional flags
         case '-':
             *flags |= LOCF_INVIS;
             break;
@@ -456,7 +456,7 @@
 
     switch (f->field)
     {
-    case 1:            /* X-coordinate */
+    case 1:            // X-coordinate
         res = parseFieldInt(f, &marker->x);
         f->fieldSep = ";";
         if (res)
@@ -468,7 +468,7 @@
             locPMErr(f, "Error parsing X-coordinate.\n");
         break;
 
-    case 2:            /* Y-coordinate */
+    case 2:            // Y-coordinate
         res = parseFieldInt(f, &marker->y);
         if (res)
         {
@@ -479,7 +479,7 @@
             locPMErr(f, "Error parsing Y-coordinate.\n");
         break;
 
-    case 3:            /* Label orientation and flags */
+    case 3:            // Label orientation and flags
         res = parseFieldInt(f, &marker->align);
         if (res)
             res = locParseFlags(f, &marker->flags);
@@ -493,15 +493,15 @@
             locPMErr(f, "Error parsing orientation and flags field.\n");
         break;
 
-    case 4: /* Location name(s) */
+    case 4: // Location name(s)
         locParseMultiField(f, "|;", '|', "location names", marker->names);
         break;
 
-    case 5:            /* Coders */
+    case 5:            // Coders
         locParseMultiField(f, ",;", ',', "coder names", marker->coders);
         break;
 
-    case 6:            /* Date */
+    case 6:            // Date
         marker->valid = FALSE;
         tmpStr = parseFieldString(f, f->fieldSep);
         if (tmpStr && tmpStr[0])
@@ -519,24 +519,24 @@
         locPMSet(f, PM_NEXT, PM_FIELD_SEP);
         break;
 
-    case 7:            /* URI */
+    case 7:            // URI
         th_free(marker->uri);
         marker->uri = parseFieldString(f, f->fieldSep);
         f->field++;
         locPMSet(f, PM_NEXT, PM_FIELD_SEP);
         break;
 
-    case 8:            /* Freeform */
+    case 8:            // Freeform
         tmpStr = parseFieldString(f, "\r\n");
 
-        /* Check coordinates */
+        // Check coordinates
         if (marker->x < 1 || marker->y < 1)
         {
             locPMErr(f, "Invalid X or Y coordinate (%d, %d), for location '%s'. Must be > 0.\n",
                 marker->x, marker->y, marker->names[0].name);
         }
 
-        /* Check if location already exists */
+        // Check if location already exists
         marker->x = marker->x + marker->ox - 1;
         marker->y = marker->y + marker->oy - 1;
 
@@ -550,7 +550,7 @@
         }
         else
         {
-            /* Add new location to our list */
+            // Add new location to our list
             locAddNew(l, marker->x, marker->y, marker->align, marker->flags,
                       marker->names, marker->coders, &marker->added,
                       marker->valid, marker->uri, tmpStr, f->file);
@@ -617,7 +617,7 @@
             else
             if (isdigit(ctx.ch))
             {
-                /* Start of a record */
+                // Start of a record
                 locPMSet(&ctx, PM_FIELD, -1);
                 ctx.field = 1;
             }
@@ -628,7 +628,7 @@
             }
             else
             {
-                /* Syntax error */
+                // Syntax error
                 locPMErr(&ctx, "Syntax error in '%s' line #%d.\n",
                     ctx.filename, ctx.lineNum);
             }
@@ -666,14 +666,14 @@
                 char *tmp = parseFieldString(&ctx, "(\n\r");
                 if (tmp != NULL && !strcmp(tmp, LOC_MAGIC))
                 {
-                    /* ID found, check version */
+                    // ID found, check version
                     char *verStr = parseFieldString(&ctx, ")\n\r");
                     int verMajor, verMinor;
                     if (verStr != NULL && sscanf(verStr, "(version %d.%d", &verMajor, &verMinor) == 2)
                     {
                         if (verMajor != LOC_VERSION_MAJOR)
                         {
-                            /* Major version mismatch, bail out with informative message */
+                            // Major version mismatch, bail out with informative message
                             THERR(
                                 "LOC file format version %d.%d detected, internal version is %d.%d. "
                                 "Refusing to read due to potential incompatibilities. If you neverthless "
@@ -684,7 +684,7 @@
                         else
                         if (verMinor != LOC_VERSION_MINOR)
                         {
-                            /* Minor version mismatch, just inform about it */
+                            // Minor version mismatch, just inform about it
                             THERR("LOC file format version %d.%d detected, internal version is %d.%d, proceeding.\n",
                                 verMajor, verMinor, LOC_VERSION_MAJOR, LOC_VERSION_MINOR);
                         }
@@ -720,7 +720,7 @@
                 ctx.ch = locFGetc(&ctx);
                 break;
             case '\\':
-                /* Enable continuation via '\' at EOL */
+                // Enable continuation via '\' at EOL
                 i = locFGetc(&ctx);
                 if (i != '\n' && i != '\r')
                 {
--- a/liblocfile.h	Thu Oct 26 22:20:25 2017 +0300
+++ b/liblocfile.h	Fri Oct 27 04:35:51 2017 +0300
@@ -45,43 +45,43 @@
  */
 #define LOCF_NONE           (0x00000)
 
-/* Marker types */
-#define LOCF_M_SCENIC1      (0x000001)   /* '?' Scenic marker */
-#define LOCF_M_SCENIC2      (0x000002)   /* '%' Shrine marker/etc */
-#define LOCF_M_PCITY        (0x000004)   /* 'C' Player city */
-#define LOCF_M_CITY         (0x000008)   /* 'c' City */
+// Marker types
+#define LOCF_M_SCENIC1      (0x000001)   // '?' Scenic marker
+#define LOCF_M_SCENIC2      (0x000002)   // '%' Shrine marker/etc
+#define LOCF_M_PCITY        (0x000004)   // 'C' Player city
+#define LOCF_M_CITY         (0x000008)   // 'c' City
 #define LOCF_M_MASK         (0x00000F)
 
-/* Location types */
-#define LOCF_T_SHRINE       (0x000010)   /* 'S' Raceshrine */
-#define LOCF_T_GUILD        (0x000020)   /* 'G' Guild */
-#define LOCF_T_SS           (0x000040)   /* 'P' Player guild/Secret Society */
-#define LOCF_T_MONSTER      (0x000080)   /* 'M' Special monster */
-#define LOCF_T_TRAINER      (0x000100)   /* 'T' Guild trainer */
-#define LOCF_T_FORT         (0x000200)   /* 'F' Regions fort */
+// Location types
+#define LOCF_T_SHRINE       (0x000010)   // 'S' Raceshrine
+#define LOCF_T_GUILD        (0x000020)   // 'G' Guild
+#define LOCF_T_SS           (0x000040)   // 'P' Player guild/Secret Society
+#define LOCF_T_MONSTER      (0x000080)   // 'M' Special monster
+#define LOCF_T_TRAINER      (0x000100)   // 'T' Guild trainer
+#define LOCF_T_FORT         (0x000200)   // 'F' Regions fort
 #define LOCF_T_MASK         (0x00FFF0)
 #define LOCF_MASK           (LOCF_M_MASK | LOCF_T_MASK)
 
-/* Extra flags */
-#define LOCF_INVIS          (0x010000)   /* '-' Invisible marker / Don't show label */
-#define LOCF_CLOSED         (0x020000)   /* '!' Area is CLOSED */
-#define LOCF_INSTANCED      (0x040000)   /* 'I' Location is "instanced" for each player */
-#define LOCF_INVALID        (0x400000)   /* Possibly invalid location */
-#define LOCF_NOMARKER       (0x800000)   /* Location has no marker in mapdata or explicitly defined */
+// Extra flags
+#define LOCF_INVIS          (0x010000)   // '-' Invisible marker / Don't show label
+#define LOCF_CLOSED         (0x020000)   // '!' Area is CLOSED
+#define LOCF_INSTANCED      (0x040000)   // 'I' Location is "instanced" for each player
+#define LOCF_INVALID        (0x400000)   // Possibly invalid location
+#define LOCF_NOMARKER       (0x800000)   // Location has no marker in mapdata or explicitly defined
 #define LOCF_Q_MASK         (0xFF0000)
 
 
 /* Misc constants
  */
-#define LOC_MAX_NAMES       (64)        /* Probably more than enough? */
+#define LOC_MAX_NAMES       (64)        // Probably more than enough?
 #define LOC_MARKERS         "?%C"
 #define LOC_MAX_FILES       (64)
 
 
-#define NAME_ORIG           (0x00001)   /* '@' Original area name or coder */
-#define NAME_RECODER        (0x00002)   /* '!' Converter or recoder of area */
-#define NAME_MAINTAINER     (0x00004)   /* '%' Maintainer */
-#define NAME_EXPANDER       (0x00008)   /* '&' Expander, adding new things */
+#define NAME_ORIG           (0x00001)   // '@' Original area name or coder
+#define NAME_RECODER        (0x00002)   // '!' Converter or recoder of area
+#define NAME_MAINTAINER     (0x00004)   // '%' Maintainer
+#define NAME_EXPANDER       (0x00008)   // '&' Expander, adding new things
 
 
 /* Structures
--- a/libmaputils.c	Thu Oct 26 22:20:25 2017 +0300
+++ b/libmaputils.c	Fri Oct 27 04:35:51 2017 +0300
@@ -156,7 +156,7 @@
         va_end(tap);
         if (n > -1 && n < bufsize)
         {
-            /* String fit the buffer, print it out and return */
+            // String fit the buffer, print it out and return
             int ret = mputs(buf, outFile);
             if (ret < 0)
                 return ret;
@@ -165,7 +165,7 @@
             return n;
         }
 
-        /* Didn't fit, try reallocating some more space */
+        // Didn't fit, try reallocating some more space
         if (n > -1)
             bufsize = n + 1;
         else
@@ -472,21 +472,21 @@
 
 /* Map block handling
  */
-MapBlock * mapBlockAlloc(int blockW, int blockH)
+MapBlock * mapBlockAlloc(const int width, const int height)
 {
     MapBlock *res;
 
-    /* Check arguments */
-    if (blockW <= 0 || blockH <= 0)
+    // Check arguments
+    if (width <= 0 || height <= 0)
         return NULL;
 
-    /* Allocate struct and data */
+    // Allocate struct and data
     res = (MapBlock *) th_malloc0(sizeof(MapBlock));
     if (!res) return NULL;
 
-    res->width = blockW;
-    res->height = blockH;
-    res->scansize = ((blockW / BLOCK_SCAN_ALIGN) + 1) * BLOCK_SCAN_ALIGN;
+    res->width = width;
+    res->height = height;
+    res->scansize = ((width / BLOCK_SCAN_ALIGN) + 1) * BLOCK_SCAN_ALIGN;
     res->size = res->height * res->scansize * sizeof(char);
 
     res->data = (unsigned char *) th_malloc0(res->size);
@@ -593,7 +593,7 @@
         pos = ftell(inFile);
     }
 
-    /* Probe map width */
+    // Probe map width
     resH = 1;
     resW = -1;
     x = 0;
@@ -618,7 +618,7 @@
         }
     }
 
-    /* Seek back */
+    // Seek back
     if (fseek(inFile, pos, SEEK_SET) == -1)
     {
         THERR("Could not rewind file '%s'.\n",
@@ -626,7 +626,7 @@
         return NULL;
     }
 
-    /* Allocate block */
+    // Allocate block
     if ((res = mapBlockAlloc(resW, resH)) == NULL)
     {
         THERR("Could not allocate mapblock (%d, %d) for '%s'.\n",
@@ -634,7 +634,7 @@
         return NULL;
     }
 
-    /* Read data */
+    // Read data
     o = res->data;
     y = 0;
     x = 0;
@@ -673,7 +673,7 @@
         }
     }
 
-    /* Close file */
+    // Close file
     if (y >= res->height)
     {
         THERR("Broken block in '%s', height %d >= %d\n", filename, y, res->height);
@@ -715,70 +715,67 @@
 /* Blit a block into another, assume that memory has been allocated
  * in sufficient way and other preparations are done.
  */
-int mapBlockPutDo(MapBlock *map, MapBlock *b, const int ox, const int oy)
+int mapBlockPutDo(MapBlock *map, const MapBlock *src, const int ox, const int oy)
 {
-    int x, y;
     assert(map != NULL);
     assert(b != NULL);
 
-    for (y = 0; y < b->height; y++)
-        for (x = 0; x < b->width; x++)
-        {
-            int dx = ox + x;
-            int dy = oy + y;
+    for (int y = 0; y < src->height; y++)
+    for (int x = 0; x < src->width; x++)
+    {
+        const int dx = ox + x;
+        const int dy = oy + y;
 
-            if (dx >= 0 && dx < map->width && dy >= 0 && dy < map->height)
-            {
-                char c = b->data[(y * b->scansize) + x];
+        if (dx >= 0 && dx < map->width && dy >= 0 && dy < map->height)
+        {
+            char c = src->data[(y * src->scansize) + x];
 
-                if (c != 0)
-                    map->data[(dy * map->scansize) + dx] = c;
-            }
-            else
-                return -1;
+            if (c != 0)
+                map->data[(dy * map->scansize) + dx] = c;
         }
-
-    b->mark = TRUE;
+        else
+            return -1;
+    }
 
     return 0;
 }
 
 
-int mapBlockPut(MapBlock **map, MapBlock *b, int ox, int oy)
+int mapBlockPut(MapBlock **pmap, const MapBlock *src, int ox, int oy)
 {
     MapBlock *tmp;
     int x0, y0, x1, y1, mx, my;
 
-    assert(map != NULL);
-    assert(*map != NULL);
-    assert(b != NULL);
+    assert(pmap != NULL);
+    assert(*pmap != NULL);
+    assert(src != NULL);
 
-    /* Determine new block size */
+    // Determine new block size
     x0 = mx = y0 = my = 0;
-    x1 = (*map)->width - 1;
-    y1 = (*map)->height - 1;
+    x1 = (*pmap)->width - 1;
+    y1 = (*pmap)->height - 1;
 
     if (ox < 0) { x0 = ox; mx = -ox; ox = 0; }
     if (oy < 0) { y0 = oy; my = -oy; oy = 0; }
 
-    if ((x0 + ox + b->width - 1) > x1)
-        x1 = (x0 + ox + b->width - 1);
+    if ((x0 + ox + src->width - 1) > x1)
+        x1 = (x0 + ox + src->width - 1);
 
-    if ((y0 + oy + b->height - 1) > y1)
-        y1 = (y0 + oy + b->height - 1);
+    if ((y0 + oy + src->height - 1) > y1)
+        y1 = (y0 + oy + src->height - 1);
 
-    /* Allocate new block */
+    // Allocate new block
     if ((tmp = mapBlockAlloc(x1 - x0 + 1, y1 - y0 + 1)) == NULL)
         return -1;
 
-    /* Copy data */
-    if (mapBlockPutDo(tmp, *map, mx, my) < 0)
+    // Copy data
+    if (mapBlockPutDo(tmp, *pmap, mx, my) < 0)
     {
         th_free(tmp);
         return -2;
     }
 
-    if (mapBlockPutDo(tmp, b, ox, oy) < 0)
+    if (mapBlockPutDo(tmp, src, ox, oy) < 0)
     {
         th_free(tmp);
         return -3;
@@ -787,9 +784,9 @@
     tmp->x = -mx;
     tmp->y = -my;
 
-    /* Out with the old, in with the new */
-    mapBlockFree(*map);
-    *map = tmp;
+    // Out with the old, in with the new
+    mapBlockFree(*pmap);
+    *pmap = tmp;
 
     return 0;
 }
@@ -799,19 +796,17 @@
  */
 void mapBlockClean(MapBlock *map, char *symbols)
 {
-    int x, y;
-    unsigned char *c;
     assert(map != NULL);
     assert(symbols != NULL);
 
-    for (y = 0; y < map->height; y++)
+    for (int y = 0; y < map->height; y++)
     {
-        c = map->data + (y * map->scansize);
-        for (x = 0; x < map->width; x++)
+        unsigned char *dp = map->data + (y * map->scansize);
+        for (int x = 0; x < map->width; x++)
         {
-            if (strchr(symbols, *c) != NULL)
-                *c = 0;
-            c++;
+            if (strchr(symbols, *dp) != NULL)
+                *dp = 0;
+            dp++;
         }
     }
 }
@@ -819,18 +814,16 @@
 
 void mapBlockPrintRaw(FILE *f, MapBlock *map)
 {
-    unsigned char *c;
-    int x, y;
     assert(f != NULL);
     assert(map != NULL);
 
-    for (y = 0; y < map->height; y++)
+    for (int y = 0; y < map->height; y++)
     {
-        c = map->data + (y * map->scansize);
-        for (x = 0; x < map->width; x++)
+        unsigned char *dp = map->data + (y * map->scansize);
+        for (int x = 0; x < map->width; x++)
         {
-            fputc(*c, f);
-            c++;
+            fputc(*dp, f);
+            dp++;
         }
 
         fputc(0xff, f);
--- a/libmaputils.h	Thu Oct 26 22:20:25 2017 +0300
+++ b/libmaputils.h	Fri Oct 27 04:35:51 2017 +0300
@@ -109,14 +109,14 @@
 
 /* Mapblock handling
  */
-MapBlock *  mapBlockAlloc(int, int);
-MapBlock *  mapBlockCopy(MapBlock *);
-void        mapBlockFree(MapBlock *);
-MapBlock *  mapBlockParseStream(const char *, FILE *, BOOL);
-MapBlock *  mapBlockParseFile(const char *, BOOL);
-void        mapBlockPrint(FILE *, MapBlock *);
-int         mapBlockPutDo(MapBlock *, MapBlock *, const int, const int);
-int         mapBlockPut(MapBlock **, MapBlock *, int, int);
+MapBlock *  mapBlockAlloc(int width, int height);
+MapBlock *  mapBlockCopy(MapBlock *block);
+void        mapBlockFree(MapBlock *block);
+MapBlock *  mapBlockParseStream(const char *filename, FILE *fh, BOOL isDiff);
+MapBlock *  mapBlockParseFile(const char *filename, BOOL isDiff);
+void        mapBlockPrint(FILE *fh, MapBlock *block);
+int         mapBlockPutDo(MapBlock *map, const MapBlock *src, const int ox, const int oy);
+int         mapBlockPut(MapBlock **pmap, const MapBlock *src, const int ox, const int oy);
 void        mapBlockClean(MapBlock *, char *);
 void        mapBlockPrintRaw(FILE *, MapBlock *);
 int         mapBlockGetEntropy(MapBlock *map, const unsigned char *exclude, const int nexclude);
--- a/map2ppm.c	Thu Oct 26 22:20:25 2017 +0300
+++ b/map2ppm.c	Fri Oct 27 04:35:51 2017 +0300
@@ -148,7 +148,7 @@
     int x, y, yscale, xscale, res = 0;
     uint8_t *row = NULL;
 
-    /* Allocate memory for row buffer */
+    // Allocate memory for row buffer
     if ((row = th_malloc(map->width * 3 * scale + 16)) == NULL)
     {
         res = -16;
@@ -234,12 +234,12 @@
 
 int writePPMFile(FILE *outFile, MapBlock *map, int scale)
 {
-    /* Write header for 24-bit PPM */
+    // Write header for 24-bit PPM
     fprintf(outFile,
         "P6\n%d %d\n255\n",
         map->width * scale, map->height * scale);
 
-    /* Write image data */
+    // Write image data
     return writeImageData(map, (void *) outFile, writePPMRow, scale);
 }
 
@@ -268,7 +268,7 @@
     width = map->width * scale;
     height = map->height * scale;
 
-    /* Create PNG structures */
+    // Create PNG structures
     png_ptr = png_create_write_struct(
         PNG_LIBPNG_VER_STRING,
         NULL, NULL, NULL);
@@ -294,7 +294,7 @@
 
     png_init_io(png_ptr, outFile);
 
-    /* Write PNG header info */
+    // Write PNG header info
     if (setjmp(png_jmpbuf(png_ptr)))
     {
         THERR("PNG: Error during writing header.\n");
@@ -304,8 +304,8 @@
     png_set_IHDR(png_ptr, info_ptr,
         width,
         height,
-        8,                    /* bits per component */
-        PNG_COLOR_TYPE_RGB,   /* 3 components, RGB */
+        8,                    // bits per component
+        PNG_COLOR_TYPE_RGB,   // 3 components, RGB
         PNG_INTERLACE_NONE,
         PNG_COMPRESSION_TYPE_DEFAULT,
         PNG_FILTER_TYPE_DEFAULT);
@@ -315,7 +315,7 @@
     png_write_info(png_ptr, info_ptr);
 
 
-    /* Write compressed image data */
+    // Write compressed image data
     if (setjmp(png_jmpbuf(png_ptr)))
     {
         THERR("PNG: Error during writing image data.\n");
@@ -325,7 +325,7 @@
     writeImageData(map, (void *) png_ptr, writePNGRow, scale);
 
 
-    /* Write footer */
+    // Write footer
     if (setjmp(png_jmpbuf(png_ptr)))
     {
         THERR("PNG: Error during writing image footer.\n");
@@ -334,7 +334,7 @@
 
     png_write_end(png_ptr, NULL);
 
-    /* Dellocate shit */
+    // Dellocate shit
     if (png_ptr && info_ptr)
         png_destroy_write_struct(&png_ptr, &info_ptr);
 
@@ -351,11 +351,11 @@
     MapBlock *map;
     int ret;
 
-    /* Initialize */
+    // Initialize
     th_init("map2ppm", "ASCII map to PPM/PNG image converter", "0.5", NULL, NULL);
     th_verbosityLevel = 0;
 
-    /* Parse arguments */
+    // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
         exit(1);
@@ -366,7 +366,7 @@
         exit(0);
     }
 
-    /* Read input file */
+    // Read input file
     THMSG(1, "Reading map file '%s'\n", srcFilename);
 
     if ((map = mapBlockParseFile(srcFilename, optInputIsDiff)) == NULL)
@@ -376,7 +376,7 @@
         exit(1);
     }
 
-    /* Open output file */
+    // Open output file
     if (destFilename == NULL)
         outFile = stdout;
     else
--- a/mapstats.c	Thu Oct 26 22:20:25 2017 +0300
+++ b/mapstats.c	Fri Oct 27 04:35:51 2017 +0300
@@ -213,11 +213,11 @@
     ulint_t statTotal, statUnknown;
     MapPieceStat statPieces[nmapPieces];
 
-    /* Initialize */
+    // Initialize
     th_init("mapstats", "ASCII map statistics generator", "0.2", NULL, NULL);
     th_verbosityLevel = 1;
 
-    /* Parse arguments */
+    // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
         exit(1);
@@ -228,7 +228,7 @@
         exit(0);
     }
 
-    /* Read input file */
+    // Read input file
     statTotal = statUnknown = 0;
     for (i = 0; i < nmapPieces; i++)
     {
@@ -268,7 +268,7 @@
         mapBlockFree(map);
     }
 
-    /* Sort results, if needed */
+    // Sort results, if needed
     if (optSortBy != SORT_NONE)
     {
         compareFunc tmpFunc;
@@ -287,7 +287,7 @@
         qsort(statPieces, nmapPieces, sizeof(MapPieceStat), tmpFunc);
     }
 
-    /* Open output file */
+    // Open output file
     if (destFilename == NULL)
         outFile = stdout;
     else
--- a/mkcitymap.c	Thu Oct 26 22:20:25 2017 +0300
+++ b/mkcitymap.c	Fri Oct 27 04:35:51 2017 +0300
@@ -404,7 +404,7 @@
     th_verbosityLevel = 0;
 
 
-    /* Parse arguments */
+    // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
         exit(1);
@@ -415,7 +415,7 @@
         exit(0);
     }
 
-    /* Parse location file */
+    // Parse location file
     setLocFileInfo(&locFile, optLocFilename, NULL);
     if ((inFile = fopen(locFile.filename, "rb")) == NULL)
     {
@@ -429,7 +429,7 @@
 
     fclose(inFile);
 
-    /* Open mapfile */
+    // Open mapfile
     map = mapBlockParseFile(optMapFilename, FALSE);
     if (map == NULL)
     {
@@ -448,12 +448,12 @@
         exit(1);
     }
 
-    /* Output data */
+    // Output data
     outputHTMLHeader(outFile, &locations);
     outputMapHTML(outFile, map, &locations);
     outputHTMLFooter(outFile, &locations);
 
-    /* Close input and output files */
+    // Close input and output files
     fclose(outFile);
 
     mapBlockFree(map);
--- a/mkloc.c	Thu Oct 26 22:20:25 2017 +0300
+++ b/mkloc.c	Fri Oct 27 04:35:51 2017 +0300
@@ -26,7 +26,7 @@
     GMAPS_LAST
 };
 
-/* These must be in lower case */
+// These must be in lower case
 const char *gmapsModes[GMAPS_LAST] =
 {
     "xml",
@@ -285,7 +285,7 @@
         if (optLabelType)
             len += locPrintType(NULL, tmp, TRUE, NULL, FALSE);
 
-        /* Compute text location */
+        // Compute text location
         switch (tmp->align)
         {
         case LOCD_LEFTDOWN:
@@ -319,7 +319,7 @@
         if (y < 0) y += 2;
         if (y > map->height) y -= (y - map->height - 1);
 
-        /* Update location info */
+        // Update location info
         tmp->x = x0;
         tmp->y = y;
     }
@@ -359,7 +359,7 @@
 
     THMSG(2, "Updating location information ..\n");
 
-    /* Find new, unknown locations */
+    // Find new, unknown locations
     for (y = 0; y < worldMap->height; y++)
     {
         dp = worldMap->data + (worldMap->scansize * y);
@@ -372,7 +372,7 @@
                 char tmpDesc[512];
                 int tmpFlags;
 
-                /* Check for new locations */
+                // Check for new locations
                 if (locFindByCoords(worldLoc, x, y, TRUE) >= 0)
                     continue;
 
@@ -413,21 +413,21 @@
             }
             else
             {
-                /* Check for misplaced locations */
+                // Check for misplaced locations
                 if ((n = locFindByCoords(worldLoc, x, y, TRUE)) >= 0)
                 {
                     tmpl = worldLoc->locations[n];
 
                     if (tmpl->flags == LOCF_NONE)
                     {
-                        /* Mark as possibly invalid */
+                        // Mark as possibly invalid
                         tmpl->flags |= LOCF_INVALID;
                         numInvLoc++;
                     }
 
                     if ((tmpl->flags & LOCF_M_MASK) == 0)
                     {
-                        /* No apparent marker */
+                        // No apparent marker
                         tmpl->flags |= LOCF_NOMARKER;
                         numNoMarker++;
                     }
@@ -628,7 +628,7 @@
 {
     int i, n;
 
-    /* Output header */
+    // Output header
     fprintf(outFile,
     "# " LOC_MAGIC " (version %d.%d)\n",
     LOC_VERSION_MAJOR, LOC_VERSION_MINOR
@@ -639,12 +639,12 @@
     "#\n"
     );
 
-    /* Output each location entry */
+    // Output each location entry
     for (i = 0; i < l->n; i++)
     {
         LocMarker *tmp = l->locations[i];
 
-        /* Add comment in few cases */
+        // Add comment in few cases
         if (tmp->flags & LOCF_Q_MASK)
         {
             char *s = NULL;
@@ -740,21 +740,21 @@
 {
     int i, numCity, numLoc, numTotal;
 
-    /* Output script start */
+    // Output script start
     fprintf(outFile,
         "#!/bin/sh\n"
         "convert \"$1\" @OPTS_START@ \\\n");
 
     numCity = numLoc = numTotal = 0;
 
-    /* Output instructions for each visible location */
+    // Output instructions for each visible location
     for (i = 0; i < l->n; i++)
     {
         LocMarker *tmp = l->locations[i];
         int x, y, leftMove;
         char *cs;
 
-        /* Is location visible? */
+        // Is location visible?
         if (tmp->flags & LOCF_INVIS)
             continue;
 
@@ -784,7 +784,7 @@
             break;
         }
 
-        /* Determine colour */
+        // Determine colour
         switch (tmp->flags & LOCF_M_MASK)
         {
         case LOCF_M_CITY:
@@ -814,7 +814,7 @@
 
         numTotal++;
 
-        /* Location marker */
+        // Location marker
         fprintf(outFile,
             "\t-fill black -draw \"circle %d,%d %d,%d\" ",
             tmp->x, tmp->y,
@@ -827,7 +827,7 @@
             (int) (tmp->y + optUnitSize * 0.90f));
 
 
-        /* Location description text */
+        // Location description text
         if (!optNoLabels)
         {
             fprintf(outFile,
@@ -900,7 +900,7 @@
         fpr(outFile, "</b><br>");
     }
 
-    /* Alternative names, if any */
+    // Alternative names, if any
     if (tmp->nnames > 1)
     {
         fpr(outFile, "Also known as <i>");
@@ -915,14 +915,14 @@
         fpr(outFile, "</i>.<br>");
     }
 
-    /* Added to game timestamp */
+    // Added to game timestamp
     if (tmp->valid)
     {
         fpr(outFile, "Added " LOC_TIMEFMT ".<br>",
             tmp->added.day, tmp->added.month, tmp->added.year);
     }
 
-    /* Coder names or societies */
+    // Coder names or societies
     if (tmp->ncoders > 0)
     {
         if (tmp->flags & LOCF_M_PCITY)
@@ -960,7 +960,7 @@
         fps(".<br>", outFile);
     }
 
-    /* Print out freeform information field */
+    // Print out freeform information field
     if (tmp->freeform)
     {
         char *ptr = tmp->freeform;
@@ -1001,30 +1001,30 @@
     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
     "<markers>\n");
 
-    /* Output each location entry */
+    // Output each location entry
     for (i = 0; i < l->n; i++)
     {
         LocMarker *tmp = l->locations[i];
 
-        /* Skip disabled / invisible locations */
+        // Skip disabled / invisible locations
         if (tmp->flags & (LOCF_INVIS | LOCF_INVALID)) continue;
 
-        /* Print out coordinates etc. */
+        // Print out coordinates etc.
         fprintf(outFile, "<marker x=\"%d\" y=\"%d\" labeldir=\"%d\" name=\"",
             tmp->ox, tmp->oy, tmp->align);
 
-        /* Location name */
+        // Location name
         locPrintType(outFile, tmp, FALSE, fputse, FALSE);
 
         fprintf(outFile, "\" html=\"");
 
         outputGMapsHTML(outFile, tmp, fprintfe, fputse);
 
-        /* Flags */
+        // Flags
         fprintf(outFile, "\" flags=\"%d\"",
             tmp->flags);
 
-        /* Continent name */
+        // Continent name
         if (tmp->file != NULL && tmp->file->continent != NULL)
         {
             fprintf(outFile, " continent=\"");
@@ -1032,7 +1032,7 @@
             fprintf(outFile, "\"");
         }
 
-        /* Type of the marker */
+        // Type of the marker
         fprintf(outFile, " type=\"%s\"/>\n", locGetLocationType(tmp->flags));
     }
 
@@ -1046,29 +1046,29 @@
 
     fprintf(outFile, "[\n");
 
-    /* Output each location entry */
+    // Output each location entry
     for (i = 0; i < l->n; i++)
     {
         LocMarker *tmp = l->locations[i];
 
-        /* Skip disabled / invisible locations */
+        // Skip disabled / invisible locations
         if (tmp->flags & (LOCF_INVIS | LOCF_INVALID)) continue;
 
-        /* Print out coordinates etc. */
+        // Print out coordinates etc.
         fprintf(outFile, "{\"x\":%d,\"y\":%d,\"labeldir\":%d,\"name\":\"",
             tmp->ox, tmp->oy, tmp->align);
 
-        /* Location name */
+        // Location name
         locPrintType(outFile, tmp, FALSE, fputsesc1, FALSE);
         fprintf(outFile, "\",\"html\":\"");
 
         outputGMapsHTML(outFile, tmp, fprintfesc1, fputsesc1);
 
-        /* Flags */
+        // Flags
         fprintf(outFile, "\",\"flags\":%d",
             tmp->flags);
 
-        /* Continent name */
+        // Continent name
         if (tmp->file != NULL && tmp->file->continent != NULL)
         {
             fprintf(outFile, ",\"continent\":\"");
@@ -1076,7 +1076,7 @@
             fprintf(outFile, "\"");
         }
 
-        /* Type of the marker */
+        // Type of the marker
         fprintf(outFile, "}");
 
         if (i < l->n - 1)
@@ -1098,16 +1098,16 @@
 
     memset(&worldLoc, 0, sizeof(worldLoc));
 
-    /* Initialize */
+    // Initialize
     th_init("mkloc", "Manipulate and convert location files and ASCII map data", "1.6", NULL, NULL);
     th_verbosityLevel = 0;
 
-    /* Parse arguments */
+    // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, NULL, OPTH_BAILOUT))
         exit(1);
 
-    /* Check the mode and arguments */
+    // Check the mode and arguments
     if (srcFile == NULL && optGetUpdateLoc && optOutput == OUTFMT_LOCFILE)
     {
         THERR("Map file required for location update mode!\n");
@@ -1132,7 +1132,7 @@
         exit(0);
     }
 
-    /* Read initial map */
+    // Read initial map
     if (srcFile)
     {
         THMSG(2, "Reading map '%s'\n", srcFile);
@@ -1146,7 +1146,7 @@
         THMSG(2, "Initial dimensions (%d x %d)\n", worldMap->width, worldMap->height);
     }
 
-    /* Read location info */
+    // Read location info
     for (i = 0; i <= nlocFiles; i++)
     {
         LocFileInfo *f = &locFiles[i];
@@ -1172,11 +1172,11 @@
         fclose(inFile);
     }
 
-    /* Update locations */
+    // Update locations
     if (optGetUpdateLoc)
         updateLocations(worldMap, &worldLoc);
 
-    /* Scale locations */
+    // Scale locations
     if (optScale > 0)
     {
         int i;
@@ -1192,7 +1192,7 @@
         }
     }
 
-    /* Open output file */
+    // Open output file
     if (destFile == NULL)
         outFile = stdout;
     else
@@ -1203,7 +1203,7 @@
         exit(1);
     }
 
-    /* Output results */
+    // Output results
     switch (optOutput) {
     case OUTFMT_SCRIPT:
         THMSG(1, "Generating ImageMagick script ...\n");
--- a/mkspecial.c	Thu Oct 26 22:20:25 2017 +0300
+++ b/mkspecial.c	Fri Oct 27 04:35:51 2017 +0300
@@ -208,16 +208,16 @@
 
         if (c1 == 0 || c2 == 0)
         {
-            /* Both empty ... */
+            // Both empty ...
         } else
         if (c1 != 0 && c1 == c2)
         {
-            /* Exact match, increase % */
+            // Exact match, increase %
             n++;
         } else
         if (hardDrop)
         {
-            /* Mismatch, return failure */
+            // Mismatch, return failure
             return -1;
         }
     }
@@ -303,7 +303,7 @@
         return NULL;
     }
 
-    /* Check for discardable lines */
+    // Check for discardable lines
     if (!getLineData(inFile, s, sizeof(s)))
         return NULL;
 
@@ -313,7 +313,7 @@
             return NULL;
     }
 
-    /* New 'map' output in city maps has an empty line before the data, ignore it */
+    // New 'map' output in city maps has an empty line before the data, ignore it
     if ((tmpW = getLineWidth(s, sizeof(s))) <= 9)
     {
         if (!getLineData(inFile, s, sizeof(s)))
@@ -401,7 +401,7 @@
     th_init("mkspecial", "Yet Another ASCII Map Auto-Stitcher", "0.5", NULL, NULL);
     th_verbosityLevel = 1;
 
-    /* Parse arguments */
+    // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
         exit(1);
@@ -412,7 +412,7 @@
         exit(0);
     }
 
-    /* Read initial map */
+    // Read initial map
     if (optInitialMap)
     {
         THMSG(1, "Reading initial map '%s'\n", optInitialMap);
@@ -521,19 +521,19 @@
      */
 
 
-    /* If initial map is available, find a match and determine coords */
+    // If initial map is available, find a match and determine coords
     if (initialMap)
     {
         initialMap->x = optInitialX;
         initialMap->y = optInitialY;
     }
 
-    /* Clear marks */
+    // Clear marks
     for (i = 0; i < nmapBlocks; i++)
         mapBlocks[i]->mark = FALSE;
 
 
-    /* Get world dimensions */
+    // Get world dimensions
     worldX0 = worldY0 = worldX1 = worldY1 = 0;
     for (i = 0; i < nmapBlocks; i++)
         findWorldSize(mapBlocks[i], &worldX0, &worldY0, &worldX1, &worldY1);
@@ -541,8 +541,8 @@
     if (initialMap)
         findWorldSize(initialMap, &worldX0, &worldY0, &worldX1, &worldY1);
 
-    /* Compute offsets, allocate world map */
-    /* FIXME: check dimensions */
+    // Compute offsets, allocate world map
+    // FIXME: check dimensions
     offsetX = -worldX0;
     offsetY = -worldY0;
 
@@ -552,7 +552,7 @@
         exit(4);
     }
 
-    /* Place optional initial map */
+    // Place optional initial map
     if (initialMap)
     {
         if (mapBlockPut(&worldMap, initialMap, offsetX+initialMap->x, offsetY+initialMap->y) < 0)
@@ -569,28 +569,29 @@
             THERR("Initial map mapBlockPut() failed!\n");
             exit(9);
         }
+        mapBlocks[i]->mark = TRUE;
     }
 
     THMSG(1, "Initialized world map of (%d x %d), offset (%d, %d)\n",
         worldMap->width, worldMap->height, offsetX, offsetY);
 
 
-    /* Start placing blocks */
+    // Start placing blocks
     currRounds = 0;
     isOK = FALSE;
-    while ((currRounds++ < optRounds) && !isOK)
+    while (currRounds++ < optRounds && !isOK)
     {
         int usedBlocks;
 
-        /* Get number of used blocks */
+        // Get number of used blocks
         for (usedBlocks = i = 0; i < nmapBlocks; i++)
             if (mapBlocks[i]->mark) usedBlocks++;
 
-        /* Print out status information */
+        // Print out status information
         THPRINT(2, "#%d [%d/%d]: ",
             currRounds, usedBlocks, nmapBlocks);
 
-        /* Place and match blocks */
+        // Place and match blocks
         isOK = TRUE;
         currBlocks = 0;
         for (i = 0; i < nmapBlocks && currBlocks < optReset; i++)
@@ -599,7 +600,7 @@
 
             if (!tmp->mark)
             {
-                int    qx = offsetX + tmp->x,
+                int qx = offsetX + tmp->x,
                     qy = offsetY + tmp->y;
 
                 isOK = FALSE;
@@ -612,6 +613,7 @@
                             offsetX, offsetY, i);
                         exit(9);
                     }
+                    tmp->mark = TRUE;
 
                     currBlocks++;
                     THPRINT(2, "X");
@@ -621,7 +623,7 @@
                     THPRINT(2, ".");
 
 #if 0
-                /* Debug unmatching blocks */
+                // Debug unmatching blocks
                 char mysti[512];
                 snprintf(mysti, sizeof(mysti),
                     "[%d]: %d,%d (%d,%d)",
@@ -661,20 +663,21 @@
 
         fclose(tmpFile);
 
-        /* Compute number of unused blocks */
+        // Compute number of unused blocks
         for (unusedBlocks = i = 0; i < nmapBlocks; i++)
-            if (!mapBlocks[i]->mark)
+        if (!mapBlocks[i]->mark)
+        {
+            if (optDumpRejected)
             {
-                if (optDumpRejected)
-                {
-                    fprintf(stderr, "\n#%d: %d,%d (%d,%d)\n",
-                        i, mapBlocks[i]->x, mapBlocks[i]->y,
-                        mapBlocks[i]->x + offsetX, mapBlocks[i]->y + offsetY);
+                fprintf(stderr, "\n#%d: %d,%d (%d,%d)\n",
+                    i, mapBlocks[i]->x, mapBlocks[i]->y,
+                    mapBlocks[i]->x + offsetX,
+                    mapBlocks[i]->y + offsetY);
 
-                    mapBlockPrint(stderr, mapBlocks[i]);
-                }
-                unusedBlocks++;
+                mapBlockPrint(stderr, mapBlocks[i]);
             }
+            unusedBlocks++;
+        }
 
         THMSG(1, "%d mapblocks unused/discarded\n", unusedBlocks);
     }
--- a/patchmap.c	Thu Oct 26 22:20:25 2017 +0300
+++ b/patchmap.c	Fri Oct 27 04:35:51 2017 +0300
@@ -94,14 +94,12 @@
 {
     MapBlock *map, *patch;
     FILE *outFile;
-    int x, y;
-    unsigned char *s, *d;
 
-    /* Initialize */
+    // Initialize
     th_init("patchmap", "Patch a mapfile with a diff", "0.1", NULL, NULL);
     th_verbosityLevel = 1;
 
-    /* Parse arguments */
+    // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
         exit(1);
@@ -112,14 +110,14 @@
         exit(0);
     }
 
-    /* Read map and patch file */
+    // Read map and patch file
     if ((map = mapBlockParseFile(mapFilename, FALSE)) == NULL)
         exit(2);
 
     if ((patch = mapBlockParseFile(patchFilename, TRUE)) == NULL)
         exit(3);
 
-    /* Check map and diff sizes */
+    // Check map and diff sizes
     if (optCheck && (map->width != patch->width || map->height != patch->height))
     {
         THERR("Map and patch dimensions do not match (%d x %d [map] <-> %d x %d [patch])\n",
@@ -128,15 +126,16 @@
         exit(4);
     }
 
-    /* Generate */
-    for (y = 0; y < map->height; y++)
+    // Generate
+    for (int y = 0; y < map->height; y++)
     {
-        d = map->data + (map->scansize * y);
-        s = patch->data + (patch->scansize * y);
+        unsigned char
+            *d = map->data + (map->scansize * y),
+            *s = patch->data + (patch->scansize * y);
 
-        for (x = 0; x < map->width; x++)
+        for (int x = 0; x < map->width; x++)
         {
-            int i = (*s & 63);
+            const int i = (*s & 63);
             unsigned char c;
 
             if (*s == 0xfd)
@@ -162,11 +161,12 @@
             else
                 *d = c;
 
-            s++; d++;
+            s++;
+            d++;
         }
     }
 
-    /* Open output file */
+    // Open output file
     if (destFilename == NULL)
         outFile = stdout;
     else