FAQ  •  Register  •  Login

Raptor's Helper Functions

<<

bobdaduck

User avatar

Global Moderator

Posts: 790

Joined: Thu Mar 11, 2010 1:39 pm

Location: Utah

Post Wed May 08, 2013 7:11 am

Raptor's Helper Functions

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
Little_Apple wrote:DnD: the REAL bitfighter levelgen documentation

Santiago ZAP wrote:bob doesn't make new maps, he makes new gamemodes
<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Wed May 08, 2013 8:31 am

Re: Raptor's Helper Functions

Neat!!
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.
<<

bobdaduck

User avatar

Global Moderator

Posts: 790

Joined: Thu Mar 11, 2010 1:39 pm

Location: Utah

Post Thu May 16, 2013 6:13 am

Re: Raptor's Helper Functions

  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
Little_Apple wrote:DnD: the REAL bitfighter levelgen documentation

Santiago ZAP wrote:bob doesn't make new maps, he makes new gamemodes
<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Fri May 17, 2013 9:36 am

Re: Raptor's Helper Functions

Why not use:
  Code:
math.randomseed( os.time() )


for better random :o
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.
<<

raptor

Posts: 1046

Joined: Mon Oct 11, 2010 9:03 pm

Post Mon Jul 15, 2013 6:49 am

Re: Raptor's Helper Functions

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 :)
<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Wed Jul 31, 2013 11:24 am

Re: Raptor's Helper Functions

Alright!
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.

Return to Levelgen Gallery

Who is online

Users browsing this forum: No registered users and 3 guests

cron