2018-11-24 11:45:43 +02:00
-- dependencies
local Event = require ' utils.event '
local Game = require ' utils.game '
local Global = require ' utils.global '
2019-02-02 15:21:43 +02:00
local Toast = require ' features.gui.toast '
2018-11-24 11:45:43 +02:00
local ForceControl = require ' features.force_control '
2019-02-01 03:15:41 +02:00
local ScoreTable = require ' map_gen.maps.diggy.score_table '
2018-11-25 21:09:46 +02:00
local Retailer = require ' features.retailer '
local Gui = require ' utils.gui '
2018-12-29 19:39:20 +02:00
local Utils = require ' utils.core '
local Color = require ' resources.color_presets '
2018-11-25 21:09:46 +02:00
local format = string.format
local floor = math.floor
local log = math.log
2018-12-13 21:59:00 +02:00
local max = math.max
2018-11-25 21:09:46 +02:00
local insert = table.insert
2018-12-13 21:59:00 +02:00
local pairs = pairs
local add_experience = ForceControl.add_experience
local add_experience_percentage = ForceControl.add_experience_percentage
local remove_experience_percentage = ForceControl.remove_experience_percentage
local print_player_floating_text_position = Game.print_player_floating_text_position
local get_force_data = ForceControl.get_force_data
local get_player_by_index = Game.get_player_by_index
local set_item = Retailer.set_item
2018-12-28 22:31:02 +02:00
local disable_item = Retailer.disable_item
local enable_item = Retailer.enable_item
2018-11-24 11:45:43 +02:00
-- this
local Experience = { }
local mining_efficiency = {
active_modifier = 0 ,
research_modifier = 0 ,
level_modifier = 0 ,
}
local inventory_slots = {
active_modifier = 0 ,
research_modifier = 0 ,
level_modifier = 0 ,
}
local health_bonus = {
active_modifier = 0 ,
research_modifier = 0 ,
level_modifier = 0 ,
}
Global.register ( {
mining_efficiency = mining_efficiency ,
inventory_slots = inventory_slots ,
health_bonus = health_bonus
} , function ( tbl )
mining_efficiency = tbl.mining_efficiency
inventory_slots = tbl.inventory_slots
health_bonus = tbl.health_bonus
end )
2018-11-24 18:17:15 +02:00
local config = { }
2018-11-24 11:45:43 +02:00
2018-12-29 19:39:20 +02:00
local gain_xp_color = Color.light_sky_blue
local lose_xp_color = Color.red
local unlocked_color = Color.black
local locked_color = Color.gray
2019-02-21 01:39:49 +02:00
local table_column_layout = { type = ' table ' , column_count = 2 }
2018-11-26 21:31:10 +02:00
2019-02-21 01:39:49 +02:00
local level_up_formula = ( function ( level_reached )
2018-11-30 20:57:31 +02:00
local difficulty_scale = floor ( config.difficulty_scale )
local level_fine_tune = floor ( config.xp_fine_tune )
2019-02-19 18:24:21 +02:00
local start_value = ( floor ( config.first_lvl_xp ) )
2018-11-30 20:57:31 +02:00
local precision = ( floor ( config.cost_precision ) )
2018-11-24 11:45:43 +02:00
local function formula ( level )
2019-02-19 18:24:21 +02:00
return ( floor (
2019-02-21 01:39:49 +02:00
( 1.15 ^ ( level * 0.1 ) )
+ difficulty_scale * ( level ) ^ 3
+ level_fine_tune * ( level ) ^ 2
+ start_value * ( level )
- difficulty_scale * ( level )
- level_fine_tune * ( level ) )
2018-11-24 11:45:43 +02:00
)
end
local value = formula ( level_reached + 1 )
local lower_value = formula ( level_reached )
2018-11-30 20:57:31 +02:00
value = value - ( value % ( 10 ^ ( floor ( log ( value , 10 ) ) - precision ) ) )
2018-11-24 11:45:43 +02:00
if lower_value == 0 then
return value - lower_value
end
2018-11-24 18:17:15 +02:00
lower_value = lower_value - ( lower_value % ( 10 ^ ( floor ( log ( lower_value , 10 ) ) - precision ) ) )
2018-11-24 11:45:43 +02:00
return value - lower_value
end )
2019-02-19 18:24:21 +02:00
local level_table = { }
---Get experience requirement for a given level
---Primarily used for the Experience GUI to display total experience required to unlock a specific item
---@param level number a number specifying the level
---@return number required total experience to reach supplied level
local function calculate_level_xp ( level )
if level_table [ level ] == nil then
local value
if level == 1 then
2019-02-21 01:39:49 +02:00
value = level_up_formula ( level - 1 )
2019-02-19 18:24:21 +02:00
else
2019-02-21 01:39:49 +02:00
value = level_up_formula ( level - 1 ) + calculate_level_xp ( level - 1 )
2019-02-19 18:24:21 +02:00
end
insert ( level_table , level , value )
end
return level_table [ level ]
end
---Get a percentage of required experience between a level and the next level
---@param level number a number specifying the current level
---@return number a percentage of the required experience to level up from one level to the other
local function percentage_of_level_req ( level , percentage )
2019-02-21 01:39:49 +02:00
return level_up_formula ( level ) * percentage
2019-02-19 18:24:21 +02:00
end
2018-11-25 21:09:46 +02:00
---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 )
2018-12-13 21:59:00 +02:00
local current_level = get_force_data ( force ) . current_level
2018-11-25 21:09:46 +02:00
local force_name = force.name
2018-11-26 22:37:41 +02:00
for _ , prototype in pairs ( config.unlockables ) do
2018-12-28 22:31:02 +02:00
local prototype_level = prototype.level
if current_level < prototype_level then
disable_item ( force_name , prototype.name , format ( ' Unlocks at level %d ' , prototype_level ) )
else
enable_item ( force_name , prototype.name )
2018-11-25 21:09:46 +02:00
end
end
end
2018-11-24 11:45:43 +02:00
---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 level_up number a level if updating as part of a level up (optional)
function Experience . update_mining_speed ( force , level_up )
2018-11-24 18:17:15 +02:00
local buff = config.buffs [ ' mining_speed ' ]
2019-02-19 18:24:21 +02:00
if buff.max == nil or force.manual_mining_speed_modifier < buff.max then
level_up = level_up ~= nil and level_up or 0
if level_up > 0 and buff ~= nil then
local level = get_force_data ( force ) . current_level
2019-02-21 01:39:49 +02:00
local adjusted_value = floor ( max ( buff.value , 24 * 0.9 ^ level ) )
2019-02-19 18:24:21 +02:00
local value = ( buff.double_level ~= nil and level_up % buff.double_level == 0 ) and adjusted_value * 2 or adjusted_value
mining_efficiency.level_modifier = mining_efficiency.level_modifier + ( value * 0.01 )
end
-- remove the current buff
2019-02-21 01:47:16 +02:00
local old_modifier = ( force.manual_mining_speed_modifier == 0 ) and 0 or force.manual_mining_speed_modifier - mining_efficiency.active_modifier
old_modifier = old_modifier >= 0 and old_modifier or 0
2019-02-19 18:24:21 +02:00
-- update the active modifier
mining_efficiency.active_modifier = mining_efficiency.research_modifier + mining_efficiency.level_modifier
2018-11-24 11:45:43 +02:00
2019-02-19 18:24:21 +02:00
-- add the new active modifier to the non-buffed modifier
force.manual_mining_speed_modifier = old_modifier + mining_efficiency.active_modifier
end
2018-11-24 11:45:43 +02:00
end
---Updates a forces inventory slots. By removing active modifiers and re-adding
---@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)
function Experience . update_inventory_slots ( force , level_up )
2018-11-24 18:17:15 +02:00
local buff = config.buffs [ ' inventory_slot ' ]
2019-02-19 18:24:21 +02:00
if buff.max == nil or force.character_inventory_slots_bonus < buff.max then
level_up = level_up ~= nil and level_up or 0
if level_up > 0 and buff ~= nil then
local value = ( buff.double_level ~= nil and level_up % buff.double_level == 0 ) and buff.value * 2 or buff.value
inventory_slots.level_modifier = inventory_slots.level_modifier + value
end
2018-11-24 11:45:43 +02:00
2019-02-19 18:24:21 +02:00
-- remove the current buff
2019-02-21 01:47:16 +02:00
local old_modifier = ( force.character_inventory_slots_bonus == 0 ) and 0 or force.character_inventory_slots_bonus - inventory_slots.active_modifier
old_modifier = old_modifier >= 0 and old_modifier or 0
2019-02-19 18:24:21 +02:00
-- update the active modifier
inventory_slots.active_modifier = inventory_slots.research_modifier + inventory_slots.level_modifier
2018-11-24 11:45:43 +02:00
2019-02-19 18:24:21 +02:00
-- add the new active modifier to the non-buffed modifier
force.character_inventory_slots_bonus = old_modifier + inventory_slots.active_modifier
end
2018-11-24 11:45:43 +02:00
end
2019-02-19 18:24:21 +02:00
---Updates a forces health bonus. By removing active modifiers and re-adding
2018-11-24 11:45:43 +02:00
---@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)
function Experience . update_health_bonus ( force , level_up )
2018-11-24 18:17:15 +02:00
local buff = config.buffs [ ' health_bonus ' ]
2019-02-19 18:24:21 +02:00
if buff.max == nil or force.character_health_bonus < buff.max then
level_up = level_up ~= nil and level_up or 0
if level_up > 0 and buff ~= nil then
2019-02-21 01:39:49 +02:00
local value = ( buff.double_level ~= nil and level_up % buff.double_level == 0 ) and buff.value * 2 or buff.value
2019-02-19 18:24:21 +02:00
health_bonus.level_modifier = health_bonus.level_modifier + value
end
2018-11-24 11:45:43 +02:00
2019-02-19 18:24:21 +02:00
-- remove the current buff
2019-02-21 01:47:16 +02:00
local old_modifier = ( force.character_health_bonus == 0 ) and 0 or force.character_health_bonus - health_bonus.active_modifier
old_modifier = old_modifier >= 0 and old_modifier or 0
2019-02-19 18:24:21 +02:00
-- update the active modifier
health_bonus.active_modifier = health_bonus.research_modifier + health_bonus.level_modifier
2018-11-24 11:45:43 +02:00
2019-02-19 18:24:21 +02:00
-- add the new active modifier to the non-buffed modifier
force.character_health_bonus = old_modifier + health_bonus.active_modifier
end
2018-11-24 11:45:43 +02:00
end
2018-11-30 20:57:31 +02:00
-- declaration of variables to prevent table look ups @see Experience.register
2018-11-24 11:45:43 +02:00
local sand_rock_xp
2018-11-30 21:30:50 +02:00
local rock_big_xp
2018-11-24 11:45:43 +02:00
local rock_huge_xp
2018-11-25 03:27:41 +02:00
---Awards experience when a rock has been mined (increases by 1 XP every 5th level)
2018-11-24 11:45:43 +02:00
---@param event LuaEvent
local function on_player_mined_entity ( event )
local entity = event.entity
2018-12-01 18:32:54 +02:00
local name = entity.name
2018-11-24 11:45:43 +02:00
local player_index = event.player_index
2018-12-13 21:59:00 +02:00
local force = get_player_by_index ( player_index ) . force
local level = get_force_data ( force ) . current_level
2018-12-01 18:32:54 +02:00
local exp = 0
if name == ' sand-rock-big ' then
2018-11-25 13:53:05 +02:00
exp = sand_rock_xp + floor ( level / 5 )
2018-12-01 18:32:54 +02:00
elseif name == ' rock-big ' then
exp = rock_big_xp + floor ( level / 5 )
elseif name == ' rock-huge ' then
2018-11-25 13:53:05 +02:00
exp = rock_huge_xp + floor ( level / 5 )
2018-12-01 18:32:54 +02:00
end
if exp == 0 then
2018-11-24 11:45:43 +02:00
return
end
2018-12-01 18:32:54 +02:00
2019-02-21 01:39:49 +02:00
print_player_floating_text_position ( player_index , format ( ' +%s XP ' , exp ) , gain_xp_color , 0 , - 0.5 )
2018-12-13 21:59:00 +02:00
add_experience ( force , exp )
2018-11-24 11:45:43 +02:00
end
---Awards experience when a research has finished, based on ingredient cost of research
---@param event LuaEvent
local function on_research_finished ( event )
local research = event.research
local force = research.force
2019-02-19 18:24:21 +02:00
local exp
if research.research_unit_count_formula ~= nil then
local force_data = get_force_data ( force )
exp = percentage_of_level_req ( force_data.current_level , config.XP [ ' infinity-research ' ] )
else
local award_xp = 0
for _ , ingredient in pairs ( research.research_unit_ingredients ) do
local name = ingredient.name
local reward = config.XP [ name ]
award_xp = award_xp + reward
end
exp = award_xp * research.research_unit_count
2018-11-24 11:45:43 +02:00
end
2019-02-13 19:18:40 +02:00
local text = format ( ' Research completed! +%s XP ' , exp )
2018-11-24 11:45:43 +02:00
for _ , p in pairs ( game.connected_players ) do
local player_index = p.index
2018-12-13 21:59:00 +02:00
print_player_floating_text_position ( player_index , text , gain_xp_color , - 1 , - 0.5 )
2018-11-24 11:45:43 +02:00
end
2018-12-13 21:59:00 +02:00
add_experience ( force , exp )
2018-11-24 11:45:43 +02:00
local current_modifier = mining_efficiency.research_modifier
2018-11-24 18:17:15 +02:00
local new_modifier = force.mining_drill_productivity_bonus * config.mining_speed_productivity_multiplier * 0.5
2018-11-24 11:45:43 +02:00
if ( current_modifier == new_modifier ) then
-- something else was researched
return
end
mining_efficiency.research_modifier = new_modifier
inventory_slots.research_modifier = force.mining_drill_productivity_bonus * 50 -- 1 per level
Experience.update_inventory_slots ( force , 0 )
Experience.update_mining_speed ( force , 0 )
2019-02-21 01:47:16 +02:00
Experience.update_health_bonus ( force , 0 )
2018-11-24 11:45:43 +02:00
game.forces . player.technologies [ ' landfill ' ] . enabled = false
end
2018-11-25 03:27:41 +02:00
---Awards experience when a rocket has been launched based on percentage of total experience
2018-11-24 11:45:43 +02:00
---@param event LuaEvent
local function on_rocket_launched ( event )
2018-11-25 03:27:41 +02:00
local force = event.rocket . force
2018-12-13 21:59:00 +02:00
local exp = add_experience_percentage ( force , config.XP [ ' rocket_launch ' ] )
2019-02-13 07:18:17 +02:00
local text = format ( ' Rocket launched! +%s XP ' , exp )
2018-11-24 11:45:43 +02:00
for _ , p in pairs ( game.connected_players ) do
local player_index = p.index
2018-12-13 21:59:00 +02:00
print_player_floating_text_position ( player_index , text , gain_xp_color , - 1 , - 0.5 )
2018-11-24 11:45:43 +02:00
end
end
---Awards experience when a player kills an enemy, based on type of enemy
---@param event LuaEvent
2018-12-22 01:37:44 +02:00
local function on_entity_died ( event )
2018-11-24 11:45:43 +02:00
local entity = event.entity
2018-11-24 17:05:01 +02:00
local force = event.force
2018-11-24 11:45:43 +02:00
local cause = event.cause
2018-11-25 14:45:14 +02:00
--For bot mining and turrets
2018-12-01 18:32:54 +02:00
if not cause or not cause.valid or cause.type ~= ' player ' then
local exp = 0
local floating_text_position
-- stuff killed by the player force, but not the player
2018-11-25 06:14:08 +02:00
if force and force.name == ' player ' then
2018-11-30 21:30:50 +02:00
local entity_name = entity.name
2018-11-25 14:45:14 +02:00
if cause and ( cause.name == ' artillery-turret ' or cause.name == ' gun-turret ' or cause.name == ' laser-turret ' or cause.name == ' flamethrower-turret ' ) then
2018-12-01 18:32:54 +02:00
exp = config.XP [ ' enemy_killed ' ] * ( config.alien_experience_modifiers [ entity_name ] or 1 )
floating_text_position = cause.position
2018-11-25 14:45:14 +02:00
else
2018-12-13 21:59:00 +02:00
local level = get_force_data ( force ) . current_level
2018-11-30 21:30:50 +02:00
if entity_name == ' sand-rock-big ' then
exp = floor ( ( sand_rock_xp + level * 0.2 ) * 0.5 )
elseif entity_name == ' rock-big ' then
exp = floor ( ( rock_big_xp + level * 0.2 ) * 0.5 )
elseif entity_name == ' rock-huge ' then
exp = floor ( ( rock_huge_xp + level * 0.2 ) * 0.5 )
2018-11-25 14:45:14 +02:00
end
2018-12-01 22:44:01 +02:00
floating_text_position = entity.position
2018-11-24 17:05:01 +02:00
end
end
2018-12-01 18:32:54 +02:00
if exp > 0 then
2019-02-13 08:10:16 +02:00
Game.print_floating_text ( entity.surface , floating_text_position , format ( ' +%s XP ' , exp ) , gain_xp_color )
2018-12-13 21:59:00 +02:00
add_experience ( force , exp )
2018-11-25 21:09:46 +02:00
end
2018-12-01 18:32:54 +02:00
2018-11-26 21:23:53 +02:00
return
2018-11-24 11:45:43 +02:00
end
2018-11-24 17:05:01 +02:00
if entity.force . name ~= ' enemy ' then
return
end
2018-12-01 18:32:54 +02:00
local exp = config.XP [ ' enemy_killed ' ] * ( config.alien_experience_modifiers [ entity.name ] or 1 )
2018-12-13 21:59:00 +02:00
print_player_floating_text_position ( cause.player . index , format ( ' +%d XP ' , exp ) , gain_xp_color , - 1 , - 0.5 )
add_experience ( force , exp )
2018-11-24 11:45:43 +02:00
end
---Deducts experience when a player respawns, based on a percentage of total experience
---@param event LuaEvent
local function on_player_respawned ( event )
2018-12-13 21:59:00 +02:00
local player = get_player_by_index ( event.player_index )
local exp = remove_experience_percentage ( player.force , config.XP [ ' death-penalty ' ] , 50 )
2019-02-13 08:10:16 +02:00
local text = format ( ' -%s XP ' , exp )
game.print ( format ( ' %s drained %s experience. ' , player.name , exp ) , lose_xp_color )
2018-11-24 11:45:43 +02:00
for _ , p in pairs ( game.connected_players ) do
2018-12-13 21:59:00 +02:00
print_player_floating_text_position ( p.index , text , lose_xp_color , - 1 , - 0.5 )
2018-11-24 11:45:43 +02:00
end
2018-12-22 01:37:44 +02:00
ScoreTable.add ( ' Experience lost ' , exp )
2018-11-24 11:45:43 +02:00
end
2018-11-25 21:09:46 +02:00
local function redraw_title ( data )
2018-12-13 21:59:00 +02:00
local force_data = get_force_data ( ' player ' )
2018-12-29 19:39:20 +02:00
data.frame . caption = Utils.comma_value ( force_data.total_experience ) .. ' total experience earned! '
2018-11-25 21:09:46 +02:00
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 )
2018-11-30 20:57:31 +02:00
local heading_table = frame.add ( table_column_layout )
2019-02-21 01:39:49 +02:00
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 )
2018-11-25 21:09:46 +02:00
end
local function redraw_progressbar ( data )
2018-12-13 21:59:00 +02:00
local force_data = get_force_data ( ' player ' )
2018-11-25 21:09:46 +02:00
local flow = data.experience_progressbars
Gui.clear ( flow )
2019-02-21 01:39:49 +02:00
apply_heading_style ( flow.add ( { type = ' label ' , tooltip = ' Currently at level: ' .. force_data.current_level .. ' \n Next level at: ' .. Utils.comma_value ( ( force_data.total_experience - force_data.current_experience ) + force_data.experience_level_up_cap ) .. ' xp \n Remaining 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 ' } )
2018-11-25 21:09:46 +02:00
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
2018-12-13 21:59:00 +02:00
local current_force_level = get_force_data ( ' player ' ) . current_level
2018-11-25 21:09:46 +02:00
2018-11-26 22:37:41 +02:00
for _ , prototype in pairs ( config.unlockables ) do
2018-11-25 21:09:46 +02:00
local current_item_level = prototype.level
local first_item_for_level = current_item_level ~= last_level
local color
2019-02-21 01:39:49 +02:00
if current_force_level >= current_item_level then
2018-11-26 21:31:10 +02:00
color = unlocked_color
2018-11-25 21:09:46 +02:00
else
2018-11-26 21:31:10 +02:00
color = locked_color
2018-11-25 21:09:46 +02:00
end
2018-11-30 20:57:31 +02:00
local list = experience_scroll_pane.add ( table_column_layout )
2018-11-25 21:09:46 +02:00
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 ,
2018-12-29 19:39:20 +02:00
tooltip = ' XP: ' .. Utils.comma_value ( calculate_level_xp ( current_item_level ) ) ,
2018-11-25 21:09:46 +02:00
} )
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
2018-11-30 20:57:31 +02:00
local list = buff_scroll_pane.add ( table_column_layout )
2018-11-25 21:09:46 +02:00
list.style . horizontal_spacing = 16
local level_caption = ' '
if not all_levels_shown then
all_levels_shown = true
level_caption = ' All levels '
end
2019-02-21 01:39:49 +02:00
local level_label = list.add ( { type = ' label ' , caption = level_caption } )
2018-11-25 21:09:46 +02:00
level_label.style . minimal_width = 100
2018-11-26 21:31:10 +02:00
level_label.style . font_color = unlocked_color
2018-11-25 21:09:46 +02:00
local buff_caption
local effect_value = effects.value
if name == ' mining_speed ' then
2018-12-01 18:32:54 +02:00
buff_caption = format ( ' +%d mining speed ' , effect_value )
2018-11-25 21:09:46 +02:00
elseif name == ' inventory_slot ' then
2018-12-01 18:32:54 +02:00
buff_caption = format ( ' +%d inventory slot%s ' , effect_value , effect_value > 1 and ' s ' or ' ' )
2018-11-25 21:09:46 +02:00
elseif name == ' health_bonus ' then
2018-12-01 18:32:54 +02:00
buff_caption = format ( ' +%d max health ' , effect_value )
2018-11-25 21:09:46 +02:00
else
2018-12-01 18:32:54 +02:00
buff_caption = format ( ' +%d %s ' , effect_value , name )
2018-11-25 21:09:46 +02:00
end
2019-02-21 01:39:49 +02:00
local buffs_label = list.add ( { type = ' label ' , caption = buff_caption } )
2018-11-25 21:09:46 +02:00
buffs_label.style . minimal_width = 220
2018-11-26 21:31:10 +02:00
buffs_label.style . font_color = unlocked_color
2018-11-25 21:09:46 +02:00
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
2019-02-21 01:39:49 +02:00
frame = left.add ( { name = ' Diggy.Experience.Frame ' , type = ' frame ' , direction = ' vertical ' } )
2018-11-25 21:09:46 +02:00
2019-02-21 01:39:49 +02:00
local experience_progressbars = frame.add ( { type = ' flow ' , direction = ' vertical ' } )
local experience_list_heading = frame.add ( { type = ' flow ' , direction = ' horizontal ' } )
2018-11-25 21:09:46 +02:00
2019-02-21 01:39:49 +02:00
local experience_scroll_pane = frame.add ( { type = ' scroll-pane ' } )
2018-11-25 21:09:46 +02:00
experience_scroll_pane.style . maximal_height = 300
2019-02-21 01:39:49 +02:00
local buff_list_heading = frame.add ( { type = ' flow ' , direction = ' horizontal ' } )
2018-11-25 21:09:46 +02:00
2019-02-21 01:39:49 +02:00
local buff_scroll_pane = frame.add ( { type = ' scroll-pane ' } )
2018-11-25 21:09:46 +02:00
buff_scroll_pane.style . maximal_height = 100
2019-02-21 01:39:49 +02:00
frame.add ( { type = ' button ' , name = ' Diggy.Experience.Button ' , caption = ' Close ' } )
2018-11-25 21:09:46 +02:00
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 )
2018-12-13 21:59:00 +02:00
get_player_by_index ( event.player_index ) . gui.top . add ( {
2018-11-25 21:09:46 +02:00
name = ' Diggy.Experience.Button ' ,
type = ' sprite-button ' ,
sprite = ' entity/market ' ,
} )
end
2018-12-16 03:28:00 +02:00
Gui.allow_player_to_toggle_top_element_visibility ( ' Diggy.Experience.Button ' )
2018-11-25 21:09:46 +02:00
Gui.on_click ( ' Diggy.Experience.Button ' , toggle )
2019-02-21 01:39:49 +02:00
Gui.on_custom_close ( ' Diggy.Experience.Frame ' , function ( event )
2018-11-25 21:09:46 +02:00
event.element . destroy ( )
end )
---Updates the experience progress gui for every player that has it open
local function update_gui ( )
2018-11-26 22:37:41 +02:00
local players = game.connected_players
for i = # players , 1 , - 1 do
local p = players [ i ]
2018-11-25 21:09:46 +02:00
local frame = p.gui . left [ ' Diggy.Experience.Frame ' ]
if frame and frame.valid then
2019-02-21 01:39:49 +02:00
local data = { player = p , trigger = ' update_gui ' }
2018-11-25 21:09:46 +02:00
toggle ( data )
end
end
2019-02-21 01:47:16 +02:00
--Resets buffs if they have been set to 0
local force = game.forces . player
Experience.update_inventory_slots ( force , 0 )
Experience.update_mining_speed ( force , 0 )
Experience.update_health_bonus ( force , 0 )
2018-11-25 21:09:46 +02:00
end
2018-11-24 11:45:43 +02:00
function Experience . register ( cfg )
2018-11-24 18:17:15 +02:00
config = cfg
2018-11-24 11:45:43 +02:00
2018-12-22 01:37:44 +02:00
ScoreTable.reset ( ' Experience lost ' )
2018-11-24 11:45:43 +02:00
--Adds the function on how to calculate level caps (When to level up)
2018-11-25 21:09:46 +02:00
local ForceControlBuilder = ForceControl.register ( level_up_formula )
2018-11-24 11:45:43 +02:00
--Adds a function that'll be executed at every level up
2019-02-21 01:39:49 +02:00
ForceControlBuilder.register_on_every_level ( function ( level_reached , force )
Toast.toast_force ( force , 10 , format ( ' Your team has reached level %d! ' , level_reached ) )
2018-11-24 11:45:43 +02:00
Experience.update_inventory_slots ( force , level_reached )
Experience.update_mining_speed ( force , level_reached )
Experience.update_health_bonus ( force , level_reached )
2018-11-25 21:09:46 +02:00
Experience.update_market_contents ( force )
2018-11-24 11:45:43 +02:00
end )
-- Events
Event.add ( defines.events . on_player_mined_entity , on_player_mined_entity )
Event.add ( defines.events . on_research_finished , on_research_finished )
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_entity_died , on_entity_died )
2018-11-25 21:09:46 +02:00
Event.add ( defines.events . on_player_created , on_player_created )
Event.on_nth_tick ( 61 , update_gui )
2018-11-24 11:45:43 +02:00
-- Prevents table lookup thousands of times
2018-11-24 18:17:15 +02:00
sand_rock_xp = config.XP [ ' sand-rock-big ' ]
2018-11-30 21:30:50 +02:00
rock_big_xp = config.XP [ ' rock-big ' ]
2018-11-24 18:17:15 +02:00
rock_huge_xp = config.XP [ ' rock-huge ' ]
2018-11-24 11:45:43 +02:00
end
function Experience . on_init ( )
--Adds the 'player' force to participate in the force control system.
local force = game.forces . player
ForceControl.register_force ( force )
2018-12-28 22:31:02 +02:00
local force_name = force.name
for _ , prototype in pairs ( config.unlockables ) do
set_item ( force_name , prototype )
end
Experience.update_market_contents ( force )
2018-11-24 11:45:43 +02:00
end
return Experience