forked from huu-bo/micg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.py
201 lines (163 loc) · 6.75 KB
/
block.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
import os
import json
def load(directory='blocks/'):
out = {}
for filename in os.listdir(directory):
with open(directory + filename) as file:
out[filename.split('.')[0]] = json.load(file)
return out
class block:
def __init__(self, name, blocks):
self.blocks = blocks
self.support = 0
self.name = name
self.x = None # should be set by noise.world.set()
self.y = None
self.on_floor = False # solid block under this block, only used if self.h_support
if name in blocks:
if 'solid' in blocks[name]: # there has got to be a better way to do this
self.solid = blocks[name]['solid']
else:
print(name, "doesn't have property 'solid'")
if 'render' in blocks[name]:
self.render = blocks[name]['render']
else:
print(name, "doesn't have property 'render'")
# optional parameters
if 'color' in blocks[name]:
self.color = tuple(blocks[name]['color'])
else:
self.color = (0, 255, 0)
if 'max support' in blocks[name]:
self.max_support = blocks[name]['max support']
else:
self.max_support = 10
if 'gravity' in blocks[name]:
self.gravity = blocks[name]['gravity']
else:
self.gravity = True
if 'h support' in blocks[name]:
self.h_support = blocks[name]['h support']
else:
self.h_support = False
# template
# if 'p' in blocks[name]:
# self.p = blocks[name]['p']
# else:
# print(name, "doesn't have property 'p'")
else:
print('invalid block name:', name)
self.solid = True
self.render = True
self.color = (255, 30, 30)
self.max_support = 1
self.gravity = True
self.h_support = False
def update(self, world):
if self.x is None or not self.solid or not self.gravity:
return []
pre_x = self.x
pre_y = self.y
moved = False
if world.get(self.x, self.y + 1).solid and not self.on_floor and self.gravity:
moved = True
self.on_floor = world.get(self.x, self.y + 1).on_floor
if (world.get(self.x - 1, self.y).solid or world.get(self.x + 1, self.y).solid) and\
not world.get(self.x, self.y + 1).solid:
self.on_floor = world.get(self.x - 1, self.y).on_floor or world.get(self.x + 1, self.y).on_floor
if not self.on_floor and self.h_support:
self.support += 1
if (not world.get(self.x, self.y + 1).solid) and\
((not world.get(self.x - 1, self.y).solid) and (not world.get(self.x + 1, self.y).solid) or not self.h_support):
world.set(self.x, self.y, block('air', self.blocks))
self.y += 1
world.set(self.x, self.y, self)
moved = True
else:
if world.get(self.x, self.y - 1).solid:
if self.support != world.get(self.x, self.y - 1).support + 1:
moved = True
self.support = world.get(self.x, self.y - 1).support + 1
elif world.get(self.x + 1, self.y).solid and self.h_support: # TODO: directional
if self.support != world.get(self.x + 1, self.y).support + 1:
moved = True
self.support = world.get(self.x + 1, self.y).support + 1
elif world.get(self.x - 1, self.y).solid and self.h_support:
if self.support != world.get(self.x - 1, self.y).support + 1:
moved = True
self.support = world.get(self.x - 1, self.y).support + 1
else:
if self.support != 0:
moved = True
self.support = 0
if self.support > self.max_support:
if not world.get(self.x + 1, self.y + 1).solid:
world.set(self.x, self.y, block('air', self.blocks))
self.y += 1
self.x += 1
world.set(self.x, self.y, self)
moved = True
elif not world.get(self.x - 1, self.y + 1).solid:
world.set(self.x, self.y, block('air', self.blocks))
self.y += 1
self.x -= 1
world.set(self.x, self.y, self)
moved = True
if moved:
if self.solid:
assert self.name != 'air', 'wtf'
y = self.y + 1
if world.get(self.x, self.y - 1).solid:
self.support = world.get(self.x, self.y - 1).support + 1
else:
self.support = 0
support = self.support
while world.get(self.x, self.y).solid and y > 1:
support += 1
world.get(self.x, y).support = support
y += 1
if y > 40:
break
# update neighbouring blocks
return [world.get(self.x - 1, self.y), world.get(self.x, self.y - 1),
world.get(self.x + 1, self.y), world.get(self.x, self.y + 1),
world.get(self.x - 1, self.y - 1), world.get(self.x + 1, self.y - 1),
world.get(self.x - 1, self.y + 1), world.get(self.x + 1, self.y + 1),
world.get(pre_x - 1, pre_y),
world.get(pre_x + 1, pre_y), world.get(pre_x, pre_y - 1),
world.get(pre_x - 1, pre_y - 1), world.get(pre_x + 1, pre_y - 1),
world.get(pre_x - 1, pre_y + 1), world.get(pre_x + 1, pre_y + 1),
self]
else:
return []
def craft(pi, b, f, blocks, amount=1):
if b not in blocks:
print(b, 'does not exist')
return
if 'craft' not in blocks[b]:
print(b, 'not craftable')
return
if f not in blocks[b]['craft']:
print('cannot craft', b, 'from', f)
return
if pi[f] < amount * blocks[b]['craft'][f]:
print('not enough', f, 'amount:', pi[f], 'amount needed:', amount * blocks[b]['craft'][f])
return
pi[f] -= amount * blocks[b]['craft'][f]
pi[b] += amount
return
def recipies(b, blocks):
if b not in blocks:
return False
if 'craft' not in blocks[b]:
return False
return blocks[b]['craft']
def color(b, blocks):
if b in blocks:
if 'color' in blocks[b]:
c = blocks[b]['color']
else:
c = (0, 255, 0)
else:
c = (255, 10, 10)
return c