wip
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
-- Imports & Exports -------------------------------------------------------------------------------
|
||||
|
||||
local core = _G["core"]
|
||||
local core = _G.core
|
||||
|
||||
|
||||
-- Registrations -----------------------------------------------------------------------------------
|
||||
|
||||
+45
-40
@@ -1,37 +1,34 @@
|
||||
-- Imports & Exports -------------------------------------------------------------------------------
|
||||
|
||||
local biomes = _G["jvmi_biomes"]
|
||||
local core = _G["core"]
|
||||
local math = _G["math"]
|
||||
local mapgen = _G["mapgen"]
|
||||
local table = _G["jvmi_table"]
|
||||
local vector = _G["vector"]
|
||||
local core = _G.core
|
||||
local vector = _G.vector
|
||||
|
||||
-- Since this script runs inside the mapgen environment, we can't count on the jvmi_constants mod
|
||||
-- to have been loaded. Instead, we just need to load the constants manually.
|
||||
local jvmi_constants_path = core.get_modpath("jvmi_constants")
|
||||
dofile(jvmi_constants_path.."/constants.lua")
|
||||
local C = _G["jvmi_constants"]
|
||||
local A = C.ALTITUDE
|
||||
local path = core.get_game_info().path
|
||||
local C = dofile(path.."/common/constants.lua")
|
||||
local Region = dofile(path.."/common/region.lua")
|
||||
local table = dofile(path.."/common/table.lua")
|
||||
|
||||
local Region = mapgen.Region
|
||||
local biomes = dofile(core.get_modpath("jvmi_biomes").."/mapenv.lua")
|
||||
|
||||
|
||||
-- Constants ---------------------------------------------------------------------------------------
|
||||
|
||||
local A = C.ALTITUDE
|
||||
local TERRAIN_TYPES = nil
|
||||
local TERRAIN_TYPE_NOISE = nil
|
||||
|
||||
-- Common Node Content Ids -----------------------------------------------------
|
||||
|
||||
local AIR_ID = core.CONTENT_AIR
|
||||
local BEDROCK_ID = core.get_content_id("jvmi_nodes:bedrock")
|
||||
local STONE_ID = core.get_content_id("jvmi_nodes:stone")
|
||||
local WATER_ID = core.get_content_id("jvmi_nodes:water_source")
|
||||
|
||||
--[[ Create a deep clone of the shared terrain types as we need to augment the entries. ]]--
|
||||
local TERRAIN_TYPES = nil
|
||||
|
||||
|
||||
-- Private -----------------------------------------------------------------------------------------
|
||||
|
||||
local private = {
|
||||
--[[ A share object which assists in generating regions of the map. ]]--
|
||||
--[[ A shared object which assists in generating regions of the map. ]]--
|
||||
region = Region:new()
|
||||
}
|
||||
|
||||
@@ -43,31 +40,27 @@ local private = {
|
||||
written back to the same map.
|
||||
]]--
|
||||
function private.assign_altitudes(region)
|
||||
local pos = vector:new()
|
||||
local pos = vector:new(0, 0, 0)
|
||||
|
||||
region.iterate_columns(function(x, z)
|
||||
region:iterate_columns(function(x, z)
|
||||
local altitude = 0
|
||||
pos.x, pos.y = x, z
|
||||
pos.x, pos.y = x, z -- odd, but as required by get_2d
|
||||
|
||||
for _, blend in ipairs(region:get_meta(Region.META.TERRAIN_BLEND)) do
|
||||
for _, blend in ipairs(region:get_meta(Region.META.TERRAIN_BLEND, x, z)) do
|
||||
local terrain = TERRAIN_TYPES[blend.terrain_type]
|
||||
local contribution = blend.weight * terrain.noise:get_2d(pos)
|
||||
altitude = altitude + contribution
|
||||
end
|
||||
|
||||
region:set_meta(Region.META.ALTITUDE, x, z, altitude)
|
||||
end)
|
||||
end
|
||||
|
||||
--[[
|
||||
]]--
|
||||
function private.assign_biomes(region)
|
||||
|
||||
end
|
||||
|
||||
--[[
|
||||
Compute the combination of terrain types which contribute to each map location.
|
||||
|
||||
This function uses a set of individual terrain types defined by `C.TERRAIN_TYPES`. An overall
|
||||
terrain noise generator, `C.TERRAIN_TYPE_NOISE`, is used to determine which terrain type should
|
||||
This function uses a set of individual terrain types defined by `TERRAIN_TYPES`. An overall
|
||||
terrain noise generator, `TERRAIN_TYPE_NOISE`, is used to determine which terrain type should
|
||||
appear at a given location.
|
||||
|
||||
To avoid abrupt lines where we shift from one terrain type to another, the `C.TERRAIN_BLEND`
|
||||
@@ -76,7 +69,7 @@ end
|
||||
|
||||
Finally, we want the map to make up an island with a smooth transition from higher-altitude
|
||||
terrain in the center to deep ocean terrain at the edges. So, we modify the values from
|
||||
`C.TERRAIN_TYPE_NOISE` to force them lower and lower as you move toward `C.TERRAIN_RADIUS`. By
|
||||
`TERRAIN_TYPE_NOISE` to force them lower and lower as you move toward `C.TERRAIN_RADIUS`. By
|
||||
the time you reach that distance from the center of the islands, the terrain selection will be
|
||||
pinned to the lowest possible terrain type.
|
||||
|
||||
@@ -91,12 +84,12 @@ function private.assign_terrain_meta(region)
|
||||
local radius_squared = C.TERRAIN_RADIUS * C.TERRAIN_RADIUS
|
||||
local pos = { x = 0, y = 0, z = 0 }
|
||||
|
||||
region.iterate_columns(function(x, z)
|
||||
region:iterate_columns(function(x, z)
|
||||
local radius_weight = ((x * x) + (z * z)) / radius_squared
|
||||
radius_weight = math.max(0, math.min(1, 1.0 - radius_weight))
|
||||
|
||||
pos.x, pos.y = x, z
|
||||
local raw_index = (C.TERRAIN_TYPE_NOISE:get_2d(pos) + 2) / 2
|
||||
local raw_index = (TERRAIN_TYPE_NOISE:get_2d(pos) + 2) / 2
|
||||
raw_index = math.max(0, math.min(1, raw_index))
|
||||
raw_index = raw_index * radius_weight
|
||||
|
||||
@@ -120,7 +113,7 @@ function private.assign_terrain_meta(region)
|
||||
end
|
||||
end
|
||||
|
||||
region:set_meta(Region.META_TERRAIN_BLEND, x, z, blend)
|
||||
region:set_meta(Region.META.TERRAIN_BLEND, x, z, blend)
|
||||
end
|
||||
end)
|
||||
end
|
||||
@@ -132,7 +125,7 @@ end
|
||||
region:Region - the portion of the world currently being generated
|
||||
]]--
|
||||
function private.fill_bedrock(region)
|
||||
region.iterate_plane(A.BEDROCK, function(x, y, z)
|
||||
region:iterate_plane(A.BEDROCK, function(x, y, z)
|
||||
region:set_content_id(x, y, z, BEDROCK_ID)
|
||||
region:set_content_id(x, y + 1, z, STONE_ID)
|
||||
end)
|
||||
@@ -145,7 +138,7 @@ end
|
||||
region:Region - the portion of the world currently being generated
|
||||
]]--
|
||||
function private.fill_ocean(region)
|
||||
region.iterate_below(A.SURFACE, function(x, y, z)
|
||||
region:iterate_below(A.SURFACE, function(x, y, z)
|
||||
if region:get_content_id(x, y, z) == AIR_ID then
|
||||
region:set_content_id(x, y, z, WATER_ID)
|
||||
end
|
||||
@@ -160,7 +153,13 @@ end
|
||||
]]--
|
||||
function private.fill_terrain(region)
|
||||
region:iterate_fill(function(x, y, z)
|
||||
local altitude = region:get_meta(Region.META.ALTITUDE)
|
||||
local altitude = region:get_meta(Region.META.ALTITUDE, x, z)
|
||||
if type(altitude) ~= "number" then
|
||||
error(
|
||||
"expected altitude at ("..x..","..z..") to be a number, "..
|
||||
"but was a "..type(altitude)
|
||||
)
|
||||
end
|
||||
if y > altitude then return end
|
||||
region:set_content_id(x, y, z, STONE_ID)
|
||||
end)
|
||||
@@ -189,6 +188,12 @@ function private.make_noise_in_range(lowest, highest)
|
||||
})
|
||||
end
|
||||
|
||||
--[[ Perform any setup this module requires when it first loads. ]]--
|
||||
function private.on_load()
|
||||
private.prepare_terrain_table()
|
||||
TERRAIN_TYPE_NOISE = core.get_value_noise(C.TERRAIN_TYPE_NOISE_PARAMS)
|
||||
end
|
||||
|
||||
--[[
|
||||
Clone the `C.TERRAIN_TYPES` table and add pre-computed values to it.
|
||||
|
||||
@@ -227,7 +232,7 @@ end
|
||||
|
||||
core.register_on_generated(function(voxels, fill_min, fill_max)
|
||||
local region = private.region
|
||||
region:read(voxels, fill_min, fill_max)
|
||||
region:read_voxels(voxels, fill_min, fill_max)
|
||||
|
||||
private.assign_terrain_meta(region)
|
||||
private.assign_altitudes(region)
|
||||
@@ -236,12 +241,12 @@ core.register_on_generated(function(voxels, fill_min, fill_max)
|
||||
private.fill_bedrock(region)
|
||||
private.fill_ocean(region)
|
||||
|
||||
biomes.apply_biomes(region)
|
||||
biomes.apply_to(region)
|
||||
|
||||
region:save()
|
||||
region:save_voxels()
|
||||
end)
|
||||
|
||||
|
||||
-- Script ------------------------------------------------------------------------------------------
|
||||
|
||||
private.prepare_terrain_table()
|
||||
private.on_load()
|
||||
|
||||
@@ -2,4 +2,4 @@ name = jvmi_mapgen
|
||||
title = Jules Verne's Mysterious Island / Map Generation
|
||||
description = do the work of actually generating the map
|
||||
author = tungstonminer
|
||||
depends = jvmi_nodes
|
||||
depends = jvmi_nodes, jvmi_biomes
|
||||
|
||||
@@ -1,272 +0,0 @@
|
||||
-- Import ------------------------------------------------------------------------------------------
|
||||
|
||||
local jvmi_mapgen = _G["jvmi_mapgen"]
|
||||
local math = _G["math"]
|
||||
local VoxelArea = _G["VoxelArea"]
|
||||
|
||||
|
||||
-- Region Class ------------------------------------------------------------------------------------
|
||||
|
||||
local Region = {
|
||||
--[[ Defines the names of the known metadata tables. ]]--
|
||||
META = {
|
||||
--[[ The highest solid block in a certain x,z location. ]]--
|
||||
ALTITUDE = "altitude",
|
||||
|
||||
--[[ The id of the biome at each x,z location. ]]--
|
||||
BIOME = "biome",
|
||||
|
||||
--[[ A list of {weight:int, terrain_type:int} tuples for each x,z location. ]]--
|
||||
TERRAIN_BLEND = "terrain_blend",
|
||||
|
||||
--[[ The heaviest weighted terrain type from the `TERRAIN_BLEND` list. ]]--
|
||||
TERRAIN_TYPE = "terrain_type",
|
||||
}
|
||||
}
|
||||
jvmi_mapgen.Region = Region
|
||||
|
||||
--[[
|
||||
Create a new Region object to manage the generation of a part of the map.
|
||||
|
||||
Returns:
|
||||
an empty Region ready to be filled
|
||||
]]--
|
||||
function Region:new()
|
||||
local result = {
|
||||
_emerged_area = nil,
|
||||
_emerged_data = {},
|
||||
_fill_min = nil,
|
||||
_fill_max = nil,
|
||||
_meta = {},
|
||||
_voxels = nil,
|
||||
}
|
||||
setmetatable(result, Region)
|
||||
return result
|
||||
end
|
||||
|
||||
--[[
|
||||
Return the content_id of the node at the indicated coordinate.
|
||||
|
||||
Args:
|
||||
x:int - the x-axis position
|
||||
y:int - the y-axis position
|
||||
z:int - the z-axis position
|
||||
|
||||
Returns:
|
||||
an int giving the content_id of the node at that location
|
||||
]]--
|
||||
function Region:get_content_id(x, y, z)
|
||||
return self._emerged_data[self._emerged_area:index(x, y, z)]
|
||||
end
|
||||
|
||||
--[[
|
||||
Alter the content_id of the node at the indicated coordinate.
|
||||
|
||||
Args:
|
||||
x:int - the x-axis position
|
||||
y:int - the y-axis position
|
||||
z:int - the z-axis position
|
||||
content_id:int - the new content_id for the given location
|
||||
]]--
|
||||
function Region:set_content_id(x, y, z, content_id)
|
||||
self._emerged_data[self._emerged_area:index(x, y, z)] = content_id
|
||||
end
|
||||
|
||||
--[[
|
||||
Iterate over every voxel within a specified area.
|
||||
|
||||
For each voxel, inside the described cuboid, call the `doOperation` function which should have
|
||||
the following signature:
|
||||
|
||||
function doOperation(x:int, y:int, z:int) -> None
|
||||
|
||||
The called function can then inspect and manipulate the content ids and meta data in the region
|
||||
using the various methods on this class.
|
||||
|
||||
Args:
|
||||
x_min:int - the lowest x coordinate in the desired cuboid
|
||||
x_max:int - the highest x coordinate in the desired cuboid
|
||||
y_min:int - the lowest y coordinate in the desired cuboid
|
||||
y_max:int - the highest y coordinate in the desired cuboid
|
||||
z_min:int - the lowest z coordinate in the desired cuboid
|
||||
z_max:int - the highest z coordinate in the desired cuboid
|
||||
doOperation:function(x:int,y:int,z:int) - the callback to be invoked for each voxel
|
||||
]]--
|
||||
function Region:iterate(x_min, x_max, y_min, y_max, z_min, z_max, doOperation)
|
||||
x_min = math.max(x_min, self._fill_min.x)
|
||||
x_max = math.min(x_max, self._fill_max.x)
|
||||
y_min = math.max(y_min, self._fill_min.y)
|
||||
y_max = math.min(y_max, self._fill_max.y)
|
||||
z_min = math.max(z_min, self._fill_min.z)
|
||||
z_max = math.min(z_max, self._fill_max.z)
|
||||
|
||||
for z = z_min, z_max do
|
||||
for y = y_min, y_max do
|
||||
for x = x_min, x_max do
|
||||
doOperation(self, x, y, z)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
Iterates over all voxels at or below a certain altitude.
|
||||
|
||||
Works identically to the `Region:iterate` function, except that the x & z ranges are the full
|
||||
area being filled by this region, and the y range is capped at the given altitude.
|
||||
|
||||
Args:
|
||||
y_max:int - the maximum y value to include
|
||||
doOperation:function(x:int,y:int,z:int) - the callback to be invoked for each voxel
|
||||
]]--
|
||||
function Region:iterate_below(y_max, doOperation)
|
||||
self:iterate(
|
||||
self._fill_min.x, self._fill_max.x,
|
||||
self._fill_min.y, y_max,
|
||||
self._fill_min.z, self._fill_max.z,
|
||||
doOperation
|
||||
)
|
||||
end
|
||||
|
||||
--[[
|
||||
Iterates over all the x,z columns in the fill region.
|
||||
|
||||
Works identically to the `Region:iterate` function, except that the x & z ranges are the full
|
||||
area being filled by this region, and the y-axis coordinate is omitted.
|
||||
|
||||
Args:
|
||||
doOperation:function(x:int,z:int) - the callback to be invoked for each voxel
|
||||
]]--
|
||||
function Region:iterate_columns(doOperation)
|
||||
self:iterate(
|
||||
self._fill_min.x, self._fill_max.x,
|
||||
0, 0,
|
||||
self._fill_min.z, self._fill_max.z,
|
||||
function(x, y, z) doOperation(x, z) end
|
||||
)
|
||||
end
|
||||
|
||||
--[[
|
||||
Iterates over all voxels being filled in this region.
|
||||
|
||||
Works identically to the `Region:iterate` function, except that the x, y, and z ranges are the
|
||||
full area being filled by this region.
|
||||
|
||||
Args:
|
||||
doOperation:function(nt,int,int) - the callback to be invoked for each voxel
|
||||
]]--
|
||||
function Region:iterate_fill(doOperation)
|
||||
self:iterate(
|
||||
self._fill_min.x, self._fill_max.x,
|
||||
self._fill_min.y, self._fill_max.y,
|
||||
self._fill_min.z, self._fill_max.z,
|
||||
doOperation
|
||||
)
|
||||
end
|
||||
|
||||
--[[
|
||||
Iterates over all voxels at a specific altitude.
|
||||
|
||||
Works identically to the `Region:iterate` function, except that the x & z ranges are the full
|
||||
area being filled by this region, and y is limited to exactly the given altitude.
|
||||
|
||||
Args:
|
||||
y:int - the y value to use
|
||||
doOperation:function(int,int,int) - the callback to be invoked for each voxel
|
||||
]]--
|
||||
function Region:iterate_plane(y, doOperation)
|
||||
if y < self._fill_min.y then return end
|
||||
if y > self._fill_max.y then return end
|
||||
|
||||
self:iterate(
|
||||
self._fill_min.x, self._fill_max.x,
|
||||
y, y,
|
||||
self._fill_min.z, self._fill_max.z,
|
||||
doOperation
|
||||
)
|
||||
end
|
||||
|
||||
--[[
|
||||
Get the named metadata at the indicated coordinate.
|
||||
|
||||
The canonical use for this method is to find the previously-calculated altitude of the base
|
||||
terrain at a certain location (e.g., `get_meta(Region.META.ALTITUDE, 292, 998)`). However, any
|
||||
meta name can be used, and any data stored. This data is let until overwritten, so each mapgen
|
||||
cycle should be careful to recompute any metadata it requires before running algorithms which
|
||||
depend upon it.
|
||||
|
||||
Args:
|
||||
name:string - the name of the metadata table to access
|
||||
x:int - the x coordinate of the desired data
|
||||
z:int - the z coordinate of the desired data
|
||||
|
||||
Returns:
|
||||
the value previously stored for that location or `nil` if there isn't any
|
||||
]]--
|
||||
function Region:get_meta(name, x, z)
|
||||
local meta_data = self._meta[name]
|
||||
if not meta_data then return nil end
|
||||
return meta_data[self._emerged_area:index(x, 0, z)]
|
||||
end
|
||||
|
||||
--[[
|
||||
Change the metadata at the indicated coordinate.
|
||||
|
||||
This will automatically fill in any necessary pieces to ensure that the operation succeeds
|
||||
(e.g., creating a missing metadata table), so it is highly advised to use the provided
|
||||
`Region.META` constants to avoid accidentally creating new tables through misspellings.
|
||||
|
||||
Args:
|
||||
name:string - the name of the metadata table to access
|
||||
x:int - the x coordinate of the desired data
|
||||
z:int - the z coordinate of the desired data
|
||||
value:any - the value to be stored
|
||||
]]--
|
||||
function Region:set_meta(name, x, z, value)
|
||||
local meta_data = self._meta[name]
|
||||
if not meta_data then
|
||||
meta_data = {}
|
||||
self._meta[name] = meta_data
|
||||
end
|
||||
|
||||
meta_data[self._emerged_area:index(x, 0, z)] = value
|
||||
end
|
||||
|
||||
--[[
|
||||
Reads raw voxel data from a voxel manipulator.
|
||||
|
||||
Args:
|
||||
voxels:VoxelManip - the voxel manipulator containing the data
|
||||
fill_min:vector - the smaller corner of the cuboid to be filled
|
||||
fill_max:vector - the larger corner of the cuboid to be filled
|
||||
]]--
|
||||
function Region:read_voxels(voxels, fill_min, fill_max)
|
||||
self:save_voxels()
|
||||
|
||||
self._voxels = voxels
|
||||
self._fill_min = fill_min
|
||||
self._fill_max = fill_max
|
||||
|
||||
local min_emerged, max_emerged = voxels:get_emerged_area()
|
||||
self._emerged_area = VoxelArea(min_emerged, max_emerged)
|
||||
self._voxels:get_data(self._emerged_data)
|
||||
end
|
||||
|
||||
--[[
|
||||
Writes raw voxel data back into the source voxel manipulator.
|
||||
|
||||
If no voxel data has been read (or none has been read since the last save), then this function
|
||||
returns immediately.
|
||||
]]--
|
||||
function Region:save_voxels()
|
||||
if not self._voxels then return end
|
||||
|
||||
self._voxels:set_data(self._emerged_data)
|
||||
self._voxels:calc_lighting()
|
||||
self._voxels:update_liquids()
|
||||
|
||||
self._emerged_area = nil
|
||||
self._fill_max = nil
|
||||
self._fill_min = nil
|
||||
self._voxels = nil
|
||||
end
|
||||
Reference in New Issue
Block a user