DrFair

Crafter

Feb 1st, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. rednet.open("back")
  2. items = {}
  3. amount = {}
  4. order = {}
  5. recipe = {}
  6. recipes = {}
  7. state = 1
  8. addname = false
  9. w,h = term.getSize()
  10. w = w+1
  11. -- 52
  12. h = h+1
  13. -- 20
  14. selection = 1
  15. 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 }
  16.  
  17. function swrite(str,x,y,color)
  18. term.setCursorPos(x,y)
  19. term.setTextColor(col[color])
  20. term.write(str)
  21. end
  22.  
  23. function swritecenter(str,y,color)
  24. term.setCursorPos((w/2-#str/2)+1,y)
  25. term.setTextColor(col[color])
  26. term.write(str)
  27. end
  28.  
  29. function drawbox(str,x1,x2,y1,y2,strcol,color)
  30. term.setCursorPos(x1,y1)
  31. term.setBackgroundColor(col[color])
  32. term.setTextColor(col[strcol])
  33. local bw = x2-x1
  34. local bh = y2-y1
  35. local bstr = " "
  36. while #bstr < bw do
  37. bstr = bstr.." "
  38. end
  39. for i=1,bh do
  40. term.setCursorPos(x1,y1+i-1)
  41. term.write(bstr)
  42. end
  43. term.setCursorPos(x1+bw/2-#str/2,y1+bh/2)
  44. term.write(str)
  45. term.setBackgroundColor(col["black"])
  46. return { [1]=x1, [2]=x2, [3]=y1, [4]=y2 }
  47. end
  48.  
  49. function press(but)
  50. if event == "mouse_click" then
  51. if pr2 >= but[1] and pr2 < but[2] and pr3 >= but[3] and pr3 < but[4] then
  52. return true
  53. else
  54. return false
  55. end
  56. end
  57. end
  58.  
  59. function drawoutline(str,strcol,color)
  60. term.setBackgroundColor(col[color])
  61. term.setTextColor(col[strcol])
  62. drawbox(str,1,w,1,2,strcol,color)
  63. drawbox("",1,2,1,h,strcol,color)
  64. drawbox("",w-1,w,1,h,strcol,color)
  65. drawbox("",1,w,h-1,h,strcol,color)
  66. end
  67.  
  68. function checkfor(str)
  69. exist = false
  70. for i=1,#items do
  71. if items[i] == str then
  72. exist = true
  73. end
  74. end
  75. if not exist then
  76. table.insert(items,str)
  77. end
  78. end
  79.  
  80. function update(str)
  81. term.setCursorPos(3,4)
  82. term.setTextColor(1)
  83. term.write(" ")
  84. term.setCursorPos(3,4)
  85. term.write(str)
  86. if state == 1 and addname == false then
  87. search(input)
  88. end
  89. end
  90.  
  91. function additems(str,num)
  92. cexist = false
  93. for i=1,#citems do
  94. if citems[i] == str then
  95. cexist = true
  96. end
  97. end
  98. if not cexist then
  99. table.insert(citems,str)
  100. camount[str] = tonumber(num)
  101. else
  102. camount[str] = camount[str] + tonumber(num)
  103. end
  104. for i=1,#citems do
  105. swrite(" ",32,3+i,"white")
  106. end
  107. for i=1,#citems do
  108. swrite(string.sub(citems[i],1,14),32,3+i,"white")
  109. swrite("("..camount[citems[i]]..")",46,3+i,"white")
  110. end
  111. end
  112.  
  113. function search(str)
  114. results = {}
  115. local sstr = string.lower(str)
  116. if smenu == 1 then
  117. for i=1,#rnames do
  118. if string.find(string.lower(rnames[i]),sstr) then
  119. table.insert(results,rnames[i])
  120. end
  121. end
  122. elseif smenu == 2 then
  123. for i=1,#arnames do
  124. if string.find(string.lower(arnames[i]),sstr) then
  125. table.insert(results,arnames[i])
  126. end
  127. end
  128. for i=1,#items do
  129. if string.find(string.lower(items[i]),sstr) then
  130. table.insert(results,items[i])
  131. end
  132. end
  133. end
  134. if selection > #results then
  135. selection = #results
  136. end
  137. for i=1,11 do
  138. swrite(" ",2,6+i,"white")
  139. end
  140. if #results > 0 and str ~= "" then
  141. for i=1,#results do
  142. if i == selection then
  143. swrite(">",2,6+i,"red")
  144. end
  145. if i <= 11 then
  146. swrite(string.sub(results[i],1,25),3,6+i,"white")
  147. end
  148. end
  149. else
  150. swrite("Sorry, no results.",3,7,"white")
  151. selection = 1
  152. end
  153. term.setCursorPos(3+#input,4)
  154. end
  155.  
  156. function load(num)
  157. rnames = {}
  158. ritems = {}
  159. ramount = {}
  160. file = io.open("recipes"..num,"r")
  161. if file then
  162. line = file:read()
  163. while line do
  164. table.insert(rnames,line)
  165. line = file:read()
  166. table.insert(ritems,line)
  167. line = file:read()
  168. table.insert(ramount,line)
  169. line = file:read()
  170. end
  171. file:close()
  172. end
  173. end
  174.  
  175. function loadall()
  176. arnames = {}
  177. aritems = {}
  178. aramount = {}
  179. for i=1,6 do
  180. file = io.open("recipes"..i,"r")
  181. if file then
  182. line = file:read()
  183. while line do
  184. table.insert(arnames,line)
  185. line = file:read()
  186. table.insert(aritems,line)
  187. line = file:read()
  188. table.insert(aramount,line)
  189. line = file:read()
  190. end
  191. file:close()
  192. end
  193. end
  194. end
  195.  
  196. function save(num)
  197. if fs.exists("recipes"..tostring(num)) then
  198. local resolve = shell.resolve("recipes"..tostring(num))
  199. fs.delete(resolve)
  200. end
  201. file = fs.open("recipes"..tostring(num),"w")
  202. for i=1,#rnames do
  203. file.write(rnames[i].."\n")
  204. file.write(ritems[i].."\n")
  205. file.write(ramount[i].."\n")
  206. end
  207. file.close()
  208. end
  209.  
  210. function drawmainmenu()
  211. term.clear()
  212. drawoutline("Welcome to Fair's Crafting!","black","white")
  213. swritecenter("Pick a crafting menu!",3,"lightblue")
  214. vanilla = drawbox("Vanilla",5,25,5,8,"black","blue")
  215. industrial = drawbox("Industrial Craft",27,47,5,8,"black","yellow")
  216. thermal = drawbox("Thermal Exp.",5,25,10,13,"black","orange")
  217. redpower = drawbox("Red Power",27,47,10,13,"black","red")
  218. buildcraft = drawbox("Buildcraft etc",5,25,15,18,"black","green")
  219. misc = drawbox("Misc",27,47,15,18,"black","pink")
  220. term.setCursorBlink(false)
  221. menu = 0
  222. end
  223.  
  224. function drawadd()
  225. term.clear()
  226. drawoutline("Recipe adder","black","white")
  227. swrite("Search for an item:",3,3,"white")
  228. swrite("Search results:",3,6,"white")
  229. swrite("Items in recipe:",32,3,"white")
  230. back = drawbox("Back",38,48,15,18,"black","red")
  231. add = drawbox("Add",27,37,15,18,"black","green")
  232. smenu = 2
  233. term.setCursorPos(3,4)
  234. term.setTextColor(1)
  235. citems = {}
  236. camount = {}
  237. end
  238.  
  239. function lastorder(str)
  240. swrite(" ",30,4,"white")
  241. swrite(string.sub(str,1,21),30,4,"white")
  242. term.setCursorPos(3,4)
  243. end
  244.  
  245. function drawmenu(str)
  246. term.clear()
  247. if str == 1 then
  248. drawoutline("Vanilla Crafting","black","white")
  249. menu = 1
  250. elseif str == 2 then
  251. drawoutline("Industrial Crafting","black","white")
  252. menu = 2
  253. elseif str == 3 then
  254. drawoutline("Thermal Expansion Crafting","black","white")
  255. menu = 3
  256. elseif str == 4 then
  257. drawoutline("Red Power Crafting","black","white")
  258. menu = 4
  259. elseif str == 5 then
  260. drawoutline("Buildcraft/forestry Crafting","black","white")
  261. menu = 5
  262. elseif str == 6 then
  263. drawoutline("Misc Crafting","black","white")
  264. menu = 6
  265. end
  266. smenu = 1
  267. load(menu)
  268. loadall()
  269. add = drawbox("Add",38,48,7,10,"black","green")
  270. delete = drawbox("Delete",38,48,11,14,"black","yellow")
  271. mainmenu = drawbox("Back",38,48,15,18,"black","red")
  272. swrite("Search for recipe:",3,3,"white")
  273. swrite("Stored recipes:",3,6,"white")
  274. swrite("Last order/status:",30,3,"yellow")
  275. input = ""
  276. lastorder("Nothing")
  277. update(input)
  278. term.setCursorPos(3,4)
  279. term.setCursorBlink(true)
  280. end
  281.  
  282. function checkstock(str)
  283. for i=1,#arnames do
  284. if arnames[i] == str then
  285. tritems = textutils.unserialize(aritems[i])
  286. tramount = textutils.unserialize(aramount[i])
  287. for i=1,#tritems do
  288. if tramount[tritems[i]] > amount[tritems[i]] then
  289. lastorder("Out of "..tritems[i])
  290. return false
  291. end
  292. end
  293. break
  294. end
  295. end
  296. return true
  297. end
  298.  
  299. function rorder(str)
  300. order = {}
  301. for i=1,#arnames do
  302. if arnames[i] == str then
  303. tritems = textutils.unserialize(aritems[i])
  304. tramount = textutils.unserialize(aramount[i])
  305. for i=1,#tritems do
  306. order = { [1]="order"..tritems[i], [2]=tramount[tritems[i]] }
  307. rednet.broadcast(textutils.serialize(order))
  308. amount[tritems[i]] = amount[tritems[i]] - tramount[tritems[i]]
  309. os.sleep(0.1)
  310. end
  311. lastorder(str)
  312. break
  313. end
  314. end
  315. end
  316.  
  317. rednet.broadcast("updatestock")
  318. drawmainmenu()
  319.  
  320. while true do
  321. event,pr1,pr2,pr3 = os.pullEvent()
  322. if event == "rednet_message" then
  323. if string.sub(pr2,1,1) == "{" then
  324. item = textutils.unserialize(pr2)
  325. if item[1] == "item" then
  326. checkfor(item[2])
  327. amount[item[2]] = item[3]
  328. end
  329. end
  330. end
  331. if menu == 0 then
  332. if press(vanilla) then
  333. drawmenu(1)
  334. elseif press(industrial) then
  335. drawmenu(2)
  336. elseif press(thermal) then
  337. drawmenu(3)
  338. elseif press(redpower) then
  339. drawmenu(4)
  340. elseif press(buildcraft) then
  341. drawmenu(5)
  342. elseif press(misc) then
  343. drawmenu(6)
  344. end
  345. elseif menu > 0 and menu <= 6 then
  346. if smenu == 1 then
  347. if press(mainmenu) then
  348. drawmainmenu()
  349. elseif press(add) then
  350. drawadd()
  351. elseif press(delete) then
  352. for i=1,#rnames do
  353. if rnames[i]==results[selection] then
  354. table.remove(rnames,i)
  355. table.remove(ritems,i)
  356. table.remove(ramount,i)
  357. end
  358. end
  359. save(menu)
  360. load(menu)
  361. loadall()
  362. update(input)
  363. end
  364. elseif smenu == 2 then
  365. if press(back) then
  366. drawmenu(menu)
  367. elseif press(add) and not addname then
  368. addname = true
  369. state = 1
  370. swrite("Name the recipe: ",3,3,"white")
  371. input = ""
  372. update(input)
  373. end
  374. end
  375. if event == "char" and #input < 20 then
  376. if state == 1 then
  377. input = input..pr1
  378. update(input)
  379. elseif state == 2 then
  380. 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
  381. input = input..pr1
  382. update(input)
  383. end
  384. end
  385. elseif event == "key" then
  386. if pr1 == 14 then
  387. input = string.sub(input,1,#input-1)
  388. update(input)
  389. elseif pr1 == 28 then
  390. if state == 1 then
  391. if addname == false then
  392. if #results > 0 then
  393. if smenu == 1 then
  394. if checkstock(results[selection]) then
  395. lastorder("Ordering..")
  396. rorder(results[selection])
  397. end
  398. elseif smenu == 2 then
  399. rexist = false
  400. for i=1,#arnames do
  401. if arnames[i] == results[selection] then
  402. rexist = true
  403. tritems = textutils.unserialize(aritems[i])
  404. tramount = textutils.unserialize(aramount[i])
  405. for i=1,#tritems do
  406. additems(tritems[i],tramount[tritems[i]])
  407. end
  408. break
  409. end
  410. end
  411. if not rexist then
  412. swrite("Type amount needed:",3,3,"white")
  413. state = 2
  414. end
  415. end
  416. end
  417. else
  418. table.insert(rnames,input)
  419. table.insert(ritems,textutils.serialize(citems))
  420. table.insert(ramount,textutils.serialize(camount))
  421. save(menu)
  422. drawmenu(menu)
  423. addname = false
  424. end
  425. elseif state == 2 then
  426. if smenu == 1 then
  427. swrite("Search for recipe: ",3,3,"white")
  428. elseif smenu == 2 then
  429. swrite("Search for an item:",3,3,"white")
  430. additems(results[selection],input)
  431. end
  432. state = 1
  433. end
  434. input = ""
  435. update(input)
  436. elseif pr1 == 200 then
  437. if selection == 1 then
  438. if #results > 11 then
  439. selection = 11
  440. else
  441. selection = #results
  442. end
  443. else
  444. selection = selection - 1
  445. end
  446. search(input)
  447. elseif pr1 == 208 then
  448. if selection == #results or selection == 11 then
  449. selection = 1
  450. else
  451. selection = selection + 1
  452. end
  453. search(input)
  454. end
  455. end
  456. end
  457. end
Advertisement
Add Comment
Please, Sign In to add comment