Zefram
2014-07-03 636b0f20df4fef3eef821c23ccc2f606828f5c6f
commit | author | age
aa8af0 1
N 2 local S = technic.getter
3
4 local tube = {
5     insert_object = function(pos, node, stack, direction)
6         local meta = minetest.get_meta(pos)
7         local inv = meta:get_inventory()
8         return inv:add_item("src", stack)
9     end,
10     can_insert = function(pos, node, stack, direction)
11         local meta = minetest.get_meta(pos)
12         local inv = meta:get_inventory()
13         return inv:room_for_item("src", stack)
14     end,
15     connect_sides = {left = 1, right = 1, back = 1, top = 1, bottom = 1},
16 }
17
18 function technic.register_base_machine(data)
19     local typename = data.typename
d55ecc 20     local numitems = technic.recipes[typename].numitems
aa8af0 21     local machine_name = data.machine_name
N 22     local machine_desc = data.machine_desc
23     local tier = data.tier
24     local ltier = string.lower(tier)
25
26     local groups = {cracky = 2}
27     local active_groups = {cracky = 2, not_in_creative_inventory = 1}
28     if data.tube then
29         groups.tubedevice = 1
30         groups.tubedevice_receiver = 1
31         active_groups.tubedevice = 1
32         active_groups.tubedevice_receiver = 1
33     end
34
35
36     local formspec =
37         "invsize[8,9;]"..
d55ecc 38         "list[current_name;src;"..(4-numitems)..",1;"..numitems..",1;]"..
aa8af0 39         "list[current_name;dst;5,1;2,2;]"..
N 40         "list[current_player;main;0,5;8,4;]"..
5f2d09 41         "label[0,0;"..machine_desc:format(tier).."]"
aa8af0 42     if data.upgrade then
N 43         formspec = formspec..
44             "list[current_name;upgrade1;1,3;1,1;]"..
45             "list[current_name;upgrade2;2,3;1,1;]"..
46             "label[1,4;"..S("Upgrade Slots").."]"
47     end
48
49     minetest.register_node("technic:"..ltier.."_"..machine_name, {
50         description = machine_desc:format(tier),
51         tiles = {"technic_"..ltier.."_"..machine_name.."_top.png", 
52                      "technic_"..ltier.."_"..machine_name.."_bottom.png",
53                  "technic_"..ltier.."_"..machine_name.."_side.png",
54                  "technic_"..ltier.."_"..machine_name.."_side.png",
55                  "technic_"..ltier.."_"..machine_name.."_side.png",
56                  "technic_"..ltier.."_"..machine_name.."_front.png"},
57         paramtype2 = "facedir",
58         groups = groups,
59         tube = data.tube and tube or nil,
60         legacy_facedir_simple = true,
61         sounds = default.node_sound_wood_defaults(),
62         on_construct = function(pos)
63             local node = minetest.get_node(pos)
64             local meta = minetest.get_meta(pos)
65             meta:set_string("infotext", machine_desc:format(tier))
66             meta:set_int("tube_time",  0)
67             meta:set_string("formspec", formspec)
68             local inv = meta:get_inventory()
d55ecc 69             inv:set_size("src", numitems)
aa8af0 70             inv:set_size("dst", 4)
N 71             inv:set_size("upgrade1", 1)
72             inv:set_size("upgrade2", 1)
73         end,
74         can_dig = technic.machine_can_dig,
75         allow_metadata_inventory_put = technic.machine_inventory_put,
76         allow_metadata_inventory_take = technic.machine_inventory_take,
77         allow_metadata_inventory_move = technic.machine_inventory_move,
78     })
79
80     minetest.register_node("technic:"..ltier.."_"..machine_name.."_active",{
81         description = machine_desc:format(tier),
82         tiles = {"technic_"..ltier.."_"..machine_name.."_top.png",
83                  "technic_"..ltier.."_"..machine_name.."_bottom.png",
84                  "technic_"..ltier.."_"..machine_name.."_side.png",
85                  "technic_"..ltier.."_"..machine_name.."_side.png",
86                  "technic_"..ltier.."_"..machine_name.."_side.png",
87                  "technic_"..ltier.."_"..machine_name.."_front_active.png"},
88         paramtype2 = "facedir",
89         drop = "technic:"..ltier.."_"..machine_name,
90         groups = active_groups,
91         legacy_facedir_simple = true,
92         sounds = default.node_sound_wood_defaults(),
93         tube = data.tube and tube or nil,
94         can_dig = technic.machine_can_dig,
95         allow_metadata_inventory_put = technic.machine_inventory_put,
96         allow_metadata_inventory_take = technic.machine_inventory_take,
97         allow_metadata_inventory_move = technic.machine_inventory_move,
98     })
99
100     minetest.register_abm({
101         nodenames = {"technic:"..ltier.."_"..machine_name,
102                      "technic:"..ltier.."_"..machine_name.."_active"},
103         interval = 1,
104         chance   = 1,
105         action = function(pos, node, active_object_count, active_object_count_wider)
106             local meta     = minetest.get_meta(pos)
107             local inv      = meta:get_inventory()
108             local eu_input = meta:get_int(tier.."_EU_input")
109
110             local machine_desc_tier = machine_desc:format(tier)
111             local machine_node      = "technic:"..ltier.."_"..machine_name
112             local machine_demand    = data.demand
113
114             -- Setup meta data if it does not exist.
115             if not eu_input then
116                 meta:set_int(tier.."_EU_demand", machine_demand[1])
117                 meta:set_int(tier.."_EU_input", 0)
118                 return
119             end
120         
121             -- Power off automatically if no longer connected to a switching station
122             technic.switching_station_timeout_count(pos, tier)
123
124             local EU_upgrade, tube_upgrade = 0, 0
125             if data.upgrade then
126                 EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
127             end
128             if data.tube then
129                 technic.handle_machine_pipeworks(pos, tube_upgrade)
130             end
131
d55ecc 132             local result = technic.get_recipe(typename, inv:get_list("src"))
aa8af0 133
N 134             if not result then
135                 technic.swap_node(pos, machine_node)
136                 meta:set_string("infotext", S("%s Idle"):format(machine_desc_tier))
137                 meta:set_int(tier.."_EU_demand", 0)
138                 return
139             end
140         
141             if eu_input < machine_demand[EU_upgrade+1] then
142                 -- Unpowered - go idle
143                 technic.swap_node(pos, machine_node)
144                 meta:set_string("infotext", S("%s Unpowered"):format(machine_desc_tier))
145             elseif eu_input >= machine_demand[EU_upgrade+1] then
146                 -- Powered    
147                 technic.swap_node(pos, machine_node.."_active")
148                 meta:set_string("infotext", S("%s Active"):format(machine_desc_tier))
149
150                 meta:set_int("src_time", meta:get_int("src_time") + 1)
151                 if meta:get_int("src_time") >= result.time / data.speed then
152                     meta:set_int("src_time", 0)
153                     local result_stack = ItemStack(result.output)
154                     if inv:room_for_item("dst", result_stack) then
d55ecc 155                         inv:set_list("src", result.new_input)
aa8af0 156                         inv:add_item("dst", result_stack)
N 157                     end
158                 end
159             end
160             meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1])
161         end
162     })
163
164     technic.register_machine(tier, "technic:"..ltier.."_"..machine_name,            technic.receiver)
165     technic.register_machine(tier, "technic:"..ltier.."_"..machine_name.."_active", technic.receiver)
166
167 end -- End registration
168