-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.py
170 lines (138 loc) · 4.83 KB
/
world.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from vec import Vec
class Cell(object):
def __init__(self, *args, **kwargs):
self.pos = Vec()
self.update(*args, **kwargs)
def update(self, cid=-1, x=0, y=0, size=0, name='',
color=(1, 0, 1), is_virus=False, is_agitated=False, skin_url=''):
self.cid = cid
self.pos.set(x, y)
self.size = size
self.mass = size ** 2 / 100.0
self.name = getattr(self, 'name', name) or name
self.color2 = color
self.color = tuple(map(lambda rgb: rgb / 255.0, color))
self.is_virus = is_virus
self.is_agitated = is_agitated
self.skin_url = skin_url
@property
def is_food(self):
return self.size < 20 and not self.name
@property
def is_ejected_mass(self):
return self.size in (37, 38) and not self.name
def same_player(self, other):
"""
Compares name and color.
Returns True if both are owned by the same player.
"""
return self.name == other.name \
and self.color == other.color
def __lt__(self, other):
if self.mass != other.mass:
return self.mass < other.mass
return self.cid < other.cid
class World(object):
cell_class = Cell
def __init__(self):
self.cells = {}
self.leaderboard_names = []
self.leaderboard_groups = []
self.top_left = Vec(0, 0)
self.bottom_right = Vec(0, 0)
self.reset()
def reset(self):
"""
Clears the `cells` and leaderboards, and sets all corners to `0,0`.
"""
self.cells.clear()
del self.leaderboard_names[:]
del self.leaderboard_groups[:]
self.top_left.set(0, 0)
self.bottom_right.set(0, 0)
def create_cell(self, cid):
"""
Creates a new cell in the world.
Override to use a custom cell class.
"""
self.cells[cid] = self.cell_class()
@property
def center(self):
return Vec(self.size.x/2,self.size.y/2)
@property
def size(self):
return self.top_left.abs() + self.bottom_right.abs()
def __eq__(self, other):
"""Compares two worlds by comparing their leaderboards."""
for ls, lo in zip(self.leaderboard_names, other.leaderboard_names):
if ls != lo:
return False
for ls, lo in zip(self.leaderboard_groups, other.leaderboard_groups):
if ls != lo:
return False
if self.top_left != other.top_left:
return False
if self.bottom_right != other.bottom_right:
return False
return True
class Player(object):
def __init__(self):
# The world that the player's cells are in.
self.world = World()
# All controlled cell IDs.
self.own_ids = set()
# The center of all controlled cells.
self.center = self.world.center
# Combined size of all controlled cells.
self.total_size = 0
# Combined mass of all controlled cells.
self.total_mass = 0
self.nick = ''
self.scale = 1.0
self.reset()
def reset(self):
"""
Clears `nick` and `own_ids`, sets `center` to `world.center`,
and then calls `cells_changed()`.
"""
self.own_ids.clear()
self.nick = ''
self.center = self.world.center
self.cells_changed()
def cells_changed(self):
"""
Calculates `total_size`, `total_mass`, `scale`, and `center`.
Has to be called when the controlled cells (`own_ids`) change.
"""
self.total_size = sum(cell.size for cell in self.own_cells)
self.total_mass = sum(cell.mass for cell in self.own_cells)
self.scale = pow(min(1.0, 64.0 / self.total_size), 0.4) \
if self.total_size > 0 else 1.0
if self.own_ids:
left = min(cell.pos.x for cell in self.own_cells)
right = max(cell.pos.x for cell in self.own_cells)
top = min(cell.pos.y for cell in self.own_cells)
bottom = max(cell.pos.y for cell in self.own_cells)
self.center = Vec(left + right, top + bottom) / 2
# else: keep old center
@property
def own_cells(self):
cells = self.world.cells
return (cells[cid] for cid in self.own_ids)
@property
def is_alive(self):
return bool(self.own_ids)
@property
def is_spectating(self):
return not self.is_alive
@property
def visible_area(self):
"""
Calculated like in the official client.
Returns (top_left, bottom_right).
"""
# looks like zeach has a nice big screen
half_viewport = Vec(1920, 1080) / 2 / self.scale
top_left = self.world.center - half_viewport
bottom_right = self.world.center + half_viewport
return top_left, bottom_right