-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimule.py
295 lines (267 loc) · 8.15 KB
/
simule.py
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
import math
import os
import re
import sys
import time
import subprocess
import threading
import queue
def error(msg):
print("\033[91mError:\033[0m {}".format(msg), file=sys.stderr)
class NonBlockingStreamReader:
def __init__(self, stream):
self._s = stream
self._q = queue.Queue()
def _populateQueue(stream, queue):
while True:
line = stream.readline()
if line:
queue.put(line)
else:
# Either closed or blocked
time.sleep(0.1)
self._t = threading.Thread(target=_populateQueue, args=(self._s, self._q))
self._t.daemon = True
self._t.start() # start collecting lines from the stream
def readline(self, timeout=1):
try:
return self._q.get(block=timeout is not None, timeout=timeout)
except queue.Empty:
global player
player.kill()
error("Your program timed out.")
sys.exit(1)
class State:
def __init__(self, x, y, hSpeed, vSpeed, fuel, rotate, power):
self.x = x
self.y = y
self.hSpeed = hSpeed
self.vSpeed = vSpeed
self.fuel = fuel
self.rotate = rotate
self.power = power
self.drawCmd = []
def next(self, inputRotate, inputPower):
nextRotate = self.rotate + min(15, max(-15, inputRotate - self.rotate))
nextPower = self.power + (1 if inputPower > self.power else -1 if inputPower < self.power else 0)
nextPower = min(nextPower, self.fuel)
xAcc = math.cos(math.radians(nextRotate)) * nextPower
yAcc = math.sin(math.radians(nextRotate)) * nextPower - 3.711
nextHSpeed = self.hSpeed + xAcc
nextVSpeed = self.vSpeed + yAcc
nextX = self.x + self.hSpeed + xAcc / 2
nextY = self.y + self.vSpeed + yAcc / 2
nextFuel = self.fuel - nextPower
return State(nextX, nextY, nextHSpeed, nextVSpeed, nextFuel, nextRotate, nextPower)
def dict(self):
return {
"x": round(self.x),
"y": round(self.y),
"hSpeed": round(self.hSpeed),
"vSpeed": round(self.vSpeed),
"fuel": round(self.fuel),
"rotate": round(self.rotate),
"power": round(self.power),
"drawCmd": self.drawCmd
}
def __str__(self):
return "State(x={}, y={}, hSpeed={}, vSpeed={}, fuel={}, rotate={}, power={})".format(
round(self.x),
round(self.y),
round(self.hSpeed),
round(self.vSpeed),
round(self.fuel),
round(self.rotate - 90),
round(self.power)
)
def round(n):
return int(n + (0.5 if n > 0 else -0.5))
def crossLand_old(state, prevState):
global land
if prevState is None:
return None
for i in range(len(land) - 1):
# find the equation of the line
# y = ax + b
a = (land[i + 1]["y"] - land[i]["y"]) / (land[i + 1]["x"] - land[i]["x"])
b = land[i]["y"] - a * land[i]["x"]
# find the equation of the state line
# y = cx + d
c = 0 if state.x == prevState.x else (state.y - prevState.y) / (state.x - prevState.x)
d = state.y - c * state.x
# find the intersection point
if a == c:
continue
x = (d - b) / (a - c)
y = a * x + b
if ((prevState.y <= y <= state.y or prevState.y >= y >= state.y)
and (prevState.x <= x <= state.x or prevState.x >= x >= state.x)
and (land[i]["x"] <= x <= land[i + 1]["x"] or land[i]["x"] >= x >= land[i + 1]["x"])
and (land[i]["y"] <= y <= land[i + 1]["y"] or land[i]["y"] >= y >= land[i + 1]["y"])):
return (x, y)
return None
def crossLand(state, prevState):
global land
if prevState is None:
return None
for i in range(len(land) - 1):
# find the equation of the line
# ax + by + c = 0
a = land[i + 1]["y"] - land[i]["y"]
b = land[i]["x"] - land[i + 1]["x"]
c = land[i + 1]["x"] * land[i]["y"] - land[i]["x"] * land[i + 1]["y"]
# find the equation of the state line
# ax + by + c = 0
a2 = state.y - prevState.y
b2 = prevState.x - state.x
c2 = state.x * prevState.y - prevState.x * state.y
# find the intersection point
det = a * b2 - a2 * b
if det == 0:
continue
x = (b * c2 - b2 * c) / det
y = (a2 * c - a * c2) / det
if ((prevState.y <= y <= state.y or prevState.y >= y >= state.y)
and (prevState.x <= x <= state.x or prevState.x >= x >= state.x)
and (land[i]["x"] <= x <= land[i + 1]["x"] or land[i]["x"] >= x >= land[i + 1]["x"])
and (land[i]["y"] <= y <= land[i + 1]["y"] or land[i]["y"] >= y >= land[i + 1]["y"])):
return (x, y)
return None
def outOfBounds(state):
return state.x < 0 or state.x > 6999 or state.y < 0 or state.y > 2999
def hasLanded(s, intersection):
global landingSurface
if intersection is None:
return False
return (
intersection[1] <= landingSurface["y"]
and landingSurface["x1"] <= intersection[0] <= landingSurface["x2"]
and abs(int(s.hSpeed)) <= 20
and abs(int(s.vSpeed)) <= 40
and s.rotate == 90
)
def simule(data, program):
global land
land = data["land"]
data["start"]["rotate"] += 90
state = State(**data["start"])
prevState = None
game = [state.dict()]
# get landing surface
global landingSurface
landingSurface = (0, 0, 0)
for i in range(len(land) - 1):
if land[i]["y"] == land[i+1]["y"]:
landingSurface = {
"x1": land[i]["x"],
"x2": land[i+1]["x"],
"y": land[i]["y"]
}
# execute the program in a subprocess
global player
try:
player = subprocess.Popen(
program.split(),
stdout=subprocess.PIPE,
stderr=sys.stderr,
stdin=subprocess.PIPE
)
except Exception as e:
error("Error while executing your program: {}".format(e))
sys.exit(1)
# create a non-blocking reader for the subprocess
io = NonBlockingStreamReader(player.stdout)
# send the surface to the program
# print("Game sending surface...", file=sys.stderr, flush=True)
player.stdin.write("{}\n".format(len(land)).encode())
for i in range(data["surface_n"]):
# print("Game sending surface point {}...".format(i), file=sys.stderr, flush=True)
player.stdin.write("{} {}\n".format(land[i]["x"], land[i]["y"]).encode())
while True:
intersection = crossLand_old(state, prevState)
if hasLanded(state, intersection):
print("\033[92mYou landed successfully!\033[0m", file=sys.stderr, flush=True)
break
if outOfBounds(state) or intersection is not None:
print("\033[91mYou crashed !\033[0m", file=sys.stderr, flush=True)
break
# send the state to the program
# print("Game sending state...", file=sys.stderr, flush=True)
player.stdin.write("{} {} {} {} {} {} {}\n".format(
round(state.x),
round(state.y),
round(state.hSpeed),
round(state.vSpeed),
round(state.fuel),
round(state.rotate - 90),
round(state.power)
).encode())
player.stdin.flush()
# read input from stdin
# print("Game waiting for rotation and power...", file=sys.stderr, flush=True)
str = io.readline().decode()
if not re.match(r"^[\d-]+ \d+$", str):
error("invalid input format")
player.kill()
exit(1)
rotate, power = map(int, str.split(" "))
# check input values
if not -90 <= rotate <= 90:
error("invalid rotate value")
player.kill()
exit(1)
if not 0 <= power <= 4:
error("invalid power value")
player.kill()
exit(1)
prevState = state
state = state.next(rotate + 90, power)
# read the draw command
# print("Game waiting for draw command...", file=sys.stderr, flush=True)
draw_n = io.readline().decode()
if not re.match(r"^\d+$", draw_n):
error("invalid number of draw command")
player.kill()
exit(1)
draw_n = int(draw_n)
state.drawCmd = []
for i in range(draw_n):
str = io.readline().decode()
if re.match(r"LINE [\d-]+ [\d-]+ [\d-]+ [\d-]+ \d+ #[\da-fA-F]{6}$", str):
args = str.split(" ")
state.drawCmd.append({
"type": "line",
"x1": int(args[1]),
"y1": int(args[2]),
"x2": int(args[3]),
"y2": int(args[4]),
"width": int(args[5]),
"color": args[6]
})
elif re.match(r"CIRCLE [\d-]+ [\d-]+ \d+ \d+ #[\da-fA-F]{6}$", str):
args = str.split(" ")
state.drawCmd.append({
"type": "circle",
"x": int(args[1]),
"y": int(args[2]),
"radius": int(args[3]),
"width": int(args[4]),
"color": args[5]
})
elif re.match(r"POINT [\d-]+ [\d-]+ \d+ #[\da-fA-F]{6}$", str):
args = str.split(" ")
state.drawCmd.append({
"type": "point",
"x": int(args[1]),
"y": int(args[2]),
"width": int(args[3]),
"color": args[4]
})
else:
error("invalid draw command: {}".format(str))
player.kill()
exit(1)
# print("game ", state, file=sys.stderr, flush=True)
game.append(state.dict())
player.kill()
return game