64 lines
2.1 KiB
Lua

--- The game module.
-- @module Game
-- @usage local Game = require('stdlib/game')
local Game = {_module_name = 'Game'}
setmetatable(Game, {__index = require('stdlib/core')})
--- Return a valid player object from event, index, string, or userdata
-- @tparam string|number|LuaPlayer|event mixed
-- @treturn LuaPlayer a valid player or nil
function Game.get_player(mixed)
if type(mixed) == 'table' then
if mixed.__self then
return mixed and mixed.valid and mixed
elseif mixed.player_index then
local player = game.players[mixed.player_index]
return player
end
elseif mixed then
local player = game.players[mixed]
return player
end
end
--- Return a valid force object from event, string, or userdata
-- @tparam string|LuaForce|event mixed
-- @treturn LuaForce a valid force or nil
function Game.get_force(mixed)
if type(mixed) == 'table' then
if mixed.__self then
return mixed and mixed.valid and mixed
elseif mixed.force then
return Game.get_force(mixed.force)
end
elseif type(mixed) == 'string' then
local force = game.forces[mixed]
return (force and force.valid) and force
end
end
--- Messages all players currently connected to the game.
--> Offline players are not counted as having received the message.
-- If no players exist msg is stored in the `global._print_queue` table.
-- @tparam string msg the message to send to players
-- @tparam[opt] ?|nil|boolean condition the condition to be true for a player to be messaged
-- @treturn uint the number of players who received the message.
function Game.print_all(msg, condition)
local num = 0
if #game.players > 0 then
for _, player in pairs(game.players) do
if condition == nil or select(2, pcall(condition, player)) then
player.print(msg)
num = num + 1
end
end
return num
else
global._print_queue = global._print_queue or {}
global._print_queue[#global._print_queue + 1] = msg
end
end
return Game