forked from Refactorio/RedMew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow.lua
61 lines (59 loc) · 1.7 KB
/
follow.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
global.follows = {}
global.follows.n_entries = 0
local Utils = require "utils.utils"
local Game = require 'utils.game'
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
function walk_on_tick()
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]
if follower ~= nil and target ~= nil then
local d = Utils.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