[Tux4kids-commits] [SCM] tuxhistory - Educational history game branch, master, updated. 3ac0680b9d19e4647ad76d31b2b0274e1ab1d40d
julio (none)
julio at julio-desktop.
Fri Jul 16 01:24:25 UTC 2010
The following commit has been merged in the master branch:
commit 3ac0680b9d19e4647ad76d31b2b0274e1ab1d40d
Author: julio <julio at julio-desktop.(none)>
Date: Thu Jul 15 20:23:08 2010 -0500
Adding objects.c/h, and introducing the TuxHistory API: tuxrts.c/h
diff --git a/configure.ac b/configure.ac
index 336f0f4..3f50a08 100644
--- a/configure.ac
+++ b/configure.ac
@@ -460,6 +460,7 @@ data/images/terrain/tundra/Makefile
data/images/terrain/unexplored/Makefile
data/images/forest/Makefile
data/maps/Makefile
+data/objects/Makefile
data/menus/Makefile
data/sounds/Makefile
doc/Makefile
diff --git a/data/Makefile.am b/data/Makefile.am
index ede0074..5c11c2a 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -5,6 +5,7 @@ SUBDIRS = fonts \
images \
menus \
sounds \
- maps
+ maps \
+ objects
#EXTRA_DIST = options
diff --git a/src/Makefile.am b/src/Makefile.am
index 38d629a..fb29a8e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -29,6 +29,7 @@ tuxhistory_SOURCES = tuxhistory.c \
setup.c \
titlescreen.c \
map.c \
+ objects.c \
menu.c \
game.c \
fileops_media.c \
@@ -43,6 +44,7 @@ tuxhistory_SOURCES = tuxhistory.c \
audio.c \
fileops.c \
convert_utf.c \
+ players.c \
SDL_extras.c \
SDL_rotozoom.c \
scandir.c \
@@ -67,10 +69,12 @@ EXTRA_DIST = credits.h \
loaders.h \
titlescreen.h \
map.h \
+ objects.h \
options.h \
setup.h \
tuxhistory.h \
convert_utf.h \
+ players.h \
SDL_extras.h \
SDL_rotozoom.h \
gettext.h \
diff --git a/src/graphs.c b/src/graphs.c
index 817d706..ddf8bb6 100644
--- a/src/graphs.c
+++ b/src/graphs.c
@@ -53,19 +53,6 @@ static int gmaps_alloc(int xsize, int ysize, int maps)
return 0;
}
-static void gmaps_free(int xsize, int ysize, int maps)
-{
- int i, j;
- for(i = 0; i < maps; i++)
- {
- for(j = 0; j < xsize; j++)
- {
- FREE(gmaps[i][j]);
- }
- FREE(gmaps[i]);
- }
-}
-
int create_gmaps(int players)
{
int i,j,k,l;
@@ -119,6 +106,11 @@ int create_gmaps(int players)
}
}
+int update_gmaps(void)
+{
+ return 0;
+}
+
static void gmaps_free(int xsize, int ysize, int maps)
{
int i, j;
diff --git a/src/graphs.h b/src/graphs.h
index addf227..0406713 100644
--- a/src/graphs.h
+++ b/src/graphs.h
@@ -14,6 +14,7 @@
#ifndef GRAPHS_H
#define GRAPHS_H
+#include "globals.h"
#include "map.h"
enum
@@ -31,10 +32,10 @@ enum
typedef struct gnode{
int id;
- int anchor_x, anchor_y; //Anchors in main map surface.
+ th_point anchor; //Anchors in main map surface.
int visible;
int explored;
- int usable;
+ int usable;
struct gnode *nodes[8];
th_obj *object;
int terrain;
diff --git a/src/llist.c b/src/llist.c
index a8539f1..0e49552 100644
--- a/src/llist.c
+++ b/src/llist.c
@@ -32,6 +32,9 @@ list_node *list_add(list_node **ptr, th_obj obj)
node->next = *ptr;
*ptr = node;
node->obj = obj;
+
+ object_counter++;
+
return node;
}
diff --git a/src/llist.h b/src/llist.h
index eadef45..04609f8 100644
--- a/src/llist.h
+++ b/src/llist.h
@@ -13,7 +13,7 @@
#ifndef DSTRUCTS_H
#define DSTRUCTS_H
-#include "map.h"
+#include "objects.h"
#include "tuxhistory.h"
typedef struct list_node{
diff --git a/src/loaders.c b/src/loaders.c
index de980ef..3bf7fa4 100644
--- a/src/loaders.c
+++ b/src/loaders.c
@@ -654,7 +654,7 @@ Mix_Music* LoadMusic(char *datafile )
return tempMusic;
}
-/* load_map: Load map from a XML datafile */
+/* LoadMap: Load map from a XML datafile */
FILE *LoadMap(const char* name)
{
FILE *fp = NULL;
@@ -668,7 +668,7 @@ FILE *LoadMap(const char* name)
if(strcmp(fn + fn_len - 4, ".xml"))
{
- DEBUGMSG(debug_loaders, "load_map(): %s is not an TuxHistory XML Map file\n", fn);
+ DEBUGMSG(debug_loaders, "LoadMap(): %s is not an TuxHistory XML Map file\n", fn);
return NULL;
}
@@ -676,10 +676,38 @@ FILE *LoadMap(const char* name)
if(fp == NULL)
{
- DEBUGMSG(debug_loaders, "load_image(): file_name is NULL, exiting.\n");
+ DEBUGMSG(debug_loaders, "LoadMap(): file_name is NULL, exiting.\n");
printf("Error at trying to find map file!\n");
return NULL;
}
return fp;
}
+/* LoadObj: Load object decription from a XML datafile */
+FILE *LoadObj(const char* name)
+{
+ FILE *fp = NULL;
+ char fn[PATH_MAX];
+ int fn_len;
+ int i;
+
+ /* check if objects file is present */
+ sprintf(fn, "%s/objects/%s.xml", DATA_PREFIX, name);
+ fn_len = strlen(fn);
+
+ if(strcmp(fn + fn_len - 4, ".xml"))
+ {
+ DEBUGMSG(debug_loaders, "LoadObj(): %s is not an TuxHistory XML objects description file\n", fn);
+ return NULL;
+ }
+
+ fp = fopen(fn, "r");
+
+ if(fp == NULL)
+ {
+ DEBUGMSG(debug_loaders, "LoadObj(): file_name is NULL, exiting.\n");
+ printf("Error at trying to find objects file!\n");
+ return NULL;
+ }
+ return fp;
+}
diff --git a/src/loaders.h b/src/loaders.h
index c7f8b4e..746d25c 100644
--- a/src/loaders.h
+++ b/src/loaders.h
@@ -54,5 +54,6 @@ Mix_Chunk* LoadSound(char* datafile);
Mix_Music* LoadMusic(char *datafile);
FILE* LoadMap(const char* file_name);
+FILE* LoadObj(const char* file_name);
#endif /* LOADERS_H */
diff --git a/src/map.c b/src/map.c
index 26a5d97..7b411a3 100644
--- a/src/map.c
+++ b/src/map.c
@@ -47,12 +47,6 @@ static int init_map_hash(void)
if(map_table_hash == NULL)
return 1;
- object_types[FOREST].type = FOREST;
- object_types[FOREST].live = 30;
- object_types[FOREST].defence = 0;
- object_types[FOREST].attack = 0;
- object_types[FOREST].move = 0;
-
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);
@@ -85,6 +79,8 @@ int map_xml(FILE *fp)
mxml_node_t *node;
mxml_node_t *inode;
mxml_node_t *jnode;
+
+ list_nodes = NULL;
tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
if(init_map_hash())
@@ -115,8 +111,8 @@ int map_xml(FILE *fp)
//value = get_terrain_enum(node->child->value.text.string);
- value = hashtable_lookup(map_table_hash, node->child->value.text.string);
- if(value != -1)
+ value = (int)hashtable_lookup(map_table_hash, node->child->value.text.string);
+ if(value != (int)-1)
{
map[x][y].terrain = value;
printf("%s",node->child->value.text.string);
@@ -146,7 +142,9 @@ int map_xml(FILE *fp)
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++;
@@ -522,6 +520,7 @@ static int get_tile_num(int i, int k)
int generate_map(void)
{
SDL_Rect dest;
+ th_point anchor;
int i, j, k, l;
int oe;
int x, y;
@@ -586,7 +585,29 @@ int generate_map(void)
}
}
- printf(".");
+
+ //Write values to gmaps
+ anchor.x = dest.x + terrain[*img_enums]->w/2;
+ anchor.y = dest.y + terrain[*img_enums]->h/2;
+
+ gmap[0][i][j].anchor = anchor;
+
+ // TODO: This is better in graph.h
+ if(map[i][j].terrain == HIGHSEA ||
+ map[i][j].terrain == OCEAN)
+ {
+ gmap[0][i][j].usable = 0;
+ }
+ else
+ {
+ gmap[0][i][j].usable = 1;
+ }
+
+ gmap[0][i][j].terrain = map[i][j].terrain;
+
+
+
+
//Prepare te new coords for the next tile
dest.x = dest.x - (terrain[*img_enums]->w/2);
dest.y = dest.y + (terrain[*img_enums]->h/2);
diff --git a/src/map.h b/src/map.h
index beed808..74569e8 100644
--- a/src/map.h
+++ b/src/map.h
@@ -24,34 +24,8 @@
#include "hashtable.h"
#include "globals.h"
-// List of objects that can be used
-// in create_object.
-// TODO: (maybe this should be in game.c
-// with int create_object(int type, int x, int y)
-// prototype...)
-//
-
-enum{
- FOREST,
- GOLD,
- STONE,
- BUILDING,
- UNI,
- NUM_OF_TYPES
-};
/*Global tuxhistory vars*/
-typedef struct th_obj{
- int id;
- int x, y; // (x,y) in the th_map array
- int type; // using the enum NUM_OF_TYPES of map.h
- int live; // 100 to 0
- char name[30];
- int defence;
- int attack;
- int move;
- int player;
-}th_obj;
// th_map is the main data strucutre
// th_map_tilde specifies the terrain
diff --git a/src/objects.c b/src/objects.c
new file mode 100644
index 0000000..3ab9e0e
--- /dev/null
+++ b/src/objects.c
@@ -0,0 +1,149 @@
+#include<stdio.h>
+#include<string.h>
+#include<ctype.h>
+#include<mxml.h>
+
+#include "objects.h"
+
+static int init_obj_hash(void);
+
+static int init_obj_hash(void)
+{
+
+ obj_table_hash = make_hashtable(hashtable_default_hash, NUM_OBJECTS+NUM_OF_TYPES);
+
+ if(obj_table_hash == NULL)
+ return 1;
+
+ hashtable_add(obj_table_hash, "FOREST_MIXED", FOREST_MIXED);
+ hashtable_add(obj_table_hash, "FOREST_TROPICAL", FOREST_TROPICAL);
+ hashtable_add(obj_table_hash, "FOREST_CONIFER", FOREST_CONIFER);
+ hashtable_add(obj_table_hash, "FOREST_SCRUB", FOREST_SCRUB);
+ hashtable_add(obj_table_hash, "FOREST_BOREAL", FOREST_BOREAL);
+ hashtable_add(obj_table_hash, "FOREST_WETLAND", FOREST_WETLAND);
+ hashtable_add(obj_table_hash, "FOREST_RAIN", FOREST_RAIN);
+
+ hashtable_add(obj_table_hash, "FOREST", FOREST);
+ hashtable_add(obj_table_hash, "GOLD", GOLD);
+ hashtable_add(obj_table_hash, "STONE", STONE);
+ hashtable_add(obj_table_hash, "BUILDING", BUILDING);
+ hashtable_add(obj_table_hash, "UNIT", UNIT);
+
+ return 0;
+}
+
+int objects_xml(FILE *fp)
+{
+ int value;
+ mxml_node_t *tree;
+ mxml_node_t *node;
+ mxml_node_t *inode;
+
+ th_obj object;
+
+ tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
+ if(init_obj_hash())
+ return 1;
+
+ for(inode = mxmlFindElement(tree, tree, "object",
+ NULL, NULL, MXML_DESCEND);
+ inode != NULL;
+ inode = mxmlFindElement(inode, tree, "object",
+ NULL, NULL, MXML_DESCEND))
+ {
+ node = mxmlFindElement(jnode, jnode, "type",
+ NULL, NULL, MXML_DESCEND);
+ if(node != NULL)
+ {
+ if(value != -1)
+ {
+ value = (int)hashtable_lookup(map_table_hash, node->child->value.text.string);
+ object.type = value;
+ }
+ }
+ else
+ {
+ object.type = -1;
+ printf("objects_xml: Error loading objects description file");
+ return 1;
+ }
+
+ node = mxmlFindElement(jnode, jnode, "name",
+ NULL, NULL, MXML_DESCEND);
+ if(node != NULL)
+ {
+ value = (int)hashtable_lookup(map_table_hash, node->child->value.text.string);
+ if(value != -1)
+ {
+ object.name_enum = value;
+ strcpy(object.name, node->child->value.text.string);
+ }
+ }
+ else
+ {
+ object.name_enum = -1;
+ printf("objects_xml: Error loading objects description file");
+ return 1;
+ }
+
+ node = mxmlFindElement(jnode, jnode, "rname",
+ NULL, NULL, MXML_DESCEND);
+ if(node != NULL)
+ {
+ strcpy(object.rname, node->child->value.text.string);
+ }
+ else
+ {
+ strcpy(object.rname, "");
+ printf("objects_xml: Error loading objects description file");
+ return 1;
+ }
+ node = mxmlFindElement(jnode, jnode, "description",
+ NULL, NULL, MXML_DESCEND);
+ if(node != NULL)
+ {
+ strcpy(object.description, node->child->value.text.string);
+ }
+ else
+ {
+ strcpy(object.rname, "");
+ printf("objects_xml: Error loading objects description file");
+ return 1;
+ }
+
+ node = mxmlFindElement(jnode, jnode, "live",
+ NULL, NULL, MXML_DESCEND);
+
+ if(node->child->value.integer >= 0)
+ {
+ object.live = node->child->value.integer;
+ }
+ else
+ {
+ object.live = -1;
+ printf("objects_xml: Error loading objects description file");
+ 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.lives);
+ }
+
+
+ free_hashtable(map_table_hash);
+
+ mxmlDelete(jnode);
+ mxmlDelete(inode);
+ mxmlDelete(node);
+
+ fclose(fp);
+
+ return 0;
+}
+
diff --git a/src/objects.h b/src/objects.h
new file mode 100644
index 0000000..8538cee
--- /dev/null
+++ b/src/objects.h
@@ -0,0 +1,37 @@
+#ifndef OBJECTS_H
+#define OBJECTS_H
+
+#include "hashtable.h"
+
+enum{
+ FOREST,
+ GOLD,
+ STONE,
+ BUILDING,
+ UNIT,
+ NUM_OF_TYPES
+};
+
+
+typedef struct th_obj{
+ int id;
+ int x, y; // (x,y) in the th_map array
+ int type; // using the enum NUM_OF_TYPES of map.h
+ int live; // 100 to 0
+ char name[30];
+ int name_enum;
+ char rname[50];
+ char description[200];
+ int defence;
+ int attack;
+ int move;
+ int player;
+}th_obj;
+
+int object_counter;
+struct hashtable *obj_table_hash; //Strings to enums
+struct hashtable *objects_hash; //Names to objects
+
+int objects_xml(FILE *fp);
+
+#endif
diff --git a/src/players.c b/src/players.c
index 8a527e9..bfdd51d 100644
--- a/src/players.c
+++ b/src/players.c
@@ -57,7 +57,7 @@ int add_player(char *name, int civ, int max_pop, int stone,
printf("add_player(): gmap isn't allocated, cant giva a position in map to player!\n");
return 1;
}
- player[last_player].pos = &gmap[0][point.x][point.y];
+ player[last_player].pos = &gmap[0][pos.x][pos.y];
return 0;
}
diff --git a/src/tuxrts.c b/src/tuxrts.c
index 07359e2..4ed971d 100644
--- a/src/tuxrts.c
+++ b/src/tuxrts.c
@@ -2,8 +2,11 @@
#include "loaders.h"
#include "graphs.h"
-int tuxrts_mapinit(char *map_name, th_players *players)
+int tuxrts_init(char *map_name, th_players *players)
{
+
+ object_counter = 0;
+
fp = LoadMap("map");
if(fp == NULL)
{
@@ -19,6 +22,10 @@ int tuxrts_mapinit(char *map_name, th_players *players)
return 1;
}
+ if(create_gmaps(2))
+ {
+ return 1;
+ }
generate_map();
return 0;
}
--
tuxhistory - Educational history game
More information about the Tux4kids-commits
mailing list