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

julio (none) julio at julio-desktop.
Sat Aug 7 06:53:45 UTC 2010


The following commit has been merged in the master branch:
commit de153de6cc148cc25de52205f6234fcdeafc1f9b
Author: julio <julio at julio-desktop.(none)>
Date:   Sat Aug 7 01:52:45 2010 -0500

    Tuxhistory has now a complet(but still in a initial stage) path finding function, without segfaults! Other bugs fixed: Selection of objects, ***gmaps 0 player is now accesible, and io variables are now initialize in game_init.

diff --git a/src/ai.c b/src/ai.c
index 35afce2..a1026ef 100644
--- a/src/ai.c
+++ b/src/ai.c
@@ -64,7 +64,9 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
         goal.y >= 0 && goal.y <= y_tildes       )
     {
         
-        e = (bheap_node *)malloc(x_tildes * y_tildes * sizeof(bheap_node));
+        // TODO: Actual way to store binary heap nodes is a bad idea. We need make 
+        //       te code to use the dinamic strucures in bheap_add.
+        e = (bheap_node *)malloc(1000 *  x_tildes * y_tildes * sizeof(bheap_node));
         if(e == NULL)
             return NULL;
 
@@ -77,8 +79,8 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
 
         // Defining the initial node 
         sprintf(e[count].id, "%03d%03d", source.x, source.y);
-        printf("====================== A* STARTING... =====================\n");
-        printf("Element id to store: %s\n", e[count].id);
+        //printf("====================== A* STARTING... =====================\n");
+        //printf("Element id to store: %s\n", e[count].id);
         e[count].deph = 0;
         e[count].point = source;
         e[count].h = HDIST(e[count].point.x, e[count].point.y, goal.x, goal.y);
@@ -93,11 +95,11 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
             printf("Coudn't add element to the open list!\n");
             return NULL;
         }
-        bheap_print(open);
+        //bheap_print(open);
 
         while(open->count >= 0)
         {
-            printf("********** New Loop Cycle\n");
+            //printf("********** New Loop Cycle\n");
             // Remove the lowest element in open list
             // and add it to the closed list
             n = bheap_del(open);
@@ -106,17 +108,17 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
                 printf("Error deleting the priority element from open list!\n");
                 return NULL;
             }
-            printf("Removed id: %s\n", n->id);
-            bheap_print(open);
+            //printf("Removed id: %s\n", n->id);
+            //bheap_print(open);
             
-            printf("Element id to store in loop: %s, index: %d\n", n->id, n->index);
+            //printf("Element id to store in loop: %s, index: %d\n", n->id, n->index);
 
             if(!hashtable_add(closed, e[n->index].id, &e[n->index]))
             {
                 printf("Error adding to hashtable!\n");
                 return NULL;
             }
-            hashtable_iter(closed, hashtable_default_hash);
+            //hashtable_iter(closed, hashtable_default_hash);
             
 
             //Is this element the goal?
@@ -133,10 +135,13 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
                     n = n->parent;
                     i++;
                 } 
+                free_hashtable(closed);
+                bheap_free(open);
+                FREE(e);
                 return solution;
             }
 
-            printf("This element is not the goal!.. Trying...\n");
+            //printf("This element is not the goal!.. Trying...\n");
 
             //For each valid move for n
             for(a = 0; a < NUM_DIRS; a++)
@@ -144,15 +149,15 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
                 vector = get_vector(n->point, a);
                 if(vector.x != -2 && vector.y != -2)
                 {
-                    printf("Vector is valid... \n");
-                    printf("For %d direction tile in (%d,%d) is valid?\n", a, n->point.x, n->point.y);
+                    //printf("Vector is valid... \n");
+                    //printf("For %d direction tile in (%d,%d) is valid?\n", a, n->point.x, n->point.y);
 
                     pt.x = vector.x + n->point.x;
                     pt.y = vector.y + n->point.y;
                     if(rts_valid_tile(player, unit, pt))
                     {
 
-                        printf("Adding direction %d to open list!\n", a);
+                        //printf("Adding direction %d to open list!\n", a);
 
                         //New valid element
                         count++;
@@ -171,13 +176,13 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
                             e[count].g = n->g + 14;
                         e[count].val = e[count].g + e[count].h; // F = G + H
                         e[count].parent = n;
-                        printf("Actual id: %s, H: %d G:%d F:%d Deph:%d\n", e[count].id, e[count].h,
-                               e[count].g, e[count].val, e[count].deph);
+                        //printf("Actual id: %s, H: %d G:%d F:%d Deph:%d\n", e[count].id, e[count].h,
+                        //       e[count].g, e[count].val, e[count].deph);
 
                         //Is this element in closed list?
                         if((p = hashtable_lookup(closed, e[count].id)) != NULL)
                         {
-                            printf("P exists in cloded list!\n");
+                            //printf("P exists in cloded list!\n");
                             if(p->val > e[count].val)
                             {
                                 if(!hashtable_remove(closed, p->id))
@@ -186,29 +191,29 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
                                     hashtable_iter(closed, hashtable_default_hash);
                                     return NULL;
                                 }
-                                else
-                                {
-                                    printf("Removes OK, let's check integrity!\n");
-                                    hashtable_iter(closed, hashtable_default_hash);
-                                }
+                                //else
+                                //{
+                                    //printf("Removes OK, let's check integrity!\n");
+                                    //hashtable_iter(closed, hashtable_default_hash);
+                                //}
                                 if(!bheap_add(open, p))
                                 {
                                     printf("Error ocurred while adding a element to open list\n");
                                     return NULL;
                                 }
-                                printf("Succesfully removed from closed list and added to open list\n");
+                                //printf("Succesfully removed from closed list and added to open list\n");
                             }
                         }   
                         else
                         {
-                            printf("P doesn't exist in closed list!\n");
+                            //printf("P doesn't exist in closed list!\n");
                             if(!bheap_add(open, &e[count]))
                             {
                                 printf("Error ocurred while adding a new element to open list\n");
                                 return NULL;
                             }
                         }
-                        bheap_print(open);
+                        //bheap_print(open);
                     }
                 }
             }
diff --git a/src/bheap.c b/src/bheap.c
index a20fc5d..2ac64fc 100644
--- a/src/bheap.c
+++ b/src/bheap.c
@@ -62,32 +62,32 @@ int bheap_add(bheap *heap, bheap_node *data)
     *node = data;
     */
 
-    printf("Begining bheap_add... the count is %d\n", heap->count);
+    //printf("Begining bheap_add... the count is %d\n", heap->count);
 
-    printf("Data to store, val %d, x %d, y %d.\n", data->val, data->point.x, data->point.y);
-    for(i=0; i<heap->count; i++)
+    //printf("Data to store, val %d, x %d, y %d.\n", data->val, data->point.x, data->point.y);
+    for(i=0; i<=heap->count; i++)
     {
         if(!heap->items[i])
         {
-            printf("Error accesing item!");
+            //printf("Error accesing item!");
             return 0;
         }
-        printf("In loop %d, val %d\n", i, heap->items[i]->val);
+        //printf("In loop %d, val %d\n", i, heap->items[i]->val);
         if( heap->items[i]->point.x == data->point.x &&
             heap->items[i]->point.y == data->point.y)
         {
-            printf("Is allready in openlist\n"); 
+            //printf("Is allready in openlist\n"); 
             return 1;
         }
     }
-    printf("Is not already in open list!\n");
+    //printf("Is not already in open list!\n");
 
     heap->count++;
-    printf("* New count value %d\n", heap->count);
+    //printf("* New count value %d\n", heap->count);
     if(heap->size < heap->count)
         return 0;
 
-    printf("* Count is smaller than size..\n");
+    //printf("* Count is smaller than size..\n");
     m = heap->count;
     heap->items[heap->count] = data;
     while(m != 0)
@@ -103,9 +103,9 @@ int bheap_add(bheap *heap, bheap_node *data)
         }
     }
 
-    printf("Ending bheap_add...\n");
-    printf("OK Cheking integrity...\n");
-    printf("Priority :%d New element: %d ALL O.K.\n", heap->items[0]->val, heap->items[heap->count]->val);
+    //printf("Ending bheap_add...\n");
+    //printf("OK Cheking integrity...\n");
+    //printf("Priority :%d New element: %d ALL O.K.\n", heap->items[0]->val, heap->items[heap->count]->val);
     return 1;
 }
 
@@ -167,23 +167,23 @@ void bheap_print(bheap *heap)
         printf("bheap_print: NULL parameter!\n");
         return;
     }
-    printf("Begining bheap_printf... the count is %d\n", heap->count);
+    //printf("Begining bheap_printf... the count is %d\n", heap->count);
     if(heap->count < 0 || heap->count >= heap->size)
     {
         printf("Error, there are no elements to print!\n");
         return;
     }
-    for(i=0; i<heap->count; i++)
+    for(i=0; i<=heap->count; i++)
     {
         if(!heap->items)
         {
             printf("Error trying to print item %d\n", i);
             return;
         }
-        printf("loop %d ",i);
+        //printf("loop %d ",i);
         printf("%s:%d\n ", heap->items[i]->id, heap->items[i]->val);
     }
-    printf("End of bheap_print\n");
+    //printf("End of bheap_print\n");
 }
 
 void bheap_free(bheap *heap)
diff --git a/src/game.c b/src/game.c
index 47e8442..d932a90 100644
--- a/src/game.c
+++ b/src/game.c
@@ -80,6 +80,8 @@ static th_point Pscreen;
 static int screen_margin_in;
 static int screen_margin_out;
 
+int this_player;
+
 typedef struct io_vars
 {
     th_point Pmouse;
@@ -156,6 +158,46 @@ static int game_init(void)
     SDL_quit_received = 0;
     escape_received = 0;
 
+    // Input/output data strucure init.
+    io.Pmouse.x = 0;
+    io.Pmouse.y = 0;
+    io.Prclick.x = -1;
+    io.Prclick.y = -1;
+    io.Plclick.x = -1;
+    io.Plclick.y = -1;
+    io.mousedown_flag = 0;
+    io.mousedownr_flag = 0;
+    io.mouseclicked_flag = 0;
+    io.select_xy.x = 0;
+    io.select_xy.y = 0;
+    io.select_xy.w = 0;
+    io.select_xy.h = 0;
+    io.select_rect.x = 0;
+    io.select_rect.y = 0;
+    io.select_rect.w = 0;
+    io.select_rect.h = 0;
+    io.select_rect_dest.x = 0;
+    io.select_rect_dest.y = 0;
+    io.select_rect_dest.w = 0;
+    io.select_rect_dest.h = 0;
+    io.select.x = 0;
+    io.select.y = 0;
+    io.select.w = 0;
+    io.select.h = 0;
+    io.go_xy.x = -1;
+    io.go_xy.y = -1;
+    io.go_rect.x = 0;
+    io.go_rect.y = 0;
+    io.go_rect.w = 0;
+    io.go_rect.h = 0;
+    io.go_rect_dest.x = 0;
+    io.go_rect_dest.y = 0;
+    io.go_rect_dest.w = 0;
+    io.go_rect_dest.h = 0;
+    io.go_valid_flag = 0;
+
+    // Player?
+    this_player = 1;
 
     if(tuxrts_init("objects", "map", 2))
         return 1;
@@ -309,13 +351,13 @@ static void game_draw(void)
             dest.x = dest.x + 2;
             dest.y = dest.y + 2;
 
-            //th_ShowMessage(selection.selected_objs[0]->rname, 12, dest.x+2, dest.y+2);
+            th_ShowMessage(selection.selected_objs[0]->rname, 12, dest.x+2, dest.y+2);
 
-            //sprintf(tmp_text,"%d / %d", selection.selected_objs[0]->actual_live,
-            //                            selection.selected_objs[0]->live);
+            sprintf(tmp_text,"%d / %d", selection.selected_objs[0]->actual_live,
+                                        selection.selected_objs[0]->live);
             //printf("dir is: %s\n", tmp_text);
-            //th_ShowMessage(tmp_text, 15, 
-            //        objects[selection.selected_objs[0]->name_enum]->w + dest.x + 10, dest.y+20);
+            th_ShowMessage(tmp_text, 15, 
+                    objects[selection.selected_objs[0]->name_enum]->w + dest.x + 10, dest.y+20);
 
 
             dest.y = dest.y + 20;
@@ -418,6 +460,7 @@ static void game_handle_mouse(void)
             Pmousemap = mouse_map(io.Plclick, Pscreen);
             if(Pmousemap.x != -1 && Pmousemap.y != -1)
             {
+                printf("Mouse clicked in a valid tile!\n");
                 io.mousedown_flag = 0;
                 io.select_rect.x = gmaps[0][Pmousemap.x][Pmousemap.y].rect.x; 
                 io.select_rect.y = gmaps[0][Pmousemap.x][Pmousemap.y].rect.y;
@@ -428,6 +471,7 @@ static void game_handle_mouse(void)
                 // select that object
 
                 selection.selected_objs[0]=rts_get_object(0,Pmousemap);
+
                 if(selection.selected_objs[0] != NULL)
                 {
                     selection.selected_num = 0;
diff --git a/src/graphs.c b/src/graphs.c
index 8c16519..626e332 100644
--- a/src/graphs.c
+++ b/src/graphs.c
@@ -72,24 +72,31 @@ int create_gmaps(int players)
     }
 
     printf("x_tiles: %d, y_tiles: %d\n", x_tildes, y_tildes);
-    for(i = 1; i < players; i++)
+    for(i = 0; i < players; i++)
     {
         count = 0;
-        for(j = 0; j < x_tildes; j++)
+        for(j = 0; j <= x_tildes; j++)
         {
-            for(k = 0; k < y_tildes; k++)
+            for(k = 0; k <= y_tildes; k++)
             {
                 count++;
-                gmaps[i][j][k].visible = 1;
                 gmaps[i][j][k].terrain = map[j][k].terrain;
                 gmaps[i][j][k].object = NULL;
                 gmaps[i][j][k].point.x = j;
                 gmaps[i][j][k].point.y = k;
+                if(i == 0)
+                {
+                    gmaps[i][j][k].visible = 1;
+                    gmaps[i][j][k].explored = 1;
+                }
+                else
+                {
+                    gmaps[i][j][k].visible = 0;
+                    gmaps[i][j][k].explored = 0;
+                }
 
                 for(l = 0; l < NUM_DIRS; l++)
                 {
-                    point.x = j;
-                    point.y = k;
                     vector = get_vector(point, l);
                     if(vector.x != -2 && vector.y != -2)
                     {
@@ -99,21 +106,12 @@ int create_gmaps(int players)
                     {
                         gmaps[i][j][k].nodes[l] = NULL;
                     }
-                    if(i > 0)
-                    {
-                        gmaps[i][j][k].visible = 1;
-                        gmaps[i][j][k].explored = 1;
-                    }
-                    else
-                    {
-                        gmaps[i][j][k].visible = 0;
-                        gmaps[i][j][k].explored = 0;
-                    }
                 }
                 gmaps[i][j][k].id = count;
             }
         }
     }
+
     return 0;
 }
 
diff --git a/src/hashtable.c b/src/hashtable.c
index 09342fa..c5cd9cd 100644
--- a/src/hashtable.c
+++ b/src/hashtable.c
@@ -57,7 +57,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 static inline unsigned int compute_hash(const struct hashtable *table,
                                         const char *key)
 {
-    printf("Init compute hash for %s\n", key);
+    //printf("Init compute hash for %s\n", key);
     unsigned int n = table->hash(key);
     return (n < table->nbuckets) ? n : (n % table->nbuckets);
 }
@@ -74,7 +74,7 @@ int hashtable_add(struct hashtable *table, char *key, void *value)
     struct hashtable_entry *entry = NEW(struct hashtable_entry);
     struct hashtable_entry *entries;
 
-    printf("Begining hashtable_add...\n");
+    //printf("Begining hashtable_add...\n");
 
     if(!table) return 0;
 
@@ -87,23 +87,23 @@ int hashtable_add(struct hashtable *table, char *key, void *value)
         entries = table->bucket[n];
         if(!entries)
         {
-            printf(" - No entry in buket, storing!\n");
+            //printf(" - No entry in buket, storing!\n");
             table->bucket[n] = entry;
         }
         else {
-          printf(" - First buket full Next!\n");
+          //printf(" - First buket full Next!\n");
           while(entries->next) 
           {
-              printf(" - Next>\n");
+              //printf(" - Next>\n");
               entries = entries->next;
           }
           entries->next = entry;
           entries->next->prev = entries;
         }
-        printf("Ending hashtable_add...\n");
+        //printf("Ending hashtable_add...\n");
         return 1;
     }
-    printf("Ending hashtable_add...\n");
+    //printf("Ending hashtable_add...\n");
     return 0;
 }
 
@@ -181,7 +181,7 @@ void hashtable_iter(const struct hashtable *table,
         {
             for(entry = table->bucket[i]; entry; entry = entry->next) {
                 //func(entry->key, entry->value);
-                continue;
+                printf("key -> %s\n", entry->key);
             }
         }
     }
@@ -198,22 +198,24 @@ void *hashtable_lookup(const struct hashtable *table,
     unsigned int n = compute_hash(table, key);
     struct hashtable_entry *entry = table->bucket[n];
     
-    printf("Begining hashtable_lookup...\n");
+    //printf("Begining hashtable_lookup...\n");
 
     while(entry) {
-        printf("LUNext>\n");
+        //printf("LUNext>\n");
+        //printf("%s key OK\n", key);
         if(!strcmp(key, entry->key)) break;
+        //printf("lunext cmp passed\n");
         entry = entry->next;
     }
     
-    printf("Ending hashtable_lookup...\n");
+    //printf("Ending hashtable_lookup...\n");
     if(entry)
     {
         return entry->value;
     }
     else
     {
-        printf("Returning NULL pointer\n");
+        //printf("Returning NULL pointer\n");
         return NULL;
     }
     //return entry ? entry->value : NULL;
@@ -230,9 +232,9 @@ int hashtable_remove(const struct hashtable *table,
   unsigned int n = compute_hash(table, key);
   struct hashtable_entry *entry = table->bucket[n];
 
-  printf("Begining hashtable_remove...\n");
+  //printf("Begining hashtable_remove...\n");
   while(entry) {
-    printf("Next>\n");
+    //printf("Next>\n");
     if(!strcmp(key, entry->key)) break;
     entry = entry->next;
   }
diff --git a/src/tuxrts.c b/src/tuxrts.c
index 28379c6..73eb097 100644
--- a/src/tuxrts.c
+++ b/src/tuxrts.c
@@ -126,13 +126,20 @@ th_obj *rts_get_object(int player, th_point coords)
 {
     list_node *obj_node;
 
-    if(gmaps[player][coords.x][coords.y].visible != 0)
+    printf("Enter to rts_get_object\n");
+
+    if(gmaps[player][coords.x][coords.y].visible != 1)
+    {
+        printf("No visible in (%d,%d).\n", coords.x, coords.y);
         return NULL;
+    }
 
     obj_node = list_nodes;
     if(obj_node != NULL)
     {
         do{
+            printf("Object: (%d,%d) = (%d,%d)\n", obj_node->obj.x, 
+                    obj_node->obj.y, coords.x, coords.y);
             if( obj_node->obj.x == coords.x &&
                 obj_node->obj.y == coords.y   )
                 return &obj_node->obj;

-- 
tuxhistory - Educational history game



More information about the Tux4kids-commits mailing list