1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/custom_commands.lua

623 lines
20 KiB
Lua
Raw Normal View History

local Task = require 'utils.Task'
local Event = require 'utils.event'
local Token = require 'utils.global_token'
2018-06-04 17:35:18 +02:00
local UserGroups = require 'user_groups'
local Utils = require 'utils.utils'
local Antigrief = require 'antigrief'
function player_print(str)
if game.player then
game.player.print(str)
else
log(str)
end
end
2017-07-06 19:37:57 +02:00
function cant_run(name)
player_print("Can't run command (" .. name .. ') - insufficient permission.')
2017-07-06 19:37:57 +02:00
end
2017-09-03 12:29:19 +02:00
local function invoke(cmd)
2018-05-23 19:29:35 +02:00
if not (game.player and game.player.admin) then
2017-07-06 19:37:57 +02:00
cant_run(cmd.name)
return
end
local target = cmd['parameter']
2017-07-08 20:58:45 +02:00
if target == nil or game.players[target] == nil then
player_print('Unknown player.')
2017-07-08 20:58:45 +02:00
return
2017-07-06 19:37:57 +02:00
end
local pos = game.player.surface.find_non_colliding_position('player', game.player.position, 0, 1)
game.players[target].teleport({pos.x, pos.y}, game.player.surface)
game.print(target .. ', get your ass over here!')
2017-07-06 19:37:57 +02:00
end
2017-09-03 12:29:19 +02:00
local function teleport_player(cmd)
2018-05-23 19:29:35 +02:00
if not (game.player and game.player.admin) then
2017-07-06 19:37:57 +02:00
cant_run(cmd.name)
return
end
local target = cmd['parameter']
2017-07-08 20:58:45 +02:00
if target == nil or game.players[target] == nil then
player_print('Unknown player.')
2017-07-08 20:58:45 +02:00
return
2017-07-06 19:37:57 +02:00
end
local surface = game.players[target].surface
local pos = surface.find_non_colliding_position('player', game.players[target].position, 0, 1)
game.player.teleport(pos, surface)
2017-07-06 19:37:57 +02:00
game.print(target .. "! watcha doin'?!")
end
2017-09-03 12:29:19 +02:00
local function teleport_location(cmd)
2018-05-23 19:29:35 +02:00
if not (game.player and game.player.admin) then
2017-07-06 19:37:57 +02:00
cant_run(cmd.name)
return
end
if game.player.selected == nil then
player_print('Nothing selected.')
2017-07-06 19:37:57 +02:00
return
end
local pos = game.player.surface.find_non_colliding_position('player', game.player.selected.position, 0, 1)
game.player.teleport(pos)
2017-07-06 19:37:57 +02:00
end
2017-09-03 12:29:19 +02:00
local function kill()
if game.player and game.player.character then
game.player.character.die()
end
2017-07-08 20:58:45 +02:00
end
2017-07-06 19:37:57 +02:00
global.walking = {}
local custom_commands_return_player =
Token.register(
function(args)
2018-06-07 22:06:48 +02:00
local player = args.player
if not player.valid then
return
end
global.walking[player.index] = false
player.character.destroy()
local character = args.character
if character ~= nil and character.valid then
2018-06-07 22:06:48 +02:00
player.character = character
else
2018-06-07 22:06:48 +02:00
player.create_character()
player.teleport(args.position)
end
2018-06-07 22:06:48 +02:00
player.force = args.force
game.print(args.player.name .. ' came back from his walkabout.')
end
)
2017-09-03 12:29:19 +02:00
local function walkabout(cmd)
2018-06-04 17:35:18 +02:00
if game.player and not game.player.admin then
cant_run(cmd.name)
return
end
local params = {}
if cmd.parameter == nil then
2018-06-07 22:06:48 +02:00
player_print('Walkabout failed, check /help walkabout.')
return
end
for param in string.gmatch(cmd.parameter, '%S+') do
table.insert(params, param)
end
local player_name = params[1]
local duration = 60
if #params > 2 then
player_print('Walkabout failed, check /help walkabout.')
return
elseif #params == 2 and tonumber(params[2]) == nil then
player_print(params[2] .. ' is not a number.')
return
elseif #params == 2 and tonumber(params[2]) then
duration = tonumber(params[2])
end
if duration < 15 then
duration = 15
end
local player = game.players[player_name]
2018-06-07 22:06:48 +02:00
if player == nil or not player.valid or global.walking[player.index] then
player_print(player_name .. ' could not go on a walkabout.')
return
end
local chunks = {}
for chunk in player.surface.get_chunks() do
table.insert(chunks, chunk)
end
2018-06-07 22:06:48 +02:00
local surface = player.surface
local chunk = surface.get_random_chunk()
local pos = {x = chunk.x * 32, y = chunk.y * 32}
2018-06-07 22:06:48 +02:00
local non_colliding_pos = surface.find_non_colliding_position('player', pos, 100, 1)
if non_colliding_pos then
game.print(player_name .. ' went on a walkabout, to find himself.')
Task.set_timeout(
duration,
2018-06-04 18:21:03 +02:00
custom_commands_return_player,
2018-06-07 22:06:48 +02:00
{
player = player,
force = player.force,
position = {x = player.position.x, y = player.position.y},
character = player.character
}
)
player.character = nil
player.create_character()
player.teleport(non_colliding_pos)
2018-06-07 22:06:48 +02:00
player.force = 'neutral'
global.walking[player.index] = true
else
player_print('Walkabout failed: count find non colliding position')
end
2017-07-08 23:06:39 +02:00
end
2017-07-06 19:37:57 +02:00
2017-07-09 15:20:01 +02:00
local function regular(cmd)
2018-06-04 17:35:18 +02:00
if game.player and not game.player.admin then
cant_run(cmd.name)
return
end
if cmd.parameter == nil then
player_print('Command failed. Usage: /regular <promote, demote>, <player>')
return
end
local params = {}
for param in string.gmatch(cmd.parameter, '%S+') do
table.insert(params, param)
end
if params[2] == nil then
player_print('Command failed. Usage: /regular <promote, demote>, <player>')
return
elseif (params[1] == 'promote') then
2018-06-04 17:35:18 +02:00
UserGroups.add_regular(params[2])
elseif (params[1] == 'demote') then
2018-06-04 17:35:18 +02:00
UserGroups.remove_regular(params[2])
else
player_print('Command failed. Usage: /regular <promote, demote>, <player>')
end
2017-07-09 15:20:01 +02:00
end
2017-07-18 22:53:53 +02:00
local function afk()
for _, v in pairs(game.players) do
if v.afk_time > 300 then
local time = ' '
if v.afk_time > 21600 then
time = time .. math.floor(v.afk_time / 216000) .. ' hours '
end
if v.afk_time > 3600 then
time = time .. math.floor(v.afk_time / 3600) % 60 .. ' minutes and '
end
time = time .. math.floor(v.afk_time / 60) % 60 .. ' seconds.'
player_print(v.name .. ' has been afk for' .. time)
end
end
2017-07-18 22:53:53 +02:00
end
2017-07-25 01:20:59 +02:00
local function follow(cmd)
if not game.player then
log("<Server can't do that.")
return
end
if cmd.parameter ~= nil and game.players[cmd.parameter] ~= nil then
global.follows[game.player.name] = cmd.parameter
global.follows.n_entries = global.follows.n_entries + 1
else
player_print('Usage: /follow <player> makes you follow the player. Use /unfollow to stop following a player.')
end
2017-07-25 01:20:59 +02:00
end
local function unfollow(cmd)
if not game.player then
log("<Server can't do that.")
return
end
if global.follows[game.player.name] ~= nil then
global.follows[game.player.name] = nil
global.follows.n_entries = global.follows.n_entries - 1
end
2017-07-25 01:20:59 +02:00
end
2017-08-19 07:41:58 +02:00
global.tp_players = {}
2017-08-18 03:04:29 +02:00
local function built_entity(event)
local index = event.player_index
2017-08-18 03:04:29 +02:00
if global.tp_players[index] then
local entity = event.created_entity
2017-08-18 03:04:29 +02:00
2018-06-04 17:35:18 +02:00
if not entity or not entity.valid or entity.type ~= 'entity-ghost' then
return
end
game.players[index].teleport(entity.position)
entity.destroy()
end
2017-08-18 03:04:29 +02:00
end
2018-04-06 21:58:50 +02:00
Event.add(defines.events.on_built_entity, built_entity)
2017-08-19 07:41:58 +02:00
2017-08-26 13:32:49 +02:00
local function toggle_tp_mode(cmd)
2018-06-04 17:35:18 +02:00
if not (game.player and game.player.admin) then
cant_run(cmd.name)
return
end
local index = game.player.index
local toggled = global.tp_players[index]
if toggled then
global.tp_players[index] = nil
player_print('tp mode is now off')
else
global.tp_players[index] = true
player_print('tp mode is now on - place a ghost entity to teleport there.')
end
2017-08-18 03:04:29 +02:00
end
global.old_force = {}
global.force_toggle_init = true
local function forcetoggle(cmd)
2018-06-04 17:35:18 +02:00
if not (game.player and game.player.admin and game.player.character) then
cant_run(cmd.name)
return
end
if global.force_toggle_init then
game.forces.enemy.research_all_technologies() --avoids losing logstics slot configuration
global.force_toggle_init = false
end
-- save the logistics slots
local slots = {}
local slot_counts = game.player.character.request_slot_count
if game.player.character.request_slot_count > 0 then
for i = 1, slot_counts do
2018-06-04 18:21:03 +02:00
local slot = game.player.character.get_request_slot(i)
if slot ~= nil then
table.insert(slots, slot)
2017-09-30 18:40:06 +02:00
end
end
end
2018-06-04 17:35:18 +02:00
if game.player.force.name == 'enemy' then
local old_force = global.old_force[game.player.name]
if not old_force then
game.player.force = 'player'
game.player.print("You're are now on the player force.")
else
if game.forces[old_force] then
game.player.force = old_force
else
game.player.force = 'player'
end
end
else
2018-06-04 17:35:18 +02:00
--Put roboports into inventory
2018-06-04 18:21:03 +02:00
local inv = game.player.get_inventory(defines.inventory.player_armor)
if inv[1].valid_for_read then
local name = inv[1].name
if name:match('power') or name:match('modular') then
local equips = inv[1].grid.equipment
for _, equip in pairs(equips) do
if
equip.name == 'personal-roboport-equipment' or equip.name == 'personal-roboport-mk2-equipment' or
equip.name == 'personal-laser-defense-equipment'
then
if game.player.insert {name = equip.name} == 0 then
game.player.surface.spill_item_stack(game.player.position, {name = equip.name})
end
inv[1].grid.take(equip)
end
end
end
end
global.old_force[game.player.name] = game.player.force.name
game.player.force = 'enemy'
2017-10-15 15:57:25 +02:00
end
game.player.print('You are now on the ' .. game.player.force.name .. ' force.')
2017-10-06 00:43:30 +02:00
-- Attempt to rebuild the request slots
if game.player.character.request_slot_count > 0 then
for _, slot in ipairs(slots) do
game.player.character.set_request_slot(slot, _)
end
end
end
2018-06-04 18:21:03 +02:00
local function get_group()
local group = game.permissions.get_group('Banned')
if not group then
game.permissions.create_group('Banned')
group = game.permissions.get_group('Banned')
if group then
for i = 2, 174 do
group.set_allows_action(i, false)
end
else
game.print(
'This would have nearly crashed the server, please consult the next best scenario dev (valansch or TWLtriston).'
)
end
end
2018-06-04 18:21:03 +02:00
return group
end
function custom_commands_untempban(param)
game.print(param.name .. ' is out of timeout.')
game.permissions.get_group('Default').add_player(param.name)
2017-10-06 00:43:30 +02:00
end
local custom_commands_untempban =
Token.register(
function(param)
game.print(param.name .. ' is out of timeout.')
game.permissions.get_group('Default').add_player(param.name)
end
)
2017-11-12 16:49:28 +02:00
local function tempban(cmd)
2018-06-04 18:21:03 +02:00
if (not game.player) or not game.player.admin then
cant_run(cmd.name)
return
end
if cmd.parameter == nil then
player_print('Tempban failed. Usage: /tempban <player> <minutes> Temporarily bans a player.')
return
end
local params = {}
for param in string.gmatch(cmd.parameter, '%S+') do
table.insert(params, param)
end
if #params < 2 or not tonumber(params[2]) then
player_print('Tempban failed. Usage: /tempban <player> <minutes> Temporarily bans a player.')
return
end
if not game.players[params[1]] then
player_print("Player doesn't exist.")
return
end
local group = get_group()
2018-06-04 18:21:03 +02:00
game.print(Utils.get_actor() .. ' put ' .. params[1] .. ' in timeout for ' .. params[2] .. ' minutes.')
if group then
group.add_player(params[1])
if not tonumber(cmd.parameter) then
Task.set_timeout(60 * tonumber(params[2]), custom_commands_untempban, {name = params[1]})
end
end
2017-11-12 16:49:28 +02:00
end
local custom_commands_replace_ghosts =
Token.register(
function(param)
for _, ghost in pairs(param.ghosts) do
local new_ghost =
game.surfaces[param.surface_index].create_entity {
name = 'entity-ghost',
position = ghost.position,
inner_name = ghost.ghost_name,
expires = false,
force = 'enemy',
direction = ghost.direction
}
new_ghost.last_user = ghost.last_user
end
end
)
2017-11-12 16:49:28 +02:00
local function spyshot(cmd)
if not cmd then
return 0
end
local player_name = cmd.parameter
if player_name and game.players[player_name] then
for _, spy in pairs(global.spys) do
if game.players[spy] and game.players[spy].connected then
local pos = game.players[player_name].position
2018-06-04 18:21:03 +02:00
local pseudo_ghosts = {}
for _, ghost in pairs(
game.players[player_name].surface.find_entities_filtered {
area = {{pos.x - 60, pos.y - 35}, {pos.x + 60, pos.y + 35}},
name = 'entity-ghost',
2018-06-04 18:21:03 +02:00
force = 'enemy'
}
) do
local pseudo_ghost = {
position = ghost.position,
ghost_name = ghost.ghost_name,
expires = false,
force = 'enemy',
direction = ghost.direction,
last_user = ghost.last_user
}
table.insert(pseudo_ghosts, pseudo_ghost)
ghost.destroy()
end
game.take_screenshot {
by_player = spy,
position = pos,
show_gui = false,
show_entity_info = true,
resolution = {1920, 1080},
anti_alias = true,
zoom = 0.5,
path = 'spyshot.png'
}
game.players[spy].print('You just took a screenshot!')
Task.set_timeout(
2,
custom_commands_replace_ghosts,
{ghosts = pseudo_ghosts, surface_index = game.players[player_name].surface.index}
) --delay replacements for the screenshot to render
return
end
2017-11-12 16:49:28 +02:00
end
player_print('No spy online!')
2017-11-12 16:49:28 +02:00
end
end
2017-11-12 20:43:51 +02:00
local function zoom(cmd)
if game.player and cmd and cmd.parameter and tonumber(cmd.parameter) then
game.player.zoom = tonumber(cmd.parameter)
end
2017-11-12 20:43:51 +02:00
end
local function pool()
if game.player and game.player.admin then
local t = {}
2018-06-04 18:21:03 +02:00
local p = game.player.position
for x = p.x - 3, p.x + 3 do
for y = p.y + 2, p.y + 7 do
table.insert(t, {name = 'water', position = {x, y}})
end
end
game.player.surface.set_tiles(t)
game.player.surface.create_entity {name = 'fish', position = {p.x + 0.5, p.y + 5}}
end
end
global.undo_warned_players = {}
local function undo(cmd)
2018-06-07 22:06:48 +02:00
if (not game.player) or not game.player.admin then
cant_run(cmd.name)
return
end
if cmd.parameter and game.players[cmd.parameter] then
if
not global.undo_warned_players[game.player.index] or
global.undo_warned_players[game.player.index] ~= game.players[cmd.parameter].index
then
global.undo_warned_players[game.player.index] = game.players[cmd.parameter].index
game.player.print(
string.format(
'Warning! You are about to remove %s entities and restore %s entities.',
#Utils.find_entities_by_last_user(game.players[cmd.parameter], game.surfaces.nauvis),
Antigrief.count_removed_entities(game.players[cmd.parameter])
)
)
game.player.print('To execute the command please run it again.')
return
end
Antigrief.undo(game.players[cmd.parameter])
game.print(string.format('Undoing everything %s did...', cmd.parameter))
global.undo_warned_players[game.player.index] = nil
else
player_print('Usage: /undo <player>')
end
end
local function antigrief_surface_tp()
2018-06-07 22:06:48 +02:00
if (not game.player) or not game.player.admin then
cant_run(cmd.name)
return
end
Antigrief.antigrief_surface_tp()
end
2018-06-08 00:50:45 +02:00
local function find_player(cmd)
local player = game.player
if not player then
return
end
local name = cmd.parameter
if not name then
player.print('Usage: /find-player <player>')
return
end
local target = game.players[name]
if not target then
player.print('player ' .. name .. ' not found')
return
end
target = target.character
if not target or not target.valid then
player.print('player ' .. name .. ' does not have a character')
return
end
player.add_custom_alert(target, {type = 'virtual', name = 'signal-F'}, player.name, true)
end
2018-05-22 02:03:04 +02:00
if not _DEBUG then
local old_add_command = commands.add_command
commands.add_command =
function(name, desc, func)
old_add_command(
name,
desc,
function(cmd)
local success, error = pcall(func, cmd)
if not success then
log(error)
player_print(error)
end
end
)
end
2018-05-07 14:16:41 +02:00
end
commands.add_command('kill', 'Will kill you.', kill)
2018-06-04 17:35:18 +02:00
commands.add_command('tpplayer', '<player> - Teleports you to the player. (Admins only)', teleport_player)
commands.add_command('invoke', '<player> - Teleports the player to you. (Admins only)', invoke)
commands.add_command('tppos', 'Teleports you to a selected entity. (Admins only)', teleport_location)
2018-06-04 17:35:18 +02:00
commands.add_command('walkabout', '<player> <duration> - Send someone on a walk. (Admins only)', walkabout)
commands.add_command('regulars', 'Prints a list of game regulars.', UserGroups.print_regulars)
commands.add_command('regular', '<promote, demote>, <player> Change regular status of a player. (Admins only)', regular)
commands.add_command('afk', 'Shows how long players have been afk.', afk)
2018-06-07 22:06:48 +02:00
commands.add_command(
'follow',
'<player> makes you follow the player. Use /unfollow to stop following a player.',
follow
)
commands.add_command('unfollow', 'stops following a player.', unfollow)
2018-06-07 22:06:48 +02:00
commands.add_command(
'tpmode',
'Toggles tp mode. When on place a ghost entity to teleport there (Admins only)',
toggle_tp_mode
)
2018-06-04 17:35:18 +02:00
commands.add_command('forcetoggle', 'Toggles the players force between player and enemy (Admins only)', forcetoggle)
commands.add_command('tempban', '<player> <minutes> Temporarily bans a player (Admins only)', tempban)
2018-06-07 22:06:48 +02:00
commands.add_command(
'spyshot',
'<player> Sends a screenshot of player to discord. (If a host is online. If no host is online, you can become one yourself. Ask on discord :))',
spyshot
)
commands.add_command('zoom', '<number> Sets your zoom.', zoom)
2018-06-07 22:06:48 +02:00
commands.add_command(
'all-tech',
'researches all technologies',
function()
if game.player and game.player.admin then
game.player.force.research_all_technologies()
end
end
)
commands.add_command(
'hax',
'Toggles your hax',
function()
if game.player and game.player.admin then
game.player.cheat_mode = not game.player.cheat_mode
end
end
)
commands.add_command('pool', 'Spawns a pool', pool)
commands.add_command('undo', '<player> undoes everything a player has done (Admins only)', undo)
2018-06-07 22:06:48 +02:00
commands.add_command(
'antigrief_surface',
'moves you to the antigrief surface or back (Admins only)',
antigrief_surface_tp
)
2018-06-08 00:50:45 +02:00
commands.add_command('find-player', '<player> shows an alert on the map where the player is located', find_player)