]> git.lizzy.rs Git - xdecor.git/blob - worktable.lua
4753a520776d51867f094a59afa2d7139d87692b
[xdecor.git] / worktable.lua
1 local worktable = {}
2
3 local material = {
4         "cloud", -- Only used for the formspec display.
5         "wood", "junglewood", "pinewood", "acacia_wood",
6         "tree", "jungletree", "pinetree", "acacia_tree",
7         "cobble", "mossycobble", "desert_cobble",
8         "stone", "sandstone", "desert_stone", "obsidian",
9         "stonebrick", "sandstonebrick", "desert_stonebrick", "obsidianbrick",
10         "snowblock", "coalblock", "copperblock", "steelblock", "goldblock", 
11         "bronzeblock", "mese", "diamondblock",
12         "brick", "cactus", "clay", "ice", "meselamp",
13         "glass", "obsidian_glass"
14 }
15
16 local def = { -- Node name, yield, nodebox shape.
17         {"nanoslab", "16", {-0.5, -0.5, -0.5, 0, -0.4375, 0}},
18         {"micropanel", "16", {-0.5, -0.5, -0.5, 0.5, -0.4375, 0}},
19         {"microslab", "8", {-0.5, -0.5, -0.5, 0.5, -0.4375, 0.5}},
20         {"panel", "4", {-0.5, -0.5, -0.5, 0.5, 0, 0}},
21         {"slab", "2", {-0.5, -0.5, -0.5, 0.5, 0, 0.5}},
22         {"outerstair", "1", {{-0.5, -0.5, -0.5, 0.5, 0, 0.5}, {-0.5, 0, 0, 0, 0.5, 0.5}}},
23         {"stair", "1", {{-0.5, -0.5, -0.5, 0.5, 0, 0.5}, {-0.5, 0, 0, 0.5, 0.5, 0.5}}},
24         {"innerstair", "1", {{-0.5, -0.5, -0.5, 0.5, 0, 0.5}, {-0.5, 0, 0, 0.5, 0.5, 0.5}, {-0.5, 0, -0.5, 0, 0.5, 0}}}
25 }
26
27 function worktable.construct(pos)
28         local meta = minetest.get_meta(pos)
29
30         local nodebtn = {}
31         for i=1, #def do
32                 nodebtn[#nodebtn+1] = "item_image_button["..(i-1)..
33                                 ",0.5;1,1;xdecor:"..def[i][1].."_cloud;"..def[i][1]..";]"
34         end
35         nodebtn = table.concat(nodebtn)
36
37         meta:set_string("formspec", "size[8,7;]"..xdecor.fancy_gui..
38                 "label[0,0;Cut your material into...]"..
39                 nodebtn..
40                 "label[0,1.5;Input]"..
41                 "list[current_name;input;0,2;1,1;]"..
42                 "image[1,2;1,1;xdecor_saw.png]"..
43                 "label[2,1.5;Output]"..
44                 "list[current_name;output;2,2;1,1;]"..
45                 "label[5,1.5;Tool]"..
46                 "list[current_name;tool;5,2;1,1;]"..
47                 "image[6,2;1,1;xdecor_anvil.png]"..
48                 "label[6.8,1.5;Hammer]]"..
49                 "list[current_name;hammer;7,2;1,1;]"..
50                 "list[current_player;main;0,3.25;8,4;]")
51         meta:set_string("infotext", "Work Table")
52
53         local inv = meta:get_inventory()
54         inv:set_size("output", 1)
55         inv:set_size("input", 1)
56         inv:set_size("tool", 1)
57         inv:set_size("hammer", 1)
58 end
59
60 function worktable.fields(pos, formname, fields, sender)
61         local meta = minetest.get_meta(pos)
62         local inv = meta:get_inventory()
63         local inputstack = inv:get_stack("input", 1)
64         local outputstack = inv:get_stack("output", 1)
65         local outputcount = outputstack:get_count()
66         local inputname = inputstack:get_name()
67         local shape, get = {}, {}
68         local anz = 0
69
70         for _, d in pairs(def) do
71                 local nb, anz = d[1], d[2]
72                 if outputcount < 99 and fields[nb] then
73                         shape = "xdecor:"..nb.."_"..string.sub(inputname, 9)
74                         get = shape.." "..anz
75
76                         if not minetest.registered_nodes[shape] then return end
77                         inv:add_item("output", get)
78                         inputstack:take_item()
79                         inv:set_stack("input", 1, inputstack)
80                 end
81         end
82 end
83
84 function worktable.dig(pos, player)
85         local meta = minetest.get_meta(pos)
86         local inv = meta:get_inventory()
87
88         if not inv:is_empty("input") or not inv:is_empty("output") or not
89                         inv:is_empty("hammer") or not inv:is_empty("tool") then
90                 return false
91         end
92         return true
93 end
94
95 function worktable.put(pos, listname, index, stack, player)
96         local stackname = stack:get_name()
97         local count = stack:get_count()
98
99         if listname == "output" then return 0 end
100         if listname == "input" then
101                 if string.find(stackname, "default:") then return count
102                 else return 0 end
103         end
104         if listname == "hammer" then
105                 if not (stackname == "xdecor:hammer") then return 0 end
106         end
107         if listname == "tool" then
108                 local tdef = minetest.registered_tools[stackname]
109                 local twear = stack:get_wear()
110                 if not (tdef and twear > 0) then return 0 end
111         end
112
113         return count
114 end
115
116 xdecor.register("worktable", {
117         description = "Work Table",
118         groups = {cracky=2},
119         sounds = xdecor.wood,
120         tiles = {
121                 "xdecor_worktable_top.png", "xdecor_worktable_top.png",
122                 "xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
123                 "xdecor_worktable_front.png", "xdecor_worktable_front.png"
124         },
125         on_construct = worktable.construct,
126         on_receive_fields = worktable.fields,
127         can_dig = worktable.dig,
128         allow_metadata_inventory_put = worktable.put
129 })
130
131 for _, m in pairs(material) do
132 for n=1, #def do
133         local w = def[n]
134         local nodename = "default:"..m
135         local ndef = minetest.registered_nodes[nodename]
136         if not ndef then return end
137
138         local function description(m)
139                 if m == "cloud" then return "" end
140                 return string.gsub(m, "%l", string.upper, 1).." "..string.gsub(w[1], "%l", string.upper, 1)
141         end
142
143         local function groups(m)
144                 if string.find(m, "tree") or string.find(m, "wood") or m == "cactus" then
145                         return {choppy=3, not_in_creative_inventory=1}
146                 elseif m == "clay" or m == "snowblock" then
147                         return {snappy=3, not_in_creative_inventory=1}
148                 end
149                 return {cracky=3, not_in_creative_inventory=1}
150         end
151
152         local function shady(w)
153                 if string.find(w, "stair") or w == "slab" then return false end
154                 return true
155         end
156
157         xdecor.register(w[1].."_"..m, {
158                 description = description(m),
159                 light_source = ndef.light_source,
160                 sounds = ndef.sounds,
161                 tiles = ndef.tiles,
162                 groups = groups(m),
163                 node_box = {type = "fixed", fixed = w[3]},
164                 sunlight_propagates = shady(w[1]),
165                 on_place = minetest.rotate_node
166         })
167 end
168 end
169
170 minetest.register_abm({
171         nodenames = {"xdecor:worktable"},
172         interval = 3, chance = 1,
173         action = function(pos, node, active_object_count, active_object_count_wider)
174                 local meta = minetest.get_meta(pos)
175                 local inv = meta:get_inventory()
176                 local tool = inv:get_stack("tool", 1)
177                 local hammer = inv:get_stack("hammer", 1)
178                 local wear = tool:get_wear()
179                 local wear2 = hammer:get_wear()
180
181                 local repair = -500 -- Tool's repairing factor (0-65535 -- 0 = new condition).
182                 local wearhammer = 250 -- Hammer's wearing factor (0-65535 -- 0 = new condition).
183
184                 if tool:is_empty() or hammer:is_empty() or wear == 0 then return end
185
186                 tool:add_wear(repair)
187                 hammer:add_wear(wearhammer)
188
189                 inv:set_stack("tool", 1, tool)
190                 inv:set_stack("hammer", 1, hammer)
191         end
192 })