-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent_queue.py
More file actions
60 lines (50 loc) · 1.64 KB
/
Copy pathevent_queue.py
File metadata and controls
60 lines (50 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import heapq
from global_variables import TICKS_PER_TURN
class EventQueue:
"""
The Event Queue that controls who does what, when.
The player entity needs to start in control.
When the player is ready to move, the event queue is filled.
The topmost (smallest value) entity is removed from the Q and executes their turn.
If they have ticks remaining, they are reentered into the Q.
Once the Q is empty, the turn changes, and the Q is refilled.
"""
def __init__(self):
self.queue = []
heapq.heapify(self.queue)
def empty(self):
"""
Check to see if the queue is empty.
"""
if len(self.queue) == 0:
return True
return False
def register_list(self, entities):
"""
Add an entire list of entities to the queue.
"""
for entity in entities:
self.register(entity)
def register(self, entity):
"""
Add an entity to the queue.
"""
criteria_a = -1 * entity.action_points
criteria_b = -1 * entity.propulsion.speed
criteria_c = -1 * entity.age
heapq.heappush(self.queue, (criteria_a, criteria_b, criteria_c, entity.name, entity.uuid))
def release(self, entity):
"""
Remove an entity from the queue.
"""
if entity in self.queue:
self.queue.remove(entity)
heapq.heapify(self.queue)
def fetch(self):
"""
Fetch the next entity's uuid to have a turn.
This removes them from the queue.
"""
if len(self.queue) > 0:
return heapq.heappop(self.queue)[-1]
return None