1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-18 03:21:47 +02:00

Merge pull request #461 from iltar/market-retailer

Added a Market Retailer to manage the market contents of market groups
This commit is contained in:
Lynn 2018-11-26 21:36:31 +01:00 committed by GitHub
commit e3ad0a4018
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 428 additions and 565 deletions

View File

@ -74,6 +74,6 @@ your own resources (for example bobs or angels) with a value and the scenario wi
spawn chances. spawn chances.
### Adding Market Items ### Adding Market Items
Items can be configured by adding the desired item under the `MarketExchange` configuration. You only have to define a Items can be configured by adding the desired item under the `Experience` configuration. You only have to define a level
level at which it unlocks, a price or prices in case it can cost more, and what the item prototype is. For a list of at which it unlocks and a price. For a list of items, you can look up each possible item on the
items, you can look up each possible item on the [Factorio raw data page](https://wiki.factorio.com/Data.raw#item). [Factorio raw data page](https://wiki.factorio.com/Data.raw#item).

View File

@ -4,6 +4,8 @@ local Event = require 'utils.event'
local raise_event = script.raise_event local raise_event = script.raise_event
local ceil = math.ceil local ceil = math.ceil
local max = math.max local max = math.max
local floor = math.floor
local format = string.format
-- this, things that can be done run-time -- this, things that can be done run-time
local ForceControl = {} local ForceControl = {}
@ -323,16 +325,14 @@ function ForceControl.get_formatted_force_data(lua_force_or_name)
return return
end end
local result = return format(
string.format(
'Current experience: %d Total experience: %d Current level: %d Next level at: %d Percentage to level up: %d%%', 'Current experience: %d Total experience: %d Current level: %d Next level at: %d Percentage to level up: %d%%',
force_config.current_experience, force_config.current_experience,
force_config.total_experience, force_config.total_experience,
force_config.current_level, force_config.current_level,
force_config.experience_level_up_cap, force_config.experience_level_up_cap,
math.floor(force_config.experience_percentage * 100) / 100 floor(force_config.experience_percentage * 100) * 0.01
) )
return result
end end
return ForceControl return ForceControl

104
features/retailer.lua Normal file
View File

@ -0,0 +1,104 @@
local Global = require 'utils.global'
local insert = table.insert
local Retailer = {}
---Global storage
---Markets are indexed by the group_name and is a sequential list of LuaEntities
---Items are indexed by the group name and is a list indexed by the item name and contains the prices per item
local memory = {
markets = {},
items = {},
}
Global.register({
memory = memory,
}, function (tbl)
memory = tbl.memory
end)
---Add a market to the group_name retailer.
---@param group_name string
---@param market_entity LuaEntity
function Retailer.add_market(group_name, market_entity)
if not memory.markets[group_name] then
memory.markets[group_name] = {market_entity}
return
end
insert(memory.markets[group_name], market_entity)
end
---Sets an item for all the group_name markets.
---@param group_name string
---@param item_name string
---@param prices table associative table where the key is the currency item and the value the amount of it
function Retailer.set_item(group_name, item_name, prices)
if not memory.items[group_name] then
memory.items[group_name] = {}
end
local market_format_prices = {}
for currency, amount in pairs(prices) do
insert(market_format_prices, {currency, amount})
end
memory.items[group_name][item_name] = market_format_prices
end
---Returns all item for the group_name retailer.
---@param group_name string
function Retailer.get_items(group_name)
return memory.items[group_name] or {}
end
---Removes an item from the markets for the group_name retailer.
---@param group_name string
---@param item_name string
function Retailer.remove_item(group_name, item_name)
if not memory.items[group_name] then
return
end
memory.items[group_name][item_name] = nil
end
---Ships the current list of items with their prices to all markets for the group_name retailer.
---@param group_name string
function Retailer.ship_items(group_name)
local markets = memory.markets[group_name]
if not markets then
return
end
local market_items = memory.items[group_name]
if not market_items then
-- items have not been added yet
return
end
for _, market in ipairs(markets) do
if market.valid then
-- clean the current inventory
local remove_market_item = market.remove_market_item
-- remove re-indexes the offers, to prevent shifting, go backwards
local current_market_items = market.get_market_items()
if current_market_items then
for current_index = #current_market_items, 1, -1 do
remove_market_item(current_index)
end
end
-- re-add the whole list
local add_market_item = market.add_market_item
for item_name, prices in pairs(market_items) do
add_market_item({
price = prices,
offer = {type = 'give-item', item = item_name, count = 1}
})
end
end
end
end
return Retailer

View File

@ -11,6 +11,9 @@ local Config = {
-- initial starting position size, higher values are not recommended -- initial starting position size, higher values are not recommended
starting_size = 8, starting_size = 8,
-- where the market should spawn
market_spawn_position = {x = 0, y = 3},
}, },
-- controls the Daylight (Default diggy: enabled = true) -- controls the Daylight (Default diggy: enabled = true)
@ -34,7 +37,7 @@ local Config = {
manual_mining_speed_modifier = 1000, manual_mining_speed_modifier = 1000,
-- increase the amount of inventory slots for the player force -- increase the amount of inventory slots for the player force
character_inventory_slots_bonus = 1000, character_inventory_slots_bonus = 0,
-- increases the run speed of all characters for the player force -- increases the run speed of all characters for the player force
character_running_speed_modifier = 2, character_running_speed_modifier = 2,
@ -43,7 +46,7 @@ local Config = {
character_health_bonus = 1000000, character_health_bonus = 1000000,
-- unlock all research by default, only useful when testing -- unlock all research by default, only useful when testing
unlock_all_research = false, unlock_all_research = true,
-- adds additional items to the player force when starting in addition to defined in start_items above -- adds additional items to the player force when starting in addition to defined in start_items above
starting_items = { starting_items = {
@ -338,58 +341,6 @@ local Config = {
}, },
}, },
-- controls the market and buffs
MarketExchange = {
enabled = true,
-- percentage * mining productivity level gets added to mining speed
mining_speed_productivity_multiplier = 5,
-- market config
market_spawn_position = {x = 0, y = 3},
currency_item = 'coin',
-- add or remove a table entry to add or remove a unlockable item from the mall.
-- format: {unlock_at_level, price, prototype_name},
-- alternative format: {level = 32, price = {{"stone", 2500}, {"coin", 100}}, name = 'power-armor'},
unlockables = require('map_gen.Diggy.FormatMarketItems').initialize_unlockables(
{
{level = 1, price = 5, name = 'iron-axe'},
{level = 2, price = 5, name = 'raw-wood'},
{level = 3, price = 20, name = 'pistol'},
{level = 3, price = 2, name = 'firearm-magazine'},
{level = 5, price = 2, name = 'stone-brick'},
{level = 6, price = 6, name = 'small-lamp'},
{level = 6, price = 5, name = 'raw-fish'},
{level = 8, price = 10, name = 'stone-wall'},
{level = 10, price = 85, name = 'shotgun'},
{level = 10, price = 2, name = 'shotgun-shell'},
{level = 13, price = 25, name = 'steel-axe'},
{level = 13, price = 50, name = 'light-armor'},
{level = 15, price = 85, name = 'submachine-gun'},
{level = 18, price = 8, name = 'piercing-rounds-magazine'},
{level = 18, price = 8, name = 'piercing-shotgun-shell'},
{level = 20, price = 100, name = 'heavy-armor'},
{level = 25, price = 250, name = 'modular-armor'},
{level = 25, price = 100, name = 'landfill'},
{level = 28, price = 250, name = 'personal-roboport-equipment'},
{level = 28, price = 75, name = 'construction-robot'},
{level = 32, price = 850, name = 'power-armor'},
{level = 34, price = 100, name = 'battery-equipment'},
{level = 33, price = 1000, name = 'fusion-reactor-equipment'},
{level = 36, price = 150, name = 'energy-shield-equipment'},
{level = 42, price = 650, name = 'combat-shotgun'},
{level = 46, price = 25, name = 'uranium-rounds-magazine'},
{level = 58, price = 250, name = 'rocket-launcher'},
{level = 58, price = 40, name = 'rocket'},
{level = 66, price = 80, name = 'explosive-rocket'},
{level = 73, price = 2000, name = 'satellite'},
{level = 100, price = 1, name = 'iron-stick'},
}
),
},
--Tracks players causing collapses --Tracks players causing collapses
Antigrief = { Antigrief = {
enabled = true, enabled = true,
@ -423,10 +374,47 @@ local Config = {
['death-penalty'] = 0.002, -- XP deduct in percentage of total experience when a player dies (Diggy default: 0.002 which equals 0.2%) ['death-penalty'] = 0.002, -- XP deduct in percentage of total experience when a player dies (Diggy default: 0.002 which equals 0.2%)
}, },
buffs = { --Define new buffs here, they are handed out for each level. buffs = {
['mining_speed'] = {value = 5}, -- define new buffs here, they are handed out for each level
['inventory_slot'] = {value = 1}, mining_speed = {value = 5},
['health_bonus'] = {value = 2.5, double_level = 5}, -- double_level is the level interval for receiving a double bonus (Diggy default: 5 which equals every 5th level) inventory_slot = {value = 1},
-- double_level is the level interval for receiving a double bonus (Diggy default: 5 which equals every 5th level)
health_bonus = {value = 2.5, double_level = 5},
},
-- add or remove a table entry to add or remove a unlockable item from the market.
unlockables = {
{level = 1, price = 5, name = 'iron-axe'},
{level = 2, price = 5, name = 'raw-wood'},
{level = 3, price = 20, name = 'pistol'},
{level = 3, price = 2, name = 'firearm-magazine'},
{level = 5, price = 2, name = 'stone-brick'},
{level = 6, price = 6, name = 'small-lamp'},
{level = 6, price = 5, name = 'raw-fish'},
{level = 8, price = 10, name = 'stone-wall'},
{level = 10, price = 85, name = 'shotgun'},
{level = 10, price = 2, name = 'shotgun-shell'},
{level = 13, price = 25, name = 'steel-axe'},
{level = 13, price = 50, name = 'light-armor'},
{level = 15, price = 85, name = 'submachine-gun'},
{level = 18, price = 8, name = 'piercing-rounds-magazine'},
{level = 18, price = 8, name = 'piercing-shotgun-shell'},
{level = 20, price = 100, name = 'heavy-armor'},
{level = 25, price = 250, name = 'modular-armor'},
{level = 25, price = 100, name = 'landfill'},
{level = 28, price = 250, name = 'personal-roboport-equipment'},
{level = 28, price = 75, name = 'construction-robot'},
{level = 32, price = 850, name = 'power-armor'},
{level = 34, price = 100, name = 'battery-equipment'},
{level = 33, price = 1000, name = 'fusion-reactor-equipment'},
{level = 36, price = 150, name = 'energy-shield-equipment'},
{level = 42, price = 650, name = 'combat-shotgun'},
{level = 46, price = 25, name = 'uranium-rounds-magazine'},
{level = 58, price = 250, name = 'rocket-launcher'},
{level = 58, price = 40, name = 'rocket'},
{level = 66, price = 80, name = 'explosive-rocket'},
{level = 73, price = 2000, name = 'satellite'},
{level = 100, price = 1, name = 'iron-stick'},
}, },
}, },

View File

@ -4,9 +4,18 @@ local Game = require 'utils.game'
local Global = require 'utils.global' local Global = require 'utils.global'
local ForceControl = require 'features.force_control' local ForceControl = require 'features.force_control'
local Debug = require 'map_gen.Diggy.Debug' local Debug = require 'map_gen.Diggy.Debug'
local Retailer = require 'features.retailer'
local Gui = require 'utils.gui'
local force_control = require 'features.force_control'
local utils = require 'utils.utils'
local format = string.format
local string_format = string.format
local floor = math.floor
local log = math.log
local insert = table.insert
-- Will be registered in Experience.register -- hack
local ForceControl_builder = {} local alien_coin_modifiers = require 'map_gen.Diggy.Config'.features.ArtefactHunting.alien_coin_modifiers
-- this -- this
local Experience = {} local Experience = {}
@ -39,15 +48,14 @@ Global.register({
health_bonus = tbl.health_bonus health_bonus = tbl.health_bonus
end) end)
local config = {} local config = {}
local string_format = string.format
local alien_coin_modifiers = require 'map_gen.Diggy.Config'.features.ArtefactHunting.alien_coin_modifiers local gain_xp_color = {r = 144, g = 202, b = 249}
local floor = math.floor local lose_xp_color = {r = 255, g = 0, b = 0}
local unlocked_color = {r = 255, g = 255, b = 255}
local locked_color = {r = 127, g = 127, b = 127}
local level_up_formula = (function (level_reached) local level_up_formula = (function (level_reached)
local floor = math.floor
local log = math.log
local Config = require 'map_gen.Diggy.Config'.features.Experience local Config = require 'map_gen.Diggy.Config'.features.Experience
local difficulty_scale = floor(Config.difficulty_scale) local difficulty_scale = floor(Config.difficulty_scale)
local level_fine_tune = floor(Config.xp_fine_tune) local level_fine_tune = floor(Config.xp_fine_tune)
@ -72,6 +80,20 @@ local level_up_formula = (function (level_reached)
return value - lower_value return value - lower_value
end) end)
---Updates the market contents based on the current level.
---@param force LuaForce the force which the unlocking requirement should be based of
function Experience.update_market_contents(force)
local current_level = ForceControl.get_force_data(force).current_level
local force_name = force.name
for _, prototype in ipairs(config.unlockables) do
if (current_level >= prototype.level) then
Retailer.set_item(force_name, prototype.name, {coin = prototype.price})
end
end
Retailer.ship_items(force_name)
end
---Updates a forces manual mining speed modifier. By removing active modifiers and re-adding ---Updates a forces manual mining speed modifier. By removing active modifiers and re-adding
---@param force LuaForce the force of which will be updated ---@param force LuaForce the force of which will be updated
---@param level_up number a level if updating as part of a level up (optional) ---@param level_up number a level if updating as part of a level up (optional)
@ -156,7 +178,7 @@ local function on_player_mined_entity(event)
return return
end end
local text = string_format('+%d XP', exp) local text = string_format('+%d XP', exp)
Game.print_player_floating_text_position(player_index, text, {r = 144, g = 202, b = 249},0, -0.5) Game.print_player_floating_text_position(player_index, text, gain_xp_color,0, -0.5)
ForceControl.add_experience(force, exp) ForceControl.add_experience(force, exp)
end end
@ -176,7 +198,7 @@ local function on_research_finished(event)
local text = string_format('Research completed! +%d XP', exp) local text = string_format('Research completed! +%d XP', exp)
for _, p in pairs(game.connected_players) do for _, p in pairs(game.connected_players) do
local player_index = p.index local player_index = p.index
Game.print_player_floating_text_position(player_index, text, {r = 144, g = 202, b = 249}, -1, -0.5) Game.print_player_floating_text_position(player_index, text, gain_xp_color, -1, -0.5)
end end
ForceControl.add_experience(force, exp) ForceControl.add_experience(force, exp)
@ -206,7 +228,7 @@ local function on_rocket_launched(event)
local text = string_format('Rocket launched! +%d XP', exp) local text = string_format('Rocket launched! +%d XP', exp)
for _, p in pairs(game.connected_players) do for _, p in pairs(game.connected_players) do
local player_index = p.index local player_index = p.index
Game.print_player_floating_text_position(player_index, text, {r = 144, g = 202, b = 249},-1, -0.5) Game.print_player_floating_text_position(player_index, text, gain_xp_color,-1, -0.5)
end end
end end
@ -224,7 +246,7 @@ local function on_entity_died (event)
if cause and (cause.name == 'artillery-turret' or cause.name == 'gun-turret' or cause.name == 'laser-turret' or cause.name == 'flamethrower-turret') then if cause and (cause.name == 'artillery-turret' or cause.name == 'gun-turret' or cause.name == 'laser-turret' or cause.name == 'flamethrower-turret') then
exp = config.XP['enemy_killed'] * alien_coin_modifiers[entity.name] exp = config.XP['enemy_killed'] * alien_coin_modifiers[entity.name]
local text = string_format('+ %d XP', exp) local text = string_format('+ %d XP', exp)
Game.print_floating_text(cause.surface, cause.position, text, {r = 144, g = 202, b = 249}) Game.print_floating_text(cause.surface, cause.position, text, gain_xp_color)
ForceControl.add_experience(force, exp) ForceControl.add_experience(force, exp)
return return
else else
@ -238,9 +260,11 @@ local function on_entity_died (event)
end end
end end
end end
if exp then
local text = string_format('+ %d XP', exp) local text = string_format('+ %d XP', exp)
Game.print_floating_text(entity.surface, entity.position, text, {r = 144, g = 202, b = 249}) Game.print_floating_text(entity.surface, entity.position, text, gain_xp_color)
ForceControl.add_experience(force, exp) ForceControl.add_experience(force, exp)
end
return return
end end
@ -250,7 +274,7 @@ local function on_entity_died (event)
local exp = config.XP['enemy_killed'] * alien_coin_modifiers[entity.name] local exp = config.XP['enemy_killed'] * alien_coin_modifiers[entity.name]
local text = string_format('+ %d XP', exp) local text = string_format('+ %d XP', exp)
local player_index = cause.player.index local player_index = cause.player.index
Game.print_player_floating_text_position(player_index, text, {r = 144, g = 202, b = 249},-1, -0.5) Game.print_player_floating_text_position(player_index, text, gain_xp_color,-1, -0.5)
ForceControl.add_experience(force, exp) ForceControl.add_experience(force, exp)
end end
@ -262,53 +286,232 @@ local function on_player_respawned(event)
local exp = ForceControl.remove_experience_percentage(force, config.XP['death-penalty'], 50) local exp = ForceControl.remove_experience_percentage(force, config.XP['death-penalty'], 50)
local text = string_format('%s resurrected! -%d XP', player.name, exp) local text = string_format('%s resurrected! -%d XP', player.name, exp)
for _, p in pairs(game.connected_players) do for _, p in pairs(game.connected_players) do
Game.print_player_floating_text_position(p.index, text, {r = 255, g = 0, b = 0},-1, -0.5) Game.print_player_floating_text_position(p.index, text, lose_xp_color, -1, -0.5)
end end
end end
---Get list of defined buffs
---@return table with the same format as in the Diggy Config
---@see Diggy.Config.features.Experience.Buffs
function Experience.get_buffs()
return config.buffs
end
local level_table = {} local level_table = {}
---Get experiment requirement for a given level ---Get experiment requirement for a given level
---Primarily used for the market GUI to display total experience required to unlock a specific item ---Primarily used for the Experience GUI to display total experience required to unlock a specific item
---@param level number a number specifying the level ---@param level number a number specifying the level
---@return number required total experience to reach supplied level ---@return number required total experience to reach supplied level
function Experience.calculate_level_xp(level) local function calculate_level_xp(level)
if level_table[level] == nil then if level_table[level] == nil then
local value local value
if level == 1 then if level == 1 then
value = level_up_formula(level-1) value = level_up_formula(level-1)
else else
value = level_up_formula(level-1)+Experience.calculate_level_xp(level-1) value = level_up_formula(level-1)+calculate_level_xp(level-1)
end end
table.insert(level_table, level, value) insert(level_table, level, value)
end end
return level_table[level] return level_table[level]
end end
local function redraw_title(data)
local force_data = force_control.get_force_data('player')
data.frame.caption = utils.comma_value(force_data.total_experience) .. ' total experience earned!'
end
local function apply_heading_style(style, width)
style.font = 'default-bold'
style.width = width
end
local function redraw_heading(data, header)
local head_condition = (header == 1)
local frame = (head_condition) and data.experience_list_heading or data.buff_list_heading
local header_caption = (head_condition) and 'Reward Item' or 'Reward Buff'
Gui.clear(frame)
local heading_table = frame.add({type = 'table', column_count = 2})
apply_heading_style(heading_table.add({ type = 'label', caption = 'Requirement'}).style, 100)
apply_heading_style(heading_table.add({type = 'label', caption = header_caption}).style, 220)
end
local function redraw_progressbar(data)
local force_data = force_control.get_force_data('player')
local flow = data.experience_progressbars
Gui.clear(flow)
apply_heading_style(flow.add({type = 'label', tooltip = 'Currently at level: ' .. force_data.current_level .. '\nNext level at: ' .. utils.comma_value((force_data.total_experience - force_data.current_experience) + force_data.experience_level_up_cap) ..' xp\nRemaining xp: ' .. utils.comma_value(force_data.experience_level_up_cap - force_data.current_experience), name = 'Diggy.Experience.Frame.Progress.Level', caption = 'Progress to next level:'}).style)
local level_progressbar = flow.add({type = 'progressbar', tooltip = floor(force_data.experience_percentage*100)*0.01 .. '% xp to next level'})
level_progressbar.style.width = 350
level_progressbar.value = force_data.experience_percentage * 0.01
end
local function redraw_table(data)
local experience_scroll_pane = data.experience_scroll_pane
Gui.clear(experience_scroll_pane)
redraw_progressbar(data)
redraw_heading(data, 1)
local last_level = 0
local current_force_level = force_control.get_force_data('player').current_level
for _, prototype in ipairs(config.unlockables) do
local current_item_level = prototype.level
local first_item_for_level = current_item_level ~= last_level
local color
if current_force_level >= current_item_level then
color = unlocked_color
else
color = locked_color
end
local list = experience_scroll_pane.add({type = 'table', column_count = 2})
local level_caption = ''
if first_item_for_level then
level_caption = 'level ' .. current_item_level
end
local level_column = list.add({
type = 'label',
caption = level_caption,
tooltip = 'XP: ' .. utils.comma_value(calculate_level_xp(current_item_level)),
})
level_column.style.minimal_width = 100
level_column.style.font_color = color
local item_column = list.add({
type = 'label',
caption = prototype.name
})
item_column.style.minimal_width = 22
item_column.style.font_color = color
last_level = current_item_level
end
end
local function redraw_buff(data)
local buff_scroll_pane = data.buff_scroll_pane
Gui.clear(buff_scroll_pane)
local all_levels_shown = false
for name, effects in pairs(config.buffs) do
local list = buff_scroll_pane.add({type = 'table', column_count = 2})
list.style.horizontal_spacing = 16
local level_caption = ''
if not all_levels_shown then
all_levels_shown = true
level_caption = 'All levels'
end
local level_label = list.add({type = 'label', caption = level_caption})
level_label.style.minimal_width = 100
level_label.style.font_color = unlocked_color
local buff_caption
local effect_value = effects.value
if name == 'mining_speed' then
buff_caption = format('+ %d mining speed', effect_value)
elseif name == 'inventory_slot' then
buff_caption = format('+ %d inventory slot%s', effect_value, effect_value > 1 and 's' or '')
elseif name == 'health_bonus' then
buff_caption = format('+ %d max health', effect_value)
else
buff_caption = format('+ %d %s', effect_value, name)
end
local buffs_label = list.add({ type = 'label', caption = buff_caption})
buffs_label.style.minimal_width = 220
buffs_label.style.font_color = unlocked_color
end
end
local function toggle(event)
local player = event.player
local left = player.gui.left
local frame = left['Diggy.Experience.Frame']
if (frame and event.trigger == nil) then
Gui.destroy(frame)
return
elseif (frame) then
local data = Gui.get_data(frame)
redraw_title(data)
redraw_progressbar(data)
redraw_table(data)
return
end
frame = left.add({name = 'Diggy.Experience.Frame', type = 'frame', direction = 'vertical'})
local experience_progressbars = frame.add({ type = 'flow', direction = 'vertical'})
local experience_list_heading = frame.add({type = 'flow', direction = 'horizontal'})
local experience_scroll_pane = frame.add({ type = 'scroll-pane'})
experience_scroll_pane.style.maximal_height = 300
local buff_list_heading = frame.add({type = 'flow', direction = 'horizontal'})
local buff_scroll_pane = frame.add({type = 'scroll-pane'})
buff_scroll_pane.style.maximal_height = 100
frame.add({type = 'button', name = 'Diggy.Experience.Button', caption = 'Close'})
local data = {
frame = frame,
experience_progressbars = experience_progressbars,
experience_list_heading = experience_list_heading,
experience_scroll_pane = experience_scroll_pane,
buff_list_heading = buff_list_heading,
buff_scroll_pane = buff_scroll_pane,
}
redraw_title(data)
redraw_table(data)
redraw_heading(data, 2)
redraw_buff(data)
Gui.set_data(frame, data)
end
local function on_player_created(event)
Game.get_player_by_index(event.player_index).gui.top.add({
name = 'Diggy.Experience.Button',
type = 'sprite-button',
sprite = 'entity/market',
})
end
Gui.on_click('Diggy.Experience.Button', toggle)
Gui.on_custom_close('Diggy.Experience.Frame', function (event)
event.element.destroy()
end)
---Updates the experience progress gui for every player that has it open
local function update_gui()
for _, p in ipairs(game.connected_players) do
local frame = p.gui.left['Diggy.Experience.Frame']
if frame and frame.valid then
local data = {player = p, trigger = 'update_gui'}
toggle(data)
end
end
end
function Experience.register(cfg) function Experience.register(cfg)
config = cfg config = cfg
--Adds the function on how to calculate level caps (When to level up) --Adds the function on how to calculate level caps (When to level up)
ForceControl_builder = ForceControl.register(level_up_formula) local ForceControlBuilder = ForceControl.register(level_up_formula)
--Adds a function that'll be executed at every level up --Adds a function that'll be executed at every level up
ForceControl_builder.register_on_every_level(function (level_reached, force) ForceControlBuilder.register_on_every_level(function (level_reached, force)
force.print(string_format('%s Leveled up to %d!', '## - ', level_reached)) force.print(string_format('%s Leveled up to %d!', '## - ', level_reached))
force.play_sound{path='utility/new_objective', volume_modifier = 1 } force.play_sound{path='utility/new_objective', volume_modifier = 1 }
local Experience = require 'map_gen.Diggy.Feature.Experience' local Experience = require 'map_gen.Diggy.Feature.Experience'
Experience.update_inventory_slots(force, level_reached) Experience.update_inventory_slots(force, level_reached)
Experience.update_mining_speed(force, level_reached) Experience.update_mining_speed(force, level_reached)
Experience.update_health_bonus(force, level_reached) Experience.update_health_bonus(force, level_reached)
local MarketExchange = require 'map_gen.Diggy.Feature.MarketExchange' Experience.update_market_contents(force)
local market = MarketExchange.get_market()
MarketExchange.update_market_contents(market, force)
MarketExchange.update_gui()
end) end)
-- Events -- Events
@ -317,6 +520,8 @@ function Experience.register(cfg)
Event.add(defines.events.on_rocket_launched, on_rocket_launched) Event.add(defines.events.on_rocket_launched, on_rocket_launched)
Event.add(defines.events.on_player_respawned, on_player_respawned) Event.add(defines.events.on_player_respawned, on_player_respawned)
Event.add(defines.events.on_entity_died, on_entity_died) Event.add(defines.events.on_entity_died, on_entity_died)
Event.add(defines.events.on_player_created, on_player_created)
Event.on_nth_tick(61, update_gui)
-- Prevents table lookup thousands of times -- Prevents table lookup thousands of times
sand_rock_xp = config.XP['sand-rock-big'] sand_rock_xp = config.XP['sand-rock-big']

View File

@ -1,386 +0,0 @@
--[[-- info
Provides the ability to purchase items from the market.
]]
-- dependencies
local Event = require 'utils.event'
local Token = require 'utils.global_token'
local Task = require 'utils.Task'
local Gui = require 'utils.gui'
local Debug = require 'map_gen.Diggy.Debug'
local Template = require 'map_gen.Diggy.Template'
local Game = require 'utils.game'
local insert = table.insert
local force_control = require 'features.force_control'
local Experience = require 'map_gen.Diggy.Feature.Experience'
local max = math.max
local floor = math.floor
local utils = require 'utils.utils'
-- this
local MarketExchange = {}
local config = {}
local on_market_timeout_finished = Token.register(function(params)
Template.market(params.surface, params.position, params.player_force)
end)
---Updates market content with new items if they are to be unlocked
---Primarily used by the force control system at every level up
---@param market LuaEntity The market to be updated
---@param force LuaForce the force which the unlocking requirement should be based of
function MarketExchange.update_market_contents(market, force)
local add_market_item
local item_unlocked = false
for _, unlockable in pairs(config.unlockables) do
local is_in_range = force_control.get_force_data(force).current_level == unlockable.level
-- only add the item to the market if it's between the old and new stone range
if (is_in_range and unlockable.type == 'market') then
add_market_item = add_market_item or market.add_market_item
local name = unlockable.prototype.name
local price = unlockable.prototype.price
if type(price) == 'number' then
add_market_item({
price = {{config.currency_item, price}},
offer = {type = 'give-item', item = name, count = 1}
})
elseif type(price) == 'table' then
add_market_item({
price = price,
offer = {type = 'give-item', item = name, count = 1}
})
end
item_unlocked = true
end
end
end
local function redraw_title(data)
local force_data = force_control.get_force_data('player')
data.frame.caption = utils.comma_value(force_data.total_experience) .. ' total experience earned!'
end
local function get_data(unlocks, stone, type)
local result = {}
for _, data in pairs(unlocks) do
if data.level == stone and data.type == type then
insert(result, data)
end
end
return result
end
local tag_label_stone = Gui.uid_name()
local tag_label_buff = Gui.uid_name()
local tag_label_item = Gui.uid_name()
local function apply_heading_style(style, width)
style.font = 'default-bold'
style.width = width
end
local function redraw_heading(data, header)
local head_condition = (header == 1)
local frame = (head_condition) and data.market_list_heading or data.buff_list_heading
local header_caption = (head_condition) and 'Reward Item' or 'Reward Buff'
Gui.clear(frame)
local heading_table = frame.add({type = 'table', column_count = 2})
apply_heading_style(heading_table.add({type = 'label', name = tag_label_stone, caption = 'Requirement'}).style, 100)
apply_heading_style(heading_table.add({type = 'label', name = tag_label_buff, caption = header_caption}).style, 220)
end
local function redraw_progressbar(data)
local force_data = force_control.get_force_data('player')
local flow = data.market_progressbars
Gui.clear(flow)
apply_heading_style(flow.add({type = 'label', tooltip = 'Currently at level: ' .. force_data.current_level .. '\nNext level at: ' .. utils.comma_value((force_data.total_experience - force_data.current_experience) + force_data.experience_level_up_cap) ..' xp\nRemaining xp: ' .. utils.comma_value(force_data.experience_level_up_cap - force_data.current_experience), name = 'Diggy.MarketExchange.Frame.Progress.Level', caption = 'Progress to next level:'}).style)
local level_progressbar = flow.add({type = 'progressbar', tooltip = floor(force_data.experience_percentage*100)*0.01 .. '% xp to next level'})
level_progressbar.style.width = 350
level_progressbar.value = force_data.experience_percentage * 0.01
end
local function redraw_table(data)
local market_scroll_pane = data.market_scroll_pane
Gui.clear(market_scroll_pane)
local buffs = {}
local items = {}
local last_stone = 0
local number_of_rows = 0
local row = {}
-- create the progress bars in the window
redraw_progressbar(data)
-- create table headings
redraw_heading(data, 1)
-- create table
for i = 1, #config.unlockables do
if config.unlockables[i].level ~= last_stone then
-- get items and buffs for each stone value
items = get_data(config.unlockables, config.unlockables[i].level, 'market')
-- get number of rows
number_of_rows = max(#buffs, #items)
-- loop through buffs and items for number of rows
for j = 1, number_of_rows do
local result = {}
local item = items[j]
local level = item.level
-- 1st column
result[1] = level
-- 3rd column
if items[j] ~= nil then
result[3] = '+ ' .. item.prototype.name
else
result[3] = ''
end
-- indicator to stop print stone number
if j > 1 then
result[4] = true
else
result[4] = false
end
-- indicator to draw horizontal line
if j == number_of_rows then
result[5] = true
else
result[5] = false
end
insert(row, result)
end
end
-- save lastStone
last_stone = config.unlockables[i].level
end
-- print table
for _, unlockable in pairs(row) do
local is_unlocked = unlockable[1] <= force_control.get_force_data('player').current_level
local list = market_scroll_pane.add {type = 'table', column_count = 2 }
list.style.horizontal_spacing = 16
local caption = ''
if unlockable[4] ~= true then
caption = 'Level ' .. unlockable[1]
else
caption = ''
end
local tag_stone = list.add {type = 'label', name = tag_label_stone, caption = caption, tooltip = 'XP: ' .. utils.comma_value(Experience.calculate_level_xp(unlockable[1]))}
tag_stone.style.minimal_width = 100
local tag_items = list.add {type = 'label', name = tag_label_item, caption = unlockable[3]}
tag_items.style.minimal_width = 220
-- draw horizontal line
if unlockable[5] == true then
list.draw_horizontal_line_after_headers = true
end
if (is_unlocked) then
tag_stone.style.font_color = {r = 1, g = 1, b = 1 }
tag_items.style.font_color = {r = 1, g = 1, b = 1 }
else
tag_stone.style.font_color = {r = 0.5, g = 0.5, b = 0.5 }
tag_items.style.font_color = {r = 0.5, g = 0.5, b = 0.5 }
end
end
end
local function redraw_buff(data) --! Almost equals to the redraw_table() function !
local buff_scroll_pane = data.buff_scroll_pane
Gui.clear(buff_scroll_pane)
local buffs = Experience.get_buffs()
local row = {}
local i = 0
for k, v in pairs(buffs) do
i = i + 1
local result = {}
-- 1st column
result[1] = 'All levels'
-- 2nd column
if k == 'mining_speed' then
result[2] = '+ '.. v.value .. '% mining speed'
elseif k == 'inventory_slot' then
if v.value > 1 then
result[2] = '+ '.. v.value .. ' inventory slots'
else
result[2] = '+ '.. v.value .. ' inventory slot'
end
elseif k == 'health_bonus' then
result[2] = '+ '.. v.value .. ' max health'
else
result[2] = 'Description missing: unknown buff. Please contact admin'
end
-- 3rd column
result[3] = ''
-- indicator to stop print level number
if i > 1 then
result[4] = true
else
result[4] = false
end
insert(row, result)
end
for _, unlockable in pairs(row) do
local list = buff_scroll_pane.add {type = 'table', column_count = 2 }
list.style.horizontal_spacing = 16
local caption = ''
if unlockable[4] ~= true then
caption = unlockable[1]
else
caption = ''
end
local tag_stone = list.add {type = 'label', name = buff_tag_label_stone, caption = caption}
tag_stone.style.minimal_width = 100
local tag_buffs = list.add {type = 'label', name = buff_tag_label_buff, caption = unlockable[2]}
tag_buffs.style.minimal_width = 220
tag_stone.style.font_color = {r = 1, g = 1, b = 1 }
tag_buffs.style.font_color = {r = 1, g = 1, b = 1 }
end
end
---Interface for force control system to access the primary market
---@return LuaEntity the primary market (The one at spawn)
function MarketExchange.get_market()
local markets = game.surfaces.nauvis.find_entities_filtered({name = 'market', position = config.market_spawn_position, limit = 1})
if (#markets == 0) then
Debug.print_position(config.market_spawn_position, 'Unable to find a market')
return
end
return markets[1]
end
local function on_placed_entity(event)
local market = event.entity
if 'market' ~= market.name then
return
end
end
function MarketExchange.get_extra_map_info(config)
return 'Market Exchange, come make a deal at the foreman\'s shop'
end
local function toggle(event)
local player = event.player
local left = player.gui.left
local frame = left['Diggy.MarketExchange.Frame']
if (frame and event.trigger == nil) then
Gui.destroy(frame)
return
elseif (frame) then
local data = Gui.get_data(frame)
redraw_title(data)
redraw_progressbar(data)
redraw_table(data)
return
end
frame = left.add({name = 'Diggy.MarketExchange.Frame', type = 'frame', direction = 'vertical'})
local market_progressbars = frame.add({type = 'flow', direction = 'vertical'})
local market_list_heading = frame.add({type = 'flow', direction = 'horizontal'})
local market_scroll_pane = frame.add({type = 'scroll-pane'})
market_scroll_pane.style.maximal_height = 300
local buff_list_heading = frame.add({type = 'flow', direction = 'horizontal'})
local buff_scroll_pane = frame.add({type = 'scroll-pane'})
buff_scroll_pane.style.maximal_height = 100
frame.add({ type = 'button', name = 'Diggy.MarketExchange.Button', caption = 'Close'})
local data = {
frame = frame,
market_progressbars = market_progressbars,
market_list_heading = market_list_heading,
market_scroll_pane = market_scroll_pane,
buff_list_heading = buff_list_heading,
buff_scroll_pane = buff_scroll_pane,
}
redraw_title(data)
redraw_table(data)
redraw_heading(data, 2)
redraw_buff(data)
Gui.set_data(frame, data)
end
local function on_player_created(event)
Game.get_player_by_index(event.player_index).gui.top.add({
name = 'Diggy.MarketExchange.Button',
type = 'sprite-button',
sprite = 'entity/market',
})
end
Gui.on_click('Diggy.MarketExchange.Button', toggle)
Gui.on_custom_close('Diggy.MarketExchange.Frame', function (event)
event.element.destroy()
end)
---Updates the market progress gui for every player that has it open
function MarketExchange.update_gui()
for _, p in ipairs(game.connected_players) do
local frame = p.gui.left['Diggy.MarketExchange.Frame']
if frame and frame.valid then
local data = {player = p, trigger = 'update_gui'}
toggle(data)
end
end
end
function MarketExchange.on_init()
Task.set_timeout_in_ticks(1, on_market_timeout_finished, {
surface = game.surfaces.nauvis,
position = config.market_spawn_position,
player_force = game.forces.player,
})
end
--[[--
Registers all event handlers.
]]
function MarketExchange.register(cfg)
config = cfg
--Events
Event.add(Template.events.on_placed_entity, on_placed_entity)
Event.add(defines.events.on_player_created, on_player_created)
Event.on_nth_tick(61, MarketExchange.update_gui)
end
return MarketExchange

View File

@ -6,11 +6,13 @@ local Event = require 'utils.event'
local Token = require 'utils.global_token' local Token = require 'utils.global_token'
local Template = require 'map_gen.Diggy.Template' local Template = require 'map_gen.Diggy.Template'
local Debug = require 'map_gen.Diggy.Debug' local Debug = require 'map_gen.Diggy.Debug'
local Retailer = require 'features.retailer'
local DiggyCaveCollapse = require 'map_gen.Diggy.Feature.DiggyCaveCollapse' local DiggyCaveCollapse = require 'map_gen.Diggy.Feature.DiggyCaveCollapse'
local insert = table.insert local insert = table.insert
local random = math.random local random = math.random
local sqrt = math.sqrt local sqrt = math.sqrt
local floor = math.floor local floor = math.floor
local raise_event = script.raise_event
-- this -- this
local StartingZone = {} local StartingZone = {}
@ -67,7 +69,23 @@ function StartingZone.register(config)
end end
end end
Template.insert(event.surface, tiles, rocks) Template.insert(surface, tiles, rocks)
local position = config.market_spawn_position;
local player_force = game.forces.player;
local market = surface.create_entity({name = 'market', position = position})
market.destructible = false
Retailer.add_market(player_force.name, market)
Retailer.ship_items(player_force.name)
player_force.add_chart_tag(surface, {
text = 'Market',
position = position,
})
raise_event(Template.events.on_placed_entity, {entity = market})
Event.remove_removable(defines.events.on_chunk_generated, callback_token) Event.remove_removable(defines.events.on_chunk_generated, callback_token)
end end
@ -77,5 +95,4 @@ function StartingZone.register(config)
Event.add_removable(defines.events.on_chunk_generated, callback_token) Event.add_removable(defines.events.on_chunk_generated, callback_token)
end end
return StartingZone return StartingZone

View File

@ -1,45 +0,0 @@
-- dependencies
-- this
local FormatMarketItems = {}
local market_prototype_items = {}
local insert = table.insert
--- Returns the correct format for Diggy.Feature.MarketExhange.lua to process
-- @param self_level number of the level the given item should be unlocked at
-- @param self_price number of the price in the configured currency_item the given item should cost
-- @param self_name string of the factorio entity prototype-name
--
local function add(self_level, self_price, self_name)
if (not market_prototype_items[self_level]) then
insert(market_prototype_items, self_level, {})
end
insert(market_prototype_items[self_level], {price = self_price, name = self_name})
end
--- handles the unlockable market items from Config.lua in map_gen.Diggy
-- serves as a handler for an array of items and passes it on to FormatMarketItems.add() that returns the correct format for Diggy.Feature.MarketExhange.lua to process.
-- @param items table of items where each item is an table with keys: level (integer level it unlocks at), price (price in the configured currency_item) and name (has to be an entity's prototype-name)
-- @returns table of items formated in the correct way for Diggy.Feature.MarketExhange.lua to interpret.
-- @usage Pass an table with each value being another table with these members:
-- @field level number of the level wished to unlock the item
-- @field price number of the price in the configured currency_item to buy the item in the market
-- @field name string of the factorio prototype-name for the entity to be unlocked
--
function FormatMarketItems.initialize_unlockables(items)
local unlockables = {}
for _, item in ipairs(items) do
add(item.level, item.price, item.name)
end
for lvl, v in pairs(market_prototype_items) do
for _, w in ipairs(v) do
insert(unlockables, {level = lvl, type = 'market', prototype = w})
end
end
return unlockables
end
return FormatMarketItems

View File

@ -10,8 +10,8 @@ local raise_event = script.raise_event
-- this -- this
local Template = {} local Template = {}
local tiles_per_call = 5 --how many tiles are inserted with each call of insert_action local tiles_per_call = 256 --how many tiles are inserted with each call of insert_action
local entities_per_call = 5 --how many entities are inserted with each call of insert_action local entities_per_call = 8 --how many entities are inserted with each call of insert_action
Template.events = { Template.events = {
--[[-- --[[--
@ -71,7 +71,7 @@ local function insert_next_entities(data)
for i = data.entity_iterator, min(data.entity_iterator + entities_per_call - 1, data.entities_n) do for i = data.entity_iterator, min(data.entity_iterator + entities_per_call - 1, data.entities_n) do
local entity = data.entities[i] local entity = data.entities[i]
local created_entity = create_entity(entity) local created_entity = create_entity(entity)
if (nil == created_entity) then if not created_entity then
error('Failed creating entity ' .. entity.name .. ' on surface.') error('Failed creating entity ' .. entity.name .. ' on surface.')
end end
@ -128,7 +128,7 @@ function Template.insert(surface, tiles, entities)
} }
local continue = true local continue = true
for i = 1, 4 do for _ = 1, 4 do
continue = insert_action(data) continue = insert_action(data)
if not continue then if not continue then
return return
@ -155,24 +155,4 @@ function Template.resources(surface, resources)
end end
end end
--[[--
Designed to spawn a market.
@param surface LuaSurface
@param position Position
@param force LuaForce
@param market_items Table
]]
function Template.market(surface, position, force)
local market = surface.create_entity({name = 'market', position = position})
market.destructible = false
force.add_chart_tag(surface, {
text = 'Market',
position = position,
})
raise_event(Template.events.on_placed_entity, {entity = market})
end
return Template return Template