lib/bucketed_game: revamp constructor of `Buckets`
[nit.git] / lib / bucketed_game.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Provides basic game logic utilities using buckets to coordinate and
18 # optimize actions on game turn ends. Supports both action at each
19 # end of turn as well as actions on some end of turns.
20 #
21 # Allows for fast support of a large number of entities with rare actions,
22 # such as a forest with many individual trees.
23 module bucketed_game
24
25 # Something acting on the game
26 class Turnable[G: Game]
27
28 # Execute `turn` for this instance.
29 fun do_turn(turn: GameTurn[G]) is abstract
30 end
31
32 # Something acting on the game from time to time
33 class Bucketable[G: Game]
34 super Turnable[G]
35 private var act_at: nullable Int = null
36
37 # Cancel the previously registered acting turn
38 #
39 # Once called, `self.do_turn` will not be invoked until `GameTurn::act_next`
40 # or `GameTurn::act_in` are called again.
41 fun cancel_act do act_at = null
42 end
43
44 # Optimized organization of `Bucketable` instances
45 class Buckets[G: Game]
46 super Turnable[G]
47
48 # Bucket type used in this implementation.
49 type BUCKET: HashSet[Bucketable[G]]
50
51 private var next_bucket: nullable BUCKET = null
52 private var current_bucket_key: Int = -1
53
54 # Number of `buckets`, default at 100
55 #
56 # Must be set prior to using any other methods of this class.
57 var n_buckets = 100
58
59 private var buckets: Array[BUCKET] =
60 [for b in n_buckets.times do new HashSet[Bucketable[G]]] is lazy
61
62 # Add the Bucketable event `e` at `at_tick`.
63 fun add_at(e: Bucketable[G], at_tick: Int)
64 do
65 var at_key = key_for_tick(at_tick)
66
67 if at_key == current_bucket_key then
68 next_bucket.as(not null).add(e)
69 else
70 buckets[at_key].add(e)
71 end
72
73 e.act_at = at_tick
74 end
75
76 private fun key_for_tick(at_tick: Int): Int
77 do
78 return at_tick % buckets.length
79 end
80
81 redef fun do_turn(turn: GameTurn[G])
82 do
83 current_bucket_key = key_for_tick(turn.tick)
84 var current_bucket = buckets[current_bucket_key]
85
86 var next_bucket = new HashSet[Bucketable[G]]
87 buckets[current_bucket_key] = next_bucket
88 self.next_bucket = next_bucket
89
90 for e in current_bucket do
91 var act_at = e.act_at
92 if act_at != null then
93 if turn.tick == act_at then
94 e.do_turn(turn)
95 else if act_at > turn.tick and
96 key_for_tick(act_at) == current_bucket_key
97 then
98 next_bucket.add(e)
99 end
100 end
101 end
102 end
103 end
104
105 # Game related event
106 interface GameEvent
107
108 # Apply `self` to `game` logic.
109 fun apply( game : ThinGame ) is abstract
110 end
111
112 # Event raised at the first turn
113 class FirstTurnEvent
114 super GameEvent
115 end
116
117 # Game logic on the client
118 class ThinGame
119
120 # Game tick when `self` should act.
121 #
122 # Default is 0.
123 var tick: Int = 0 is protected writable
124 end
125
126 # Game turn on the client
127 class ThinGameTurn[G: ThinGame]
128
129 # Game tick when `self` should act.
130 var tick: Int is protected writable
131
132 # List of game events occured for `self`.
133 var events = new List[GameEvent] is protected writable
134 end
135
136 # Game turn on the full logic
137 class GameTurn[G: Game]
138 super ThinGameTurn[G]
139
140 # Game that `self` belongs to.
141 var game: G
142
143 # Create a new game turn for `game`.
144 init (game: G) is old_style_init do
145 super(game.tick)
146 self.game = game
147 end
148
149 # Insert the Bucketable event `e` to be executed at next tick.
150 fun act_next(e: Bucketable[G]) do game.buckets.add_at(e, tick + 1)
151
152 # Insert the Bucketable event `e` to be executed at tick `t`.
153 fun act_in(e: Bucketable[G], t: Int) do game.buckets.add_at(e, tick + t)
154
155 # Add and `apply` a game `event`.
156 fun add_event( event : GameEvent )
157 do
158 event.apply( game )
159 events.add( event )
160 end
161 end
162
163 # Full game logic
164 class Game
165 super ThinGame
166
167 # Game type used in this implementation.
168 type G: Game
169
170 # Bucket list in this game.
171 var buckets: Buckets[G] = new Buckets[G]
172
173 # Last turn executed in this game
174 # Can be used to consult the latest events (by the display for example),
175 # but cannot be used to add new Events.
176 var last_turn: nullable ThinGameTurn[G] = null
177
178 # Execute and return a new GameTurn.
179 #
180 # This method calls `do_pre_turn` before executing the GameTurn
181 # and `do_post_turn` after.
182 fun do_turn: GameTurn[G]
183 do
184 var turn = new GameTurn[G](self)
185
186 do_pre_turn(turn)
187 buckets.do_turn(turn)
188 do_post_turn(turn)
189
190 last_turn = turn
191
192 tick += 1
193
194 return turn
195 end
196
197 # Execute something before executing the current GameTurn.
198 #
199 # Should be redefined by clients to add a pre-turn behavior.
200 # See `Game::do_turn`.
201 fun do_pre_turn(turn: GameTurn[G]) do end
202
203 # Execute something after executing the current GameTurn.
204 #
205 # Should be redefined by clients to add a post-turn behavior.
206 # See `Game::do_turn`.
207 fun do_post_turn(turn: GameTurn[G]) do end
208 end