From 9fab174d98d7abd85648ae986c4956e33ce0063a Mon Sep 17 00:00:00 2001 From: Valansch Date: Tue, 25 Jul 2017 01:20:59 +0200 Subject: [PATCH 1/3] implemented /follow.lua --- control.lua | 1 + custom_commands.lua | 22 ++++++++++++++++ follow.lua | 64 +++++++++++++++++++++++++++++++++++++++++++++ on_tick.lua | 1 + 4 files changed, 88 insertions(+) create mode 100644 follow.lua diff --git a/control.lua b/control.lua index d930d818..f9142bbb 100644 --- a/control.lua +++ b/control.lua @@ -17,6 +17,7 @@ require "custom_commands" require "nuke_control" require "walk_distance" require "on_tick" +require "follow" diff --git a/custom_commands.lua b/custom_commands.lua index f5a43ebb..9b1413be 100644 --- a/custom_commands.lua +++ b/custom_commands.lua @@ -281,6 +281,26 @@ 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 + if game.player.selected ~= nil and game.player.selected.type == "player" then + global.follows[game.player.name] = game.player.selected.name + global.follows.n_entries = global.follows.n_entries + 1 + else + game.player.print('usage: /follow makes you follow the player. You can also select a player instead of typing his username. Use /unfollow to stop following a player.') + end + 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 +318,5 @@ commands.add_command("mods", 'Prints a list of game mods.', print_mods) commands.add_command("mod", ', 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", ' Sets a players tag. (Admins only)', tag) +commands.add_command("follow", ' makes you follow the player. You can also select a player instead of typing his username. Use /unfollow to stop following a player.', follow) +commands.add_command("unfollow", 'stops following a player.', unfollow) diff --git a/follow.lua b/follow.lua new file mode 100644 index 00000000..a222e205 --- /dev/null +++ b/follow.lua @@ -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 diff --git a/on_tick.lua b/on_tick.lua index 20caf787..8d7db0b3 100644 --- a/on_tick.lua +++ b/on_tick.lua @@ -1,4 +1,5 @@ local function on_tick() + walk_on_tick() if game.tick % 60 == 0 then poll_on_second() walk_distance_on_second() From 89fdc94dfc11bb6cccc03ac721be89cb025034b5 Mon Sep 17 00:00:00 2001 From: Valansch Date: Thu, 27 Jul 2017 11:10:29 +0200 Subject: [PATCH 2/3] removed /follow selected_player because it doesnt work --- custom_commands.lua | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/custom_commands.lua b/custom_commands.lua index 9b1413be..59cfbb73 100644 --- a/custom_commands.lua +++ b/custom_commands.lua @@ -285,12 +285,6 @@ 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 - if game.player.selected ~= nil and game.player.selected.type == "player" then - global.follows[game.player.name] = game.player.selected.name - global.follows.n_entries = global.follows.n_entries + 1 - else - game.player.print('usage: /follow makes you follow the player. You can also select a player instead of typing his username. Use /unfollow to stop following a player.') end end end @@ -318,5 +312,5 @@ commands.add_command("mods", 'Prints a list of game mods.', print_mods) commands.add_command("mod", ', 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", ' Sets a players tag. (Admins only)', tag) -commands.add_command("follow", ' makes you follow the player. You can also select a player instead of typing his username. Use /unfollow to stop following a player.', follow) +commands.add_command("follow", ' makes you follow the player. Use /unfollow to stop following a player.', follow) commands.add_command("unfollow", 'stops following a player.', unfollow) From 189f7a79b5bf4ba7e4fceb1cffdfad8298379c5a Mon Sep 17 00:00:00 2001 From: Valansch Date: Thu, 27 Jul 2017 11:18:40 +0200 Subject: [PATCH 3/3] removed /follow because it doesnt work yet --- custom_commands.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_commands.lua b/custom_commands.lua index 59cfbb73..1e7eaa6f 100644 --- a/custom_commands.lua +++ b/custom_commands.lua @@ -285,7 +285,8 @@ 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 - end + else + game.player.print(" makes you follow the player. Use /unfollow to stop following a player.") end end