Advertisement
Necrotico

Computercraft Logistics Factory Fluid Manager

Nov 30th, 2021 (edited)
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.13 KB | None | 0 0
  1. --[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
  2. -- Lua Library inline imports
  3. function __TS__StringSubstring(self, start, ____end)
  4.     if ____end ~= ____end then
  5.         ____end = 0
  6.     end
  7.     if ____end ~= nil and start > ____end then
  8.         start, ____end = ____end, start
  9.     end
  10.     if start >= 0 then
  11.         start = start + 1
  12.     else
  13.         start = 1
  14.     end
  15.     if ____end ~= nil and ____end < 0 then
  16.         ____end = 0
  17.     end
  18.     return string.sub(self, start, ____end)
  19. end
  20.  
  21. function __TS__StringAccess(self, index)
  22.     if index >= 0 and index < #self then
  23.         return string.sub(self, index + 1, index + 1)
  24.     end
  25. end
  26.  
  27. function __TS__StringSplit(source, separator, limit)
  28.     if limit == nil then
  29.         limit = 4294967295
  30.     end
  31.     if limit == 0 then
  32.         return {}
  33.     end
  34.     local out = {}
  35.     local index = 0
  36.     local count = 0
  37.     if separator == nil or separator == "" then
  38.         while index < #source - 1 and count < limit do
  39.             out[count + 1] = __TS__StringAccess(source, index)
  40.             count = count + 1
  41.             index = index + 1
  42.         end
  43.     else
  44.         local separatorLength = #separator
  45.         local nextIndex = (string.find(source, separator, nil, true) or 0) - 1
  46.         while nextIndex >= 0 and count < limit do
  47.             out[count + 1] = __TS__StringSubstring(source, index, nextIndex)
  48.             count = count + 1
  49.             index = nextIndex + separatorLength
  50.             nextIndex = (string.find(
  51.                 source,
  52.                 separator,
  53.                 math.max(index + 1, 1),
  54.                 true
  55.             ) or 0) - 1
  56.         end
  57.     end
  58.     if count < limit then
  59.         out[count + 1] = __TS__StringSubstring(source, index)
  60.     end
  61.     return out
  62. end
  63.  
  64. function __TS__StringSubstr(self, from, length)
  65.     if from ~= from then
  66.         from = 0
  67.     end
  68.     if length ~= nil then
  69.         if length ~= length or length <= 0 then
  70.             return ""
  71.         end
  72.         length = length + from
  73.     end
  74.     if from >= 0 then
  75.         from = from + 1
  76.     end
  77.     return string.sub(self, from, length)
  78. end
  79.  
  80. __TS__parseInt_base_pattern = "0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTvVwWxXyYzZ"
  81. function __TS__ParseInt(numberString, base)
  82.     if base == nil then
  83.         base = 10
  84.         local hexMatch = string.match(numberString, "^%s*-?0[xX]")
  85.         if hexMatch then
  86.             base = 16
  87.             local ____string_match_result__0_0
  88.             if string.match(hexMatch, "-") then
  89.                 ____string_match_result__0_0 = "-" .. __TS__StringSubstr(numberString, #hexMatch)
  90.             else
  91.                 ____string_match_result__0_0 = __TS__StringSubstr(numberString, #hexMatch)
  92.             end
  93.             numberString = ____string_match_result__0_0
  94.         end
  95.     end
  96.     if base < 2 or base > 36 then
  97.         return 0 / 0
  98.     end
  99.     local ____temp_1
  100.     if base <= 10 then
  101.         ____temp_1 = __TS__StringSubstring(__TS__parseInt_base_pattern, 0, base)
  102.     else
  103.         ____temp_1 = __TS__StringSubstr(__TS__parseInt_base_pattern, 0, 10 + 2 * (base - 10))
  104.     end
  105.     local allowedDigits = ____temp_1
  106.     local pattern = ("^%s*(-?[" .. allowedDigits) .. "]*)"
  107.     local number = tonumber(
  108.         string.match(numberString, pattern),
  109.         base
  110.     )
  111.     if number == nil then
  112.         return 0 / 0
  113.     end
  114.     if number >= 0 then
  115.         return math.floor(number)
  116.     else
  117.         return math.ceil(number)
  118.     end
  119. end
  120.  
  121. function __TS__ArrayIndexOf(arr, searchElement, fromIndex)
  122.     local len = #arr
  123.     if len == 0 then
  124.         return -1
  125.     end
  126.     local n = 0
  127.     if fromIndex then
  128.         n = fromIndex
  129.     end
  130.     if n >= len then
  131.         return -1
  132.     end
  133.     local k
  134.     if n >= 0 then
  135.         k = n
  136.     else
  137.         k = len + n
  138.         if k < 0 then
  139.             k = 0
  140.         end
  141.     end
  142.     do
  143.         local i = k
  144.         while i < len do
  145.             if arr[i + 1] == searchElement then
  146.                 return i
  147.             end
  148.             i = i + 1
  149.         end
  150.     end
  151.     return -1
  152. end
  153.  
  154. function simple_write(self, path, data)
  155.     local fh = fs.open(path, "w")
  156.     fh:write(data)
  157.     fh:close()
  158. end
  159. function simple_read(self, path)
  160.     local fh = fs.open(path, "r")
  161.     local fr = fh:readAll()
  162.     fh:close()
  163.     return fr
  164. end
  165. DATA_PATH = "/disk/data.txt"
  166. FLUID_TARGET_PATH = "/disk/fluid.txt"
  167. fluid_map = {"empty", "empty", "empty", "empty"}
  168. current_fluid = 0
  169. target_fluid = 0
  170. last_target_fluid_name = "empty"
  171. await_next_signal = false
  172. await_signal_stop = false
  173. output_state = false
  174. function write_data(self)
  175.     simple_write(
  176.         nil,
  177.         DATA_PATH,
  178.         (tostring(current_fluid) .. ",") .. table.concat(fluid_map, "," or ",")
  179.     )
  180. end
  181. function parse_data(self)
  182.     local raw_data = __TS__StringSplit(
  183.         simple_read(nil, DATA_PATH),
  184.         ","
  185.     )
  186.     current_fluid = __TS__ParseInt(raw_data[1])
  187.     target_fluid = current_fluid
  188.     do
  189.         local i = 0
  190.         while i < 4 do
  191.             fluid_map[i + 1] = raw_data[i + 1 + 1]
  192.             i = i + 1
  193.         end
  194.     end
  195.     last_target_fluid_name = fluid_map[current_fluid + 1]
  196. end
  197. if fs.exists(DATA_PATH) then
  198.     parse_data(nil)
  199. else
  200.     write_data(nil)
  201. end
  202. if not fs.exists(FLUID_TARGET_PATH) then
  203.     simple_write(nil, FLUID_TARGET_PATH, "idle")
  204. end
  205. function set_output_state(self, state)
  206.     output_state = state
  207.     redstone.setAnalogOutput("back", state and 14 or 0)
  208. end
  209. set_output_state(nil, false)
  210. while true do
  211.     local contact_signal = redstone.getAnalogInput("top")
  212.     if await_next_signal then
  213.         if contact_signal > 0 then
  214.             current_fluid = current_fluid + 1
  215.             if current_fluid > 3 then
  216.                 current_fluid = 0
  217.             end
  218.             write_data(nil)
  219.             if current_fluid == target_fluid then
  220.                 set_output_state(nil, false)
  221.                 await_next_signal = false
  222.             else
  223.                 await_signal_stop = true
  224.             end
  225.         end
  226.     elseif contact_signal == 0 then
  227.         if await_signal_stop then
  228.             if current_fluid ~= target_fluid then
  229.                 await_next_signal = true
  230.             end
  231.             await_signal_stop = false
  232.         else
  233.             set_output_state(nil, false)
  234.         end
  235.     else
  236.         local target_fluid_name = simple_read(nil, FLUID_TARGET_PATH)
  237.         if target_fluid_name ~= "idle" and target_fluid_name ~= last_target_fluid_name then
  238.             target_fluid = __TS__ArrayIndexOf(fluid_map, target_fluid_name)
  239.             if target_fluid == -1 then
  240.                 local empty_index = __TS__ArrayIndexOf(fluid_map, "empty")
  241.                 if empty_index == -1 then
  242.                     print("ERROR: NO AVAILABLE FLUID TANKS!")
  243.                 else
  244.                     fluid_map[empty_index + 1] = target_fluid_name
  245.                     write_data(nil)
  246.                 end
  247.             end
  248.             last_target_fluid_name = target_fluid_name
  249.             await_signal_stop = true
  250.             set_output_state(nil, true)
  251.         else
  252.             sleep(1)
  253.         end
  254.     end
  255. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement