ShadowNinja
2013-10-06 363f0332788e04e2e4bb63af5cd21fac5ae56ae5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 
function technic.handle_machine_upgrades(meta)
    -- Get the names of the upgrades
    local inv = meta:get_inventory()
    local upg_item1
    local upg_item2
    local srcstack = inv:get_stack("upgrade1", 1)
    if srcstack then
        upg_item1 = srcstack:to_table()
    end
    srcstack = inv:get_stack("upgrade2", 1)
    if srcstack then
        upg_item2 = srcstack:to_table()
    end
 
    -- Save some power by installing battery upgrades.
    -- Tube loading speed can be upgraded using control logic units.
    local EU_upgrade = 0
    local tube_upgrade = 0
    if upg_item1 then
        if     upg_item1.name == "technic:battery" then
            EU_upgrade = EU_upgrade + 1
        elseif upg_item1.name == "technic:control_logic_unit" then
            tube_upgrade = tube_upgrade + 1
        end
    end
    if upg_item2 then
        if     upg_item2.name == "technic:battery" then
            EU_upgrade = EU_upgrade + 1
        elseif upg_item2.name == "technic:control_logic_unit" then
            tube_upgrade = tube_upgrade + 1
        end
    end
    return EU_upgrade, tube_upgrade
end
 
 
function technic.send_items(pos, x_velocity, z_velocity)
    -- Send items on their way in the pipe system.
    local meta = minetest.get_meta(pos) 
    local inv = meta:get_inventory()
    local i = 0
    for _, stack in ipairs(inv:get_list("dst")) do
        i = i + 1
        if stack then
            local item0 = stack:to_table()
            if item0 then 
                item0["count"] = "1"
                local item1 = tube_item({x=pos.x, y=pos.y, z=pos.z}, item0)
                item1:get_luaentity().start_pos = {x=pos.x, y=pos.y, z=pos.z}
                item1:setvelocity({x=x_velocity, y=0, z=z_velocity})
                item1:setacceleration({x=0, y=0, z=0})
                stack:take_item(1)
                inv:set_stack("dst", i, stack)
                return
            end
        end
    end
end
 
 
function technic.smelt_item(meta, result, speed)
    local inv = meta:get_inventory()
    meta:set_int("cook_time", meta:get_int("cook_time") + 1)
    if meta:get_int("cook_time") < result.time / speed then
        return
    end
    local result = minetest.get_craft_result({method = "cooking", width = 1, items = inv:get_list("src")})
 
    if result and result.item then
        meta:set_int("cook_time", 0)
        -- check if there's room for output in "dst" list
        if inv:room_for_item("dst", result.item) then
            srcstack = inv:get_stack("src", 1)
            srcstack:take_item()
            inv:set_stack("src", 1, srcstack)
            inv:add_item("dst", result.item)
        end
    end
end
 
function technic.handle_machine_pipeworks(pos, tube_upgrade)
    local node = minetest.get_node(pos)
    local meta = minetest.get_meta(pos)
    local inv = meta:get_inventory()
    local pos1 = vector.new(pos)
    local x_velocity = 0
    local z_velocity = 0
 
    -- Output is on the left side of the furnace
    if node.param2 == 3 then pos1.z = pos1.z - 1  z_velocity = -1 end
    if node.param2 == 2 then pos1.x = pos1.x - 1  x_velocity = -1 end
    if node.param2 == 1 then pos1.z = pos1.z + 1  z_velocity =  1 end
    if node.param2 == 0 then pos1.x = pos1.x + 1  x_velocity =  1 end
 
    local output_tube_connected = false
    local meta1 = minetest.get_meta(pos1) 
    if meta1:get_int("tubelike") == 1 then
        output_tube_connected = true
    end
    tube_time = meta:get_int("tube_time")
    tube_time = tube_time + tube_upgrade
    if tube_time > 3 then
        tube_time = 0
        if output_tube_connected then
            technic.send_items(pos, x_velocity, z_velocity)
        end
    end
    meta:set_int("tube_time", tube_time)
end