Skip to content

Commit

Permalink
Merge pull request #310 from Amulet-Team/impl-1.17
Browse files Browse the repository at this point in the history
Added support for 1.17 worlds
  • Loading branch information
gentlegiantJGC committed Jun 7, 2021
2 parents b494629 + 1cab4ce commit 55b3dec
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 103 deletions.
151 changes: 78 additions & 73 deletions amulet_map_editor/api/opengl/mesh/level/chunk/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def __init__(
region_size: int,
chunk_coords: Tuple[int, int],
dimension: Dimension,
draw_floor: bool = True,
draw_floor: bool = False,
draw_ceil: bool = False,
):
# the chunk geometry is stored in chunk space (floating point)
# at shader time it is transformed by the players transform
Expand All @@ -36,6 +37,7 @@ def __init__(
self._coords = chunk_coords
self._dimension = dimension
self._draw_floor = draw_floor
self._draw_ceil = draw_ceil
self._chunk_state = 0 # 0 = chunk does not exist, 1 = chunk exists but failed to load, 2 = chunk exists
self._changed_time = 0
self._needs_rebuild = True
Expand Down Expand Up @@ -126,40 +128,37 @@ def _sub_chunks(
sub_chunk.shape + numpy.array((2, 2, 2)), sub_chunk.dtype
)
sub_chunk_box = SelectionBox.create_sub_chunk_box(self.cx, cy, self.cz)
if self._level.selection_bounds.intersects(sub_chunk_box):
boxes = self._level.selection_bounds.intersection(sub_chunk_box)
for box in boxes.selection_boxes:
larger_blocks[1:-1, 1:-1, 1:-1][
box.sub_chunk_slice(self.cx, cy, self.cz)
] = sub_chunk[box.sub_chunk_slice(self.cx, cy, self.cz)]
for chunk_offset, neighbour_blocks in neighbour_chunks.items():
if cy not in neighbour_blocks:
continue
if chunk_offset == (-1, 0):
larger_blocks[0, 1:-1, 1:-1] = neighbour_blocks.get_sub_chunk(
cy
)[-1, :, :]
elif chunk_offset == (1, 0):
larger_blocks[-1, 1:-1, 1:-1] = neighbour_blocks.get_sub_chunk(
cy
)[0, :, :]
elif chunk_offset == (0, -1):
larger_blocks[1:-1, 1:-1, 0] = neighbour_blocks.get_sub_chunk(
cy
)[:, :, -1]
elif chunk_offset == (0, 1):
larger_blocks[1:-1, 1:-1, -1] = neighbour_blocks.get_sub_chunk(
cy
)[:, :, 0]
if cy - 1 in blocks:
larger_blocks[1:-1, 0, 1:-1] = blocks.get_sub_chunk(cy - 1)[
:, -1, :
# if self._level.bounds(self.dimension).intersects(sub_chunk_box):
# boxes = self._level.bounds(self.dimension).intersection(sub_chunk_box)
# for box in boxes.selection_boxes:
# larger_blocks[1:-1, 1:-1, 1:-1][
# box.sub_chunk_slice(self.cx, cy, self.cz)
# ] = sub_chunk[box.sub_chunk_slice(self.cx, cy, self.cz)]
larger_blocks[1:-1, 1:-1, 1:-1] = sub_chunk
for chunk_offset, neighbour_blocks in neighbour_chunks.items():
if cy not in neighbour_blocks:
continue
if chunk_offset == (-1, 0):
larger_blocks[0, 1:-1, 1:-1] = neighbour_blocks.get_sub_chunk(cy)[
-1, :, :
]
if cy + 1 in blocks:
larger_blocks[1:-1, -1, 1:-1] = blocks.get_sub_chunk(cy + 1)[
:, 0, :
elif chunk_offset == (1, 0):
larger_blocks[-1, 1:-1, 1:-1] = neighbour_blocks.get_sub_chunk(cy)[
0, :, :
]
sub_chunks.append((larger_blocks, (0, cy * 16, 0)))
elif chunk_offset == (0, -1):
larger_blocks[1:-1, 1:-1, 0] = neighbour_blocks.get_sub_chunk(cy)[
:, :, -1
]
elif chunk_offset == (0, 1):
larger_blocks[1:-1, 1:-1, -1] = neighbour_blocks.get_sub_chunk(cy)[
:, :, 0
]
if cy - 1 in blocks:
larger_blocks[1:-1, 0, 1:-1] = blocks.get_sub_chunk(cy - 1)[:, -1, :]
if cy + 1 in blocks:
larger_blocks[1:-1, -1, 1:-1] = blocks.get_sub_chunk(cy + 1)[:, 0, :]
sub_chunks.append((larger_blocks, (0, cy * 16, 0)))
return sub_chunks

def create_geometry(self):
Expand All @@ -179,45 +178,59 @@ def create_geometry(self):
self._sub_chunks(chunk.blocks)
)
self._set_verts(chunk_verts, chunk_verts_translucent)
if self._draw_floor:
plane: numpy.ndarray = numpy.ones(
(self._vert_len * 12), dtype=numpy.float32
).reshape((-1, self._vert_len))
plane[:, :3], plane[:, 3:5] = self._create_chunk_plane(-0.01)
plane[:, 5:9] = self.resource_pack.texture_bounds(
self.resource_pack.get_texture_path(
"amulet", "amulet_ui/translucent_white"
)
if self._draw_floor or self._draw_ceil:
plane = self._create_grid(
"amulet",
"amulet_ui/translucent_white",
(0.55, 0.5, 0.9) if (self.cx + self.cz) % 2 else (0.4, 0.4, 0.85),
)
if (self.cx + self.cz) % 2:
plane[:, 9:12] = [0.55, 0.5, 0.9]
else:
plane[:, 9:12] = [0.4, 0.4, 0.85]
self.verts = numpy.concatenate([self.verts, plane.ravel()], 0)
self.draw_count += 12
self.draw_count += len(plane)
self._needs_rebuild = True

def _create_empty_geometry(self):
if self._draw_floor:
plane: numpy.ndarray = numpy.ones(
(self._vert_len * 12), dtype=numpy.float32
).reshape((-1, self._vert_len))
plane[:, :3], plane[:, 3:5] = self._create_chunk_plane(0)
plane[:, 5:9] = self.resource_pack.texture_bounds(
self.resource_pack.get_texture_path(
"amulet", "amulet_ui/translucent_white"
)
plane = self._create_grid(
"amulet",
"amulet_ui/translucent_white",
(0.3, 0.3, 0.3) if (self.cx + self.cz) % 2 else (0.2, 0.2, 0.2),
)
if (self.cx + self.cz) % 2:
plane[:, 9:12] = [0.3, 0.3, 0.3]
else:
plane[:, 9:12] = [0.2, 0.2, 0.2]
self.verts = plane.ravel()
self.draw_count = 12
self.draw_count = len(plane)
else:
self.verts = numpy.ones(0, numpy.float32)
self.draw_count = 0

def _create_grid(
self,
texture_namespace: str,
texture_path: str,
tint: Tuple[float, float, float],
):
plane: numpy.ndarray = numpy.ones(
(self._vert_len * 12 * (self._draw_floor + self._draw_ceil)),
dtype=numpy.float32,
).reshape((-1, self._vert_len))
bounds = self._level.bounds(self.dimension)
if self._draw_floor:
plane[:12, :3], plane[:12, 3:5] = self._create_chunk_plane(
bounds.min_y - 0.01
)
if self._draw_ceil:
plane[12:, :3], plane[12:, 3:5] = self._create_chunk_plane(
bounds.max_y + 0.01
)
elif self._draw_ceil:
plane[:12, :3], plane[:12, 3:5] = self._create_chunk_plane(
bounds.max_y + 0.01
)

plane[:, 5:9] = self.resource_pack.texture_bounds(
self.resource_pack.get_texture_path(texture_namespace, texture_path)
)
plane[:, 9:12] = tint
return plane

def _create_chunk_plane(
self, height: Union[int, float]
) -> Tuple[numpy.ndarray, numpy.ndarray]:
Expand Down Expand Up @@ -253,21 +266,13 @@ def _create_chunk_plane(

def _create_error_geometry(self):
if self._draw_floor:
plane: numpy.ndarray = numpy.ones(
(self._vert_len * 12), dtype=numpy.float32
).reshape((-1, self._vert_len))
plane[:, :3], plane[:, 3:5] = self._create_chunk_plane(0)
plane[:, 5:9] = self.resource_pack.texture_bounds(
self.resource_pack.get_texture_path(
"amulet", "amulet_ui/translucent_white"
)
plane = self._create_grid(
"amulet",
"amulet_ui/translucent_white",
(1, 0.2, 0.2) if (self.cx + self.cz) % 2 else (0.75, 0.2, 0.2),
)
if (self.cx + self.cz) % 2:
plane[:, 9:12] = [1, 0.2, 0.2]
else:
plane[:, 9:12] = [0.75, 0.2, 0.2]
self.verts = plane.ravel()
self.draw_count = 12
self.draw_count = len(plane)
else:
self.verts = numpy.ones(0, numpy.float32)
self.draw_count = 0
Expand Down
14 changes: 11 additions & 3 deletions amulet_map_editor/api/opengl/mesh/level/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ def __init__(
context_identifier: Any,
opengl_resource_pack: OpenGLResourcePack,
level: "BaseLevel",
draw_floor=True,
draw_box=False,
draw_floor=False,
draw_ceil=False,
):
OpenGLResourcePackManager.__init__(self, opengl_resource_pack)
ContextManager.__init__(self, context_identifier)
self._level = level
self._camera_location: CameraLocationType = (0, 150, 0)
# yaw (-180 to 180), pitch (-90 to 90)
self._camera_rotation: CameraRotationType = (0, 90)
self._dimension: Dimension = "overworld"
self._dimension: Dimension = level.dimensions[0]
self._render_distance = 5
self._garbage_distance = 10
self._draw_box = draw_box
self._draw_floor = draw_floor
self._draw_ceil = draw_ceil
self._selection = GreenRenderSelectionGroup(
context_identifier, self.resource_pack, self.level.selection_bounds
context_identifier, self.resource_pack, self.level.bounds(self.dimension)
)
self._chunk_manager = ChunkManager(self.context_identifier, self.resource_pack)

Expand Down Expand Up @@ -143,6 +145,7 @@ def thread_action(self):
chunk_coords,
self.dimension,
self.draw_floor,
self.draw_ceil,
)

try:
Expand Down Expand Up @@ -227,6 +230,11 @@ def draw_floor(self):
"""Should the floor under the level be drawn."""
return self._draw_floor

@property
def draw_ceil(self):
"""Should the ceiling above the level be drawn."""
return self._draw_ceil

def chunk_coords(self) -> Generator[ChunkCoordinates, None, None]:
"""Get all of the chunks to draw/load"""
# This yield chunk coordinates in a spiral around the camera
Expand Down
5 changes: 2 additions & 3 deletions amulet_map_editor/api/opengl/mesh/level_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ def append(
self.context_identifier,
self._resource_pack,
level,
draw_floor=False,
draw_box=True,
)
render_level.dimension = dimension
Expand All @@ -136,8 +135,8 @@ def append(
(
-(
(
level.selection_bounds.min_array
+ level.selection_bounds.max_array
level.bounds(dimension).min_array
+ level.bounds(dimension).max_array
)
// 2
).astype(int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def _chunkify_selection(self):
numpy.ceil(box.max_array / self.canvas.world.sub_chunk_size)
* self.canvas.world.sub_chunk_size
)
min_point[1] = self.canvas.world.selection_bounds.min[1]
max_point[1] = self.canvas.world.selection_bounds.max[1]
bounds = self.canvas.world.bounds(self.canvas.dimension)
min_point[1] = bounds.min[1]
max_point[1] = bounds.max[1]
selections.append(SelectionBox(min_point, max_point))
selection_group = SelectionGroup(selections)
if selection_group != self.canvas.selection.selection_group:
Expand Down Expand Up @@ -141,8 +142,9 @@ def _update_pointer(self):
chunk_size = self.canvas.world.sub_chunk_size
location_min: numpy.ndarray = (location // chunk_size) * chunk_size
location_max = location_min + chunk_size
location_min[1] = self.canvas.world.selection_bounds.min[1]
location_max[1] = self.canvas.world.selection_bounds.max[1]
bounds = self.canvas.world.bounds(self.canvas.dimension)
location_min[1] = bounds.min[1]
location_max[1] = bounds.max[1]
self._pointer.point1, self._pointer.point2 = location_min, location_max
if self._editing:
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Optional, Generator
import weakref

import minecraft_model_reader.api.resource_pack.java.download_resources
from minecraft_model_reader.api.resource_pack.java.download_resources import (
get_java_vanilla_latest_iter,
get_java_vanilla_fix,
Expand Down Expand Up @@ -45,6 +46,8 @@

from amulet.api.level import BaseLevel

minecraft_model_reader.api.resource_pack.java.download_resources.INCLUDE_SNAPSHOT = True


class BaseEditCanvas(EventCanvas):
"""Adds base logic for drawing everything related to the edit program to the canvas.
Expand Down
4 changes: 2 additions & 2 deletions amulet_map_editor/programs/edit/api/canvas/edit_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ def select_all(self):
(
(
min_x * self.world.sub_chunk_size,
self.world.selection_bounds.min[1],
self.world.bounds(self.dimension).min[1],
min_z * self.world.sub_chunk_size,
),
(
(max_x + 1) * self.world.sub_chunk_size,
self.world.selection_bounds.max[1],
self.world.bounds(self.dimension).max[1],
(max_z + 1) * self.world.sub_chunk_size,
),
)
Expand Down
2 changes: 2 additions & 0 deletions amulet_map_editor/programs/edit/api/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def __init__(
context_identifier,
opengl_resource_pack,
world,
draw_floor=True,
draw_ceil=True,
)
self._chunk_generator.register(self._render_world)

Expand Down
5 changes: 1 addition & 4 deletions amulet_map_editor/programs/edit/api/ui/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ def __init__(self, canvas: "EditCanvas"):

self._dim_options = SimpleChoiceAny(canvas)
self._dim_options.SetItems(level.level_wrapper.dimensions)
if "overworld" in level.level_wrapper.dimensions:
self._dim_options.SetValue("overworld")
else:
self._dim_options.SetValue(level.level_wrapper.dimensions[0])
self._dim_options.SetValue(level.level_wrapper.dimensions[0])
self._dim_options.Bind(wx.EVT_CHOICE, self._on_dimension_change)

self.Add(self._dim_options, 0, wx.TOP | wx.BOTTOM | wx.RIGHT | wx.CENTER, 5)
Expand Down

0 comments on commit 55b3dec

Please sign in to comment.