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

julio (none) julio at julio-desktop.
Fri Aug 6 20:55:07 UTC 2010


The following commit has been merged in the master branch:
commit b7e0be5ab940170615eef642b82927b0dc02f08c
Author: julio <julio at julio-desktop.(none)>
Date:   Fri Aug 6 15:53:37 2010 -0500

    Some advances against segfaults, but the memory corruption persists.

diff --git a/src/Makefile.am b/src/Makefile.am
index 226d503..1a2f309 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -51,7 +51,6 @@ tuxhistory_SOURCES = tuxhistory.c \
 	SDL_rotozoom.c	\
 	scandir.c	\
 	pixels.c	\
-	throttle.c	\
 	tuxrts.c
 
 
@@ -87,7 +86,6 @@ EXTRA_DIST = 	credits.h 	\
 	pixels.h		\
 	compiler.h		\
 	transtruct.h	\
-	throttle.h 		\
 	tuxrts.h
         
 
diff --git a/src/ai.c b/src/ai.c
index 6e9db73..35afce2 100644
--- a/src/ai.c
+++ b/src/ai.c
@@ -19,7 +19,7 @@
 #include "hashtable.h"
 
 // Hueristic distance between to points
-#define HDIST(x1, y1, x2, y2) ((x1<x2)?(x2-x1):(x1-x2)) + ((y1<y2)?(y2-y1):(y1-y2))
+#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)
@@ -49,6 +49,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;
 
     bheap *open;
     struct hashtable *closed;
@@ -76,13 +77,14 @@ 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);
         e[count].deph = 0;
         e[count].point = source;
         e[count].h = HDIST(e[count].point.x, e[count].point.y, goal.x, goal.y);
-        e[count].g = e[count].deph;
+        e[count].g = 0;
         e[count].val = e[count].g + e[count].h;
-        e[count].index = i;
+        e[count].index = count;
         e[count].parent = NULL;
         
         // Insert the initial node to the open list
@@ -95,6 +97,7 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
 
         while(open->count >= 0)
         {
+            printf("********** New Loop Cycle\n");
             // Remove the lowest element in open list
             // and add it to the closed list
             n = bheap_del(open);
@@ -103,6 +106,7 @@ 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("Element id to store in loop: %s, index: %d\n", n->id, n->index);
@@ -112,20 +116,24 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
                 printf("Error adding to hashtable!\n");
                 return NULL;
             }
-
-            printf("Element added to hashtable!\n");
+            hashtable_iter(closed, hashtable_default_hash);
             
 
             //Is this element the goal?
             if(n->point.x == goal.x && n->point.y == goal.y)
             {
+                printf("Solution deph is %d\n", n->deph);
+                solution = (th_point *)malloc(n->deph * sizeof(th_point));
+                i = 0;
+
                 while(n->parent)
                 {
                     printf("(%d,%d)\n",n->point.x, n->point.y);
+                    solution[i] = n->point;
                     n = n->parent;
-                }
-                
-                return NULL;
+                    i++;
+                } 
+                return solution;
             }
 
             printf("This element is not the goal!.. Trying...\n");
@@ -154,31 +162,46 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
                         sprintf(e[count].id, "%03d%03d", pt.x, pt.y);
                         e[count].index = count;
                         e[count].h = HDIST(e[count].point.x, e[count].point.y, goal.x, goal.y);
-                        e[count].g = e[count].deph; //Unnecessary?
+                        if( a == ISO_N || 
+                            a == ISO_S ||
+                            a == ISO_W ||
+                            a == ISO_E)
+                            e[count].g = n->g + 10; 
+                        else
+                            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\n, H: %d G:%d F:%d Deph:%d\n", e[count].id, e[count].h,
+                        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)
                         {
-                            if(p->val < e[count].val)
+                            printf("P exists in cloded list!\n");
+                            if(p->val > e[count].val)
                             {
                                 if(!hashtable_remove(closed, p->id))
                                 {
                                     printf("Error ocurred while trying to remove key in hashtable!\n");
+                                    hashtable_iter(closed, hashtable_default_hash);
                                     return NULL;
                                 }
+                                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");
                             }
                         }   
                         else
                         {
+                            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");
@@ -190,6 +213,8 @@ th_point *ai_shortes_path(int player, int unit, th_point source, th_point goal)
                 }
             }
         }
+        free_hashtable(closed);
+        bheap_free(open);
         FREE(e);
     }
     else
diff --git a/src/bheap.c b/src/bheap.c
index ba73011..a20fc5d 100644
--- a/src/bheap.c
+++ b/src/bheap.c
@@ -12,6 +12,7 @@
 
 #include<stdio.h>
 #include<stdlib.h>
+#include<assert.h>
 
 #include "bheap.h"
 
@@ -45,16 +46,12 @@ int bheap_add(bheap *heap, bheap_node *data)
 {
     //bheap_node *node;
     int m;
+    int i;
 
-    heap->count++;
-
-    if(heap->size < heap->count)
-        return 0;
-
-    if(heap == NULL)
+    if(!heap)
         return 0;
 
-    if(data == NULL)
+    if(!data)
         return 0;
 
     /*
@@ -64,7 +61,33 @@ int bheap_add(bheap *heap, bheap_node *data)
 
     *node = data;
     */
-    
+
+    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++)
+    {
+        if(!heap->items[i])
+        {
+            printf("Error accesing item!");
+            return 0;
+        }
+        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"); 
+            return 1;
+        }
+    }
+    printf("Is not already in open list!\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");
     m = heap->count;
     heap->items[heap->count] = data;
     while(m != 0)
@@ -79,6 +102,10 @@ int bheap_add(bheap *heap, bheap_node *data)
             break;
         }
     }
+
+    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;
 }
 
@@ -96,6 +123,7 @@ bheap_node *bheap_del(bheap *heap)
     node = heap->items[0];
     //free(heap->items[0]);
     heap->items[0] = heap->items[heap->count];
+    heap->items[heap->count] = NULL;
     heap->count--;
     if(heap->count < 0)
         printf("heap is empty!\n");
@@ -134,14 +162,28 @@ bheap_node *bheap_del(bheap *heap)
 void bheap_print(bheap *heap)
 {
     int i;
-    if(heap->count < 0)
+    if(!heap)
+    {
+        printf("bheap_print: NULL parameter!\n");
+        return;
+    }
+    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++)
-        printf("%d ", heap->items[i]->val);
-    printf("\n");
+    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("%s:%d\n ", heap->items[i]->id, heap->items[i]->val);
+    }
+    printf("End of bheap_print\n");
 }
 
 void bheap_free(bheap *heap)
@@ -152,8 +194,10 @@ void bheap_free(bheap *heap)
         free(heap->items[i]);
         heap->items[i] = NULL;
     }*/
+    assert(heap->items != NULL);
     free(heap->items);
     heap->items = NULL;
+    assert(heap != NULL);
     free(heap);
     heap = NULL;
 }
diff --git a/src/game.c b/src/game.c
index ee8503e..47e8442 100644
--- a/src/game.c
+++ b/src/game.c
@@ -224,8 +224,8 @@ static void game_draw(void)
 
     dest.y = 0;
 
-    /*TODO: Separate each Layer drawing in different functions.*/
 
+    /*TODO: Separate each Layer drawing in different functions.*/
 
     /*First layer: terrain*/
     SDL_BlitSurface(map_image, &origin, screen, &dest);
@@ -284,9 +284,10 @@ static void game_draw(void)
         SDL_BlitSurface(images[IMG_ISOWRONG], NULL, screen, &io.go_rect_dest);
         io.go_rect.x = -1;
         io.go_rect.y = -1;
-        io.go_valid_flag = 0;
     }
    
+    io.go_valid_flag = 0;
+
     /*Third layer: User Interface*/
 
     //TODO: Write a panel function to manipulate the game...
@@ -307,13 +308,14 @@ 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);
 
-            sprintf(tmp_text,"%d / %d", selection.selected_objs[0]->actual_live,
-                                        selection.selected_objs[0]->live);
-            th_ShowMessage(tmp_text, 15, 
-                    objects[selection.selected_objs[0]->name_enum]->w + dest.x + 10, dest.y+20);
+            //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);
+            //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);
 
 
             dest.y = dest.y + 20;
@@ -325,11 +327,11 @@ static void game_draw(void)
     dest.x = (screen->w - mini_map_image->w - 5);
     dest.y = (screen->h - mini_map_image->h - 5);
     SDL_BlitSurface(mini_map_image, NULL, screen, &dest);
-    
 
     dest.x = (screen->w - images[IMG_STOP]->w - 5);
     dest.y = glyph_offset;
     SDL_BlitSurface(images[IMG_STOP], NULL, screen, &dest);
+    
 
     /*dest.x = 20;
     dest.y = 20;
@@ -344,8 +346,9 @@ static void game_handle_mouse(void)
 {
     th_point Pmousemap;
     th_point Pdtmap;
+    th_point *path;
     int i, j;
-
+    
     Pmousemap = mouse_map(io.Pmouse, Pscreen);
     Pdtmap = Pscreen;
     if(Pmousemap.x != -1 && Pmousemap.y != -1)
@@ -492,12 +495,14 @@ static void game_handle_mouse(void)
                 selection.selected_objs[0]->y,
                 io.go_xy.x,
                 io.go_xy.y);
-        if(!ai_shortes_path(0,0,Pmousemap, io.go_xy))
+        if(!(path = ai_shortes_path(0,0,Pmousemap, io.go_xy)))
             printf("No shortes path found or a error ocurred!\n");
+        else
+            printf("Path found!\n");
     }
-
 }
 
+
 static int pause_game(void)
 {
   /* NOTE - done and quit changed to pause_done and pause_quit */
diff --git a/src/hashtable.c b/src/hashtable.c
index e15ccf4..09342fa 100644
--- a/src/hashtable.c
+++ b/src/hashtable.c
@@ -74,6 +74,8 @@ 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");
+
     if(!table) return 0;
 
     if(entry) {
@@ -83,16 +85,25 @@ int hashtable_add(struct hashtable *table, char *key, void *value)
         entry->prev = NULL;
         n = compute_hash(table, key);
         entries = table->bucket[n];
-        if(!entries) table->bucket[n] = entry;
+        if(!entries)
+        {
+            printf(" - No entry in buket, storing!\n");
+            table->bucket[n] = entry;
+        }
         else {
+          printf(" - First buket full Next!\n");
           while(entries->next) 
-            entries = entries->next;
+          {
+              printf(" - Next>\n");
+              entries = entries->next;
+          }
           entries->next = entry;
-          entry->prev = entries;
+          entries->next->prev = entries;
         }
+        printf("Ending hashtable_add...\n");
         return 1;
     }
-    
+    printf("Ending hashtable_add...\n");
     return 0;
 }
 
@@ -166,8 +177,12 @@ void hashtable_iter(const struct hashtable *table,
     struct hashtable_entry *entry;
     
     for(i = 0; i < table->nbuckets; i++) {
-        for(entry = table->bucket[i]; entry; entry = entry->next) {
-            func(entry->key, entry->value);
+        if(table->bucket[i])
+        {
+            for(entry = table->bucket[i]; entry; entry = entry->next) {
+                //func(entry->key, entry->value);
+                continue;
+            }
         }
     }
 }
@@ -183,12 +198,25 @@ 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");
+
     while(entry) {
+        printf("LUNext>\n");
         if(!strcmp(key, entry->key)) break;
         entry = entry->next;
     }
     
-    return entry ? entry->value : NULL;
+    printf("Ending hashtable_lookup...\n");
+    if(entry)
+    {
+        return entry->value;
+    }
+    else
+    {
+        printf("Returning NULL pointer\n");
+        return NULL;
+    }
+    //return entry ? entry->value : NULL;
 }
 
  /** remove the value bound to a key.
@@ -202,7 +230,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");
   while(entry) {
+    printf("Next>\n");
     if(!strcmp(key, entry->key)) break;
     entry = entry->next;
   }
@@ -215,6 +245,7 @@ int hashtable_remove(const struct hashtable *table,
       }
       else {
         table->bucket[n] = entry->next;
+        entry->next->prev= NULL;
       }
     }
     else if(entry->prev)
@@ -227,9 +258,11 @@ int hashtable_remove(const struct hashtable *table,
     
     free(entry);
     entry = NULL;
+    printf("Ending hashtable_remove...\n");
     return 1;
   }
   else {
+    printf("Ending hashtable_remove...\n");
     return 0;
   }
 }   
diff --git a/src/highscore.c b/src/highscore.c
index 8ceb984..5afbe59 100644
--- a/src/highscore.c
+++ b/src/highscore.c
@@ -21,8 +21,6 @@
 #include "SDL_extras.h"
 #include "convert_utf.h"
 #include "transtruct.h"
-#include "throttle.h"
-
 
 typedef struct high_score_entry {
   int score;
@@ -844,7 +842,6 @@ int Ready(const char* heading)
     }
 
     HandleTitleScreenAnimations();
-    Throttle(20, &timer);
     frame++;
   }  // End of while (!finished) loop
 
diff --git a/src/objects.h b/src/objects.h
index 6a75c2d..a80a5dc 100644
--- a/src/objects.h
+++ b/src/objects.h
@@ -27,6 +27,7 @@ typedef struct th_obj{
     int attack;
     int move;
     int player;
+    int path_flag; //Need pathfinding?
 }th_obj;
 
 int object_counter;
diff --git a/src/throttle.c b/src/throttle.c
deleted file mode 100644
index 5052fc8..0000000
--- a/src/throttle.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
-*  C Implementation: network.c
-*
-*         Description: A simple function that uses SDL_Delay() to keep 
-*                      loops from eating all available CPU.
-
-*
-* Author: David Bruce, and the TuxMath team, (C) 2009
-* Developers list: <tuxmath-devel at lists.sourceforge.net>
-*
-* Copyright: See COPYING file that comes with this distribution.  (Briefly, GNU GPL).
-*/
-
-
-#include "SDL.h"
-
-/* NOTE now store the time elsewhere to make function thread-safe                          */
-
-void Throttle(int loop_msec, Uint32* last_t)
-{
-  Uint32 now_t, wait_t;
-
-  if(!last_t)
-    return;
-
-  //Target loop time must be between 0 and 1000 msec:
-  if(loop_msec < 0)
-    loop_msec = 0;
-  if(loop_msec > 1000)
-    loop_msec = 1000;
-
-  //See if we need to wait:
-  now_t = SDL_GetTicks();
-  if (now_t < (*last_t + loop_msec))
-  {
-    wait_t = (*last_t + loop_msec) - now_t;
-    //Avoid problem if we somehow wrap past uint32 size (at 49.7 days!)
-    if(wait_t < 0)
-      wait_t = 0;
-    if(wait_t > loop_msec)
-      wait_t = loop_msec;
-    SDL_Delay(wait_t);
-  }
-  *last_t = SDL_GetTicks();
-}
diff --git a/src/titlescreen.c b/src/titlescreen.c
index 420b58c..3fea1a8 100644
--- a/src/titlescreen.c
+++ b/src/titlescreen.c
@@ -28,7 +28,6 @@
 #include "loaders.h"
 #include "SDL_extras.h"
 #include "menu.h"
-#include "throttle.h"
 
 /* --- Data Structure for Dirty Blitting --- */
 SDL_Rect srcupdate[MAX_UPDATES];
@@ -583,8 +582,6 @@ void ShowMessage(int font_size, const char* str1, const char* str2,
 
     HandleTitleScreenAnimations();
 
-    /* Wait so we keep frame rate constant: */
-    Throttle(20, &timer);
   }  // End of while (!finished) loop
 
   SDL_FreeSurface(s1);

-- 
tuxhistory - Educational history game



More information about the Tux4kids-commits mailing list