Superhot/control.lua

98 lines
2.5 KiB
Lua

-- Copyright (C) 2019 MrBesen
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
--
local miningActive = 100
function init()
if not global.Superhot then
global.Superhot = {}
end
if not global.Superhot.playermovement then
global.Superhot.playermovement = {}
end
end
function getDist(mov)
return math.sqrt((mov[1] * mov[1]) + (mov[2] * mov[2]))
end
-- caps the speed using a logarithmic function, is undefined for x <= 0!
function movementcap(x)
return (75.88 * math.log(x)) + 244
end
script.on_event({defines.events.on_tick},
function (e)
init() -- init variables TODO: do somewhere else!
local activeMax = 0
local lastplayer -- just for DEBUG
for index,player in pairs(game.connected_players) do --loop through all online players on the server
lastplayer = player
if player.valid and player.connected and player.character then
local active = 0
-- is mining?
if player.mining_state.mining then
active = miningActive
end
--is moving
local newpos = player.position
local oldpos = global.Superhot.playermovement[player.index]
local mov = {0,0}
if oldpos ~= nil then
mov = {oldpos.x - newpos.x, oldpos.y - newpos.y}
local dist = getDist(mov)
if dist > 0 then
active = active + movementcap(dist)
end
end
--update movement
global.Superhot.playermovement[player.index] = newpos
--update max
if active > activeMax then
activeMax = active
end
end
end
local goal = activeMax/100.0
local diff = goal - game.speed
-- make acceleration smoother
if diff > 0.2 or diff < -0.2 then
diff = diff / 2.0
end
goal = game.speed + diff
--clip to upper and lower bound
if goal < 0.1 then
goal = 0.1
elseif goal > 10 then
goal = 10
end
--actually set it
game.speed = goal
end
)
-- spped modifyer: game.speed=1
-- freeze time: game.player.surface.freeze_daytime=true
-- defines.events.on_pre_player_mined_item