FAQ  •  Register  •  Login

BitSwitch

<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Fri Jan 17, 2014 4:25 pm

BitSwitch

So, what does the levelgen do?

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:
  Code:
bitSwitch 10000 false false


So, the arguments are:
  Code:
-Time Delay

-Have a Margin (to have random intervals)

-Say Switches


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:
  Code:
Time Delay = 30000
Have Margin = true
Say Switches = true


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
Last edited by Fordcars on Fri Jan 31, 2014 5:39 pm, edited 7 times in total.
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.
<<

Skybax

User avatar

Posts: 1035

Joined: Thu Mar 11, 2010 9:17 am

Location: Washington

Post Fri Jan 17, 2014 10:27 pm

Re: BitSwitch

That could actually add for a really cool game mechanic lol
raptor wrote:Sorry Skybax, I hijacked your signature so I could post lots of info.
Whittling While wrote:Does this mean I finally get quoted in someone's signature?
watusimoto wrote:Who are the devs around here?!?
<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Sat Jan 18, 2014 12:36 am

Re: BitSwitch

Hehe thanks :zapdance:
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.
<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Sat Jan 18, 2014 12:01 pm

Re: BitSwitch

Update: Removed debugging stuff, sorry!
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.
<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Sat Jan 18, 2014 12:07 pm

Re: BitSwitch

Update: Get this! I finally got it! I replaced a timer with a different type of timer when the script has the Have Margin argument set to false. It's all clean of debugging too. There still might be a problem with Bitfighter and Lua Timers, though
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.
<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Mon Jan 27, 2014 7:25 pm

Re: BitSwitch

UPDATE: It's all good, for real this time :) There is a bug in 019 that raptor happily removed in 019a. It is still fine in 019 though, but the timing will be off. Happy switching!
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.
<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Fri Jan 31, 2014 5:41 pm

Re: BitSwitch

Done! Yes, the bug is confirmed to be repaired in 019a, happy levelgenning!
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 2 guests

cron