ShadowNinja
2013-07-11 5d470cd753efe8f4640099165a7bfc0c6e181c35
commit | author | age
ce5dfa 1 technic.extractor_recipes ={}
M 2
e606b9 3 technic.register_extractor_recipe = function(src, src_count, dst, dst_count)
KO 4                    technic.extractor_recipes[src] = {src_count = src_count, dst_name = dst, dst_count = dst_count}
ce5dfa 5                    if unified_inventory then
M 6                       unified_inventory.register_craft(
7                      {
8                         type = "extracting",
e606b9 9                         output = dst.." "..dst_count,
KO 10                         items = {src.." "..src_count},
ce5dfa 11                         width = 0,
M 12                      })
13                    end
14                 end
15
16 -- Receive an ItemStack of result by an ItemStack input
e606b9 17 technic.get_extractor_recipe = function(item)
KO 18                 if technic.extractor_recipes[item.name]
19                    and item.count >= technic.extractor_recipes[item.name].src_count then
20                    return technic.extractor_recipes[item.name]
ce5dfa 21                 else
M 22                    return nil
23                 end
24                  end
25
26
27
e606b9 28 technic.register_extractor_recipe("technic:coal_dust", 1, "dye:black", 2)
KO 29 technic.register_extractor_recipe("default:cactus", 1, "dye:green", 2)
30 technic.register_extractor_recipe("default:dry_shrub", 1, "dye:brown", 2)
31 technic.register_extractor_recipe("flowers:geranium", 1, "dye:blue", 2)
32 technic.register_extractor_recipe("flowers:dandelion_white", 1, "dye:white", 2)
33 technic.register_extractor_recipe("flowers:dandelion_yellow", 1, "dye:yellow", 2)
34 technic.register_extractor_recipe("flowers:tulip", 1, "dye:orange", 2)
35 technic.register_extractor_recipe("flowers:rose", 1, "dye:red", 2)
36 technic.register_extractor_recipe("flowers:viola", 1, "dye:violet", 2)
37 technic.register_extractor_recipe("technic:raw_latex", 1, "technic:rubber", 3)
38 technic.register_extractor_recipe("moretrees:rubber_tree_trunk_empty", 1, "technic:rubber", 1)
39 technic.register_extractor_recipe("moretrees:rubber_tree_trunk", 1, "technic:rubber", 1)
8be389 40 technic.register_extractor_recipe("technic:uranium", 5, "technic:enriched_uranium", 1)
ce5dfa 41
M 42 minetest.register_alias("extractor", "technic:extractor")
43 minetest.register_craft({
44                output = 'technic:extractor',
45                recipe = {
46                   {'technic:treetap', 'technic:motor', 'technic:treetap'},
47                   {'technic:treetap', 'technic:lv_cable', 'technic:treetap'},
48                   {'','',''},
49                }
50             })
51
52 minetest.register_craftitem("technic:extractor", {
53                    description = "Extractor",
54                    stack_max = 99,
55                 })
56
57 local extractor_formspec =
58    "invsize[8,9;]"..
59    "label[0,0;Extractor]"..
60    "list[current_name;src;3,1;1,1;]"..
61    "list[current_name;dst;5,1;2,2;]"..
62    "list[current_player;main;0,5;8,4;]"
63
64 minetest.register_node(
65    "technic:extractor",
66    {
67       description = "Extractor",
68       tiles = {"technic_lv_grinder_top.png", "technic_lv_grinder_bottom.png", "technic_lv_grinder_side.png",
69            "technic_lv_grinder_side.png", "technic_lv_grinder_side.png", "technic_lv_grinder_front.png"},
70       paramtype2 = "facedir",
71       groups = {cracky=2},
72       legacy_facedir_simple = true,
73       sounds = default.node_sound_wood_defaults(),
74       on_construct = function(pos)
75             local meta = minetest.env:get_meta(pos)
76             meta:set_string("infotext", "Extractor")
77             meta:set_float("technic_power_machine", 1)
78             meta:set_string("formspec", extractor_formspec)
79             local inv = meta:get_inventory()
80             inv:set_size("src", 1)
81             inv:set_size("dst", 4)
82              end,
83       can_dig = function(pos,player)
84            local meta = minetest.env:get_meta(pos);
85            local inv = meta:get_inventory()
86            if not inv:is_empty("src") or not inv:is_empty("dst") then
87               minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
88               return false
89            else
90               return true
91            end
92         end,
93    })
94
95 minetest.register_node(
96    "technic:extractor_active",
97    {
98       description = "Extractor",
99       tiles = {"technic_lv_grinder_top.png", "technic_lv_grinder_bottom.png", "technic_lv_grinder_side.png",
100            "technic_lv_grinder_side.png", "technic_lv_grinder_side.png", "technic_lv_grinder_front_active.png"},
101       paramtype2 = "facedir",
102       groups = {cracky=2,not_in_creative_inventory=1},
103       legacy_facedir_simple = true,
104       sounds = default.node_sound_wood_defaults(),
105       can_dig = function(pos,player)
106            local meta = minetest.env:get_meta(pos);
107            local inv = meta:get_inventory()
108            if not inv:is_empty("src") or not inv:is_empty("dst") then
109               minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
110               return false
111            else
112               return true
113            end
114         end,
115    })
116
117 minetest.register_abm(
118    { nodenames = {"technic:extractor","technic:extractor_active"},
119      interval = 1,
120      chance   = 1,
121      action = function(pos, node, active_object_count, active_object_count_wider)
122          -- Run a machine through its states. Takes the same arguments as the ABM action
123          -- and adds the machine's states and any extra data which is needed by the machine.
124          -- A machine is characterized by running through a set number of states (usually 2:
125          -- Idle and active) in some order. A state decides when to move to the next one
126          -- and the machine only changes state if it is powered correctly.
127          -- The machine will automatically shut down if disconnected from power in some fashion.
128          local meta         = minetest.env:get_meta(pos)
129          local eu_input     = meta:get_int("LV_EU_input")
130          local state        = meta:get_int("state")
131          local next_state   = state
132
133          -- Machine information
134          local machine_name         = "Extractor"
135          local machine_node         = "technic:extractor"
136          local machine_state_demand = { 50, 300 }
137              
138          -- Setup meta data if it does not exist. state is used as an indicator of this
139          if state == 0 then
140             meta:set_int("state", 1)
141             meta:set_int("LV_EU_demand", machine_state_demand[1])
142             meta:set_int("LV_EU_input", 0)
143             return
144          end
145              
146          -- Power off automatically if no longer connected to a switching station
147          technic.switching_station_timeout_count(pos, "LV")
148              
149          -- State machine
150          if eu_input == 0 then
151             -- unpowered - go idle
152             hacky_swap_node(pos, machine_node)
153             meta:set_string("infotext", machine_name.." Unpowered")
154             next_state = 1
155          elseif eu_input == machine_state_demand[state] then
156             -- Powered - do the state specific actions
157                 
158             local inv    = meta:get_inventory()
159             local empty  = inv:is_empty("src")
e606b9 160             local srcstack  = inv:get_stack("src", 1)
KO 161             local src_item = nil
162             local recipe = nil
163             local result = nil
164             
165             if srcstack then
166                src_item = srcstack:to_table()
167             end
168             if src_item then
169                recipe = technic.get_extractor_recipe(src_item)
170             end
171             if recipe then
172                result = {name=recipe.dst_name, count=recipe.dst_count}
173             end 
174             
ce5dfa 175             if state == 1 then
M 176                hacky_swap_node(pos, machine_node)
177                meta:set_string("infotext", machine_name.." Idle")
178
179                if not empty and result and inv:room_for_item("dst",result) then
180               meta:set_int("src_time", 0)
181               next_state = 2
182                end
183
184             elseif state == 2 then
185                hacky_swap_node(pos, machine_node.."_active")
186                meta:set_string("infotext", machine_name.." Active")
187
188                if empty then
189               next_state = 1
190                else
191               meta:set_int("src_time", meta:get_int("src_time") + 1)
192               if meta:get_int("src_time") == 4 then -- 4 ticks per output
193                  -- check if there's room for output in "dst" list
194
195                  meta:set_int("src_time", 0)
e606b9 196                  if recipe and inv:room_for_item("dst",result) then
ce5dfa 197                 -- take stuff from "src" list
e606b9 198                 srcstack:take_item(recipe.src_count)
KO 199                     inv:set_stack("src", 1, srcstack)
ce5dfa 200                 -- Put result in "dst" list
M 201                 inv:add_item("dst", result)
202                  else
203                 -- all full: go idle
204                 next_state = 1
205                  end
206               end
207                end
208             end
209          end
210          -- Change state?
211          if next_state ~= state then
212             meta:set_int("LV_EU_demand", machine_state_demand[next_state])
213             meta:set_int("state", next_state)
214          end
215           end
216   })
217
218 technic.register_LV_machine ("technic:extractor","RE")
219 technic.register_LV_machine ("technic:extractor_active","RE")
220