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

62 lines
1.7 KiB
Lua
Raw Normal View History

2017-07-25 01:20:59 +02:00
global.follows = {}
global.follows.n_entries = 0
2018-06-02 18:16:04 +02:00
local Utils = require "utils.utils"
2018-09-23 00:25:13 +02:00
local Game = require 'utils.game'
2017-07-25 01:20:59 +02:00
2018-11-06 13:55:52 +02:00
local function get_direction(follower, target)
2017-07-25 01:20:59 +02:00
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
2018-11-06 13:55:52 +02:00
local function walk_on_tick()
2017-07-25 01:20:59 +02:00
if global.follows.n_entries > 0 then
for k,v in pairs(global.follows) do
local follower = game.playesr[k]
local target = game.players[v]
2017-07-25 01:20:59 +02:00
if follower ~= nil and target ~= nil then
2018-06-02 18:16:04 +02:00
local d = Utils.distance(follower, target)
2017-07-25 01:20:59 +02:00
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