~fkmclane/armagetronad/armagetron.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import sys
import time


poll_interval = 0.5

ladderlog = sys.stdin
server = sys.stdout


def add_handler(command, handler):
    if command not in commands:
        commands[command] = []

    commands[command].append(handler)


def remove_handler(command, handler):
    commands[command].remove(handler)


def set_chat_handler(command, handler):
    chat_commands[command] = handler


def remove_chat_handler(command):
    del chat_commands[command]


def send_command(command):
    server.write(command + '\n')


def say(message):
    send_command('SAY ' + message)


def console_message(message):
    send_command('CONSOLE_MESSAGE "' + message + '"')


def center_message(message):
    send_command('CENTER_MESSAGE "' + message + '"')


def send_message(player, message):
    if type(player) == Player:
        send_command('PLAYER_MESSAGE ' + player.name + ' "' + message + '"')
    else:
        send_command('PLAYER_MESSAGE ' + player + ' "' + message + '"')


def pause_round():
    send_command('WAIT_FOR_EXTERNAL_SCRIPT 1')


def continue_round():
    send_command('WAIT_FOR_EXTERNAL_SCRIPT 0')


def set_repository(address):
    send_command('RESOURCE_REPOSITORY_SERVER ' + address)


def set_map(resource):
    send_command('MAP_FILE ' + resource)


def include(config):
    send_command('INCLUDE ' + config)


def rinclude(config):
    send_command('RINCLUDE ' + config)


def reload():
    include('settings.cfg')
    include('server_info.cfg')
    include('settings_custom.cfg')
    send_command('START_NEW_MATCH')


def end_round():
    send_command('WIN_ZONE_MIN_LAST_DEATH 0')
    send_command('WIN_ZONE_MIN_ROUND_TIME 0')


def chat_command(command):
    if command[1] in chat_commands:
        chat_commands[command[1]](command[1:])
    else:
        send_message(command[2], 'Unknown chat command "' + command[1] + '".')


def init(command):
    send_command('LADDERLOG_WRITE_NUM_HUMANS 1')
    send_command('LADDERLOG_WRITE_POSITIONS 1')
    send_command('LADDERLOG_WRITE_INVALID_COMMAND 1')
    send_command('INTERCEPT_UNKNOWN_COMMANDS 1')
    send_command('WAIT_FOR_EXTERNAL_SCRIPT_TIMEOUT 10')


def run():
    while True:
        line = ladderlog.readline()
        if not line:
            time.sleep(poll_interval)
            continue
        if line.startswith('QUIT'):
            break
        command = line.split()
        if command and command[0] in commands:
            for handler in commands[command[0]]:
                handler(command)


# grid stuff

class Team:
    def __init__(self, name):
        self.name = name
        self.score = 0
        self.players = {}
        self.positions = []


class Player:
    def __init__(self, name, screenname, ip):
        self.name = name
        self.screenname = screenname
        self.ip = ip
        self.score = 0
        self.alive = False

    def send_message(self, message):
        send_message(self, message)

    def kill(self):
        send_command('KILL ' + self.name)

    def kick(self, reason=None):
        command = ['KICK', self.name]
        if reason:
            command.append(reason)

        send_command(' '.join(command))

    def ban(self, time=None, reason=None):
        command = ['BAN', self.name]
        if time:
            command.append(time)
        if reason:
            command.append(reason)

        send_command(' '.join(command))

    def ban_ip(self, time, reason=None):
        command = ['BAN_IP', self.ip]
        if time:
            command.append(time)
            if reason:
                command.append(reason)

        send_command(' '.join(command))

    def declare_winner(self):
        send_command('DECLARE_ROUND_WINNER ' + self.name)

    def teleport(self, x, y, xdir, ydir):
        send_command('TELEPORT_PLAYER ' + self.name + ' ' + x + ' ' + y + ' ' + xdir + ' ' + ydir)

    def respawn(self, x, y, xdir, ydir):
        if not self.alive:
            send_command('RESPAWN_PLAYER ' + self.name + ' 1 ' + x + ' ' + y + ' ' + xdir + ' ' + ydir)


class Zone:
    def __init__(self, name, type, x, y, size, growth=0, xdir=0, ydir=0, interactive=None, r=None, g=None, b=None, target_size=None, rubber=None, player=None, owner=None, command=None):
        self.name = name
        self.type = type
        self.x = x
        self.y = y
        self.size = size
        self.growth = growth
        self.xdir = xdir
        self.ydir = ydir

        if interactive:
            self.interactive = interactive

        if r and g and b:
            self.r = r
            self.g = g
            self.b = b

        if target_size:
            self.target_size = target_size

        if type == 'rubber':
            self.rubber = rubber

        if type == 'zombie':
            self.player = player

        if type == 'zombieOwner':
            self.player = player
            self.owner = owner

        if type == 'target' and command is not None:
            self.set_command(command)

    def __del__(self):
        self.change_size(0)

    def change_color(self, r, g, b):
        self.r = r
        self.g = g
        self.b = b
        send_command('SET_ZONE_COLOR ' + self.name + ' ' + r + ' ' + g + ' ' + b)

    def change_expansion(self, growth):
        self.growth = growth
        send_command('SET_ZONE_EXPANSION ' + self.name + ' ' + growth)

    def change_position(self, x, y):
        self.x = x
        self.y = y
        send_command('SET_ZONE_POSITION ' + self.name + ' ' + x + ' ' + y)

    def change_size(self, size, growth=None):
        self.size = size

        command = ['SET_ZONE_RADIUS', self.name, size]
        if growth:
            command.append(growth)

        send_command(' '.join(command))

    def change_speed(self, xdir, ydir):
        self.xdir = xdir
        self.ydir = ydir
        send_command('SET_ZONE_SPEED ' + self.name + ' ' + xdir + ' ' + ydir)

    def set_command(self, command):
        if self.type == 'target':
            send_command('SET_TARGET_COMMAND ' + self.name + ' ' + command)


class Grid:
    def __init__(self):
        self.reset(None)

    def get_team(self, name):
        if name in self.teams:
            return self.teams[name]

    def get_player(self, name):
        if name in self.players:
            return self.players[name]

    def get_zone(self, name):
        if name in self.zones:
            return self.zones[name]

    def create_zone(self, name, type, x, y, size, growth=0, xdir=0, ydir=0, interactive=None, r=None, g=None, b=None, target_size=None, rubber=None, player=None, owner=None, target_command=None):
        command = ['SPAWN_ZONE', 'n', name, type]

        if type == 'zombie':
            if player is None:
                return None
            command.append(player)

        if type == 'zombieOwner':
            if player is None or owner is None:
                return None
            command.append(player)
            command.append(owner)

        command.extend([x, y, size, growth, xdir, ydir])

        if type == 'rubber':
            if rubber is None:
                return None
            command.append(rubber)

        if interactive:
            command.append(interactive)

            if r and g and b:
                command.extend([r, g, b])

                if target_size:
                    command.append(target_size)

        send_command(' '.join(command))

        zone = Zone(name, type, x, y, size, growth, xdir, ydir, interactive, r, g, b, target_size, rubber, player, owner, target_command)
        self.zones[name] = zone

        return zone

    # ladderlog commands

    def new_round(self, command):
        self.round += 1
        self.zones = {}
        for player in self.players.values():
            player.alive = True

    def new_match(self, command):
        self.round = 1
        for team in self.teams.values():
            team.score = 0
        for player in self.players.values():
            player.score = 0

    def round_score(self, command):
        if command[2] in self.players:
            self.players[command[2]].score += int(command[1])

    def round_score_team(self, command):
        if command[2] in self.teams:
            self.teams[command[2]].score += int(command[1])

    def team_created(self, command):
        self.teams[command[1]] = Team(command[1])

    def team_destroyed(self, command):
        if command[1] in self.teams:
            del self.teams[command[1]]

    def team_renamed(self, command):
        if command[1] in self.teams:
            self.teams[command[2]] = self.teams.pop(command[1])
            self.teams[command[2]].name = command[2]

    def team_player_added(self, command):
        if command[1] in self.teams and command[2] in self.players:
            self.teams[command[1]].players[command[2]] = self.players[command[2]]

    def team_player_removed(self, command):
        if command[1] in self.teams and command[2] in self.teams[command[1]].players:
            del self.teams[command[1]].players[command[2]]

    def player_entered(self, command):
        self.players[command[1]] = Player(command[1], command[3], command[2])

    def player_left(self, command):
        if command[1] in self.players:
            del self.players[command[1]]

    def player_renamed(self, command):
        if command[1] in self.players:
            if not command[2] in self.players:
                self.players[command[2]] = self.players.pop(command[1])
                self.players[command[2]].name = command[2]
            self.players[command[2]].screenname = command[5]

    def num_humans(self, command):
        self.num_players = command[1]

    def positions(self, command):
        if command[1] in self.teams:
            self.teams[command[1]].positions = []
            for i in range(2, len(command)):
                self.teams[command[1]].positions.append(self.getPlayer(command[i]))

    def zone_spawned(self, command):
        if command[2]:
            self.zones[command[2]] = Zone(command[2], None, command[3], command[4], None)
        else:
            self.zones[command[1]] = Zone(command[1], None, command[3], command[4], None)

    def zone_collapsed(self, command):
        if command[1] in self.zones:
            del self.zones[command[1]]
        elif command[2] in self.zones:
            del self.zones[command[2]]

    def death_frag(self, command):
        if command[1] in self.players:
            self.players[command[1]].alive = False

    def death_suicide(self, command):
        if command[1] in self.players:
            self.players[command[1]].alive = False

    def death_teamkill(self, command):
        if command[1] in self.players:
            self.players[command[1]].alive = False

    def death_basezone_conquered(self, command):
        if command[1] in self.players:
            self.players[command[1]].alive = False

    def death_deathzone(self, command):
        if command[1] in self.players:
            self.players[command[1]].alive = False

    def death_rubberzone(self, command):
        if command[1] in self.players:
            self.players[command[1]].alive = False

    def death_shot_frag(self, command):
        if command[1] in self.players:
            self.players[command[1]].alive = False

    def death_shot_suicide(self, command):
        if command[1] in self.players:
            self.players[command[1]].alive = False

    def death_shot_teamkill(self, command):
        if command[1] in self.players:
            self.players[command[1]].alive = False

    def reset(self, command):
        self.round = 0
        self.teams = {}
        self.num_players = 0
        self.players = {}
        self.zones = {}


grid = Grid()

commands = {
    'NEW_ROUND': [grid.new_round],
    'NEW_MATCH': [grid.new_match],
    'ROUND_SCORE': [grid.round_score],
    'ROUND_SCORE_TEAM': [grid.round_score_team],
    'TEAM_CREATED': [grid.team_created],
    'TEAM_DESTROYED': [grid.team_destroyed],
    'TEAM_RENAMED': [grid.team_renamed],
    'TEAM_PLAYER_ADDED': [grid.team_player_added],
    'TEAM_PLAYER_REMOVED': [grid.team_player_removed],
    'PLAYER_ENTERED': [grid.player_entered],
    'PLAYER_LEFT': [grid.player_left],
    'PLAYER_RENAMED': [grid.player_renamed],
    'NUM_HUMANS': [grid.num_humans],
    'POSITIONS': [grid.positions],
    'ZONE_SPAWNED': [grid.zone_spawned],
    'ZONE_COLLAPSED': [grid.zone_collapsed],
    'DEATH_FRAG': [grid.death_frag],
    'DEATH_SUICIDE': [grid.death_suicide],
    'DEATH_TEAMKILL': [grid.death_teamkill],
    'DEATH_BASEZONE_CONQUERED': [grid.death_basezone_conquered],
    'DEATH_DEATHZONE': [grid.death_deathzone],
    'DEATH_RUBBERZONE': [grid.death_rubberzone],
    'DEATH_SHOT_FRAG': [grid.death_shot_frag],
    'DEATH_SHOT_SUICIDE': [grid.death_shot_suicide],
    'DEATH_SHOT_TEAMKILL': [grid.death_shot_teamkill],
    'GAME_END': [grid.reset],
    'ENCODING': [init],
    'INVALID_COMMAND': [chat_command]
}

chat_commands = {}