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

julio (none) julio at julio-desktop.
Fri Jul 16 19:30:28 UTC 2010


The following commit has been merged in the master branch:
commit 4b774c36bf6d4445cca8fa62e159c8ec721d4612
Author: julio <julio at julio-desktop.(none)>
Date:   Fri Jul 16 14:29:45 2010 -0500

    Ready: Objects linked list

diff --git a/data/objects/.objects.xml.swp b/data/objects/.objects.xml.swp
deleted file mode 100644
index a304f8d..0000000
Binary files a/data/objects/.objects.xml.swp and /dev/null differ
diff --git a/src/Makefile.am b/src/Makefile.am
index fb29a8e..40d13b7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -36,7 +36,7 @@ tuxhistory_SOURCES = tuxhistory.c \
 	options.c	\
 	credits.c	\
 	hashtable.c \
-	highscore.c	\
+	highscore.c	*/\
 	llist.c		\
 	graphs.c	\
 	linewrap.c	\
diff --git a/src/game.c b/src/game.c
index e34cda8..71acec3 100644
--- a/src/game.c
+++ b/src/game.c
@@ -132,47 +132,47 @@ static int game_init(void)
     user_quit_received = 0;
     SDL_quit_received = 0;
     escape_received = 0;
-    
-    fp = LoadMap("map");
+
+    fp = LoadObj("objects");
     if(fp == NULL)
     {
+        printf("File not found!\n");
         DEBUGMSG(debug_game, "File not found!");
         return 1;
     }
-
-    
-    if(map_xml(fp))
+    printf("Object files in memory!\n");
+    if(objects_xml(fp))
     {
         printf("Error parsing file!");
-        DEBUGMSG(debug_game, "Error loading the map file.\n");
+        DEBUGMSG(debug_game, "Error loading the objects description file.\n");
         return 1;
     }
-
-    fp = LoadObj("objects");
+    printf("Object file parsed.\n");
+   
+    fp = LoadMap("map");
     if(fp == NULL)
     {
         DEBUGMSG(debug_game, "File not found!");
         return 1;
     }
-    if(objects_xml(fp))
+    printf("Map file in memory.\n");
+
+    if(map_xml(fp))
     {
         printf("Error parsing file!");
-        DEBUGMSG(debug_game, "Error loading the objects description file.\n");
+        DEBUGMSG(debug_game, "Error loading the map file.\n");
         return 1;
     }
-
+    printf("Map file parsed!\n");
     if(create_gmaps(2))
     {
         printf("Couldn't generate grpah mesh!\n");
     }
     generate_map();
 
-    
-
     return 0;
 }
 
-
 int game(void)
 {
     Uint32 last_time, now_time;
diff --git a/src/globals.h b/src/globals.h
index 5009929..fe56040 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -120,7 +120,8 @@ extern const int debug_all;
 #define MAX_X_TILDES 100
 #define MAX_Y_TILDES 100
 
-#define MAX_OBJECTS 100
+#define MAX_OBJECTS 100000
+#define MAX_DEF_OBJECTS 1000
 
 /* These values are hard-coded and used 'as is' by the program */
 /* (i.e. these behaviors require recompilation to change)   */ 
diff --git a/src/hashtable.c b/src/hashtable.c
index e6de361..8a97dc5 100644
--- a/src/hashtable.c
+++ b/src/hashtable.c
@@ -180,7 +180,7 @@ void *hashtable_lookup(const struct hashtable *table,
         entry = entry->next;
     }
     
-    return entry ? entry->value : -1;
+    return entry ? entry->value : NULL;
 }
     
 /** Create a hash table.
diff --git a/src/llist.c b/src/llist.c
index 0e49552..0b49aa3 100644
--- a/src/llist.c
+++ b/src/llist.c
@@ -66,6 +66,20 @@ list_node **list_search(list_node **node, int data)
     return NULL;
 }
 
+void list_print(list_node *node)
+{
+    if (node == NULL)
+    {
+        printf("list is empty\n");
+    }
+    while (node != NULL)
+    {
+        printf("print %p %p %s\n", node, node->next, node->obj.name);
+        node = node->next;
+    }
+}
+
+
 void list_clean(list_node **node)
 {
     while(*node != NULL)
diff --git a/src/llist.h b/src/llist.h
index 04609f8..cd85dfc 100644
--- a/src/llist.h
+++ b/src/llist.h
@@ -24,6 +24,7 @@ typedef struct list_node{
 list_node *list_add(list_node **, th_obj);
 void list_remove(list_node **);
 list_node **list_search(list_node **, int);
+void list_print(list_node *);
 void list_clean(list_node **);
  
 // This list contains all objects that we load
diff --git a/src/map.c b/src/map.c
index 2d8fd9e..e19c735 100644
--- a/src/map.c
+++ b/src/map.c
@@ -22,6 +22,7 @@
 
 #include "globals.h"
 #include "fileops.h"
+#include "objects.h"
 #include "map.h"
 #include "hashtable.h"
 #include "llist.h"
@@ -78,11 +79,18 @@ int map_xml(FILE *fp)
     mxml_node_t *inode;
     mxml_node_t *jnode;
 
+    th_obj *object_ptr;
+    th_obj tmp_obj;
+
+    object_ptr = NULL;
     list_nodes = NULL;
    
     tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
     if(init_map_hash())
+    {
+        printf("Couldn't initalize init_map_hash()\n");
         return 1;
+    }
 
     x = 0;
     y = 0;
@@ -106,21 +114,28 @@ int map_xml(FILE *fp)
             // Get terrain value
             node = mxmlFindElement(jnode, jnode, "terrain",
                     NULL, NULL, MXML_DESCEND);
-    
-            //value = get_terrain_enum(node->child->value.text.string);
-
+            
+            if(node == NULL)
+            {
+                printf("Error: No terrain field...");
+                return 0;
+            }
+            value = NULL;
             value = (int)hashtable_lookup(map_table_hash, node->child->value.text.string);
-            if(value != (int)-1)
+            if(value != NULL)
             {
                 map[x][y].terrain = value;
                 printf("%s",node->child->value.text.string);
             }
 
-            
-             
             node = mxmlFindElement(jnode, jnode, "height",
                     NULL, NULL, MXML_DESCEND);
             
+            if(node == NULL)
+            {
+                printf("Error: field not found...");
+                //return 0;
+            }
             //printf("%d",node->child->value.integer);
 
             if(node->child->value.integer >= 0)
@@ -133,11 +148,22 @@ int map_xml(FILE *fp)
             // Get objects
             node = mxmlFindElement(jnode, jnode, "object",
                     NULL, NULL, MXML_DESCEND);
-
             if(node->child != NULL)
             {
-                if(node->child->value.text.string)
+                object_ptr = hashtable_lookup(objects_hash, node->child->value.text.string);
+                if(object_ptr != NULL)
+                {
                     printf("(%s", node->child->value.text.string);
+                    
+                    printf(" *%s ", object_ptr->description);
+                    tmp_obj = *object_ptr;
+                    list_add(&list_nodes, tmp_obj);
+                }
+                else
+                {
+                    printf("Wrong object name\n");
+                }
+                
                 value=(int)hashtable_lookup(map_table_hash, node->child->value.text.string);
                 if(value!=-1)
                 {
@@ -174,6 +200,7 @@ int map_xml(FILE *fp)
         return 1;
     }
     
+    list_print(list_nodes);
     free_hashtable(map_table_hash);
 
     mxmlDelete(jnode);
diff --git a/src/objects.c b/src/objects.c
index a2eedf0..918c741 100644
--- a/src/objects.c
+++ b/src/objects.c
@@ -3,6 +3,7 @@
 #include<ctype.h>
 #include<mxml.h>
 
+#include "tuxhistory.h"
 #include "fileops.h"
 #include "objects.h"
 
@@ -35,23 +36,32 @@ static int init_obj_hash(void)
 
 int objects_xml(FILE *fp)
 {
+    int i;
     int value;
     mxml_node_t *tree;
     mxml_node_t *node;
     mxml_node_t *inode;
 
-    th_obj object;
+    objects_hash = make_hashtable(hashtable_default_hash, 1000);
+    if(objects_hash == NULL)
+        return 1;
 
     tree = mxmlLoadFile(NULL, fp, MXML_OPAQUE_CALLBACK);
     if(init_obj_hash())
         return 1;
 
+    i = 0;
     for(inode = mxmlFindElement(tree, tree, "object", 
                 NULL, NULL, MXML_DESCEND);
             inode != NULL;
             inode = mxmlFindElement(inode, tree, "object",
                 NULL, NULL, MXML_DESCEND))
     {
+        if(i > MAX_DEF_OBJECTS)
+        {
+            printf("To many objects, the limit is %d", MAX_DEF_OBJECTS);
+            return 1;
+        }
         node = mxmlFindElement(inode, inode, "type",
                                NULL, NULL, MXML_DESCEND);
         if(node != NULL)
@@ -59,13 +69,13 @@ int objects_xml(FILE *fp)
             if(value != -1)
             {
                 value = (int)hashtable_lookup(obj_table_hash, node->child->value.opaque);
-                object.type = value;
+                object[i].type = value;
             }
         }
         else
         {
-            object.type = -1;
-            printf("objects_xml: Error loading objects description file");
+            object[i].type = -1;
+            printf("objects_xml: Error loading objects description file\n");
             return 1;
         }
 
@@ -73,17 +83,22 @@ int objects_xml(FILE *fp)
                                NULL, NULL, MXML_DESCEND);
         if(node != NULL)
         {
+            if((int)hashtable_lookup(objects_hash, node->child->value.opaque) != NULL)
+            {
+                printf("This element was already added to the hash table!\n");
+                return 1;
+            }
             value = (int)hashtable_lookup(obj_table_hash, node->child->value.opaque);
             if(value != -1)
             {
-                object.name_enum = value;
-                strcpy(object.name, node->child->value.opaque);    
+                object[i].name_enum = value;
+                strcpy(object[i].name, node->child->value.opaque);    
             }
         }
         else
         {
-            object.name_enum = -1;
-            printf("objects_xml: Error loading objects description file");
+            object[i].name_enum = -1;
+            printf("objects_xml: Error loading objects description file.\n");
             return 1;
         }
 
@@ -91,25 +106,24 @@ int objects_xml(FILE *fp)
                                NULL, NULL, MXML_DESCEND);
         if(node != NULL)
         {
-            strcpy(object.rname, node->child->value.opaque);
+            strcpy(object[i].rname, node->child->value.opaque);
         }
         else
         {
-            strcpy(object.rname, "");
-            printf("objects_xml: Error loading objects description file");
+            strcpy(object[i].rname, "");
+            printf("objects_xml: Error loading objects description file.\n");
             return 1;
         }
         node = mxmlFindElement(inode, inode, "description",
                                NULL, NULL, MXML_DESCEND);
         if(node != NULL)
         {
-            strcpy(object.description, node->child->value.opaque);
-            printf(" string: %s\n", node->child->value.opaque);
+            strcpy(object[i].description, node->child->value.opaque);
         }
         else
         {
-            strcpy(object.rname, "");
-            printf("objects_xml: Error loading objects description file");
+            strcpy(object[i].rname, "");
+            printf("objects_xml: Error loading objects description file.\n");
             return 1;
         }
 
@@ -118,26 +132,42 @@ int objects_xml(FILE *fp)
             
         if(atoi(node->child->value.opaque) >= 0)
         {
-            object.live = atoi(node->child->value.opaque);
+            object[i].live = atoi(node->child->value.opaque);
         }
         else
         {
-            object.live = -1;
-            printf("objects_xml: Error loading objects description file");
+            object[i].live = -1;
+            printf("objects_xml: Error loading objects description file.\n");
             return 1;
         }
 
         /* Debug: print the values of current object */
         printf("%d %s:%d(%s) %s lives: %d\n", 
-                object.type,
-                object.name,
-                object.name_enum,
-                object.rname,
-                object.description,
-                object.live);
+                object[i].type,
+                object[i].name,
+                object[i].name_enum,
+                object[i].rname,
+                object[i].description,
+                object[i].live);
+
+        /* End of debug */
+        hashtable_add(objects_hash, object[i].name, &object[i]);
+
+        i++;
     }
 
-        
+    /* Use the objects_hash table this way *
+    th_obj *db_obj;
+    db_obj = hashtable_lookup(objects_hash, "FOREST_CONIFER");
+    printf("%d %s:%d(%s) %s lives: %d\n", 
+                db_obj->type,
+                db_obj->name,
+                db_obj->name_enum,
+                db_obj->rname,
+                db_obj->description,
+                db_obj->live);
+                */
+
     free_hashtable(obj_table_hash);
 
     mxmlDelete(inode);
diff --git a/src/objects.h b/src/objects.h
index 8538cee..9b5d7b8 100644
--- a/src/objects.h
+++ b/src/objects.h
@@ -31,6 +31,7 @@ typedef struct th_obj{
 int object_counter;
 struct hashtable *obj_table_hash; //Strings to enums
 struct hashtable *objects_hash; //Names to objects
+th_obj *object;
 
 int objects_xml(FILE *fp);
 
diff --git a/src/setup.c b/src/setup.c
index 542385a..b11054d 100644
--- a/src/setup.c
+++ b/src/setup.c
@@ -600,7 +600,7 @@ void data_memory_alloc(void)
 {
     int i;
     object = NULL;
-    object = (th_obj*)malloc(MAX_OBJECTS * sizeof(th_obj));
+    object = (th_obj*)malloc(MAX_DEF_OBJECTS * sizeof(th_obj));
     if (object == NULL)
     {
         DEBUGMSG(debug_setup, "Allocation of game objects faild!\n");

-- 
tuxhistory - Educational history game



More information about the Tux4kids-commits mailing list