Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Converts simple commands to FILT colours for my "Quad-Instruction FILT Computer" [2714304] in The Powder Toy
- To use, create a .txt file with a command per line, open the TPT console with ~ and type:
- dofile("PATH-TO-THIS-SCRIPT")
- Replace PATH-TO-THIS-SCRIPT with the complete directory and any backslashes with 2 backslashes (if on Windows), for example:
- dofile("C:\\Users\\User\\Downloads\\quadfilt.lua")
- Then type the directory to your script in the prompt, with only 1 slash for folders this time. You may like to add a suffix directory below this comment block, so you only have to type the file name in only a specific folder. Close the dialog box, and click on the top-right most white FILT that's sticking out of the memory rectangle.
- Keep in mind that the computer starts reading from pixel 0, so if you're setting variables
- Available commands (anything else will be ignored):
- `sub [a] [b]`: Subtracts [b] from [a], and puts the result in [a]
- `goto [a] [b]`: If [a] <= 0, set the instruction position to [b]
- `print [a]`: Outputs the value of [a] to the display module
- `read [a]`: Waits for the input module to send a number, and puts the value into [a]
- `set [a]`: Sets the pixel in memory to a numeric value
- Adding `(TEXT)` to the end of a line will mark it as a memory position that can be referred to later. All instances of `[TEXT]` will be replaced with the RAM/ROM index of the line with `(TEXT)` in it. For example, the following script will subtract 5 from 12 and replace 12 with the result, then loop until the number at 12 is less than or equal to 0:
- 0| sub [count] [inc] (start)
- 1| goto [count] [start]
- 2|
- 3| set 5 (inc)
- 4| set 12 (count)
- If you were to look at the code after it has been processed, you would see `[start]` has been replaced with 0, `[count]` with 4, etc.
- ]]
- folder = "C:\\" -- Set this to the folder with your scripts, for example: "C:\\Users\\User\\Documents\\"
- debugMode = false
- numAdd = 0x20000000 -- All numbers need the last bit set, in case it's a zero and the default value is sent instead
- -- Numbers corresponding to commands
- commandNums = {["sub"] = 0, ["goto"] = 1, ["print"] = 2, ["read"] = 3}
- function command_to_num (args)
- if not contains(commandNums, args[1]) then return nil end
- local mode = commandNums[args[1]]
- local a = tonumber(args[2])
- local b = 0
- if mode <= 1 then b = tonumber(args[3]) end
- if a == nil or b == nil then return nil end
- return bit.bor(numAdd, a, bit.lshift(b, 13), bit.lshift(mode, 27))
- end
- function contains (tbl, element)
- for key, value in pairs(tbl) do
- if key == element then
- return true
- end
- end
- return false
- end
- function contains_tag (str)
- return string.match(str, "%(%w+%)")
- end
- function split (splitted, sep)
- if sep == nil then
- sep = "%s"
- end
- local t = {}
- for str in string.gmatch(splitted, "([^" .. sep .. "]+)") do
- table.insert(t, str)
- end
- return t
- end
- function get_len (tbl)
- count = 0
- for _ in pairs(tbl) do count = count + 1 end
- return count
- end
- function file_exists (file)
- local f = io.open(file, "rb")
- if f then f:close() end
- if f == nil then
- tpt.log("\nFailed to load file")
- end
- return f ~= nil
- end
- function lines_from (file)
- if not file_exists(file) then return false end
- lines = {}
- tpt.log("\nReading")
- for line in io.lines(file) do
- table.insert(lines, line)
- end
- return lines
- end
- function generate_script (file)
- lines = lines_from(file)
- strippedLines = {}
- commands = {}
- gotoTags = {}
- if not lines then
- if debugMode then tpt.log("\nNo input!") end
- return nil
- end
- for index, command in pairs(lines) do -- Ignore invalid commands
- args = split(command)
- if contains(commandNums, args[1]) or args[1] == "set" then
- table.insert(strippedLines, command)
- end
- end
- if debugMode then tpt.log("\nGoto tags are as follows") end
- for index, command in pairs(strippedLines) do -- Store tag if present
- tag = contains_tag(command)
- args = split(command)
- if tag ~= nil then
- tag = string.gsub(tag, "[%(%)]", "")
- gotoTags[tag] = index - 1
- if debugMode then tpt.log("\n " .. tag .. ": " .. gotoTags[tag]) end
- end
- end
- for index, command in pairs(strippedLines) do -- Replace all tags with their value
- for tag, value in pairs(gotoTags) do
- command = string.gsub(command, "%[" .. tag .. "%]", value)
- end
- strippedLines[index] = command -- Overwrite new command
- end
- if debugMode then tpt.log("\n" .. get_len(strippedLines) .. " lines are as follows:") end
- for index, command in pairs(strippedLines) do
- if debugMode then tpt.log("\n " .. command) end
- args = split(command)
- local filtVal = nil
- if contains(commandNums, args[1]) then -- Set values to commands if specified
- filtVal = command_to_num(args)
- if filtVal == nil then
- tpt.log("\nError at line " .. index .. ":\n " .. command)
- return nil
- end
- elseif args[1] == "set" then -- Set value to raw number
- local num = tonumber(args[2])
- if num == nil then
- tpt.log("\nError at line " .. index .. ":\n " .. command)
- return nil
- end
- filtVal = bit.bor(numAdd, num)
- end
- table.insert(commands, filtVal)
- end
- return commands
- end
- function paste_script (commands)
- tpt.log("\nCreating " .. get_len(commands) .. " commands")
- for index, command in pairs(commands) do
- if debugMode then tpt.log(command) end
- x, y = sim.adjustCoords(tpt.mousex, tpt.mousey)
- x = x - (index - 1) % 64 - 1
- y = y + math.floor((index - 1) / 64) + 1
- tpt.create(x, y, "filt")
- tpt.set_property("tmp", 0, x, y)
- tpt.set_property("ctype", command, x, y)
- end
- end
- function preview_code ()
- x, y = sim.adjustCoords(tpt.mousex, tpt.mousey)
- graphics.fillRect(x - 64, y + 1, 64, 128, 255, 0, 0, 96)
- graphics.drawLine(x, y, x, y + 128, 255, 255, 255, 96)
- end
- function click_paste (x, y, btn)
- if btn == 1 then
- paste_script(cmds)
- end
- stop_listen()
- end
- function start_listen ()
- event.register(event.tick, preview_code)
- event.register(event.mousedown, click_paste)
- tpt.set_console(0)
- tpt.message_box("Notice", "Make sure the computer is reset, then align & left click")
- tpt.log("\nReady to")
- end
- function stop_listen ()
- cmds = nil
- event.register(event.tick, preview_code)
- event.unregister(event.tick, preview_code)
- event.unregister(event.mousedown, click_paste)
- tpt.log("\nDone")
- end
- if debugMode then tpt.log("\nLoaded code") end
- file = tpt.input("Script file", "Convert script file\n" .. folder, "")
- if file == "" then
- tpt.log("\nCancelled")
- return
- end
- cmds = generate_script(folder .. file)
- if cmds then
- start_listen()
- else
- stop_listen()
- end
Advertisement
Add Comment
Please, Sign In to add comment