--[[
This file will be run whenever a lua state is created. It's a good place to
initialize any functions you want available globally, as well as to override
default lua behaviors.
--]]

math.isinteger = function(test)
	return((test % 1)==0)
end

-- math.random doesn't really do what we want it to do (ie easily allow random
-- numbers in a floating point range) and will often do the wrong thing or
-- fail silently, so it gets removed. Keep a local reference to the function
-- to allow behavior of our choosing
local _random = math.random
math.random = nil

-- the GHF random. math.random isn't reused in order to avoid confusion for
-- people reading the lua docs (ie it would work differently than documented
-- so make people call something else.)

-- Random function that allows two floating point numbers to be used as the
-- range. If rangeMax is not present, range is considered 1 to rangeMin

-- If both inputs are integers, return value will be a random integer,
-- otherwise, result will be a floating point number (which will be the
-- range mulitplied by the result of math.random())
math.Random = function(rangeMin, rangeMax)
	if rangeMax == nil then
		if rangeMin == nil then
			return _random()
		else
			rangeMin, rangeMax = 1, rangeMin
		end
	elseif rangeMin > rangeMax then
		rangeMax, rangeMin = rangeMin, rangeMax
	end
	
	if math.isinteger(rangeMin) and math.isinteger(rangeMax) then
		return _random(rangeMin, rangeMax)
	end
	
	local range = rangeMax - rangeMin
	local r = _random()
	return rangeMin + (r * range)
end
