[gnome-games/gnibbles-clutter-rebased: 18/129] First step to connect AI with worm's movement
- From: Jason Clinton <jclinton src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnome-games/gnibbles-clutter-rebased: 18/129] First step to connect AI with worm's movement
- Date: Mon, 12 Oct 2009 21:27:44 +0000 (UTC)
commit eabfe1b6e97302f66deb68ef79196e009d37d49f
Author: Guillaume Beland <guillaume beland gmail com>
Date: Thu Jun 18 23:06:59 2009 -0400
First step to connect AI with worm's movement
gnibbles/main.c | 107 +++++++++++++++++++++++++++++----------
gnibbles/worm-clutter.c | 127 ++++++-----------------------------------------
2 files changed, 95 insertions(+), 139 deletions(-)
---
diff --git a/gnibbles/main.c b/gnibbles/main.c
index 1a50e7e..ee2a6fc 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -1275,50 +1275,105 @@ move_worm_cb (ClutterTimeline *timeline, gint msecs, gpointer data)
guint size;
gboolean direction;
GValue val = {0,};
- gint i;
+ gint i, olddir;
g_value_init (&val, G_TYPE_BOOLEAN);
for (i = 0; i < 4 /*numworms*/; i++) {
+
ClutterActor *first = g_list_first (cworms[i]->list)->data;
ClutterActor *last = g_list_last (cworms[i]->list)->data;
- g_object_get_property (G_OBJECT (first), "repeat-x", &val);
- direction = g_value_get_boolean (&val);
+ olddir = cworms[i]->direction;
+ // this determine the direction the worm will take
+ gnibbles_cworm_ai_move (cworms[i]);
+
+ // Add an actor when we change direction
+ if (olddir != cworms[i]->direction)
+ gnibbles_cworm_add_straight_actor (cworms[i]);
if (first == last) {
clutter_actor_get_position (CLUTTER_ACTOR (first), &x, &y);
- if (direction)
- clutter_actor_set_position (CLUTTER_ACTOR (first), x + properties->tilesize, y);
- else
- clutter_actor_set_position (CLUTTER_ACTOR (first), x, y + properties->tilesize);
- } else {
+ // If the worm is constitued of one actor, simply move it
+ switch (cworms[i]->direction) {
+ case WORMRIGHT:
+ clutter_actor_set_position (CLUTTER_ACTOR (first), x + properties->tilesize, y);
+ cworms[i]->xhead += properties->tilesize;
+ break;
+ case WORMDOWN:
+ clutter_actor_set_position (CLUTTER_ACTOR (first), x, y + properties->tilesize);
+ cworms[i]->yhead += properties->tilesize;
+ break;
+ case WORMLEFT:
+ clutter_actor_set_position (CLUTTER_ACTOR (first), x - properties->tilesize, y);
+ cworms[i]->xhead -= properties->tilesize;
+ break;
+ case WORMUP:
+ clutter_actor_set_position (CLUTTER_ACTOR (first), x, y - properties->tilesize);
+ cworms[i]->yhead -= properties->tilesize;
+ break;
+ default:
+ break;
+ }
+ } else {
+ g_object_get_property (G_OBJECT (first), "repeat-x", &val);
+ direction = g_value_get_boolean (&val);
clutter_actor_get_size (CLUTTER_ACTOR (first), &w, &h);
size = w < h ? h : w;
-
+ // set the size of the first actor
+ /*
+ switch (cworms[i]->direction) {
+ case WORMRIGHT:
+ clutter_actor_set_size (CLUTTER_ACTOR (first), size + properties->tilesize, properties->tilesize);
+ break;
+ case WORMDOWN:
+ clutter_actor_set_size (CLUTTER_ACTOR (first), properties->tilesize , size + properties->tilesize);
+ break;
+ case WORMLEFT:
+ clutter_actor_set_size (CLUTTER_ACTOR (first), size + properties->tilesize, properties->tilesize);
+ break;
+ case WORMUP:
+ clutter_actor_set_size (CLUTTER_ACTOR (first), properties->tilesize, size + properties->tilesize);
+ break;
+ default:
+ break;
+ }
+ */
if (direction)
- clutter_actor_set_size (first, properties->tilesize + size, properties->tilesize);
- else
- clutter_actor_set_size (first, properties->tilesize, properties->tilesize + size);
+ clutter_actor_set_size (CLUTTER_ACTOR (first), size + properties->tilesize, properties->tilesize);
+ return
+ clutter_actor_set_size (CLUTTER_ACTOR (first), properties->tilesize, size + properties->tilesize);
- g_object_get_property (G_OBJECT (last), "repeat-x", &val);
- direction = g_value_get_boolean (&val);
clutter_actor_get_size (CLUTTER_ACTOR (last), &w, &h);
clutter_actor_get_position (CLUTTER_ACTOR (last), &x, &y);
size = w < h ? h : w;
size = size / (properties->tilesize + 1);
- //TODO: Set move UP/DOWn RIGHT/LEFT
- if (direction) {
- clutter_actor_set_size (last, properties->tilesize * size, properties->tilesize);
- clutter_actor_set_position (last, x + properties->tilesize, y);
- cworms[i]->xhead += properties->tilesize;
- } else {
- clutter_actor_set_size (last, properties->tilesize, properties->tilesize * size);
- clutter_actor_set_position (last, x, y + properties->tilesize);
- cworms[i]->yhead += properties->tilesize;
+ switch (cworms[i]->direction) {
+ case WORMRIGHT:
+ clutter_actor_set_size (CLUTTER_ACTOR (last), properties->tilesize * size, properties->tilesize);
+ clutter_actor_set_position (CLUTTER_ACTOR (last), x + properties->tilesize, y);
+ cworms[i]->xhead += properties->tilesize;
+ break;
+ case WORMDOWN:
+ clutter_actor_set_size (CLUTTER_ACTOR (last), properties->tilesize, properties->tilesize * size);
+ clutter_actor_set_position (CLUTTER_ACTOR (last), x, y + properties->tilesize);
+ cworms[i]->yhead += properties->tilesize;
+ break;
+ case WORMLEFT:
+ clutter_actor_set_size (CLUTTER_ACTOR (last), properties->tilesize * size, properties->tilesize);
+ clutter_actor_set_position (CLUTTER_ACTOR (last), x - properties->tilesize, y);
+ cworms[i]->xhead -= properties->tilesize;
+ break;
+ case WORMUP:
+ clutter_actor_set_size (CLUTTER_ACTOR (last), properties->tilesize, properties->tilesize * size);
+ clutter_actor_set_position (CLUTTER_ACTOR (last), x, y - properties->tilesize);
+ cworms[i]->yhead -= properties->tilesize;
+ break;
+ default:
+ break;
}
-
+
if (size <= 0)
gnibbles_cworm_remove_actor (cworms[i]);
}
@@ -1415,10 +1470,8 @@ main (int argc, char **argv)
clutter_actor_raise_top (cworms[i]->actors);
}
- ClutterTimeline *timeline = clutter_timeline_new (150);
+ ClutterTimeline *timeline = clutter_timeline_new (115);
clutter_timeline_set_loop (timeline, TRUE);
- cworms[2]->direction = WORMDOWN;
- gnibbles_cworm_add_straight_actor (cworms[2]);
g_signal_connect (timeline, "new-frame", G_CALLBACK (move_worm_cb), NULL);
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index d6794dc..4a9cdb0 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -278,86 +278,6 @@ gnibbles_cworm_move (ClutterTimeline *timeline, gint frame_num, gpointer data)
}
}
-void
-gnibbles_cworm_draw_head (GnibblesCWorm * worm)
-{
- //worm->keypress = 0;
-
- switch (worm->direction) {
- case WORMUP:
- //worm->xoff[worm->start] = 0;
- //worm->yoff[worm->start] = 1;
- worm->yhead--;
- break;
- case WORMDOWN:
- //worm->xoff[worm->start] = 0;
- //worm->yoff[worm->start] = -1;
- worm->yhead++;
- break;
- case WORMLEFT:
- //worm->xoff[worm->start] = 1;
- //worm->yoff[worm->start] = 0;
- worm->xhead--;
- break;
- case WORMRIGHT:
- //worm->xoff[worm->start] = -1;
- //worm->yoff[worm->start] = 0;
- worm->xhead++;
- break;
- }
-
- if (worm->xhead == BOARDWIDTH) {
- worm->xhead = 0;
- //worm->xoff[worm->start] += BOARDWIDTH;
- }
- if (worm->xhead < 0) {
- worm->xhead = BOARDWIDTH - 1;
- //worm->xoff[worm->start] -= BOARDWIDTH;
- }
- if (worm->yhead == BOARDHEIGHT) {
- worm->yhead = 0;
- //worm->yoff[worm->start] += BOARDHEIGHT;
- }
- if (worm->yhead < 0) {
- worm->yhead = BOARDHEIGHT - 1;
- //worm->yoff[worm->start] -= BOARDHEIGHT;
- }
-
- if ((level->walls[worm->xhead][worm->yhead] != EMPTYCHAR) &&
- (level->walls[worm->xhead][worm->yhead] != WARPLETTER)) {
- //gnibbles_cworm_grok_bonus (worm);
- if ((level->walls[worm->xhead][worm->yhead] == BONUSREGULAR + 'A') &&
- !gnibbles_boni_fake (boni, worm->xhead, worm->yhead)) {
- gnibbles_boni_remove_bonus_final (boni, worm->xhead, worm->yhead);
-
- if (boni->numleft != 0)
- gnibbles_add_bonus (1);
-
- } else
- gnibbles_boni_remove_bonus_final (boni, worm->xhead, worm->yhead);
- }
-
- if (level->walls[worm->xhead][worm->yhead] == WARPLETTER) {
- //gnibbles_warpmanager_worm_change_pos (warpmanager, worm);
- //games_sound_play ("teleport");
- }
-
- worm->start++;
-
- if (worm->start == CAPACITY)
- worm->start = 0;
-
- level->walls[worm->xhead][worm->yhead] = WORMCHAR + worm->number;
-/*
- gnibbles_draw_pixmap (properties->wormprops[worm->number]->color,
- worm->xhead, worm->yhead);
-
- if (key_queue[worm->number] && !g_queue_is_empty (key_queue[worm->number])) {
- gnibbles_cworm_dequeue_keypress (worm);
- }
-*/
-}
-
gint
gnibbles_cworm_can_move_to (GnibblesCWorm * worm, gint x, gint y)
{
@@ -433,33 +353,6 @@ gnibbles_cworm_is_move_safe (GnibblesCWorm * worm)
return TRUE;
}
-void
-gnibbles_cworm_move_tail (GnibblesCWorm * worm)
-{
- if (worm->change <= 0) {
- //gnibbles_draw_pixmap (BLANKPIXMAP, worm->xtail, worm->ytail);
- //worm->xtail -= worm->xoff[worm->stop];
- //worm->ytail -= worm->yoff[worm->stop];
- worm->stop++;
- if (worm->stop == CAPACITY)
- worm->stop = 0;
- if (worm->change) {
- //gnibbles_draw_pixmap (BLANKPIXMAP, worm->xtail, worm->ytail);
- level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
- //worm->xtail -= worm->xoff[worm->stop];
- //worm->ytail -= worm->yoff[worm->stop];
- worm->stop++;
- if (worm->stop == CAPACITY)
- worm->stop = 0;
- worm->change++;
- worm->length--;
- }
- } else {
- worm->change--;
- worm->length++;
- }
-}
-
/* Check whether the worm will be trapped in a dead end. A location
within the dead end and the length of the worm is given. This
prevents worms getting trapped in a spiral, or in a corner sharper
@@ -745,20 +638,28 @@ gnibbles_cworm_ai_move (GnibblesCWorm * worm)
that the dead end will disappear (e.g. if it's made from the tail
of some worm, as often happens). */
olddir = worm->direction;
- bestyet = CAPACITY*2;
+ bestyet = CAPACITY * 2;
bestdir = -1;
+
for (dir = 1; dir <= 4; dir++) {
worm->direction = dir;
- if (dir == opposite) continue;
+
+ if (dir == opposite)
+ continue;
thislen = 0;
+
if(!gnibbles_cworm_test_move_head (worm))
thislen += CAPACITY;
+
if(gnibbles_cworm_ai_tooclose (worm))
thislen += 4;
+
if(!gnibbles_cworm_is_move_safe (worm))
thislen += 4;
+
thislen += gnibbles_cworm_ai_deadend_after
(worm->xhead, worm->yhead, dir, worm->length + worm->change);
+
if (dir == olddir && !thislen)
thislen -= 100;
/* If the favoured direction isn't appropriate, then choose
@@ -767,20 +668,22 @@ gnibbles_cworm_ai_move (GnibblesCWorm * worm)
right corner of the board. */
if (thislen <= 0)
thislen -= random() % 100;
- if (thislen < bestyet)
- {
+ if (thislen < bestyet) {
bestyet = thislen;
bestdir = dir;
}
}
+
if (bestdir == -1) /* this should never happen, but just in case... */
bestdir = olddir;
+
worm->direction = bestdir;
/* Make sure we are at least avoiding walls.
* Mostly other snakes should avoid our head. */
for (dir = 1; dir <= 4; dir++) {
- if (dir == opposite) continue;
+ if (dir == opposite)
+ continue;
if (!gnibbles_cworm_test_move_head (worm)) {
worm->direction = dir;
} else {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]