[Tux4kids-commits] [SCM] tuxhistory - Educational history game branch, master, updated. b8305e3522d74866994fe4a20f45b32ac334ee37

julio (none) julio at julio-desktop.
Sun Jul 11 05:56:06 UTC 2010


The following commit has been merged in the master branch:
commit b8305e3522d74866994fe4a20f45b32ac334ee37
Author: julio <julio at julio-desktop.(none)>
Date:   Sun Jul 11 00:54:52 2010 -0500

    Using hash tables in maps

diff --git a/data/maps/map.xml b/data/maps/map.xml
index 6f129d7..49e8c44 100644
--- a/data/maps/map.xml
+++ b/data/maps/map.xml
@@ -10,14 +10,14 @@
         <tilde>
             <height>0</height>
             <terrain>OCEAN</terrain>
-            <object></object>
+            <object>FOREST_MIXED</object>
             <unit></unit>
             <building></building>
         </tilde>
         <tilde>
             <height>1</height>
             <terrain>GRASSLAND</terrain>
-            <object></object>
+            <object>FOREST_RAIN</object>
             <unit></unit>
             <building></building>
         </tilde>
@@ -1832,14 +1832,14 @@
         <tilde>
             <height>1</height>
             <terrain>GRASSLAND</terrain>
-            <object></object>
+            <object>BLABLA</object>
             <unit></unit>
             <building></building>
         </tilde>
         <tilde>
             <height>1</height>
             <terrain>SWAMP</terrain>
-            <object></object>
+            <object>FOREST_MIXED</object>
             <unit></unit>
             <building></building>
         </tilde>
diff --git a/src/fileops.h b/src/fileops.h
index 10b060c..1aff488 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -367,7 +367,6 @@ enum{
     FOREST_WETLAND,
     FOREST_RAIN,
     FOREST_BROADLEAF,
-    FOREST_OBJECTS, // Must be at end of forest enums
     NUM_OBJECTS // Must be at the end.
 };
 
diff --git a/src/hashtable.c b/src/hashtable.c
index 0d28004..73cb52b 100644
--- a/src/hashtable.c
+++ b/src/hashtable.c
@@ -157,7 +157,7 @@ void *hashtable_lookup(const struct hashtable *table,
         entry = entry->next;
     }
     
-    return entry ? entry->value : NULL;
+    return entry ? entry->value : -1;
 }
     
 /** Create a hash table.
diff --git a/src/llist.c b/src/llist.c
index 72046cf..22ac20d 100644
--- a/src/llist.c
+++ b/src/llist.c
@@ -1,7 +1,18 @@
+/*
+ * llist.h
+ *
+ * Description: Linked list implementations 
+ * 
+ * Author: Jesús Manuel Mager Hois (fongog at gmail.com) (C) 2010
+ * Copyright: GPL v3 or later
+ *
+ * Part of "Tux4Kids Project
+ * http://www.tux4kids.com
+ * 
+ */
 #include<stdio.h>
 #include<stdlib.h>
 
-#include "tuxhistory.h"
 #include "llist.h"
 
 list_node *list_add(list_node **ptr, th_obj obj)
diff --git a/src/llist.h b/src/llist.h
index 8e6f3da..38d387e 100644
--- a/src/llist.h
+++ b/src/llist.h
@@ -1,4 +1,17 @@
+/*
+ * llist.h
+ *
+ * Description: Linked list h file
+ * 
+ * Author: Jesús Manuel Mager Hois (fongog at gmail.com) (C) 2010
+ * Copyright: GPL v3 or later
+ *
+ * Part of "Tux4Kids Project
+ * http://www.tux4kids.com
+ * 
+ */
 #ifndef DSTRUCTS_H 
+#define DSTRUCTS_H
 
 #include "tuxhistory.h"
 
@@ -12,5 +25,4 @@ void list_remove(list_node **);
 list_node **list_search(list_node **, int);
 void list_clean(list_node **);
  
-#define DSTRUCT_H
 #endif
diff --git a/src/map.c b/src/map.c
index 1d50f83..fc17aa4 100644
--- a/src/map.c
+++ b/src/map.c
@@ -23,17 +23,50 @@
 #include "globals.h"
 #include "fileops.h"
 #include "map.h"
-#include "llist.c"
+#include "hashtable.h"
+#include "llist.h"
 
 SDL_Surface* map_image;
 
-
+static int init_map_hash(void);
+static void end_map_hash(void);
 static int get_terrain_enum(char *);
 static int *get_context_tildes(int, int);
 static int *get_draw_tilde(int *, int);
 static int get_tile_num(int, int);
 static void str_upper(char *);
 
+static int init_map_hash(void)
+{
+    map_table_hash = make_hashtable(hashtable_default_hash, 30);
+
+    if(map_table_hash == NULL)
+        return 1;
+
+    hashtable_add(map_table_hash, "FOREST_MIXED", FOREST_MIXED);
+    hashtable_add(map_table_hash, "FOREST_TROPICAL", FOREST_TROPICAL);
+    hashtable_add(map_table_hash, "FOREST_CONIFER", FOREST_CONIFER);
+    hashtable_add(map_table_hash, "FOREST_SCRUB", FOREST_SCRUB);
+    hashtable_add(map_table_hash, "FOREST_BOREAL", FOREST_BOREAL);
+    hashtable_add(map_table_hash, "FOREST_WETLAND", FOREST_WETLAND);
+    hashtable_add(map_table_hash, "FOREST_RAIN", FOREST_RAIN);
+    hashtable_add(map_table_hash, "FOREST_BROADLEAF", FOREST_BROADLEAF);
+
+    hashtable_add(map_table_hash, "HIGHSEA", HIGHSEA);
+    hashtable_add(map_table_hash, "TUNDRA", TUNDRA);
+    hashtable_add(map_table_hash, "SWAMP", SWAMP);
+    hashtable_add(map_table_hash, "UNEXPLORED", UNEXPLORED);
+    hashtable_add(map_table_hash, "DESERT", DESERT);
+    hashtable_add(map_table_hash, "GRASSLAND", GRASSLAND);
+    hashtable_add(map_table_hash, "ARCTIC", ARCTIC);
+    hashtable_add(map_table_hash, "OCEAN", OCEAN);
+    hashtable_add(map_table_hash, "MARSH", MARSH);
+    hashtable_add(map_table_hash, "SAVANNAH", SAVANNAH);
+    hashtable_add(map_table_hash, "PLAINS", PLAINS);
+    hashtable_add(map_table_hash, "PRAIRIE", PRAIRIE);
+
+    return 0;
+}
 
 int map_xml(FILE *fp)
 {
@@ -45,6 +78,8 @@ int map_xml(FILE *fp)
     mxml_node_t *jnode;
    
     tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
+    if(init_map_hash())
+        return 1;
 
     x = 0;
     y = 0;
@@ -69,24 +104,21 @@ int map_xml(FILE *fp)
             node = mxmlFindElement(jnode, jnode, "terrain",
                     NULL, NULL, MXML_DESCEND);
     
-            value = get_terrain_enum(node->child->value.text.string);
+            //value = get_terrain_enum(node->child->value.text.string);
+
+            value = hashtable_lookup(map_table_hash, node->child->value.text.string);
             if(value != -1)
             {
                 map[x][y].terrain = value;
+                printf("%s",node->child->value.text.string);
             }
 
-            printf("%s",node->child->value.text.string);
             
-            // Get objects
-            node = mxmlFindElement(jnode, jnode, "object",
-                    NULL, NULL, MXML_DESCEND);
-
-            value = get_obj_enum(node->child->value.text.string);
              
             node = mxmlFindElement(jnode, jnode, "height",
                     NULL, NULL, MXML_DESCEND);
             
-            printf("%d",node->child->value.integer);
+            //printf("%d",node->child->value.integer);
 
             if(node->child->value.integer >= 0)
             {
@@ -95,6 +127,19 @@ int map_xml(FILE *fp)
 
             printf("%d ", map[x][y].terrain);
 
+            // Get objects
+            node = mxmlFindElement(jnode, jnode, "object",
+                    NULL, NULL, MXML_DESCEND);
+
+            if(node->child != NULL)
+            {
+                if(node->child->value.text.string)
+                    printf("(%s", node->child->value.text.string);
+                value=hashtable_lookup(map_table_hash, node->child->value.text.string);
+                if(value!=-1)
+                    printf(" Hash object: %d) ", value);
+            }
+
             y++;
         }
         if(y_tildes == -1)
@@ -124,6 +169,8 @@ int map_xml(FILE *fp)
         return 1;
     }
     
+    free_hashtable(map_table_hash);
+
     mxmlDelete(jnode);
     mxmlDelete(inode);
     mxmlDelete(node);
@@ -133,60 +180,6 @@ int map_xml(FILE *fp)
     return 0;
 }
 
-// Returns the enum value for each terrain type. If the terrain
-// type don't exists it returns -1
-
-
-int get_obj_enum(char *terrain_string)
-{
-    if(strcmp(terrain_string, "FOREST_BOREAL") == 0)
-        return FOREST_BOREAL;
-    else if(strcmp(terrain_string, "FOREST_CONIFER") == 0)
-        return FOREST_CONFIER;
-    else if(strcmp(terrain_string, "FOREST_MIXED") == 0)
-        return FOREST_MIXED;
-    else if(strcmp(terrain_string, "FOREST_SCRUB") == 0)
-        return FOREST_SCRUB;
-    else if(strcmp(terrain_string, "BROADLEAF") == 0)
-        return FOREST_BROADLEAF;
-    else if(strcmp(terrain_string, "FOREST_RAIN") == 0)
-        return FOREST_RAIN;
-    else if(strcmp(terrain_string, "FOREST_TROPICAL") == 0)
-        return FOREST_TROPICAL;
-    else
-        return -1;
-}
-
-int get_terrain_enum(char *terrain_string)
-{
-    if(strcmp(terrain_string, "HIGHSEA") == 0)
-        return HIGHSEA;
-    else if(strcmp(terrain_string, "TUNDRA") == 0)
-        return TUNDRA;
-    else if(strcmp(terrain_string, "SWAMP") == 0)
-        return SWAMP;
-    else if(strcmp(terrain_string, "UNEXPLORED") == 0)
-        return UNEXPLORED;
-    else if(strcmp(terrain_string, "DESERT") == 0)
-        return DESERT;
-    else if(strcmp(terrain_string, "GRASSLAND") == 0)
-        return GRASSLAND;
-    else if(strcmp(terrain_string, "ARCTIC") == 0)
-        return ARCTIC;
-    else if(strcmp(terrain_string, "OCEAN") == 0)
-        return OCEAN;
-    else if(strcmp(terrain_string, "MARSH") == 0)
-        return MARSH;
-    else if(strcmp(terrain_string, "SAVANNAH") == 0)
-        return SAVANNAH;
-    else if(strcmp(terrain_string, "PLAINS") == 0)
-        return PLAINS;
-    else if(strcmp(terrain_string, "PRAIRIE") == 0)
-        return PRAIRIE;
-    else
-        return -1;
-}
-
 /*  ---------------------------
  * |        |         |        |
  * |     1  |    2    |  3     |
@@ -313,11 +306,11 @@ static int *get_context_tildes(int x, int y)
             *(a + 8) = map[x+1][y+1].terrain;
         }
     }
-    for (i=0; i<9; i++)
+    /*for (i=0; i<9; i++)
     {
         printf("%d ", *(a + i));
-    }
-    printf("\n");
+    }*/
+    ////printf("\n");
 
     return a; 
 }        
@@ -395,20 +388,20 @@ static int *get_draw_tilde(int *array, int oe)
 
     for(i = 1; i < 9; i++)
     {
-        printf("I: %d ", i);
+        //printf("I: %d ", i);
         if(*(array+i) != -1)
         {
             if(*array != *(array+i))
             {
                 j=get_tile_num(i,oe);
-                printf("J: %d ", i, j);
+                //printf("J: %d ", i, j);
                 if(j < 0)
                 {
                     printf("Error parsing tiles\n");
                     return NULL;
                 }
-                printf("NUM: %d, %d, %d\n", *(array + i), (NUM_COMPTILDE - 1), 
-                    *(array + i) * (NUM_COMPTILDE) + j);
+                //printf("NUM: %d, %d, %d\n", *(array + i), (NUM_COMPTILDE - 1), 
+                //    *(array + i) * (NUM_COMPTILDE) + j);
                 *(a + i) = *(array + i) * (NUM_COMPTILDE) + j;
             }
             else
@@ -472,7 +465,7 @@ int generate_map(void)
     dest.x = (map_image->w/2)-(terrain[TUNDRA_CENTER_1]->w/2);
     dest.y = map_image->h-terrain[TUNDRA_CENTER_1]->h;
 
-    printf("[%d,%d]\n", x_tildes, y_tildes);
+    //printf("[%d,%d]\n", x_tildes, y_tildes);
 
     x = dest.x;
     y = dest.y;
@@ -499,7 +492,7 @@ int generate_map(void)
                 return 1;
             }
             
-            printf("ENUM: %d GRASSLAND: %d\n", *img_enums, GRASSLAND_CENTER_0);
+            //printf("ENUM: %d GRASSLAND: %d\n", *img_enums, GRASSLAND_CENTER_0);
             //Draw in the map buffer the resulting values
             for(l = 0; l < 1; l++)
             {
diff --git a/src/map.h b/src/map.h
index bcb2b8f..6878d0e 100644
--- a/src/map.h
+++ b/src/map.h
@@ -21,6 +21,8 @@
 #ifndef MAP_H
 #define MAP_H
 
+//#include "hashtable.h"
+
 // List of objects that can be used
 // in create_object. 
 // TODO: (maybe this should be in game.c
@@ -58,6 +60,7 @@ typedef struct {
     //th_obj *obj;// Pointer to object
 }th_map;
 
+struct hashtable *map_table_hash; //Values of Terrains and objects
 int flag_map; // Map flag: is a map surface allocated? 
 int x_tildes;
 int y_tildes;

-- 
tuxhistory - Educational history game



More information about the Tux4kids-commits mailing list