/
World.py
612 lines (487 loc) · 20.8 KB
/
World.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
from math import cos, sin, pi
from random import randint, seed
import pygame
import vector2
import Tile
import Building
import Builder
import Farmer
import Lumberjack
from Clips import Clips
from VoronoiMapGen import point, mapGen
grass_img = pygame.image.load("Images/Tiles/MinecraftDarkGrass.png")
tree_img = pygame.image.load("Images/Tiles/MinecraftGrass.png")
water_img = pygame.image.load("Images/Tiles/AndrewWater2.png")
sand_img = pygame.image.load("Images/Tiles/Sand.PNG")
cobble_img = pygame.image.load("Images/Tiles/AndrewCobble2.png")
SStone_img = pygame.image.load("Images/Tiles/AndrewSmoothStone.png")
deepwater_img = pygame.image.load("Images/Tiles/AndrewDeepWater.png")
snow_img = pygame.image.load("Images/Tiles/MinecraftSnow.png")
WithTree_img = pygame.image.load("Images/Tiles/GrassWithCenterTree.png")
lumber_yard_img = pygame.image.load("Images/Buildings/LumberYard.png")
house_img = pygame.image.load("Images/Buildings/House.png")
uc_house_img = pygame.image.load("Images/Buildings/UC_House.png")
dock_img = pygame.image.load("Images/Buildings/Dock.png")
manor_img = pygame.image.load("Images/Buildings/Manor.png")
uc_img = pygame.image.load("Images/Buildings/UC.png")
ucw_img = pygame.image.load("Images/Buildings/UC_Dock.png")
lumberjack_img = pygame.image.load("Images/Entities/Lumberjack.png")
farmer_img = pygame.image.load("Images/Entities/Farmer.png")
builder_img = pygame.image.load("Images/Entities/Builder.png")
class World(object):
"""Class that stores basically EVERYTHING
"""
def __init__(self, ss, WorldSize, font, rand_seed):
self.size = WorldSize
#How big the map is
self.TileSize = 32
self.ssize = (
self.size[0] / self.TileSize, self.size[1] / self.TileSize)
self.seed = rand_seed
seed(self.seed)
self.ss = ss
self.w, self.h = self.size
#Certain functions entities need this.
self.center = vector2.Vector2(self.w / 2, self.h / 2)
self.building = {}
self.entities = {}
#Stores all entities the game processes
self.entity_id = 0
#Each entity is given a unique id so the program can find it
self.wood = 0
#Probably will add other resources
self.MAXwood = 50
self.food = 0
self.MAXfood = 0
self.population = 0
self.MAXpopulation = 15
self.background_pos = vector2.Vector2(ss[0] / 5.0, 0)
self.mapGenerator = mapGen()
self.background_over = pygame.Surface((1600, 900),
pygame.HWSURFACE)
self.background_over.set_alpha(128)
self.background_over.fill((0, 0, 20, 128))
self.full_surface = pygame.Surface(self.size, pygame.HWSURFACE)
self.clock = pygame.time.Clock()
print self.size
self.background = pygame.Surface(
(self.size[0], self.size[1]), pygame.HWSURFACE)
self.background.fill((255, 255, 255))
self.font = font
self.font_size = self.font.size("A")[1]
self.text = self.font.render(str(self.wood), True, (0, 0, 0))
#World entity also stores global font for rendering to the screen.
self.clock_degree = 0
#Used for the clock
self.convert_all()
self.new_world()
self.cliper = Clips(self, self.ss)
self.BuildingQueue = []
self.buildqueue = 0
def new_world(self):
del self.full_surface
#seed(self.seed)
img = self.mapGenerator.negative(
self.mapGenerator.reallyCoolFull(self.ssize, num_p=23))
#img = self.mapGenerator.whole_new(25, self.ssize, 1, -1)
self.map_width, self.map_height = img.get_size()
self.minimap_img = pygame.Surface((self.map_width, self.map_height))
self.TileArray = [[0 for i in xrange(
self.map_width)] for a in xrange(self.map_height)]
self.TreeID = 0
self.TreeLocations = {}
self.Baby_TreeID = 0
self.Baby_TreeLocations = {}
self.buildings = {"LumberYard": {},
"Dock": {},
"House": {},
"Manor": {},
"UC": {}}
self.building = {}
self.entities = {}
#Stores all entities the game processes
self.entity_id = 0
#Each entity is given a unique id so the program can find it
self.wood = 0
#Probably will add other resources
self.MAXwood = 50
self.food = 0
self.MAXfood = 0
self.population = 0
self.MAXpopulation = 15
self.background_pos = vector2.Vector2(self.ss[0] / 5.0, 0)
self.mapGenerator = mapGen()
self.full_surface = pygame.Surface(self.size, pygame.HWSURFACE)
self.clock_degree = 0
#Used for the clock
self.BuildingQueue = []
self.buildqueue = 0
for i in xrange(self.map_width):
self.current_height = 0
w_last = False
for a in xrange(self.map_height):
f_color = img.get_at((i, a))
color = f_color[0]
to_rotate = 1
color2 = (255, 0, 220)
# if color < 95:
# colorb = 0
# tile = DeepWaterTile(self, self.deepwater_img)
if color < 110:
colorb = 0
tile = Tile.WaterTile(self, self.water_img)
last_image = self.sand_img
last_color = 0
elif color >= 110 and color < 120:
colorb = 110
tile = Tile.BeachTile(self, self.sand_img)
last_image = self.sand_img
#to_rotate = 0
last_color = 110
elif color >= 120 and color < 140:
colorb = 120
tile = Tile.GrassTile(self, self.grass_img)
last_image = self.sand_img
last_color = 120
elif color >= 140 and color < 160:
colorb = 140
tile = Tile.TreePlantedTile(self, self.tree_img)
last_image = self.grass_img
last_color = 140
elif color >= 160 and color < 170:
colorb = 160
if color2[2] == 220:
tile = Tile.TreePlantedTile_w(self, self.WithTree_img)
tile.location = vector2.Vector2(i << 5, a << 5)
tile.rect.topleft = tile.location
tile.id = self.TreeID
self.TreeLocations[str(self.TreeID)] = tile.location
self.TreeID += 1
to_rotate = 0
else:
tile = Tile.TreePlantedTile(self, self.tree_img)
last_image = self.tree_img
last_color = 160
elif color >= 170 and color < 190:
colorb = 170
tile = Tile.TreePlantedTile(self, self.tree_img)
last_image = self.WithTree_img
last_color = 170
elif color >= 190 and color < 236:
colorb = 190
tile = Tile.SmoothStoneTile(self, self.SStone_img)
last_image = self.tree_img
to_rotate = 0
last_color = 190
else:
colorb = 236
tile = Tile.SnowTile(self, self.snow_img)
last_image = self.SStone_img
last_color = 236
#Shadows----
fake_color = color
if color < 110:
fake_color = 110
if fake_color > self.current_height - 3:
dark_surface = pygame.Surface((32, 32))
dark_surface.set_alpha(0)
if color >= 110:
self.current_height = fake_color
else:
self.current_height -= 3
else:
self.current_height -= 3
dark_surface = pygame.Surface((1, 1))
dark_surface.set_alpha(128)
#-----------
#Used for determening start position
if color2[1] == 255:
WORLD_START_POS = (i, a)
#-----
tile.location = vector2.Vector2(i << 5, a << 5)
tile.rect.topleft = tile.location
tile.color = color
if to_rotate:
tile.img = pygame.transform.rotate(
tile.img, randint(0, 4) * 90)
self.background.blit(tile.img, tile.location)
dark_surface2 = pygame.Surface((32, 32))
alph = 235 - color
if color >= 190 and color < 236:
alph = 330 - color
dark_surface2.set_alpha(alph)
tile.darkness = alph
self.background.blit(dark_surface, tile.location)
self.background.blit(dark_surface2, tile.location)
"""
try:
percentage = (color-last_color) / float(
colorb - last_color)
except ZeroDivisionError:
percentage = 0.0
combined_img = self.mapGenerator.lerp_two_images(tile.img,
last_image, percentage)
"""
#self.minimap_img.blit(combined_img.subsurface(
# (0,0,1,1)), (i,a))
self.minimap_img.blit(tile.img.subsurface(
(0, 0, 1, 1)), (i, a))
self.minimap_img.blit(dark_surface.subsurface(
(0, 0, 1, 1)), (i, a))
self.minimap_img.blit(dark_surface2.subsurface(
(0, 0, 1, 1)), (i, a))
self.TileArray[a][i] = tile
self.populate()
self.cliper = Clips(self, self.ss)
def populate(self):
"""adds all the people and make sure they don't go all ape shit
"""
FARMER_COUNT = 4
VILLAGER_COUNT = 5
BUILDER_COUNT = 1
lumber1 = Building.LumberYard(self, self.lumberyard_img)
lumber1.location = vector2.Vector2(self.w / 2, self.h / 2)
lumber1.tile_x, lumber1.tile_y = 4, 4
self.add_entity(lumber1)
for Villager_no in xrange(VILLAGER_COUNT):
"""Adds all Wood Cutters
"""
villager = Lumberjack.Lumberjack(self, self.lumberjack_img)
villager.location = lumber1.location.copy()
villager.LastLumberYard = lumber1
villager.brain.set_state("Searching")
self.add_entity(villager)
self.population += 1
for Building_no in xrange(BUILDER_COUNT):
builder = Builder.Builder(self, self.builder_img, lumber1)
builder.location = lumber1.location.copy()
builder.brain.set_state("Idle")
self.add_entity(builder)
self.population += 1
for FARMER in xrange(FARMER_COUNT):
"""Adds all the farmers
"""
farmer = Farmer.Farmer(self, self.farmer_img)
farmer.location = vector2.Vector2(20, 20)
farmer.brain.set_state("Planting")
self.add_entity(farmer)
self.population += 1
def add_building(self, building, pos):
buildable = self.test_buildable(building, 0, pos)
print pos
if buildable:
Build = buildable[1]
Build.location = self.get_tile_pos(pos - self.background_pos) * 32
print "add_building LOC: ", building, Build.location
self.add_entity(Build)
self.buildings[building] = Build
self.BuildingQueue.append(Build)
return 1
def add_built(self, building, pos):
buildable = self.test_buildable(building, 1, pos)
print pos
if buildable:
Build = buildable[1]
Build.location = pos
print "buildable LOC2: ", building, Build.location
self.add_entity(Build)
self.buildings[building] = Build
return 1
def test_buildable(self, building, built, pos):
if building == "LumberYard":
if built:
Build = Building.LumberYard(self, self.lumberyard_img)
else:
Build = Building.UnderConstruction(self, self.uc_img,
"LumberYard")
elif building == "House":
if built:
Build = Building.House(self, self.house_img)
else:
Build = Building.UnderConstruction(self,
self.uc_house_img, "House")
elif building == "Dock":
if built:
Build = Building.Dock(self, self.dock_img)
else:
Build = Building.UnderConstruction(self, self.ucw_img, "Dock")
elif building == "Manor":
if built:
Build = Building.Manor(self, self.manor_img)
else:
Build = Building.UnderConstruction(self, self.uc_img, "Manor")
buildable = 1
land = 0
water = 0
Twidth, Theight = Build.image.get_size()
for i in range(Twidth >> 5):
for j in range(Theight >> 5):
try:
if built:
test_tile = self.get_tile(vector2.Vector2(
(pos.x - 32) + (i << 5), (pos.y - 32) + (j << 5)))
#print "A", test_tile, test_tile.location
else:
test_tile = self.get_tile(vector2.Vector2(
((pos.x - 32) - self.background_pos.x) + (i << 5), (
(pos.y - 32) - self.background_pos.y) + (j >> 5)))
#print "B", test_tile, test_tile.location
if test_tile.buildable != 1 and building != "Dock":
buildable = 0
return 0
elif building == "Dock":
if test_tile.buildable_w:
water += 1
elif test_tile.buildable:
land += 1
except IndexError:
print 'IndexError'
return 0
if building == "Dock" and not built:
if water >= 1 and land <= 2 and land > 0:
#print ('buildable', water, land)
buildable = 1
return 1, Build
else:
#print ('not buildable', water, land)
buildable = 0
return 0
return 1, Build
def convert_all(self):
self.grass_img = grass_img.convert()
self.tree_img = tree_img.convert()
self.water_img = water_img.convert()
self.sand_img = sand_img.convert()
self.cobble_img = cobble_img.convert()
self.SStone_img = SStone_img.convert()
self.deepwater_img = deepwater_img.convert()
self.snow_img = snow_img.convert()
self.WithTree_img = WithTree_img.convert()
self.lumberyard_img = lumber_yard_img.convert()
self.lumberyard_img.set_colorkey((255, 0, 255))
self.house_img = house_img.convert()
self.house_img.set_colorkey((255, 0, 255))
self.dock_img = dock_img.convert()
self.dock_img.set_colorkey((255, 0, 255))
self.manor_img = manor_img.convert()
self.manor_img.set_colorkey((255, 0, 255))
self.uc_img = uc_img.convert_alpha()
self.ucw_img = ucw_img.convert()
self.ucw_img.set_colorkey((255, 0, 255))
self.uc_house_img = uc_house_img.convert()
self.uc_house_img.set_colorkey((255, 0, 255))
self.lumberjack_img = lumberjack_img.convert()
self.farmer_img = farmer_img.convert()
self.builder_img = builder_img.convert()
def grow_trees(self, trees):
for i in range(len(trees)):
ran = randint(0, 100)
if ran == 1 and len(trees) > 0:
try:
a = trees.keys()
old_tile = self.get_tile(trees[a[i]])
darkness = pygame.Surface((32, 32))
darkness.set_alpha(old_tile.darkness)
new_tile = Tile.TreePlantedTile_w(self, WithTree_img)
new_tile.darkness = old_tile.darkness
new_tile.location = old_tile.location
new_tile.rect.topleft = new_tile.location
new_tile.color = old_tile.color
new_tile.id = self.TreeID
self.TreeID += 1
self.TileArray[int(new_tile.location.y / 32)][int(
new_tile.location.x / 32)] = new_tile
self.background.blit(new_tile.img, new_tile.location)
self.background.blit(darkness, new_tile.location)
#print self.Baby_TreeLocations
del self.Baby_TreeLocations[str(a[i])]
except IndexError:
pass
def add_entity(self, entity):
"""Used to add entities to the world
"""
self.entities[self.entity_id] = entity
entity.id = self.entity_id
self.entity_id += 1
def remove_entity(self, entity):
"""function for removing an entity
"""
try:
del self.entities[entity.id]
except KeyError:
pass
def remove_tree(self, tree_id):
#print len(self.TreeLocations)
try:
del self.TreeLocations[str(tree_id)]
return 0
except KeyError:
return None
def get(self, entity_id):
#Return an entity
if entity_id in self.entities:
return self.entities[entity_id]
else:
return None
def process(self, time_passed):
#Run the world through 1 cycle
for entity in self.entities.values():
entity.process(time_passed)
self.wood_text = self.font.render("Wood: %d/%d" % (
self.wood, self.MAXwood), True, (255, 255, 255))
self.food_text = self.font.render("Food: %d/%d" % (
self.food, self.MAXfood), True, (255, 255, 255))
self.pop_text = self.font.render("Population: %d/%d" % (
self.population, self.MAXpopulation), True, (255, 255, 255))
self.frame_text = self.font.render("FPS: %.2f" % (
self.clock.get_fps()), True, (255, 255, 255))
semi_angle = abs(self.clock_degree - 180.0)
self.background_alpha = min((255 - (255 * (
abs(semi_angle / 180)))), 220.0)
self.background_over.set_alpha(self.background_alpha)
def render(self, surface):
surface.blit(self.background, self.background_pos)
#for i in self.tilearray:
# for tile in i:
# tile.render(surface)
for entity in self.entities.itervalues():
entity.render(surface)
surface.blit(self.background_over, (0, 0))
#for point in self.pond_points:
# pygame.draw.circle(surface, (255,0,0), (
# int(point[0]), int(point[1])), 5)
# surface.set_clip(0,0,self.w,self.font_size+4)
# surface.blit(self.wood_text, (40,2))
# surface.blit(self.food_text, (200,2))
# surface.blit(self.pop_text, (360,2))
# surface.set_clip(None)
def render_all(self, surface, tp, mouse_pos):
self.cliper.render(surface, tp, mouse_pos)
def get_close_entity(self, name, location, range=100.):
location = vector2.Vector2(*location)
for entity in self.entities.itervalues():
if entity.name == name:
distance = location.get_distance_to(entity.location)
if range == -1:
return entity
if distance < range:
return entity
return None
def get_tile(self, location):
tile = self.get_tile_pos(location)
return self.TileArray[int(tile.y)][int(tile.x)]
def get_tile_pos(self, location):
return vector2.Vector2(int(location.x) >> 5, int(location.y) >> 5)
def get_tile_array(self, start_pos, dimensions):
dimensions = (int(dimensions[0]), int(dimensions[1]))
start_tile = self.get_tile_pos(start_pos)
array = [[None for i in xrange(
(dimensions[0] * 2) + 1)] for a in xrange((dimensions[1] * 2) + 1)]
for i in xrange((dimensions[0] * 2) + 1):
for a in xrange((dimensions[1] * 2) + 1):
if start_tile.x + i < 0 or start_tile.y + a < 0:
continue
else:
array[a][i] = self.TileArray[int(
(start_tile.y + a) - 1)][int((start_tile.x + i) - 1)]
return array