summaryrefslogtreecommitdiff
path: root/robots/utils.py
blob: 544bec7a0cfde1319b85ae35ce8d85c5397c4294 (about) (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
from random import sample
import time

def add_spawns(map_, n_spawns):
    available = []
    for x, y, cell in iter_board(map_):
        if cell != '*':
            available.append((x, y))

    spawns = sample(available, n_spawns)
    for i, (x, y) in enumerate(spawns):
        map_[y][x] = str(i)

def empty_map(width, height, n_spawns):
    board = [['.'] * width for y in range(height)]

    add_spawns(board, n_spawns)
    return board

def border_map(width, height, n_spawns):
    board = [
        ['.'] * (width + 2)
        for y in range(height + 2)
    ]
    for x in range(width + 2):
        board[0][x] = '*'
        board[-1][x] = '*'
    for y in range(height + 2):
        board[y][0] = '*'
        board[y][-1] = '*'

    add_spawns(board, n_spawns)
    return board

def rate_limit(fps):
    delay_ms = 1. / fps

    while True:
        # time one iteration of the loop
        start = time.time()
        yield
        end = time.time()
        yield_time = end - start

        # delay to fill up the rest of the time
        wait_time = delay_ms - yield_time
        if wait_time < 0:
            # TODO: log failure
#            log.debug('not meeting rate (%.2f ms)' % (wait_time * 1000))
            continue
        time.sleep(wait_time)

def iter_board(board):
    for y, row in enumerate(board):
        for x, cell in enumerate(row):
            yield x, y, cell