Lua copy/paste thread

A forum for topics that don't fit elsewhere. Introduce yourselves to other members of the forums, discuss how your name evolves when written out in the Game of Life, or just tell us how you found it. Forum rules still apply.
Post Reply
User avatar
PHPBB12345
Posts: 1150
Joined: August 5th, 2015, 11:55 pm
Contact:

Lua copy/paste thread

Post by PHPBB12345 »

Code: Select all

-- Invert all cell states in the current selection.
-- Author: Andrew Trevorrow (andrew@trevorrow.com), Mar 2016.

local g = golly()
local gp = require "gplus"

-- re-assigning inner loop functions results in a 10% speed up
local setcell = g.setcell
local getcell = g.getcell

local r = gp.rect(g.getselrect())
if r.empty then g.exit("There is no selection.") end

-- local t1 = os.clock()

local oldsecs = os.clock()

for row = r.top, r.bottom do
    -- if large selection then give some indication of progress
    local newsecs = os.clock()
    if newsecs - oldsecs >= 1.0 then
        oldsecs = newsecs
        g.update()
    end
    for col = r.left, r.right do
        local n = 0
	if (col + row) % 2 == 0 then
            local x = math.random()
            if x < 0.3 then
                n = 1
            elseif x < 0.35 then
                n = 4
            end
        end
        setcell(col, row, n)
    end
end

if not r.visible() then g.fitsel() end

-- g.show(""..(os.clock()-t1))
Post Reply