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