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