Module Data
Data stage helpers.
Module Status: Work in progress.
Usage:
local Data = require('__eradicators-library__/erlib/factorio/Data/!init')()
custom-input
SimpleCustomInput(name_prefix, name, key[, consuming=false]) | Creates a custom input for use with a custom event handler. |
SimpleLinkedInput(...) | Creates a custom input that always uses the same key as a base game input even if the user changes it. |
path
Data.Path.join(...) | Joins several partial paths into a single string. |
item
Data.Item.find_item_prototype(name) | Finds an item prototype by name without having to know the type. |
all prototypes
Inscribe(prototype) | Advanced prototype creation and forking. |
Prototype
Prototype.get(type, name) | Retrieves a prototype table reference from data.raw Raises an error if that prototype doesn't exist. |
Prototype.get_enabled(prototype) | Retrieves enabled status of a recipe, technology or custom-input. |
Prototype.set_enabled(prototype, normal_enabled, expensive_enabled) | Sets the enabled status on a recipe, technology or custom-input. |
Recipe
Recipe.copy_unlock_condition(source_name, target_names) | Copies the unlock conditions of one recipe to another. |
Sprite
Sprite.format_icon(file_path, options) | Extracts icon size and mipmaps from file name. |
Sprite.brute_scale(prototype, scale) | Changes scale and shift of all sub-tables. |
custom-input
- SimpleCustomInput(name_prefix, name, key[, consuming=false])
-
Creates a custom input for use with a custom event handler.
Note: Factorios key sequence detection is very strict:
- All letters must be capitalized, i.e
A
,B
,SPACE
,ENTER
. - Except for
mouse-
keys which use lower case, i.e.mouse-button-1
. - "+" plus must be surrounded by space.
- Modifiers are CONTROL, SHIFT and ALT.
- LCTRL, RCTRL, LSHIFT, RSHIFT, LALT and RALT can only be used standalone.
- Numpad keys start with
KP_
, i.e.KP_MULTIPLY
,KP_1
.
Parameters:
- name_prefix string Each mod should use a unique prototype name prefix to avoid incompatibilities with other mods.
- name string The name of every CustomInputPrototype is also an EventUID.
- key string The default key sequence. Can be changed by the player later. If you want an input to use the same key as a base game control use Data.SimpleLinkedInput instead.
- consuming boolean If true the input will block vanilla inputs with the same key sequence. (default false)
Returns:
-
CustomInputPrototype
A table reference. You do not need to call
data:extend
on this as it has already been added.See also:
Usage:
-- in data.lua Data.SimpleCustomInput('my-mod:', 'a-new-hotkey', 'CONTROL + N') -- in control.lua EventManager.new_handler { 'my-mod:a-new-hotkey', function(e) local player = game.players[e.player_index] player.print('You have pressed the hotkey!') end }
- All letters must be capitalized, i.e
- SimpleLinkedInput(...)
-
Creates a custom input that always uses the same key as a base game
input even if the user changes it.
The EventUID and prototype name are prefixed with
on_linked_input_
and-
dashes are replaced with_
underscores.Parameters:
- ... InputName Any number of input names.
Usage:
-- in data.lua Data.SimpleLinkedInput('rotate','toggle-map') -- in control.lua EventManager.new_handler { 'on_linked_input_rotate', function(e) local player = game.players[e.player_index] player.print('You have pressed the rotate button!') if player.selected then game.print('Your mouse is hovering over:'..player.selected.name) end end } EventManager.new_handler { 'on_linked_input_toggle_map', function(e) -- do something here end }
path
- Data.Path.join(...)
-
Joins several partial paths into a single string.
Parameters:
- ... string Any number of partial paths. They will be joined with '/' and any erroneously duplicated slashes will be removed.
Returns:
-
string
The joined path.
item
- Data.Item.find_item_prototype(name)
-
Finds an item prototype by name without having to know the type.
Supports all item types like
'ammo'
,'module'
, etc.Parameters:
- name string
Returns:
all prototypes
- Inscribe(prototype)
-
Advanced prototype creation and forking.
Processing order:
- Deep-copy the input table.
- Shallow merge with parent deep-copy.
- Adjust minable result (if applicable).
- Applymagic.post_patches
.
- Apply Table.remove_nil.Parameters:
- prototype A normal prototype table like you would pass to
data:extend{}
.- magic table A table of advanced inscription instructions. (optional)
- magic.parent_name string (optional)
- magic.parent_type string If name or type or both are given then prototype will be merged onto a deep copy of the parent before any further processing. (optional)
- magic.post_patches string A DenseArray of TablePatch, applied after forking via Table.patch. (optional)
Returns:
-
table
A reference to the prototype table after it was added to data.raw.
- prototype A normal prototype table like you would pass to
Prototype
Usage:
local Prototype = require('__eradicators-library__/erlib/factorio/Data/Prototype')()
- Prototype.get(type, name)
-
Retrieves a prototype table reference from data.raw
Raises an error if that prototype doesn't exist.
Parameters:
Returns:
-
table
prototype
- Prototype.get_enabled(prototype)
-
Retrieves enabled status of a recipe, technology or custom-input.
If neither normal nor expensive difficulty is explicitly defined
then inline data is treated as normal difficulty data.
Parameters:
- prototype table
Returns:
- Prototype.set_enabled(prototype, normal_enabled, expensive_enabled)
-
Sets the enabled status on a recipe, technology or custom-input.
If neither normal nor expensive difficulty is explicitly defined
then inline data is treated as normal difficulty data.
Parameters:
Recipe
Usage:
local Recipe = require('__eradicators-library__/erlib/factorio/Data/Recipe')()
- Recipe.copy_unlock_condition(source_name, target_names)
-
Copies the unlock conditions of one recipe to another.
This includes Prototype/Recipe.enabled for all defined difficulties
as well as the technology unlocks if there are any.
Parameters:
- source_name string
- target_names string or DenseArray
Sprite
Usage:
local Sprite = require('__eradicators-library__/erlib/factorio/Data/Sprite')()
- Sprite.format_icon(file_path, options)
-
Extracts icon size and mipmaps from file name.
Guesses the icon size from the last number. Optionally guesses the mipmap count from the last number prefixed with "mip".
Parameters:
Returns:
Usage:
print(Hydra.lines(Data.Sprite.format_icon('__mod__/my-icon-1_64²-mip2.png'))) > { > icon = "__mod__/my-icon-1_64²-mip2.png", > icon_mipmaps = 2, > icon_size = 64 > }
- Sprite.brute_scale(prototype, scale)
-
Changes scale and shift of all sub-tables.
Iterates into the given
prototype
and appliesscale
to the scale and shift of all seemingly appropriate sub-tables. As factorio prototype tables are not class-marked this uses a heuristic approach that may leave some useless scale and shift entries that usually won't do any damage.Should work for: Sprite, Animation, RotatedAnimation, etc..
Parameters:
Returns:
-
table
prototype