View difference between Paste ID: 06cjixf9 and UnRJqdZA
SHOW: | | - or go back to the newest paste.
1
-- Furnace control
2
-- Setup:
3
-- (from above)
4-
local output = peripheral.wrap("bottom")
4+
-- Comp - Outp
5
-- Inpu - Furn
6
-- the second computer under the furnace,
7-
local signal = "back"
7+
-- and a wired modem connecting the 2 computers
8-
-- side of the redstone torch
8+
9
-- Computer 1: (this one) http://pastebin.com/06cjixf9
10-
local inputslot = 1
10+
-- Computer 2: http://pastebin.com/RjyREdT2
11-
local fuelslot = 2
11+
12-
local outputslot = 3
12+
-- Screenshots of setup:
13-
-- slots inside furnace inv
13+
-- 1 http://i.imgur.com/t9GFE2r.png
14
-- 2 http://i.imgur.com/WjnunBC.png
15
16-
  -- {id=number,dmg=number,burn=number}
16+
-- how often computer shall retry
17-
  -- burn = burntime, how many items
17+
local updaterate = 2 -- times a sec
18-
  -- the fuel can burn.
18+
19-
  -- ex: coal/charcoal burns 8
19+
-- where the chests are in relation to the furnace
20-
  -- and wood burns 1.5
20+
local outputToFurnace = "north"
21-
  {id=263,dmg=0,burn=8},--coal
21+
local inputToFurnace = "west"
22-
  {id=263,dmg=1,burn=8},--charcoal
22+
-- directions: north,south,east,west,up,down
23-
  --{id=5,dmg=-1,burn=1.5},--planks
23+
24-
  --{id=17,dmg=-1,burn=1.5},--wood
24+
-- slots in the furnace inventory
25-
  --{id=6,dmg=-1,burn=.5},--sapling
25+
local inputSlot = 0
26-
  --{id=280,dmg=0,burn=.5},--stick
26+
local fuelSlot = 1
27
local outputSlot = 2
28
29-
local furnaceToOutput = "west"
29+
-- 1 for normal furnace
30-
local inputToFurnace = "down"
30+
-- 1.25 for IC2 iron furnace
31-
local inputSlot = 1
31+
local furnaceModifier = 1
32-
local outputSlot = 3
32+
33
local fuel = {
34-
-- returns statement,fuel
34+
	-- {id,dmg,burntime},
35
	{id=263,dmg=0,burn=8},--coal
36
	{id=263,dmg=1,burn=8},--charcoal
37
	{id=5,dmg=-1,burn=1.5},--planks
38-
  for _,fuel in pairs(fuel) do
38+
	--{id=17,dmg=-1,burn=1.5},--wood
39-
    if item.id == fuel.id
39+
	{id=6,dmg=-1,burn=.5},--sapling
40-
    and (item.dmg == fuel.dmg
40+
	{id=280,dmg=0,burn=.5},--stick
41-
    or fuel.dmg == -1) then
41+
42-
      return true,fuel
42+
43-
    end
43+
44-
  end
44+
45-
  return false
45+
local output = peripheral.wrap("right")
46
or error("Unable to wrap output chest!",0)
47
48
local modemside = "bottom"
49
local modem
50-
  local items = input.getAllStacks()
50+
if peripheral.getType(modemside) == "modem" then
51-
  for slot,item in pairs(items) do
51+
	modem = peripheral.wrap(modemside)
52-
    local state,fuel = isFuel(item)
52+
	or error("Unable to wrap modem!",0)
53-
    if state then
53+
else error("No modem found, please correct!",0) end
54-
      return slot,fuel
54+
55-
    end
55+
local function getAllStacks(inv)
56-
  end
56+
	local t = {}
57
	local size = inv.getInventorySize()
58
	for slot = 1,size do
59
		local item = inv.getStackInSlot(slot) or {}
60
		
61-
  local slot,fuel = findFuel()
61+
		item.rawName = item.rawName or ""
62-
  if slot and fuel then
62+
		item.qty = item.qty or 0
63-
    local items = input.getAllStacks()
63+
		item.dmg = item.dmg or 0
64-
    for slot,item in pairs(items) do
64+
		item.name = item.name or ""
65-
      local state = isFuel(item)
65+
		item.id = item.id or 0
66-
      if not state then
66+
		
67-
        if item.qty >= fuel.burn then
67+
		t[slot] = item
68-
          return slot,item
68+
	end
69-
        end
69+
	return t
70-
      end
70+
71-
    end
71+
72-
  end
72+
73
-- fuel = table
74
local function isFuel(item)
75
	for _,fuel in pairs(fuel) do
76-
  output.pullItem("east",outputslot,64)
76+
		if item.id == fuel.id
77
		and (item.dmg == fuel.dmg
78
		or fuel.dmg == -1) then
79
			return true,fuel
80-
  -- if furnace is empty
80+
		end
81-
  if rs.getInput(signal) == false then
81+
	end
82-
    -- get fuel and item
82+
	return false
83-
    local fuelslot,fuel,fueli = findFuel()
83+
84-
    local itemslot,item = findItem()
84+
85-
    
85+
86-
    -- no fuel/item found: cancel out
86+
87-
    if not fuel then return end
87+
	local items = getAllStacks(input)
88-
    if not item then return end
88+
	for slot,item in pairs(items) do
89-
    
89+
		local state,fuel = isFuel(item)
90-
    -- # items to move
90+
		if state then
91-
    local fuelmove = math.floor(item.qty/fuel.burn)
91+
			return slot,fuel,item
92-
    local itemmove = item.qty-(item.qty%fuel.burn)
92+
		end
93-
    
93+
	end
94-
    -- move items
94+
95-
    input.pushItemIntoSlot("down",fuelslot,fuelmove,fuelslot)
95+
96-
    input.pushItemIntoSlot("down",itemslot,itemmove,inputslot)
96+
97-
  end
97+
98
	local slot,fuel = findFuel()
99
	if slot and fuel then
100-
cook()
100+
		local items = getAllStacks(input)
101
		for slot,item in pairs(items) do
102
			local state = isFuel(item)
103
			if not state then
104
				if item.qty >= fuel.burn then
105
					return slot,item
106
				end
107
			end
108
		end
109
	end
110
end
111
112
local function emptyFurnace()
113
	output.pullItem(outputToFurnace,outputSlot,64)
114
end
115
116
local function isFurnaceEmpty()
117
	local timeout = os.startTimer(2)
118
	modem.open(1)
119
	local msg = textutils.serialize({task="furnace",value="question"})
120
	modem.transmit(1,1,msg)
121
	
122
	local state = false
123
	
124
	while true do
125
		local ev,p1,p2,p3,p4,p5 = os.pullEvent()
126
		if ev == "modem_message" and p1 == modemside and p2 == 1 then
127
			local msg = textutils.unserialize(p4)
128
			if type(msg) == "table" then
129
				if msg.task == "furnace"
130
				and type(msg.value) == "boolean" then
131
					state = msg.value
132
					break
133
				end
134
			end
135
		elseif ev == "timer" then
136
			if p1 == timeout then
137
				state = false
138
				break
139
			end
140
		end
141
	end
142
	modem.close(1)
143
	return state
144
end
145
146
local function cook()
147
	-- check with computer
148
	if isFurnaceEmpty() then
149
		-- get fuel and item
150
		local fuelslot,fuel,fuelitem = findFuel()
151
		local itemslot,item = findItem()
152
		
153
		-- no fuel/item found: cancel out
154
		if not fuel then return end
155
		if not item then return end
156
		
157
		local maxburn = fuelitem.qty*fuel.burn
158
		local fuelmove,itemmove
159
		if item.qty > maxburn * furnaceModifier then
160
			-- # of items are more then we can burn
161
			fuelmove = fuelitem.qty
162
			itemmove = maxburn
163
		else
164
			-- we have more or enough fuel for all items
165
			fuelmove = math.floor(item.qty/(fuel.burn*furnaceModifier))
166
			itemmove = item.qty - (item.qty%(fuel.burn*furnaceModifier))
167
		end
168
		
169
		-- debugging
170
		print("Moving "..tostring(fuelmove).." peices of "..tostring(fuelitem.name).." to fuel slot.")
171
		print("Moving "..tostring(itemmove).." peices of "..tostring(item.name).." to input slot.")
172
		
173
		-- move items
174
		-- pushItemItemIntoSlot(direction, fromSlot, maxQuantity, intoSlot)
175
		input.pushItemIntoSlot(inputToFurnace,fuelslot,fuelmove,fuelSlot)
176
		input.pushItemIntoSlot(inputToFurnace,itemslot,itemmove,inputSlot)
177
	else
178
		emptyFurnace()
179
	end
180
end
181
182
print("Checks input chest for valid fuel and items to burn.")
183
print("If theres items in the furnace i shall not insert more.")
184
print("Checks "..updaterate.." times a second,")
185
print("i.e. "..1/updaterate.." seconds between each update.")
186
while true do
187
	cook()
188
	sleep(1/updaterate)
189
end