Page 1 of 1

Raptor's Helper Functions

PostPosted: Wed May 08, 2013 7:11 am
by bobdaduck
Raptor build these for me during my... Exploits. Anyway, if you use these, especially the array ones, it'll really cut down on script crashes. I believe you can actually just do "require helperFunctions.levelgen" up at the top of your script and it'll include these functions automatically.

  Code:
----Credit to raptor for these.  
tau = 2 * math.pi --radians

---------------------------------------------------------------//Array Helpers
 function removeFromDictionary(dictionary, key)
    dictionary[key] = nil
end

--Returns the size of a dictionary.  Probably.
function dsize(dictionary)
    local counter = 0
    for k, v in pairs(dictionary) do
        if v ~= nil then
            counter = counter + 1
        end
    end
    return counter
end

--This removes an entry from a table, safely.
function searchAndRemoveFromTable(someTable, valueObject)
    local localIndexToRemove = -1
    for i, v in ipairs(someTable)
    do
        if(v == valueObject) then
            localIndexToRemove = i
            break
        end
    end

    if localIndexToRemove ~= -1 then
        table.remove(someTable, localIndexToRemove)
    end
end

--This adds to a table if its not already there.
function insertIntoTableIfUnique(someTable, valueObject)
    local foundObject = false
    for i, v in ipairs(someTable)
    do
        if(v == valueObject) then
            foundObject = true
            break
        end
    end

    if foundObject == false then
        table.insert(someTable, valueObject)
    end
end
-------------------------------------------------------------------//String helpers
function getArgs(inputString)
    local words = {}
    for word in inputString:gmatch("[^|]+") do --on a pipe.  I think.  "%S+" would make it split the string on a space.
        table.insert(words, word)
    end
    return words
end
--[[  use getArgs like this!
    local arguments = getArgs(message)
    local arg1 = arguments[1] --first word of the string
    local arg2 = arguments[2] --second word of the string
    local arg3 = arguments[3] --And so on.
   
--]]

-----------------------------------------------------------------------//Zone geometry helpers
function rotateAboutPoint(inputPoint, rotationPoint, angle)
    -- Trig!
    local cosAngle = math.cos(angle)
    local sinAngle = math.sin(angle)

    local x = rotationPoint.x + (((inputPoint.x-rotationPoint.x)*cosAngle) - ((inputPoint.y-rotationPoint.y)*sinAngle))
    local y = rotationPoint.y + (((inputPoint.x-rotationPoint.x)*sinAngle) + ((inputPoint.y-rotationPoint.y)*cosAngle))
   
    return point.new(x, y);
end

function translateObjectGeom(inputGeom, startPoint, angle)
    local outputGeom = {}
   
    -- Our rotatePoint will always be the first in the input geometry
    local rotatePoint = inputGeom[1]
   
    for index, value in ipairs(inputGeom) do
        -- Our first point we don't need to do translation/rotation
        if index == 1 then
            table.insert(outputGeom, point.new(startPoint.x + rotatePoint.x, startPoint.y + rotatePoint.y))
        else
            local newPoint = rotateAboutPoint(value, rotatePoint, angle)
            table.insert(outputGeom, point.new(startPoint.x + newPoint.x, startPoint.y + newPoint.y))
        end
    end
   
    return outputGeom
end

Re: Raptor's Helper Functions

PostPosted: Wed May 08, 2013 8:31 am
by Fordcars
Neat!!

Re: Raptor's Helper Functions

PostPosted: Thu May 16, 2013 6:13 am
by bobdaduck
  Code:
--
-- Random float generator - because math.random() is stupid
--
-- Returns a floating point number between two numbers.
--
function randomFloat(arg1, arg2)
    -- One argument returns a random floating point number between 0 and the number
    if arg2 == nil then
        return math.random() * arg1
    end
   
    -- Else return a random number between both floating point numbers
    return (math.random() * (arg2 - arg1)) + arg1
end

Re: Raptor's Helper Functions

PostPosted: Fri May 17, 2013 9:36 am
by Fordcars
Why not use:
  Code:
math.randomseed( os.time() )


for better random :o

Re: Raptor's Helper Functions

PostPosted: Mon Jul 15, 2013 6:49 am
by raptor
Changing the seed would be needed if you were using Lua's default math.random() function. BUT...

We override the default Lua math.random() it in Bitfighter to use the systems random number generator. It is much better :)

Re: Raptor's Helper Functions

PostPosted: Wed Jul 31, 2013 11:24 am
by Fordcars
Alright!