From 7071b0a81c3372fe1390255d2d2129acacd78013 Mon Sep 17 00:00:00 2001 From: AKreuzer Date: Mon, 20 May 2024 20:47:12 +0200 Subject: [PATCH] Update to use gdpc Transform (simplifies a lot) --- buildings/Building.py | 2 +- buildings/Facade.py | 46 +++++++------------ buildings/Foundations.py | 20 ++++---- buildings/elements/Window.py | 81 +++++++++++++++------------------ buildings/geometry/Polygon.py | 5 +- buildings/geometry/Rectangle.py | 5 +- buildings/geometry/Tile.py | 25 +++++----- buildings/geometry/Vertice.py | 2 +- main.py | 4 +- utils/Enums.py | 8 ++-- 10 files changed, 84 insertions(+), 114 deletions(-) diff --git a/buildings/Building.py b/buildings/Building.py index 5a7b740..5df0fc1 100644 --- a/buildings/Building.py +++ b/buildings/Building.py @@ -17,7 +17,7 @@ class Building: 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) def gen_tile_size(self) -> int: diff --git a/buildings/Facade.py b/buildings/Facade.py index daaa962..e542bb1 100644 --- a/buildings/Facade.py +++ b/buildings/Facade.py @@ -1,49 +1,35 @@ import random as rd -from utils.Enums import COLLUMN_STYLE, DIRECTION -from gdpc import Editor +from utils.Enums import COLLUMN_STYLE +from gdpc import Editor, Transform from buildings.geometry.Vertice import Vertice -from buildings.geometry.Rectangle import Rectangle from buildings.elements.Window import Window 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.vertices = vertices self.is_inner_or_outer = is_inner_or_outer self.height = height - self.lenght = lenght - self.window_size = self.get_window_size() + self.length = length + self.padding = 0 self.window = self.get_window() self.has_balcony = self.has_balcony() self.has_inter_floor = self.has_inter_floor() - def build(self, editor : Editor, materials : list[str], y : int): - padding = 0 - if self.is_inner_or_outer == COLLUMN_STYLE.OUTER or self.is_inner_or_outer == COLLUMN_STYLE.BOTH: - padding = 1 - + def build(self, editor : Editor, materials : list[str]): for vertice in self.vertices: - xpadding, zpadding = 0, 0 - if vertice.facing == DIRECTION.NORTH or vertice.facing == DIRECTION.SOUTH: - xpadding = padding - 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) - ) + vertice.fill(editor, materials[0], self.height, xpadding = self.padding, zpadding = self.padding) + with editor.pushTransform(Transform(vertice.point1.position,rotation = vertice.facing.value)): + self.window.build(editor, vertice.get_len(), self.height, materials) 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: pass diff --git a/buildings/Foundations.py b/buildings/Foundations.py index 76c5eab..51a365e 100644 --- a/buildings/Foundations.py +++ b/buildings/Foundations.py @@ -11,8 +11,7 @@ from buildings.elements.Collumn import Collumn class Foundations: # TODO : gérer les collones sur les tiles trop petites et les colones 1tile/2 + fulltile - def __init__(self, - position : tuple[int,int], + def __init__(self, size : tuple[int, int], matrice : list[list[int]], tile_size : int, @@ -25,11 +24,8 @@ class Foundations: self.is_collumn_full_tile = is_collumn_full_tile self.is_inner_or_outer = is_inner_or_outer - x,z = position - self.position = Point(x = x, z = z) self.size = size - self.length = size[0] - self.width = size[1] + self.length, self.width = size self.matrice = matrice self.tiles = [] self.vertices = [] @@ -45,7 +41,7 @@ class Foundations: def get_polygon(self) -> Polygon: ## 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) # 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]) # this bullshit is to create tiles from the matrice and the distribution - x_padding = self.position.x - for x,xsize in utils.Enumerate(self.x_distribution): - z_padding = self.position.z - for z,zsize in utils.Enumerate(self.z_distribution): + x_padding = 0 + for x,xsize in enumerate(self.x_distribution): + z_padding = 0 + for z,zsize in enumerate(self.z_distribution): if self.matrice[x][z] == 1: for xi in range(xsize): for zi in range(zsize): @@ -131,7 +127,7 @@ class Foundations: return self._suppr_doubblons_collumns(collumns) 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 for compare in collumns[index+1:]: if collumn.point1.position == compare.point1.position : diff --git a/buildings/elements/Window.py b/buildings/elements/Window.py index f44623c..395cc5c 100644 --- a/buildings/elements/Window.py +++ b/buildings/elements/Window.py @@ -1,14 +1,14 @@ import random as rd import math -from gdpc import Editor, Block, geometry -from utils.Enums import DIRECTION +from gdpc import Editor, Block, geometry, Transform +from utils.Enums import COLLUMN_STYLE from buildings.geometry.Point import Point from buildings.geometry.Vertice import Vertice class Window: - def __init__(self, rdata, size : tuple[int,int]): + def __init__(self, rdata, max_width : int, max_height : int): 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.has_multiple_windows = self.has_multiple_windows() self.is_alternate = self.is_alternate() @@ -16,26 +16,22 @@ class Window: self.padding = 0 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.materials = materials - len = vertice.get_size() - self.padding = (len - self.width)//2 - self.width = len - self.padding*2 + # correction to avoid asymetry + self.padding = (facade_len - self.width)//2 + self.width = facade_len - self.padding*2 + 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) - else : - xpadding, zpadding = self.padding, 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), - Point(vertice.point2.x-xpadding, y+self.height, vertice.point2.z-zpadding)) + if self.has_multiple_windows: self.build_multiple_windows() + else : + self.place_glasses(self.padding, self.width+self.padding) - def build_multiple_windows(self, vertice : Vertice, y : int): + def build_multiple_windows(self): slices = rd.randint(3, self.width//self.rdata["size"]["min_width"]) mid = math.ceil(slices/2) windows_count = mid @@ -53,46 +49,41 @@ class Window: # kepp a spacing between windows, "is revert" is used to keep symetry if is_window: - #set the values to orient windows in x or z axis - xpadding,xlen,zpadding,zlen = 0,0,0,0 - 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)) + x= self.padding + gap + self.place_glasses(x, x+wsize) gap += wsize else : gap += isize is_window = not is_window - def place_glasses(self, pos1 : Point, pos2 : Point): - - xlen, zlen = pos2.x - pos1.x, pos2.z - pos1.z - len = xlen + zlen + def place_glasses(self, x1 : int, x2 : int): + len = x2 - x1 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 - for x in range(xlen+1): - for z in range(zlen+1): - if is_even and (x+z) == mid: is_block = not is_block # to keep symetry - 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])) - is_block = not is_block + is_block, is_even = False, len % 2 == 0 + for x in range(x1,x2): + if is_even and x == mid: is_block = not is_block # to keep symetry + id = 1 if not is_block else 2 + geometry.placeCuboid(self.editor,(x,0,0),(x,self.height,0),Block(self.materials[id])) + is_block = not is_block 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) - - - def build_crossbars(self, pos1 : Point, pos2 : Point, len : int): + 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, x1 : int, x2 : int, len : int): 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 - 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"]: pass diff --git a/buildings/geometry/Polygon.py b/buildings/geometry/Polygon.py index 16a03bd..4783523 100644 --- a/buildings/geometry/Polygon.py +++ b/buildings/geometry/Polygon.py @@ -6,8 +6,7 @@ from buildings.geometry.Rectangle import Rectangle from buildings.geometry.Vertice import Vertice class Polygon: - def __init__(self, position : Point, size: tuple[int,int]): - self.position = position + def __init__(self, size: tuple[int,int]): self.size = size self.shape = [] self.vertices = [] @@ -70,7 +69,7 @@ class Polygon: def set_vertices_and_neighbors(self, tiles : list[Tile], vertices : list[Vertice]): for tile in tiles: 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) if not has_neighbor: vertice = tile.get_vertice(vertice_num) diff --git a/buildings/geometry/Rectangle.py b/buildings/geometry/Rectangle.py index 784d1fc..a199982 100644 --- a/buildings/geometry/Rectangle.py +++ b/buildings/geometry/Rectangle.py @@ -9,9 +9,8 @@ class Rectangle: def get_position(self): 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.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)) diff --git a/buildings/geometry/Tile.py b/buildings/geometry/Tile.py index de864da..45f6370 100644 --- a/buildings/geometry/Tile.py +++ b/buildings/geometry/Tile.py @@ -27,15 +27,14 @@ class Tile: self.north_vertice = None self.south_vertice = None - def fill(self, editor : Editor, material : str, y : int, y2 : int = None) -> list[Point]: - if y2 == None: y2 = y - 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 fill(self, editor : Editor, material : str, y : int = 0) -> list[Point]: + 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)) def get_neighbors_coords(self): - return [Point(x = self.pos.x - self.size, z = self.pos.z), # west - Point(x = self.pos.x + self.size, z = self.pos.z), # east - Point(x = self.pos.x, z = self.pos.z - self.size), # north - Point(x = self.pos.x, z = self.pos.z + self.size)] # south + return [Point(x = self.pos.x, z = self.pos.z - self.size), # north + Point(x = self.pos.x - self.size, z = self.pos.z), # west + 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: @@ -62,16 +61,16 @@ class Tile: def get_vertice(self,vertice : int|DIRECTION) -> Vertice: # gives the corresponding vertice : - # 0 = west, 1 = east, 2 = north, 3 = south + # 0 = north, 1 = west, 2 = south, 3 = east match(vertice): 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) - 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) + case 3 : + return Vertice(self.north_east, self.south_east, DIRECTION.EAST) case DIRECTION.WEST : return self.west_vertice case DIRECTION.EAST : diff --git a/buildings/geometry/Vertice.py b/buildings/geometry/Vertice.py index 6740e7a..c576af5 100644 --- a/buildings/geometry/Vertice.py +++ b/buildings/geometry/Vertice.py @@ -16,6 +16,6 @@ class Vertice(Rectangle): return [Point(x = self.point1.x, z = self.point1.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 \ No newline at end of file diff --git a/main.py b/main.py index ecf046d..c6dd711 100644 --- a/main.py +++ b/main.py @@ -18,7 +18,7 @@ shapes = f.data y = YamlReader('params.yml') random_data = y.data -transform = Transform((-2,0,-5),rotation = 3) +transform = Transform((0,-60,0),rotation = 0) editor.transform.push(transform) geometry.placeCuboid(editor, (0,-60,-5), (100,-45,-5), Block("air")) @@ -31,7 +31,7 @@ for i in range(3,13): x += i+2 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']) diff --git a/utils/Enums.py b/utils/Enums.py index 37a15fa..66c55b9 100644 --- a/utils/Enums.py +++ b/utils/Enums.py @@ -1,10 +1,10 @@ from enum import Enum class DIRECTION(Enum): - WEST = 0 - EAST = 1 - NORTH = 2 - SOUTH = 3 + NORTH = 0 + WEST = 1 + SOUTH = 2 + EAST = 3 class COLLUMN_STYLE(Enum): NONE = 0