-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal.gd
285 lines (228 loc) · 8.67 KB
/
global.gd
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
extends Node
const LETTER_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const LETTER_LOWER = "abcdefghijklmnopqrstuvwxyz"
const LETTER_DIGITS = "1234567890"
const GO_NONE = 0
const GO_LOW = 1
const GO_MEDIUM = 2
const GO_HIGH = 3
const GO_RANDOM = 4
const GS_SPIRAL2 = 0
const GS_SPIRAL3 = 1
const GS_SPIRAL4 = 2
const GS_CLUSTER = 3
const GS_ELLIPTICAL = 4
const GS_DISC = 5
const GS_BOX = 6
const GS_IRREGULAR = 7
const GS_RING = 8
const GS_RANDOM = 9
const GA_YOUNG = 0
const GA_MATURE = 1
const GA_ANCIENT = 2
const GA_RANDOM = 3
const AIA_BEGINNER = 0
const AIA_TURTLE = 1
const AIA_CAUTIOUS = 2
const AIA_TYPICAL = 3
const AIA_AGGRESSIVE = 4
const AIA_MANIACAL = 5
const SP_HUMAN = 0
const SP_LAENFA = 1
const SP_SCYLIOR = 2
const SP_EGASSEM = 3
const SP_TRITH = 4
const MIN_SYS_DIST = 1
const MAX_STARLANE_LENGTH = 15
var gs_seed: String = "0"
var gs_map_size: int = 500
var starnames: Array
var galaxy: Galaxy
var starfield: Spatial
class Starlane:
var source: int
var dest: int
func _init(a_source, a_dest):
source = a_source
dest = a_dest
if not valid():
print("WARNING: Created starlane where source and destination system are the same")
func valid():
return (source != dest)
class StarSystem:
var id: int
var pos: Vector3
var name: String
var starlanes: Array
var spatial: Spatial
func _init(a_id: int, a_pos: Vector3):
id = a_id
pos = a_pos
name = global.starnames.pop_front()
starlanes = []
func add_starlane(starlane: Starlane):
if not starlane.valid():
print("ERROR: Attemting to add invalid starlane")
return
var already_linked_sys = get_linked_systems()
if (starlane.source in already_linked_sys) or (starlane.dest in already_linked_sys):
return
if (starlane.source == id) or (starlane.dest == id):
starlanes.append(starlane)
else:
print("ERROR: Attempting to add starlane to system that does not start or end there")
func get_linked_systems():
var linked_sys = []
for starlane in starlanes:
if starlane.source == id:
linked_sys.append(starlane.dest)
elif starlane.dest == id:
linked_sys.append(starlane.source)
return linked_sys
class Fleet:
var id: int
var pos: Vector3
var current_sys: StarSystem
var dest_sys: StarSystem
var dist_travelled: float
var spatial: Spatial
func _init(a_id: int, at_sys: StarSystem):
id = a_id
current_sys = at_sys
pos = current_sys.pos
dest_sys = null
dist_travelled = 0.0
func set_transit(dest: StarSystem, dist: float):
dest_sys = dest
dist_travelled = dist
update_position()
func set_stationary():
set_transit(null, 0)
func is_stationary():
return dist_travelled == 0
func update_position():
if not spatial:
return
if dist_travelled:
var vec_to_dest = (dest_sys.pos - current_sys.pos).normalized()
pos = current_sys.pos + vec_to_dest * dist_travelled
spatial.look_at_from_position(current_sys.pos, dest_sys.pos, Vector3(0, 1, 0))
spatial.translation = pos
else:
pos = current_sys.pos
spatial.translation = current_sys.pos + Vector3(0, 0.5, 0)
class Galaxy extends AStar:
var systems = {}
var starlanes = []
var fleets = {}
func add_system(sys: StarSystem):
systems[sys.id] = sys
add_point(sys.id, sys.pos)
func add_starlane(starlane: Starlane):
if not ((starlane.source in systems.keys()) and (starlane.dest in systems.keys())):
print("ERROR: Attempting to add starlane to non-existing systems")
return
var ssys: StarSystem = systems[starlane.source]
var ssys_linked_sys = ssys.get_linked_systems()
var dsys: StarSystem = systems[starlane.dest]
var dsys_linked_sys = dsys.get_linked_systems()
if (ssys.id in dsys_linked_sys) and (not dsys.id in ssys_linked_sys) or (dsys.id in ssys_linked_sys) and (not ssys.id in dsys_linked_sys):
print("ERROR: Found corrupted starlane lists when attempting to add starlane to system")
return
ssys.add_starlane(starlane)
dsys.add_starlane(starlane)
starlanes.append(starlane)
connect_points(starlane.source, starlane.dest)
func get_all_sys_connected_to(sys_id, connected_sys_list: Array = []):
if sys_id in connected_sys_list:
return
connected_sys_list.append(sys_id)
var this_sys: StarSystem = systems[sys_id]
for ssid in this_sys.get_linked_systems():
if ssid in connected_sys_list:
continue
get_all_sys_connected_to(ssid, connected_sys_list)
return connected_sys_list
func get_islands():
var islands = []
var already_assigned_sys = []
for ssid in systems.keys():
if ssid in already_assigned_sys:
continue
var island = get_all_sys_connected_to(ssid)
islands.append(island)
already_assigned_sys += island
return islands
func calc_positions(size, radius):
# Calculate positions for the disc galaxy shape.
for i in range(size):
var attempts: int = 100
var too_close = false
var new_pos: Vector3
while attempts:
attempts -= 1
var dist = rand_range(0.0, radius)
var angle = rand_range(0.0, 6.2831853072)
new_pos = Vector3(dist * cos(angle), 0, dist * sin(angle))
var nearest_neighbor = get_closest_point(new_pos)
if nearest_neighbor < 0:
too_close = false
break
elif new_pos.distance_to(get_point_position(nearest_neighbor)) < MIN_SYS_DIST:
too_close = true
else:
too_close = false
break
if not too_close:
add_system(StarSystem.new(i, new_pos))
func generate_starlanes():
for ss in systems.values():
set_point_disabled(ss.id, true)
var dest = get_closest_point(ss.pos, false)
set_point_disabled(ss.id, false)
if (dest >= 0) and (not dest in ss.get_linked_systems()):
add_starlane(Starlane.new(ss.id, dest))
var islands = get_islands()
var last_amount_of_islands: int = 0
while len(islands) != last_amount_of_islands:
for island in islands:
for ssid in island:
set_point_disabled(ssid, true)
var dist_map = {}
var dist_list = []
for ssid in island:
var ss: StarSystem = systems[ssid]
var closest_island = get_closest_point(ss.pos)
if closest_island >= 0:
var dist = ss.pos.distance_to(get_point_position(closest_island))
if dist in dist_map.keys():
dist_map[dist].append(ss)
else:
dist_map[dist] = [ss]
dist_list.append(dist)
if dist_list:
dist_list.sort()
for ss in dist_map[dist_list[0]]:
add_starlane(Starlane.new(ss.id, get_closest_point(ss.pos)))
for ssid in island:
set_point_disabled(ssid, false)
last_amount_of_islands = len(islands)
islands = get_islands()
for ss in systems.values():
var linked_sys = ss.get_linked_systems()
if len(linked_sys) == 1:
set_point_disabled(ss.id, true)
set_point_disabled(linked_sys[0], true)
var closest_neighbor = get_closest_point(ss.pos)
add_starlane(Starlane.new(ss.id, closest_neighbor))
set_point_disabled(ss.id, false)
set_point_disabled(linked_sys[0], false)
func _ready():
var name_file: File = File.new()
starnames = []
name_file.open("res://assets/text/starnames.txt", File.READ)
while not name_file.eof_reached():
var starname = name_file.get_line()
if starname:
starnames.append(starname)
starnames.shuffle()