Raptor's Helper Functions
Posted: Wed May 08, 2013 7:11 am
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