summaryrefslogtreecommitdiffhomepage
path: root/host/simu/view/table_eurobot2012.py
blob: 89ada042e9dbd162bbd9b3643e64231acdf531f3 (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
# simu - Robot simulation. {{{
#
# Copyright (C) 2011 Nicolas Schodet
#
# APBTeam:
#        Web: http://apbteam.org/
#      Email: team AT apbteam DOT org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# }}}
"""Eurobot 2012 table."""
from simu.inter.drawable import Drawable
import math
from math import pi

PURPLE = '#a737ff'
RED = '#fe4543'
BLUE = '#01c3ff'
GREEN = '#81ff00'
BLACK = '#1f1b1d'
YELLOW = '#f7ff00'
WHITE = '#f5fef2'
TRANS = '#c0c2b5'
BROWN = '#9d4e01'

def draw_coin (d, coin):
    d.trans_push ()
    color = WHITE if coin.value else BLACK
    d.draw_circle ((0, 0), coin.radius, fill = color)
    d.draw_circle ((0, 0), 7.5, fill = color)
    d.trans_translate ((40.5, 0))
    d.draw_rectangle ((-9, -9), (9, 9), fill = color, outline = TRANS)
    d.trans_pop ()

class Coin (Drawable):

    def __init__ (self, onto, model):
        Drawable.__init__ (self, onto)
        self.model = model
        self.model.register (self.__notified)
        self.__notified ()

    def __notified (self):
        self.pos = self.model.pos
        self.angle = self.model.angle
        self.update ()

    def draw (self):
        self.reset ()
        if self.pos:
            self.trans_translate (self.pos)
            self.trans_rotate (self.angle)
            draw_coin (self, self.model)
            Drawable.draw (self)

def draw_gold_bar (d, gold_bar):
    d.draw_rectangle ((-gold_bar.dim[0] / 2, -gold_bar.dim[1] / 2),
            (gold_bar.dim[0] / 2, gold_bar.dim[1] / 2), fill = YELLOW)
    d.draw_rectangle ((-gold_bar.dim[0] / 2 + 13, -gold_bar.dim[1] / 2 + 13),
            (gold_bar.dim[0] / 2 - 13, gold_bar.dim[1] / 2 - 13), fill = YELLOW)

class GoldBar (Drawable):

    def __init__ (self, onto, model):
        Drawable.__init__ (self, onto)
        self.model = model
        self.model.register (self.__notified)
        self.__notified ()

    def __notified (self):
        self.pos = self.model.pos
        self.angle = self.model.angle
        self.update ()

    def draw (self):
        self.reset ()
        if self.pos:
            self.trans_translate (self.pos)
            self.trans_rotate (self.angle)
            draw_gold_bar (self, self.model)
            Drawable.draw (self)

class Table (Drawable):
    """The table and its elements."""

    def __init__ (self, onto, model):
        Drawable.__init__ (self, onto)
        self.model = model
        for e in self.model.coins:
            if e.level <= 2:
                Coin (self, e)
        for e in self.model.gold_bars:
            GoldBar (self, e)
        for e in self.model.coins:
            if e.level > 2:
                Coin (self, e)

    def draw_both (self, primitive, *args, **kargs):
        """Draw a primitive on both sides."""
        primitive (*args, **kargs)
        primitive (*((3000 - x, y) for x, y in args), **kargs)

    def draw (self):
        # Redraw.
        self.reset ()
        # Table.
        self.draw_rectangle ((-22, -22), (3000 + 22, 2000 + 22), fill = BLUE)
        self.draw_rectangle ((0, 0), (3000, 2000), fill = BLUE)
        #  Start zones.
        self.draw_rectangle ((0, 2000 - 500), (500, 2000), fill = PURPLE)
        self.draw_rectangle ((3000, 2000 - 500), (3000 - 500, 2000), fill = RED)
        #  Black lines.
        pup = 2000 - 500 + 50
        self.draw_both (self.draw_polygon, (500, pup), (650, pup), (650, 0), (630, 0),
                (630, pup - 20), (500, pup - 20), fill = BLACK, outline = 'black')
        #  Ships.
        ba = 0.04995839
        self.draw_both (self.draw_polygon, (0, 2000 - 500), (400, 2000 - 500), (325, 0), (0, 0),
                fill = BROWN, outline = 'black')
        self.draw_both (self.draw_rectangle, (0, 2000 - 500), (400, 2000 - 500 - 18), fill = BROWN)
        cba = 750 * math.cos (ba)
        sba = 750 * math.sin (ba)
        self.draw_both (self.draw_polygon, (325, 0), (325 + sba, cba), (325 + sba + 18, cba), (325 + 18, 0),
                fill = BROWN, outline = 'black')
        self.draw_both (self.draw_rectangle, (-22, -22), (340, 610 - 22), fill = TRANS)
        self.draw_both (self.draw_rectangle, (-4, -4), (340 - 18, 610 - 22 - 18), fill = TRANS)
        #  Peanut island.
        self.draw_both (self.draw_circle, (1500 - 400, 1000), r = 300, fill = YELLOW)
        self.draw_rectangle ((1500 - 400 + 141, 1000 + 265), (1500 + 400 - 141, 1000 - 265), fill = YELLOW, outline = '')
        self.draw_arc ((1500, 1000 + 750), 550, start = pi + 1, extent = pi - 2, fill = BLUE, outline = '')
        self.draw_arc ((1500, 1000 + 750), 550, start = pi + 1.08083, extent = pi - 2 * 1.08083, style = 'arc')
        self.draw_arc ((1500, 1000 - 750), 550, start = 1, extent = pi - 2, fill = BLUE, outline = '')
        self.draw_arc ((1500, 1000 - 750), 550, start = 1.08083, extent = pi - 2 * 1.08083, style = 'arc')
        self.draw_both (self.draw_circle, (1500 - 400, 1000), r = 200, fill = GREEN)
        self.draw_circle ((1500, 1000), r = 75, fill = GREEN)
        self.draw_both (self.draw_rectangle, (1500 - 400 - 125, 1000 - 125), (1500 - 400 + 125, 1000 + 125), fill = BROWN)
        #  Map island.
        self.draw_arc ((1500, 2000), 400, start = pi, extent = pi, fill = YELLOW)
        self.draw_arc ((1500, 2000), 300, start = pi, extent = pi, fill = GREEN)
        #  Map.
        self.draw_rectangle ((1500 - 200 - 22, 2000 + 148), (1500 + 200 + 22, 2000 + 140), fill = BLUE)
        self.draw_rectangle ((1500 - 200, 2000 + 140), (1500 + 200, 2000 - 8), fill = BLUE)
        self.draw_both (self.draw_rectangle, (1500 - 200 - 22, 2000 + 140), (1500 - 200, 2000 + 125), fill = BLUE)
        self.draw_both (self.draw_rectangle, (1500 - 200 - 22, 2000 + 125), (1500 - 200, 2000 - 23), fill = BLUE)
        self.draw_rectangle ((1500 - 22, 2000 + 140), (1500 + 22, 2000 + 125), fill = BLUE)
        self.draw_rectangle ((1500 - 22, 2000 + 125), (1500 + 22, 2000 + 115), fill = BLUE)
        self.draw_rectangle ((1500 - 22, 2000 + 2), (1500 + 22, 2000 - 13), fill = BLUE)
        self.draw_rectangle ((1500 - 22, 2000 - 13), (1500 + 22, 2000 - 23), fill = BLUE)
        # Bottles.
        def draw_bottle (x, color):
            self.draw_rectangle ((x - 100, 0), (x + 100, -44), fill = color)
            self.draw_rectangle ((x - 100, -44), (x + 100, -66), fill = color)
            self.draw_rectangle ((x - 11, 44), (x + 11, 0), fill = color)
            self.draw_rectangle ((x - 50, 22), (x + 100, -44), fill = color)
            self.draw_polygon ((x - 50, 22), (x - 80, 0), (x - 100, 0), (x - 100, -22), (x - 80, -22), (x - 50, -44),
                    fill = color, outline = 'black')
        draw_bottle (640, PURPLE)
        draw_bottle (640 + 477, RED)
        draw_bottle (3000 - 640, RED)
        draw_bottle (3000 - 640 - 477, PURPLE)
        # Axes.
        self.draw_line ((0, 200), (0, 0), (200, 0), arrow = 'both')
        # Beacons.
        self.draw_both (self.draw_rectangle, (-22, 2000 + 22), (-22 - 80, 2000 + 22 + 80), fill = BLACK)
        self.draw_both (self.draw_rectangle, (-22, 1000 - 40), (-22 - 80, 1000 + 40), fill = BLACK)
        self.draw_both (self.draw_rectangle, (-22, -80 - 22), (-22 - 80, -22), fill = BLACK)
        # Children.
        Drawable.draw (self)

if __name__ == '__main__':
    from simu.inter.inter import Inter
    import simu.model.table_eurobot2012 as model
    app = Inter ()
    m = model.Table ()
    Table (app.table_view, m)
    app.mainloop ()