2018-09-23 00:24:47 +02:00
|
|
|
local Global = require 'utils.global'
|
2018-11-06 22:36:11 +02:00
|
|
|
local random = math.random
|
2018-09-23 00:24:47 +02:00
|
|
|
|
|
|
|
local Game = {}
|
|
|
|
|
2018-09-23 12:46:58 +02:00
|
|
|
local bad_name_players = {}
|
|
|
|
Global.register(
|
|
|
|
bad_name_players,
|
|
|
|
function(tbl)
|
|
|
|
bad_name_players = tbl
|
|
|
|
end
|
|
|
|
)
|
2018-09-23 00:24:47 +02:00
|
|
|
|
2018-09-23 12:46:58 +02:00
|
|
|
--[[
|
|
|
|
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)
|
2018-09-23 00:24:47 +02:00
|
|
|
local p = game.players[index]
|
2018-09-23 12:46:58 +02:00
|
|
|
|
2018-09-23 00:24:47 +02:00
|
|
|
if not p then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
if p.index == index then
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
|
2018-09-23 12:46:58 +02:00
|
|
|
p = bad_name_players[index]
|
|
|
|
if p then
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
|
2018-09-23 00:24:47 +02:00
|
|
|
for k, v in pairs(game.players) do
|
|
|
|
if k == index then
|
2018-09-23 12:46:58 +02:00
|
|
|
bad_name_players[index] = v
|
2018-09-23 00:24:47 +02:00
|
|
|
return v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-06 13:55:52 +02:00
|
|
|
function Game.player_print(str)
|
|
|
|
if game.player then
|
|
|
|
game.player.print(str)
|
|
|
|
else
|
|
|
|
print(str)
|
|
|
|
end
|
2018-11-10 01:54:18 +02:00
|
|
|
end
|
2018-11-10 00:49:23 +02:00
|
|
|
|
2018-11-06 22:36:11 +02:00
|
|
|
--[[
|
|
|
|
@param Position String to display at
|
|
|
|
@param text String to display
|
|
|
|
@param color table in {r = 0~1, g = 0~1, b = 0~1}, defaults to white.
|
|
|
|
@param surface LuaSurface
|
|
|
|
|
|
|
|
@return the created entity
|
|
|
|
]]
|
|
|
|
function Game.print_floating_text(surface, position, text, color)
|
|
|
|
color = color or {r = 1, g = 1, b = 1}
|
|
|
|
|
|
|
|
return surface.create_entity {
|
|
|
|
name = 'tutorial-flying-text',
|
|
|
|
color = color,
|
|
|
|
text = text,
|
|
|
|
position = position,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
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_index, text, color)
|
|
|
|
local player = Game.get_player_by_index(player_index)
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local position = player.position
|
|
|
|
return Game.print_floating_text(player.surface, {x = position.x, y = position.y - 1.5}, text, color)
|
2018-11-06 13:55:52 +02:00
|
|
|
end
|
|
|
|
|
2018-09-23 00:24:47 +02:00
|
|
|
return Game
|