Update to use gdpc Transform (simplifies a lot)

This commit is contained in:
AKreuzer
2024-05-20 20:47:12 +02:00
parent fb6206c52f
commit 7071b0a81c
10 changed files with 84 additions and 114 deletions

View File

@@ -17,7 +17,7 @@ class Building:
is_inner_or_outer = COLLUMN_STYLE.BOTH is_inner_or_outer = COLLUMN_STYLE.BOTH
self.foundations = Foundations(position, size, matrice, tile_size, is_collumn_full_tile, is_inner_or_outer) self.foundations = Foundations(size, matrice, tile_size, is_collumn_full_tile, is_inner_or_outer)
self.facade = Facade(self.foundations.vertices, floor_height, is_inner_or_outer) self.facade = Facade(self.foundations.vertices, floor_height, is_inner_or_outer)
def gen_tile_size(self) -> int: def gen_tile_size(self) -> int:

View File

@@ -1,49 +1,35 @@
import random as rd import random as rd
from utils.Enums import COLLUMN_STYLE, DIRECTION from utils.Enums import COLLUMN_STYLE
from gdpc import Editor from gdpc import Editor, Transform
from buildings.geometry.Vertice import Vertice from buildings.geometry.Vertice import Vertice
from buildings.geometry.Rectangle import Rectangle
from buildings.elements.Window import Window from buildings.elements.Window import Window
class Facade: class Facade:
def __init__(self, rdata, vertices : list[Vertice], height : int, lenght : int, is_inner_or_outer : COLLUMN_STYLE): def __init__(self, rdata, vertices : list[Vertice], height : int, length : int, is_inner_or_outer : COLLUMN_STYLE):
self.rdata = rdata self.rdata = rdata
self.vertices = vertices self.vertices = vertices
self.is_inner_or_outer = is_inner_or_outer self.is_inner_or_outer = is_inner_or_outer
self.height = height self.height = height
self.lenght = lenght self.length = length
self.window_size = self.get_window_size() self.padding = 0
self.window = self.get_window() self.window = self.get_window()
self.has_balcony = self.has_balcony() self.has_balcony = self.has_balcony()
self.has_inter_floor = self.has_inter_floor() self.has_inter_floor = self.has_inter_floor()
def build(self, editor : Editor, materials : list[str], y : int): def build(self, editor : Editor, materials : list[str]):
padding = 0
if self.is_inner_or_outer == COLLUMN_STYLE.OUTER or self.is_inner_or_outer == COLLUMN_STYLE.BOTH:
padding = 1
for vertice in self.vertices: for vertice in self.vertices:
xpadding, zpadding = 0, 0 vertice.fill(editor, materials[0], self.height, xpadding = self.padding, zpadding = self.padding)
if vertice.facing == DIRECTION.NORTH or vertice.facing == DIRECTION.SOUTH: with editor.pushTransform(Transform(vertice.point1.position,rotation = vertice.facing.value)):
xpadding = padding self.window.build(editor, vertice.get_len(), self.height, materials)
else: zpadding = padding
vertice.fill(editor, materials[0], y, y + self.height, xpadding = xpadding, zpadding = zpadding)
self.window.build(editor, vertice, self.height, y, materials)
def get_window_size(self) -> tuple[int,int]:
max_width = self.lenght
max_height = min(self.height, self.rdata["windows"]["size"]["max_height"])
if self.is_inner_or_outer == COLLUMN_STYLE.OUTER or self.is_inner_or_outer == COLLUMN_STYLE.BOTH:
max_width -= 2
return (
rd.randint(self.rdata["windows"]["size"]["min_width"],max_width),
rd.randint(self.rdata["windows"]["size"]["min_height"],max_height)
)
def get_window(self) -> Window: def get_window(self) -> Window:
return Window(self.rdata["windows"] ,self.window_size) if self.is_inner_or_outer == COLLUMN_STYLE.OUTER or self.is_inner_or_outer == COLLUMN_STYLE.BOTH:
self.padding = 1
max_width = self.length-2*self.padding
max_height = min(self.height, self.rdata["windows"]["size"]["max_height"])
return Window(self.rdata["windows"] ,max_width, max_height)
def has_balcony(self) -> bool: def has_balcony(self) -> bool:
pass pass

View File

@@ -12,7 +12,6 @@ class Foundations:
# TODO : gérer les collones sur les tiles trop petites et les colones 1tile/2 + fulltile # TODO : gérer les collones sur les tiles trop petites et les colones 1tile/2 + fulltile
def __init__(self, def __init__(self,
position : tuple[int,int],
size : tuple[int, int], size : tuple[int, int],
matrice : list[list[int]], matrice : list[list[int]],
tile_size : int, tile_size : int,
@@ -25,11 +24,8 @@ class Foundations:
self.is_collumn_full_tile = is_collumn_full_tile self.is_collumn_full_tile = is_collumn_full_tile
self.is_inner_or_outer = is_inner_or_outer self.is_inner_or_outer = is_inner_or_outer
x,z = position
self.position = Point(x = x, z = z)
self.size = size self.size = size
self.length = size[0] self.length, self.width = size
self.width = size[1]
self.matrice = matrice self.matrice = matrice
self.tiles = [] self.tiles = []
self.vertices = [] self.vertices = []
@@ -45,7 +41,7 @@ class Foundations:
def get_polygon(self) -> Polygon: def get_polygon(self) -> Polygon:
## The polygon is a shape of tiles representing the foundation shape ## The polygon is a shape of tiles representing the foundation shape
polygon = Polygon(self.position, self.size) polygon = Polygon(self.size)
avaliable_space = (self.length_in_tiles, self.width_in_tiles) avaliable_space = (self.length_in_tiles, self.width_in_tiles)
# we save the distribution, usefull for the next steps # we save the distribution, usefull for the next steps
@@ -53,10 +49,10 @@ class Foundations:
self.z_distribution = self.get_distribution(len(self.matrice[0]), avaliable_space[1]) self.z_distribution = self.get_distribution(len(self.matrice[0]), avaliable_space[1])
# this bullshit is to create tiles from the matrice and the distribution # this bullshit is to create tiles from the matrice and the distribution
x_padding = self.position.x x_padding = 0
for x,xsize in utils.Enumerate(self.x_distribution): for x,xsize in enumerate(self.x_distribution):
z_padding = self.position.z z_padding = 0
for z,zsize in utils.Enumerate(self.z_distribution): for z,zsize in enumerate(self.z_distribution):
if self.matrice[x][z] == 1: if self.matrice[x][z] == 1:
for xi in range(xsize): for xi in range(xsize):
for zi in range(zsize): for zi in range(zsize):
@@ -131,7 +127,7 @@ class Foundations:
return self._suppr_doubblons_collumns(collumns) return self._suppr_doubblons_collumns(collumns)
def _suppr_doubblons_collumns(self, collumns : list[Collumn]): def _suppr_doubblons_collumns(self, collumns : list[Collumn]):
for index,collumn in utils.Enumerate(collumns): for index,collumn in enumerate(collumns):
if index == len(collumns)-1: break if index == len(collumns)-1: break
for compare in collumns[index+1:]: for compare in collumns[index+1:]:
if collumn.point1.position == compare.point1.position : if collumn.point1.position == compare.point1.position :

View File

@@ -1,14 +1,14 @@
import random as rd import random as rd
import math import math
from gdpc import Editor, Block, geometry from gdpc import Editor, Block, geometry, Transform
from utils.Enums import DIRECTION from utils.Enums import COLLUMN_STYLE
from buildings.geometry.Point import Point from buildings.geometry.Point import Point
from buildings.geometry.Vertice import Vertice from buildings.geometry.Vertice import Vertice
class Window: class Window:
def __init__(self, rdata, size : tuple[int,int]): def __init__(self, rdata, max_width : int, max_height : int):
self.rdata = rdata self.rdata = rdata
self.width, self.height = size self.width, self.height = self.get_size(max_width, max_height)
self.is_grounded = self.is_grounded() self.is_grounded = self.is_grounded()
self.has_multiple_windows = self.has_multiple_windows() self.has_multiple_windows = self.has_multiple_windows()
self.is_alternate = self.is_alternate() self.is_alternate = self.is_alternate()
@@ -16,26 +16,22 @@ class Window:
self.padding = 0 self.padding = 0
self.editor, self.materials = None,None self.editor, self.materials = None,None
def build(self, editor : Editor, vertice : Vertice, height : int, y : int, materials : list[str]): def build(self, editor : Editor, facade_len : int, facade_height : int, materials : list[str]):
self.editor = editor self.editor = editor
self.materials = materials self.materials = materials
len = vertice.get_size() # correction to avoid asymetry
self.padding = (len - self.width)//2 self.padding = (facade_len - self.width)//2
self.width = len - self.padding*2 self.width = facade_len - self.padding*2
self.is_alternate = True self.is_alternate = True
if not self.is_grounded: y += (height - self.height)//2 if not self.is_grounded: editor.transform @= Transform((0,(facade_height-self.height)//2,0))
if self.has_multiple_windows: self.build_multiple_windows(vertice, y) if self.has_multiple_windows: self.build_multiple_windows()
else : else :
xpadding, zpadding = self.padding, self.padding self.place_glasses(self.padding, self.width+self.padding)
if vertice.facing == DIRECTION.NORTH or vertice.facing == DIRECTION.SOUTH: zpadding = 0
else: xpadding = 0
self.place_glasses(Point(vertice.point1.x+xpadding, y, vertice.point1.z+zpadding), def build_multiple_windows(self):
Point(vertice.point2.x-xpadding, y+self.height, vertice.point2.z-zpadding))
def build_multiple_windows(self, vertice : Vertice, y : int):
slices = rd.randint(3, self.width//self.rdata["size"]["min_width"]) slices = rd.randint(3, self.width//self.rdata["size"]["min_width"])
mid = math.ceil(slices/2) mid = math.ceil(slices/2)
windows_count = mid windows_count = mid
@@ -53,46 +49,41 @@ class Window:
# kepp a spacing between windows, "is revert" is used to keep symetry # kepp a spacing between windows, "is revert" is used to keep symetry
if is_window: if is_window:
#set the values to orient windows in x or z axis x= self.padding + gap
xpadding,xlen,zpadding,zlen = 0,0,0,0 self.place_glasses(x, x+wsize)
if vertice.facing == DIRECTION.NORTH or vertice.facing == DIRECTION.SOUTH:
xpadding,xlen = self.padding + gap, wsize-1
else: zpadding,zlen = self.padding + gap, wsize-1
self.place_glasses(Point(vertice.point1.x+xpadding, y, vertice.point1.z+zpadding),
Point(vertice.point1.x+xpadding+xlen, y+self.height, vertice.point1.z+zpadding+zlen))
gap += wsize gap += wsize
else : else :
gap += isize gap += isize
is_window = not is_window is_window = not is_window
def place_glasses(self, pos1 : Point, pos2 : Point): def place_glasses(self, x1 : int, x2 : int):
len = x2 - x1
xlen, zlen = pos2.x - pos1.x, pos2.z - pos1.z
len = xlen + zlen
if self.is_alternate: if self.is_alternate:
mid = len//2 + 1 mid = x1 + len//2
is_block, is_even = False, len % 2 == 1 # yeah the result isn't actually even but it's because either xlen or zlen is 1, we want to know of the other result is even is_block, is_even = False, len % 2 == 0
for x in range(xlen+1): for x in range(x1,x2):
for z in range(zlen+1): if is_even and x == mid: is_block = not is_block # to keep symetry
if is_even and (x+z) == mid: is_block = not is_block # to keep symetry
id = 1 if not is_block else 2 id = 1 if not is_block else 2
geometry.placeCuboid(self.editor,(pos1.x+x,pos1.y,pos1.z+z),(pos1.x+x,pos2.y,pos1.z+z),Block(self.materials[id])) geometry.placeCuboid(self.editor,(x,0,0),(x,self.height,0),Block(self.materials[id]))
is_block = not is_block is_block = not is_block
else: else:
geometry.placeCuboid(self.editor,pos1.position,pos2.position,Block(self.materials[1])) geometry.placeCuboid(self.editor,(x1,0,0),(x2,self.height,0),Block(self.materials[1]))
self.build_crossbars(pos1, pos2, len) self.build_crossbars(x1, x2-1, len)
def get_size(self, max_width : int ,max_height : int) -> tuple[int,int]:
return (
rd.randint(self.rdata["size"]["min_width"],max_width),
rd.randint(self.rdata["size"]["min_height"],max_height)
)
def build_crossbars(self, pos1 : Point, pos2 : Point, len : int): def build_crossbars(self, x1 : int, x2 : int, len : int):
if self.has_vertical_crossbar and self.height >= self.rdata["crossbars"]["min_height_for_vertical_crossbar"]: if self.has_vertical_crossbar and self.height >= self.rdata["crossbars"]["min_height_for_vertical_crossbar"]:
print(pos1.x,pos2.x)
y = self.height//2 y = self.height//2
geometry.placeCuboid(self.editor,(pos1.x,pos1.y+y,pos1.z),(pos2.x,pos2.y-y,pos2.z),Block(self.materials[3])) geometry.placeCuboid(self.editor,(x1,y,0),(x2,self.height-y,0),Block(self.materials[3]))
if self.has_horizontal_crossbar and len >= self.rdata["crossbars"]["min_width_for_horizontal_crossbar"]: if self.has_horizontal_crossbar and len >= self.rdata["crossbars"]["min_width_for_horizontal_crossbar"]:
pass pass

View File

@@ -6,8 +6,7 @@ from buildings.geometry.Rectangle import Rectangle
from buildings.geometry.Vertice import Vertice from buildings.geometry.Vertice import Vertice
class Polygon: class Polygon:
def __init__(self, position : Point, size: tuple[int,int]): def __init__(self, size: tuple[int,int]):
self.position = position
self.size = size self.size = size
self.shape = [] self.shape = []
self.vertices = [] self.vertices = []
@@ -70,7 +69,7 @@ class Polygon:
def set_vertices_and_neighbors(self, tiles : list[Tile], vertices : list[Vertice]): def set_vertices_and_neighbors(self, tiles : list[Tile], vertices : list[Vertice]):
for tile in tiles: for tile in tiles:
targets = tile.get_neighbors_coords() targets = tile.get_neighbors_coords()
for vertice_num,target in utils.Enumerate(targets): for vertice_num,target in enumerate(targets):
has_neighbor = self._has_neighbor(target, tiles) has_neighbor = self._has_neighbor(target, tiles)
if not has_neighbor: if not has_neighbor:
vertice = tile.get_vertice(vertice_num) vertice = tile.get_vertice(vertice_num)

View File

@@ -9,9 +9,8 @@ class Rectangle:
def get_position(self): def get_position(self):
return (self.point1.position, self.point2.position) return (self.point1.position, self.point2.position)
def fill(self,editor : Editor, material : str, y : int, y2 : int = None, xpadding : int = 0, zpadding : int = 0): def fill(self,editor : Editor, material : str, y : int = None, xpadding : int = 0, zpadding : int = 0):
if self.point2.x - self.point1.x < 2*xpadding: xpadding = 0 if self.point2.x - self.point1.x < 2*xpadding: xpadding = 0
if self.point2.z - self.point1.z < 2*zpadding: zpadding = 0 if self.point2.z - self.point1.z < 2*zpadding: zpadding = 0
if y2 == None: y2 = y
geometry.placeCuboid(editor, (self.point1.x+xpadding, y, self.point1.z+zpadding), (self.point2.x-xpadding, y2, self.point2.z-zpadding), Block(material)) geometry.placeCuboid(editor, (self.point1.x+xpadding, 0, self.point1.z+zpadding), (self.point2.x-xpadding, y, self.point2.z-zpadding), Block(material))

View File

@@ -27,15 +27,14 @@ class Tile:
self.north_vertice = None self.north_vertice = None
self.south_vertice = None self.south_vertice = None
def fill(self, editor : Editor, material : str, y : int, y2 : int = None) -> list[Point]: def fill(self, editor : Editor, material : str, y : int = 0) -> list[Point]:
if y2 == None: y2 = y geometry.placeCuboid(editor, (self.pos.x, 0, self.pos.z), (self.pos.x+self.size-1, y, self.pos.z+self.size-1), Block(material))
geometry.placeCuboid(editor, (self.pos.x, y, self.pos.z), (self.pos.x+self.size-1, y2, self.pos.z+self.size-1), Block(material))
def get_neighbors_coords(self): def get_neighbors_coords(self):
return [Point(x = self.pos.x - self.size, z = self.pos.z), # west return [Point(x = self.pos.x, z = self.pos.z - self.size), # north
Point(x = self.pos.x + self.size, z = self.pos.z), # east Point(x = self.pos.x - self.size, z = self.pos.z), # west
Point(x = self.pos.x, z = self.pos.z - self.size), # north Point(x = self.pos.x, z = self.pos.z + self.size), # south
Point(x = self.pos.x, z = self.pos.z + self.size)] # south Point(x = self.pos.x + self.size, z = self.pos.z)] # east
def get_neighbor(self, direction) -> Point: def get_neighbor(self, direction) -> Point:
@@ -62,16 +61,16 @@ class Tile:
def get_vertice(self,vertice : int|DIRECTION) -> Vertice: def get_vertice(self,vertice : int|DIRECTION) -> Vertice:
# gives the corresponding vertice : # gives the corresponding vertice :
# 0 = west, 1 = east, 2 = north, 3 = south # 0 = north, 1 = west, 2 = south, 3 = east
match(vertice): match(vertice):
case 0 : case 0 :
return Vertice(self.north_west, self.south_west, DIRECTION.WEST)
case 1 :
return Vertice(self.north_east, self.south_east, DIRECTION.EAST)
case 2 :
return Vertice(self.north_west, self.north_east, DIRECTION.NORTH) return Vertice(self.north_west, self.north_east, DIRECTION.NORTH)
case 3 : case 1 :
return Vertice(self.north_west, self.south_west, DIRECTION.WEST)
case 2 :
return Vertice(self.south_west, self.south_east, DIRECTION.SOUTH) return Vertice(self.south_west, self.south_east, DIRECTION.SOUTH)
case 3 :
return Vertice(self.north_east, self.south_east, DIRECTION.EAST)
case DIRECTION.WEST : case DIRECTION.WEST :
return self.west_vertice return self.west_vertice
case DIRECTION.EAST : case DIRECTION.EAST :

View File

@@ -16,6 +16,6 @@ class Vertice(Rectangle):
return [Point(x = self.point1.x, z = self.point1.z - 1), return [Point(x = self.point1.x, z = self.point1.z - 1),
Point(x = self.point2.x, z = self.point2.z + 1)] Point(x = self.point2.x, z = self.point2.z + 1)]
def get_size(self): def get_len(self):
return self.point2.x - self.point1.x + self.point2.z - self.point1.z + 1 return self.point2.x - self.point1.x + self.point2.z - self.point1.z + 1

View File

@@ -18,7 +18,7 @@ shapes = f.data
y = YamlReader('params.yml') y = YamlReader('params.yml')
random_data = y.data random_data = y.data
transform = Transform((-2,0,-5),rotation = 3) transform = Transform((0,-60,0),rotation = 0)
editor.transform.push(transform) editor.transform.push(transform)
geometry.placeCuboid(editor, (0,-60,-5), (100,-45,-5), Block("air")) geometry.placeCuboid(editor, (0,-60,-5), (100,-45,-5), Block("air"))
@@ -31,7 +31,7 @@ for i in range(3,13):
x += i+2 x += i+2
for f in facade: for f in facade:
f.build(editor, ["stone_bricks","glass_pane","glass","cobblestone_wall"], -60) f.build(editor, ["stone_bricks","glass_pane","glass","cobblestone_wall"])
# F = Foundations((0,0), (20,20), shapes[0]['matrice']) # F = Foundations((0,0), (20,20), shapes[0]['matrice'])

View File

@@ -1,10 +1,10 @@
from enum import Enum from enum import Enum
class DIRECTION(Enum): class DIRECTION(Enum):
WEST = 0 NORTH = 0
EAST = 1 WEST = 1
NORTH = 2 SOUTH = 2
SOUTH = 3 EAST = 3
class COLLUMN_STYLE(Enum): class COLLUMN_STYLE(Enum):
NONE = 0 NONE = 0