[gnome-nibbles] Be more careful when moving head/tail pointers



commit 638df4d3e773cf02c43421192863bb1ba395c508
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sun Jul 20 11:15:55 2014 -0500

    Be more careful when moving head/tail pointers
    
    If we are at the edge of the board, we should not assume we intend to
    move our head/tail to the other edge of the board unless we are really
    moving in that direction.
    
    Hopefully fixes the invisible positions of death that get left behind
    sometimes when we go off the edge of the map or use teleporters.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=654072

 src/worm.c |   55 +++++++++++++++++++++++++++----------------------------
 src/worm.h |    4 ++--
 2 files changed, 29 insertions(+), 30 deletions(-)
---
diff --git a/src/worm.c b/src/worm.c
index 80bb1a6..c9f1a37 100644
--- a/src/worm.c
+++ b/src/worm.c
@@ -375,29 +375,28 @@ gnibbles_worm_move_head_pointer (GnibblesWorm *worm)
   switch (worm->direction) {
     case WORMRIGHT:
       worm->xhead++;
+      if (worm->xhead >= BOARDWIDTH)
+        worm->xhead = 0;
       break;
     case WORMDOWN:
       worm->yhead++;
+      if (worm->yhead >= BOARDHEIGHT)
+        worm->yhead = 0;
       break;
     case WORMLEFT:
       worm->xhead--;
+      if (worm->xhead <= 0)
+        worm->xhead = BOARDWIDTH - 1;
       break;
     case WORMUP:
       worm->yhead--;
+      if (worm->yhead <= 0)
+        worm->yhead = BOARDHEIGHT - 1;
       break;
     default:
-      break;
+      g_assert_not_reached ();
   }
 
-  if (worm->xhead <= 0)
-    worm->xhead = BOARDWIDTH - 1;
-  if (worm->yhead <= 0)
-    worm->yhead = BOARDHEIGHT - 1;
-  if (worm->xhead >= BOARDWIDTH)
-    worm->xhead = 0;
-  if (worm->yhead >= BOARDHEIGHT)
-    worm->yhead = 0;
-
   gnibbles_worm_handle_bonus (worm);
   gnibbles_worm_add_actor (worm);
 }
@@ -411,29 +410,28 @@ gnibbles_worm_move_tail_pointer (GnibblesWorm *worm)
   switch (tail_dir) {
     case WORMRIGHT:
       worm->xtail++;
+      if (worm->xtail >= BOARDWIDTH)
+        worm->xtail = 0;
       break;
     case WORMDOWN:
       worm->ytail++;
+      if (worm->ytail >= BOARDHEIGHT)
+        worm->ytail = 0;
       break;
     case WORMLEFT:
       worm->xtail--;
+      if (worm->xtail <= 0)
+        worm->xtail = BOARDWIDTH - 1;
       break;
     case WORMUP:
       worm->ytail--;
+      if (worm->ytail <= 0)
+        worm->ytail = BOARDHEIGHT - 1;
       break;
     default:
       break;
   }
 
-  if (worm->xtail <= 0)
-    worm->xtail = BOARDWIDTH - 1;
-  if (worm->ytail <= 0)
-    worm->ytail = BOARDHEIGHT - 1;
-  if (worm->xtail >= BOARDWIDTH)
-    worm->xtail = 0;
-  if (worm->ytail >= BOARDHEIGHT)
-    worm->ytail = 0;
-
   if (board->walls[worm->xtail][worm->ytail] == WARPLETTER) {
     gnibbles_warpmanager_worm_change_tail_pos (warpmanager, worm);
     tail_dir = gnibbles_worm_get_tail_direction (worm);
@@ -754,26 +752,27 @@ gnibbles_worm_position_move_head (GnibblesWorm * worm, gint *x, gint *y)
   switch (worm->direction) {
     case WORMUP:
       *y = worm->yhead - 1;
+      if (*y < 0)
+        *y = BOARDHEIGHT - 1;
       break;
     case WORMDOWN:
       *y = worm->yhead + 1;
+      if (*y == BOARDHEIGHT)
+        *y = 0;
       break;
     case WORMLEFT:
       *x = worm->xhead - 1;
+      if (*x < 0)
+        *x = BOARDWIDTH - 1;
       break;
     case WORMRIGHT:
       *x = worm->xhead + 1;
+      if (*x == BOARDWIDTH)
+        *x = 0;
       break;
+    default:
+      g_assert_not_reached ();
   }
-
-  if (*x == BOARDWIDTH)
-    *x = 0;
-  if (*x < 0)
-    *x = BOARDWIDTH - 1;
-  if (*y == BOARDHEIGHT)
-    *y = 0;
-  if (*y < 0)
-    *y = BOARDHEIGHT - 1;
 }
 
 gboolean
diff --git a/src/worm.h b/src/worm.h
index c1fccde..52d425a 100644
--- a/src/worm.h
+++ b/src/worm.h
@@ -42,8 +42,8 @@ typedef struct {
   ClutterActor *actors;
   GList *list;
   gint xstart, ystart;
-  guint xhead, yhead;
-  guint xtail, ytail;
+  gint xhead, yhead;
+  gint xtail, ytail;
   gint direction, direction_start;
   gint length;
   gint lives;


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