Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
- -- Lua Library inline imports
- function __TS__StringSubstring(self, start, ____end)
- if ____end ~= ____end then
- ____end = 0
- end
- if ____end ~= nil and start > ____end then
- start, ____end = ____end, start
- end
- if start >= 0 then
- start = start + 1
- else
- start = 1
- end
- if ____end ~= nil and ____end < 0 then
- ____end = 0
- end
- return string.sub(self, start, ____end)
- end
- function __TS__StringAccess(self, index)
- if index >= 0 and index < #self then
- return string.sub(self, index + 1, index + 1)
- end
- end
- function __TS__StringSplit(source, separator, limit)
- if limit == nil then
- limit = 4294967295
- end
- if limit == 0 then
- return {}
- end
- local out = {}
- local index = 0
- local count = 0
- if separator == nil or separator == "" then
- while index < #source - 1 and count < limit do
- out[count + 1] = __TS__StringAccess(source, index)
- count = count + 1
- index = index + 1
- end
- else
- local separatorLength = #separator
- local nextIndex = (string.find(source, separator, nil, true) or 0) - 1
- while nextIndex >= 0 and count < limit do
- out[count + 1] = __TS__StringSubstring(source, index, nextIndex)
- count = count + 1
- index = nextIndex + separatorLength
- nextIndex = (string.find(
- source,
- separator,
- math.max(index + 1, 1),
- true
- ) or 0) - 1
- end
- end
- if count < limit then
- out[count + 1] = __TS__StringSubstring(source, index)
- end
- return out
- end
- function __TS__StringSubstr(self, from, length)
- if from ~= from then
- from = 0
- end
- if length ~= nil then
- if length ~= length or length <= 0 then
- return ""
- end
- length = length + from
- end
- if from >= 0 then
- from = from + 1
- end
- return string.sub(self, from, length)
- end
- __TS__parseInt_base_pattern = "0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTvVwWxXyYzZ"
- function __TS__ParseInt(numberString, base)
- if base == nil then
- base = 10
- local hexMatch = string.match(numberString, "^%s*-?0[xX]")
- if hexMatch then
- base = 16
- local ____string_match_result__0_0
- if string.match(hexMatch, "-") then
- ____string_match_result__0_0 = "-" .. __TS__StringSubstr(numberString, #hexMatch)
- else
- ____string_match_result__0_0 = __TS__StringSubstr(numberString, #hexMatch)
- end
- numberString = ____string_match_result__0_0
- end
- end
- if base < 2 or base > 36 then
- return 0 / 0
- end
- local ____temp_1
- if base <= 10 then
- ____temp_1 = __TS__StringSubstring(__TS__parseInt_base_pattern, 0, base)
- else
- ____temp_1 = __TS__StringSubstr(__TS__parseInt_base_pattern, 0, 10 + 2 * (base - 10))
- end
- local allowedDigits = ____temp_1
- local pattern = ("^%s*(-?[" .. allowedDigits) .. "]*)"
- local number = tonumber(
- string.match(numberString, pattern),
- base
- )
- if number == nil then
- return 0 / 0
- end
- if number >= 0 then
- return math.floor(number)
- else
- return math.ceil(number)
- end
- end
- function __TS__ArrayIndexOf(arr, searchElement, fromIndex)
- local len = #arr
- if len == 0 then
- return -1
- end
- local n = 0
- if fromIndex then
- n = fromIndex
- end
- if n >= len then
- return -1
- end
- local k
- if n >= 0 then
- k = n
- else
- k = len + n
- if k < 0 then
- k = 0
- end
- end
- do
- local i = k
- while i < len do
- if arr[i + 1] == searchElement then
- return i
- end
- i = i + 1
- end
- end
- return -1
- end
- function simple_write(self, path, data)
- local fh = fs.open(path, "w")
- fh:write(data)
- fh:close()
- end
- function simple_read(self, path)
- local fh = fs.open(path, "r")
- local fr = fh:readAll()
- fh:close()
- return fr
- end
- DATA_PATH = "/disk/data.txt"
- FLUID_TARGET_PATH = "/disk/fluid.txt"
- fluid_map = {"empty", "empty", "empty", "empty"}
- current_fluid = 0
- target_fluid = 0
- last_target_fluid_name = "empty"
- await_next_signal = false
- await_signal_stop = false
- output_state = false
- function write_data(self)
- simple_write(
- nil,
- DATA_PATH,
- (tostring(current_fluid) .. ",") .. table.concat(fluid_map, "," or ",")
- )
- end
- function parse_data(self)
- local raw_data = __TS__StringSplit(
- simple_read(nil, DATA_PATH),
- ","
- )
- current_fluid = __TS__ParseInt(raw_data[1])
- target_fluid = current_fluid
- do
- local i = 0
- while i < 4 do
- fluid_map[i + 1] = raw_data[i + 1 + 1]
- i = i + 1
- end
- end
- last_target_fluid_name = fluid_map[current_fluid + 1]
- end
- if fs.exists(DATA_PATH) then
- parse_data(nil)
- else
- write_data(nil)
- end
- if not fs.exists(FLUID_TARGET_PATH) then
- simple_write(nil, FLUID_TARGET_PATH, "idle")
- end
- function set_output_state(self, state)
- output_state = state
- redstone.setAnalogOutput("back", state and 14 or 0)
- end
- set_output_state(nil, false)
- while true do
- local contact_signal = redstone.getAnalogInput("top")
- if await_next_signal then
- if contact_signal > 0 then
- current_fluid = current_fluid + 1
- if current_fluid > 3 then
- current_fluid = 0
- end
- write_data(nil)
- if current_fluid == target_fluid then
- set_output_state(nil, false)
- await_next_signal = false
- else
- await_signal_stop = true
- end
- end
- elseif contact_signal == 0 then
- if await_signal_stop then
- if current_fluid ~= target_fluid then
- await_next_signal = true
- end
- await_signal_stop = false
- else
- set_output_state(nil, false)
- end
- else
- local target_fluid_name = simple_read(nil, FLUID_TARGET_PATH)
- if target_fluid_name ~= "idle" and target_fluid_name ~= last_target_fluid_name then
- target_fluid = __TS__ArrayIndexOf(fluid_map, target_fluid_name)
- if target_fluid == -1 then
- local empty_index = __TS__ArrayIndexOf(fluid_map, "empty")
- if empty_index == -1 then
- print("ERROR: NO AVAILABLE FLUID TANKS!")
- else
- fluid_map[empty_index + 1] = target_fluid_name
- write_data(nil)
- end
- end
- last_target_fluid_name = target_fluid_name
- await_signal_stop = true
- set_output_state(nil, true)
- else
- sleep(1)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement