summaryrefslogtreecommitdiff
path: root/subscriber.py
blob: cedee99a387bc1403a19dd0ed5bfcfdcbf79cdd7 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from log import log
from collections import deque
import sys
import mechanics

class DummySubscriber:
    def on_connect_error(self,s):
        log("on conn err"+s)

    def on_sock_open(self):
        log("on sock open")

    def on_sock_closed(self):
        log("on sock closed")

    def on_message_error(self,s):
        log("on msg err "+s)

    def on_ingame(self):
        log("we're ingame :)")

    def on_world_update_pre(self):
        log("updatepre")

    def on_cell_eaten(self,eater_id, eaten_id):
        log("%s ate %s" % (eater_id, eaten_id))

    def on_death(self):
        log("we died :(")

    def on_cell_removed(self,cid):
        log("cell removed")

    def on_cell_info(self,cid, x,y, size, name, color, is_virus, is_agitated):
        log("cell info")

    def on_world_update_post(self):
        log("updatepost")

    def on_leaderboard_names(self,leaderboard):
        #OAR WINDOWS
        if sys.platform != "win32":
            log("leaderboard names")
            log(leaderboard)

    def on_leaderboard_groups(self,angles):
        log("leaderboard groups")

    def on_respawn(self):
        log("respawned")

    def on_own_id(self,cid):
        log("my id is %i" % cid)

    def on_world_rect(self,left,top,right,bottom):
        log("worldrect %i,%i,%i,%i"%(left,top,right,bottom))

    def on_spectate_update(self,pos, scale):
        log("spect update")

    def on_experience_info(self,level, current_xp, next_xp):
        log("exper info")

    def on_clear_cells(self):
        log("clear cells")

    def on_debug_line(self,x,y):
        log("debug line")

class CellHistory:
    def __init__(self):
        self.poslog = deque(maxlen=300)
        self.stale = False

class OtherPlayer:
    def __init__(self, playerid):
        self.playerid = playerid
        self.cells = set()

class EnhancingSubscriber(DummySubscriber):
    def __init__(self):
        self.c = None
        self.history = {}

    def set_client(self,c):
        self.c = c
    
    def on_world_update_post(self):

        # create and purge poslog history, movement and movement_angle
        for cid in self.history:
            self.history[cid].stale = True

        for cid in self.c.world.cells:
            if cid not in self.history:
                self.history[cid] = CellHistory()
            
            self.history[cid].poslog.append(self.c.world.cells[cid].pos.copy())
            self.c.world.cells[cid].poslog = self.history[cid].poslog
            
            self.history[cid].stale = False

        self.history = {k: v for k, v in self.history.items() if v.stale == False}
        

        for cid in self.c.world.cells:
            cell = self.c.world.cells[cid]
            try:
                oldpos = cell.poslog[-3-1]
                cell.movement = (cell.pos - oldpos)/3
                cell.movement_angle = cell.movement.angle()
            except (AttributeError, IndexError):
                pass


        # create OtherPlayer entries
        otherplayers = {}
        for cell in self.c.world.cells.values():
            playerid = None
            if not cell.is_food and not cell.is_ejected_mass and not cell.is_virus:
                playerid = (cell.name, cell.color)
            elif cell.is_virus:
                playerid = "virus"
            elif cell.is_food:
                playerid = "food"
            elif cell.is_ejected_mass:
                playerid = "ejected mass"
            else:
                playerid = "???"

            if playerid not in otherplayers:
                otherplayers[playerid] = OtherPlayer(playerid)

            cell.player = otherplayers[playerid]
            cell.player.cells.add(cell)

        # detect split cells and clean up obsolete parent references
        for cell in self.c.world.cells.values():
            # create attribute if not already there
            try:
                cell.parent = cell.parent
            except:
                cell.parent = None
                cell.calmed_down = True

            # clean up obsolete parent references
            if cell.parent and cell.parent.cid not in self.c.world.cells:
                cell.parent = None

            # find split cells
            is_split = False
            if not cell.is_food and not cell.is_ejected_mass and not cell.is_virus:
                try:
                    if cell.parent == None and cell.movement.len() > 2 * mechanics.speed(cell.size):
                            print("looks like a split!"+str(cell.movement.len() / mechanics.speed(cell.size)))
                            is_split = True
                except AttributeError:
                    pass

                if is_split:
                    history_len = len(cell.poslog)
                    cell.parent = min(cell.player.cells, key=lambda c : (c.poslog[-history_len] - cell.poslog[-history_len]).len() if c != cell and len(c.poslog) >= history_len else float('inf'))
                    try:
                        cell.shoot_vec = cell.parent.movement.copy()
                    except:
                        cell.shoot_vec = None
                    cell.calmed_down = False

            elif cell.is_virus:
                try:
                    if cell.parent == None and cell.movement.len() > 0:
                            print("split virus!")
                            is_split = True
                except AttributeError:
                    pass

                if is_split:
                    cell.parent = min(cell.player.cells, key=lambda c : (c.pos - cell.poslog[0]).len() if c != cell else float('inf'))
                    cell.shoot_vec = None # TODO FIXME: use direction of the last ejected blob feed into the mother virus
                    cell.calmed_down = False
                
            elif cell.is_ejected_mass:
                try:
                    if cell.parent == None and cell.movement.len() > 0:
                            print("ejected mass!")
                            is_split = True
                except AttributeError:
                    pass

                if is_split:
                    history_len = len(cell.poslog)
                    try:
                        cell.parent = min(filter(lambda c : not c.is_ejected_mass and not c.is_food and not c.is_virus and c.color == cell.color, self.c.world.cells.values()), key=lambda c : (c.poslog[-history_len] - cell.poslog[-history_len]).len() if len(c.poslog) >= history_len else float('inf'))
                        try:
                            cell.shoot_vec = cell.parent.movement.copy()
                        except:
                            cell.shoot_vec = None
                        cell.calmed_down = False
                    except ValueError:
                        # if no possible parents are found, min will raise a ValueError. ignore that.
                        pass

            if is_split:
                cell.spawnpoint = cell.pos.copy()
                cell.parentsize_when_spawned = cell.parent.size if cell.parent != None else None
                cell.parentpos_when_spawned = cell.parent.pos.copy() if cell.parent != None else None