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

julio (none) julio at julio-desktop.
Sun Aug 8 07:30:28 UTC 2010


The following commit has been merged in the master branch:
commit 66cfec496b6bd13b7efe5fed6233f0574bde6a96
Author: julio <julio at julio-desktop.(none)>
Date:   Sun Aug 8 02:29:09 2010 -0500

    State Machine advances, and player owning on objects ready. Also visible and explored tildes updated in gmaps.

diff --git a/src/ai.c b/src/ai.c
index a1026ef..3eb5989 100644
--- a/src/ai.c
+++ b/src/ai.c
@@ -9,7 +9,14 @@
  * http://www.tux4kids.com
  * 
  */
+#include<stdio.h>
 
+// Lua headers
+#include "lua.h"
+#include "lualib.h"
+#include "lauxlib.h"
+
+//Tuxhistory headers
 #include "tuxhistory.h"
 #include "globals.h"
 #include "graphs.h"
@@ -17,32 +24,28 @@
 #include "tuxrts.h"
 #include "bheap.h"
 #include "hashtable.h"
+#include "objects.h"
 
 // Hueristic distance between to points
 #define HDIST(x1, y1, x2, y2) (((x1<x2)?(x2-x1):(x1-x2) + ((y1<y2)?(y2-y1):(y1-y2)))*10)
 
-/* itoa: thanks to Lukás Chmel */
-static char* itoa(int value, char* result, int base)
+int ai_init(int players)
 {
-    if (base < 2 || base > 36) { *result = '\0'; return result; }
-    char* ptr = result, *ptr1 = result, tmp_char;
-    int tmp_value;
-    do {
-        tmp_value = value;
-        value /= base;
-        *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
-    } while ( value );
-    if (tmp_value < 0) *ptr++ = '-';
-    *ptr-- = '\0';
-    while(ptr1 < ptr) {
-        tmp_char = *ptr;
-        *ptr--= *ptr1;
-        *ptr1++ = tmp_char;
+    L = lua_open();
+    if(!L)
+    {
+        printf("Error starting LUA!\n");
+        return 0;
     }
-    return result;
+    return 1;
+}
+
+void ai_free(void)
+{
+    lua_close(L);
 }
 
-th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
+th_path *ai_shortes_path(int player, int unit, th_point source, th_point goal)
 {
     int i, a;
     int count;
@@ -50,6 +53,7 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
     th_vector vector;
     th_point pt;
     th_point *solution;
+    th_path *path;
 
     bheap *open;
     struct hashtable *closed;
@@ -126,19 +130,28 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
             {
                 printf("Solution deph is %d\n", n->deph);
                 solution = (th_point *)malloc(n->deph * sizeof(th_point));
-                i = 0;
+                if(!solution)
+                    return NULL;
+                path = (th_path *)malloc(sizeof(th_path));
+                if(!path)
+                    return NULL;
+
+                i = n->deph - 1;
 
                 while(n->parent)
                 {
                     printf("(%d,%d)\n",n->point.x, n->point.y);
                     solution[i] = n->point;
                     n = n->parent;
-                    i++;
                 } 
+                
+                path->path = solution;
+                path->size = i;
+
                 free_hashtable(closed);
                 bheap_free(open);
                 FREE(e);
-                return solution;
+                return path;
             }
 
             //printf("This element is not the goal!.. Trying...\n");
@@ -228,3 +241,69 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
     }
 }
 
+void ai_free_path(th_path *path)
+{
+    FREE(path->path);
+    FREE(path);
+}
+
+int ai_modify_state(int player, th_obj *object, int state)
+{
+    if(!object)
+    {
+        printf("Incorrect object!\n");
+        return 0;
+    }
+    if(object->player == player || object->player == 0)
+    {
+        object->state.old_state = object->state.state;
+        object->state.state = state;
+        object->state.flag = 1;
+        return 1;
+    }
+    printf("Not a valid player to modify objects state!");
+    return 0;
+}
+
+int ai_state_update(list_node *node)
+{
+    if(!node)
+        return 0;
+    do{
+        if(node->obj.state.flag)
+        {
+            if(node->obj.state.state == GOTO)
+            {
+                node->obj.state.count = node->obj.state.path->size;
+                node->obj.state.flag = 0;
+                node->obj.state.agains_flag = 0;
+                node->obj.state.action_againts = 0;
+                node->obj.state.path_flag = 1;
+            }
+        }
+        if(node->obj.state.agains_flag)
+        {
+        }
+        if(node->obj.state.path_flag)
+        {
+            node->obj.state.count++;
+            if(node->obj.state.path_count < 0)
+            {
+                node->obj.x = node->obj.state.path->path[node->obj.state.path_count].x;
+                node->obj.x = node->obj.state.path->path[node->obj.state.path_count].y;
+                node->obj.state.path_count--;
+            }
+            else
+            {
+                if(node->obj.state.state == GOTO)
+                {
+                    ai_modify_state(node->obj.player, &(node->obj), INACTIVE);
+                }
+                ai_free_path(node->obj.state.path);
+            }
+        }
+        node = node->next;
+    }while(node != NULL);
+    return 1;
+}
+
diff --git a/src/ai.h b/src/ai.h
index 828b6fc..4b31ab0 100644
--- a/src/ai.h
+++ b/src/ai.h
@@ -1,4 +1,3 @@
-
 /* ai.h
  *
  * Description: AI mainy path finding functions for the game lives here
@@ -11,8 +10,27 @@
  * 
  */
 
+#ifndef AI_H
+#define AI_H
+
+#include "lua.h"
 #include "tuxhistory.h"
 #include "globals.h"
 #include "graphs.h"
+#include "llist.h"
+
+
+// Global state!
+lua_State *L;
+
+int ai_init(int players);
+
+th_path *ai_shortes_path(int, int, th_point source, th_point goal);
+
+void ai_free_path(th_path *path);
+
+int ai_modify_state(int player, th_obj *object, int state);
+
+int ai_state_update(list_node *node);
 
-th_point *ai_shortes_path(int, int, th_point source, th_point goal);
+#endif
diff --git a/src/game.c b/src/game.c
index d932a90..904cd21 100644
--- a/src/game.c
+++ b/src/game.c
@@ -227,7 +227,8 @@ int game(void)
     {
         last_time = SDL_GetTicks();
 
-        update_gmaps();
+        //update_gmaps();
+        rts_update_game();
         game_handle_user_events();
         game_handle_mouse();
 
diff --git a/src/globals.h b/src/globals.h
index fe56040..1c87cc4 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -176,7 +176,10 @@ typedef struct th_vector{
     int y;
 } th_vector;
 
-
+typedef struct th_path{
+    th_point *path;
+    int size;
+}th_path;
 
 #define NAME_BUF_SIZE 200
 /* data for 'Training Academy' lessons: */
diff --git a/src/graphs.h b/src/graphs.h
index 8705d5b..b581de6 100644
--- a/src/graphs.h
+++ b/src/graphs.h
@@ -41,7 +41,7 @@ typedef struct gnode{
     int visible;
     int explored;
     int usable; 
-    struct gnode *nodes[8];
+    struct gnode *nodes[NUM_DIRS-1];
     th_obj *object;
     int terrain;
 }gnode;
diff --git a/src/map.c b/src/map.c
index 09fd050..bffad15 100644
--- a/src/map.c
+++ b/src/map.c
@@ -14,6 +14,7 @@
 
 #include<ctype.h>
 #include<mxml.h>
+#include<assert.h>
 
 #include "SDL.h"
 #include "SDL_image.h"
@@ -85,6 +86,8 @@ int map_xml(FILE *fp)
     th_obj *object_ptr;
     th_obj tmp_obj;
 
+    char *tmp_text;
+
     object_ptr = NULL;
     list_nodes = NULL;
 
@@ -150,33 +153,50 @@ int map_xml(FILE *fp)
             // Get objects
             node = mxmlFindElement(jnode, jnode, "object",
                     NULL, NULL, MXML_DESCEND);
-            if(node->child != NULL)
+            if(node)
             {
-                object_ptr = hashtable_lookup(objects_hash, node->child->value.text.string);
-                if(object_ptr != NULL)
+                if(node->child != NULL)
                 {
-                    printf("(%s", 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;
-                    tmp_obj.id = object_counter;
-                    tmp_obj.x = x;
-                    tmp_obj.y = y;
-                    list_add(&list_nodes, tmp_obj);
-
-                    object_counter++;
-                }
-                else
-                {
-                    printf("Wrong object name\n");
-                }
+                        printf(" *%s ", object_ptr->description);
+                        tmp_obj = *object_ptr;
+
+                        if(tmp_text = mxmlElementGetAttr(node, "player"))
+                        {
+                            tmp_obj.player = atoi(tmp_text);
+                            printf(" PLAYER: %03d ", tmp_obj.player);
+                        }
+                        else
+                        {
+                            tmp_obj.player = 0;
+                            printf(" NO PLAYER ");
+                        }
+
+                        tmp_obj.id = object_counter;
+                        tmp_obj.x = x;
+                        tmp_obj.y = y;
+                        list_add(&list_nodes, tmp_obj);
+
+                        object_counter++;
+                    }
+                    else
+                    {
+                        printf("Wrong object name\n");
+                    }
                 
-                value=(int)hashtable_lookup(map_table_hash, node->child->value.text.string);
-                if(value!=-1)
-                {
-                    printf(" Hash object: %d) ", value);
+                    value=(int)hashtable_lookup(map_table_hash, node->child->value.text.string);
+                    if(value!=-1)
+                    {
+                        printf(" Hash object: %d) ", value);
+                    }
                 }
             }
+            //node = NULL;
 
             y++;
         }
diff --git a/src/objects.c b/src/objects.c
index 9868968..47756e1 100644
--- a/src/objects.c
+++ b/src/objects.c
@@ -147,6 +147,23 @@ int objects_xml(FILE *fp)
             object[i].type != GOLD   ||
             object[i].type != STONE)
         {
+            node = mxmlFindElement(inode, inode, "vision",
+                    NULL, NULL, MXML_DESCEND);
+            
+            if(node != NULL)
+            {
+                if(atoi(node->child->value.opaque) >= 0)
+                {
+                    object[i].vision_range = atoi(node->child->value.opaque);
+                }
+                else
+                {
+                    object[i].vision_range = -1;
+                    printf("objects_xml: Error loading objects description file.\n");
+                    return 1;
+                }
+            }
+
             node = mxmlFindElement(inode, inode, "defence",
                     NULL, NULL, MXML_DESCEND);
             
@@ -204,6 +221,23 @@ int objects_xml(FILE *fp)
             object[i].attack = -1;
             object[i].move = -1;
         }
+
+        //Initializing states
+
+        object[i].state.state = INACTIVE;
+        object[i].state.old_state = INACTIVE;
+        object[i].state.count = 0;
+        object[i].state.flag = 0;
+        object[i].state.action_againts = INACTIVE;
+        object[i].state.agains_flag = 0;
+        object[i].state.path = NULL;
+        object[i].state.path_count = 0; 
+        object[i].state.path_flag = 0;
+        object[i].state.carrying = 0;
+        object[i].state.resource_type = REC_NONE;
+        object[i].state.target_obj = NULL;
+
+
         /* Debug: print the values of current object */
         printf("%d %s:%d(%s) %s lives: %d, def: %d, att: %d, mov: %d\n", 
                 object[i].type,
diff --git a/src/objects.h b/src/objects.h
index a80a5dc..bf6a5fd 100644
--- a/src/objects.h
+++ b/src/objects.h
@@ -1,9 +1,31 @@
 #ifndef OBJECTS_H
 #define OBJECTS_H
 
+#include "globals.h"
 #include "hashtable.h"
 
 enum{
+    REC_NONE,
+    REC_GOLD,
+    REC_STONE,
+    REC_WOOD,
+    REC_FOOD,
+    NUM_RESOURCES
+};
+
+enum{
+    INACTIVE,
+    BUILD,
+    REPAIR,
+    ATTACK,
+    CREATE,
+    GOTO,
+    USE,
+    DIE,
+    NUM_OF_STATES
+};
+
+enum{
     FOREST,
     GOLD,
     STONE,
@@ -12,6 +34,21 @@ enum{
     NUM_OF_TYPES
 };
 
+typedef struct th_state{
+    int state;
+    int old_state;
+    int count; // Counter to anime
+    int flag; //Has a new state? 
+    int action_againts;
+    int agains_flag;
+    th_path *path;
+    int path_count;
+    int path_flag; //Need pathfinding?
+    int carrying;
+    int resource_type;
+    struct th_obj *target_obj;
+}th_state;
+
 
 typedef struct th_obj{
     int id;
@@ -23,17 +60,19 @@ typedef struct th_obj{
     int name_enum;
     char rname[50];
     char description[200];
+    int vision_range;
     int defence;
     int attack;
     int move;
     int player;
-    int path_flag; //Need pathfinding?
+    struct th_state state;
 }th_obj;
 
-int object_counter;
 struct hashtable *obj_table_hash; //Strings to enums
 struct hashtable *objects_hash; //Names to objects
+
 th_obj *object;
+int object_counter;
 
 int objects_xml(FILE *fp);
 
diff --git a/src/tuxrts.c b/src/tuxrts.c
index 73eb097..5f58481 100644
--- a/src/tuxrts.c
+++ b/src/tuxrts.c
@@ -9,6 +9,8 @@
 #include "players.h"
 #include "graphs.h"
 #include "llist.h"
+#include "ai.h"
+
 
 int tuxrts_init(char *object_name, char *map_name, int players)
 {
@@ -138,10 +140,11 @@ th_obj *rts_get_object(int player, th_point coords)
     if(obj_node != NULL)
     {
         do{
-            printf("Object: (%d,%d) = (%d,%d)\n", obj_node->obj.x, 
-                    obj_node->obj.y, coords.x, coords.y);
+            printf("Object: (%d,%d) = (%d,%d) and player %d = %d ?\n", obj_node->obj.x, 
+                    obj_node->obj.y, coords.x, coords.y, obj_node->obj.player, player);
             if( obj_node->obj.x == coords.x &&
-                obj_node->obj.y == coords.y   )
+                obj_node->obj.y == coords.y &&
+                obj_node->obj.player == player)
                 return &obj_node->obj;
             else
                 obj_node = obj_node->next;
@@ -152,3 +155,122 @@ th_obj *rts_get_object(int player, th_point coords)
     return 0;
 }
 
+static void rts_set_visible(int player, th_point point, int deph, int count)
+{
+    int l;
+    if(count == deph)
+        return;
+
+    gmaps[player][point.x][point.y].explored = 1;
+    gmaps[player][point.x][point.y].visible = 1;
+
+    for(l = 0; l < NUM_DIRS; l++)
+    {
+        if(gmaps[player][point.x][point.y].nodes[l])
+            rts_set_visible(player, gmaps[player][point.x][point.y].nodes[l]->point, deph, count+1);
+    }
+    return;
+}
+
+int rts_update_game(void)
+{
+    int i, j, player;
+    list_node *obj_node;
+    th_point point;
+
+    // Update gmaps...
+    for(player = 1; player <= num_of_players; player++)
+    {
+        for(i = 0; i <= x_tildes; i++)
+        {
+            for(j = 0; j <= y_tildes; j++)
+            {
+                gmaps[player][i][j].visible = 0;
+                gmaps[player][i][j].object = NULL;
+            }
+        }
+    }
+    obj_node = list_nodes;
+    if(obj_node != NULL)
+    {
+        do{
+            point.x = obj_node->obj.x;
+            point.y = obj_node->obj.y;
+            rts_set_visible(obj_node->obj.player,
+                            point,
+                            obj_node->obj.vision_range,
+                            0);
+            gmaps[obj_node->obj.player][point.y][point.y].object = &(obj_node->obj);
+            obj_node = obj_node->next;
+        }while(obj_node != NULL);
+    }
+    return 0;
+}
+
+/*************** Change state functions ****************/
+
+
+int rts_goto(th_obj *obj, th_point point)
+{
+    th_path *path;
+    th_point source;
+    if(!obj)
+    {
+        printf("rts_goto error: object invalid!\n");
+        return 0;
+    }
+    printf("Chanche %s state: go from (%d,%d) to (%d,%d)\n", 
+                obj->rname,
+                obj->x,
+                obj->y,
+                point.x,
+                point.y);
+    
+    source.x = obj->x;
+    source.y = obj->y;
+
+    if(!(path = ai_shortes_path(obj->player,obj->type,source, point)))
+    {
+        printf("No shortes path found or a error ocurred!\n");
+        return 0;
+    }
+    
+    obj->state.path = path;
+
+    ai_modify_state(obj->player, obj, GOTO);
+
+    //printf("Path found!\n");
+
+    return 0;
+}
+
+int rts_build(th_obj *obj, int type, th_point point)
+{
+    return 1;
+}
+
+int rts_die(th_obj *obj)
+{
+    return 1;
+}
+int rts_create(th_obj *obj, int type)
+{
+    return 1;
+}
+// For the folowing functions: target can be NULL or point 
+// can be -1,-1, but one must be valid. If all to are valid,
+// it will give preference to target.
+
+int rts_attack(th_obj *obj, th_obj *target, th_point point)
+{
+    return 1;
+}
+int rts_repair(th_obj *obj, th_obj *target, th_point point)
+{
+    return 1;
+}
+int rts_use(th_obj *obj, th_obj *target, th_point point)
+{
+    return 1;
+}
+
diff --git a/src/tuxrts.h b/src/tuxrts.h
index 7c97450..4e54033 100644
--- a/src/tuxrts.h
+++ b/src/tuxrts.h
@@ -8,12 +8,6 @@
 #include "llist.h"
 #include "objects.h"
 
-enum{
-    BUILD,
-    REPAIR,
-    ATTACK,
-    CREATE
-};
 
 typedef struct rts_vars{
     th_obj *selected_objs[30];
@@ -21,6 +15,8 @@ typedef struct rts_vars{
 }rts_vars;
 
 
+list_node *selected_node;
+
 /* tuxrts_mapinit(): Inizialize all map vars. This is
  * the fisrt function to call when we begin the game.
  * char *: Object file name, without .xml
@@ -50,6 +46,26 @@ th_obj *rts_get_object(int, th_point);
 
 void tuxrts_cleanup(void);
 
-list_node *selected_node;
+/*************** Change state functions ****************/
+
+int rts_update_game(void);
+
+int rts_goto(th_obj *obj, th_point point);
+
+int rts_build(th_obj *obj, int type, th_point point);
+
+int rts_die(th_obj *obj);
+
+int rts_create(th_obj *obj, int type);
+
+// For the folowing functions: target can be NULL or point 
+// can be -1,-1, but one must be valid. If all to are valid,
+// it will give preference to target.
+
+int rts_attack(th_obj *obj, th_obj *target, th_point point);
+
+int rts_repair(th_obj *obj, th_obj *target, th_point);
+
+int rts_use(th_obj *obj, th_obj *target, th_point point);
 
 #endif

-- 
tuxhistory - Educational history game



More information about the Tux4kids-commits mailing list