Source

BASIC-RoBots / src / env.py

Full commit
  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
##
## env.py for BASIC-RoBots
## 
## Copyright (C) 2012 Pierre Surply
## <pierre.surply@gmail.com>
##
## This file is part of BASIC-RoBots.
##
##    BASIC-RoBots 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 3 of the License, or
##    (at your option) any later version.
##
##    BASIC-RoBots 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 BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>. 
##
## Started on  Thu Jun 28 13:22:05 2012 Pierre Surply
## Last update Mon Aug 27 19:37:59 2012 Pierre Surply
##

import os
import random
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

import surface
import noise
import savable
from robot import mothership, woodcutter

class Env(savable.Savable):
    temp_tiles = [((255, 255, 255, 255), 0, "Grass", 0.7),\
                  ((150, 100, 0, 255), 1, "Dirt", 0.4),\
                  ((0, 0, 100, 255), 2, "Water", 0),\
                  ((255, 255, 150, 255), 3, "Sand", 0.2),\
                  ((0, 0, 150, 255), 4, "Deep water", 0),\
                  ((100, 100, 100, 255), 5, "Stone", 1),\
                  ((255, 0, 0, 255), 6, "Lava", 0),\
                  ((20, 20, 20, 255), 7, "Gravel", 0.4),\
                  ((10, 10, 10, 255), 8, "Volvanic stone", 1),\
                  ((100, 0, 100, 255), 9, "Meteorite dirt", 1),\
                  ((100, 0, 255, 255), 10, "Meteorite stone", 1),\
                  ((101, 101, 101, 255), 11, "Stone", 1)]

    temp_elts = [((0, 100, 0, 255), 0, "Tree"),\
                 ((150, 150, 150, 255), 1, "Stone"),\
                 ((100, 100, 100, 255), 2, "Bricks"),\
                 ((100, 50, 0, 255), 3, "Wood"),\
                 ((100, 0, 50, 255), 4, "Meteorite"), \
                 ((200, 200, 200, 255), 5, "Iron")]

    biomes = [([(0.3, 4),\
                   (0.8,2),\
                   (1.0,3)], []),\
                  ([(0.5, 2),\
                       (0.7, 3),\
                       (0.8, 1),\
                       (1.0, 0)], []),\
                  ([(0.3, 2),\
                       (0.33, 3),\
                       (0.6, 1),\
                       (1.0, 0)], [(0.4, 0)]),\
                  ([(0.1, 2),\
                       (0.2, 1),\
                       (0.4, 11),\
                       (0.6, 5),\
                       (0.7, 7),\
                       (0.8, 8),\
                       (1.0, 6)], [(0.4, 1)]),\
                  ([(0.5, 6),\
                       (0.8, 8),\
                       (1.0, 5)], []),\
                  ([(0.2, 2),\
                       (0.5, 1),\
                       (0.7, 11),\
                       (1.0, 5)], \
                       []),\
                  ([(0.2, 2),\
                       (0.21,3),\
                       (0.4,1),\
                       (0.8,0),\
                       (1.0,5)], [(0.5, 0)]),\
                  ([(0.5, 6),\
                       (0.8, 8),\
                       (1.0, 5)], []),\
                  ([(0.1, 2),\
                       (0.3, 9),\
                       (0.6, 10),\
                       (0.8, 5),\
                       (1.0, 11)], []),\
                  ([(0.4, 4),\
                       (0.8, 2),\
                       (1.0, 3)], []),\
                  ([(0.1, 2),\
                       (0.2, 1),\
                       (0.4, 11),\
                       (0.6, 5),\
                       (0.7, 7),\
                       (0.8, 8),\
                       (1.0, 6)], [])]
    size = 64
    
    def __init__(self, path, biome, (x, y), events):
        self.set_display_list()
        self.init_tile()
        self.angle = 0
        self.robots = []
        self.running_robot = 0
        self.biome = biome
        self.x = x
        self.y = y
        self.events = events
        self.path = path
        self.path_img = "saves/" + path + "/env/" + str(x) + "_" + str(y) + ".bmp"
        self.path_img_elts = "saves/" + path + "/env/" + str(x) + "_" + str(y) + "_elts.bmp"
        if os.path.isfile(self.path_img):
            self.load()
        else:
            self.build(biome)
            self.save()
        self.sprite_floor = surface.cut_surface(pygame.image.load("res/img/floor.png").convert_alpha(), (16, 16))
        self.sprite_floor_mini = surface.cut_surface(pygame.image.load("res/img/floor_mini.png").convert_alpha(), (2, 2))
        self.sprite_elts = surface.cut_surface(pygame.image.load("res/img/lvl_elts.png").convert_alpha(), (16, 16))
        self.sprite_robots = surface.cut_surface(pygame.image.load("res/img/robots.png").convert_alpha(), (32, 32))
        self.cursor = surface.surface2texture(pygame.image.load("res/img/cursor.png").convert_alpha())


    def set_display_list(self):
        self.list_floor = glGenLists(2)
        glNewList(self.list_floor,GL_COMPILE)
        glBegin(GL_QUADS)
        glTexCoord2d(0,0); glVertex3d(0, 1, 0)
        glTexCoord2d(0,1); glVertex3d(0, 0, 0)
        glTexCoord2d(1,1); glVertex3d(1, 0, 0)
        glTexCoord2d(1,0); glVertex3d(1, 1, 0)
        glEnd()
        glEndList()

        self.list_floor_side = glGenLists(3)
        glNewList(self.list_floor_side,GL_COMPILE)
        glBegin(GL_QUADS)
        glTexCoord2d(0,0); glVertex3d(0, 0, 1)
        glTexCoord2d(0,1); glVertex3d(0, 0, 0)
        glTexCoord2d(1,1); glVertex3d(1, 0, 0)
        glTexCoord2d(1,0); glVertex3d(1, 0, 1)
        glEnd()
        glEndList()

        self.list_elt = glGenLists(3)
        glNewList(self.list_elt,GL_COMPILE)
        glBegin(GL_QUADS)
        glTexCoord2d(0,0); glVertex3d(0, 0, 0)
        glTexCoord2d(0,1); glVertex3d(0, 0, 1.41)
        glTexCoord2d(1,1); glVertex3d(1, 1, 1.41)
        glTexCoord2d(1,0); glVertex3d(1, 1, 0)
        glEnd()
        glBegin(GL_QUADS)
        glTexCoord2d(0,0); glVertex3d(0, 1, 0)
        glTexCoord2d(0,1); glVertex3d(0, 1, 1.41)
        glTexCoord2d(1,1); glVertex3d(1, 0, 1.41)
        glTexCoord2d(1,0); glVertex3d(1, 0, 0)
        glEnd()
        glEndList()

        self.list_robot = glGenLists(3)
        glNewList(self.list_robot,GL_COMPILE)
        glBegin(GL_QUADS)
        glTexCoord2d(0,0); glVertex3d(-0.5, 0.5, 0)
        glTexCoord2d(0,1); glVertex3d(-0.5, -0.5, 0)
        glTexCoord2d(1,1); glVertex3d(0.5, -0.5, 0)
        glTexCoord2d(1,0); glVertex3d(0.5, 0.5, 0)
        glEnd()
        glEndList()
        
    def init_tile(self):
        self.tile = [[0] * self.size for i in range(self.size)]
        self.tile_elts = [[None] * self.size for i in range(self.size)]

    def load(self):
        self.load_img(self.path_img, self.size, self.tile, self.color2tile)
        if os.path.isfile(self.path_img_elts):
            self.load_img(self.path_img_elts,\
                              self.size,\
                              self.tile_elts,\
                              self.color2elt)
        else:
            self.build_elts()
            self.save_img(self.path_img_elts, self.size, self.tile_elts, self.elt2color)
            
    def save(self):
        self.save_img(self.path_img, self.size, self.tile, self.tile2color)
        self.save_img(self.path_img_elts, self.size, self.tile_elts, self.elt2color)
        for i in self.robots:
            i.save()

    def tile2color(self, (x, y)):
        tile = self.tile[x][y]
        for i in self.temp_tiles:
            if tile == i[1]:
                return pygame.Color(i[0][0],\
                                        i[0][1],\
                                        i[0][2],\
                                        i[0][3])
        
    def color2tile(self, color):
        for i in self.temp_tiles:
            if color == i[0]:
                return i[1]
        return 0

    def elt2color(self, (x, y)):
        elt = self.tile_elts[x][y]
        for i in self.temp_elts:
            if elt == i[1]:
                return pygame.Color(i[0][0],\
                                        i[0][1],\
                                        i[0][2],\
                                        i[0][3])
        return pygame.Color(255, 255, 255)

    def color2elt(self, color):
        if (color != (255, 255, 255)):
            for i in self.temp_elts:
                if color == i[0]:
                    return i[1]
        else:
            return None

    def render_case(self, x, y):
        glBindTexture(GL_TEXTURE_2D, self.sprite_floor[self.tile[x][y]])
        z = self.temp_tiles[self.tile[x][y]][3]
        glPushMatrix()
        glTranslated(x, y, z)
        glCallList(self.list_floor)
        if (not self.tile[x][y] in [2, 4]):
            if (y >= self.size-1 or self.temp_tiles[self.tile[x][y+1]][3] < z):
                glPushMatrix()
                glTranslated(0, 1, -1)
                glCallList(self.list_floor_side)
                glPopMatrix()
            if (y <= 0 or self.temp_tiles[self.tile[x][y-1]][3] < z):
                glPushMatrix()
                glTranslated(0, 0, -1)
                glCallList(self.list_floor_side)
                glPopMatrix()
            if (x <= 0 or self.temp_tiles[self.tile[x-1][y]][3] < z):
                glPushMatrix()
                glTranslated(0, 0, -1)
                glRotated(90, 0, 0, 1)
                glCallList(self.list_floor_side)
                glPopMatrix()
            if (x >= self.size-1 or self.temp_tiles[self.tile[x+1][y]][3] < z):
                glPushMatrix()
                glTranslated(1, 0, -1)
                glRotated(90, 0, 0, 1)
                glCallList(self.list_floor_side)
                glPopMatrix()
        glPopMatrix()

    def render_elt(self, x, y): 
        glBindTexture(GL_TEXTURE_2D, self.sprite_elts[self.tile_elts[x][y]])
        z = self.temp_tiles[self.tile[x][y]][3]
        glPushMatrix()
        glTranslated(x, y, z)
        glCallList(self.list_elt)
        glPopMatrix()

    def render_robot(self, r):
        glBindTexture(GL_TEXTURE_2D, self.sprite_robots[r.id_sprite])
        z = self.temp_tiles[self.tile[r.pos_x][r.pos_y]][3] + 0.4
        glPushMatrix()
        glTranslated(r.pos_x+0.5, r.pos_y+0.5, z)
        glRotated(90*r.orient, 0, 0, 1)
        glCallList(self.list_robot)
        glPopMatrix()

    def render(self, (select_x, select_y, cam_w, cam_h), (dx, dy)):
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity()
        gluPerspective(40, cam_w/cam_h,1,64)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_ALPHA_TEST);
        glAlphaFunc(GL_GREATER, 127.0/255.0);

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        gluLookAt(-2, 6, 10,\
                      0, 0, 0, \
                      0, 0, 1)
        glRotated(self.angle, 0, 0, 1)

        z = self.temp_tiles[self.tile[select_x][select_y]][3] + 0.1
        glBindTexture(GL_TEXTURE_2D, self.cursor)
        glPushMatrix()
        glTranslated(-dx, -dy, z)
        glCallList(self.list_robot)
        glPopMatrix()
        glTranslated(-select_x-0.5-dx, -select_y-0.5-dy, 0);

        for y in range(max(select_y-7, 0), min(select_y+8, self.size)):
            for x in range(max(select_x-6, 0), min(select_x+10, self.size)):
                self.render_case(x, y)
                if self.tile_elts[x][y] != None:
                    self.render_elt(x, y)
        for i in self.robots:
            x = i.pos_x
            y = i.pos_y
            if x >= select_x-8 and \
                    y >= select_y-16 and \
                    x < select_x+16 and \
                    y < select_y+8:
                self.render_robot(i)
        glMatrixMode(GL_PROJECTION)
        glPopMatrix()

        glDisable(GL_DEPTH_TEST)
        glDisable(GL_ALPHA_TEST);

    def render_mini(self, surface, (dx, dy)):
        for y in range(self.size):
            for x in range(self.size):
                surface.blit(self.sprite_floor_mini[self.tile[x][y]], (x*2+dx, y*2+dy))

    def build(self, biome):
        n = noise.Noise(33, 33, 8, 8)
        for y in range(self.size):
            for x in range(self.size):
                self.tile[x][y] = self.get_rndtile(x, y, n, biome)
        self.build_elts()
        self.land_mothership()
                
    def get_rndtile(self, x, y, noise, biome):
        val = noise.smooth_noise(x, y, 0.6)
        for i in self.biomes[biome][0]:
            if val < i[0]:
                return i[1]
        return 0

    def build_elts(self):
        biome = self.biome
        n = noise.Noise(33, 33, 8, 8)
        for y in range(self.size):
            for x in range(self.size):
                elt = self.get_rndelts(x, y, n, biome)
                tile = self.tile[x][y]
                self.tile_elts[x][y] = None
                if elt == 0:
                    if tile in [0,1]:
                        self.tile_elts[x][y] = elt
                elif elt == 1:
                    if tile in [0,1, 5, 7, 8, 9, 10, 11]:
                        self.tile_elts[x][y] = elt
                else:
                    self.tile_elts[x][y] = elt
                
    def get_rndelts(self, x, y, noise, biome):
        val = noise.smooth_noise(x, y, 0.7)
        for i in self.biomes[biome][1]:
            if val < i[0]:
                return i[1]
        return None


    def land_mothership(self):
        r = mothership.Mothership(True,\
                                      self.path,\
                                      "Mothership_"+str(self.x)+"-"+str(self.y),\
                                      self, \
                                      (0, 0),\
                                      self.events)
        
        x = random.randint(0, self.size-1)
        y = random.randint(0, self.size-1)
        while r.move_to((x, y)) == 0:
            x = random.randint(0, self.size-1)
            y = random.randint(0, self.size-1)
        self.robots.append(r)

    def get_pos_robot(self, n):
        if n < len(self.robots):
            return self.robots[n].get_pos()
        else:
            return (0,0)

    def get_pos_robots(self):
        return [ i.get_pos() for i in self.robots]
            
    def update_robots(self, n, display, events):
        for i in range(len(self.robots)):
            self.robots[i].update((i == n), \
                                   display, events)
        #self.robots[self.running_robot].update((self.running_robot == n), \
        #                                           display, events)
        #if n != None and self.running_robot != n:
        #    self.robots[n].update(True, \
        #                              display, events)
        #self.running_robot += 1
        #if self.running_robot >= len(self.robots):
        #    self.running_robot = 0