Cleaned up use of the Game module

This commit is contained in:
Cooldude2606
2020-08-14 01:54:51 +01:00
parent 86370aeada
commit e0eba193a4
14 changed files with 69 additions and 89 deletions

View File

@@ -1,64 +1,39 @@
local Global = require 'utils.global' --- @dep utils.global
local Color = require 'utils.color_presets' --- @dep utils.color_presets
local pairs = pairs
local Color = require 'utils.color_presets' --- @dep utils.color_presets
local Game = {}
local bad_name_players = {}
Global.register(
bad_name_players,
function(tbl)
bad_name_players = tbl
end
)
--[[ Note to readers
Game.get_player_from_name was removed because game.players[name] works without any edge cases
always true: game.players[name].name == name
Game.get_player_by_index was added originally as a workaround for the following edge case:
player with index of 5 and name of "Cooldude2606"
player with index of 10 and name of "5"
game.players[5].name == "5"
Discovered the following logic:
all keys are first converted to string and search against player names
if this fails it attempts to convert it to a number and search against player indexes
sometimes fails: game.players[index].index == index
Game.get_player_by_index was removed after the above logic was corrected to the following:
when a key is a number it is searched against player indexes, and only their indexes
when a key is a string it is searched against player names, and then against their indexes
always true: game.players[name].name == name; game.players[index].index == index
--[[
Due to a bug in the Factorio api the following expression isn't guaranteed to be true.
game.players[player.index] == player
get_player_by_index(index) will always return the correct player.
When looking up players by name or iterating through all players use game.players instead.
]]
function Game.get_player_by_index(index)
local p = game.players[index]
if not p then
return nil
end
if p.index == index then
return p
end
p = bad_name_players[index]
if p then
if p.valid then
return p
else
return nil
end
end
for k, v in pairs(game.players) do
if k == index then
bad_name_players[index] = v
return v
end
end
end
--- Returns a valid LuaPlayer if given a number, string, or LuaPlayer. Returns nil otherwise.
-- obj <number|string|LuaPlayer>
function Game.get_player_from_any(obj)
local o_type = type(obj)
local p
if o_type == 'number' then
p = Game.get_player_by_index(obj)
elseif o_type == 'string' then
local o_type, p = type(obj)
if o_type == 'table' then
p = obj
elseif o_type == 'string' or o_type == 'number' then
p = game.players[obj]
elseif o_type == 'table' and obj.valid and obj.is_player() then
return obj
end
if p and p.valid then
if p and p.valid and p.is_player() then
return p
end
end
@@ -95,16 +70,18 @@ function Game.print_floating_text(surface, position, text, color)
end
--[[
Creates a floating text entity at the player location with the specified color in {r, g, b} format.
Creates a floating text entity at the player location with the specified color and offset.
Example: "+10 iron" or "-10 coins"
@param text String to display
@param color table in {r = 0~1, g = 0~1, b = 0~1}, defaults to white.
@param x_offset number the x offset for the floating text
@param y_offset number the y offset for the floating text
@return the created entity
]]
function Game.print_player_floating_text_position(player_index, text, color, x_offset, y_offset)
local player = Game.get_player_by_index(player_index)
function Game.print_player_floating_text_position(player, text, color, x_offset, y_offset)
player = Game.get_player_from_any(player)
if not player or not player.valid then
return
end
@@ -113,8 +90,17 @@ function Game.print_player_floating_text_position(player_index, text, color, x_o
return Game.print_floating_text(player.surface, {x = position.x + x_offset, y = position.y + y_offset}, text, color)
end
function Game.print_player_floating_text(player_index, text, color)
Game.print_player_floating_text_position(player_index, text, color, 0, -1.5)
--[[
Creates a floating text entity at the player location with the specified color in {r, g, b} format.
Example: "+10 iron" or "-10 coins"
@param text String to display
@param color table in {r = 0~1, g = 0~1, b = 0~1}, defaults to white.
@return the created entity
]]
function Game.print_player_floating_text(player, text, color)
Game.print_player_floating_text_position(player, text, color, 0, -1.5)
end
return Game
return Game