From 9867909e9fc185a2eafda2294bac02ae5c6ac647 Mon Sep 17 00:00:00 2001 From: Xeon0X Date: Thu, 6 Jun 2024 19:53:00 +0200 Subject: [PATCH] Redo Lane --- networks/roads/Road.py | 36 ++++++++++++++++++++++++++++++++---- networks/roads/lanes/Lane.py | 30 ++++++++++++++++++++---------- networks/roads/lines/Line.py | 35 +++++++++++++---------------------- 3 files changed, 65 insertions(+), 36 deletions(-) diff --git a/networks/roads/Road.py b/networks/roads/Road.py index 1522f6c..0e69f04 100644 --- a/networks/roads/Road.py +++ b/networks/roads/Road.py @@ -1,5 +1,9 @@ import networks.geometry.curve_tools as curve_tools import networks.geometry.Strip as Strip +import networks.roads.lanes.Lane as Lane +import networks.roads.lines.Line as Line +import json +import random from gdpc import Editor, Block, geometry @@ -25,15 +29,39 @@ class Road: for i in range(len(self.curve_surface.curvature)): self.curvature.append((0, 1, 0)) + with open('networks/roads/lanes/lanes.json') as lanes_materials: + lane_type = json.load(lanes_materials).get('classic_lane') + + # for coordinate, block in surface: + # editor.placeBlock(coordinate, Block(block)) + + with open('networks/roads/lines/lines.json') as lines_materials: + line_type = json.load(lines_materials).get('broken_white') + + print(line_type, lane_type) + + # for coordinate, block in surface: + # editor.placeBlock(coordinate, Block(block)) + # Perpendicular self.curve_surface.compute_surface_perpendicular(10, self.curvature) for i in range(len(self.curve_surface.surface)): for j in range(len(self.curve_surface.surface[i])): - # block = random.choice(block_list) for k in range(len(self.curve_surface.surface[i][j])): - editor.placeBlock( - self.curve_surface.surface[i][j][k], Block("blackstone")) - + for l in range(len(self.curve_surface.surface[i][j][k])): + editor.placeBlock( + self.curve_surface.surface[i][j][k][l], Block(random.choices( + list(lane_type.keys()), + weights=lane_type.values(), + k=1,)[0])) + if k == 0: + block = random.choices( + list(pattern_materials[pattern_iteration].keys()), + weights=pattern_materials[pattern_iteration].values( + ), + k=1)[0] + if block != 'None': + self.surface.append((coordinate, block)) # offset = curve.offset(curve_surface.curve, -9, curvature) # for i in range(len(offset)-1): # line = segment.discrete_segment(offset[i], offset[i+1]) diff --git a/networks/roads/lanes/Lane.py b/networks/roads/lanes/Lane.py index 803a0ef..a60031a 100644 --- a/networks/roads/lanes/Lane.py +++ b/networks/roads/lanes/Lane.py @@ -24,15 +24,25 @@ class Lane: for i in range(len(curve_surface.curvature)): normals.append((0, 1, 0)) - # Compute each line - for distance in range(self.width): - offset = curve_tools.offset(curve_surface.curve, distance, normals) - for i in range(len(offset)-1): - line = segment_tools.discrete_segment(offset[i], offset[i+1]) - for coordinate in line: - self.surface.append((coordinate, random.choices( - list(self.lane_materials.keys()), - weights=self.lane_materials.values(), - k=1,))) + # # Compute each line + # for distance in range(self.width): + # offset = curve_tools.offset(curve_surface.curve, distance, normals) + # for i in range(len(offset)-1): + # line = segment_tools.discrete_segment(offset[i], offset[i+1]) + # for coordinate in line: + # self.surface.append((coordinate, random.choices( + # list(self.lane_materials.keys()), + # weights=self.lane_materials.values(), + # k=1,)[0])) + + curve_surface.compute_surface_perpendicular(self.width, normals) + for i in range(len(curve_surface.surface)): + for j in range(len(curve_surface.surface[i])): + for k in range(len(curve_surface.surface[i][j])): + for l in range(len(curve_surface.surface[i][j][k])): + self.surface.append((curve_surface.surface[i][j][k][l], random.choices( + list(self.lane_materials.keys()), + weights=self.lane_materials.values(), + k=1,)[0])) return self.surface diff --git a/networks/roads/lines/Line.py b/networks/roads/lines/Line.py index fb00dfc..13d257e 100644 --- a/networks/roads/lines/Line.py +++ b/networks/roads/lines/Line.py @@ -5,40 +5,31 @@ import random class Line: def __init__(self, coordinates, line_materials): - self.coordinates = coordinates - self.line_materials = line_materials - self.surface = [] - - def get_surface(self): - resolution, distance = curve_tools.resolution_distance( - self.coordinates, 6) - - curve_points = curve_tools.curve(self.coordinates, resolution) - - # Compute the line + self.coordinates = coordinates # Full lines coordinates, not just endpoints + self.line_materials = line_materials # From lines.json + self.coordinates_with_blocks = [] # Output + def get_blocks(self): pattern_length = 0 pattern_materials = [] + # Create the pattern materials list with correct materials depending on the selected pattern. for pattern in self.line_materials: pattern_length += pattern[1] for _ in range(pattern[1]): pattern_materials.append(pattern[0]) pattern_iteration = 0 - for i in range(len(curve_points)-1): - line = segment_tools.discrete_segment( - curve_points[i], curve_points[i+1]) - for coordinate in line: - block = random.choices( - list(pattern_materials[pattern_iteration].keys()), - weights=pattern_materials[pattern_iteration].values(), - k=1)[0] - if block != 'None': - self.surface.append((coordinate, block)) + for coordinate in self.coordinates: + block = random.choices( + list(pattern_materials[pattern_iteration].keys()), + weights=pattern_materials[pattern_iteration].values(), + k=1)[0] + if block != 'None': + self.coordinates_with_blocks.append((coordinate, block)) pattern_iteration += 1 if pattern_iteration >= pattern_length: pattern_iteration = 0 - return self.surface + return self.coordinates_with_blocks