Rename REAMDE to README.md
[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 import serialization
26
27 # Something acting on the game
28 class Turnable[G: Game]
29 auto_serializable
30
31 # Execute `turn` for this instance.
32 fun do_turn(turn: GameTurn[G]) is abstract
33 end
34
35 # Something acting on the game from time to time
36 class Bucketable[G: Game]
37 super Turnable[G]
38 auto_serializable
39
40 private var act_at: nullable Int = null
41
42 # Cancel the previously registered acting turn
43 #
44 # Once called, `self.do_turn` will not be invoked until `GameTurn::act_next`
45 # or `GameTurn::act_in` are called again.
46 fun cancel_act do act_at = null
47 end
48
49 # Optimized organization of `Bucketable` instances
50 class Buckets[G: Game]
51 super Turnable[G]
52 auto_serializable
53
54 # Bucket type used in this implementation.
55 type BUCKET: HashSet[Bucketable[G]]
56
57 private var next_bucket: nullable BUCKET = null
58 private var current_bucket_key: Int = -1
59
60 # Number of `buckets`, default at 100
61 #
62 # Must be set prior to using any other methods of this class.
63 var n_buckets = 100
64
65 private var buckets: Array[BUCKET] =
66 [for b in n_buckets.times do new HashSet[Bucketable[G]]] is lazy
67
68 # Add the Bucketable event `e` at `at_tick`.
69 fun add_at(e: Bucketable[G], at_tick: Int)
70 do
71 var at_key = key_for_tick(at_tick)
72
73 if at_key == current_bucket_key then
74 next_bucket.as(not null).add(e)
75 else
76 buckets[at_key].add(e)
77 end
78
79 e.act_at = at_tick
80 end
81
82 private fun key_for_tick(at_tick: Int): Int
83 do
84 return at_tick % buckets.length
85 end
86
87 redef fun do_turn(turn: GameTurn[G])
88 do
89 current_bucket_key = key_for_tick(turn.tick)
90 var current_bucket = buckets[current_bucket_key]
91
92 var next_bucket = new HashSet[Bucketable[G]]
93 buckets[current_bucket_key] = next_bucket
94 self.next_bucket = next_bucket
95
96 for e in current_bucket do
97 var act_at = e.act_at
98 if act_at != null then
99 if turn.tick == act_at then
100 e.do_turn(turn)
101 else if act_at > turn.tick and
102 key_for_tick(act_at) == current_bucket_key
103 then
104 next_bucket.add(e)
105 end
106 end
107 end
108 end
109 end
110
111 # Game related event
112 interface GameEvent
113
114 # Apply `self` to `game` logic.
115 fun apply( game : ThinGame ) is abstract
116 end
117
118 # Event raised at the first turn
119 class FirstTurnEvent
120 super GameEvent
121 auto_serializable
122 end
123
124 # Game logic on the client
125 class ThinGame
126 auto_serializable
127
128 # Game tick when `self` should act.
129 #
130 # Default is 0.
131 var tick: Int = 0 is protected writable
132 end
133
134 # Game turn on the client
135 class ThinGameTurn[G: ThinGame]
136 auto_serializable
137
138 # Game tick when `self` should act.
139 var tick: Int is protected writable
140
141 # Game events occurred for `self`.
142 var events = new Array[GameEvent] is protected writable
143 end
144
145 # Game turn on the full logic
146 class GameTurn[G: Game]
147 super ThinGameTurn[G]
148 auto_serializable
149
150 # Game that `self` belongs to.
151 var game: G
152
153 # Create a new game turn for `game`.
154 init (game: G) is old_style_init do
155 super(game.tick)
156 self.game = game
157 end
158
159 # Insert the Bucketable event `e` to be executed at next tick.
160 fun act_next(e: Bucketable[G]) do game.buckets.add_at(e, tick + 1)
161
162 # Insert the Bucketable event `e` to be executed at tick `t`.
163 fun act_in(e: Bucketable[G], t: Int) do game.buckets.add_at(e, tick + t)
164
165 # Add and `apply` a game `event`.
166 fun add_event( event : GameEvent )
167 do
168 event.apply( game )
169 events.add( event )
170 end
171 end
172
173 # Full game logic
174 class Game
175 super ThinGame
176 auto_serializable
177
178 # Game type used in this implementation.
179 type G: Game
180
181 # Bucket list in this game.
182 var buckets: Buckets[G] = new Buckets[G]
183
184 # Last turn executed in this game
185 # Can be used to consult the latest events (by the display for example),
186 # but cannot be used to add new Events.
187 var last_turn: nullable ThinGameTurn[G] = null
188
189 # Execute and return a new GameTurn.
190 #
191 # This method calls `do_pre_turn` before executing the GameTurn
192 # and `do_post_turn` after.
193 fun do_turn: GameTurn[G]
194 do
195 var turn = new GameTurn[G](self)
196
197 do_pre_turn(turn)
198 buckets.do_turn(turn)
199 do_post_turn(turn)
200
201 last_turn = turn
202
203 tick += 1
204
205 return turn
206 end
207
208 # Execute something before executing the current GameTurn.
209 #
210 # Should be redefined by clients to add a pre-turn behavior.
211 # See `Game::do_turn`.
212 fun do_pre_turn(turn: GameTurn[G]) do end
213
214 # Execute something after executing the current GameTurn.
215 #
216 # Should be redefined by clients to add a post-turn behavior.
217 # See `Game::do_turn`.
218 fun do_post_turn(turn: GameTurn[G]) do end
219 end