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

julio (none) julio at julio-desktop.
Thu Aug 5 01:43:26 UTC 2010


The following commit has been merged in the master branch:
commit 4f18f96e3a6d74e7755925e311d7818083f0a2a8
Author: julio <julio at julio-desktop.(none)>
Date:   Wed Aug 4 20:42:28 2010 -0500

    Binary heap for A* ready and some advances in A* algorithm.

diff --git a/src/Makefile.am b/src/Makefile.am
index 5713c6a..226d503 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -26,6 +26,8 @@ endif
 
 
 tuxhistory_SOURCES = tuxhistory.c \
+	ai.c		\
+	bheap.c		\
 	setup.c 	\
 	titlescreen.c	\
 	map.c		\
@@ -58,6 +60,8 @@ TuxHistory_SOURCES  = $(tuxhistory_SOURCES) tuxmathrc.rc
 
 
 EXTRA_DIST = 	credits.h 	\
+	ai.h		\
+	bheap.h		\
 	fileops.h 	\
 	game.h		\
 	menu.h		\
diff --git a/src/ai.c b/src/ai.c
new file mode 100644
index 0000000..78e8ab3
--- /dev/null
+++ b/src/ai.c
@@ -0,0 +1,40 @@
+
+/* ai.c
+ *
+ * Description: AI mainy path finding functions for the game lives here
+ * 
+ * Author: Jesús Manuel Mager Hois (fongog at gmail.com) 2010
+ * Copyright: GPL v3 or later
+ *
+ * Part of "Tux4Kids Project
+ * http://www.tux4kids.com
+ * 
+ */
+
+#include "tuxhistory.h"
+#include "globals.h"
+#include "graphs.h"
+#include "ai.h"
+#include "tuxrts.h"
+#include "bheap.h"
+
+// Hueristic distance between to points
+#define HDIST(x1, y1, x2, y2) ((x1<x2)?(x2-x1):(x1-x2)) + ((y1<y2)?(y2-y1):(y1-y2))
+
+void ai_shortes_path(th_point source, th_point goal)
+{
+    int H, G, F;
+    if( source.x >= 0 && source.x < x_tildes   &&
+        source.y >= 0 && source.y < y_tildes   &&
+        goal.x >= 0 && goal.x < x_tildes       &&
+        goal.y >= 0 && goal.y < y_tildes       )
+    {
+        H = HDIST(source.x, source.y, goal.x, goal.y);
+        printf("H Distance %d\n", H);
+    }
+    else
+    {
+        printf("Bad point references!\n");
+    }
+}
+
diff --git a/src/ai.h b/src/ai.h
new file mode 100644
index 0000000..5597b40
--- /dev/null
+++ b/src/ai.h
@@ -0,0 +1,18 @@
+
+/* ai.h
+ *
+ * Description: AI mainy path finding functions for the game lives here
+ * 
+ * Author: Jesús Manuel Mager Hois (fongog at gmail.com) 2010
+ * Copyright: GPL v3 or later
+ *
+ * Part of "Tux4Kids Project
+ * http://www.tux4kids.com
+ * 
+ */
+
+#include "tuxhistory.h"
+#include "globals.h"
+#include "graphs.h"
+
+void ai_shortes_path(th_point source, th_point goal);
diff --git a/src/bheap.c b/src/bheap.c
new file mode 100644
index 0000000..3c1a1a6
--- /dev/null
+++ b/src/bheap.c
@@ -0,0 +1,132 @@
+#include<stdio.h>
+#include<stdlib.h>
+
+#include "bheap.h"
+
+#define LEFT(K) (2*K)
+#define RIGHT(K) (2*K+1)
+#define SWAP(H,A,B) void *tmp = H->items[A]; \
+                    H->items[A] = H->items[B]; \
+                    H->items[B] = tmp;
+
+bheap *bheap_init(int size)
+{
+    bheap *heap;
+    bheap_node **items;
+
+    heap = (bheap *)malloc(sizeof(bheap));
+    if(heap == NULL)
+        return NULL;
+ 
+    items = (bheap_node **)malloc(size * sizeof(bheap_node *));
+    if(items == NULL)
+        return NULL;
+   
+    heap->size = size;
+    heap->count = -1;
+    heap->items = items;
+
+    return heap;
+}
+
+int bheap_add(bheap *heap, bheap_node data)
+{
+    bheap_node *node;
+    int m;
+
+    heap->count++;
+
+    if(heap->size < heap->count)
+        return 0;
+
+    if(heap == NULL)
+        return 0;
+
+    node = (bheap_node *)malloc(sizeof(bheap_node));
+    if(node == NULL)
+        return 0;
+
+    *node = data;
+    
+    m = heap->count;
+    heap->items[heap->count] = node;
+    while(m != 0)
+    {
+        if(heap->items[m]->val < heap->items[m/2]->val)
+        {
+            SWAP(heap, m, m/2);
+            m = m/2;
+        }
+        else
+        {
+            break;
+        }
+    }
+    return 1;
+}
+
+bheap_node bheap_del(bheap *heap)
+{
+    int u, v;
+    bheap_node node;
+    
+    node = *heap->items[0];
+    free(heap->items[0]);
+    heap->items[0] = heap->items[heap->count];
+    heap->count--;
+    v = 0;
+    do{
+        u = v;
+        if(RIGHT(u) <= heap->count)
+        {
+            if(heap->items[u]->val >= heap->items[LEFT(u)]->val)
+            {
+                v = LEFT(u);
+            }
+            if(heap->items[v]->val >= heap->items[RIGHT(u)]->val)
+            {
+                v = RIGHT(u);
+            }
+        }
+        else if(LEFT(u) <= heap->count)
+        {
+            if(heap->items[u]->val >= heap->items[LEFT(u)]->val)
+            {
+                v = LEFT(u);
+            }
+        }
+        if(u != v)
+        {
+            SWAP(heap,u, v);
+        }
+        else
+        {
+            return node;
+        }
+    }while(1);
+}
+
+void bheap_print(bheap *heap)
+{
+    int i;
+    for(i=0; i<=heap->count; i++)
+        printf("%d ", heap->items[i]->val);
+    printf("\n");
+}
+
+void bheap_free(bheap *heap)
+{
+    int i;
+    for(i=0; i<=heap->count; i++)
+    {
+        free(heap->items[i]);
+        heap->items[i] = NULL;
+    }
+    free(heap);
+    heap = NULL;
+}
+
+
+    
+
+
diff --git a/src/bheap.h b/src/bheap.h
new file mode 100644
index 0000000..94518e5
--- /dev/null
+++ b/src/bheap.h
@@ -0,0 +1,20 @@
+#ifndef BHEAP_H
+#define BHEAP_H
+
+typedef struct bheap_node{
+    int val;
+}bheap_node;
+
+typedef struct bheap{
+    int size;
+    int count;
+    bheap_node **items;
+}bheap;
+
+bheap *bheap_init(int size);
+int bheap_add(bheap *heap, bheap_node data);
+void bheap_print(bheap *heap);
+bheap_node bheap_del(bheap *heap);
+void bheap_free(bheap *heap);
+
+#endif
diff --git a/src/game.c b/src/game.c
index b63f2d0..c9faab1 100644
--- a/src/game.c
+++ b/src/game.c
@@ -481,6 +481,15 @@ static void game_handle_mouse(void)
         io.go_rect.y = -1;
         io.go_valid_flag = 0;
     }
+    if( selection.selected_num != -1        && 
+        selection.selected_objs[0] != NULL  &&
+        io.go_valid_flag)
+    {
+        Pmousemap.x = selection.selected_objs[0]->x;
+        Pmousemap.y = selection.selected_objs[0]->y;
+        ai_shortes_path(Pmousemap, io.go_xy);
+    }
+
 }
 
 static int pause_game(void)

-- 
tuxhistory - Educational history game



More information about the Tux4kids-commits mailing list