share/hedgewars/Data/Scripts/Multiplayer/Gravity.lua
author unc0rr
Fri, 03 Jan 2014 00:46:23 +0400
changeset 9910 b0988e40e411
parent 9908 81eb25ddf594
child 9985 42cd42e44c9a
permissions -rw-r--r--
Oops, fix message
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
     1
HedgewarsScriptLoad("/Scripts/Locale.lua")
9906
93c16a4b0c6a Only emit roomNameUpdated when it actually is changed
unc0rr
parents: 9902
diff changeset
     2
HedgewarsScriptLoad("/Scripts/Params.lua")
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
     3
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
     4
local gravity = 100
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
     5
local mingravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
     6
local maxgravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
     7
local delta = 0
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
     8
local period
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
     9
local periodtimer = 0
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    10
local wdGameTicks = 0
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    11
local wdTTL = 0
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    12
local mln = 1000000
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    13
9816
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    14
function onNewTurn()
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    15
    SetGravity(gravity)
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    16
    wdGameTicks = GameTime
9816
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    17
end
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    18
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    19
function onGameTick20()
9898
b87695f82d2e Don't set gravity to normal immediately on turn end, let watchdog do its job if game gets frozen
unc0rr
parents: 9836
diff changeset
    20
    if wdGameTicks + 15000 < GameTime then
9816
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    21
        SetGravity(100)
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    22
    else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    23
        if wdTTL ~= TurnTimeLeft then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    24
            wdGameTicks = GameTime
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    25
        end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    26
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    27
        if delta == nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    28
            if periodtimer == 0 then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    29
                periodtimer = period * 2
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    30
                SetGravity(div(GetRandom(maxgravity - mingravity) + mingravity, mln))
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    31
            else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    32
                periodtimer = periodtimer - 1
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    33
            end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    34
        elseif delta == 0 then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    35
            SetGravity(gravity)
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    36
        else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    37
            if delta > 0 and gravity + delta > maxgravity then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    38
                gravity = maxgravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    39
                delta = -delta
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    40
            elseif delta < 0 and gravity - delta < mingravity then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    41
                gravity = mingravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    42
                delta = -delta
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    43
            else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    44
                gravity = gravity + delta
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    45
            end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    46
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    47
            SetGravity(div(gravity, mln))
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    48
        end
9816
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    49
    end
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    50
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    51
    wdTTL = TurnTimeLeft
9816
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    52
end
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    53
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    54
function onGameInit()
9906
93c16a4b0c6a Only emit roomNameUpdated when it actually is changed
unc0rr
parents: 9902
diff changeset
    55
    parseParams()
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    56
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    57
    gravity = params["g"]
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    58
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    59
    mingravity = gravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    60
    maxgravity = params["g2"]
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    61
    period = params["period"]
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    62
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    63
    if mingravity ~= nil and maxgravity ~= nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    64
        if period ~= nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    65
            period = div(period, 40)
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    66
        else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    67
            period = 125
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    68
        end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    69
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    70
        if mingravity > maxgravity then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    71
            mingravity, maxgravity = maxgravity, mingravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    72
        end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    73
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    74
        mingravity = mingravity * mln
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    75
        maxgravity = maxgravity * mln
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    76
        gravity = mingravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    77
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    78
        if period > 0 then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    79
            delta = div(maxgravity - mingravity, period)
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    80
        else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    81
            period = -period
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    82
            delta = nil
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    83
        end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    84
    end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    85
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    86
    if gravity == nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    87
        gravity = 100
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    88
    end
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    89
end
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    90
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    91
function onGameStart()
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    92
    if delta == nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    93
        v = string.format(loc("random in range from %i%% to %i%% with period of %i msec"), div(mingravity, mln), div(maxgravity, mln), period * 40)
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    94
    elseif period ~= nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    95
        v = string.format(loc("changing range from %i%% to %i%% with period of %i msec"), div(mingravity, mln), div(maxgravity, mln), period * 40)
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    96
    else
9910
b0988e40e411 Oops, fix message
unc0rr
parents: 9908
diff changeset
    97
        v = gravity .. "%"
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    98
    end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    99
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
   100
    ShowMission(loc("Gravity"),
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   101
                loc("Current setting is ") .. v,
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   102
                loc("Setup:|'g=150', where 150 is 150% of normal gravity") .. "|"
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   103
                .. loc("or 'g=50, g2=150, period=4000' for gravity changing|from 50 to 150 and back with period of 4000 msec")
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   104
                .. "||" .. loc("Set period to negative value for random gravity"),
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
   105
                0, 5000)
9902
eb8e8b09b2f0 Special script parameter variable in game scheme
unc0rr
parents: 9898
diff changeset
   106
end