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'
|
2018-11-18 21:03:33 +02:00
|
|
|
|
|
|
|
global.walking = {}
|
|
|
|
global.walking_storage = {}
|
|
|
|
|
|
|
|
--- Returns a player from walkabout after the timeout.
|
|
|
|
-- If the player is no longer logged in, store their info
|
|
|
|
-- in the global table walking_storage
|
2019-01-02 17:34:17 +02:00
|
|
|
local redmew_commands_return_player =
|
2018-11-18 21:03:33 +02:00
|
|
|
Token.register(
|
|
|
|
function(args)
|
|
|
|
local player = args.player
|
|
|
|
if not player.valid then
|
|
|
|
return
|
|
|
|
end
|
2018-11-19 20:35:21 +02:00
|
|
|
local index = player.index
|
2018-11-18 21:03:33 +02:00
|
|
|
-- If walking is false it means they got cleaned by clean_on_join
|
|
|
|
if global.walking[index] == false then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If the player is no longer connected, store that information.
|
|
|
|
if player.connected then
|
|
|
|
global.walking[index] = false
|
|
|
|
|
|
|
|
local walkabout_character = player.character
|
|
|
|
if walkabout_character and walkabout_character.valid then
|
|
|
|
walkabout_character.destroy()
|
|
|
|
end
|
|
|
|
|
|
|
|
local character = args.character
|
|
|
|
if character ~= nil and character.valid then
|
|
|
|
player.character = character
|
|
|
|
else
|
|
|
|
player.create_character()
|
|
|
|
player.teleport(args.position)
|
|
|
|
end
|
|
|
|
|
|
|
|
player.force = args.force
|
|
|
|
|
|
|
|
game.print(args.player.name .. ' came back from his walkabout.')
|
|
|
|
else
|
|
|
|
local data = {index = index, character = args.character, force = args.force, position = args.position}
|
|
|
|
table.insert(global.walking_storage, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
--- Sends a player on a walkabout:
|
|
|
|
-- They are teleported far away, placed on a neutral force, and are given a new character.
|
2019-01-02 17:34:17 +02:00
|
|
|
-- They are turned after the timeout by redmew_commands_return_player
|
|
|
|
local function walkabout(args)
|
|
|
|
local player_name = args.player
|
|
|
|
|
|
|
|
local duration = tonumber(args.duration)
|
|
|
|
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]
|
2018-11-28 01:23:40 +02:00
|
|
|
if not player or not player.character or global.walking[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.')
|
|
|
|
Task.set_timeout(
|
|
|
|
duration,
|
2019-01-02 17:34:17 +02:00
|
|
|
redmew_commands_return_player,
|
2018-11-18 21:03:33 +02:00
|
|
|
{
|
|
|
|
player = player,
|
|
|
|
force = player.force,
|
|
|
|
position = {x = player.position.x, y = player.position.y},
|
|
|
|
character = character
|
|
|
|
}
|
|
|
|
)
|
|
|
|
player.character = nil
|
|
|
|
player.create_character()
|
|
|
|
player.teleport(non_colliding_pos)
|
|
|
|
player.force = 'neutral'
|
|
|
|
global.walking[player.index] = true
|
|
|
|
else
|
|
|
|
Game.player_print('Walkabout failed: could not find non colliding position')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Cleans the walkabout status off players who disconnected during walkabout.
|
|
|
|
-- Restores their original force, character, and position.
|
2018-11-20 12:46:19 +02:00
|
|
|
local function clean_on_join(event)
|
2018-11-18 21:03:33 +02:00
|
|
|
local player = Game.get_player_by_index(event.player_index)
|
|
|
|
local index = player.index
|
|
|
|
if global.walking[index] then
|
|
|
|
global.walking[index] = false
|
2018-11-18 21:07:03 +02:00
|
|
|
local walking_storage = global.walking_storage
|
2018-11-20 12:46:19 +02:00
|
|
|
for _, s in pairs(walking_storage) do
|
2018-11-18 21:03:33 +02:00
|
|
|
if s.index == index then
|
|
|
|
local walkabout_character = player.character
|
|
|
|
if walkabout_character and walkabout_character.valid then
|
|
|
|
walkabout_character.destroy()
|
|
|
|
end
|
|
|
|
|
|
|
|
local character = s.character
|
|
|
|
if character ~= nil and character.valid then
|
|
|
|
player.character = character
|
|
|
|
else
|
|
|
|
player.create_character()
|
|
|
|
player.teleport(s.position)
|
|
|
|
end
|
|
|
|
|
|
|
|
player.force = s.force
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Event.add(defines.events.on_player_joined_game, clean_on_join)
|
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},
|
|
|
|
admin_only = true,
|
|
|
|
allowed_by_server = true
|
|
|
|
},
|
|
|
|
walkabout
|
|
|
|
)
|