mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-12 10:04:40 +02:00
Merge branch 'master' into develop
This commit is contained in:
commit
faab85c865
@ -17,6 +17,7 @@ require "custom_commands"
|
||||
require "nuke_control"
|
||||
require "walk_distance"
|
||||
require "on_tick"
|
||||
require "follow"
|
||||
|
||||
|
||||
|
||||
|
@ -281,6 +281,21 @@ local function tag(cmd)
|
||||
end
|
||||
end
|
||||
|
||||
local function follow(cmd)
|
||||
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
|
||||
game.player.print("<player> makes you follow the player. Use /unfollow to stop following a player.")
|
||||
end
|
||||
end
|
||||
|
||||
local function unfollow(cmd)
|
||||
if global.follows[game.player.name] ~= nil then
|
||||
global.follows[game.player.name] = nil
|
||||
global.follows.n_entries = global.follows.n_entries - 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
commands.add_command("kill", "Will kill you.", kill)
|
||||
@ -298,3 +313,5 @@ commands.add_command("mods", 'Prints a list of game mods.', print_mods)
|
||||
commands.add_command("mod", '<promote, demote>, <player> Changes moderator status of a player. (Admins only)', mod)
|
||||
commands.add_command("afktime", 'Shows how long players have been afk.', afk)
|
||||
commands.add_command("tag", '<player> <tag> Sets a players tag. (Admins only)', tag)
|
||||
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)
|
||||
|
64
follow.lua
Normal file
64
follow.lua
Normal file
@ -0,0 +1,64 @@
|
||||
global.follows = {}
|
||||
global.follows.n_entries = 0
|
||||
|
||||
function get_direction(follower, target)
|
||||
local delta_x = target.position.x - follower.position.x
|
||||
local delta_y = follower.position.y - target.position.y --reversed x axis
|
||||
local a = delta_y/delta_x
|
||||
if a >= -1.5 and a < -0.5 then
|
||||
--SE OR NW
|
||||
if delta_x > 0 then
|
||||
return defines.direction.southeast
|
||||
else
|
||||
return defines.direction.northwest
|
||||
end
|
||||
elseif a >= -0.5 and a < 0.5 then
|
||||
--E OR W
|
||||
if delta_x > 0 then
|
||||
return defines.direction.east
|
||||
else
|
||||
return defines.direction.west
|
||||
end
|
||||
elseif a >= 0.5 and a < 1.5 then
|
||||
--NE OR SW
|
||||
if delta_x > 0 then
|
||||
return defines.direction.northeast
|
||||
else
|
||||
return defines.direction.southwest
|
||||
end
|
||||
else
|
||||
-- N or S
|
||||
if a < 0 then delta_x = - delta_x end -- mirrow x axis if player is NNW or SSE
|
||||
if delta_x > 0 then
|
||||
return defines.direction.north
|
||||
else
|
||||
return defines.direction.south
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function distance(player_1, player_2)
|
||||
local d_x = player_1.position.x - player_2.position.x
|
||||
local d_y = player_1.position.y - player_2.position.y
|
||||
return math.sqrt(d_x*d_x + d_y + d_y)
|
||||
end
|
||||
function walk_on_tick()
|
||||
if global.follows.n_entries > 0 then
|
||||
for k,v in pairs(global.follows) do
|
||||
local follower = game.players[k]
|
||||
local target = game.players[v]
|
||||
if follower ~= nil and target ~= nil then
|
||||
local d = distance(follower, target)
|
||||
if follower.connected and target.connected and d < 32 then
|
||||
if d > 5 then
|
||||
direction = get_direction(follower, target)
|
||||
follower.walking_state = {walking = true, direction = direction}
|
||||
end
|
||||
else
|
||||
global.follows[follower.name] = nil
|
||||
global.follows.n_entries = global.follows.n_entries - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,4 +1,5 @@
|
||||
local function on_tick()
|
||||
walk_on_tick()
|
||||
if game.tick % 60 == 0 then
|
||||
poll_on_second()
|
||||
walk_distance_on_second()
|
||||
|
Loading…
Reference in New Issue
Block a user