-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
273 lines (224 loc) · 7.95 KB
/
init.lua
File metadata and controls
273 lines (224 loc) · 7.95 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
-- (C) 2016 Tai "DuCake" Kedzierski
-- This program is Free Software, you can modify and redistribute it as long as
-- that you provide the same rights to whomever you provide the original or
-- modified version of the software to, and provide the source to whomever you
-- distribute the software to.
-- Released under the terms of the GPLv3
local default_mods = {
"default",
--"butterflies", -- not in older default game
"flowers",
"bones",
"beds",
"doors",
"fire",
"farming",
"stairs",
"farming",
"stairs",
"vessels",
"walls",
"wool",
"xpanes",
"moreblocks",
}
local function stringjoin(t, sep)
local final = ""
for _,v in ipairs(t) do
final = final..","..v
end
minetest.debug(final:sub(2))
return final:sub(2)
end
bbattle = {}
bbattle.radius = tonumber(minetest.settings:get("buildbattle.radius") ) or 16
bbattle.mods = minetest.settings:get("buildbattle.mods") or stringjoin(default_mods,",")
bbattle.forbidden = minetest.settings:get("buildbattle.forbidden") or ""
bbattle.mods = bbattle.mods:split(",")
bbattle.forbidden = bbattle.forbidden:split(",")
local notify_failures = minetest.settings:get_bool("buildbattle.report_registration_failures")
local allow_hidden_inventory = minetest.settings:get_bool("buildbattle.allow_hidden_inventory")
local require_all = minetest.settings:get_bool("buildbattle.require_all")
dofile(minetest.get_modpath("build_battle").."/forceloads.lua")
-- Groups to copy over from original block to BB block
-- All blocks become 'oddly_breakable_by_hand' unless 'dig_immediate' is on the original block
local allowed_groups = {
'attached_node',
'dig_immediate',
}
local function is_in_array(item,array)
for k,v in pairs(array) do
if v == item then
return true
end
end
return false
end
bbattle.is_in_bbfield = function(pos)
local mcount = minetest.find_nodes_in_area(
{x=pos.x-bbattle.radius,y=pos.y-bbattle.radius,z=pos.z-bbattle.radius},
{x=pos.x+bbattle.radius,y=pos.y+bbattle.radius,z=pos.z+bbattle.radius},
{"build_battle:marker"}
)
return #mcount > 0
end
local battlefy = function(name)
return "build_battle:"..name:gsub(":","_")
end
local battlize = function(name)
if type(name) == "string" then
return battlefy(name)
elseif type(name) == "table" then
local newnames = {}
for key,value in pairs(name) do
newnames[#newnames+1] = battlefy(key)
end
return newnames
else
return name
end
end
local function deepclone (t) -- deep-copy a table -- from https://gist.github.com/MihailJP/3931841
if type(t) ~= "table" then return t end
local target = {}
for k, v in pairs(t) do
if k ~= "__index" and type(v) == "table" then -- omit circular reference
target[k] = deepclone(v)
elseif k == "__index" then
target[k] = target -- own circular reference, not reference to original object!
else
target[k] = v
end
end
return target
end
local function sanitize_groups(def)
local newdef = {}
if not def then return {} end
for _,level in pairs(allowed_groups) do
newdef[level] = def[level]
end
-- Always easy to break by hand - this is a mini creative mode.
newdef['dig_immediate'] = 3
return newdef
end
local function mark_forceload(pos, nodename)
minetest.debug("Checking forceload "..nodename)
if nodename == "build_battle:marker" and bbattle.radius > 32 then -- FIXME this should be congruent with the server setting for active block send range
minetest.debug("Registering forceload on "..nodename.." at "..minetest.pos_to_string(pos))
bbattle.register_forceload(pos)
end
end
local function nullify(def)
for _,k in pairs({
"formspec",
"on_place", -- Defining on_place prevents on_placenode handlers from being called, which are needed to check for marker
"on_rightclick",
"drop",
"on_construct",
"on_destruct",
"after_destruct",
"on_flood",
"preserve_metadata",
"after_place_node",
"after_dig_node",
"can_dig",
"on_punch",
"on_dig",
"on_timer",
"on_use",
"on_receive_fields",
"allow_metadata_inventory_move",
"allow_metadata_inventory_put",
"allow_metadata_inventory_take",
"on_metadata_inventory_move",
}) do
def[k] = nil
end
-- Prevent from being affected by TNT
def.on_blast = function() end
return def
end
local function check_mods_loaded()
local modname,_
local all_mods_loaded = true
for _,modname in ipairs(bbattle.mods) do
if require_all and not minetest.get_modpath(modname) then
minetest.log("error", "Build Battle: add mod or mod dependency: "..modname)
all_mods_loaded = false
end
end
return all_mods_loaded
end
-- Active Script Commences
if not check_mods_loaded() then
minetest.log("error", "Dependencies were not met - aborting Build Battle registrations")
return
end
minetest.register_on_placenode( function(pos, newnode, placer, oldnode, itemstack, pointed_thing)
local node = newnode.name
if not node:find("build_battle:") then return end
mark_forceload(pos, node)
if not bbattle.is_in_bbfield(pos) then
minetest.chat_send_player(
placer:get_player_name(),
node.." can only be placed in a Build Battle Arena!"
)
minetest.swap_node(pos,{name = oldnode.name})
return true
end
end
)
local function should_register(oldnode, olddef, nodeparts)
return (not oldnode:find("build_battle:")
and is_in_array(nodeparts[1],bbattle.mods)
and not is_in_array(oldnode,bbattle.forbidden)
and not ( -- DISALLOW if
olddef.groups
and olddef.groups.not_in_creative_inventory -- CREATIVE denied
and not allow_hidden_inventory -- and CREATIVE override denied
)
)
end
for oldnode,olddef in pairs(minetest.registered_nodes) do
local nodeparts = oldnode:split(":")
if should_register(oldnode, olddef, nodeparts) then
local node = battlize(oldnode)
local def = deepclone(olddef)
def.drop = node
local desc = def.description or "("..oldnode..")"
def.description = desc.." +"
if def.liquid_alternative_flowing or def.liquid_alternative_source then
def.liquid_alternative_flowing = node:gsub("_source","_flowing")
def.liquid_alternative_source = node:gsub("_flowing","_source")
end
def.groups = sanitize_groups(def.groups)
def.groups.not_in_creative_inventory = 1
def = nullify(def)
minetest.register_node(node,def)
if not minetest.registered_nodes[node] and notify_failures then
minetest.debug("BB - Failed to register "..node) -- use "info" log level, as "error" level would get sent to clients
end
end
end
if notify_failures then
minetest.after(0,function()
for oldnode,olddef in pairs(minetest.registered_nodes) do
local nodeparts = oldnode:split(":")
if should_register(oldnode, olddef, nodeparts) then
local battlenode = battlize(oldnode)
if not minetest.registered_nodes[battlenode] then
minetest.debug("Build Battle ---- "..battlenode.." failed registration!")
end
end
end
end)
end
minetest.register_node("build_battle:marker", {
description = "Build Battle Marker",
tiles = {"default_stone.png^default_tool_diamondsword.png"},
groups = {unbreakable = 1}
})
dofile(minetest.get_modpath("build_battle").."/api.lua")
dofile(minetest.get_modpath("build_battle").."/buildbook.lua")
dofile(minetest.get_modpath("build_battle").."/areas.lua")