Added production to control

This commit is contained in:
Cooldude2606
2019-07-20 23:44:48 +01:00
parent 5d0ef2f11a
commit f5619165f6
409 changed files with 21360 additions and 23447 deletions

View File

@@ -102,7 +102,7 @@ end
--- Warnings added and removed
if config.player_warnings then
local Warnings = require 'modules.addons.warnings'
local Warnings = require 'modules.control.warnings'
Event.add(Warnings.events.on_warning_added,function(event)
local player_name,by_player_name = get_player_name(event)
emit_event{

View File

@@ -1,5 +1,5 @@
local Commands = require 'expcore.commands'
local Warnings = require 'modules.addons.warnings'
local Warnings = require 'modules.control.warnings'
local format_chat_player_name = ext_require('expcore.common','format_chat_player_name')
local config = require 'config.warnings'
require 'config.expcore-commands.parse_roles'

View File

@@ -1,9 +1,12 @@
--[[-- Control Module - Jail
- Adds a way to jail players and temp ban players.
@module Jail
@control Jail
@alias Jail
@usage
-- import the module from the control modules
local Jail = require 'modules.control.jail'
-- This will move 'MrBiter' to the jail role and remove all other roles from them
-- the player name and reason are only so they can be included in the event for user feedback
Jail.jail_player('MrBiter','Cooldude2606','Likes biters too much')
@@ -17,9 +20,17 @@
Jail.temp_ban_player('MrBiter','Cooldude2606','Likes biters too much')
]]
--- Allows moving players into the jail role
-- @dep expcore.roles
local Roles = require 'expcore.roles'
--- Allows accessing a player from any value
-- @dep utils.game
local Game = require 'utils.game'
--- Allows storing data in the global table
-- @dep utils.global
local Global = require 'utils.global'
--- Use of move_items to clear inventroies
-- @dep expcore.common
local move_items = ext_require('expcore.common','move_items')
local valid_player = Game.get_player_from_any
@@ -82,7 +93,7 @@ local function event_emit(event,player,by_player_name,reason)
})
end
--- Jail functions.
--- Jail.
-- Functions related to jail
-- @section jail-functions
@@ -137,7 +148,7 @@ function Jail.unjail_player(player,by_player_name)
return true
end
--- Temp ban functions.
--- Temp ban.
-- Functions related to temp ban
-- @section temp-ban-functions

View File

@@ -0,0 +1,237 @@
--[[-- Control Module - Production
- Common functions used to track production of items
@control Production
@alias Production
@usage
-- import the module from the control modules
local Production = require 'modules.control.production'
-- This will return the less precise index from the one given
-- this means that one_second will return one_minute or ten_hours will return fifty_hours
-- the other precision work like wise
Production.precision_up(defines.flow_precision_index.one_second)
-- The get production function is used to get production, consumion and net
-- it may be used for any item and with any precision level, use total for total
Production.get_production(game.forces.player,'iron-plate',defines.flow_precision_index.one_minute)
-- The fluctuations works by compearing recent production with the average over time
-- again any precision may be used, apart from one_thousand_hours as there would be no valid average
Production.get_fluctuations(game.forces.player,'iron-plate',defines.flow_precision_index.one_minute)
-- ETA is calculated based on what function you use but all share a similar method
-- for production eta it will take current production average given by the precision
-- and work out how many ticks it will require to make the required amount (1000 by default)
Production.get_production_eta(game.forces.player,'iron-plate',defines.flow_precision_index.one_minute,250000)
-- Both get_color and format_number are helper functions to help format production stats
-- get_color will return green,orange,red,or grey based on the active_value
-- the passive_value is used when active_value is 0 and can only return orange,red,or grey
Production.get_color(clamp,active_value,passive_value)
]]
--- Provides colors for Production.get_color
-- @dep resources.color_presets
local Colors = require 'resources.color_presets'
--- Provides format_number function to add surfixs
-- @dep util
local format_number = ext_require('util','format_number')
local precision_index = defines.flow_precision_index
local Production = {}
--- Precision.
-- Functions which are used to do basic things
-- @section precision
--- Gets the next lesser precision index value, eg 1 second -> 1 minute
-- @tparam defines.flow_precision_index precision
-- @treturn[1] defines.flow_precision_index the next precision value
-- @treturn[1] number the multiplicive difference between the values
function Production.precision_up(precision)
if precision == precision_index.one_second then return precision_index.one_minute,60
elseif precision == precision_index.one_minute then return precision_index.ten_minutes,10
elseif precision == precision_index.ten_minutes then return precision_index.one_hour,6
elseif precision == precision_index.one_hour then return precision_index.ten_hours,10
elseif precision == precision_index.ten_hours then return precision_index.fifty_hours,5
elseif precision == precision_index.fifty_hours then return precision_index.two_hundred_fifty_hours,5
elseif precision == precision_index.two_hundred_fifty_hours then return precision_index.one_thousand_hours,4
end
end
--- Gets the next greater precision index value, eg 1 minute -> 1 second
-- @tparam defines.flow_precision_index precision
-- @treturn[1] defines.flow_precision_index the next precision value
-- @treturn[1] number the multiplicive difference between the values
function Production.precision_down(precision)
if precision == precision_index.one_minute then return precision_index.one_second,60
elseif precision == precision_index.ten_minutes then return precision_index.one_minute,10
elseif precision == precision_index.one_hour then return precision_index.ten_minutes,6
elseif precision == precision_index.ten_hours then return precision_index.one_hour,10
elseif precision == precision_index.fifty_hours then return precision_index.ten_hours,5
elseif precision == precision_index.two_hundred_fifty_hours then return precision_index.fifty_hours,5
elseif precision == precision_index.one_thousand_hours then return precision_index.two_hundred_fifty_hours,4
end
end
--- Gets the number of tick that precision is given over, eg 1 minute -> 60 ticks
-- @tparam defines.flow_precision_index precision
-- @treturn number the number of ticks in this time
function Production.precision_ticks(precision)
if precision == precision_index.one_second then return 60
elseif precision == precision_index.one_minute then return 3600
elseif precision == precision_index.ten_minutes then return 36000
elseif precision == precision_index.one_hour then return 216000
elseif precision == precision_index.ten_hours then return 2160000
elseif precision == precision_index.fifty_hours then return 10800000
elseif precision == precision_index.two_hundred_fifty_hours then return 54000000
elseif precision == precision_index.one_thousand_hours then return 216000000
end
end
--- Statistics.
-- Functions used to get information about production
-- @section stats
--- Returns the production data for the whole game time
-- @tparam LuaForce force the force to get the data for
-- @tparam string item_name the name of the item that you want the data about
-- @treturn table contains total made, used and net
function Production.get_production_total(force,item_name)
local stats = force.item_production_statistics
local made = stats.get_input_count(item_name) or 0
local used = stats.get_output_count(item_name) or 0
return {
made=made,
used=used,
net=made-used
}
end
--- Returns the production data for the given precision game time
-- @tparam LuaForce force the force to get the data for
-- @tparam string item_name the name of the item that you want the data about
-- @tparam defines.flow_precision_index precision the precision that you want the data given to
-- @treturn table contains made, used and net
function Production.get_production(force,item_name,precision)
local stats = force.item_production_statistics.get_flow_count
local made = stats{name=item_name,input=true,precision_index=precision} or 0
local used = stats{name=item_name,input=false,precision_index=precision} or 0
return {
made=made,
used=used,
net=made-used
}
end
--- Returns the current fluctuation from the average
-- @tparam LuaForce force the force to get the data for
-- @tparam string item_name the name of the item that you want the data about
-- @tparam defines.flow_precision_index precision the precision that you want the data given to
-- @treturn table contains made, used and net
function Production.get_fluctuations(force,item_name,precision)
local percision_up = Production.precision_up(precision)
local current = Production.get_production(force,item_name,precision)
local previous = Production.get_production(force,item_name,percision_up)
return {
made=(current.made/previous.made)-1,
used=(current.used/previous.used)-1,
net=(current.net/previous.net)-1,
}
end
--- Returns the amount of ticks required to produce a certain amount
-- @tparam LuaForce force the force to get the data for
-- @tparam string item_name the name of the item that you want the data about
-- @tparam defines.flow_precision_index precision the precision that you want the data given to
-- @tparam[opt=1000] number required the number of items that are required to be made
-- @treturn number the number of ticks required to produce this ammount of items
function Production.get_production_eta(force,item_name,precision,required)
required = required or 1000
local ticks = Production.precision_ticks(precision)
local production = Production.get_production(force,item_name,precision)
return production.made == 0 and -1 or ticks*required/production.made
end
--- Returns the amount of ticks required to consume a certain amount
-- @tparam LuaForce force the force to get the data for
-- @tparam string item_name the name of the item that you want the data about
-- @tparam defines.flow_precision_index precision the precision that you want the data given to
-- @tparam[opt=1000] number required the number of items that are required to be consumed
-- @treturn number the number of ticks required to consume this ammount of items
function Production.get_consumsion_eta(force,item_name,precision,required)
required = required or 1000
local ticks = Production.precision_ticks(precision)
local production = Production.get_production(force,item_name,precision)
return production.used == 0 and -1 or ticks*required/production.used
end
--- Returns the amount of ticks required to produce but not consume a certain amount
-- @tparam LuaForce force the force to get the data for
-- @tparam string item_name the name of the item that you want the data about
-- @tparam defines.flow_precision_index precision the precision that you want the data given to
-- @tparam[opt=1000] number required the number of items that are required to be made but not used
-- @treturn number the number of ticks required to produce, but not use, this ammount of items
function Production.get_net_eta(force,item_name,precision,required)
required = required or 1000
local ticks = Production.precision_ticks(precision)
local production = Production.get_production(force,item_name,precision)
return production.net == 0 and -1 or ticks*required/production.net
end
--- Formating.
-- Functions used to format production values
-- @section formating
--- Returns a color value bassed on the value that was given
-- @tparam number clamp value which seperates the different colours
-- @tparam number active_value first value tested, tested against clamp
-- @tparam number passive_value second value tested, tested against 0
-- @treturn table contains r,g,b keys
function Production.get_color(clamp,active_value,passive_value)
if active_value > clamp then
return Colors.light_green
elseif active_value < -clamp then
return Colors.indian_red
elseif active_value ~= 0 then
return Colors.orange
elseif passive_value and passive_value > 0 then
return Colors.orange
elseif passive_value and passive_value ~= 0 then
return Colors.indian_red
else
return Colors.grey
end
end
--- Returns three parts used to format a number
-- @tparam number value the value to format
-- @treturn[1] string the sign for the number
-- @treturn[1] string the surfix for any unit used
function Production.format_number(value)
local rtn = format_number(math.round(value,1),true)
local surfix = rtn:sub(-1)
if value > 0 then
rtn = '+'..rtn
elseif value == 0 and rtn:sub(1,1) == '-' then
rtn = rtn:sub(2)
end
if not tonumber(surfix) then
return surfix,rtn:sub(1,-2)
else
return '',rtn
end
end
return Production

View File

@@ -1,9 +1,12 @@
--[[-- Control Module - Reports
- Adds a way to report players and store report messages.
@module Reports
@control Reports
@alias Reports
@usage
-- import the module from the control modules
local Reports = require 'modules.control.reports'
-- This will place a report on "MrBiter" (must be a valid player) the report will have been made
-- by "Cooldude2606" (must be the player name) with the reason 'Liking biters too much' this can be
-- seen by using Reports.get_report.
@@ -21,7 +24,11 @@
]]
--- Allows getting player from any value
-- @dep utils.game
local Game = require 'utils.game'
--- Allows storing of data in global table
-- @dep utils.global
local Global = require 'utils.global'
local valid_player = Game.get_player_from_any
@@ -49,7 +56,7 @@ Global.register(user_reports,function(tbl)
user_reports = tbl
end)
--- Get functions.
--- Getters.
-- Functions used to get information from reports
-- @section get-functions
@@ -112,7 +119,7 @@ function Reports.count_reports(player,custom_count)
end
end
--- Set functions.
--- Setters.
-- Functions used to get information from reports
-- @section set-functions

View File

@@ -1,9 +1,12 @@
--[[-- Control Module - Warnings
- Adds a way to give and remove warnings to players.
@module Warnings
@control Warnings
@alias Warnings
@usage
-- import the module from the control modules
local Warnings = require 'modules.control.warnings'
-- This will add a warning to the player
Warnings.add_warning('MrBiter','Cooldude2606','Killed too many biters')
@@ -18,9 +21,17 @@
Warnings.clear_warnings('MrBiter','Cooldude2606')
]]
--- Allows registering of custom events
-- @dep utils.event
local Event = require 'utils.event'
--- Allows getting player from any value
-- @dep utils.game
local Game = require 'utils.game'
--- Allows storing in the global table
-- @dep utils.global
local Global = require 'utils.global'
--- Config file for this module
-- @dep config.warnings
local config = require 'config.warnings'
local valid_player = Game.get_player_from_any

View File

@@ -1,53 +1,13 @@
--- Adds a science info gui that shows production usage and net for the different science packs as well as an eta
local Gui = require 'expcore.gui'
local Event = require 'utils.event'
local Colors = require 'resources.color_presets'
local format_time = ext_require('expcore.common','format_time')
local format_number = ext_require('util','format_number')
local config = require 'config.science'
local Production = require 'modules.control.production'
local null_time_short = {'science-info.eta-time',format_time(0,{hours=true,minutes=true,seconds=true,time=true,null=true})}
local null_time_long = format_time(0,{hours=true,minutes=true,seconds=true,long=true,null=true})
--- Gets the production stats for a certain science pack
local function get_production_stats(player,science_pack)
local force = player.force
local stats = force.item_production_statistics
local total_made = stats.get_input_count(science_pack)
local total_used = stats.get_output_count(science_pack)
local minute_made = stats.get_flow_count{
name=science_pack,
input=true,
precision_index=defines.flow_precision_index.one_minute,
}
local minute_used = stats.get_flow_count{
name=science_pack,
input=false,
precision_index=defines.flow_precision_index.one_minute,
}
return {
total_made=total_made,
total_used=total_used,
total_net=total_made-total_used,
minute_made=minute_made,
minute_used=minute_used,
minute_net=minute_made-minute_used
}
end
--- Gets the font colour for a certain level of production
local function get_font_colour(value,secondary)
if value > config.required_for_green then
return Colors.light_green
elseif value < config.required_for_red then
return Colors.indian_red
elseif secondary and secondary > 0 or not secondary and value ~= 0 then
return Colors.orange
else
return Colors.grey
end
end
--[[ Generates the main structure for the gui
element
> container
@@ -60,7 +20,7 @@ end
>>> eta
>>>> label
]]
local function generate_container(player,element)
local function generate_container(element)
Gui.set_padding(element,1,2,2,2)
element.style.minimal_width = 200
@@ -137,21 +97,8 @@ end
> spm-"name"
]]
local function add_data_label(element,name,value,secondary,tooltip)
local data_colour = get_font_colour(value,secondary)
local caption = format_number(math.round(value,1),true)
local surfix = caption:sub(-1)
if not tonumber(surfix) then
caption = caption:sub(1,-2)
else
surfix = ''
end
if value > 0 then
caption = '+'..caption
elseif value == 0 and caption:sub(1,1) == '-' then
caption = caption:sub(2)
end
local data_colour = Production.get_color(config.color_clamp, value, secondary)
local surfix,caption = Production.format_number(value)
if element[name] then
local data = element[name].label
@@ -197,16 +144,18 @@ end
> net-"science_pack" (add_data_label)
]]
local function generate_science_pack(player,element,science_pack)
local stats = get_production_stats(player,science_pack)
if stats.total_made > 0 then
local total = Production.get_production_total(player.force, science_pack)
local minute = Production.get_production(player.force, science_pack, defines.flow_precision_index.one_minute)
if total.made > 0 then
element.parent.non_made.visible = false
local icon_style = 'quick_bar_slot_button'
if stats.minute_net > config.required_for_green then
local flux = Production.get_fluctuations(player.force, science_pack, defines.flow_precision_index.one_minute)
if flux.net > -config.color_flux/2 then
icon_style = 'green_slot_button'
elseif stats.minute_net < config.required_for_red then
elseif flux.net < -config.color_flux then
icon_style = 'red_slot_button'
elseif stats.minute_made > 0 then
elseif minute.made > 0 then
icon_style = 'selected_slot_button'
end
@@ -257,9 +206,9 @@ local function generate_science_pack(player,element,science_pack)
Gui.set_padding(delta_table)
end
add_data_label(delta.table,'pos-'..science_pack,stats.minute_made,nil,{'science-info.pos-tooltip',stats.total_made})
add_data_label(delta.table,'neg-'..science_pack,-stats.minute_used,nil,{'science-info.neg-tooltip',stats.total_used})
add_data_label(element,'net-'..science_pack,stats.minute_net,stats.minute_made+stats.minute_used,{'science-info.net-tooltip',stats.total_net})
add_data_label(delta.table,'pos-'..science_pack,minute.made,nil,{'science-info.pos-tooltip',total.made})
add_data_label(delta.table,'neg-'..science_pack,-minute.used,nil,{'science-info.neg-tooltip',total.used})
add_data_label(element,'net-'..science_pack,minute.net,minute.made+minute.used,{'science-info.net-tooltip',total.net})
end
end
@@ -281,18 +230,9 @@ local function update_eta(player,element)
for _,ingredient in pairs(research.research_unit_ingredients) do
local pack_name = ingredient.name
local required = ingredient.amount * remaining
local consumed = stats.get_flow_count{
name=pack_name,
input=false,
precision_index=defines.flow_precision_index.one_minute,
}
if consumed == 0 then
limit = -1
break
end
local minutes = required / consumed
if not limit or limit < minutes then
limit = minutes
local time = Production.get_consumsion_eta(force, pack_name, defines.flow_precision_index.one_minute, required)
if not limit or limit < time then
limit = time
end
end
@@ -301,9 +241,8 @@ local function update_eta(player,element)
element.tooltip = null_time_long
else
local ticks = limit*3600
element.caption = {'science-info.eta-time',format_time(ticks,{hours=true,minutes=true,seconds=true,time=true})}
element.tooltip = format_time(ticks,{hours=true,minutes=true,seconds=true,long=true})
element.caption = {'science-info.eta-time',format_time(limit,{hours=true,minutes=true,seconds=true,time=true})}
element.tooltip = format_time(limit,{hours=true,minutes=true,seconds=true,long=true})
end
end
@@ -316,7 +255,7 @@ Gui.new_left_frame('gui/science-info')
:set_direction('vertical')
:set_tooltip{'science-info.main-tooltip'}
:on_creation(function(player,element)
local table, eta = generate_container(player,element)
local table, eta = generate_container(element)
for _,science_pack in ipairs(config) do
generate_science_pack(player,table,science_pack)