1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-06 00:23:49 +02:00

switch to scheduler

This commit is contained in:
Nathan Fox 2022-10-19 22:51:01 -04:00
parent 9b19af539f
commit 050a09f3fc
No known key found for this signature in database
GPG Key ID: 7802E8DA4841B24F
3 changed files with 51 additions and 41 deletions

View File

@ -231,7 +231,7 @@ require 'utils.freeplay'
--require 'maps.cube'
--require 'maps.mountain_race.main'
--require 'maps.native_war.main'
--require 'maps.scrap_towny_ffa.main'
require 'maps.scrap_towny_ffa.main'
---------------------------------------------------------------
---------------- MORE MODULES HERE ----------------

View File

@ -18,6 +18,7 @@ require 'maps.scrap_towny_ffa.fluids_are_explosive'
require 'maps.scrap_towny_ffa.trap'
require 'maps.scrap_towny_ffa.turrets_drop_ammo'
require 'maps.scrap_towny_ffa.vehicles'
require 'maps.scrap_towny_ffa.suicide'
local Event = require 'utils.event'
local Autostash = require 'modules.autostash'
@ -37,7 +38,6 @@ local Gui = require 'utils.gui'
local Color = require 'utils.color_presets'
local Where = require 'utils.commands.where'
local Inventory = require 'modules.show_inventory'
local Suicide = require 'maps.scrap_towny_ffa.suicide'
local function spairs(t, order)
local keys = {}
@ -183,7 +183,6 @@ local tick_actions = {
[60 * 5] = Team.update_town_chart_tags, -- each minute, at 05 seconds
[60 * 10] = Team.set_all_player_colors, -- each minute, at 10 seconds
[60 * 15] = Fish.reproduce, -- each minute, at 15 seconds
[60 * 20] = Suicide.check, -- each minute, at 20 seconds
[60 * 25] = Biters.unit_groups_start_moving, -- each minute, at 25 seconds
[60 * 30] = Radar.reset, -- each minute, at 30 seconds
[60 * 45] = Biters.validate_swarms, -- each minute, at 45 seconds

View File

@ -1,8 +1,52 @@
local Scheduler = require 'utils.scheduler'
local ScenarioTable = require 'maps.scrap_towny_ffa.table'
local Event = require 'utils.event'
local yellow = { r = 200, g = 200, b = 0 }
-- Must be at least 1 minute
local minutes_to_die = 10
local one_minute = 60 * 60
local one_minute = 60 * 10 -- 60 * 60
local function on_player_died(event)
local this = ScenarioTable.get_table()
local player = game.players[event.player_index]
this.suicides[player.index] = nil
end
Event.add(defines.events.on_player_died, on_player_died)
local suicide_handler = Scheduler.set(function(data)
for i = 1, #data do
local this = ScenarioTable.get_table()
local player_index = data[i].player_index
local player = game.get_player(player_index)
if not player or not player.valid or not player.character then
return
end
if not this.suicides[player.index] then
-- the suicide was cancelled (the character died)
return
end
local minutes_remaining = this.suicides[player.index].minutes_remaining
if minutes_remaining <= 0 then
player.character.die(player.force, player.character)
this.suicides[player.index] = nil
else
if minutes_remaining == 1 then
player.print(minutes_remaining .. " minute remaining until death.", yellow)
else
player.print(minutes_remaining .. " minutes remaining until death.", yellow)
end
this.suicides[player.index].minutes_remaining = this.suicides[player.index].minutes_remaining - 1
Scheduler.timer(game.tick + one_minute, data[i].handler, { player_index = player.index, handler = data[i].handler})
end
end
end)
commands.add_command(
'suicide',
@ -15,46 +59,13 @@ commands.add_command(
return
end
if this.suicides[player.name] then
if this.suicides[player.index] then
player.print("You are already dying!", yellow)
return
end
-- Schedule death for 30 seconds less than the average. Death is checked every minute, so this keeps
-- the average correct.
this.suicides[player.name] = game.tick + (one_minute * minutes_to_die - (one_minute * 0.5))
player.print("You ate a poison pill. You will die in approximately " .. minutes_to_die .. " minutes.", yellow)
this.suicides[player.index] = {minutes_remaining = minutes_to_die - 1}
Scheduler.timer(game.tick + one_minute, suicide_handler, { player_index = player.index, handler = suicide_handler })
player.print("You ate a poison pill. You will die in " .. minutes_to_die .. " minutes.", yellow)
end
)
local Public = {}
function Public.check()
local this = ScenarioTable.get_table()
for name, death_time in pairs(this.suicides) do
local remaining_time = death_time - game.tick
local player = game.get_player(name)
if not player or not player.valid then
return
end
if not player.character then
return
end
if remaining_time <= 0 then
player.character.die(player.force, player.character)
this.suicides[player.name] = nil
else
local remaining_minutes = math.ceil(remaining_time / 3600)
if remaining_minutes ~= minutes_to_die then
if remaining_minutes == 1 then
player.print(remaining_minutes .. " minute remaining until death.", yellow)
else
player.print(remaining_minutes .. " minutes remaining until death.", yellow)
end
end
end
end
end
return Public