share/hedgewars/Data/Scripts/Multiplayer/Gravity.lua
author unc0rr
Fri, 03 Jan 2014 00:37:48 +0400
changeset 9908 81eb25ddf594
parent 9906 93c16a4b0c6a
child 9910 b0988e40e411
permissions -rw-r--r--
- Utility script for parameters handling - Add varying gravity to Gravity script

HedgewarsScriptLoad("/Scripts/Locale.lua")
HedgewarsScriptLoad("/Scripts/Params.lua")

local gravity = 100
local mingravity
local maxgravity
local delta = 0
local period
local periodtimer = 0
local wdGameTicks = 0
local wdTTL = 0
local mln = 1000000

function onNewTurn()
    SetGravity(gravity)
    wdGameTicks = GameTime
end

function onGameTick20()
    if wdGameTicks + 15000 < GameTime then
        SetGravity(100)
    else
        if wdTTL ~= TurnTimeLeft then
            wdGameTicks = GameTime
        end

        if delta == nil then
            if periodtimer == 0 then
                periodtimer = period * 2
                SetGravity(div(GetRandom(maxgravity - mingravity) + mingravity, mln))
            else
                periodtimer = periodtimer - 1
            end
        elseif delta == 0 then
            SetGravity(gravity)
        else
            if delta > 0 and gravity + delta > maxgravity then
                gravity = maxgravity
                delta = -delta
            elseif delta < 0 and gravity - delta < mingravity then
                gravity = mingravity
                delta = -delta
            else
                gravity = gravity + delta
            end

            SetGravity(div(gravity, mln))
        end
    end

    wdTTL = TurnTimeLeft
end

function onGameInit()
    parseParams()

    gravity = params["g"]

    mingravity = gravity
    maxgravity = params["g2"]
    period = params["period"]

    if mingravity ~= nil and maxgravity ~= nil then
        if period ~= nil then
            period = div(period, 40)
        else
            period = 125
        end

        if mingravity > maxgravity then
            mingravity, maxgravity = maxgravity, mingravity
        end

        mingravity = mingravity * mln
        maxgravity = maxgravity * mln
        gravity = mingravity

        if period > 0 then
            delta = div(maxgravity - mingravity, period)
        else
            period = -period
            delta = nil
        end
    end

    if gravity == nil then
        gravity = 100
    end
end

function onGameStart()
    if delta == nil then
        v = string.format(loc("random in range from %i%% to %i%% with period of %i msec"), div(mingravity, mln), div(maxgravity, mln), period * 40)
    elseif period ~= nil then
        v = string.format(loc("changing range from %i%% to %i%% with period of %i msec"), div(mingravity, mln), div(maxgravity, mln), period * 40)
    else
        v = div(gravity, mln) .. "%"
    end

    ShowMission(loc("Gravity"),
                loc("Current setting is ") .. v,
                loc("Setup:|'g=150', where 150 is 150% of normal gravity") .. "|"
                .. loc("or 'g=50, g2=150, period=4000' for gravity changing|from 50 to 150 and back with period of 4000 msec")
                .. "||" .. loc("Set period to negative value for random gravity"),
                0, 5000)
end