Skip to content

Instantly share code, notes, and snippets.

@reedacartwright
Last active January 7, 2022 02:57
Show Gist options
  • Save reedacartwright/b242166720eea9e348b7e21463cfdb10 to your computer and use it in GitHub Desktop.
Save reedacartwright/b242166720eea9e348b7e21463cfdb10 to your computer and use it in GitHub Desktop.
Plotting Biome using RBedrock
## Today we will work on making maps with ggplot2.
# Install the latest version of rbedrock
devtools::install_github("reedacartwright/rbedrock")
# Load tidyverse and rbedrock
library(tidyverse)
library(rbedrock)
# Open World Database
dbpath <- "ClassSMP"
db <- bedrockdb(dbpath)
# Biome Table contains information about biomes in the game
list_biomes()
# Biomes are stored in two different datasets right now
chunk_keys <- get_keys(db, starts_with="@0:0:0") %>% parse_chunk_keys()
chunk_keys
# Mapping one chunk --------------------------------------------------
# Lets extract 1.18 biome information for one chunk:
dat <- get_cnc_biomes_value(db, 0, -10, 0)
dim(dat)
# dat contains per-block biome information. The number of y-values varies by chunk.
# We're going to extract the biomes at y=-64 and turn them into a table.
m <- dat[,1,]
a <- arrayInd(1:256,dim(m))-1
df <- data.frame(x=a[,1]+16*0, z=a[,2]+16*-10, biome=c(m))
# Plot the data using raster
gg <- ggplot(data=df,aes(x=x+0.5,y=z+0.5,fill=biome)) + geom_raster()
gg
# Let's clean up the plot
gg <- ggplot(data=df,aes(x=x+0.5,y=z+0.5,fill=biome)) + geom_raster()
gg <- gg + coord_fixed() + scale_y_reverse()
gg <- gg + labs(x="x", y="z")
gg
# Let's add proper colors
biome_col <- list_biomes() %>% select(name, color) %>% deframe()
gg <- ggplot(data=df,aes(x=x+0.5,y=z+0.5,fill=biome)) + geom_raster()
gg <- gg + coord_fixed() + scale_y_reverse()
gg <- gg + labs(x="x", y="z")
gg <- gg + scale_fill_manual(values=biome_col)
gg
## Challenge #1
## Which of the following overworld chunks has 5 biomes?
## 1,-12; 4,3; 9,9; 14,12; -16,-6
##
## Which of the following overworld chunks contains a forest biome?
## 1,-12; 4,3; 9,9; 14,12; -16,-6
# Mapping multiple chunks --------------------------------------------
# grid of 1024 chunks
g <- expand_grid(x=-16:15,z=-16:15)
# Helper function to process each chunk as we did above
biomes_to_df <- function(x, key) {
pos <- chunk_origins(key)
m <- x[,1,]
a <- arrayInd(1:256,dim(m))-1
df <- data.frame(x=a[,1]+pos[1], z=a[,2]+pos[2], biome=c(m))
df
}
# extract the data and process it
dat <- get_cnc_biomes_data(db, x=g$x, z=g$z, dimension=0)
df <- dat %>% compact() %>% imap_dfr(biomes_to_df)
# Let's start with our graph we had before
gg <- ggplot(data=df,aes(x=x+0.5,y=z+0.5,fill=biome)) + geom_raster()
gg <- gg + coord_fixed() + scale_y_reverse()
gg <- gg + labs(x="x", y="z")
gg <- gg + scale_fill_manual(values=biome_col)
gg
# Let's add some guide lines and a title
breaks64 <- seq(-256,256,64)
breaks128 <- seq(-256,256,128)
gg <- ggplot(data=df,aes(x=x+0.5,y=z+0.5,fill=biome)) + geom_raster()
gg <- gg + coord_fixed()
gg <- gg + scale_y_reverse(breaks=breaks64)
gg <- gg + scale_x_continuous(breaks=breaks64)
gg <- gg + labs(x="x", y="z", title="Biome Map of Spawn")
gg <- gg + scale_fill_manual(values=biome_col)
gg <- gg + geom_vline(xintercept=breaks128)
gg <- gg + geom_hline(yintercept=breaks128)
gg <- gg + theme_minimal()
gg
## Challenge #2
## Draw a map of the chunk region x = 0 to 15 and z = 16 to 31
# Filling in missing data --------------------------------------------
# If there is time let's try to use 2dMaps to fill in missing data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment