share/hedgewars/Data/Scripts/Multiplayer/Gravity.lua
author nemo
Sat, 20 Dec 2014 16:20:37 -0500
changeset 10696 173069dd7b17
parent 10509 d62d3c706947
child 10738 62659c427e5d
permissions -rw-r--r--
Make amGirder invisible in construction mode since it allows placing anywhere in fortress. Print "insufficient power" if placement was blocked due to that, to avoid confusing noobs.
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
9985
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    14
local script2_onNewTurn
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    15
local script2_onGameTick20
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    16
local script2_onGameStart
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    17
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    18
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    19
function grav_onNewTurn()
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    20
    SetGravity(gravity)
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    21
    wdGameTicks = GameTime
9985
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    22
    
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    23
    if script2_onNewTurn ~= nil then
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    24
        script2_onNewTurn()
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    25
    end    
9816
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    26
end
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    27
9985
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    28
function grav_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
    29
    if wdGameTicks + 15000 < GameTime then
9816
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    30
        SetGravity(100)
9908
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
        if wdTTL ~= TurnTimeLeft then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    33
            wdGameTicks = GameTime
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    34
        end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    35
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    36
        if delta == nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    37
            if periodtimer == 0 then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    38
                periodtimer = period * 2
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    39
                SetGravity(div(GetRandom(maxgravity - mingravity) + mingravity, mln))
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    40
            else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    41
                periodtimer = periodtimer - 1
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    42
            end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    43
        elseif delta == 0 then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    44
            SetGravity(gravity)
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    45
        else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    46
            if delta > 0 and gravity + delta > maxgravity then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    47
                gravity = maxgravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    48
                delta = -delta
10509
d62d3c706947 some gravity script fixes
sheepluva
parents: 9985
diff changeset
    49
            elseif delta < 0 and gravity + delta < mingravity then
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    50
                gravity = mingravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    51
                delta = -delta
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    52
            else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    53
                gravity = gravity + delta
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    54
            end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    55
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    56
            SetGravity(div(gravity, mln))
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    57
        end
9816
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    58
    end
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    59
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    60
    wdTTL = TurnTimeLeft
9985
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    61
    
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    62
    if script2_onGameTick20 ~= nil then
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    63
        script2_onGameTick20()
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    64
    end    
9816
e71188c140ee No gravity mode
unc0rr
parents:
diff changeset
    65
end
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
    66
9985
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
    67
function onParameters()
9906
93c16a4b0c6a Only emit roomNameUpdated when it actually is changed
unc0rr
parents: 9902
diff changeset
    68
    parseParams()
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    69
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    70
    gravity = params["g"]
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    71
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    72
    mingravity = gravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    73
    maxgravity = params["g2"]
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    74
    period = params["period"]
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    75
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    76
    if mingravity ~= nil and maxgravity ~= nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    77
        if period ~= nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    78
            period = div(period, 40)
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    79
        else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    80
            period = 125
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    81
        end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    82
10509
d62d3c706947 some gravity script fixes
sheepluva
parents: 9985
diff changeset
    83
        mingravity = mingravity * mln
d62d3c706947 some gravity script fixes
sheepluva
parents: 9985
diff changeset
    84
        maxgravity = maxgravity * mln
d62d3c706947 some gravity script fixes
sheepluva
parents: 9985
diff changeset
    85
d62d3c706947 some gravity script fixes
sheepluva
parents: 9985
diff changeset
    86
        -- note: mingravity and maxgravity MUST NOT be strings at this point
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    87
        if mingravity > maxgravity then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    88
            mingravity, maxgravity = maxgravity, mingravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    89
        end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    90
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    91
        gravity = mingravity
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    92
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    93
        if period > 0 then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    94
            delta = div(maxgravity - mingravity, period)
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    95
        else
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    96
            period = -period
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
    97
            delta = nil
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
    end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   100
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   101
    if gravity == nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   102
        gravity = 100
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   103
    end
9985
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   104
    
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   105
    secondScript = params["script2"]
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   106
    
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   107
    if secondScript ~= nil then
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   108
        onParameters = nil
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   109
        HedgewarsScriptLoad("/Scripts/Multiplayer/" .. secondScript .. ".lua")
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   110
        
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   111
        script2_onNewTurn = onNewTurn
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   112
        script2_onGameTick20 = onGameTick20
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   113
        script2_onGameStart = onGameStart
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   114
                
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   115
        if onParameters ~= nil then
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   116
            onParameters()
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   117
        end
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   118
    end
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   119
    
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   120
    onNewTurn = grav_onNewTurn
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   121
    onGameTick20 = grav_onGameTick20
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   122
    onGameStart = grav_onGameStart
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
   123
end
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
   124
9985
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   125
function grav_onGameStart()
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   126
    if delta == nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   127
        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
   128
    elseif period ~= nil then
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   129
        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
   130
    else
9910
b0988e40e411 Oops, fix message
unc0rr
parents: 9908
diff changeset
   131
        v = gravity .. "%"
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   132
    end
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   133
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
   134
    ShowMission(loc("Gravity"),
9908
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   135
                loc("Current setting is ") .. v,
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   136
                loc("Setup:|'g=150', where 150 is 150% of normal gravity") .. "|"
81eb25ddf594 - Utility script for parameters handling
unc0rr
parents: 9906
diff changeset
   137
                .. 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
   138
                .. "||" .. loc("Set period to negative value for random gravity"),
9836
00cbbb195f11 Rework of No Gravity script:
unc0rr
parents: 9819
diff changeset
   139
                0, 5000)
9985
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   140
                
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   141
    if script2_onGameStart ~= nil then
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   142
        script2_onGameStart()
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   143
    end
9902
eb8e8b09b2f0 Special script parameter variable in game scheme
unc0rr
parents: 9898
diff changeset
   144
end
9985
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   145
42cd42e44c9a GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
unc0rr
parents: 9910
diff changeset
   146