[gnome-games/gnibbles-clutter] Trivial code refactoring & small fixes



commit 2411db6956aa0f4f9e24fddbae371a2b911f3da5
Author: Guillaume Beland <guillaume beland gmail com>
Date:   Tue Jul 21 15:02:44 2009 -0400

    Trivial code refactoring & small fixes

 gnibbles/gnibbles.c     |   51 +++++++++++++++++++++++------------------------
 gnibbles/worm-clutter.c |   19 ++++++++++-------
 2 files changed, 36 insertions(+), 34 deletions(-)
---
diff --git a/gnibbles/gnibbles.c b/gnibbles/gnibbles.c
index be481bd..282a411 100644
--- a/gnibbles/gnibbles.c
+++ b/gnibbles/gnibbles.c
@@ -221,16 +221,18 @@ gnibbles_move_worms (void)
 {
   gint i, j, olddir, length, nbr_actor;
   gint status = 1, nlives = 1;
-  gint *dead;
+  gboolean *dead;
 
-  dead = g_new (gint, properties->numworms);
+  dead = g_new (gboolean, properties->numworms);
 
-  for (i = 1; i < properties->numworms; i++) {
+  for (i = 0; i < properties->numworms; i++) {
     olddir = worms[i]->direction;
-    gnibbles_worm_ai_move (worms[i]);
+    if (!worms[i]->human) {
+      gnibbles_worm_ai_move (worms[i]);
 
-    if (olddir != worms[i]->direction)
-      gnibbles_worm_add_actor (worms[i]);
+      if (olddir != worms[i]->direction)
+        gnibbles_worm_add_actor (worms[i]);
+    }
   }
 
   if (boni->missed > MAXMISSED)
@@ -259,13 +261,7 @@ gnibbles_move_worms (void)
     status &= !dead[i];
   }
 
-  for (i = 0; i < properties->numworms; i++) {
-
-    nbr_actor = g_list_length (worms[i]->list);
-    length = gnibbles_worm_get_length (worms[i]);
-    //printf ("\nWorm ID: %d, Actors: %d, Length: %d,  xhead: %d, yhead:%d",
-    //        i, nbr_actor, length, worms[i]->xhead, worms[i]->yhead);
-
+  for (i = 0; i < properties->numworms; i++) { 
     if (worms[i]->xhead >= BOARDWIDTH) {
       worms[i]->xhead = 0;
       gnibbles_worm_add_actor(worms[i]);
@@ -280,21 +276,25 @@ gnibbles_move_worms (void)
       gnibbles_worm_add_actor (worms[i]);
     }
 
-    //if there's only one actor in the list, just move the actor
-    if (nbr_actor == 1 && !dead[i] && worms[i]->lives > 0) {
-      gnibbles_worm_move_straight_worm (worms[i]);
-    } else if (nbr_actor >= 2 && !dead[i] && worms[i]->lives > 0) {
+  }
+  
+  for (i = 0; i < properties->numworms; i++) {
+    if (g_list_length (worms[i]->list) > 1 && !dead[i] && worms[i]->lives > 0)
       gnibbles_worm_move_tail (worms[i]);
-      if (g_list_length (worms[i]->list) == 1)
-        gnibbles_worm_move_straight_worm (worms[i]);
-      else 
-        gnibbles_worm_move_head (worms[i]);
-    } else if (dead[i]) {
-      //worm's dead, do something clever about it...
-    }
+  }
+  
+  for (i = 0; i < properties->numworms; i++) {
+    if (g_list_length (worms[i]->list) > 1 && !dead[i] && worms[i]->lives > 0)
+      gnibbles_worm_move_head (worms[i]);
   }
 
   for (i = 0; i < properties->numworms; i++) {
+    if (g_list_length (worms[i]->list) == 1 && !dead[i] && worms[i]->lives > 0)
+      gnibbles_worm_move_straight_worm (worms[i]);
+  }
+
+
+  for (i = 0; i < properties->numworms; i++) {
     for (j = 0; j < properties->numworms; j++) {
       if (i != j && worms[i]->xhead == worms[j]->xhead
 	        && worms[i]->yhead == worms[j]->yhead
@@ -310,7 +310,6 @@ gnibbles_move_worms (void)
 	      worms[i]->score *= .7;
       if (!gnibbles_worm_lose_life (worms[i])) {
         /* One of the worms lost one life, but the round continues. */
-        // TODO: reset worm state
         gnibbles_worm_reset (worms[i]);
 	      games_sound_play ("crash");
 	    }
@@ -327,6 +326,7 @@ gnibbles_move_worms (void)
     if (worms[i]->lives > 0)
       nlives += 1;
   }
+
   if (nlives == 1 && (properties->ai + properties->human > 1)) {
     /* There is one player left, the other AI players are dead, and that player has won! */
     return (VICTORY);
@@ -334,7 +334,6 @@ gnibbles_move_worms (void)
     /* There was only one worm, and it died. */
     return (GAMEOVER);
   }
-
    /* Noone died, so the round can continue. */
 
   g_free (dead);
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index cb2f503..456608a 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -74,6 +74,7 @@ gnibbles_worm_queue_keypress (GnibblesWorm * worm, guint dir)
   /* Ignore duplicates in normal movement mode. This resolves the
    * key repeat issue. We ignore this in relative mode because then
    * you do want two keys that are the same in quick succession. */
+  printf ("\nfail here 1");
   if ((!properties->wormprops[worm->number]->relmove) &&
       (!g_queue_is_empty (key_queue[n])) &&
       (dir == ((key_queue_entry *) g_queue_peek_tail (key_queue[n]))->dir))
@@ -130,7 +131,7 @@ gnibbles_worm_queue_empty (GnibblesWorm * worm)
 
   if (!key_queue[n])
     return;
-
+  printf ("\nfail here 2");
   while (!g_queue_is_empty (key_queue[n])) {
     entry = g_queue_pop_head (key_queue[n]);
     g_free (entry);
@@ -325,6 +326,7 @@ gnibbles_worm_new (guint number, guint t_xhead,
   worm->list = NULL;
   worm->number = number;
   worm->lives = SLIVES;
+  worm->human = FALSE;
 
   worm->xhead = t_xhead;
   worm->xstart = t_xhead;
@@ -469,14 +471,14 @@ gnibbles_worm_reset (GnibblesWorm * worm)
   gint tail_length;
   gint tail_dir;
   gint i,j;
-
   gint nbr_actor = clutter_group_get_n_children (CLUTTER_GROUP (worm->actors));
 
   for (j = 0; j < nbr_actor; j++) {
     tail_dir = gnibbles_worm_get_tail_direction (worm);
     tail_actor = gnibbles_worm_get_tail_actor (worm);
     tail_length = gnibbles_worm_get_actor_length (tail_actor);
-    
+   
+
     switch (tail_dir) {
       case WORMUP:
         for (i = 0; i < tail_length; i++)
@@ -615,7 +617,7 @@ gnibbles_worm_move_straight_worm (GnibblesWorm *worm)
       worm->xhead--;
       level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
       if (worm->xtail == 0)
-        worm->xtail = BOARDWIDTH;
+        worm->xtail = BOARDWIDTH - 1;
       else
         worm->xtail--;
       break;
@@ -626,14 +628,14 @@ gnibbles_worm_move_straight_worm (GnibblesWorm *worm)
       worm->yhead--;
       level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
       if (worm->ytail == 0)
-        worm->ytail = BOARDHEIGHT;
+        worm->ytail = BOARDHEIGHT - 1;
       else
         worm->ytail--;
       break;
     default:
       break;
   }
-
+  printf ("\nfail here 3");
   if (key_queue[worm->number] && !g_queue_is_empty (key_queue[worm->number])) {
     gnibbles_worm_dequeue_keypress (worm);
   }
@@ -711,6 +713,7 @@ gnibbles_worm_move_head (GnibblesWorm *worm)
       break;
   }
 
+  printf ("\nfail here 4");
   if (key_queue[worm->number] && !g_queue_is_empty (key_queue[worm->number])) {
     gnibbles_worm_dequeue_keypress (worm);
   }
@@ -770,7 +773,7 @@ gnibbles_worm_move_tail (GnibblesWorm *worm)
                                 properties->tilesize);
         level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
         if (worm->xtail == 0)
-          worm->xtail = BOARDWIDTH;
+          worm->xtail = BOARDWIDTH - 1;
         else
           worm->xtail--;
         break;
@@ -780,7 +783,7 @@ gnibbles_worm_move_tail (GnibblesWorm *worm)
                                 size);
         level->walls[worm->xtail][worm->ytail] = EMPTYCHAR;
         if (worm->ytail == 0)
-          worm->ytail = BOARDHEIGHT;
+          worm->ytail = BOARDHEIGHT - 1;
         else
           worm->ytail--;
         break;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]