-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.lua
118 lines (96 loc) · 3.28 KB
/
api.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
------------------------------------------------------------
-- Copyright (c) 2016 tacigar. All rights reserved.
-- https://github.com/tacigar/siva
------------------------------------------------------------
-- siva.registered_methods represents a table that contains
-- registered methods by siva.register_method function.
siva.registered_methods = {}
-- siva.register_method registers a new definition of digging method.
-- Its function must return a table.
function siva.register_method(method_name, def)
siva.registered_methods[method_name] = def
end
-- is_activated represents whether siva system is activated.
local is_activated = true
-- siva.activate activates the siva system.
function siva.activate()
is_activated = true
end
-- siva.deactivate deactivates the siva system.
function siva.deactivate()
is_activated = false
end
-- node_method_map represents a table that maps nodename and dig method.
local node_method_map = {}
-- siva.associate_node_with_method associates node with method (and arguments)
function siva.associate_node_with_method(nodename, method_name, arguments)
local args = arguments or siva.registered_methods[method_name].default_arguments
node_method_map[nodename] = {
method_name = method_name,
arguments = args,
}
end
-- siva.default_settings set default siva settings.
function siva.default_settings()
for nodename, nodedef in pairs(minetest.registered_nodes) do
if nodedef.drawtype == "normal" then
if minetest.get_item_group(nodename, "tree") > 0 then
siva.associate_node_with_method(nodename, "siva:tree")
elseif siva.util.is_dirt(nodename) then
siva.associate_node_with_method(nodename, "siva:dirt")
else
siva.associate_node_with_method(nodename, "siva:basic")
end
end
end
end
-- load a siva config file.
;(function()
local worldpath = minetest.get_worldpath()
local file = io.open(worldpath .. "/siva-config", "r")
if file then
local config = minetest.deserialize(file:read("*a"))
is_activated = config.is_activated
node_method_map = config.node_method_map
file:close()
return
end
minetest.after(0, function()
siva.default_settings()
end)
end) ()
-- save a sive config file when minetest is finalized.
minetest.register_on_shutdown(function()
local worldpath = minetest.get_worldpath()
local file = io.open(worldpath .. "/siva-config", "w")
if file then
local config = {
is_activated = is_activated,
node_method_map = node_method_map,
}
file:write(minetest.serialize(config))
file:close()
end
end)
-- register on dignode
;(function()
minetest.register_on_dignode(function(pos, oldnode, digger)
if (not is_activated) or (not node_method_map[oldnode.name]) then
return
end
local method_name = node_method_map[oldnode.name].method_name
local method_func = siva.registered_methods[method_name].method_func
local arguments = node_method_map[oldnode.name].arguments
local targets = method_func(pos, oldnode, digger, arguments)
local inv = digger:get_inventory()
for _, target in ipairs(targets) do
local node = minetest.get_node(target)
local drops = minetest.get_node_drops(node.name)
minetest.remove_node(target)
for _, itemname in ipairs(drops) do
local leftover = inv:add_item("main", ItemStack(itemname))
minetest.add_item(pos, leftover)
end
end
end)
end) ()