autocrafter.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. local function set_bucket2cups(bt, cu)
  2. local craft = {}
  3. local b0 = " "
  4. craft[1] = {cu, cu, b0}
  5. craft[2] = {cu, cu, b0}
  6. craft[3] = {bt, b0, b0}
  7. return craft
  8. end
  9. -- hollow "checkerboard" pattern
  10. local function set_hcp(m1, m2)
  11. local craft = {}
  12. local b0 = " "
  13. craft[1] = {m1, m2, m1}
  14. craft[2] = {m2, b0, m2}
  15. craft[3] = {m1, m2, m1}
  16. return craft
  17. end
  18. -- full block pattern
  19. -- crafting patterns where one
  20. -- type of item fills all slots
  21. local function set_block(m)
  22. local craft = {}
  23. if (m[1] == "(") then return end
  24. if m == {} then
  25. craft[1] = {}
  26. craft[2] = {}
  27. craft[3] = {}
  28. else
  29. craft[1] = {m, m, m}
  30. craft[2] = craft[1]
  31. craft[3] = craft[1]
  32. end
  33. return craft
  34. end
  35. local function set_transformer(tier)
  36. -- transformers have the same crafting pattern
  37. -- but the metal material used is different
  38. local craft = {}
  39. local m1 = "mesecons_materials:fiber"
  40. local m2 = "technic:copper_coil"
  41. local m3;
  42. digiline_send("lcd", "test ".. tier)
  43. if tier == "LV" then
  44. m3 = "default:steel_ingot"
  45. elseif tier == "MV" then
  46. m3 = "technic:carbon_steel_ingot"
  47. elseif tier == "HV" then
  48. m3 = "technic:stainless_steel_ingot"
  49. end
  50. craft[1] = {m1, m3, m1}
  51. craft[2] = {m2, m3, m2}
  52. craft[3] = {m3, m3, m3}
  53. return craft
  54. end
  55. local function set_spools(metal)
  56. if metal[1] == "(" then
  57. return
  58. end
  59. local craft = {}
  60. local m1;
  61. local m2 = "basic_materials:empty_spool"
  62. if metal == "silver" then
  63. m1 = "moreores:"..metal.._"_ingot"
  64. else
  65. m1 = "default:" .. metal .. "_ingot"
  66. end
  67. craft[1] = {m1, m2, m2}
  68. craft[2] = {}
  69. craft[3] = {}
  70. return craft
  71. end
  72. local function set_battery()
  73. -- can not assign "group:wood"
  74. local craft = {}
  75. craft[1] = {"default:wood", "default:copper_ingot", "default:wood"}
  76. craft[2] = {"default:wood", "default:tin_ingot", "default:wood"}
  77. craft[3] = {"default:wood", "default:copper_ingot", "default:wood"}
  78. return craft
  79. end
  80. local function grind(grain)
  81. local craft = {}
  82. craft[1] = {grain, grain, grain}
  83. craft[2] = {grain, "farming:mortar_pestle"}
  84. craft[3] = {}
  85. return craft
  86. end
  87. local function touch_response(ac, msg)
  88. if msg["key_enter_field"] == nil and msg["quit"] ~= nil then
  89. -- ignore the event when a player closes menu
  90. return
  91. end
  92. if msg["key_enter_field"] == "ccoil" then
  93. s1 = "basic_materials:copper_wire"
  94. s2 = "default:steel_ingot"
  95. digiline_send(ac, set_hcp(s1, s2))
  96. elseif msg["key_enter_field"] == "battery" then
  97. digiline_send(ac, set_battery())
  98. elseif msg["clear"] ~= nil then
  99. digiline_send(ac, set_block({}))
  100. digiline_send(ac, "off")
  101. elseif msg["pulse"] ~= nil then
  102. mem.pulse = not mem.pulse
  103. elseif msg["key_enter_field"] == "generic" then
  104. goto craft
  105. elseif msg["transformers"] ~= nil then
  106. digiline_send(ac,
  107. set_transformer(msg["transformers"]))
  108. elseif msg["spools"] ~= nil then
  109. digiline_send(ac,
  110. set_spools(msg["spools"]))
  111. elseif msg["compress"] ~= nil then
  112. digiline_send(ac,
  113. set_block(msg["compress"]))
  114. elseif msg["grind"] ~= nil then
  115. digiline_send(ac, grind(msg["grind"]))
  116. elseif msg["milk"] ~= nil then
  117. -- if not 'cup of milk' items
  118. if msg["milk"] == "mobs:bucket_milk" then
  119. digiline_send(ac,
  120. set_bucket2cups("mobs:bucket_milk", "vessels:drinking_glass"))
  121. else
  122. digiline_send(ac,
  123. set_bucket2cups("bucket:bucket_empty", msg["milk"]))
  124. end
  125. end
  126. ::craft::
  127. local quantity = msg[msg["key_enter_field"]]
  128. if tonumber(quantity) ~= nil then
  129. for i=1, quantity, 1 do
  130. digiline_send(ac, "single")
  131. end
  132. end
  133. end
  134. local function spawn_button(name, label, x, y)
  135. local btn_width = 3
  136. local btn_height = 1
  137. local button = {}
  138. button.command = "addbutton"
  139. button["X"] = x
  140. button["Y"] = y
  141. button["W"] = btn_width
  142. button["H"] = btn_height
  143. button["name"] = name
  144. button["label"] = label
  145. return button
  146. end
  147. local function spawn_field(name, label, x, y, d)
  148. local height = 1
  149. local width = 3
  150. local field = {}
  151. field.command = "addfield"
  152. field["X"] = x
  153. field["Y"] = y
  154. field["W"] = width
  155. field["H"] = height
  156. field["name"] = name
  157. field["label"] = label
  158. field["default"] = d and d or "0"
  159. return field
  160. end
  161. local function spawn_dropdown(name, choices, x, y)
  162. local height = 1
  163. local width = 3
  164. local dd = {}
  165. dd.command = "adddropdown"
  166. dd["X"] = x
  167. dd["Y"] = y
  168. dd["W"] = width
  169. dd["H"] = height
  170. dd["name"] = name
  171. dd["choices"] = choices
  172. dd["selected_id"] = 1
  173. return dd
  174. end
  175. local function touch_init(channel, pulse)
  176. local reset = {}
  177. reset.command = "clear"
  178. digiline_send(channel, reset)
  179. local offset_x = 0.5
  180. local offset_y = -1
  181. local dds = {}
  182. dds[1] = {name="transformers",
  183. choices={"(Transformers)", "LV", "MV", "HV"}
  184. }
  185. dds[2] = {name="spools",
  186. choices={"(Spools)", "copper", "gold", "silver", "steel"}
  187. }
  188. dds[3] = {name="compress",
  189. choices={"(Compress)", "default:cobble", "default:coal_lump", "default:steel_ingot", "default:tin_ingot", "default:copper_ingot", "default:bronze_ingot", "moreores:silver_ingot", "default:gold_ingot", "moreores:mithril_ingot", "technic:carbon_steel_ingot", "technic:cast_iron_ingot", "technic:chromium_ingot","technic:lead_ingot", "technic:stainless_steel_ingot", "technic:uranium0_ingot", "technic:zinc_ingot", "basic_materials:brass_ingot", "glooptest:akalin_ingot", "glooptest:alatro_ingot", "glooptest:talinite_ingot",}
  190. }
  191. dds[4] = {name="grind",
  192. choices={"(Grind Grains)", "farming:wheat", "farming:rye", "farming:oat", "farming:barley", "farming:rice"}
  193. }
  194. dds[5] = {name="milk",
  195. choices = {"(milk in autocrafter is...)", "mobs:glass_milk", "farming:soy_milk", "mobs:bucket_milk"}
  196. }
  197. for i, v in ipairs(dds) do
  198. if i < 5 then
  199. offset_y = offset_y + 1
  200. end
  201. -- fifth dropdown is placed on the second column
  202. -- 3 = width of dropdown
  203. local x = offset_x + (math.floor(i/5) * 3)
  204. local y = offset_y
  205. local dd = spawn_dropdown(v.name, v.choices, x, y)
  206. digiline_send(channel, dd)
  207. end
  208. offset_y = offset_y + 0.5 -- add more space b/c of labels
  209. local fields = {}
  210. fields[1] = {name="ccoil", label="Copper Coil"}
  211. fields[2] = {name="battery", label="RE Battery"}
  212. for i, v in ipairs(fields) do
  213. offset_y = offset_y + 1
  214. local f = spawn_field(v.name, v.label,
  215. offset_x, offset_y)
  216. digiline_send(channel, f)
  217. end
  218. offset_y = offset_y + 1
  219. local btns = {}
  220. btns[1] = {name="clear", label="Reset Craft Grid"}
  221. if pulse == false then
  222. btns[2] = {name="pulse", label="Pulse OFF"}
  223. else
  224. btns[2] = {name="pulse", label="Pulse ON"}
  225. end
  226. for i, v in ipairs(btns) do
  227. local b = spawn_button(v.name, v.label,
  228. offset_x, offset_y)
  229. digiline_send(channel, b)
  230. offset_x = offset_x + 3
  231. end
  232. local gen = {name="generic", label="Burst Craft"}
  233. digiline_send(channel, spawn_field(gen.name, gen.label,
  234. offset_x+0.5, offset_y+0.5,
  235. "96")
  236. )
  237. end
  238. local function main()
  239. local ts = "ts"
  240. local ac = "ac"
  241. if event.type == "program" then
  242. mem.pulse = false
  243. touch_init(ts, mem.pulse)
  244. elseif event.type == "digiline" and event.channel == ts then
  245. touch_response(ac, event.msg)
  246. touch_init(ts, mem.pulse)
  247. end
  248. if event.type == "interrupt"
  249. or event.type == "program" then
  250. if mem.pulse then port.b = not port.b end
  251. interrupt(1)
  252. end
  253. end
  254. return main()