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