kpoppel
2013-07-02 053fa59739f4b772174bf0a090969b3395ab3f98
commit | author | age
ee5c6c 1 -- LV Tool workshop
K 2 -- This machine repairs tools.
82cba9 3 minetest.register_alias("tool_workshop", "technic:tool_workshop")
R 4 minetest.register_craft({
5     output = 'technic:tool_workshop',
6     recipe = {
7         {'default:wood', 'default:wood', 'default:wood'},
8d092c 8         {'default:wood', 'default:diamond', 'default:wood'},
82cba9 9         {'default:stone', 'moreores:copper_ingot', 'default:stone'},
R 10     }
11 })
12
13 minetest.register_craftitem("technic:tool_workshop", {
14     description = "Tool Workshop",
15     stack_max = 99,
16 }) 
17
ee5c6c 18 local workshop_formspec =
K 19    "invsize[8,9;]"..
20    "list[current_name;src;3,1;1,1;]"..
21    "label[0,0;Tool Workshop]"..
22    "list[current_player;main;0,5;8,4;]"
82cba9 23
ee5c6c 24 minetest.register_node(
K 25    "technic:tool_workshop",
26    {
27       description = "Tool Workshop",
28       tiles = {"technic_workshop_top.png", "technic_machine_bottom.png", "technic_workshop_side.png",
29            "technic_workshop_side.png", "technic_workshop_side.png", "technic_workshop_side.png"},
30       groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
31       sounds = default.node_sound_wood_defaults(),
32       on_construct = function(pos)
33             local meta = minetest.env:get_meta(pos)
34             meta:set_string("infotext", "Tool Workshop")
35             meta:set_float("technic_power_machine", 1)
36             meta:set_string("formspec", workshop_formspec)
37             local inv = meta:get_inventory()
38             inv:set_size("src", 1)
39              end,    
40       can_dig = function(pos,player)
41            local meta = minetest.env:get_meta(pos);
42            local inv = meta:get_inventory()
43            if not inv:is_empty("src") then
44               minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
45               return false
46            end
47            return true
48         end,
49    })
82cba9 50
ee5c6c 51 minetest.register_abm(
K 52    { nodenames = {"technic:tool_workshop"},
53      interval = 1,
54      chance   = 1,
55      action = function(pos, node, active_object_count, active_object_count_wider)
56          local meta         = minetest.env:get_meta(pos)
57          local eu_input     = meta:get_int("LV_EU_input")
58          local state        = meta:get_int("state")
59          local next_state   = state
82cba9 60
ee5c6c 61          -- Machine information
K 62          local machine_name         = "Tool Workshop"
63          local machine_node         = "technic:tool_workshop"
64          local machine_state_demand = { 50, 150 }
65              
66          -- Setup meta data if it does not exist. state is used as an indicator of this
67          if state == 0 then
68             meta:set_int("state", 1)
69             meta:set_int("LV_EU_demand", machine_state_demand[1])
70             meta:set_int("LV_EU_input", 0)
71             return
72          end
73              
74          -- Power off automatically if no longer connected to a switching station
75          technic.switching_station_timeout_count(pos, "LV")
76              
77          -- State machine
78          if eu_input == 0 then
79             -- Unpowered - go idle
80             --hacky_swap_node(pos, machine_node)
81             meta:set_string("infotext", machine_name.." Unpowered")
82             next_state = 1
83          elseif eu_input == machine_state_demand[state] then
84             -- Powered - do the state specific actions
85             local inv = meta:get_inventory()
86                 
87             if state == 1 then
88                --hacky_swap_node(pos, machine_node)
89                meta:set_string("infotext", machine_name.." Idle")
90                if not inv:is_empty("src") then
91               next_state = 2
92                end
93             elseif state == 2 then
94                --hacky_swap_node(pos, machine_node.."_active")
95                meta:set_string("infotext", machine_name.." Active")
612349 96
ee5c6c 97                if inv:is_empty("src") then
K 98               next_state = 1
99                else
100               srcstack = inv:get_stack("src", 1)
101               src_item=srcstack:to_table()
102               -- Cannot charge cans
103               if (src_item["name"]=="technic:water_can" or src_item["name"]=="technic:lava_can") then
104                  return
105               end
106               local wear=tonumber(src_item["wear"])
107               wear = math.max(1, wear-2000) -- Improve the tool this much every tick
108               src_item["wear"]=tostring(wear)
109               inv:set_stack("src", 1, src_item)
110                end
111             end
112          end
113          -- Change state?
114          if next_state ~= state then
115             meta:set_int("LV_EU_demand", machine_state_demand[next_state])
116             meta:set_int("state", next_state)
117          end
118           end
119    }) 
120
121 technic.register_LV_machine ("technic:tool_workshop","RE")
612349 122