lib: intro the a_star module (even though it is not yet A*)
[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 fun do_turn(turn: GameTurn[G]) is abstract
28 end
29
30 # Something acting on the game from time to time
31 class Bucketable[G: Game]
32 super Turnable[G]
33 private var act_at: Int = 0
34 end
35
36 # Optiomized organization of `Bucketable` instances
37 class Buckets[G: Game]
38 super Turnable[G]
39 type Bucket: HashSet[Bucketable[G]]
40
41 private var buckets: Array[Bucket]
42
43 private var next_bucket: nullable Bucket = null
44 private var current_bucket_key: Int = -1
45
46 init
47 do
48 var n_buckets = 100
49 buckets = new Array[Bucket].with_capacity(n_buckets)
50
51 for b in [0 .. n_buckets [do
52 buckets[b] = new Bucket
53 end
54 end
55
56 fun add_at(e: Bucketable[G], at_tick: Int)
57 do
58 var at_key = key_for_tick(at_tick)
59
60 if at_key == current_bucket_key then
61 next_bucket.as(not null).add(e)
62 else
63 buckets[at_key].add(e)
64 end
65
66 e.act_at = at_tick
67 end
68
69 private fun key_for_tick(at_tick: Int): Int
70 do
71 return at_tick % buckets.length
72 end
73
74 redef fun do_turn(turn: GameTurn[G])
75 do
76 current_bucket_key = key_for_tick(turn.tick)
77 var current_bucket = buckets[current_bucket_key]
78
79 next_bucket = new Bucket
80
81 for e in current_bucket do
82 if e.act_at == turn.tick then
83 e.do_turn(turn)
84 else if e.act_at > turn.tick and
85 key_for_tick(e.act_at) == current_bucket_key
86 then
87 next_bucket.as(not null).add(e)
88 end
89 end
90
91 buckets[current_bucket_key] = next_bucket.as(not null)
92 end
93 end
94
95 # Game related event
96 class GameEvent
97 end
98
99 # Event raised at the first turn
100 class FirstTurnEvent
101 super GameEvent
102 end
103
104 # Game logic on the client
105 class ThinGame
106 var tick: Int protected writable = 0
107 end
108
109 # Game turn on the client
110 class ThinGameTurn[G: ThinGame]
111 var tick: Int protected writable = 0
112
113 var events: List[GameEvent] protected writable = new List[GameEvent]
114
115 init (t: Int) do tick = t
116 end
117
118 # Game turn on the full logic
119 class GameTurn[G: Game]
120 super ThinGameTurn[G]
121 var game: G
122
123 init (g: G)
124 do
125 super(g.tick)
126 game = g
127 end
128
129 fun act_next(e: Bucketable[G])
130 do
131 game.buckets.add_at(e, tick + 1)
132 end
133
134 fun act_in(e: Bucketable[G], t: Int)
135 do
136 game.buckets.add_at(e, tick + t)
137 end
138 end
139
140 # Full game logic
141 class Game
142 super ThinGame
143 type G: Game
144
145 var buckets: Buckets[G] = new Buckets[G]
146
147 init do end
148
149 fun do_turn: GameTurn[G]
150 do
151 var turn = new GameTurn[G](self)
152
153 do_pre_turn(turn)
154 buckets.do_turn(turn)
155 do_post_turn(turn)
156
157 tick += 1
158
159 return turn
160 end
161
162 fun do_pre_turn(turn: GameTurn[G]) do end
163 fun do_post_turn(turn: GameTurn[G]) do end
164 end