0

textures: clean up chest code

- drop 1.14 compatibility
- code style fixes
This commit is contained in:
Nicolas F
2019-12-27 14:42:55 +01:00
parent 19b5f70fda
commit bdca0b9250

View File

@@ -2179,19 +2179,11 @@ def stairs(self, blockid, data):
# normal, locked (used in april's fool day), ender and trapped chest
# NOTE: locked chest used to be id95 (which is now stained glass)
@material(blockid=[54,130,146], data=list(range(30)), transparent = True)
@material(blockid=[54, 130, 146], data=list(range(30)), transparent = True)
def chests(self, blockid, data):
# the first 3 bits are the orientation as stored in minecraft,
# bits 0x8 and 0x10 indicate which half of the double chest is it.
# This is an annoying little check, but when normal_left.png exists we are using 1.15 textures
# which are different from 1.14 and before.
try:
t = self.load_image("assets/minecraft/textures/entity/chest/normal_left.png")
is115 = True
except (TextureException, IOError):
is115 = False
# first, do the rotation if needed
orientation_data = data & 7
if self.rotation == 1:
@@ -2210,7 +2202,7 @@ def chests(self, blockid, data):
elif orientation_data == 4: data = 3 | (data & 24)
elif orientation_data == 5: data = 2 | (data & 24)
if blockid == 130 and not data in [2,3,4,5]: return None
if blockid == 130 and not data in [2, 3, 4, 5]: return None
# iterate.c will only return the ancil data (without pseudo
# ancil data) for locked and ender chests, so only
# ancilData = 2,3,4,5 are used for this blockids
@@ -2223,63 +2215,61 @@ def chests(self, blockid, data):
except (TextureException, IOError):
t = self.load_image("assets/minecraft/textures/entity/chest/chest.png")
if is115:
t = ImageOps.flip(t) # for some reason the 1.15 images are upside down
# the textures is no longer in terrain.png, get it from
# item/chest.png and get by cropping all the needed stuff
if t.size != (64,64): t = t.resize((64,64), Image.ANTIALIAS)
if t.size != (64, 64): t = t.resize((64, 64), Image.ANTIALIAS)
# top
top = t.crop((28,50,42,64)) if is115 else t.crop((14,0,28,14))
top = t.crop((28, 50, 42, 64))
top.load() # every crop need a load, crop is a lazy operation
# see PIL manual
img = Image.new("RGBA", (16,16), self.bgcolor)
alpha_over(img,top,(1,1))
img = Image.new("RGBA", (16, 16), self.bgcolor)
alpha_over(img, top, (1, 1))
top = img
# front
front_top = t.crop((42,45,56,50)) if is115 else t.crop((14,14,28,19))
front_top = t.crop((42, 45, 56, 50))
front_top.load()
front_bottom = t.crop((42,21,56,31)) if is115 else t.crop((14,34,28,43))
front_bottom = t.crop((42, 21, 56, 31))
front_bottom.load()
front_lock = t.crop((1,59,3,63)) if is115 else t.crop((1,0,3,4))
front_lock = t.crop((1, 59, 3, 63))
front_lock.load()
front = Image.new("RGBA", (16,16), self.bgcolor)
alpha_over(front,front_top, (1,1))
alpha_over(front,front_bottom, (1,5))
alpha_over(front,front_lock, (7,3))
front = Image.new("RGBA", (16, 16), self.bgcolor)
alpha_over(front, front_top, (1, 1))
alpha_over(front, front_bottom, (1, 5))
alpha_over(front, front_lock, (7, 3))
# left side
# left side, right side, and back are esentially the same for
# left side, right side, and back are essentially the same for
# the default texture, we take it anyway just in case other
# textures make use of it.
side_l_top = t.crop((14,45,28,50)) if is115 else t.crop((0,14,14,19))
side_l_top = t.crop((14, 45, 28, 50))
side_l_top.load()
side_l_bottom = t.crop((14,21,28,31)) if is115 else t.crop((0,34,14,43))
side_l_bottom = t.crop((14, 21, 28, 31))
side_l_bottom.load()
side_l = Image.new("RGBA", (16,16), self.bgcolor)
alpha_over(side_l,side_l_top, (1,1))
alpha_over(side_l,side_l_bottom, (1,5))
side_l = Image.new("RGBA", (16, 16), self.bgcolor)
alpha_over(side_l, side_l_top, (1, 1))
alpha_over(side_l, side_l_bottom, (1, 5))
# right side
side_r_top = t.crop((28,45,42,50)) if is115 else t.crop((28,14,43,20))
side_r_top = t.crop((28, 45, 42, 50))
side_r_top.load()
side_r_bottom = t.crop((28,21,42,31)) if is115 else t.crop((28,33,42,43))
side_r_bottom = t.crop((28, 21, 42, 31))
side_r_bottom.load()
side_r = Image.new("RGBA", (16,16), self.bgcolor)
alpha_over(side_r,side_r_top, (1,1))
alpha_over(side_r,side_r_bottom, (1,5))
side_r = Image.new("RGBA", (16, 16), self.bgcolor)
alpha_over(side_r, side_r_top, (1, 1))
alpha_over(side_r, side_r_bottom, (1, 5))
# back
back_top = t.crop((0,45,14,50)) if is115 else t.crop((42,14,56,18))
back_top = t.crop((0, 45, 14, 50))
back_top.load()
back_bottom = t.crop((0,21,14,31)) if is115 else t.crop((42,33,56,43))
back_bottom = t.crop((0, 21, 14, 31))
back_bottom.load()
back = Image.new("RGBA", (16,16), self.bgcolor)
alpha_over(back,back_top, (1,1))
alpha_over(back,back_bottom, (1,5))
back = Image.new("RGBA", (16, 16), self.bgcolor)
alpha_over(back, back_top, (1, 1))
alpha_over(back, back_bottom, (1, 5))
else:
# large chest
# the textures is no longer in terrain.png, get it from
# item/chest.png and get all the needed stuff
if is115:
t_left = self.load_image("assets/minecraft/textures/entity/chest/normal_left.png")
t_right = self.load_image("assets/minecraft/textures/entity/chest/normal_right.png")
# for some reason the 1.15 images are upside down
@@ -2287,114 +2277,85 @@ def chests(self, blockid, data):
t_right = ImageOps.flip(t_right)
# Top
top_left = t_right.crop((29,50,44,64))
top_left = t_right.crop((29, 50, 44, 64))
top_left.load()
top_right = t_left.crop((29,50,44,64))
top_right = t_left.crop((29, 50, 44, 64))
top_right.load()
top = Image.new("RGBA", (32,16), self.bgcolor)
alpha_over(top,top_left,(1,1))
alpha_over(top,top_right,(16,1))
top = Image.new("RGBA", (32, 16), self.bgcolor)
alpha_over(top,top_left, (1, 1))
alpha_over(top,top_right, (16, 1))
# Front
front_top_left = t_left.crop((43,45,58,50))
front_top_left = t_left.crop((43, 45, 58, 50))
front_top_left.load()
front_top_right = t_right.crop((43,45,58,50))
front_top_right = t_right.crop((43, 45, 58, 50))
front_top_right.load()
front_bottom_left = t_left.crop((43,21,58,31))
front_bottom_left = t_left.crop((43, 21, 58, 31))
front_bottom_left.load()
front_bottom_right = t_right.crop((43,21,58,31))
front_bottom_right = t_right.crop((43, 21, 58, 31))
front_bottom_right.load()
front_lock = t_left.crop((1,59,3,63))
front_lock = t_left.crop((1, 59, 3, 63))
front_lock.load()
front = Image.new("RGBA", (32,16), self.bgcolor)
alpha_over(front,front_top_left,(1,1))
alpha_over(front,front_top_right,(16,1))
alpha_over(front,front_bottom_left,(1,5))
alpha_over(front,front_bottom_right,(16,5))
alpha_over(front,front_lock,(15,3))
front = Image.new("RGBA", (32, 16), self.bgcolor)
alpha_over(front, front_top_left, (1, 1))
alpha_over(front, front_top_right, (16, 1))
alpha_over(front, front_bottom_left, (1, 5))
alpha_over(front, front_bottom_right, (16, 5))
alpha_over(front, front_lock, (15, 3))
# Back
back_top_left = t_right.crop((14,45,29,50))
back_top_left = t_right.crop((14, 45, 29, 50))
back_top_left.load()
back_top_right = t_left.crop((14,45,29,50))
back_top_right = t_left.crop((14, 45, 29, 50))
back_top_right.load()
back_bottom_left = t_right.crop((14,21,29,31))
back_bottom_left = t_right.crop((14, 21, 29, 31))
back_bottom_left.load()
back_bottom_right = t_left.crop((14,21,29,31))
back_bottom_right = t_left.crop((14, 21, 29, 31))
back_bottom_right.load()
back = Image.new("RGBA", (32,16), self.bgcolor)
alpha_over(back,back_top_left,(1,1))
alpha_over(back,back_top_right,(16,1))
alpha_over(back,back_bottom_left,(1,5))
alpha_over(back,back_bottom_right,(16,5))
else:
t = self.load_image("assets/minecraft/textures/entity/chest/normal_double.png")
if t.size != (128,64): t = t.resize((128,64), Image.ANTIALIAS)
# top
top = t.crop((14,0,44,14))
top.load()
img = Image.new("RGBA", (32,16), self.bgcolor)
alpha_over(img,top,(1,1))
top = img
# front
front_top = t.crop((14,14,44,18))
front_top.load()
front_bottom = t.crop((14,33,44,43))
front_bottom.load()
front_lock = t.crop((1,0,3,5))
front_lock.load()
front = Image.new("RGBA", (32,16), self.bgcolor)
alpha_over(front,front_top,(1,1))
alpha_over(front,front_bottom,(1,5))
alpha_over(front,front_lock,(15,3))
# back
back_top = t.crop((58,14,88,18))
back_top.load()
back_bottom = t.crop((58,33,88,43))
back_bottom.load()
back = Image.new("RGBA", (32,16), self.bgcolor)
alpha_over(back,back_top,(1,1))
alpha_over(back,back_bottom,(1,5))
back = Image.new("RGBA", (32, 16), self.bgcolor)
alpha_over(back, back_top_left, (1, 1))
alpha_over(back, back_top_right, (16, 1))
alpha_over(back, back_bottom_left, (1, 5))
alpha_over(back, back_bottom_right, (16, 5))
# left side
side_l_top = t_left.crop((29,45,43,50)) if is115 else t.crop((0,14,14,18))
side_l_top = t_left.crop((29, 45, 43, 50))
side_l_top.load()
side_l_bottom = t_left.crop((29,21,43,31)) if is115 else t.crop((0,33,14,43))
side_l_bottom = t_left.crop((29, 21, 43, 31))
side_l_bottom.load()
side_l = Image.new("RGBA", (16,16), self.bgcolor)
alpha_over(side_l,side_l_top, (1,1))
alpha_over(side_l,side_l_bottom,(1,5))
side_l = Image.new("RGBA", (16, 16), self.bgcolor)
alpha_over(side_l, side_l_top, (1, 1))
alpha_over(side_l, side_l_bottom, (1, 5))
# right side
side_r_top = t_right.crop((0,45,14,50)) if is115 else t.crop((44,14,58,18))
side_r_top = t_right.crop((0, 45, 14, 50))
side_r_top.load()
side_r_bottom = t_right.crop((0,21,14,31)) if is115 else t.crop((44,33,58,43))
side_r_bottom = t_right.crop((0, 21, 14, 31))
side_r_bottom.load()
side_r = Image.new("RGBA", (16,16), self.bgcolor)
alpha_over(side_r,side_r_top, (1,1))
alpha_over(side_r,side_r_bottom,(1,5))
side_r = Image.new("RGBA", (16, 16), self.bgcolor)
alpha_over(side_r, side_r_top, (1, 1))
alpha_over(side_r, side_r_bottom, (1, 5))
if data & 24 == 8: # double chest, first half
top = top.crop((0,0,16,16))
top = top.crop((0, 0, 16, 16))
top.load()
front = front.crop((0,0,16,16))
front = front.crop((0, 0, 16, 16))
front.load()
back = back.crop((0,0,16,16))
back = back.crop((0, 0, 16, 16))
back.load()
#~ side = side_l
elif data & 24 == 16: # double, second half
top = top.crop((16,0,32,16))
top = top.crop((16, 0, 32, 16))
top.load()
front = front.crop((16,0,32,16))
front = front.crop((16, 0, 32, 16))
front.load()
back = back.crop((16,0,32,16))
back = back.crop((16, 0, 32, 16))
back.load()
#~ side = side_r
@@ -2402,39 +2363,39 @@ def chests(self, blockid, data):
return None
# compose the final block
img = Image.new("RGBA", (24,24), self.bgcolor)
img = Image.new("RGBA", (24, 24), self.bgcolor)
if data & 7 == 2: # north
side = self.transform_image_side(side_r)
alpha_over(img, side, (1,7))
alpha_over(img, side, (1, 7))
back = self.transform_image_side(back)
alpha_over(img, back.transpose(Image.FLIP_LEFT_RIGHT), (11,7))
alpha_over(img, back.transpose(Image.FLIP_LEFT_RIGHT), (11, 7))
front = self.transform_image_side(front)
top = self.transform_image_top(top.rotate(180))
alpha_over(img, top, (0,2))
alpha_over(img, top, (0, 2))
elif data & 7 == 3: # south
side = self.transform_image_side(side_l)
alpha_over(img, side, (1,7))
alpha_over(img, side, (1, 7))
front = self.transform_image_side(front).transpose(Image.FLIP_LEFT_RIGHT)
top = self.transform_image_top(top.rotate(180))
alpha_over(img, top, (0,2))
alpha_over(img, front,(11,7))
alpha_over(img, top, (0, 2))
alpha_over(img, front, (11, 7))
elif data & 7 == 4: # west
side = self.transform_image_side(side_r)
alpha_over(img, side.transpose(Image.FLIP_LEFT_RIGHT), (11,7))
alpha_over(img, side.transpose(Image.FLIP_LEFT_RIGHT), (11, 7))
front = self.transform_image_side(front)
alpha_over(img, front,(1,7))
alpha_over(img, front, (1, 7))
top = self.transform_image_top(top.rotate(270))
alpha_over(img, top, (0,2))
alpha_over(img, top, (0, 2))
elif data & 7 == 5: # east
back = self.transform_image_side(back)
side = self.transform_image_side(side_l).transpose(Image.FLIP_LEFT_RIGHT)
alpha_over(img, side, (11,7))
alpha_over(img, back, (1,7))
alpha_over(img, side, (11, 7))
alpha_over(img, back, (1, 7))
top = self.transform_image_top(top.rotate(270))
alpha_over(img, top, (0,2))
alpha_over(img, top, (0, 2))
else: # just in case
img = None