Arcane symbol class

http://pastebin.com/CDnQn7SH

A class to generate random symbols for gods/armies/alien language/alchemy

In my case, thinking about working on a god game.

As a side note, it uses an old technique of storing a bunch of booleans as an integer by using mod(i/base 2) to unpack them.

I love this idea, can’t wait to try it out when I get home.

@Ipda41001… Not only do do your randomly generated ‘symbols’ look great, once again I find I learn a lot from looking at (and tinkering) with your code. Thank you for posting it!

That said, if you have the time, would you mind explaining your statement that…

it uses an old technique of storing a bunch of booleans as an integer by using mod(i/base 2) to unpack them

a little bit further? I’m intrigued.

Really clever project. Just posting a picture of the output:

Edit: This would make a great component for a game about spell crafting.

Arcane Symbols

With regards to storing booleans in an integer, I assume that’s referring to accessing the individual bits of an integer to efficiently pack booleans. It’s a common occurrence in C code, for example:

//Bit flags
#define FLAG_1  (1<<1)
#define FLAG_2  (1<<2)
#define FLAG_3  (1<<3)
#define FLAG_4  (1<<4)

int flags = FLAG_1 | FLAG_2  //sets flag 1 and flag 2

if( flags & FLAG_2 ) { /*do something*/ } //Check for FLAG_2

I believe this is done in @Ipda41001’s Lua code in the Symbol:drawStrike method, in the tests for math.floor(self.s / 2^x) ~= 0. Although this code does not seem to isolate specific bits, could you explain it some more, @Ipda41001? From my brief read it seems the same as simply doing a greater-than test.

if math.floor(self.s/2) ~= 0 then --vert line
    line(x,y+s/2,x,y-s/2)
end
if math.floor(self.s/4) ~= 0 then --horz line
    line(x+s/2,y,x-s/2,y)
end
... and so on 

Seems to be the same as:

if self.s >= 2 then --vert line
    line(x,y+s/2,x,y-s/2)
end
if self.s >=4 then --horz line
    line(x+s/2,y,x-s/2,y)
end
... and so on

You can actually re-create bitwise operations in Lua using the following code. (From: http://lua-users.org/wiki/BitwiseOperators)

function bit(p)
    return 2 ^ (p - 1)  -- 1-based indexing
end

-- Typical call:  if hasbit(x, bit(3)) then ...
function hasbit(x, p)
    return x % (p + p) >= p       
end

function setbit(x, p)
    return hasbit(x, p) and x or x + p
end

function clearbit(x, p)
    return hasbit(x, p) and x - p or x
end

I think I messed up on the bit wise part

Here is a new version http://pastebin.com/0SB26Cp6

It repeatdily divides a number by 2, if the remainder is 1 then it flips that bit.

That looks like a much clearer way to do it. I’ll have to go back to that project sometime.

Thanks for this.


function setup()
    for i = 0, 42 do
        s = ""
        for b = 6 , 1, -1 do
            if hasbit(i,bit(b)) then
               s = s .. "1"
            else
               s = s .. "0" 
            end
        end
        print(i, " ",s)
    end
end

function draw()
    background(0, 0, 0)
end

function bit(p)
    return 2 ^ (p - 1)  -- 1-based indexing
end

-- Typical call:  if hasbit(x, bit(3)) then ...
function hasbit(x, p)
    return x % (p + p) >= p       
end

function setbit(x, p)
    return hasbit(x, p) and x or x + p
end

function clearbit(x, p)
    return hasbit(x, p) and x - p or x
end