7
7
import FFX_memory
8
8
import FFX_Logs
9
9
from math import copysign
10
+ import numpy as np
10
11
11
12
FFXC = FFX_Xbox .FFXC
12
13
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
+
13
56
def engage ():
14
57
print ("Start egg hunt" )
15
58
startTime = time .time ()
@@ -77,9 +120,27 @@ def engage():
77
120
else :
78
121
#print("Movement happening.")
79
122
#target = [-70,-70]
123
+ oldTarget = target
80
124
player = FFX_memory .getCoords ()
125
+ iceArray = FFX_memory .buildIcicles ()
81
126
(forward , right ) = FFX_memory .getMovementVectors ()
82
127
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
+
83
144
# Calculate forward and right directions relative to camera space
84
145
pX = player [0 ]
85
146
pY = player [1 ]
@@ -106,15 +167,14 @@ def engage():
106
167
107
168
FFXC .set_value ('AxisLx' , Lx )
108
169
FFXC .set_value ('AxisLy' , Ly )
109
-
170
+ target = oldTarget
110
171
111
172
#camCount += 1
112
173
#print(camCount)
113
174
#if camCount % 20 == 0:
114
175
# FFX_Logs.writePlot("TEST")
115
176
116
177
117
-
118
178
#Now if we're close, we want to slow down a bit.
119
179
if activeEgg != 99 and eggArray [activeEgg ].distance < 15 and eggArray [activeEgg ].eggLife < 130 :
120
180
time .sleep (0.15 )
0 commit comments