Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("back")
- items = {}
- amount = {}
- order = {}
- recipe = {}
- recipes = {}
- state = 1
- addname = false
- w,h = term.getSize()
- w = w+1
- -- 52
- h = h+1
- -- 20
- selection = 1
- col = { ["white"]=1, ["orange"]=2, ["magenta"]=4, ["lightblue"]=8, ["yellow"]=16, ["lime"]=32, ["pink"]=64, ["gray"]=128, ["lightgray"]=256, ["cyan"]=512, ["purple"]=1024, ["blue"]=2048, ["brown"]=4096, ["green"]=8192, ["red"]=16384, ["black"]=32768 }
- function swrite(str,x,y,color)
- term.setCursorPos(x,y)
- term.setTextColor(col[color])
- term.write(str)
- end
- function swritecenter(str,y,color)
- term.setCursorPos((w/2-#str/2)+1,y)
- term.setTextColor(col[color])
- term.write(str)
- end
- function drawbox(str,x1,x2,y1,y2,strcol,color)
- term.setCursorPos(x1,y1)
- term.setBackgroundColor(col[color])
- term.setTextColor(col[strcol])
- local bw = x2-x1
- local bh = y2-y1
- local bstr = " "
- while #bstr < bw do
- bstr = bstr.." "
- end
- for i=1,bh do
- term.setCursorPos(x1,y1+i-1)
- term.write(bstr)
- end
- term.setCursorPos(x1+bw/2-#str/2,y1+bh/2)
- term.write(str)
- term.setBackgroundColor(col["black"])
- return { [1]=x1, [2]=x2, [3]=y1, [4]=y2 }
- end
- function press(but)
- if event == "mouse_click" then
- if pr2 >= but[1] and pr2 < but[2] and pr3 >= but[3] and pr3 < but[4] then
- return true
- else
- return false
- end
- end
- end
- function drawoutline(str,strcol,color)
- term.setBackgroundColor(col[color])
- term.setTextColor(col[strcol])
- drawbox(str,1,w,1,2,strcol,color)
- drawbox("",1,2,1,h,strcol,color)
- drawbox("",w-1,w,1,h,strcol,color)
- drawbox("",1,w,h-1,h,strcol,color)
- end
- function checkfor(str)
- exist = false
- for i=1,#items do
- if items[i] == str then
- exist = true
- end
- end
- if not exist then
- table.insert(items,str)
- end
- end
- function update(str)
- term.setCursorPos(3,4)
- term.setTextColor(1)
- term.write(" ")
- term.setCursorPos(3,4)
- term.write(str)
- if state == 1 and addname == false then
- search(input)
- end
- end
- function additems(str,num)
- cexist = false
- for i=1,#citems do
- if citems[i] == str then
- cexist = true
- end
- end
- if not cexist then
- table.insert(citems,str)
- camount[str] = tonumber(num)
- else
- camount[str] = camount[str] + tonumber(num)
- end
- for i=1,#citems do
- swrite(" ",32,3+i,"white")
- end
- for i=1,#citems do
- swrite(string.sub(citems[i],1,14),32,3+i,"white")
- swrite("("..camount[citems[i]]..")",46,3+i,"white")
- end
- end
- function search(str)
- results = {}
- local sstr = string.lower(str)
- if smenu == 1 then
- for i=1,#rnames do
- if string.find(string.lower(rnames[i]),sstr) then
- table.insert(results,rnames[i])
- end
- end
- elseif smenu == 2 then
- for i=1,#arnames do
- if string.find(string.lower(arnames[i]),sstr) then
- table.insert(results,arnames[i])
- end
- end
- for i=1,#items do
- if string.find(string.lower(items[i]),sstr) then
- table.insert(results,items[i])
- end
- end
- end
- if selection > #results then
- selection = #results
- end
- for i=1,11 do
- swrite(" ",2,6+i,"white")
- end
- if #results > 0 and str ~= "" then
- for i=1,#results do
- if i == selection then
- swrite(">",2,6+i,"red")
- end
- if i <= 11 then
- swrite(string.sub(results[i],1,25),3,6+i,"white")
- end
- end
- else
- swrite("Sorry, no results.",3,7,"white")
- selection = 1
- end
- term.setCursorPos(3+#input,4)
- end
- function load(num)
- rnames = {}
- ritems = {}
- ramount = {}
- file = io.open("recipes"..num,"r")
- if file then
- line = file:read()
- while line do
- table.insert(rnames,line)
- line = file:read()
- table.insert(ritems,line)
- line = file:read()
- table.insert(ramount,line)
- line = file:read()
- end
- file:close()
- end
- end
- function loadall()
- arnames = {}
- aritems = {}
- aramount = {}
- for i=1,6 do
- file = io.open("recipes"..i,"r")
- if file then
- line = file:read()
- while line do
- table.insert(arnames,line)
- line = file:read()
- table.insert(aritems,line)
- line = file:read()
- table.insert(aramount,line)
- line = file:read()
- end
- file:close()
- end
- end
- end
- function save(num)
- if fs.exists("recipes"..tostring(num)) then
- local resolve = shell.resolve("recipes"..tostring(num))
- fs.delete(resolve)
- end
- file = fs.open("recipes"..tostring(num),"w")
- for i=1,#rnames do
- file.write(rnames[i].."\n")
- file.write(ritems[i].."\n")
- file.write(ramount[i].."\n")
- end
- file.close()
- end
- function drawmainmenu()
- term.clear()
- drawoutline("Welcome to Fair's Crafting!","black","white")
- swritecenter("Pick a crafting menu!",3,"lightblue")
- vanilla = drawbox("Vanilla",5,25,5,8,"black","blue")
- industrial = drawbox("Industrial Craft",27,47,5,8,"black","yellow")
- thermal = drawbox("Thermal Exp.",5,25,10,13,"black","orange")
- redpower = drawbox("Red Power",27,47,10,13,"black","red")
- buildcraft = drawbox("Buildcraft etc",5,25,15,18,"black","green")
- misc = drawbox("Misc",27,47,15,18,"black","pink")
- term.setCursorBlink(false)
- menu = 0
- end
- function drawadd()
- term.clear()
- drawoutline("Recipe adder","black","white")
- swrite("Search for an item:",3,3,"white")
- swrite("Search results:",3,6,"white")
- swrite("Items in recipe:",32,3,"white")
- back = drawbox("Back",38,48,15,18,"black","red")
- add = drawbox("Add",27,37,15,18,"black","green")
- smenu = 2
- term.setCursorPos(3,4)
- term.setTextColor(1)
- citems = {}
- camount = {}
- end
- function lastorder(str)
- swrite(" ",30,4,"white")
- swrite(string.sub(str,1,21),30,4,"white")
- term.setCursorPos(3,4)
- end
- function drawmenu(str)
- term.clear()
- if str == 1 then
- drawoutline("Vanilla Crafting","black","white")
- menu = 1
- elseif str == 2 then
- drawoutline("Industrial Crafting","black","white")
- menu = 2
- elseif str == 3 then
- drawoutline("Thermal Expansion Crafting","black","white")
- menu = 3
- elseif str == 4 then
- drawoutline("Red Power Crafting","black","white")
- menu = 4
- elseif str == 5 then
- drawoutline("Buildcraft/forestry Crafting","black","white")
- menu = 5
- elseif str == 6 then
- drawoutline("Misc Crafting","black","white")
- menu = 6
- end
- smenu = 1
- load(menu)
- loadall()
- add = drawbox("Add",38,48,7,10,"black","green")
- delete = drawbox("Delete",38,48,11,14,"black","yellow")
- mainmenu = drawbox("Back",38,48,15,18,"black","red")
- swrite("Search for recipe:",3,3,"white")
- swrite("Stored recipes:",3,6,"white")
- swrite("Last order/status:",30,3,"yellow")
- input = ""
- lastorder("Nothing")
- update(input)
- term.setCursorPos(3,4)
- term.setCursorBlink(true)
- end
- function checkstock(str)
- for i=1,#arnames do
- if arnames[i] == str then
- tritems = textutils.unserialize(aritems[i])
- tramount = textutils.unserialize(aramount[i])
- for i=1,#tritems do
- if tramount[tritems[i]] > amount[tritems[i]] then
- lastorder("Out of "..tritems[i])
- return false
- end
- end
- break
- end
- end
- return true
- end
- function rorder(str)
- order = {}
- for i=1,#arnames do
- if arnames[i] == str then
- tritems = textutils.unserialize(aritems[i])
- tramount = textutils.unserialize(aramount[i])
- for i=1,#tritems do
- order = { [1]="order"..tritems[i], [2]=tramount[tritems[i]] }
- rednet.broadcast(textutils.serialize(order))
- amount[tritems[i]] = amount[tritems[i]] - tramount[tritems[i]]
- os.sleep(0.1)
- end
- lastorder(str)
- break
- end
- end
- end
- rednet.broadcast("updatestock")
- drawmainmenu()
- while true do
- event,pr1,pr2,pr3 = os.pullEvent()
- if event == "rednet_message" then
- if string.sub(pr2,1,1) == "{" then
- item = textutils.unserialize(pr2)
- if item[1] == "item" then
- checkfor(item[2])
- amount[item[2]] = item[3]
- end
- end
- end
- if menu == 0 then
- if press(vanilla) then
- drawmenu(1)
- elseif press(industrial) then
- drawmenu(2)
- elseif press(thermal) then
- drawmenu(3)
- elseif press(redpower) then
- drawmenu(4)
- elseif press(buildcraft) then
- drawmenu(5)
- elseif press(misc) then
- drawmenu(6)
- end
- elseif menu > 0 and menu <= 6 then
- if smenu == 1 then
- if press(mainmenu) then
- drawmainmenu()
- elseif press(add) then
- drawadd()
- elseif press(delete) then
- for i=1,#rnames do
- if rnames[i]==results[selection] then
- table.remove(rnames,i)
- table.remove(ritems,i)
- table.remove(ramount,i)
- end
- end
- save(menu)
- load(menu)
- loadall()
- update(input)
- end
- elseif smenu == 2 then
- if press(back) then
- drawmenu(menu)
- elseif press(add) and not addname then
- addname = true
- state = 1
- swrite("Name the recipe: ",3,3,"white")
- input = ""
- update(input)
- end
- end
- if event == "char" and #input < 20 then
- if state == 1 then
- input = input..pr1
- update(input)
- elseif state == 2 then
- if pr1=="1" or pr1=="2" or pr1=="3" or pr1=="4" or pr1=="5" or pr1=="6" or pr1=="7" or pr1=="8" or pr1=="9" or pr1=="0" then
- input = input..pr1
- update(input)
- end
- end
- elseif event == "key" then
- if pr1 == 14 then
- input = string.sub(input,1,#input-1)
- update(input)
- elseif pr1 == 28 then
- if state == 1 then
- if addname == false then
- if #results > 0 then
- if smenu == 1 then
- if checkstock(results[selection]) then
- lastorder("Ordering..")
- rorder(results[selection])
- end
- elseif smenu == 2 then
- rexist = false
- for i=1,#arnames do
- if arnames[i] == results[selection] then
- rexist = true
- tritems = textutils.unserialize(aritems[i])
- tramount = textutils.unserialize(aramount[i])
- for i=1,#tritems do
- additems(tritems[i],tramount[tritems[i]])
- end
- break
- end
- end
- if not rexist then
- swrite("Type amount needed:",3,3,"white")
- state = 2
- end
- end
- end
- else
- table.insert(rnames,input)
- table.insert(ritems,textutils.serialize(citems))
- table.insert(ramount,textutils.serialize(camount))
- save(menu)
- drawmenu(menu)
- addname = false
- end
- elseif state == 2 then
- if smenu == 1 then
- swrite("Search for recipe: ",3,3,"white")
- elseif smenu == 2 then
- swrite("Search for an item:",3,3,"white")
- additems(results[selection],input)
- end
- state = 1
- end
- input = ""
- update(input)
- elseif pr1 == 200 then
- if selection == 1 then
- if #results > 11 then
- selection = 11
- else
- selection = #results
- end
- else
- selection = selection - 1
- end
- search(input)
- elseif pr1 == 208 then
- if selection == #results or selection == 11 then
- selection = 1
- else
- selection = selection + 1
- end
- search(input)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment