-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.lua
135 lines (121 loc) · 3.89 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
-- (Yet Another) Superflat Map Generator
-- MIT License (for code)
-- Modify the parameters in parameter.lua!
---------------
-- Main Code --
---------------
if sflat == nil then sflat = {} end
sflat.options = {
biome = "",
decoration = false
}
-- A helper function to get content ID if exists
-- Returns air if it does not exists
local c_air = minetest.get_content_id("air")
function sflat.get_content_id(name)
if minetest.registered_nodes[name] then
return minetest.get_content_id(name)
end
return c_air
end
dofile(minetest.get_modpath("superflat") .. "/parsetext.lua")
dofile(minetest.get_modpath("superflat") .. "/parameter.lua")
dofile(minetest.get_modpath("superflat") .. "/decoration.lua")
-- Make sure that we use singlenode mapgen
minetest.register_on_mapgen_init(function(mgparams)
minetest.set_mapgen_setting("mg_name", "singlenode", true)
end)
-- Superflat's bedrock
local bedrock_sound = nil
if default and default.node_sound_stone_defaults then
bedrock_sound = default.node_sound_stone_defaults()
end
minetest.register_node("superflat:bedrock", {
description = "Superflat's Bedrock",
tiles = {"superflat_bedrock.png"},
groups = {unbreakable = 1, not_in_creative_inventory = 1},
sounds = bedrock_sound
})
-- Read and check whether superflat.txt file exists
-- If superflat.txt does not exist, the default setting will be used instead.
if file_exists(minetest.get_worldpath() .. DIR_DELIM .. "superflat.txt") == true then
dofile(minetest.get_worldpath() .. DIR_DELIM .. "superflat.txt")
else
local list = io.open(minetest.get_worldpath() .. DIR_DELIM .. "superflat.txt", "w")
list:write(
"sflat.Y_ORIGIN = " .. sflat.Y_ORIGIN .. "\n" ..
"sflat.BLOCKS = \"" .. sflat.BLOCKS .. "\""
)
list:close()
end
-- Parse the blocks' parameter once at start
local LAYERS = sflat.parsetext(sflat.BLOCKS)
-- Wait until all nodes are loaded
minetest.after(1, function()
-- Then parse it again
LAYERS = sflat.parsetext(sflat.BLOCKS)
end)
-- The main code
minetest.register_on_generated(function(minp, maxp, seed)
-- If it's above or below the layers, do nothing.
if minp.y >= LAYERS[#LAYERS][3] or maxp.y < sflat.Y_ORIGIN then
return
end
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge = emin, MaxEdge = emax}
local data = vm:get_data()
local pr = PseudoRandom(seed + 1)
-- Generate layers
local li = 1
while (minp.y >= LAYERS[li][3] and li < #LAYERS) do
li = li + 1
end
for y = minp.y, maxp.y do
if y >= LAYERS[li][3] then
li = li + 1
end
if li > #LAYERS then
break
end
if (y >= sflat.Y_ORIGIN and y < LAYERS[#LAYERS][3]) then
local block = LAYERS[li][2]
for z = minp.z, maxp.z do
for x = minp.x, maxp.x do
local vi = area:index(x, y, z)
data[vi] = block
end
end
else
-- air
end
end
-- Decorate terrain if enabled
if sflat.options.decoration == true then
sflat.decoration.generate(minp, maxp, LAYERS, data, area, seed, pr)
end
vm:set_data(data)
vm:set_lighting({day = 0, night = 0})
vm:update_liquids()
vm:calc_lighting()
vm:write_to_map(data)
end)
-- A timer for teleporting falling players to surface
sflat.bedrock_timer = 0
minetest.register_globalstep(function(dtime)
sflat.bedrock_timer = sflat.bedrock_timer - dtime
if sflat.bedrock_timer > 0 then return end
sflat.bedrock_timer = 1
for k, player in ipairs(minetest.get_connected_players()) do
local pos = player:get_pos()
if pos.y < sflat.Y_ORIGIN - 1 then
-- Prepare space for falling players
minetest.set_node({x = pos.x, y = LAYERS[#LAYERS][3] + 1, z = pos.z}, {name = "air"})
minetest.set_node({x = pos.x, y = LAYERS[#LAYERS][3], z = pos.z}, {name = "air"})
if minetest.registered_nodes[LAYERS[#LAYERS][1]] then
minetest.set_node({x = pos.x, y = LAYERS[#LAYERS][3] - 1, z = pos.z}, {name = LAYERS[#LAYERS][1]})
end
-- Teleport them back to surface
player:set_pos({x = pos.x, y = LAYERS[#LAYERS][3] - 0.5, z = pos.z})
end
end
end)