Skip to content

Commit

Permalink
changed surface_blocks.txt to json and added more advanced functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-mooore committed Aug 18, 2023
1 parent 159bc8f commit 06aae3b
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 128 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Feel free to create a PR at any point, or open an issue if you have any problems

## How it Works

Chunk data is read by opening the world save directory and reading its region files (`.mca`). Once the region data is decoded chunks can be iterated through to find surface blocks (can be changed by editing [surface_blocks.txt](surface_blocks.txt)). To make this process more efficient, the script takes advantage of precomputed heightmaps, which are also decoded from their binary format. These heightmaps contain info such as the y-level of the first motion-blocking minecraft block, so efficiency of finding the surface is greatly improved.
Chunk data is read by opening the world save directory and reading its region files (`.mca`). Once the region data is decoded chunks can be iterated through to find surface blocks (can be changed by editing [symbols.json](symbols.json)). To make this process more efficient, the script takes advantage of precomputed heightmaps, which are also decoded from their binary format. These heightmaps contain info such as the y-level of the first motion-blocking minecraft block, so efficiency of finding the surface is greatly improved.

After the chunks are read several `.tif` files are saved in a folder called `data` at the root directory of the project. These files contain the raw data extracted from the minecraft files that is needed to create the topographical map. `.tif` files are chosen as they are a cross-compatible and effective data matrix format.

Expand Down
31 changes: 16 additions & 15 deletions src/convert_world.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import platform
import sys
Expand Down Expand Up @@ -99,8 +100,12 @@ def chunk_at(region, cx, cz):
)
args = parser.parse_args(sys.argv[1:])

surface_blocks = [line.rstrip() for line in open("surface_blocks.txt") if line.rstrip()]
# structure_blocks = [line.rstrip() for line in open("structure_blocks.txt") if line.rstrip()]
with open("symbols.json", "r") as json_file:
symbol_data = json.load(json_file)
surface_blocks = [symbol["blocks"].get("ground" )\
for _, symbol in symbol_data.items() \
if symbol["blocks"].get("ground")]
surface_blocks = list(dict.fromkeys([i for sub in surface_blocks for i in sub]))

bx1, bz1, bx2, bz2 = args.x1, args.z1, args.x2, args.z2
if bx1 > bx2 or bz1 > bz2:
Expand Down Expand Up @@ -132,7 +137,7 @@ def chunk_at(region, cx, cz):

data = {
"dem": create_mat(np.uint8) if args.compress_height_limit else create_mat(np.int16),
"vegetation": create_mat(np.bool_),
"canopy": create_mat(np.bool_),
"landcover": create_mat(np.uint8),
"trees": create_mat(np.bool_)
}
Expand Down Expand Up @@ -162,20 +167,15 @@ def chunk_at(region, cx, cz):
max_height = int(chunk.heightmap[bz_in_c, bx_in_c]) if chunk.heightmap.any() else 255
min_height = 0

canopy = False
tree = False
tree_so_far = False
for by in range(max_height, min_height, -1):
block = chunk.get_block(bx_in_c, by, bz_in_c)

# -- surface processes: dem and landcover --
if block.id == "air":
canopy = False
tree = False
continue
if block.id in surface_blocks:
data["dem"][entry] = by
data["landcover"][entry] = surface_blocks.index(block.id)
if tree:
if tree_so_far:
data["trees"][entry] = 1
break

Expand All @@ -193,13 +193,14 @@ def chunk_at(region, cx, cz):
break

# -- other processes: vegetation --
if block.id.endswith("leaves"):
data["vegetation"][entry] = 1
canopy = True
if block.id in symbol_data["405"]["blocks"]["canopy"]:
data["canopy"][entry] = 1

# -- other processes: trees --
if block.id.endswith("log") and canopy:
tree = True
if block.id in symbol_data["418"]["blocks"]["canopy"]:
tree_so_far = True
elif not block.id in symbol_data["418"]["blocks"]["trunk"]:
tree_so_far = False

logging.info("Writing data...")
try:
Expand Down
16 changes: 11 additions & 5 deletions src/create_map.r
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ suppressPackageStartupMessages({
library(terra)
library(tmap)

library(jsonlite)
library(tiff)
library(smoothr)
})

surface_blocks <- read.table("surface_blocks.txt")$V1
symbol_data <- fromJSON("symbols.json", simplifyDataFrame = F)
exif <- tiff::readTIFF("data/dem.tif", payload=F)

# increase smoothing with more downsampling to account for inaccuracies
Expand Down Expand Up @@ -57,7 +58,7 @@ size_from_mm <- function (mmwidth) {
log_info("Reading data...")
data <- lapply(list(
dem=terra::rast("data/dem.tif"),
vegetation=terra::rast("data/vegetation.tif"),
canopy=terra::rast("data/canopy.tif"),
landcover=terra::rast("data/landcover.tif"),
trees=terra::rast("data/trees.tif")
), function(raster) {crs(raster) <- "ESRI:53032"; raster})
Expand All @@ -70,6 +71,11 @@ bounds <- st_as_sf(vect(ext(
)))
st_crs(bounds) <- "ESRI:53032"

surface_blocks <- symbol_data |>
lapply(function(obj) obj$blocks$ground) |>
unlist() |>
unique()

log_info("Creating symbols...")
log_info("Creating contours...")
contours <- list(
Expand All @@ -85,7 +91,7 @@ contours <- list(

log_info("Creating water...")
water <- data$landcover
water[data$landcover != (match("water", surface_blocks) - 1)] <- NA
water[data$landcover != (match(symbol_data$`301`$blocks$ground, surface_blocks) - 1)] <- NA
water <- list(
feature=water |>
terra::as.polygons(),
Expand All @@ -97,9 +103,9 @@ water <- list(
)

log_info("Creating canopy...")
data$vegetation[data$vegetation == 0] <- NA
data$canopy[data$canopy == 0] <- NA
canopy <- list(
feature=terra::as.polygons(data$vegetation) |>
feature=terra::as.polygons(data$canopy) |>
terra::buffer(canopy_buffer * as.logical(settings$smoothing)),
render=list(tm_fill(col = "#FFFFFF")),
smoothing=20
Expand Down
107 changes: 0 additions & 107 deletions surface_blocks.txt

This file was deleted.

Loading

0 comments on commit 06aae3b

Please sign in to comment.