-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_final_ship.txt
More file actions
195 lines (164 loc) · 5.26 KB
/
Copy path03_final_ship.txt
File metadata and controls
195 lines (164 loc) · 5.26 KB
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
# ===== LAST TICK VARIABLES SAVER =====
function save_vars()
last_enemy_visible = enemy_visible
last_enemy_angle = enemy_angle
last_enemy_distance = enemy_distance
last_energy = energy
last_speed = speed
last_direction = direction
end
# ===== HELPER FUNCTIONS =====
# --- Function to have better prediction while shooting ---
function calculate_last_second_enemy_position()
# Increment the ticks counter
ticks_counter_2 += 1
# Every 50 ticks, update energy_last_second
if ticks_counter_2 == 50
last_second_enemy_angle = enemy_angle
last_second_enemy_distance = enemy_distance
ticks_counter_2 = 0 # Reset the counter for the next cycle
end
end
# --- Zig Zag Motion with Unpredictable Speed ---
function zig_zag_motion()
faster(5) # Maximum speed for evasion
if turn_amount == 0
turn_amount = rand(5, 30)
end
speed_change = rand(-1, 1)
if speed + speed_change <= 5 and speed + speed_change >= 0
faster(speed_change)
end
if turning <= 0
turning = rand(20, 50)
turn_amount *= -1
end
if turning > 0
right(turn_amount)
turning -= 1
end
end
# --- Follow spaceships and fire ---
function follow_and_fire()
# Constants
shot_speed = 10 # speed of the shot
# Calculate the delta values for angle and distance
delta_angle = enemy_angle - last_second_enemy_angle
delta_distance = enemy_distance - last_second_enemy_distance
delta_speed = speed - last_speed
# Make sure delta_angle is normalized between -90 and +90
if delta_angle > 180
delta_angle -= 360
else if delta_angle < -180
delta_angle += 360
end
# Estimate the enemy's current speed and direction
enemy_speed = sqrt(delta_distance * delta_distance + last_second_enemy_distance * last_second_enemy_distance - 2 * delta_distance * last_second_enemy_distance * cos(delta_angle))
enemy_direction_change = atan2(delta_distance * sin(delta_angle), last_second_enemy_distance + delta_distance * cos(delta_angle))
# Calculate the time it takes for a projectile to reach the enemy's current position
time_to_hit = enemy_distance / shot_speed
# Predict the enemy's future position considering their speed and direction change
predicted_enemy_angle = enemy_angle + delta_angle * time_to_hit + enemy_direction_change * time_to_hit
predicted_enemy_distance = enemy_distance + enemy_speed * time_to_hit
# Normalize predicted_enemy_angle to be between -180 and +180 for turning decisions
predicted_enemy_angle = predicted_enemy_angle % 360
# Determine the direction to turn based on the predicted angle
if predicted_enemy_angle >= 0
right(predicted_enemy_angle) # Enemy is predicted to be on the right
else
left(-predicted_enemy_angle) # Enemy is predicted to be on the left
end
# Adjust speed to maintain optimal distance
if speed < 3
faster(enemy_distance - 80)
else
faster(enemy_distance - 600)
end
# Fire if the enemy is within a reasonable distance and visible
if abs(enemy_angle) <= 10 and enemy_distance <= 300
fire
end
end
# --- Slow down and rotate ---
function slow_down_and_rotate()
if speed > 0
slower(1) # Slow down gradually
end
left(360) # Rotate in place
end
# --- Adjust speed after invisibility ---
function adjust_speed_after_invisibility()
if increasing_speed > 0
if speed < 5
faster(3) # Incrementally increase speed
left(360)
end
end
end
# --- Function to store energy in the last second ---
function manage_energy_with_ticks()
# Update current energy with the actual energy value from the game
current_energy = energy
# Increment the ticks counter
ticks_counter += 1
# Every 50 ticks, update energy_last_second
if ticks_counter == 50
energy_last_second = current_energy
ticks_counter = 0 # Reset the counter for the next cycle
end
end
# --- Try to go around by rifting ---
function quick_evasion_and_go_around()
faster(5)
rift
end
# --- Fire everytime you think you can for a by chance hit ---
function fire_func()
# Fire if the enemy is within a reasonable distance and visible
if abs(enemy_angle) <= 3 and enemy_distance <= 60
fire
end
end
# =========================
# ===== MAIN FUNCTION =====
# =========================
function play_game()
fire_func
calculate_last_second_enemy_position
manage_energy_with_ticks
save_vars # Save the state variables at the beginning of each cycle
# If the enemy is not visible
if !enemy_visible
enemy_invisible_cycles += 1
if enemy_invisible_cycles > 100
increasing_speed = 1
adjust_speed_after_invisibility
else
slow_down_and_rotate # Rotate in place to locate the enemy
end
# Check to see if we just passed them
if last_enemy_visible
# If the distance is feasable you either turn or run to go around based on the speed
if last_enemy_distance < 250
if speed > 3
quick_evasion_and_go_around
else
slow_down_and_rotate
end
end
# If you git hit and cant see the enemy
else if energy < energy_last_second
zig_zag_motion
rift
else
slow_down_and_rotate
end
else
enemy_invisible_cycles = 0
increasing_speed = 0
# Chase and attack the enemy when visible
follow_and_fire
end
end
# Calling the play_game() function and executing the logic
play_game