changeset 1772:f4c49fd6557e

And more cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 27 Oct 2017 05:17:35 +0300
parents 72adabd8e746
children 7c254d090221
files libmaputils.c libmaputils.h
diffstat 2 files changed, 109 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/libmaputils.c	Fri Oct 27 05:10:08 2017 +0300
+++ b/libmaputils.c	Fri Oct 27 05:17:35 2017 +0300
@@ -35,57 +35,57 @@
 
 const MapPiece mapPieces[] =
 {
-{ '!', "Mountain Peak",     0xcc,0xff,0xff, col_white,          -1, },
-{ '#', "Ruins",             0x88,0x88,0x88, col_light_black,    -1, },
-{ '%', "Special Location",  0xff,0xff,0xff, col_light_white,    -1, },
-{ '+', "Crossing",          0x33,0x33,0x33, col_light_black,    -1, },
-{ '-', "Road",              0x33,0x33,0x33, col_light_black,    -1, },
-{ '|', "Road",              0x33,0x33,0x33, col_light_black,    -1, },
-{ '/', "Road",              0x33,0x33,0x33, col_light_black,    -1, },
-{ '\\',"Road",              0x33,0x33,0x33, col_light_black,    -1, },
-{ '.', "Plains",            0x55,0x92,0x00, col_green,          -1, },
-{ '=', "Bridge",            0x33,0x33,0x33, col_light_black,    -1, },
-{ '?', "Scenic Location",   0xff,0xff,0xff, col_light_white,    -1, },
-{ '@', "Flowing Lava",      0xff,0x99,0x3f, col_very_light_red, -1, },
-{ 'C', "Player City",       0x88,0x88,0x88, col_light_black,    -1, },
-{ 'F', "Deep Forest",       0x00,0x88,0x00, col_green,          -1, },
-{ 'H', "Highlands",         0x66,0x3f,0x00, col_magenta,        -1, },
-{ 'L', "Lava Lake",         0xff,0x50,0x00, col_very_light_red, -1, },
-{ 'R', "Deep River",        0x33,0x66,0xff, col_blue,           -1, },
-{ 'V', "Volcano",           0xff,0x33,0x00, col_red,            -1, },
-{ '^', "Mountain",          0x71,0x82,0x92, col_light_magenta,  -1, },
-{ 'b', "Beach",             0xcf,0xc4,0xa5, col_yellow,         -1, },
-{ 'c', "City",              0x88,0x88,0x88, col_light_black,    -1, },
-{ 'd', "Desert",            0xee,0xaa,0x22, col_yellow,         -1, },
-{ 'f', "Forest",            0x00,0xb6,0x00, col_light_green,    -1, },
-{ 'h', "Hills",             0x99,0x66,0x00, col_magenta,        -1, },
-{ 'i', "Ice",               0xee,0xee,0xff, col_light_blue,     -1, },
-{ 'j', "Jungle",            0x13,0x96,0x36, col_green,          -1, },
-{ 'l', "Lake",              0x21,0x33,0xcc, col_light_blue,     -1, },
-{ 'r', "River",             0x66,0x99,0xff, col_light_blue,     -1, },
-{ 's', "Swamp",             0x9d,0xa8,0x0a, col_light_red,      -1, },
-{ 't', "Tundra",            0x61,0xc3,0xa2, col_white,          -1, },
-{ 'v', "Valley",            0x22,0xdd,0x22, col_light_green,    -1, },
-{ 'w', "Waterfall",         0x77,0xaa,0xff, col_light_cyan,     -1, },
-{ 'x', "Badlands",          0x8a,0x83,0x60, col_light_red,      -1, },
-{ 'y', "Fields",            0xa7,0xcc,0x14, col_yellow,         -1, },
-{ 'z', "Shore",             0xa7,0xcc,0x14, col_light_yellow,   -1, },
-{ ',', "Muddy Trail",       0x8c,0x57,0x38, col_light_yellow,   -1, },
-{ '&', "Monster",           0xff,0x00,0x00, col_light_red,      -1, },
+    { '!', "Mountain Peak",     0xcc,0xff,0xff, col_white,          -1, },
+    { '#', "Ruins",             0x88,0x88,0x88, col_light_black,    -1, },
+    { '%', "Special Location",  0xff,0xff,0xff, col_light_white,    -1, },
+    { '+', "Crossing",          0x33,0x33,0x33, col_light_black,    -1, },
+    { '-', "Road",              0x33,0x33,0x33, col_light_black,    -1, },
+    { '|', "Road",              0x33,0x33,0x33, col_light_black,    -1, },
+    { '/', "Road",              0x33,0x33,0x33, col_light_black,    -1, },
+    { '\\',"Road",              0x33,0x33,0x33, col_light_black,    -1, },
+    { '.', "Plains",            0x55,0x92,0x00, col_green,          -1, },
+    { '=', "Bridge",            0x33,0x33,0x33, col_light_black,    -1, },
+    { '?', "Scenic Location",   0xff,0xff,0xff, col_light_white,    -1, },
+    { '@', "Flowing Lava",      0xff,0x99,0x3f, col_very_light_red, -1, },
+    { 'C', "Player City",       0x88,0x88,0x88, col_light_black,    -1, },
+    { 'F', "Deep Forest",       0x00,0x88,0x00, col_green,          -1, },
+    { 'H', "Highlands",         0x66,0x3f,0x00, col_magenta,        -1, },
+    { 'L', "Lava Lake",         0xff,0x50,0x00, col_very_light_red, -1, },
+    { 'R', "Deep River",        0x33,0x66,0xff, col_blue,           -1, },
+    { 'V', "Volcano",           0xff,0x33,0x00, col_red,            -1, },
+    { '^', "Mountain",          0x71,0x82,0x92, col_light_magenta,  -1, },
+    { 'b', "Beach",             0xcf,0xc4,0xa5, col_yellow,         -1, },
+    { 'c', "City",              0x88,0x88,0x88, col_light_black,    -1, },
+    { 'd', "Desert",            0xee,0xaa,0x22, col_yellow,         -1, },
+    { 'f', "Forest",            0x00,0xb6,0x00, col_light_green,    -1, },
+    { 'h', "Hills",             0x99,0x66,0x00, col_magenta,        -1, },
+    { 'i', "Ice",               0xee,0xee,0xff, col_light_blue,     -1, },
+    { 'j', "Jungle",            0x13,0x96,0x36, col_green,          -1, },
+    { 'l', "Lake",              0x21,0x33,0xcc, col_light_blue,     -1, },
+    { 'r', "River",             0x66,0x99,0xff, col_light_blue,     -1, },
+    { 's', "Swamp",             0x9d,0xa8,0x0a, col_light_red,      -1, },
+    { 't', "Tundra",            0x61,0xc3,0xa2, col_white,          -1, },
+    { 'v', "Valley",            0x22,0xdd,0x22, col_light_green,    -1, },
+    { 'w', "Waterfall",         0x77,0xaa,0xff, col_light_cyan,     -1, },
+    { 'x', "Badlands",          0x8a,0x83,0x60, col_light_red,      -1, },
+    { 'y', "Fields",            0xa7,0xcc,0x14, col_yellow,         -1, },
+    { 'z', "Shore",             0xa7,0xcc,0x14, col_light_yellow,   -1, },
+    { ',', "Muddy Trail",       0x8c,0x57,0x38, col_light_yellow,   -1, },
+    { '&', "Monster",           0xff,0x00,0x00, col_light_red,      -1, },
 #ifndef SECRET_MAP_DATA_FORMAT
-{ 'S', "Shallows",          0x44,0xcc,0xcc, col_light_cyan,     -1, },
-{ '~', "Sea",               0x11,0x88,0xdd, col_blue,           -1, },
+    { 'S', "Shallows",          0x44,0xcc,0xcc, col_light_cyan,     -1, },
+    { '~', "Sea",               0x11,0x88,0xdd, col_blue,           -1, },
 #else
-{ '~', "Sea 1",             0x00,0x11,0x88, col_blue,           -1, },
-{ '"', "Sea 2",             0x11,0x22,0x99, col_blue,           -1, },
-{ '\'',"Sea 3",             0x11,0x33,0xaa, col_blue,           -1, },
-{ '`', "Shallows?",         0x11,0x66,0xdd, col_blue,           -1, },
-{ 'p', "Plains?",           0x55,0x92,0x00, col_green,          -1, },
-{ 'S', "Swamp?",            0x77,0x77,0x33, col_yellow,         -1, },
+    { '~', "Sea 1",             0x00,0x11,0x88, col_blue,           -1, },
+    { '"', "Sea 2",             0x11,0x22,0x99, col_blue,           -1, },
+    { '\'',"Sea 3",             0x11,0x33,0xaa, col_blue,           -1, },
+    { '`', "Shallows?",         0x11,0x66,0xdd, col_blue,           -1, },
+    { 'p', "Plains?",           0x55,0x92,0x00, col_green,          -1, },
+    { 'S', "Swamp?",            0x77,0x77,0x33, col_yellow,         -1, },
 #endif
-{ -1 , "Road",              0x33,0x33,0x33, col_light_black,    '.', },
-{ -1 , "Plains",            0x00,0xff,0x00, col_light_green,    'p', },
-{ -1 , "Highlands",         0x77,0x00,0x77, col_magenta,        'i', },
+    { -1 , "Road",              0x33,0x33,0x33, col_light_black,    '.', },
+    { -1 , "Plains",            0x00,0xff,0x00, col_light_green,    'p', },
+    { -1 , "Highlands",         0x77,0x00,0x77, col_magenta,        'i', },
 };
 
 const int nmapPieces = sizeof(mapPieces) / sizeof(mapPieces[0]);
@@ -93,25 +93,24 @@
 
 const MapPiece mapCityPieces[] =
 {
-{ '.', "Street",            0xaa,0xaa,0x00, col_yellow,         -1, },
-{ '-', "Door/Gate",         0xff,0xff,0x00, col_light_yellow,   -1, },
-{ '|', "Door/Gate",         0xff,0xff,0x00, col_light_yellow,   -1, },
-{ '=', "Bridge/Gate",       0x33,0x33,0x33, col_yellow,         -1, },
-{ '*', "Fountain",          0x00,0x00,0xff, col_light_blue,     -1, },
-{ '@', "???",               0xff,0xff,0x00, col_light_yellow,   -1, },
-{ '$', "Tree",              0x00,0xaa,0x00, col_green,          -1, },
-{ '#', "Wall",              0x33,0x33,0x33, col_light_black,    -1, },
-{ '"', "Grass",             0x00,0x88,0x00, col_green,          -1, },
-{ ':', "???",               0x00,0x00,0x00, col_light_white,    -1, },
-{ 'z', "Shore",             0xa7,0xcc,0x14, col_yellow,         -1, },
-{ ',', "Lawn",              0x00,0xcc,0x00, col_green,          -1, },
+    { '.', "Street",            0xaa,0xaa,0x00, col_yellow,         -1, },
+    { '-', "Door/Gate",         0xff,0xff,0x00, col_light_yellow,   -1, },
+    { '|', "Door/Gate",         0xff,0xff,0x00, col_light_yellow,   -1, },
+    { '=', "Bridge/Gate",       0x33,0x33,0x33, col_yellow,         -1, },
+    { '*', "Fountain",          0x00,0x00,0xff, col_light_blue,     -1, },
+    { '@', "???",               0xff,0xff,0x00, col_light_yellow,   -1, },
+    { '$', "Tree",              0x00,0xaa,0x00, col_green,          -1, },
+    { '#', "Wall",              0x33,0x33,0x33, col_light_black,    -1, },
+    { '"', "Grass",             0x00,0x88,0x00, col_green,          -1, },
+    { ':', "???",               0x00,0x00,0x00, col_light_white,    -1, },
+    { 'z', "Shore",             0xa7,0xcc,0x14, col_yellow,         -1, },
+    { ',', "Lawn",              0x00,0xcc,0x00, col_green,          -1, },
 
-// Old versions
-{  -1, "Street",            0x00,0x00,0x00, col_white,          '.', },
-{  -1, "Door/Gate",         0x00,0x00,0x00, col_yellow,         '-', },
-{  -1, "Door/Gate",         0x00,0x00,0x00, col_yellow,         '|', },
-{  -1, "Fountain",          0x00,0x00,0x00, col_light_white,    '*', },
-
+    // Old versions
+    {  -1, "Street",            0x00,0x00,0x00, col_white,          '.', },
+    {  -1, "Door/Gate",         0x00,0x00,0x00, col_yellow,         '-', },
+    {  -1, "Door/Gate",         0x00,0x00,0x00, col_yellow,         '|', },
+    {  -1, "Fountain",          0x00,0x00,0x00, col_light_white,    '*', },
 };
 
 const int nmapCityPieces = sizeof(mapCityPieces) / sizeof(mapCityPieces[0]);
@@ -192,10 +191,9 @@
 
     while (*s)
     {
-        int i;
         BOOL found = FALSE;
 
-        for (i = 0; i < numHTMLEntities; i++)
+        for (int i = 0; i < numHTMLEntities; i++)
         if (HTMLEntities[i].c == *s)
         {
             fprintf(outFile, "&%s;", HTMLEntities[i].ent);
@@ -334,16 +332,14 @@
 
 static int muGetPieceFromList(const MapPiece pieces[], const int npieces, int symbol, BOOL getOld)
 {
-    int i;
-
-    for (i = 0; i < npieces; i++)
+    for (int i = 0; i < npieces; i++)
     {
         if ((getOld && pieces[i].oldSymbol == symbol) ||
             (!getOld && pieces[i].symbol == symbol))
             return i;
     }
 
-    for (i = 0; i < npieces; i++)
+    for (int i = 0; i < npieces; i++)
     {
         if (getOld && pieces[i].symbol == symbol)
             return i;
@@ -366,18 +362,16 @@
 
 static int muGetColorFromList(const MapPiece pieces[], const int npieces, int symbol, BOOL getOld)
 {
-    int i;
-
     if (getOld)
     {
-        for (i = 0; i < npieces; i++)
-            if (pieces[i].oldSymbol == symbol)
-                return pieces[i].color;
+        for (int i = 0; i < npieces; i++)
+        if (pieces[i].oldSymbol == symbol)
+            return pieces[i].color;
     }
 
-    for (i = 0; i < npieces; i++)
-        if (pieces[i].symbol == symbol)
-             return pieces[i].color;
+    for (int i = 0; i < npieces; i++)
+    if (pieces[i].symbol == symbol)
+         return pieces[i].color;
 
     return -1;
 }
@@ -432,10 +426,9 @@
 
 void muPrintHTMLcolors(FILE *outFile, const char *tagName, const char *propName, const char *extra)
 {
-    int n;
     assert(outFile != NULL);
 
-    for (n = 0; n < nmapColors; n++)
+    for (int n = 0; n < nmapColors; n++)
     {
         fprintf(outFile,
         "  %s.%c { %s: #%02x%02x%02x;%s%s }\n",
@@ -511,7 +504,7 @@
 }
 
 
-MapBlock * mapBlockCopy(MapBlock *block)
+MapBlock * mapBlockCopy(const MapBlock *block)
 {
     MapBlock *res;
 
@@ -687,27 +680,24 @@
 
 /* Print block to given output stream
  */
-void mapBlockPrint(FILE *f, MapBlock *map)
+void mapBlockPrint(FILE *fh, const MapBlock *map)
 {
-    unsigned char *c;
-    int x, y;
-    assert(f != NULL);
+    assert(fh != 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 *c = map->data + (y * map->scansize);
+        for (int x = 0; x < map->width; x++)
         {
             if (*c > 0)
-                fputc(*c, f);
+                fputc(*c, fh);
             else
-                fputc(' ', f);
+                fputc(' ', fh);
 
             c++;
         }
-        fprintf(f, "\n");
+        fprintf(fh, "\n");
     }
 }
 
@@ -718,7 +708,7 @@
 int mapBlockPutDo(MapBlock *map, const MapBlock *src, const int ox, const int oy)
 {
     assert(map != NULL);
-    assert(b != NULL);
+    assert(src != NULL);
 
     for (int y = 0; y < src->height; y++)
     for (int x = 0; x < src->width; x++)
@@ -794,7 +784,7 @@
 
 /* Clean given block from position markers and whitespaces
  */
-void mapBlockClean(MapBlock *map, char *symbols)
+void mapBlockClean(MapBlock *map, const char *symbols)
 {
     assert(map != NULL);
     assert(symbols != NULL);
@@ -812,9 +802,9 @@
 }
 
 
-void mapBlockPrintRaw(FILE *f, MapBlock *map)
+void mapBlockPrintRaw(FILE *fh, const MapBlock *map)
 {
-    assert(f != NULL);
+    assert(fh != NULL);
     assert(map != NULL);
 
     for (int y = 0; y < map->height; y++)
@@ -822,29 +812,29 @@
         unsigned char *dp = map->data + (y * map->scansize);
         for (int x = 0; x < map->width; x++)
         {
-            fputc(*dp, f);
+            fputc(*dp, fh);
             dp++;
         }
 
-        fputc(0xff, f);
+        fputc(0xff, fh);
     }
 }
 
 
-int mapBlockGetEntropy(MapBlock *map, const unsigned char *exclude, const int nexclude)
+int mapBlockGetEntropy(const MapBlock *map, const unsigned char *exclude, const int nexclude)
 {
     unsigned char *list;
-    int x, y, i, num;
+    int num, i;
 
     // Allocate memory for entropy array
     if ((list = th_malloc0(256)) == NULL)
         return -1;
 
     // Collect sums into entropy array
-    for (y = 0; y < map->height; y++)
+    for (int y = 0; y < map->height; y++)
     {
         unsigned char *c = map->data + (y * map->scansize);
-        for (x = 0; x < map->width; x++)
+        for (int x = 0; x < map->width; x++)
         {
             list[*c]++;
             c++;
--- a/libmaputils.h	Fri Oct 27 05:10:08 2017 +0300
+++ b/libmaputils.h	Fri Oct 27 05:17:35 2017 +0300
@@ -20,7 +20,8 @@
 
 /* Typedefs
  */
-typedef struct {
+typedef struct
+{
     char symbol;
     char *desc;
     int  r, g, b;
@@ -29,7 +30,8 @@
 } MapPiece;
 
 
-typedef struct {
+typedef struct
+{
     unsigned char *data;
     int width, height, scansize, size;
     int x, y;
@@ -37,7 +39,8 @@
 } MapBlock;
 
 
-typedef struct {
+typedef struct
+{
     int r, g, b;
     int ansi;
     int ansiAttr;
@@ -45,7 +48,8 @@
 } MapColor;
 
 
-enum {
+enum
+{
     ANSI_OFF           = 0,
     ANSI_BOLD          = 1,
     ANSI_UNDERSCORE    = 4,
@@ -55,7 +59,8 @@
 };
 
 
-enum {
+enum
+{
     col_black = 0,
     col_blue,
     col_red,
@@ -110,16 +115,16 @@
 /* Mapblock handling
  */
 MapBlock *  mapBlockAlloc(int width, int height);
-MapBlock *  mapBlockCopy(MapBlock *block);
+MapBlock *  mapBlockCopy(const 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);
+void        mapBlockPrint(FILE *fh, const 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);
+void        mapBlockClean(MapBlock *block, const char *symbols);
+void        mapBlockPrintRaw(FILE *fg, const MapBlock *block);
+int         mapBlockGetEntropy(const MapBlock *map, const unsigned char *exclude, const int nexclude);
 
 
 #endif