1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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.
21 # Allows for fast support of a large number of entities with rare actions,
22 # such as a forest with many individual trees.
25 # Something acting on the game
26 class Turnable[G
: Game]
27 fun do_turn
(turn
: GameTurn[G
]) is abstract
30 # Something acting on the game from time to time
31 class Bucketable[G
: Game]
33 private var act_at
: nullable Int = null
35 # Cancel the previously registered acting turn
37 # Once called, `self.do_turn` will not be invoked until `GameTurn::act_next`
38 # or `GameTurn::act_in` are called again.
39 fun cancel_act
do act_at
= null
42 # Optiomized organization of `Bucketable` instances
43 class Buckets[G
: Game]
45 type BUCKET: HashSet[Bucketable[G
]]
47 private var buckets
: Array[BUCKET] is noinit
49 private var next_bucket
: nullable BUCKET = null
50 private var current_bucket_key
: Int = -1
55 buckets
= new Array[BUCKET].with_capacity
(n_buckets
)
57 for b
in [0 .. n_buckets
[do
58 buckets
[b
] = new HashSet[Bucketable[G
]]
62 fun add_at
(e
: Bucketable[G
], at_tick
: Int)
64 var at_key
= key_for_tick
(at_tick
)
66 if at_key
== current_bucket_key
then
67 next_bucket
.as(not null).add
(e
)
69 buckets
[at_key
].add
(e
)
75 private fun key_for_tick
(at_tick
: Int): Int
77 return at_tick
% buckets
.length
80 redef fun do_turn
(turn
: GameTurn[G
])
82 current_bucket_key
= key_for_tick
(turn
.tick
)
83 var current_bucket
= buckets
[current_bucket_key
]
85 var next_bucket
= new HashSet[Bucketable[G
]]
87 for e
in current_bucket
do
89 if act_at
!= null then
90 if turn
.tick
== act_at
then
92 else if act_at
> turn
.tick
and
93 key_for_tick
(act_at
) == current_bucket_key
100 self.next_bucket
= next_bucket
101 buckets
[current_bucket_key
] = next_bucket
107 fun apply
( game
: ThinGame ) is abstract
110 # Event raised at the first turn
115 # Game logic on the client
117 var tick
: Int = 0 is protected writable
120 # Game turn on the client
121 class ThinGameTurn[G
: ThinGame]
122 var tick
: Int = 0 is protected writable
124 var events
: List[GameEvent] = new List[GameEvent] is protected writable
126 init (t
: Int) do tick
= t
129 # Game turn on the full logic
130 class GameTurn[G
: Game]
131 super ThinGameTurn[G
]
140 fun act_next
(e
: Bucketable[G
])
142 game
.buckets
.add_at
(e
, tick
+ 1)
145 fun act_in
(e
: Bucketable[G
], t
: Int)
147 game
.buckets
.add_at
(e
, tick
+ t
)
150 fun add_event
( event
: GameEvent )
162 var buckets
: Buckets[G
] = new Buckets[G
]
164 # Last turn executed in this game
165 # Can be used to consult the latest events (by the display for example),
166 # but cannot be used to add new Events.
167 var last_turn
: nullable ThinGameTurn[G
] = null
171 fun do_turn
: GameTurn[G
]
173 var turn
= new GameTurn[G
](self)
176 buckets
.do_turn
(turn
)
186 fun do_pre_turn
(turn
: GameTurn[G
]) do end
187 fun do_post_turn
(turn
: GameTurn[G
]) do end