Skip to content

Commit c95e6bf

Browse files
committed
Initial version of icicle dodging
1 parent fca2ac6 commit c95e6bf

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

zz_eggHuntAuto.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,52 @@
77
import FFX_memory
88
import FFX_Logs
99
from math import copysign
10+
import numpy as np
1011

1112
FFXC = FFX_Xbox.FFXC
1213

14+
def lineSphereIntersect(start, end, circle, radius=11):
15+
numHits = 0
16+
hits = []
17+
18+
direction = end - start
19+
sphereToStart = start - circle
20+
a = np.dot(direction, direction)
21+
b = 2 * np.dot(sphereToStart, direction)
22+
c = np.dot(sphereToStart, sphereToStart) - radius**2
23+
d = b**2 - 4*a*c
24+
if d < 0: # no intersection
25+
return (numHits, hits)
26+
27+
d = np.sqrt(d)
28+
# Solve quadratic equation
29+
t1 = (-b -d)/(2*a)
30+
t2 = (-b +d)/(2*a)
31+
32+
if t1 >= 0 and t1 <= 1:
33+
numHits += 1
34+
hits.append(start + direction * t1)
35+
if t2 >= 0 and t2 <= 1:
36+
numHits += 1
37+
hits.append(start + direction * t2)
38+
return (numHits, hits)
39+
40+
def pathAround(player, circle, target, radius = 11):
41+
line = player - circle
42+
line /= np.linalg.norm(line) # normalize to length 1
43+
angle = np.arctan2(line[1],line[0])
44+
newAngle1 = angle + 0.5 * np.pi
45+
newAngle2 = angle - 0.5 * np.pi
46+
p1 = circle + [radius * np.cos(newAngle1), radius * np.sin(newAngle1)]
47+
p2 = circle + [radius * np.cos(newAngle2), radius * np.sin(newAngle2)]
48+
print(circle, p1, p2)
49+
# Find which of two possible points gives shortest path
50+
p1length = np.linalg.norm(p1 - player) + np.linalg.norm(target - p1)
51+
p2length = np.linalg.norm(p2 - player) + np.linalg.norm(target - p2)
52+
if p1length < p2length:
53+
return p1
54+
return p2
55+
1356
def engage():
1457
print("Start egg hunt")
1558
startTime = time.time()
@@ -77,9 +120,27 @@ def engage():
77120
else:
78121
#print("Movement happening.")
79122
#target = [-70,-70]
123+
oldTarget = target
80124
player = FFX_memory.getCoords()
125+
iceArray = FFX_memory.buildIcicles()
81126
(forward, right) = FFX_memory.getMovementVectors()
82127

128+
targetPos = np.array([target[0], target[1]])
129+
playerPos = np.array(player)
130+
131+
closestIntersect = 9999
132+
intersectPoint = []
133+
for icicle in iceArray:
134+
numIntersect, hits = lineSphereIntersect(playerPos, targetPos, np.array([icicle.x, icicle.y]))
135+
if numIntersect > 0:
136+
intersectDistance = (player[0] - hits[0][0])**2 + (player[1] - hits[0][1])**2
137+
if intersectDistance < closestIntersect:
138+
closestIntersect = intersectDistance
139+
intersectPoint = hits[0]
140+
141+
if closestIntersect < 9999:
142+
target = pathAround(playerPos, np.array(intersectPoint), targetPos)
143+
83144
# Calculate forward and right directions relative to camera space
84145
pX = player[0]
85146
pY = player[1]
@@ -106,15 +167,14 @@ def engage():
106167

107168
FFXC.set_value('AxisLx', Lx)
108169
FFXC.set_value('AxisLy', Ly)
109-
170+
target = oldTarget
110171

111172
#camCount += 1
112173
#print(camCount)
113174
#if camCount % 20 == 0:
114175
# FFX_Logs.writePlot("TEST")
115176

116177

117-
118178
#Now if we're close, we want to slow down a bit.
119179
if activeEgg != 99 and eggArray[activeEgg].distance < 15 and eggArray[activeEgg].eggLife < 130:
120180
time.sleep(0.15)

0 commit comments

Comments
 (0)