2019-01-04 22:02:55 +02:00
|
|
|
local Task = require 'utils.task'
|
2018-11-20 12:46:19 +02:00
|
|
|
local Game = require 'utils.game'
|
|
|
|
local Event = require 'utils.event'
|
2018-11-26 03:07:03 +02:00
|
|
|
local Token = require 'utils.token'
|
2019-01-02 17:34:17 +02:00
|
|
|
local Command = require 'utils.command'
|
2019-01-23 17:51:35 +02:00
|
|
|
local Global = require 'utils.global'
|
2019-01-30 05:52:43 +02:00
|
|
|
local Ranks = require 'resources.ranks'
|
2018-11-18 21:03:33 +02:00
|
|
|
|
2019-01-31 01:36:57 +02:00
|
|
|
local Public = {}
|
2019-01-31 01:53:58 +02:00
|
|
|
local return_player
|
2019-01-31 01:36:57 +02:00
|
|
|
|
2019-01-23 17:51:35 +02:00
|
|
|
-- Register our globals
|
|
|
|
local walkabouts = {}
|
2019-01-31 01:52:35 +02:00
|
|
|
local primitives = {
|
|
|
|
event_registered = nil
|
|
|
|
}
|
2019-01-23 17:51:35 +02:00
|
|
|
|
|
|
|
Global.register(
|
|
|
|
{
|
2019-01-31 01:52:35 +02:00
|
|
|
walkabouts = walkabouts,
|
|
|
|
primitives = primitives
|
2019-01-23 17:51:35 +02:00
|
|
|
},
|
|
|
|
function(tbl)
|
|
|
|
walkabouts = tbl.walkabout
|
2019-01-31 01:52:35 +02:00
|
|
|
primitives = tbl.primitives
|
2019-01-23 17:51:35 +02:00
|
|
|
end
|
|
|
|
)
|
2019-01-21 18:23:25 +02:00
|
|
|
|
2019-01-31 01:53:58 +02:00
|
|
|
--- Cleans the walkabout status off players who disconnected during walkabout.
|
|
|
|
local on_join_token =
|
|
|
|
Token.register(
|
|
|
|
function(event)
|
|
|
|
local player = Game.get_player_by_index(event.player_index)
|
|
|
|
local index = player.index
|
|
|
|
|
|
|
|
-- If a player joins and they're marked as being on walkabout but their timer has expired, restore them.
|
|
|
|
if player and walkabouts[index] and walkabouts[index].timer_expired then
|
|
|
|
return_player(index)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
--- Returns a player from walkabout after the timeout.
|
|
|
|
local redmew_commands_return_player =
|
|
|
|
Token.register(
|
|
|
|
function(player)
|
|
|
|
if not player or not player.valid then
|
|
|
|
log('Warning: redmew_commands_return_player received nil or invalid player')
|
|
|
|
return
|
|
|
|
end
|
2019-01-31 01:52:35 +02:00
|
|
|
|
2019-01-31 01:53:58 +02:00
|
|
|
local index = player.index
|
|
|
|
|
|
|
|
-- If the player is no longer connected, store that fact so we can clean them when/if they rejoin.
|
|
|
|
if player.connected then
|
|
|
|
return_player(index)
|
|
|
|
else
|
|
|
|
walkabouts[index].timer_expired = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
return_player = function(index)
|
2019-01-23 17:51:35 +02:00
|
|
|
local data = walkabouts[index]
|
2019-01-21 18:23:25 +02:00
|
|
|
if not data then
|
|
|
|
log('Warning: return_player received nil data')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local player = Game.get_player_by_index(index)
|
|
|
|
if not player or not player.valid then
|
|
|
|
log('Warning: return_player received nil or invalid player')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local walkabout_character = player.character
|
|
|
|
if walkabout_character and walkabout_character.valid then
|
|
|
|
walkabout_character.destroy()
|
|
|
|
end
|
|
|
|
|
|
|
|
local character = data.character
|
|
|
|
if character and character.valid then
|
|
|
|
player.character = character
|
2019-01-31 01:52:35 +02:00
|
|
|
player.character.destructible = true
|
2019-01-21 18:23:25 +02:00
|
|
|
else
|
|
|
|
player.create_character()
|
|
|
|
player.teleport(data.position)
|
|
|
|
end
|
|
|
|
player.force = data.force
|
|
|
|
|
|
|
|
game.print(data.player.name .. ' came back from walkabout.')
|
2019-01-23 17:51:35 +02:00
|
|
|
walkabouts[index] = nil
|
2019-01-31 01:52:35 +02:00
|
|
|
|
|
|
|
if #walkabouts == 0 then
|
|
|
|
--TODO unreg event
|
|
|
|
Event.remove_removable(defines.events.on_player_joined_game, on_join_token)
|
|
|
|
end
|
2019-01-21 18:23:25 +02:00
|
|
|
end
|
2018-11-18 21:03:33 +02:00
|
|
|
|
|
|
|
--- Sends a player on a walkabout:
|
|
|
|
-- They are teleported far away, placed on a neutral force, and are given a new character.
|
2019-01-21 18:23:25 +02:00
|
|
|
-- They are returned after the timeout by redmew_commands_return_player
|
2019-01-02 17:34:17 +02:00
|
|
|
local function walkabout(args)
|
2019-01-31 01:36:57 +02:00
|
|
|
if global.config.walkabout.enabled then
|
|
|
|
Game.player_print('Walkabout is disabled via the config')
|
|
|
|
return
|
|
|
|
end
|
2019-01-02 17:34:17 +02:00
|
|
|
local player_name = args.player
|
|
|
|
local duration = tonumber(args.duration)
|
2019-01-21 18:23:25 +02:00
|
|
|
|
2019-01-02 17:34:17 +02:00
|
|
|
if not duration then
|
|
|
|
Game.player_print('Duration should be a number, player will be sent on walkabout for the default 60 seconds.')
|
|
|
|
duration = 60
|
2018-11-18 21:03:33 +02:00
|
|
|
end
|
2019-01-02 17:34:17 +02:00
|
|
|
|
2018-11-18 21:03:33 +02:00
|
|
|
if duration < 15 then
|
|
|
|
duration = 15
|
|
|
|
end
|
|
|
|
|
|
|
|
local player = game.players[player_name]
|
2019-01-23 17:51:35 +02:00
|
|
|
if not player or not player.character or walkabouts[player.index] then
|
2018-11-18 21:03:33 +02:00
|
|
|
Game.player_print(player_name .. ' could not go on a walkabout.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surface = player.surface
|
|
|
|
local chunk = surface.get_random_chunk()
|
|
|
|
local pos = {x = chunk.x * 32, y = chunk.y * 32}
|
|
|
|
local non_colliding_pos = surface.find_non_colliding_position('player', pos, 100, 1)
|
|
|
|
|
|
|
|
local character = player.character
|
|
|
|
if character and character.valid then
|
|
|
|
character.walking_state = {walking = false}
|
|
|
|
end
|
|
|
|
|
|
|
|
if non_colliding_pos then
|
|
|
|
game.print(player_name .. ' went on a walkabout, to find himself.')
|
2019-01-21 18:23:25 +02:00
|
|
|
|
|
|
|
-- Information about the player's former state so we can restore them later
|
|
|
|
local data = {
|
|
|
|
player = player,
|
|
|
|
force = player.force,
|
|
|
|
position = {x = player.position.x, y = player.position.y},
|
|
|
|
character = character
|
|
|
|
}
|
|
|
|
|
|
|
|
Task.set_timeout(duration, redmew_commands_return_player, player)
|
2019-01-23 17:51:35 +02:00
|
|
|
walkabouts[player.index] = data
|
2019-01-21 18:23:25 +02:00
|
|
|
|
2019-01-31 01:52:35 +02:00
|
|
|
if not primitives then
|
|
|
|
Event.add_removable(defines.events.on_player_joined_game, on_join_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
player.character.destructible = false
|
2018-11-18 21:03:33 +02:00
|
|
|
player.character = nil
|
|
|
|
player.create_character()
|
|
|
|
player.teleport(non_colliding_pos)
|
|
|
|
player.force = 'neutral'
|
|
|
|
else
|
2019-01-21 18:23:25 +02:00
|
|
|
Game.player_print('Walkabout failed: could not find non-colliding-position')
|
2018-11-18 21:03:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-31 01:36:57 +02:00
|
|
|
--- Checks if a player is on walkabout
|
|
|
|
-- @param player_index <number>
|
|
|
|
-- @return <boolean>
|
|
|
|
function Public.is_on_walkabout(player_index)
|
|
|
|
if walkabouts[player_index] then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-01-02 17:34:17 +02:00
|
|
|
Command.add(
|
|
|
|
'walkabout',
|
|
|
|
{
|
|
|
|
description = 'Send someone on a walk. Duration is in seconds.',
|
|
|
|
arguments = {'player', 'duration'},
|
|
|
|
default_values = {duration = 60},
|
2019-01-30 05:52:43 +02:00
|
|
|
required_rank = Ranks.admin,
|
2019-01-02 17:34:17 +02:00
|
|
|
allowed_by_server = true
|
|
|
|
},
|
|
|
|
walkabout
|
|
|
|
)
|
2019-01-31 01:36:57 +02:00
|
|
|
|
|
|
|
return Public
|