-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
283 lines (223 loc) · 10.4 KB
/
main.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
import OpenGL
from OpenGL.GL import *
from OpenGL.GLU import *
import glfw
import keyboard
import os
from graphics import *
from loader import *
from star import *
from camera import *
from ui_text import *
from vector3 import *
def window_resize(window, width, height):
glfw.get_framebuffer_size(window)
glViewport(0, 0, width, height)
def clear_cmd_terminal():
if os.name == "nt":
os.system("cls")
else:
os.system("clear")
def flush_input():
try:
import msvcrt
while msvcrt.kbhit():
msvcrt.getch()
except ImportError:
import sys, termios #for linux/unix
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
def main():
global vp_size_changed
star_data = read_data("hygdata_v3.csv")
window_x = 1000
window_y = 600
near_clip = 1
far_clip = 20000
fov = 70
point_size = 2
cam_strafe_speed = 2
cam_rotate_speed = 5
cam_pitch_down = "W"
cam_pitch_up = "S"
cam_yaw_left = "A"
cam_yaw_right = "D"
cam_roll_ccw = "Q"
cam_roll_cw = "E"
cam_strafe_up = "U"
cam_strafe_down = "O"
cam_strafe_right = "L"
cam_strafe_left = "J"
cam_strafe_forward = "I"
cam_strafe_backward = "K"
incr_dist = "T"
decr_dist = "G"
use_command_line = "C"
glfw.init()
window = glfw.create_window(int(window_x),int(window_y),"Galactic Navigator", None, None)
glfw.set_window_pos(window,100,100)
glfw.make_context_current(window)
glfw.set_window_size_callback(window, window_resize)
gluPerspective(fov, int(window_x)/int(window_y), near_clip, far_clip)
glEnable(GL_CULL_FACE)
glEnable(GL_POINT_SMOOTH)
glPointSize(point_size)
cam = camera("main_cam", [0,0,0], [[1,0,0],[0,1,0],[0,0,1]], True)
cam.move([0,0,-10])
max_dist = 64
# available travel modes are 'commute' and 'sightseeing'
travel_mode = "commute"
max_jump = 10 * 3.262 # ly * 3.262 = parsecs
travel_speed = 0.9 # c
stay_time = 0.25 # years
system_start = None
system_destination = None
waypoints = []
def get_dist_between(star1, star2):
return ( (star1.x-star2.x)**2 + (star1.y-star2.y)**2 + (star1.z-star2.z)**2 )**(0.5)
def get_stars_within(origin, dist):
cstars = []
for star in star_data:
if (not star == origin) and get_dist_between(origin, star) < dist:
cstars.append(star)
return cstars
def find_system_by_name(sysname):
for star in star_data:
if star.proper == sysname or star.hyg == sysname:
return star
print("Star system", sysname, "could not be found!")
return None
def compute_route():
route = []
route.append(system_start)
if travel_mode == "commute":
while not system_destination in route:
cstars = get_stars_within(route[-1], max_jump/3.262)
if system_destination in cstars:
route.append(system_destination)
# compute route properties
target_dist = get_dist_between(system_start, system_destination)
total_distance_covered = 0
for si in range(len(route)):
if si > 0:
total_distance_covered += get_dist_between(route[si-1], route[si])
total_stay_time = (len(route)-1) * stay_time
move_time = (total_distance_covered/travel_speed) * 3.262
if travel_speed < 1:
experienced_move_time = move_time * math.sqrt(1 - travel_speed**2)
else:
experienced_move_time = "UNAVAILABLE: Travel speed >= c!"
travel_time = (total_distance_covered/travel_speed) * 3.262 + (len(route)-1) * stay_time
if travel_speed < 1:
experienced_travel_time = experienced_move_time + (len(route)-1) * stay_time
else:
experienced_travel_time = "UNAVAILABLE: Travel speed >= c!"
print("\nRoute: ", end="")
for st in route:
if not st.proper:
print("HYG", st.hyg, end="")
else:
print(st.proper + " (HYG " + st.hyg + ")", end="")
if not st == route[-1]:
print(" --> ", end="")
else:
print("")
print("INPUT Travel speed (c) = ", travel_speed)
print("INPUT Stay time (years) = ", stay_time)
print("Target distance (ly):", target_dist * 3.262)
print("Route length (ly):", total_distance_covered * 3.262)
print("Total travel time (years):", travel_time)
print("Total experienced travel time (years):", experienced_travel_time)
print("Total in-system stay time (years):", total_stay_time)
print("Total time spent in interstellar medium (years):", move_time)
print("Total time spent in interstellar medium as experienced by the traveller (years):", experienced_move_time)
return route
else:
travel_direction = vec3(system_destination.x, system_destination.y, system_destination.z) - vec3(route[-1].x, route[-1].y, route[-1].z)
best_attempt = None
best_system = None
for cstar in cstars:
if not best_attempt or get_dist_between(cstar, system_destination) < best_attempt:
best_attempt = get_dist_between(cstar, system_destination)
best_system = cstar
if best_system in route:
print("Can not plot route! Try increasing max jump distance.")
input("Press Enter to continue...")
return []
if best_system:
route.append(best_system)
else:
print("Can not plot route! Try increasing max jump distance.")
input("Press Enter to continue...")
return []
return route
clear_cmd_terminal()
print("Press", use_command_line, "to enter a command.")
while not glfw.window_should_close(window):
cam.rotate([(keyboard.is_pressed(cam_pitch_down) - keyboard.is_pressed(cam_pitch_up)) * cam_rotate_speed,
(keyboard.is_pressed(cam_yaw_left) - keyboard.is_pressed(cam_yaw_right)) * cam_rotate_speed,
(keyboard.is_pressed(cam_roll_ccw) - keyboard.is_pressed(cam_roll_cw)) * cam_rotate_speed])
cam.move([(keyboard.is_pressed(cam_strafe_left) - keyboard.is_pressed(cam_strafe_right)) * cam_strafe_speed,
(keyboard.is_pressed(cam_strafe_down) - keyboard.is_pressed(cam_strafe_up)) * cam_strafe_speed,
(keyboard.is_pressed(cam_strafe_forward) - keyboard.is_pressed(cam_strafe_backward)) * cam_strafe_speed])
if keyboard.is_pressed(incr_dist):
max_dist *= 2
elif keyboard.is_pressed(decr_dist):
max_dist *= 0.5
if keyboard.is_pressed(use_command_line):
flush_input()
frame_cmd = input(" > ")
else:
frame_cmd = ""
# - - - COMMAND INTERPRETER - - -
if frame_cmd:
frame_cmd = frame_cmd.split(" ")
frame_cmd[0] = frame_cmd[0].lower()
if frame_cmd[0] == "set_start":
sys_name = ""
for cmd_element in range(len(frame_cmd)):
if 0 < cmd_element < len(frame_cmd)-1:
sys_name += frame_cmd[cmd_element] + " "
elif cmd_element == len(frame_cmd)-1:
sys_name += frame_cmd[cmd_element]
system_start = find_system_by_name(sys_name)
print("Starting point set to " + sys_name)
elif frame_cmd[0] == "set_destination":
sys_name = ""
for cmd_element in range(len(frame_cmd)):
if 0 < cmd_element < len(frame_cmd)-1:
sys_name += frame_cmd[cmd_element] + " "
elif cmd_element == len(frame_cmd)-1:
sys_name += frame_cmd[cmd_element]
system_destination = find_system_by_name(sys_name)
print("Destination set to " + sys_name)
elif frame_cmd[0] == "set_max_jump":
max_jump = float(frame_cmd[1])
print("Max jump set to " + str(max_jump) + " ly")
elif frame_cmd[0] == "set_travel_speed":
travel_speed = float(frame_cmd[1])
print("Travel speed set to " + str(travel_speed) + " c")
elif frame_cmd[0] == "set_stay_time":
stay_time = float(frame_cmd[1])
print("Stay time set to " + str(stay_time) + " years")
elif frame_cmd[0] == "set_mode":
travel_mode = frame_cmd[1]
print("Travel mode set to " + travel_mode)
elif frame_cmd[0] == "compute":
print("Computing (depending on the parameters, this might take a while)...")
waypoints = compute_route()
elif frame_cmd[0] == "h" or frame_cmd[0] == "help":
print("")
print("Congrats! You entered a command! Any commands can be entered this way.")
print("Available commands: 'set_start <star_system_name>', 'set_destination <star_system_name>', 'compute', 'set_max_jump <number>', 'set_travel_speed <number>', 'set_stay_time <number>', set_mode <mode (commute/sightseeing)>")
print("")
else:
print("Unrecognized command:", frame_cmd[0])
pass
glfw.poll_events()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
drawOrigin()
drawStars(star_data, cam, max_dist)
drawRoute(waypoints, cam)
render_numbers(str(max_dist), [0,1,0], [-11,6], cam, 0.2)
glfw.swap_buffers(window)
main()