mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-01-03 22:52:13 +02:00
Added a GUI element + toasts to notify of biter evolution
This commit is contained in:
parent
0f05e34cd4
commit
53fad1a75b
@ -248,6 +248,10 @@ global.config = {
|
||||
ghosts_before_research = true,
|
||||
-- adds craftable loaders.
|
||||
loaders = true
|
||||
},
|
||||
-- adds a useless button with the biter percentage
|
||||
evolution_progress = {
|
||||
enabled = true,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -79,6 +79,9 @@ end
|
||||
if config.player_list.enabled then
|
||||
require 'features.gui.player_list'
|
||||
end
|
||||
if config.evolution_progress.enabled then
|
||||
require 'features.gui.evolution_progress'
|
||||
end
|
||||
if config.poll.enabled then
|
||||
require 'features.gui.poll'
|
||||
end
|
||||
|
126
features/gui/evolution_progress.lua
Normal file
126
features/gui/evolution_progress.lua
Normal file
@ -0,0 +1,126 @@
|
||||
local Event = require 'utils.event'
|
||||
local Gui = require 'utils.gui'
|
||||
local Game = require 'utils.game'
|
||||
local Global = require 'utils.global'
|
||||
local Toast = require 'features.gui.toast'
|
||||
local round = math.round
|
||||
local pairs = pairs
|
||||
local main_button_name = Gui.uid_name()
|
||||
|
||||
local memory = {
|
||||
last_percentage = 0,
|
||||
}
|
||||
|
||||
Global.register(memory, function (tbl) memory = tbl end, 'evolution_progress')
|
||||
|
||||
local button_sprites = {
|
||||
['small-biter'] = 0,
|
||||
['medium-biter'] = 0.2,
|
||||
['small-spitter'] = 0.25,
|
||||
['medium-spitter'] = 0.4,
|
||||
['big-spitter'] = 0.5,
|
||||
['big-biter'] = 0.501,
|
||||
['behemoth-spitter'] = 0.9,
|
||||
['behemoth-biter'] = 0.901,
|
||||
}
|
||||
|
||||
local function get_evolution_percentage()
|
||||
if not game then
|
||||
return 0
|
||||
end
|
||||
|
||||
local value = round(game.forces.enemy.evolution_factor * 1000) * 0.001
|
||||
if value < 0.001 then
|
||||
-- 0.00 won't be shown on the button as value
|
||||
return 0.001
|
||||
end
|
||||
|
||||
return value
|
||||
end
|
||||
|
||||
local function get_alien_name(evolution_factor)
|
||||
local last_match
|
||||
for name, alien_threshold in pairs(button_sprites) do
|
||||
if evolution_factor == alien_threshold then
|
||||
return name
|
||||
end
|
||||
|
||||
-- next alien evolution_factor isn't reached
|
||||
if alien_threshold > evolution_factor then
|
||||
return last_match
|
||||
end
|
||||
|
||||
-- surpassed this alien evolution_factor
|
||||
if alien_threshold < evolution_factor then
|
||||
last_match = name
|
||||
end
|
||||
end
|
||||
|
||||
-- shouldn't be reached but shouldn't crash either
|
||||
return 'fish'
|
||||
end
|
||||
|
||||
local function player_joined(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
if not player or not player.valid then
|
||||
return
|
||||
end
|
||||
|
||||
if player.gui.top[main_button_name] ~= nil then
|
||||
return
|
||||
end
|
||||
|
||||
local evolution_factor = get_evolution_percentage()
|
||||
local alien_name = get_alien_name(evolution_factor)
|
||||
|
||||
player.gui.top.add({
|
||||
name = main_button_name,
|
||||
type = 'sprite-button',
|
||||
sprite = 'entity/' .. alien_name,
|
||||
number = evolution_factor * 100,
|
||||
}).enabled = false
|
||||
end
|
||||
|
||||
local function on_nth_tick()
|
||||
local previous_evolution_factor = memory.last_percentage
|
||||
local evolution_factor = get_evolution_percentage()
|
||||
|
||||
if previous_evolution_factor == evolution_factor then
|
||||
return
|
||||
end
|
||||
|
||||
memory.last_percentage = evolution_factor
|
||||
|
||||
local previous_alien = get_alien_name(previous_evolution_factor)
|
||||
local current_alien = get_alien_name(evolution_factor)
|
||||
local sprite
|
||||
|
||||
if current_alien ~= previous_alien then
|
||||
sprite = 'entity/' .. current_alien
|
||||
local caption = {'', 'Evolution notice: ', {'entity-name.' .. current_alien}, ' sighted!'}
|
||||
Toast.toast_all_players_template(10, function (container)
|
||||
container.add({type = 'sprite', sprite = sprite})
|
||||
local text = container.add({type = 'label', caption = caption, name = Toast.close_toast_name})
|
||||
local text_style = text.style
|
||||
text_style.single_line = false
|
||||
text_style.vertical_align = 'center'
|
||||
end)
|
||||
end
|
||||
|
||||
local players = game.connected_players
|
||||
local evolution_button_number = evolution_factor * 100
|
||||
for i = 1, #players do
|
||||
local button = players[i].gui.top[main_button_name]
|
||||
if button and button.valid then
|
||||
button.number = evolution_button_number
|
||||
if sprite then
|
||||
button.sprite = sprite
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
||||
|
||||
Event.add(defines.events.on_player_joined_game, player_joined)
|
||||
Event.on_nth_tick(207, on_nth_tick)
|
@ -1,14 +1,11 @@
|
||||
local Event = require 'utils.event'
|
||||
local Game = require 'utils.game'
|
||||
local Global = require 'utils.global'
|
||||
local Gui = require 'utils.gui'
|
||||
local Token = require 'utils.token'
|
||||
local Color = require 'resources.color_presets'
|
||||
|
||||
local type = type
|
||||
local tonumber = tonumber
|
||||
local pairs = pairs
|
||||
local size = table.size
|
||||
local next = next
|
||||
|
||||
local Public = {}
|
||||
|
||||
|
@ -45,7 +45,7 @@
|
||||
-- Event.remove_removable(defines.events.on_built_entity, handler)
|
||||
--
|
||||
-- It's not an error to register the same token multiple times to the same event, however when
|
||||
-- removing only the first occurance is removed.
|
||||
-- removing only the first occurrence is removed.
|
||||
--
|
||||
-- ** Event.add_removable_function(event_name, func) **
|
||||
--
|
||||
|
Loading…
Reference in New Issue
Block a user