BitSwitch
It will randomly switch 2 player's or bot's position, energy and health at random intervals (can be changed with parameters or in code). I find it brings really awesome gameplay. For example, you are in a big battle with Quartz, but you are out of energy, nearly out of health, and just before he shoots you, you are lucky enough to get switched by some n00b and get full health and energy and can happily see the words "Quartz zapped Forcars" in the top of the screen.
The rest of this post is just for more details, if you want to, you can use the script now with or without any arguments.
ARGUMENTS:
So, to use arguments in levelgens, just type them next to the levelgen name in the level parameters.
Example:
So, the arguments are:
The time delay is in Milliseconds (ms), so 10000 ms is equal to 10 seconds.
Have a marging will add a 40% margin in the delay. So, if the time selay is 10000 ms (10 seconds), the switches might be, selected randomly at each switch, 6, 7, 8, 9, 10, 11, 12, 13 or 14 seconds (well, milliseconds).
The say switches will say the people the script switched in global chat.
If you don't use any arguments though, the default values will be:
So, the masterpiece:
- Code:
- -- Bit Switch
-- by fordcars
allItems = {}
function main()
bitSwitchVersion = "0.1" -- To be professional, you know
bitSwitchDev = "fordcars"
defaultDelay = 30000
haveMarginDefault = true
saySwitchedDefault = true
timeAddSubPercent = nil
marginPercentDefault = 40
tempString = nil
logprint("")
tempString = "----------Running Bit Switch " .. bitSwitchVersion
logprint(tempString)
tempString = "----------by " .. bitSwitchDev
logprint(tempString)
logprint("")
timeDelay = tonumber(arg[1]) or defaultDelay
haveMargin = arg[2] or haveMarginDefault
saySwitched = arg[3] or saySwitchedDefault
haveMargin = stringToBoolean(haveMargin)
saySwitched = stringToBoolean(saySwitched)
timeAddSubPercent = findPercentage(timeDelay,marginPercentDefault)
if(timeDelay<0) then
tempString = "Error, time delay under 0, reverting to default (" .. defaultDelay .. ")"
logprint(tempString)
timeDelay = defaultDelay
timeAddSubPercent = findPercentage(timeDelay,marginPercentDefault)
end
if(haveMargin==true) then
if(timeDelay+timeAddSubPercent<0 or timeDelay-timeAddSubPercent<0) then
tempString = "Bit Switch has gotten a delay that, after adding the margin for more randomness, is under 0, reverting it to default (" .. defaultDelay .. ")"
logprint(tempString)
timeDelay = defaultDelay
timeAddSubPercent = findPercentage(timeDelay,40)
else
Timer:scheduleOnce(switchPlayers,getRandomNumber(timeDelay-timeAddSubPercent,timeDelay+timeAddSubPercent))
end
else
Timer:scheduleRepeating(switchPlayers,timeDelay)
end
end
function stringToBoolean(stringToConvertToBoolean)
if(stringToConvertToBoolean=="true" or stringToConvertToBoolean==true) then
return true
else
return false
end
end
function findPercentage(totalValue,percentage)
local finalValue = (totalValue / 100) * percentage
return finalValue
end
function getLength(arr)
local arrLength = 0
for i,v in ipairs(arr) do
arrLength = arrLength + 1
end
return arrLength
end
function switchPlayers()
table.clear(allItems)
local playersInfo = {}
local allItemsLength = nil
local nameString = nil
-- playerOneIndex 1
-- playerOne 2
-- playerOneHealth 3
-- playerOneEnergy 4
-- playerOnePos 5
-- playerTwoIndex 6
-- playerTwo 7
-- playerTwoHealth 8
-- playerTwoEnergy 9
-- playerTwoPos 10
bf:findAllObjects(allItems, ObjType.Robot, ObjType.Ship)
if(getLength(allItems)>1) then
-- Gather both players' info
allItemsLength = getLength(allItems)
playersInfo[1] = getRandomNumber(1,allItemsLength)
playersInfo[2] = allItems[playersInfo[1]]
playersInfo[3] = playersInfo[2]:getHealth()
playersInfo[4] = playersInfo[2]:getEnergy()
playersInfo[5] = playersInfo[2]:getPos()
table.remove(allItems,playersInfo[1])
playersInfo[6] = getRandomNumber(1,allItemsLength-1)
playersInfo[7] = allItems[playersInfo[6]]
playersInfo[8] = playersInfo[7]:getHealth()
playersInfo[9] = playersInfo[7]:getEnergy()
playersInfo[10] = playersInfo[7]:getPos()
-- Switch Players
playersInfo[2]:setHealth(playersInfo[8])
playersInfo[2]:setEnergy(playersInfo[9])
playersInfo[2]:setPos(playersInfo[10])
playersInfo[7]:setHealth(playersInfo[3])
playersInfo[7]:setEnergy(playersInfo[4])
playersInfo[7]:setPos(playersInfo[5])
nameString = "Switched " .. playersInfo[2]:getPlayerInfo():getName() .. " and " .. playersInfo[7]:getPlayerInfo():getName() .. "!"
logprint(nameString)
if(saySwitched==true) then
globalMsg(nameString)
end
else
logprint("Bit Switch did not find more than 1 player, aborting")
end
if(haveMargin==true) then
Timer:scheduleOnce(switchPlayers,getRandomNumber(timeDelay-timeAddSubPercent,timeDelay+timeAddSubPercent))
end
end
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.