2019-01-28 22:20:02 +02:00
|
|
|
local Event = require 'utils.event'
|
|
|
|
local Gui = require 'utils.gui'
|
|
|
|
local Global = require 'utils.global'
|
|
|
|
local Toast = require 'features.gui.toast'
|
2024-10-22 21:22:35 +02:00
|
|
|
local RS = require 'map_gen.shared.redmew_surface'
|
2019-01-28 22:20:02 +02:00
|
|
|
local round = math.round
|
|
|
|
local pairs = pairs
|
|
|
|
local main_button_name = Gui.uid_name()
|
|
|
|
|
|
|
|
local memory = {
|
2019-02-22 14:51:20 +02:00
|
|
|
last_percentage = 0
|
2019-01-28 22:20:02 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 14:51:20 +02:00
|
|
|
Global.register(
|
|
|
|
memory,
|
|
|
|
function(tbl)
|
|
|
|
memory = tbl
|
|
|
|
end,
|
|
|
|
'evolution_progress'
|
|
|
|
)
|
2019-01-28 22:20:02 +02:00
|
|
|
|
|
|
|
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,
|
2019-02-22 14:51:20 +02:00
|
|
|
['behemoth-biter'] = 0.901
|
2019-01-28 22:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local function get_evolution_percentage()
|
|
|
|
if not game then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2024-10-22 21:22:35 +02:00
|
|
|
local value = round(game.forces.enemy.get_evolution_factor(RS.get_surface_name()) * 1000) * 0.001
|
2019-01-28 22:20:02 +02:00
|
|
|
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)
|
2019-01-28 22:27:05 +02:00
|
|
|
local last_match = 'fish'
|
2019-01-28 22:20:02 +02:00
|
|
|
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
|
2019-02-22 14:51:20 +02:00
|
|
|
if alien_threshold < evolution_factor then
|
2019-01-28 22:20:02 +02:00
|
|
|
last_match = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-28 22:27:05 +02:00
|
|
|
return last_match
|
2019-01-28 22:20:02 +02:00
|
|
|
end
|
|
|
|
|
2019-02-22 14:51:20 +02:00
|
|
|
local function update_gui(player)
|
2024-08-03 18:31:10 +02:00
|
|
|
local button = Gui.get_top_element(player, main_button_name)
|
2019-02-22 14:51:20 +02:00
|
|
|
if button and button.valid then
|
|
|
|
local evolution_factor = get_evolution_percentage()
|
|
|
|
local evolution_button_number = evolution_factor * 100
|
|
|
|
local current_alien = get_alien_name(evolution_factor)
|
|
|
|
local sprite = 'entity/' .. current_alien
|
|
|
|
|
|
|
|
button.number = evolution_button_number
|
|
|
|
if sprite then
|
|
|
|
button.sprite = sprite
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-14 22:14:15 +02:00
|
|
|
local function player_created(event)
|
2019-05-16 12:10:56 +02:00
|
|
|
local player = game.get_player(event.player_index)
|
2019-01-28 22:20:02 +02:00
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local evolution_factor = get_evolution_percentage()
|
|
|
|
local alien_name = get_alien_name(evolution_factor)
|
|
|
|
|
2024-08-03 18:31:10 +02:00
|
|
|
Gui.add_top_element(player,
|
2020-09-14 22:14:15 +02:00
|
|
|
{
|
|
|
|
name = main_button_name,
|
|
|
|
type = 'sprite-button',
|
|
|
|
sprite = 'entity/' .. alien_name,
|
|
|
|
number = evolution_factor * 100,
|
|
|
|
tooltip = {'evolution_progress.tooltip'},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function player_joined(event)
|
|
|
|
local player = game.get_player(event.player_index)
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
update_gui(player)
|
2019-01-28 22:20:02 +02:00
|
|
|
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!'}
|
2019-02-22 14:51:20 +02:00
|
|
|
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
|
|
|
|
)
|
2019-01-28 22:20:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local players = game.connected_players
|
|
|
|
for i = 1, #players do
|
2019-02-22 14:51:20 +02:00
|
|
|
update_gui(players[i])
|
2019-01-28 22:20:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
|
|
|
|
2020-09-14 22:14:15 +02:00
|
|
|
Event.add(defines.events.on_player_created, player_created)
|
2019-01-28 22:20:02 +02:00
|
|
|
Event.add(defines.events.on_player_joined_game, player_joined)
|
|
|
|
Event.on_nth_tick(207, on_nth_tick)
|