-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.lua
176 lines (155 loc) · 5.25 KB
/
orders.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
local orders = {}
local vector = require"lib/vector"
function orders.move(dt, ship, data)
orders.dumbmove(dt, ship, data)
orders.smarterturn(dt, ship, data)
if ship.shape:testPoint(data.x, data.y) then
ship.order.func = nil
ship.order.data = nil
end
end
function orders.follow(dt, ship, data)
if ship ~= game.manualship then
local temp = {}
temp.x = data.body:getX()
temp.y = data.body:getY()
orders.movedist(dt, ship, temp) --Lazy workaround
orders.smarterturn(dt, ship, temp) --Next time: make it move away if too close
end
if data.isalive == false then
ship.order.func = nil
ship.order.data = nil
end
end
function orders.attack(dt, ship, data)
if ship ~= game.manualship then
local temp = {}
temp.x = data.body:getX()
temp.y = data.body:getY()
orders.movedist(dt, ship, temp) --Lazy workaround
orders.smarterturn(dt, ship, temp) --Next time: make it move away if too close
orders.fireon(dt, ship, temp)
end
if data.isalive == false then
ship.order.func = nil
ship.order.data = nil
end
end
function orders.fireon(dt, ship, data)
local angletolerance = math.pi/8
local current = vector(ship.body:getX(), ship.body:getY())
local dest = vector(data.x, data.y)
local diff = dest - current
local tang = math.atan2(diff.y, diff.x)
local dang = tang - ship.body:getAngle()
if dang <= angletolerance and dang >= -angletolerance then
ship:fire()
end
end
function orders.smarterturn(dt, ship, data)
local current = vector(ship.body:getX(), ship.body:getY())
local dest = vector(data.x, data.y)
local diff = dest - current
local tang = math.atan2(diff.y, diff.x)
local dang = tang - ship.body:getAngle()
if dang > math.pi then dang = dang - 2*math.pi end
if dang < -math.pi then dang = dang + 2*math.pi end
if dang > 0 then
ship.body:applyTorque(ship.torque*dt*math.abs(dang))
elseif dang < 0 then
ship.body:applyTorque(-ship.torque*dt*math.abs(dang))
end
end
function orders.dumbmove(dt, ship,data)
local angletolerance = math.pi/4
local current = vector(ship.body:getX(), ship.body:getY())
local dest = vector(data.x, data.y)
local diff = dest - current
local tang = math.atan2(diff.y, diff.x)
local dang = tang - ship.body:getAngle()
if dang > math.pi then dang = dang - 2*math.pi end
if dang < -math.pi then dang = dang + 2*math.pi end
local facex = math.cos(ship.body:getAngle())
local facey = math.sin(ship.body:getAngle())
if dang < angletolerance and dang > -angletolerance then --target is within tolerance.
ship.body:applyForce(facex*ship.thrust*dt, facey*ship.thrust*dt)
end
end
function orders.movedist(dt, ship, data)
local angletolerance = math.pi/4
local movedist = 100
local current = vector(ship.body:getX(), ship.body:getY())
local dest = vector(data.x, data.y)
local diff = dest - current
local tang = math.atan2(diff.y, diff.x)
local dang = tang - ship.body:getAngle()
if dang > math.pi then dang = dang - 2*math.pi end
if dang < -math.pi then dang = dang + 2*math.pi end
local facex = math.cos(ship.body:getAngle())
local facey = math.sin(ship.body:getAngle())
if dang < angletolerance and dang > -angletolerance then --target is within tolerance.
local ddist = diff:len() - movedist
local fracddist = math.abs(ddist/movedist)
if fracddist > 1 then fracddist = 1 end
if diff:len() > movedist then
ship.body:applyForce(facex*ship.thrust*fracddist*dt, facey*ship.thrust*fracddist*dt)
else
--ship.body:applyForce(-facex*ship.thrust*fracddist*dt, -facey*ship.thrust*fracddist*dt)
ship.body:applyForce(-facex*ship.thrust*dt, -facey*ship.thrust*dt)
end
end
end
--[[
function orders.teleport(dt, ship, data)
ship.body:setX(data.x)
ship.body:setY(data.y)
end
function orders.magicmove(dt, ship, data)
local speed = 50
local current = vector(ship.body:getX(), ship.body:getY())
local dest = vector(data.x, data.y)
local diff = dest - current
diff:normalize_inplace()
ship.body:setX(ship.body:getX() + diff.x*speed*dt)
ship.body:setY(ship.body:getY() + diff.y*speed*dt)
end
function orders.face(dt, ship, data)
local current = vector(ship.body:getX(), ship.body:getY())
local dest = vector(data.x, data.y)
local diff = dest - current
local tang = math.atan2(diff.y, diff.x)
ship.body:setAngle(tang)
end
function orders.magicturn(dt, ship, data)
local turnspeed = 1
local current = vector(ship.body:getX(), ship.body:getY())
local dest = vector(data.x, data.y)
local diff = dest - current
local tang = math.atan2(diff.y, diff.x)
local dang = tang - ship.body:getAngle()
if dang > math.pi then dang = dang - 2*math.pi end
if dang < -math.pi then dang = dang + 2*math.pi end
if math.abs(dang) <= turnspeed*dt then
ship.body:setAngle(tang)
elseif dang > 0 then
ship.body:setAngle(ship.body:getAngle() + turnspeed*dt)
elseif dang < 0 then
ship.body:setAngle(ship.body:getAngle() - turnspeed*dt)
end
end
function orders.dumbturn(dt, ship, data)
local current = vector(ship.body:getX(), ship.body:getY())
local dest = vector(data.x, data.y)
local diff = dest - current
local tang = math.atan2(diff.y, diff.x)
local dang = tang - ship.body:getAngle()
if dang > math.pi then dang = dang - 2*math.pi end
if dang < -math.pi then dang = dang + 2*math.pi end
if dang > 0 then
ship.body:applyTorque(ship.torque*dt)
elseif dang < 0 then
ship.body:applyTorque(-ship.torque*dt)
end
end
--]]
return orders