forked from kaikue/Oceania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chunk.py
188 lines (173 loc) · 8.57 KB
/
Chunk.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
import random
import pygame
import json
import Game
import Convert
import World
WIDTH = 16
LEFT = False
RIGHT = True
class Chunk(object):
def __init__(self):
self.heights = [0] * WIDTH
self.blocks = [[0] * WIDTH for _ in range(World.HEIGHT)]
self.entities = []
def generate_spawn(self):
self.x = 0
self.biome = World.biomes["default"] #this works
self.generate_spawn_heights()
self.populate()
def generate_spawn_heights(self):
self.heights = [144] * WIDTH
def generate_from_chunk(self, chunk, sidegenerated):
if sidegenerated == LEFT:
self.x = chunk.x + 1
sideheight = chunk.heights[WIDTH - 1]
else:
self.x = chunk.x - 1
sideheight = chunk.heights[0]
if random.random() < 0.5: #can fiddle with this- maybe based on previous chunk's biome
biomes_wanted = []
#print(sideheight)
for biome in World.biomes:
#print(World.biomes[biome]["maxelevation"], World.biomes[biome]["minelevation"])
if World.biomes[biome]["maxelevation"] <= sideheight <= World.biomes[biome]["minelevation"]:
biomes_wanted.append(World.biomes[biome])
self.biome = biomes_wanted[random.randrange(len(biomes_wanted))] #select random from biomes_wanted
else:
self.biome = chunk.biome
#print("Chunk", self.x, "is biome", self.biome)
self.generate_heights_from_chunk(chunk, sidegenerated)
self.populate()
def generate_heights_from_chunk(self, chunk, sidegenerated):
displace = self.biome["displacement"]
floor = self.biome["maxelevation"]
ceiling = self.biome["minelevation"]
tempheights = [0, 0]
if sidegenerated == LEFT:
#Pre-existing chunk is on the left side
lefth = int(chunk.heights[WIDTH - 1])
righth = random.randint(lefth - self.biome["slope"], lefth + self.biome["slope"])
righth = min(righth, ceiling)
righth = max(righth, floor)
else:
#Pre-existing chunk is on the right side
righth = int(chunk.heights[0])
lefth = random.randint(righth - self.biome["slope"], righth + self.biome["slope"])
lefth = min(lefth, ceiling)
lefth = max(lefth, floor)
tempheights[0] = lefth
tempheights[1] = righth
#Midpoint Displacement Algorithm
while len(tempheights) < WIDTH:
newpoints = []
for i in range(len(tempheights) - 1):
midpoint = (tempheights[i] + tempheights[i + 1]) / 2
midpoint += random.randint(-int(displace), int(displace))
midpoint = min(midpoint, ceiling)
midpoint = max(midpoint, floor)
displace /= 2
newpoints.append(int(tempheights[i]))
newpoints.append(int(midpoint))
newpoints.append(int(tempheights[-1])) #add the last point which wasn't counted
tempheights = newpoints
self.heights = tempheights
def midpoint_displace(self, lst):
displace = self.biome["displacement"]
floor = self.biome["maxelevation"]
ceiling = self.biome["minelevation"]
while len(lst) < WIDTH:
newpoints = []
for i in range(len(lst) - 1):
midpoint = (lst[i] + lst[i + 1]) / 2
midpoint += random.randint(-int(displace), int(displace))
midpoint = min(midpoint, ceiling)
midpoint = max(midpoint, floor)
displace /= 2
newpoints.append(int(lst[i]))
newpoints.append(int(midpoint))
newpoints.append(int(lst[-1])) #add the last point which wasn't counted
lst = newpoints
return lst
def populate(self):
#Fill in blocks based on heights
for y in range(len(self.blocks)):
for x in range(len(self.blocks[y])):
surface_depth = self.heights[x] + 2 + random.randrange(4)
if y < World.SEA_LEVEL:
self.blocks[y][x] = World.get_block("air")
elif y < self.heights[x]:
self.blocks[y][x] = World.get_block("water")
elif y < surface_depth:
#for some reason this sometimes makes base blocks above surface blocks, but it looks cool so I'll probably leave it
self.blocks[y][x] = World.get_block(self.biome["surface"])
else:
self.blocks[y][x] = World.get_block(self.biome["base"])
self.decorate()
def decorate(self):
for x in range(WIDTH):
for structure_name in self.biome["structures"]:
structure = World.structures[structure_name]
if random.random() < structure["frequency"]:
self.generate_structure(structure, x)
break #can only have one structure at a given x
def generate_structure(self, structure, x):
if structure["type"] == "column":
height = random.randint(structure["minheight"], structure["maxheight"])
for y in range(self.heights[x] - height, self.heights[x]):
self.blocks[y][x] = World.get_block(structure["block"])
elif structure["type"] == "json":
structure_file = open(structure["location"])
structure_json = json.load(structure_file)
curr_y = self.heights[x] - len(structure_json["shape"])
for line in structure_json["shape"]:
curr_world_x = Convert.chunk_to_world(x, self)
for char in line:
#find the right chunk
chunk = self #world.chunks[Convert.world_to_chunk(x)[1]]- can't really do this...
curr_chunk_x = Convert.world_to_chunk(curr_world_x)[0]
if curr_chunk_x < WIDTH:
if char == " ":
block = "water"
else:
block = structure_json["blocks"][char]
chunk.blocks[curr_y][curr_chunk_x] = World.get_block(block)
curr_world_x += 1
curr_y += 1
structure_file.close()
elif structure["type"] == "other":
#why did I write this?
pass
def render(self, screen, viewport):
top = max(Convert.pixel_to_world(viewport.y), 0)
bottom = min(Convert.pixel_to_world(viewport.y + viewport.height) + 1, World.HEIGHT)
for blocky in range(top, bottom):
leftData = Convert.pixel_to_chunk(viewport.x)
rightData = Convert.pixel_to_chunk(viewport.x + viewport.width)
if leftData[1] == self.x:
for blockx in range(leftData[0], WIDTH):
self.render_block(self.blocks[blocky][blockx], (blockx, blocky), screen, viewport)
elif leftData[1] < self.x < rightData[1]:
for blockx in range(WIDTH):
self.render_block(self.blocks[blocky][blockx], (blockx, blocky), screen, viewport)
elif self.x == rightData[1]:
for blockx in range(0, rightData[0] + 1):
self.render_block(self.blocks[blocky][blockx], (blockx, blocky), screen, viewport)
for entity in self.entities:
entity.render(screen, Convert.world_to_viewport(entity.pos, viewport))
def render_block(self, block, pos, screen, viewport):
"""#fast render water
if block["name"] == "water":
screen.blit(World.block_images[block["id"]],
Convert.world_to_viewport([Convert.chunk_to_world(pos[0], self), pos[1]], viewport))"""
#don't render air
if block["name"] != "air":
#print(block["name"] + " " + str(block["id"]))
Game.get_world().render_block(block["id"], [Convert.chunk_to_world(pos[0], self), pos[1]], block["connectedTexture"], screen, viewport)
if Game.DEBUG:
#draw bounding box
pygame.draw.rect(screen, Game.BLACK, pygame.Rect(Convert.chunk_to_viewport(pos, self, viewport), (Game.BLOCK_SIZE * Game.SCALE, Game.BLOCK_SIZE * Game.SCALE)), 1)
def __str__(self):
return "Chunk at x=" + str(self.x) + " contains entities " + str(self.entities)
if __name__ == "__main__":
Game.main()