wip
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
-- Private -----------------------------------------------------------------------------------------
|
||||
|
||||
local private = {
|
||||
enabled = false
|
||||
}
|
||||
|
||||
--[[
|
||||
Disable all checks.
|
||||
|
||||
By default, checks start out disabled already. This function should be used to turn off checks
|
||||
when they are no longer needed following a call to `enable`.
|
||||
]]--
|
||||
function private.disable()
|
||||
private.enabled = false
|
||||
end
|
||||
|
||||
--[[
|
||||
Enable all checks.
|
||||
|
||||
By default, all checks are *disabled* for better performance. By calling this function, one can
|
||||
turn on checks for a given patch of code.
|
||||
]]--
|
||||
function private.enable()
|
||||
private.enabled = true
|
||||
end
|
||||
|
||||
--[[
|
||||
Assert that a given value is a key in a certain table.
|
||||
|
||||
Args:
|
||||
name:string - the name used for the value in error messages
|
||||
value:any - the value being tested
|
||||
table_name:string - the name used for the table in error messages
|
||||
table_value:table - the table which must contain the indicated value
|
||||
|
||||
Errors:
|
||||
if the given `value` does not refer to a non-`nil` value in `table_value`
|
||||
]]--
|
||||
function private.is_key(name, value, table_name, table_value)
|
||||
if not private.enabled then return end
|
||||
if table_value[value] ~= nil then return end
|
||||
error(name.." should be a key in "..table_name..", but "..value.." isn't present")
|
||||
end
|
||||
|
||||
--[[
|
||||
Assert that a given value is a certain type.
|
||||
|
||||
Args:
|
||||
name:string - the name used for the value in error messages
|
||||
value:any - the value being tested
|
||||
type_name:string - the string which *should* be produced by `type(value)`
|
||||
|
||||
Errors:
|
||||
if the `type(value)` does not produce `type_name`
|
||||
]]--
|
||||
function private.is_type(name, value, type_name)
|
||||
if not private.enabled then return end
|
||||
if type(value) == type_name then return end
|
||||
error(name.." should be a "..type_name..", but was a "..type(value))
|
||||
end
|
||||
|
||||
|
||||
-- Export ------------------------------------------------------------------------------------------
|
||||
|
||||
_G.jvmi.check = {
|
||||
disable = private.disable,
|
||||
enable = private.enable,
|
||||
is_key = private.is_key,
|
||||
is_type = private.is_type
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
-- Private -----------------------------------------------------------------------------------------
|
||||
|
||||
local C = {}
|
||||
|
||||
|
||||
-- Node Definition Constants ---------------------------------------------------
|
||||
|
||||
C.ANIMATION_TYPE = {
|
||||
VERTICAL_FRAMES = "vertical_frames",
|
||||
SHEET_2D = "sheet_2d",
|
||||
}
|
||||
|
||||
C.DRAW_TYPE = {
|
||||
ALL_FACES_OPTIONAL = "allfaces_optional",
|
||||
FLOWING_LIQUID = "flowingliquid",
|
||||
LIQUID = "liquid",
|
||||
MESH = "mesh",
|
||||
NODE_BOX = "nodebox",
|
||||
NORMAL = "normal",
|
||||
PLANT_LIKE = "plantlike",
|
||||
}
|
||||
|
||||
C.LIQUID_TYPE = {
|
||||
NONE = "none",
|
||||
SOURCE = "source",
|
||||
FLOWING = "flowing",
|
||||
}
|
||||
|
||||
C.TEXTURE_ALPHA = {
|
||||
OPAQUE = "opaque",
|
||||
CLIP = "clip",
|
||||
BLEND = "blend",
|
||||
}
|
||||
|
||||
C.VISCOSITY = {
|
||||
XXX_FAST = 0,
|
||||
XX_FAST = 1,
|
||||
X_FAST = 2,
|
||||
FAST = 3,
|
||||
SLOW = 4,
|
||||
X_SLOW = 5,
|
||||
XX_SLOW = 6,
|
||||
XXX_SLOW = 7,
|
||||
}
|
||||
|
||||
C.WAVING = {
|
||||
NO = 0,
|
||||
PLANT_LIKE = 1,
|
||||
LEAF_LIKE = 2,
|
||||
LIQUID_LIKE = 3,
|
||||
}
|
||||
|
||||
|
||||
-- Mapgen Constants ------------------------------------------------------------
|
||||
|
||||
--[[ Key altitudes which define the vertical regions of the generated map. ]]--
|
||||
C.ALTITUDE = {
|
||||
SKY = 256,
|
||||
MOUNTAIN = 128,
|
||||
MOOR = 64,
|
||||
HILL = 32,
|
||||
PLAIN = 16,
|
||||
SURFACE = 0,
|
||||
SHALLOW = -16,
|
||||
DEEP = -32,
|
||||
ABYSS = -64,
|
||||
BEDROCK = -128,
|
||||
}
|
||||
local A = C.ALTITUDE
|
||||
|
||||
--[[ The amount of overlap (as a perentage of total weight) between adjacent terrain types. ]]--
|
||||
C.TERRAIN_BLEND = 0.15
|
||||
|
||||
--[[ The distance from the center of the map at which only the lowest terrain types are used. ]]--
|
||||
C.TERRAIN_RADIUS = 3096
|
||||
|
||||
--[[ The noise generator that decides which terrain type is active at a given x,z location. ]]--
|
||||
C.TERRAIN_TYPE_NOISE_PARAMS = {
|
||||
flags = "eased",
|
||||
lacunarity = 2,
|
||||
octaves = 1,
|
||||
offset = 0,
|
||||
persistence = 0.5,
|
||||
scale = 1.0,
|
||||
seed = 8381909047,
|
||||
spread = { x = 512, y = 512, z = 0 },
|
||||
}
|
||||
|
||||
|
||||
--[[
|
||||
A table defining the terrain types used in generating the base terrain.
|
||||
|
||||
These must be listed in average altitude order from lowest to highest. The weights are merely
|
||||
relative values, and can be whatever you like. I find it easiest to think of them as
|
||||
percentages and choose value which tally to 100.
|
||||
|
||||
Bear in mind that noise values are adjusted downward are you approach `TERRAIN_RADIUS` meters
|
||||
from the center of the map, so these weights are only going to be fully reflected in the terrain
|
||||
generated at the center of the map, and will progressively favor lower altitudes as you move
|
||||
toward the edges.
|
||||
]]--
|
||||
C.TERRAIN_TYPES = {
|
||||
{ name = "ocean_floor", lowest = A.BEDROCK + 10, highest = A.BEDROCK + 26, weight = 5 },
|
||||
{ name = "ocean_abyss", lowest = A.ABYSS, highest = A.DEEP, weight = 15 },
|
||||
{ name = "ocean_shelf", lowest = A.DEEP, highest = A.SHALLOW, weight = 10 },
|
||||
{ name = "ocean_surface", lowest = A.SURFACE - 4, highest = A.SURFACE, weight = 5 },
|
||||
{ name = "land_plains", lowest = A.PLAIN - 8, highest = A.PLAIN, weight = 20 },
|
||||
{ name = "land_hills", lowest = A.HILL - 32, highest = A.HILL, weight = 25 },
|
||||
{ name = "land_moors", lowest = A.MOOR - 16, highest = A.MOOR, weight = 15 },
|
||||
{ name = "land_mountains", lowest = A.MOUNTAIN - 80, highest = A.MOUNTAIN, weight = 5 },
|
||||
}
|
||||
|
||||
|
||||
-- Export ------------------------------------------------------------------------------------------
|
||||
|
||||
_G.jvmi.C = C
|
||||
@@ -0,0 +1,68 @@
|
||||
-- Private -----------------------------------------------------------------------------------------
|
||||
|
||||
local private = {}
|
||||
|
||||
--[[
|
||||
Creates a diagnostic string from a given object.
|
||||
|
||||
This function recursively explores tables trying to show as much detail as possible within the
|
||||
limits dictated by its arguments. For anything other than a table, it simply uses the results
|
||||
of calling `tostring` with the object in question.
|
||||
|
||||
The output is pretty printed for ease of reading using a new line for each key/value pair, and
|
||||
indenting successive layers by four spaces.
|
||||
|
||||
Args:
|
||||
value:any - the value to be dumped
|
||||
depth:int - the maximum layers of nested tables to explore (10 by default)
|
||||
indent:string - the initial indent to use at the first layer
|
||||
max_keys:int - the maximum number of keys to print for a single table (100 by default)
|
||||
seen:table[any,true] - a table containing all the values already seen during a single,
|
||||
recursive print attempt (to avoid cycles or duplication)
|
||||
|
||||
Returns:
|
||||
a string containing the pretty-printed version of the given object
|
||||
]]--
|
||||
function private.dump(value, depth, indent, max_keys, seen, current_indent)
|
||||
current_indent = current_indent or ""
|
||||
depth = depth or 10
|
||||
indent = indent or ""
|
||||
max_keys = max_keys or 100
|
||||
seen = seen or {}
|
||||
|
||||
local newline = ((indent == "") and "") or "\n"
|
||||
|
||||
if depth == 0 then return "<too deep: "..tostring(value)..">" end
|
||||
|
||||
if seen[value] == true then return "<already seen: "..tostring(value)..">" end
|
||||
seen[value] = true
|
||||
|
||||
local result = ""
|
||||
if type(value) == "table" then
|
||||
result = result.."{"..newline
|
||||
local inner_indent = current_indent..indent
|
||||
local key_count = 0
|
||||
for inner_key, inner_value in pairs(value) do
|
||||
if key_count >= max_keys then
|
||||
result = result..inner_indent.."<too many keys>"..newline
|
||||
break
|
||||
end
|
||||
|
||||
local inner_text = private.dump(
|
||||
inner_value, depth - 1, indent, max_keys, seen, inner_indent
|
||||
)
|
||||
result = result..inner_indent..inner_key.." = "..inner_text..", "..newline
|
||||
key_count = key_count + 1
|
||||
end
|
||||
result = result..current_indent.."}"
|
||||
else
|
||||
result = tostring(value)
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
-- Export -----------------------------------------------------------------------------------------
|
||||
|
||||
_G.jvmi.dump = private.dump
|
||||
@@ -1 +1,34 @@
|
||||
-- Do nothing.
|
||||
_G.jvmi = _G.jvmi or { mods = {} }
|
||||
|
||||
local jvmi = _G.jvmi
|
||||
local core = _G.core
|
||||
local base_dir
|
||||
|
||||
if not core then
|
||||
jvmi.env = "test"
|
||||
jvmi.base_dir = "../.."
|
||||
jvmi.in_game = false
|
||||
base_dir = "."
|
||||
else
|
||||
base_dir = core.get_modpath("jvmi_first")
|
||||
jvmi.base_dir = core.get_game_info().path
|
||||
jvmi.in_game = true
|
||||
|
||||
if core.register_mapgen_script then
|
||||
jvmi.env = "main"
|
||||
core.register_mapgen_script(base_dir.."/init.lua")
|
||||
else
|
||||
jvmi.env = "mapgen"
|
||||
end
|
||||
end
|
||||
|
||||
dofile(base_dir.."/check.lua")
|
||||
dofile(base_dir.."/constants.lua")
|
||||
dofile(base_dir.."/dump.lua")
|
||||
dofile(base_dir.."/require.lua")
|
||||
dofile(base_dir.."/table.lua")
|
||||
|
||||
-- requires 'check'
|
||||
dofile(base_dir.."/region.lua")
|
||||
|
||||
jvmi.register_mod("jvmi_first")
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
-- Import ------------------------------------------------------------------------------------------
|
||||
|
||||
local jvmi = _G.jvmi
|
||||
local math = _G.math
|
||||
local VoxelArea = _G.VoxelArea
|
||||
|
||||
local check = jvmi.check
|
||||
|
||||
|
||||
-- Private -----------------------------------------------------------------------------------------
|
||||
|
||||
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",
|
||||
}
|
||||
}
|
||||
|
||||
--[[
|
||||
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 = {
|
||||
fill_min = nil,
|
||||
fill_max = nil,
|
||||
_emerged_area = nil,
|
||||
_emerged_data = {},
|
||||
_meta = {},
|
||||
_voxels = nil,
|
||||
}
|
||||
setmetatable(result, { __index = 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)
|
||||
check.is_type("x", x, "number")
|
||||
check.is_type("y", y, "number")
|
||||
check.is_type("z", z, "number")
|
||||
|
||||
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)
|
||||
check.is_type("x", x, "number")
|
||||
check.is_type("y", y, "number")
|
||||
check.is_type("z", z, "number")
|
||||
check.is_type("content_id", content_id, "number")
|
||||
|
||||
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 `do_operation` function which should have
|
||||
the following signature:
|
||||
|
||||
function do_operation(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
|
||||
do_operation: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, do_operation)
|
||||
check.is_type("x_min", x_min, "number")
|
||||
check.is_type("x_max", x_max, "number")
|
||||
check.is_type("y_min", y_min, "number")
|
||||
check.is_type("y_max", y_max, "number")
|
||||
check.is_type("z_min", z_min, "number")
|
||||
check.is_type("z_max", z_max, "number")
|
||||
check.is_type("do_operation", do_operation, "function")
|
||||
|
||||
x_min = math.floor(math.max(x_min, self.fill_min.x))
|
||||
x_max = math.floor(math.min(x_max, self.fill_max.x))
|
||||
y_min = math.floor(math.max(y_min, self.fill_min.y))
|
||||
y_max = math.floor(math.min(y_max, self.fill_max.y))
|
||||
z_min = math.floor(math.max(z_min, self.fill_min.z))
|
||||
z_max = math.floor(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
|
||||
do_operation(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
|
||||
do_operation:function(x:int,y:int,z:int) - the callback to be invoked for each voxel
|
||||
]]--
|
||||
function Region:iterate_below(y_max, do_operation)
|
||||
check.is_type("y_max", y_max, "number")
|
||||
check.is_type("do_operation", do_operation, "function")
|
||||
|
||||
self:iterate(
|
||||
self.fill_min.x, self.fill_max.x,
|
||||
self.fill_min.y, y_max,
|
||||
self.fill_min.z, self.fill_max.z,
|
||||
do_operation
|
||||
)
|
||||
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:
|
||||
do_operation:function(x:int,z:int) - the callback to be invoked for each voxel
|
||||
]]--
|
||||
function Region:iterate_columns(do_operation)
|
||||
check.is_type("do_operation", do_operation, "function")
|
||||
|
||||
self:iterate(
|
||||
self.fill_min.x, self.fill_max.x,
|
||||
self.fill_min.y, self.fill_min.y, -- we only want columns, so use a single y value
|
||||
self.fill_min.z, self.fill_max.z,
|
||||
function(x, y, z) do_operation(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:
|
||||
do_operation:function(nt,int,int) - the callback to be invoked for each voxel
|
||||
]]--
|
||||
function Region:iterate_fill(do_operation)
|
||||
check.is_type("do_operation", do_operation, "function")
|
||||
|
||||
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,
|
||||
do_operation
|
||||
)
|
||||
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
|
||||
do_operation:function(int,int,int) - the callback to be invoked for each voxel
|
||||
]]--
|
||||
function Region:iterate_plane(y, do_operation)
|
||||
check.is_type("y", y, "number")
|
||||
check.is_type("do_operation", do_operation, "function")
|
||||
|
||||
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,
|
||||
do_operation
|
||||
)
|
||||
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)
|
||||
check.is_type("name", name, "string")
|
||||
check.is_type("x", x, "number")
|
||||
check.is_type("z", z, "number")
|
||||
|
||||
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)
|
||||
check.is_type("name", name, "string")
|
||||
check.is_type("x", x, "number")
|
||||
check.is_type("z", z, "number")
|
||||
|
||||
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)
|
||||
check.is_type("voxels", voxels, "userdata")
|
||||
check.is_type("fill_min", fill_min, "table")
|
||||
check.is_type("fill_max", fill_max, "table")
|
||||
|
||||
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.fill_max = nil
|
||||
self.fill_min = nil
|
||||
self._emerged_area = nil
|
||||
self._voxels = nil
|
||||
end
|
||||
|
||||
|
||||
-- Public ------------------------------------------------------------------------------------------
|
||||
|
||||
jvmi.Region = Region
|
||||
@@ -0,0 +1,119 @@
|
||||
-- Imports -----------------------------------------------------------------------------------------
|
||||
|
||||
local core = _G.core
|
||||
local jvmi = _G.jvmi
|
||||
local string = _G.string
|
||||
|
||||
|
||||
-- Private -----------------------------------------------------------------------------------------
|
||||
|
||||
local private = {}
|
||||
|
||||
--[[
|
||||
Extract the short name from a given mod name.
|
||||
|
||||
Args:
|
||||
name:string - the full name of the mod
|
||||
|
||||
Returns:
|
||||
the short name of the mod (i.e., without the "jvmi_" prefix)
|
||||
|
||||
Errors:
|
||||
if the name doesn't have the "jvmi_" prefix
|
||||
]]--
|
||||
function private.extract_short_name(name)
|
||||
local prefix_index = string.find(name, "jvmi_", 1, true)
|
||||
if prefix_index ~= 1 then
|
||||
error(name.."does not have a 'jvmi_' prefix")
|
||||
end
|
||||
|
||||
return string.sub(name, 6)
|
||||
end
|
||||
|
||||
--[[
|
||||
Load a script which is local to a specific mod.
|
||||
|
||||
Args:
|
||||
mod:table - the mod's global table (which must still include the `base_dir` key)
|
||||
]]--
|
||||
function private.domodfile(mod, path)
|
||||
dofile(mod.base_dir.."/"..path)
|
||||
end
|
||||
|
||||
|
||||
-- Global ------------------------------------------------------------------------------------------
|
||||
|
||||
--[[
|
||||
Register a JVMI mod with the global table.
|
||||
|
||||
This will create a new table for the mod's use and populate it with a few useful items. The
|
||||
table will be returned, and the mod can then use it to store its own global state. The following
|
||||
keys will be pre-populated for each mod:
|
||||
|
||||
base_dir:string - the location on disk where the mod lives
|
||||
domodfile:function(path:string) - run `dofile` on a script in the mod's base dir
|
||||
name:string - the full name of the mod, as given
|
||||
short_name:string - the name of the mod without the "jvmi_" prefix
|
||||
|
||||
The mod's table is also added to `jvmi.mods[mod.short_name]`, so that it may be accessed as
|
||||
needed by other mods.
|
||||
|
||||
|
||||
Args:
|
||||
name:string - a string giving the full name of the mod "jvmi_..."
|
||||
|
||||
Returns:
|
||||
a table the mod can use to store its own data
|
||||
]]--
|
||||
function jvmi.register_mod(name)
|
||||
local short_name = private.extract_short_name(name)
|
||||
local mod = jvmi.mods[short_name]
|
||||
if mod then return mod end
|
||||
|
||||
mod = {
|
||||
base_dir = jvmi.base_dir.."/mods/"..name,
|
||||
name = name,
|
||||
short_name = short_name,
|
||||
}
|
||||
mod.domodfile = function(path) private.domodfile(mod, path) end
|
||||
|
||||
if jvmi.in_game then
|
||||
mod.S = core.get_translator(name)
|
||||
else
|
||||
mod.S = function(text) return text end
|
||||
end
|
||||
|
||||
jvmi.mods[short_name] = mod
|
||||
return mod
|
||||
end
|
||||
|
||||
--[[
|
||||
Obtain the global table associated with the named mod.
|
||||
|
||||
This function will return the existing table if the mod is already registered, or attempt to
|
||||
load the mod and return the newly-registered table if not. If this function is called in the
|
||||
"main" env, it will raise an error if the mod isn't already registered, since mods should use
|
||||
the `depends` entry in their `mod.conf` file to ensure their dependencies are loaded instead of
|
||||
using this function.
|
||||
|
||||
Args:
|
||||
name:string - the full name of the mod to load
|
||||
]]--
|
||||
function jvmi.require_mod(name)
|
||||
local short_name = private.extract_short_name(name)
|
||||
local mod = jvmi.mods[short_name]
|
||||
if mod then return mod end
|
||||
|
||||
if jvmi.env == "main" then
|
||||
error("Update mod.conf:depends to include "..name)
|
||||
end
|
||||
|
||||
local init_path = jvmi.base_dir.."/mods/"..name.."/init.lua"
|
||||
dofile(init_path)
|
||||
mod = jvmi.mods[short_name]
|
||||
if not mod then
|
||||
error("Loading "..init_path.." didn't register "..name)
|
||||
end
|
||||
|
||||
return mod
|
||||
end
|
||||
@@ -0,0 +1,92 @@
|
||||
-- Private -----------------------------------------------------------------------------------------
|
||||
|
||||
local private = {}
|
||||
|
||||
--[[
|
||||
Add a value to the end of an array-like table.
|
||||
|
||||
Args:
|
||||
target:array - a table which is being used like an array (i.e., contiguous integer keys)
|
||||
value:any - the value to be appeneded to the array
|
||||
|
||||
Returns:
|
||||
the target table
|
||||
]]--
|
||||
function private.append(target, value)
|
||||
target[#target + 1] = value
|
||||
return target
|
||||
end
|
||||
|
||||
--[[
|
||||
Performs a deep clone of a given table.
|
||||
|
||||
This function only clones regular keys, and makes no effort to preserve the metatables of the
|
||||
resulting tables. It also does not attempt to handle cycles among the given tables. If provided
|
||||
with a non-table value, then it simply returns the same object given without cloning it.
|
||||
|
||||
Args:
|
||||
source:table - the table to be cloned
|
||||
|
||||
Returns:
|
||||
a new table which the same keys and deeply copied values
|
||||
]]--
|
||||
function private.clone(source)
|
||||
if type(source) ~= "table" then return source end
|
||||
|
||||
local target = {}
|
||||
for key, value in pairs(source) do
|
||||
target[key] = private.clone(value)
|
||||
end
|
||||
|
||||
return target
|
||||
end
|
||||
|
||||
--[[
|
||||
Performs a shallow clone of a given table.
|
||||
|
||||
This is similar to `clone`, but it does not attempt to deeply copy the values of the given
|
||||
table, instead creating a new table with the same keys and values as the original.
|
||||
|
||||
Args:
|
||||
source:table - the table to be copied
|
||||
|
||||
Returns:
|
||||
a new table with the same keys and values as the original
|
||||
|
||||
]]--
|
||||
function private.copy(source)
|
||||
return private.merge({}, source)
|
||||
end
|
||||
|
||||
--[[
|
||||
Overwrite the contents of a target table with the values in one or more source tables.
|
||||
|
||||
Only those keys defined by one of the source tables will be overridden in the target table.
|
||||
Each successive source table in the given sequence may override values in the earlier source
|
||||
tables. This function only performs a shallow copy, so any objects in the source tables are only copied
|
||||
by reference. To create a brand new table with the merged contents of the following tables,
|
||||
provide nil as the first argument.
|
||||
|
||||
Args:
|
||||
target:table - a table whose values are to be overridden
|
||||
...:table - a sequence of one or more source tables whose keys are to be copied
|
||||
|
||||
Returns:
|
||||
the target table
|
||||
]]--
|
||||
function private.merge(target, ...)
|
||||
target = target or nil
|
||||
|
||||
for _, source in ipairs({...}) do
|
||||
for key, value in pairs(source) do
|
||||
target[key] = value
|
||||
end
|
||||
end
|
||||
|
||||
return target
|
||||
end
|
||||
|
||||
|
||||
-- Export ------------------------------------------------------------------------------------------
|
||||
|
||||
_G.jvmi.table = private.merge({}, _G.table, private)
|
||||
Reference in New Issue
Block a user