README: document nit_env.sh
[nit.git] / lib / bucketed_game.nit
index 703ff5c..b5542e4 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Provides basic game logic utilities using buckets to coordinate and
-# optimize actions on game turn ends. Supports both action at each
-# end of turn as well as actions on some end of turns.
+# Game framework with an emphasis on efficient event coordination
 #
-# Allows for fast support of a large number of entities with rare actions,
-# such as a forest with many individual trees.
-module bucketed_game
+# Provides basic game logic entities to manage a game where the logic is executed by turns:
+# `Game`, `GameTurn`, `GameEvent`, `Turnable`.
+# Also offers a bucket system to plan future events at a known number of turns in the future:
+# `Bucketable` and the services `act_next` and `act_in`.
+#
+# Efficiently support large number of entities with rare or sparse actions,
+# such as a forest with many individual trees growing slowly.
+module bucketed_game is serialize
+
+import serialization
 
 # Something acting on the game
-class Turnable[G: Game]
+abstract class Turnable[G: Game]
 
        # Execute `turn` for this instance.
        fun do_turn(turn: GameTurn[G]) is abstract
 end
 
 # Something acting on the game from time to time
-class Bucketable[G: Game]
+abstract class Bucketable[G: Game]
        super Turnable[G]
+
        private var act_at: nullable Int = null
 
        # Cancel the previously registered acting turn
@@ -48,20 +54,16 @@ class Buckets[G: Game]
        # Bucket type used in this implementation.
        type BUCKET: HashSet[Bucketable[G]]
 
-       private var buckets: Array[BUCKET] is noinit
-
        private var next_bucket: nullable BUCKET = null
        private var current_bucket_key: Int = -1
 
-       init
-       do
-               var n_buckets = 100
-               buckets = new Array[BUCKET].with_capacity(n_buckets)
+       # Number of `buckets`, default at 100
+       #
+       # Must be set prior to using any other methods of this class.
+       var n_buckets = 100
 
-               for b in [0 .. n_buckets [do
-                       buckets[b] = new HashSet[Bucketable[G]]
-               end
-       end
+       private var buckets: Array[BUCKET] =
+               [for b in n_buckets.times do new HashSet[Bucketable[G]]] is lazy
 
        # Add the Bucketable event `e` at `at_tick`.
        fun add_at(e: Bucketable[G], at_tick: Int)
@@ -133,8 +135,8 @@ class ThinGameTurn[G: ThinGame]
        # Game tick when `self` should act.
        var tick: Int is protected writable
 
-       # List of game events occured for `self`.
-       var events = new List[GameEvent] is protected writable
+       # Game events occurred for `self`.
+       var events = new Array[GameEvent] is protected writable
 end
 
 # Game turn on the full logic
@@ -179,8 +181,6 @@ class Game
        # but cannot be used to add new Events.
        var last_turn: nullable ThinGameTurn[G] = null
 
-       init do end
-
        # Execute and return a new GameTurn.
        #
        # This method calls `do_pre_turn` before executing the GameTurn