[gnome-nibbles/arnaudb/use-uint8: 14/15] Use uint8 more again.



commit cf9055b5a7d789157422e15d1511d8c16bd18032
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Jul 3 16:21:04 2020 +0200

    Use uint8 more again.

 src/nibbles-game.vala | 13 ++++++----
 src/nibbles-test.vala |  8 +++---
 src/nibbles-view.vala |  4 +--
 src/warp.vala         | 68 +++++++++++++++++++++++++++------------------------
 src/worm.vala         | 11 +++------
 5 files changed, 54 insertions(+), 50 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index ca94e4c..f6fd26a 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -74,7 +74,7 @@ private class NibblesGame : Object
     internal signal void log_score (int score, int level_reached);
     internal signal void animate_end_game ();
     internal signal void level_completed ();
-    internal signal void warp_added (int x, int y);
+    internal signal void warp_added (uint8 x, uint8 y);
     internal signal void bonus_added (Bonus bonus);
     internal signal void bonus_removed (Bonus bonus);
 
@@ -201,6 +201,9 @@ private class NibblesGame : Object
                     case 'X':
                     case 'Y':
                     case 'Z':
+                        if (j == 0 || i == 0)
+                            return false;
+
                         board[j, i] = (int) char_value;
                         warp_manager.add_warp_source (board[j, i], j - 1, i - 1);
 
@@ -418,8 +421,8 @@ private class NibblesGame : Object
                 continue;
 
             Position position = worm.position_move ();
-            int target_x;
-            int target_y;
+            uint8 target_x;
+            uint8 target_y;
             if (warp_manager.get_warp_target (position.x, position.y,
                              /* horizontal */ worm.direction == WormDirection.LEFT || worm.direction == 
WormDirection.RIGHT,
                                               out target_x, out target_y))
@@ -440,8 +443,8 @@ private class NibblesGame : Object
             worm.move_part_1 ();
             if (board[worm.head.x, worm.head.y] == NibblesGame.WARPCHAR)
             {
-                int target_x;
-                int target_y;
+                uint8 target_x;
+                uint8 target_y;
                 if (!warp_manager.get_warp_target (worm.head.x, worm.head.y,
                                   /* horizontal */ worm.direction == WormDirection.LEFT || worm.direction == 
WormDirection.RIGHT,
                                                    out target_x, out target_y))
diff --git a/src/nibbles-test.vala b/src/nibbles-test.vala
index 28910c3..bf558c9 100644
--- a/src/nibbles-test.vala
+++ b/src/nibbles-test.vala
@@ -45,10 +45,10 @@ namespace NibblesTest
 
     private struct WormTest
     {
-        int start_x;
-        int start_y;
-        int final_lives;
-        int final_score;
+        uint8 start_x;
+        uint8 start_y;
+        uint8 final_lives;
+        int   final_score;
     }
 
     private static void test_board (string [] board,
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index a3724e3..ff698a2 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -799,7 +799,7 @@ private class NibblesView : GtkClutter.Embed
     * * Warps drawing
     \*/
 
-    private void warp_added_cb (int x, int y)
+    private void warp_added_cb (uint8 x, uint8 y)
     {
         var actor = new WarpTexture ();
         try
@@ -816,7 +816,7 @@ private class NibblesView : GtkClutter.Embed
         }
 
         actor.set_size (tile_size, tile_size);
-        actor.set_position (x * tile_size, y * tile_size);
+        actor.set_position ((int) x * tile_size, (int) y * tile_size);
 
         level.add_child (actor);
 
diff --git a/src/warp.vala b/src/warp.vala
index eee1eb3..343fb1d 100644
--- a/src/warp.vala
+++ b/src/warp.vala
@@ -25,17 +25,17 @@ private class WarpManager: Object
     {
         private bool init_finished = false;
 
-        public int id       { internal get; protected construct; }
+        public int   id         { internal get; protected construct; }
 
-        public int source_x { internal get; protected construct set; }
-        public int source_y { internal get; protected construct set; }
+        public uint8 source_x   { internal get; protected construct set; }
+        public uint8 source_y   { internal get; protected construct set; }
 
-        public int target_x { private get; protected construct set; }
-        public int target_y { private get; protected construct set; }
+        public uint8 target_x   { private get; protected construct set; }
+        public uint8 target_y   { private get; protected construct set; }
 
-        public bool bidi    { internal get; protected construct set; }
+        public bool  bidi       { internal get; protected construct set; }
 
-        internal Warp.from_source (int id, int source_x, int source_y)
+        internal Warp.from_source (int id, uint8 source_x, uint8 source_y)
         {
             Object (id      : id,
                     source_x: source_x,
@@ -43,7 +43,7 @@ private class WarpManager: Object
                     bidi    : true);    // that is a "maybe for now," until init_finished is set
         }
 
-        internal Warp.from_target (int id, int target_x, int target_y)
+        internal Warp.from_target (int id, uint8 target_x, uint8 target_y)
         {
             Object (id      : id,
                     target_x: target_x,
@@ -51,7 +51,7 @@ private class WarpManager: Object
                     bidi    : false);
         }
 
-        internal void set_source (int x, int y)
+        internal void set_source (uint8 x, uint8 y)
             requires (init_finished == false)
         {
             if (bidi)   // set to "true" when created from source
@@ -67,7 +67,7 @@ private class WarpManager: Object
             init_finished = true;
         }
 
-        internal void set_target (int x, int y)
+        internal void set_target (uint8 x, uint8 y)
             requires (init_finished == false)
             requires (bidi == true)     // set to "true" when created from source
         {
@@ -77,7 +77,7 @@ private class WarpManager: Object
             init_finished = true;
         }
 
-        internal bool get_target (int x, int y, bool horizontal, ref int target_x, ref int target_y)
+        internal bool get_target (uint8 x, uint8 y, bool horizontal, ref uint8 target_x, ref uint8 target_y)
             requires (init_finished == true)
         {
             if ((x != source_x && x != source_x + 1)
@@ -93,6 +93,8 @@ private class WarpManager: Object
             {
                 if (x == source_x)
                     target_x = this.target_x + 2;
+                else if (this.target_x == 0)
+                    assert_not_reached ();
                 else
                     target_x = this.target_x - 1;
                 if (y == source_y)
@@ -108,6 +110,8 @@ private class WarpManager: Object
                     target_x = this.target_x + 1;
                 if (y == source_y)
                     target_y = this.target_y + 2;
+                else if (this.target_y == 0)
+                    assert_not_reached ();
                 else
                     target_y = this.target_y - 1;
             }
@@ -115,25 +119,25 @@ private class WarpManager: Object
         }
     }
 
-    private const int MAX_WARPS = 200;
+    private const uint8 MAX_WARPS = 200;
 
     private Gee.LinkedList<Warp> warps = new Gee.LinkedList<Warp> ();
 
-    internal void add_warp_source (int id, int x, int y)
+    internal void add_warp_source (int id, uint8 x, uint8 y)
     {
-        foreach (var warp in warps)
+        foreach (Warp warp in warps)
         {
-            if (warp.id == id)
+            if (warp.id != id)
+                continue;
+
+            warp.set_source (x, y);
+            if (warp.bidi)
             {
-                warp.set_source (x, y);
-                if (warp.bidi)
-                {
-                    Warp bidi_warp = new Warp.from_source (id, x, y);
-                    bidi_warp.set_source (warp.source_x, warp.source_y);
-                    warps.add (bidi_warp);
-                }
-                return;
+                Warp bidi_warp = new Warp.from_source (id, x, y);
+                bidi_warp.set_source (warp.source_x, warp.source_y);
+                warps.add (bidi_warp);
             }
+            return;
         }
 
         if (warps.size >= MAX_WARPS)
@@ -142,15 +146,15 @@ private class WarpManager: Object
         warps.add (new Warp.from_source (id, x, y));
     }
 
-    internal void add_warp_target (int id, int x, int y)
+    internal void add_warp_target (int id, uint8 x, uint8 y)
     {
-        foreach (var warp in warps)
+        foreach (Warp warp in warps)
         {
-            if (warp.id == id)
-            {
-                warp.set_target (x, y);
-                return;
-            }
+            if (warp.id != id)
+                continue;
+
+            warp.set_target (x, y);
+            return;
         }
 
         if (warps.size >= MAX_WARPS)
@@ -159,12 +163,12 @@ private class WarpManager: Object
         warps.add (new Warp.from_target (id, x, y));
     }
 
-    internal bool get_warp_target (int x, int y, bool horizontal, out int target_x, out int target_y)
+    internal bool get_warp_target (uint8 x, uint8 y, bool horizontal, out uint8 target_x, out uint8 target_y)
     {
         target_x = 0;   // garbage
         target_y = 0;   // garbage
 
-        foreach (var warp in warps)
+        foreach (Warp warp in warps)
             if (warp.get_target (x, y, horizontal, ref target_x, ref target_y))
                 return true;
 
diff --git a/src/worm.vala b/src/worm.vala
index 708d37d..eea1cbc 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -66,8 +66,8 @@ private enum WormDirection
 
 private struct Position
 {
-    int x;
-    int y;
+    uint8 x;
+    uint8 y;
 
     internal void move (WormDirection direction, uint8 width, uint8 height)
     {
@@ -189,14 +189,11 @@ private class Worm : Object
         Object (id: id, width: width, height: height, capacity: capacity);
     }
 
-    internal void set_start (int xhead, int yhead, WormDirection direction)
+    internal void set_start (uint8 x, uint8 y, WormDirection direction)
     {
         list.clear ();
 
-        starting_position = Position () {
-            x = xhead,
-            y = yhead
-        };
+        starting_position = Position () { x = x, y = y };
 
         list.add (starting_position);
 


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