-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpathing.lua
575 lines (495 loc) · 15.9 KB
/
pathing.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
require "/scripts/util.lua"
require "/scripts/rect.lua"
require "/scripts/pathutil.lua"
--------------------------------------------------------------------------------
function moveX(direction, run)
mcontroller.controlMove(direction, run)
end
PathFinder = {}
function PathFinder:new(options)
newFinder = {
edges = {},
options = options,
stuckTimer = 0
}
setmetatable(newFinder, extend(self))
return newFinder
end
function PathFinder:find(targetPosition)
if not self:canPathfind() then
return "pathfinding"
end
if not self.hasPath and not self.aStar then
if self.options.mustEndOnGround and not validStandingPosition(targetPosition, false) then
return false
end
self:reset()
self:start(mcontroller.position(), targetPosition)
end
return self:explore()
end
function PathFinder:start(sourcePosition, targetPosition)
self.target = targetPosition
local baseParameters = mcontroller.baseParameters()
local jumpSpeed = baseParameters.airJumpProfile.jumpSpeed
jumpSpeed = jumpSpeed + (jumpSpeed * status.stat("jumpModifier"))
baseParameters.airJumpProfile.jumpSpeed = jumpSpeed
self.aStar = world.platformerPathStart(sourcePosition, self.target, baseParameters, self.options)
end
function PathFinder:canPathfind()
return mcontroller.onGround() or not mcontroller.baseParameters().gravityEnabled
end
function PathFinder:exploreRate()
local fidelityOptions = {
minimum = 25,
low = 50,
medium = 100,
high = 150
}
if world.fidelity then
return fidelityOptions[world.fidelity()]
else
return fidelityOptions.high
end
end
function PathFinder:explore()
local result = self.aStar:explore(self:exploreRate())
if result == true and self:canPathfind() then
self.edges = self.aStar:result()
self.hasPath = true
self.currentEdgeIndex = 1
self.lastEdgeIndex = 1
self.stuckTimer = 0
self.aStar = nil
return true
elseif result == false then
self.aStar = nil
return false
end
util.debugText("Looking for path", vec2.add(mcontroller.position(), {0, -1}), "yellow")
return "pathfinding"
end
function PathFinder:reset()
self.edges = {}
self.hasPath = false
self.currentEdgeIndex = 1
end
function PathFinder:currentEdge()
return self.edges[self.currentEdgeIndex]
end
function PathFinder:lookAhead(i)
return self.edges[self.currentEdgeIndex + i]
end
function PathFinder:update(targetPosition)
-- Reset path if it's been stuck on the same node for a bit
if self.hasPath and self.stuckTimer > 0.5 then
self:reset()
end
self.stuckTimer = self.stuckTimer + script.updateDt()
--Replace current path
if self.target and world.magnitude(targetPosition, self.target) > 2 then
--self:start(mcontroller.position(), targetPosition)
self:reset()
end
-- Find a new path if none exists
if not self.hasPath then
local findProgress = self:find(targetPosition)
if findProgress == true then
return "running"
end
return findProgress
end
if self.aStar then
if self:explore() == false then
return false
end
end
if self.currentEdgeIndex > #self.edges then
self:reset()
return false
end
if self.hasPath then
if self.currentEdgeIndex ~= self.lastEdgeIndex then
self.stuckTimer = 0
self.lastEdgeIndex = self.currentEdgeIndex
end
return "running" -- Still have a path, therefore we've not arrived yet.
else
return true -- Arrived at the destination
end
end
function PathFinder:advance()
self.currentEdgeIndex = self.currentEdgeIndex + 1
end
PathMover = {}
function PathMover:new(options)
options = options or {}
local newPather = {}
local pathOptions = applyDefaults(options.pathOptions or {}, {
returnBest = false,
mustEndOnGround = mcontroller.baseParameters().gravityEnabled,
maxDistance = 200,
swimCost = 5,
dropCost = 2,
boundBox = mcontroller.boundBox(),
droppingBoundBox = padBoundBox(0.2, 0), --Wider bound box for dropping
standingBoundBox = padBoundBox(-0.7, 0), --Thinner bound box for standing and landing
smallJumpMultiplier = 1 / math.sqrt(2), -- 0.5 multiplier to jump height
jumpDropXMultiplier = 1,
enableWalkSpeedJumps = true,
enableVerticalJumpAirControl = false,
maxFScore = 400,
maxNodesToSearch = 70000,
maxLandingVelocity = -10.0,
liquidJumpCost = 15
})
newPather.finder = PathFinder:new(pathOptions)
newPather.options = applyDefaults(options, {
run = false,
movementParameters = {}
})
newPather.deltaX = mcontroller.facingDirection()
newPather.onGround = mcontroller.onGround()
newPather.position = mcontroller.position()
newPather.lastPosition = newPather.position
newPather.boundBox = mcontroller.boundBox()
newPather.canOpenDoors = config.getParameter("pathing.canOpenDoors", false)
newPather.forceWalkingBackwards = config.getParameter("pathing.forceWalkingBackwards", false)
setmetatable(newPather, extend(self))
return newPather
end
function PathMover:move(targetPosition, dt)
local run = self.options.run
if self.forceWalkingBackwards then
if run == true then run = mcontroller.movingDirection() == mcontroller.facingDirection() end
end
self.lastPosition = self.position
self.position = mcontroller.position()
self.run = run
self.targetPosition = targetPosition
self.targetDistance = world.magnitude(targetPosition, self.position)
self.toTarget = world.distance(targetPosition, self.position)
self.dt = dt
if self.jumpCooldown then
self.jumpCooldown = self.jumpCooldown - dt
if self.jumpCooldown <= 0 then self.jumpCooldown = nil end
end
self.onGround = mcontroller.onGround()
if self.onGround and self.targetDistance < 2 and math.abs(self.toTarget[2]) < 1 then
return self:approachTargetPosition()
end
if self.downHoldTimer ~= nil then
self:keepDropping(dt)
return "running"
end
local result = self.finder:update(self.targetPosition)
if result ~= "running" then
return result
end
debugPath(self.finder)
self.controlParameters = copy(self.options.movementParameters)
local result = self:edgeMove()
mcontroller.controlParameters(self.controlParameters)
return result
end
function PathMover:approachTargetPosition()
if math.abs(self.toTarget[1]) < self:tickMoveDistance() and math.abs(self.toTarget[2]) < 1 then
mcontroller.setXVelocity(0)
return true
else
moveX(self.toTarget[1], self.run)
return "running"
end
end
function PathMover:advancePath()
self.finder:advance()
self:updateEdge()
end
function PathMover:tickMoveDistance()
return 0.5
end
function PathMover:updateEdge()
self.edge = self.finder:currentEdge()
if self.edge then
self.nextPathPosition = self.edge.target.position
self.action = self.edge.action
self.delta = world.distance(self.nextPathPosition, self.position)
end
end
function PathMover:edgeMove()
setMoved(true)
self:updateEdge()
if self.edge == nil then return true end
if not self:openDoors() then
return false
end
if self.action == "Swim" then
return self:moveSwim()
elseif self.action == "Jump" then
return self:moveJump()
elseif self.action == "Drop" then
return self:moveDrop()
elseif self.action == "Arc" then
return self:moveArc()
elseif self.action == "Land" then
return self:moveLand()
elseif self.action == "Walk" then
return self:moveWalk()
elseif self.action == "Fly" then
return self:moveFly()
end
end
function PathMover:openDoors()
local bounds = rect.translate(mcontroller.boundBox(), mcontroller.position())
if util.toDirection(self.delta[1]) > 0 then
bounds[1] = bounds[3]
bounds[3] = bounds[3] + 1
else
bounds[3] = bounds[1]
bounds[1] = bounds[1] - 1
end
if world.rectTileCollision(bounds, {"Dynamic"}) then
-- There is a colliding object in the way. See if we can open it
local closedDoorIds = world.entityQuery(rect.ll(bounds), rect.ur(bounds), { includedTypes = {"object"}, callScript = "hasCapability", callScriptArgs = { "closedDoor" } })
if #closedDoorIds == 0 or not self.canOpenDoors then
return false
else
for _, closedDoorId in pairs(closedDoorIds) do
if self.options.openDoorCallback == nil or self.options.openDoorCallback(closedDoorId) then
world.callScriptedEntity(closedDoorId, "openDoor")
end
end
end
end
return true
end
function PathMover:timedDrop(time)
if holdTime == nil then holdTime = 0 end
holdTime = math.min(holdTime, 0.5)
mcontroller.controlDown()
self.downHoldTimer = holdTime
end
function PathMover:keepDropping(dt)
if self.downHoldTimer ~= nil then
mcontroller.controlDown()
self.downHoldTimer = self.downHoldTimer - dt
if self.downHoldTimer <= 0 or mcontroller.onGround() then
self.downHoldTimer = nil
end
end
end
function PathMover:moveSwim()
self.deltaX = self.delta[1]
mcontroller.controlApproachVelocity(vec2.mul(vec2.norm(self.delta), mcontroller.baseParameters().walkSpeed), mcontroller.baseParameters().liquidJumpProfile.jumpControlForce)
if passedTarget(self.edge) then
self:advancePath()
end
return "running"
end
function PathMover:moveJump()
if mcontroller.onGround() and self.jumpCooldown then
return "running"
end
if world.magnitude(mcontroller.position(), self.edge.source.position) < 1.0 then
if not self.jumpTimer then
self.jumpTimer = 0.2
mcontroller.setPosition(self.edge.source.position)
mcontroller.setVelocity({0,0})
end
self.deltaX = self.edge.jumpVelocity[1]
if mcontroller.liquidMovement() or self.jumpTimer <= 0 then
--Try to not get slowed down by friction
self.controlParameters.airFriction = 0
self.controlParameters.liquidFriction = 0
self.controlParameters.liquidImpedance = 0
self.controlParameters.groundFriction = 0
--Approach the jump position more precisely to follow the jump arc accurately
mcontroller.setVelocity({self.edge.jumpVelocity[1], self.edge.jumpVelocity[2]})
self.jumpTimer = nil
self:advancePath()
else
self.jumpTimer = self.jumpTimer - script.updateDt()
end
end
return "running"
end
function PathMover:moveDrop()
if math.abs(self.delta[1]) > self:tickMoveDistance() then
moveX(self.delta[1], false)
return "running"
end
mcontroller.setPosition({self.nextPathPosition[1], self.position[2]})
self:timedDrop(math.max(timeToFall(-self.delta[2]), 0.05))
mcontroller.setXVelocity(0)
self:advancePath()
return "running"
end
function PathMover:moveArc()
self.jumped = false
self.jumpCooldown = 0.3
-- Advance path if the target position is in a different direction than the arc is pointing
while self.edge and self.edge.action == "Arc" do
if passedTarget(self.edge) then
self:advancePath()
else
break
end
end
if not self.edge or self.edge.action ~= "Arc" then return "running" end
if mcontroller.onGround() and not mcontroller.liquidMovement() then
local nextEdge = self.finder:lookAhead(1) or {}
if nextEdge.action and nextEdge.action ~= "Arc" then
self.arcDelta = nil
self:advancePath()
else
self.arcDelta = self.arcDelta or self.delta[1]
moveX(self.arcDelta, run)
self.deltaX = self.arcDelta
end
return "running"
else
self.arcDelta = nil
--Try to not get slowed down by friction
self.controlParameters.airFriction = 0
self.controlParameters.liquidFriction = 0
self.controlParameters.liquidImpedance = 0
self.controlParameters.groundFriction = 0
local velocity = self.edge.source.velocity or self.edge.target.velocity or {0,0}
-- setXVelocity in case that changes mid-jump (e.g. when jumping straight
-- up and then to the side).
mcontroller.controlApproachXVelocity(velocity[1], mcontroller.baseParameters().groundForce)
if mcontroller.liquidMovement() then
if velocity[2] ~= 0 then
mcontroller.controlApproachYVelocity(velocity[2], mcontroller.baseParameters().airJumpProfile.jumpControlForce)
else
self:advancePath()
end
end
return "running"
end
end
function PathMover:moveLand()
if (mcontroller.onGround() or (mcontroller.liquidMovement() and math.abs(self.delta[2]) < 1)) and math.abs(self.delta[1]) < 1 then
self:advancePath()
mcontroller.controlApproachXVelocity(0, mcontroller.baseParameters().groundForce)
end
return "running"
end
function PathMover:moveWalk()
while self.edge and self.edge.action == "Walk" do
if passedTarget(self.edge) then
if self.edge.target.velocity then
mcontroller.setVelocity(self.edge.target.velocity)
end
self:advancePath()
else
break
end
end
if self.edge and self.edge.action == "Walk" then
local run = self.run
for i = 1, 5 do
if run then break end
local edge = self.finder:lookAhead(i)
if edge and edge.action == "Walk" then
if edge.target.velocity and math.abs(edge.target.velocity[1]) > mcontroller.baseParameters().walkSpeed then
run = true
end
else
break
end
end
local edgeDelta = world.distance(self.edge.target.position, self.edge.source.position)
moveX(edgeDelta[1], run)
self.deltaX = self.delta[1]
end
return "running"
end
function PathMover:moveFly()
while self.edge and self.edge.action == "Fly" do
if passedTarget(self.edge) then
self:advancePath()
else
break
end
end
if self.edge and self.edge.action == "Fly" then
mcontroller.controlFly(self.delta)
self.deltaX = self.delta[1]
end
return "running"
end
function passedTargetOnAxis(edge, axis)
local edgeDistance = world.distance(edge.target.position, edge.source.position)
local targetDistance = world.distance(edge.target.position, mcontroller.position())
return edgeDistance[axis] ~= 0 and edgeDistance[axis] * targetDistance[axis] < 0
end
function passedTarget(edge)
return passedTargetOnAxis(edge, 1) or passedTargetOnAxis(edge, 2)
end
--JUMPING AND DROPPING--
--------------------------------------------------------------------------------
--Necessary to avoid conflicts between Starbound's 'self' and Lua's 'self' tables
--TODO: Refactor 'self' to a better name, someday...
function setMoved(moved)
self.moved = moved
end
--DEBUG---------------------------------------------
function debugPathEdgeColor(edge)
local action = edge.action
if action == "Walk" then
return "blue"
elseif action == "Jump" then
return "green"
elseif action == "Drop" then
return "cyan"
elseif action == "Swim" then
return "white"
elseif action == "Fly" then
return "magenta"
elseif action == "Land" then
return "yellow"
elseif action == "Arc" then
if edge.target.velocity and edge.target.velocity[2] == 0 then
return "yellow"
end
return "red"
else
return "red"
end
end
function debugPath(pathfinder)
if not self.debug then return end
local position = mcontroller.position()
local step = 0
local prevStep = position
while true do
local edge = pathfinder:lookAhead(step)
if edge then
local nextStep = edge.target.position
local color = debugPathEdgeColor(edge)
world.debugLine(prevStep, nextStep, color)
if edge.action == "Jump" then
world.debugText(edge.jumpVelocity[2], {nextStep[1], nextStep[2]-1}, "red")
end
if edge.action == "Land" then
local bounds = padBoundBox(-0.2, 0)
bounds = {bounds[1] + nextStep[1],bounds[2] + nextStep[2],bounds[3] + nextStep[1],bounds[4] + nextStep[2]}
util.debugRect(bounds, "yellow")
end
if edge.action == "Drop" then
local bounds = padBoundBox(0.2, 0)
bounds = {bounds[1] + prevStep[1],bounds[2] + prevStep[2],bounds[3] + prevStep[1],bounds[4] + prevStep[2]}
util.debugRect(bounds, "yellow")
end
-- if edge.action == "Arc" then
-- world.debugLine(nextStep, vec2.add(nextStep, edge.source.velocity or edge.target.velocity or {0,0}), "yellow")
-- end
world.debugPoint(nextStep, color)
prevStep = nextStep
step = step + 1
else
break
end
end
end