1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-03-11 14:49:59 +02:00

Diggy integration with new ScoreTracker

This commit is contained in:
Lynn 2019-05-30 22:47:19 +02:00
parent f8812f6189
commit 8d8ce28228
6 changed files with 29 additions and 173 deletions

View File

@ -29,9 +29,6 @@ gui_buff_other=+__1__ __2__
gui_experience_button_tip=Diggy leveling progress
gui_close_btn=Close
gui_coin_button_tip=Diggy scoretable
gui_coin_sent=sent __1__ coins into space! The space station is now holding __2__ coins.
toast_new_level=Your team has reached level __1__!
cave_collapse=Cave collapsed!
@ -42,6 +39,10 @@ night_time_warning=Placing solar panels underground does not seem\nto have an ef
cracking_sound_1=R U N, Y O U F O O L S !
cracking_sound_2=C R A C K
score_cave_collapses=Cave collapses
score_mine_size=Mine size
score_experience_lost=Experience lost
# locale linked to the quadrants scenario
[quadrants]
on=ON

View File

@ -1,105 +1,20 @@
--[[-- info
Provides the ability to collect coins and send them to space.
Provides the ability to collect coins.
]]
-- dependencies
local Event = require 'utils.event'
local ScoreTable = require 'map_gen.maps.diggy.score_table'
local Debug = require 'map_gen.maps.diggy.debug'
local Template = require 'map_gen.maps.diggy.template'
local Perlin = require 'map_gen.shared.perlin_noise'
local random = math.random
local ceil = math.ceil
local pairs = pairs
local Gui = require 'utils.gui'
local utils = require 'utils.core'
-- this
local CoinGathering = {}
-- some GUI stuff
local function redraw_table(data)
local list = data.list
Gui.clear(list)
data.frame.caption = 'Scoretable'
for name, value in pairs(ScoreTable.all()) do
local table = list.add({type = 'table', column_count = 2})
local key = table.add({type = 'label', name = 'Diggy.CoinGathering.Frame.List.Key', caption = name})
key.style.minimal_width = 175
local val = table.add({type = 'label', name = 'Diggy.CoinGathering.Frame.List.Val', caption = utils.comma_value(value)})
val.style.minimal_width = 225
end
end
local function toggle(event)
local player = event.player
local center = player.gui.left
local frame = center['Diggy.CoinGathering.Frame']
if (frame and event.trigger == nil) then
Gui.destroy(frame)
return
elseif (frame) then
local data = Gui.get_data(frame)
redraw_table(data)
return
end
frame = center.add({name = 'Diggy.CoinGathering.Frame', type = 'frame', direction = 'vertical'})
local scroll_pane = frame.add({type = 'scroll-pane'})
scroll_pane.style.maximal_height = 400
frame.add({type = 'button', name = 'Diggy.CoinGathering.Button', caption = 'Close'})
local data = {
frame = frame,
list = scroll_pane
}
redraw_table(data)
Gui.set_data(frame, data)
end
local function on_player_created(event)
game.get_player(event.player_index).gui.top.add({
name = 'Diggy.CoinGathering.Button',
type = 'sprite-button',
sprite = 'item/coin',
tooltip = {'diggy.gui_coin_button_tip'}
})
end
Gui.allow_player_to_toggle_top_element_visibility('Diggy.CoinGathering.Button')
Gui.on_click('Diggy.CoinGathering.Button', toggle)
Gui.on_custom_close('Diggy.CoinGathering.Frame', function (event)
event.element.destroy()
end)
function CoinGathering.update_gui()
for _, p in pairs(game.connected_players) do
local frame = p.gui.left['Diggy.CoinGathering.Frame']
if frame and frame.valid then
local data = {player = p, trigger = 'update_gui'}
toggle(data)
end
end
end
function CoinGathering.register(config)
Event.add(defines.events.on_player_created, on_player_created)
Event.on_nth_tick(61, CoinGathering.update_gui)
ScoreTable.reset('Coins sent to space')
local seed
local noise_variance = config.noise_variance
local function get_noise(surface, x, y)
@ -109,14 +24,6 @@ function CoinGathering.register(config)
local distance_required = config.minimal_treasure_chest_distance * config.minimal_treasure_chest_distance
Event.add(defines.events.on_rocket_launched, function (event)
local coins = event.rocket.get_inventory(defines.inventory.rocket).get_item_count('coin')
if coins > 0 then
local sum = ScoreTable.add('Coins sent to space', coins)
game.print({'diggy.gui_coin_sent', coins, sum})
end
end)
local treasure_chest_noise_threshold = config.treasure_chest_noise_threshold
Event.add(Template.events.on_void_removed, function (event)
local position = event.position

View File

@ -4,7 +4,7 @@
-- dependencies
local Event = require 'utils.event'
local Template = require 'map_gen.maps.diggy.template'
local ScoreTable = require 'map_gen.maps.diggy.score_table'
local ScoreTracker = require 'utils.score_tracker'
local Debug = require 'map_gen.maps.diggy.debug'
local Task = require 'utils.task'
local Token = require 'utils.token'
@ -12,13 +12,11 @@ local Global = require 'utils.global'
local CreateParticles = require 'features.create_particles'
local RS = require 'map_gen.shared.redmew_surface'
local table = require 'utils.table'
local random = math.random
local floor = math.floor
local pairs = pairs
local pcall = pcall
local is_diggy_rock = Template.is_diggy_rock
local increment_score = ScoreTable.increment
local template_insert = Template.insert
local raise_event = script.raise_event
local set_timeout = Task.set_timeout
@ -27,6 +25,7 @@ local ceiling_crumble = CreateParticles.ceiling_crumble
local clear_table = table.clear_table
local collapse_rocks = Template.diggy_rocks
local collapse_rocks_size = #collapse_rocks
local cave_collapses_name = 'cave-collapses'
-- this
local DiggyCaveCollapse = {}
@ -173,7 +172,7 @@ local function collapse(args)
template_insert(surface, {}, create_collapse_template(positions, surface))
raise_event(DiggyCaveCollapse.events.on_collapse, args)
increment_score('Cave collapse')
ScoreTracker.change_for_global(cave_collapses_name, 1)
end
local on_collapse_timeout_finished = Token.register(collapse)
@ -336,6 +335,11 @@ end
@param global_config Table {@see Diggy.Config}.
]]
function DiggyCaveCollapse.register(cfg)
ScoreTracker.register(cave_collapses_name, {'diggy.score_cave_collapses'}, '[img=entity.assembler-wreck]')
local global_to_show = global.config.score.global_to_show
global_to_show[#global_to_show + 1] = cave_collapses_name
config = cfg
support_beam_entities = config.support_beam_entities
@ -361,8 +365,6 @@ function DiggyCaveCollapse.register(cfg)
support_beam_entities['refined-hazard-concrete-right'] = nil
end
ScoreTable.reset('Cave collapse')
Event.add(DiggyCaveCollapse.events.on_collapse_triggered, on_collapse_triggered)
Event.add(defines.events.on_robot_built_entity, on_built_entity)
Event.add(

View File

@ -7,7 +7,7 @@
local Event = require 'utils.event'
local Global = require 'utils.global'
local Template = require 'map_gen.maps.diggy.template'
local ScoreTable = require 'map_gen.maps.diggy.score_table'
local ScoreTracker = require 'utils.score_tracker'
local Command = require 'utils.command'
local CreateParticles = require 'features.create_particles'
local Ranks = require 'resources.ranks'
@ -17,8 +17,8 @@ local pairs = pairs
local is_diggy_rock = Template.is_diggy_rock
local destroy_rock = CreateParticles.destroy_rock
local mine_rock = CreateParticles.mine_rock
local increment_score = ScoreTable.increment
local raise_event = script.raise_event
local mine_size_name = 'mine-size'
-- this
local DiggyHole = {}
@ -51,8 +51,6 @@ local function update_robot_mining_damage()
-- add the new active modifier to the non-buffed modifier
robot_mining.damage = old_modifier + robot_mining.active_modifier
ScoreTable.set('Robot mining damage', robot_mining.damage)
end
---Triggers a diggy diggy hole for a given sand-rock-big, rock-big or rock-huge.
@ -152,10 +150,13 @@ end)
Registers all event handlers.
]]
function DiggyHole.register(cfg)
ScoreTracker.register(mine_size_name, {'diggy.score_mine_size'}, '[img=tile.out-of-map]')
local global_to_show = global.config.score.global_to_show
global_to_show[#global_to_show + 1] = mine_size_name
config = cfg
robot_mining.damage = cfg.robot_initial_mining_damage
ScoreTable.set('Robot mining damage', robot_mining.damage)
ScoreTable.reset('Mine size')
Event.add(defines.events.on_entity_died, function (event)
local entity = event.entity
@ -239,7 +240,7 @@ function DiggyHole.register(cfg)
end)
Event.add(Template.events.on_void_removed, function ()
increment_score('Mine size')
ScoreTracker.change_for_global(mine_size_name, 1)
end)
local robot_damage_per_mining_prod_level = cfg.robot_damage_per_mining_prod_level

View File

@ -4,12 +4,11 @@ local Game = require 'utils.game'
local Global = require 'utils.global'
local Toast = require 'features.gui.toast'
local ForceControl = require 'features.force_control'
local ScoreTable = require 'map_gen.maps.diggy.score_table'
local ScoreTracker = require 'utils.score_tracker'
local Retailer = require 'features.retailer'
local Gui = require 'utils.gui'
local Utils = require 'utils.core'
local Color = require 'resources.color_presets'
local floor = math.floor
local log = math.log
local max = math.max
@ -23,6 +22,7 @@ local get_force_data = ForceControl.get_force_data
local set_item = Retailer.set_item
local disable_item = Retailer.disable_item
local enable_item = Retailer.enable_item
local experience_lost_name = 'experience-lost'
-- this
local Experience = {}
@ -336,7 +336,7 @@ local function on_player_respawned(event)
for _, p in pairs(game.connected_players) do
print_player_floating_text_position(p.index, text, lose_xp_color, -1, -0.5)
end
ScoreTable.add('Experience lost', exp)
ScoreTracker.change_for_global(experience_lost_name, exp)
end
local function redraw_title(data)
@ -580,9 +580,12 @@ local function update_gui()
end
function Experience.register(cfg)
config = cfg
ScoreTracker.register(experience_lost_name, {'diggy.score_experience_lost'}, '[img=recipe.artillery-targeting-remote]')
ScoreTable.reset('Experience lost')
local global_to_show = global.config.score.global_to_show
global_to_show[#global_to_show + 1] = experience_lost_name
config = cfg
--Adds the function on how to calculate level caps (When to level up)
local ForceControlBuilder = ForceControl.register(level_up_formula)

View File

@ -1,58 +0,0 @@
-- dependencies
local Global = require 'utils.global'
-- this
local ScoreTable = {}
local scores = {}
Global.register({
scores = scores,
}, function (tbl)
scores = tbl.scores
end)
---Resets the score 0 for the given name
---@param name string
function ScoreTable.reset(name)
scores[name] = 0
end
---Adds score.
---@param name string
---@param value number the sum for the score added by name
---@return number the sum for the score added by the name
function ScoreTable.add(name, value)
local new = (scores[name] or 0) + value
scores[name] = new
return new
end
---Sets score.
---@param name string
---@param value number the sum for the score added by name
function ScoreTable.set(name, value)
scores[name] = value
end
---Increments the score by 1 for name.
---@param name string
---@return number the sum for the score incremented by name
function ScoreTable.increment(name)
return ScoreTable.add(name, 1)
end
---Returns the score for a single key.
---@param name string
---@return number the sum for the score by name
function ScoreTable.get(name)
return scores[name] or 0
end
---Returns all scores.
---@return table {[string] = int}
function ScoreTable.all()
return scores
end
return ScoreTable