Updated Random Save Loader

  • FeynmanLogomaker
    25th Oct 2013 Member 1 Permalink

    I made one of these before, but this one's a little different:


      It can load a save with one click instead of 2


      It has a better UI


      You can hide the UI if it gets in the way, and


      It works with every TPT, not just on jacob1's mod!


    The code is this:


    local col, buttonx
    local draw = true

    function tpt.load(id, n)

    if not n then n = 0 end

    simulation.loadSave(id, n)

    end
    local function drawButton()
    tpt.drawrect(2, 372, 84, 10, 255, 255, 255, 255)
    if tpt.mousex >= 2 and tpt.mousey >= 372 and tpt.mousex <= 86 and tpt.mousey <= 382 then
    tpt.fillrect(2, 372, 84, 10, 255, 255, 255, 125)
    end
    tpt.drawtext(4, 374, 'Load random save', 0, 255, 0, 255)
    tpt.drawrect(88, 372, 95, 10, 255, 255, 255, 255)
    if tpt.mousex >= 88 and tpt.mousey >= 372 and tpt.mousex <= 183 and tpt.mousey <= 382 then
    tpt.fillrect(88, 372, 95, 10, 255, 255, 255, 125)
    end
    tpt.drawtext(90, 374, 'Reload current save', 0, 255, 0, 255)
    tpt.drawtext(185, 374, 'Current save ID: ' .. (simulation.getSaveID() or 'nil'), 0, 255, 0, 255)
    buttonx = tpt.textwidth('Current save ID: ' .. (simulation.getSaveID() or 'nil')) + 187
    if tpt.mousex >= buttonx and tpt.mousex <= buttonx + 8 and tpt.mousey >= 372 and tpt.mousey <= 380 then
    col = 255
    else
    col = 200
    end
    tpt.drawtext(buttonx, 372, '\170', 255, 0, 0, col)
    end
    tpt.register_step(drawButton)
    local function unDrawButton()
    if tpt.mousex >= 2 and tpt.mousex <= 10 and tpt.mousey >= 372 and tpt.mousey <= 380 then
    col = 255
    else
    col = 200
    end
    tpt.drawtext(2, 372, '\207', 0, 255, 0, col)
    end
    local function buttonMouseTest(x, y, b, e)
    local ret = true
    if tpt.mousex >= 2 and tpt.mousey >= 374 and tpt.mousex <= 86 and tpt.mousey <= 383 and e == 2 and draw == true then
    tpt.load(math.floor(math.random(1, 1333000))) -- 1333000: Fairly recent
    elseif tpt.mousex >= 88 and tpt.mousey >= 372 and tpt.mousex <= 183 and tpt.mousey <= 382 and e == 2 and simulation.getSaveID() then
    tpt.load(simulation.getSaveID(), 1)
    elseif tpt.mousex >= buttonx and tpt.mousex <= buttonx + 8 and tpt.mousey >= 372 and tpt.mousey <= 380 and draw == true and e == 2 then
    draw = false
    tpt.unregister_step(drawButton)
    tpt.register_step(unDrawButton)
    elseif tpt.mousex >= 2 and tpt.mousex <= 10 and tpt.mousey >= 372 and tpt.mousey <= 380 and draw == false and e == 2 then
    draw = true
    tpt.register_step(drawButton)
    tpt.unregister_step(unDrawButton)
    end
    if (tpt.mousex >= 2 and tpt.mousey >= 374 and tpt.mousex <= 86 and tpt.mousey <= 383 and draw == true) or
    (tpt.mousex >= 88 and tpt.mousey >= 372 and tpt.mousex <= 183 and tpt.mousey <= 382) or
    (tpt.mousex >= buttonx and tpt.mousex <= buttonx + 8 and tpt.mousey >= 372 and tpt.mousey <= 380 and draw == true) or
    (tpt.mousex >= 2 and tpt.mousex <= 10 and tpt.mousey >= 372 and tpt.mousey <= 380 and draw == false) then
    ret = false
    end
    return ret
    end
    tpt.register_mouseclick(buttonMouseTest)


     


    Current version: 2.0.3


     


    CHANGELOG: 


         Added ability to show/hide contols


         Fixed bug preventing random save load because of control hiding/showing


         Changed color of buttons (not shown in images)


     



     


    Click 'Load random save' to...well, you can figure it out...and 'reload current save' to show the dialog you usually get when you manually open a save. Click the 'x' (Yay character encodings!) to close the dialog, and the check mark to open it again after you closed it.

    Edited 3 times by FeynmanLogomaker. Last: 12th Jun 2014
  • KOTOM
    25th Oct 2013 Member 0 Permalink

    35 attempt to call field 'load' (a nill value)

  • FeynmanLogomaker
    25th Oct 2013 Member 0 Permalink

    There, it should be fixed. The formatting is a bit odd, since I'm doing this from my iPad, but it at least works now.

  • KOTOM
    25th Oct 2013 Member 0 Permalink

    Now it works. Great script, I love it. But I think hiding interface for default will be better.

  • FeynmanLogomaker
    25th Oct 2013 Member 0 Permalink

    Yeah, I suppose. I'll do that when I get back to my computer.

  • jacob1
    25th Oct 2013 Developer 0 Permalink
    A better fix for tpt.load would be

    if tpt.version.jacob1s_mod then sim.load = tpt.load end

    (until I actually add sim.load ...)
  • FeynmanLogomaker
    25th Oct 2013 Member 0 Permalink

    ...Maybe. Can tpt.load in yours load a save without displaying the info first?

  • jacob1
    25th Oct 2013 Developer 0 Permalink
    @FeynmanLogomaker (View Post)
    yes, it's the exact same function, about all of the new functions I added into the sim api in the last release were just copied from my mod.
  • KOTOM
    12th Nov 2013 Member 0 Permalink

    draw = false

    buttonx = tpt.textwidth('Current save ID: ' .. (simulation.getSaveID() or 'nil')) + 187

    tpt.unregister_step(drawButton)

    tpt.register_step(unDrawButton)

     

    This you can put to bottom of the script to make interface hidden by default.