TPT Utilities

  • FeynmanLogomaker
    19th Sep 2013 Member 0 Permalink

    I know they're poorly written, but they at least work :)

     

     

    util = util or {}

    pr = pr or {} -- Presets

    pr.burn = function()
    tpt.el.fire.flammable = 9999
    tpt.el.plsm.flammable = 9999
    tpt.el.lava.flammable = 9999
    end

    pr.allburn = function()
    for k = 1, 145 do
    if k ~= 11 and k ~= 130 then
    local str = 'tpt.el.' .. string.gsub(string.lower(tpt.element(k)), '-', '') .. '.flammable=9999'
    local f = io.open('allburn','w')
    f:write(str)
    f:close()
    dofile('allburn')
    end
    end
    end

    bit.oldfromhex = function(str) --Do not use, I made a faster one
    local str = tostring(str)
    local num = 0
    local cnum
    for k = 1, #str do
    local cstr = string.lower(string.sub(str, k, k))
    if cstr == 'a' then cnum = 10
    elseif cstr == 'b' then cnum = 11
    elseif cstr == 'c' then cnum = 12
    elseif cstr == 'd' then cnum = 13
    elseif cstr == 'e' then cnum = 14
    elseif cstr == 'f' then cnum = 15
    elseif string.gsub(cstr, '%d', 'NUM') ~= 'NUM' then cnum = 0
    else cnum = tonumber(cstr)
    end
    num = num + (cnum * 16 ^ (#str - k))
    end
    return num
    end

    bit.fromhex = function(str)
    return tonumber(tostring(str))
    end

    util.test_func = function(func, iter, ...)
    local ct = os.clock()
    for k = 1, iter do
    func(...)
    end
    local nt = os.clock()
    return nt - ct
    end

    util.assert = function(errstr, ...)
    if not errstr then return ... else return errstr end
    end

    util.argb = function(a, r, g, b)
    return '0x' .. bit.tohex(a, 2) .. bit.tohex(r, 2) .. bit.tohex(g, 2) .. bit.tohex(b, 2)
    end

    util.propreplace = function(prop, old, nprop, new)
    old = type(old) == 'string' and tpt.element(old) or old
    new = type(new) == 'string' and tpt.element(new) or old
    for x = 1, 610 do
    for y = 1, 380 do
    if tpt.get_property('type', x, y) ~= 0 then
    if tpt.get_property(prop, x, y) == old then
    tpt.set_property(nprop, new, x, y)
    end
    end
    end
    end
    end

    tpt.point = function(x, y, e, r, g, b, a)
    if not e then e, r, g, b, a = 'dmnd', 255, 255, 255, 255 --Default to DMND
    elseif not r then r, g, b, a = 255 --Default to white
    elseif not a then a = 255 end --Default to opaque
    local _, errstr --[[I use util.assert a lot...]] = tpt.create(x, y, e);
    if r and g and b and a and not(errstr) then
    local _, errstr = tpt.set_property('dcolor', util.argb(a, r, g, b), x, y)
    end
    return util.assert(errstr, true)
    end

    tpt.rect = function(x1, y1, x2, y2, e, r, g, b, a)
    tpt.line(x1, y1, x1, y2, e, r, g, b, a)
    tpt.line(x1, y2, x2, y2, e, r, g, b, a)
    tpt.line(x2, y2, x2, y1, e, r, g, b, a)
    tpt.line(x2, y1, x1, y1, e, r, g, b, a)
    end

    tpt.solidrect = function(x1, y1, x2, y2, e, r, g, b, a)
    for x = x1, x2 do
    for y = y1, y2 do
    local _, errstr = tpt.point(x, y, e, r, g, b, a)
    end
    end
    return util.assert(errstr, true)
    end

    tpt.line = function(x1, y1, x2, y2, e, r, g, b, a)
    if x1 > x2 then x1, x2 = x2, x1 end --If the xcoords are in the wrong order, switch 'em
    if y1 > y2 then y1, y2 = y2, y1 end
    local xs = x2 - x1
    local ys = y2 - y1
    if xs >= ys then --Avoid unfinished line
    for x = x1, x2 do
    tpt.point(x, y1 + x * ys / xs, e, r, g, b, a)
    end
    else
    for y = y1, y2 do
    tpt.point(x1 + xs / ys, y, e, r, g, b, a)
    end
    end
    end

    tpt.ellipse = function(x1, y1, x2, y2, e, r, g, b, a) --Doesn't work yet!!
    if x1 > x2 then x1, x2 = x2, x1 end
    if y1 > y2 then y1, y2 = y2, y1 end
    local w = (x2 - x1) / 2
    local h = (y2 - y1) / 2
    local mx = x2 - w
    local my = y2 - h
    for k = 0, 1000 do
    tpt.line(mx + w * math.sin(k - .01), my + h * math.cos(k - .01), mx + w * math.sin(k), my + h * math.cos(k), e, r, g, b, a)
    end
    end

     

     

    Sorry for not putting it in code tags, that never works well for me.

  • nucular
    19th Sep 2013 Member 1 Permalink

    At least you could've fixed the allburn function, used indenting and pastebin :P

  • KOTOM
    19th Sep 2013 Member 0 Permalink

    What this script doing?

  • boxmein
    19th Sep 2013 Former Staff 0 Permalink
    @KOTOM (View Post)

    pr.allburn = function()- Makes everything flammable. Really lousy. Like honestly. It reads and writes to and from a temporary file just to apply itself. There were -some- alternatives in the allburn thread but just go fori
    bit.fromhex = function(str) - This kinda converts hex numbers to decimals I think. You would be way better off using tonumber(str, 16) for base-16
    util.test_func = function(func, iter, ...) - I think this times a function.
    util.assert = function(errstr, ...) - that's not assert: assert throws an error and the order of operators is different. Plus there already is a non-util function in the global namespace conveniently called assert. Aside that he gladly uses it so you could too :D
    util.argb = function(a, r, g, b) - Returns a bit-packed hex -string- colour value. Conveniently none of his functions even use this externally.
    tpt.point = function(x, y, e, r, g, b, a) - Creates an element point with a mandatory colour value in which everything is optional except the position.
    tpt.rect = function(x1, y1, x2, y2, e, r, g, b, a) - Creates a hollow rectangle.
    tpt.line = function(x1, y1, x2, y2, e, r, g, b, a) - Creates a line between two points
    tpt.ellipse = function(x1, y1, x2, y2, e, r, g, b, a) - Marked "Doesn't work yet" so be prepared

    Aside that, it'll come in handy for people using basic Lua. Nicely done.
    Edited 3 times by boxmein. Last: 19th Sep 2013
  • FeynmanLogomaker
    19th Sep 2013 Member 0 Permalink

    @boxmein: Thanks, I was going to do something like that but you seem to have just about covered it.

  • MiningMarsh
    19th Sep 2013 Member 0 Permalink

    This line code is completly and utterly broken.

     

    Test code:

    line = function(x1, y1, x2, y2, e, r, g, b, a)
        if x1 > x2 then
            x1, x2 = x2, x1
        end --If the xcoords are in the wrong order, switch 'em
        if y1 > y2 then
            y1, y2 = y2, y1
        end
        local xs = x2 - x1
        local ys = y2 - y1
        if xs >= ys then --Avoid unfinished line
            for x = x1, x2 do
                print(x, y1 + x * ys / xs)
            end
        else
            print("test")
            for y = y1, y2 do
                print(x1 + xs / ys, y)
            end
        end
    end

     

    line(20, 20, 30, 40, 1, 1, 1, 1, 1)

    test
    20.5    20
    20.5    21
    20.5    22
    20.5    23
    20.5    24
    20.5    25
    20.5    26
    20.5    27
    20.5    28
    20.5    29
    20.5    30
    20.5    31
    20.5    32
    20.5    33
    20.5    34
    20.5    35
    20.5    36
    20.5    37
    20.5    38
    20.5    39
    20.5    40

     

    line(20, 20, 40, 30, 1, 1, 1, 1, 1)

    20      30
    21      30.5
    22      31
    23      31.5
    24      32
    25      32.5
    26      33
    27      33.5
    28      34
    29      34.5
    30      35
    31      35.5
    32      36
    33      36.5
    34      37
    35      37.5
    36      38
    37      38.5
    38      39
    39      39.5
    40      40

    Edited 2 times by MiningMarsh. Last: 19th Sep 2013