-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.py
415 lines (353 loc) · 16.9 KB
/
system.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import random
import json
import utils
system_data = json.load(open('./tables/system.json'))
#Create dies
d10 = utils.Die(10)
d100 = utils.Die(100)
#Create tables
primary_table = utils.DieTable(d100, utils.flatten_range(system_data['primary_table']))
rare_table = utils.DieTable(d100, utils.flatten_range(system_data['rare_table']))
special_table = utils.DieTable(d100, utils.flatten_range(system_data['special_table']))
star_information = system_data['star_information']
sizes_tables = {}
giant_outer_table = utils.DieTable(d100, utils.flatten_range(system_data['giant_outer_table']))
massive_outer_table = utils.DieTable(d100, utils.flatten_range(system_data['massive_outer_table']))
inner_planet_tables = {}
outer_planet_tables = {}
for star_type in star_information:
sizes_tables[star_type] = utils.DieTable(d10, utils.flatten_range(star_information[star_type]['sizes_table']))
if 'inner_planet_table' in star_information[star_type]:
if type(star_information[star_type]['inner_planet_table']) == dict:
inner_planet_tables[star_type] = utils.DieTable(d100, utils.flatten_range(star_information[star_type]['inner_planet_table']))
else:
inner_planet_tables[star_type] = inner_planet_tables[star_information[star_type]['inner_planet_table']]
else:
inner_planet_tables[star_type] = None
if type(star_information[star_type]['outer_planet_table']) == dict:
outer_planet_tables[star_type] = utils.DieTable(d100, utils.flatten_range(star_information[star_type]['outer_planet_table']))
elif star_information[star_type]['outer_planet_table'] == 'giant_outer_table':
outer_planet_tables[star_type] = giant_outer_table
elif star_information[star_type]['outer_planet_table'] == 'massive_outer_table':
outer_planet_tables[star_type] = massive_outer_table
else:
outer_planet_tables[star_type] = outer_planet_tables[star_information[star_type]['outer_planet_table']]
gas_giant_rings_table = utils.DieTable(d10, utils.flatten_range(system_data['gas_giant_rings_table']))
moons_table = utils.DieTable(d10, utils.flatten_range(system_data['moons_table']))
gas_giant_moons_table = utils.DieTable(d10, utils.flatten_range(system_data['gas_giant_moons_table']))
moon_type_table = utils.DieTable(d10, utils.flatten_range(system_data['moon_type_table']))
orbiting_stars_table = utils.DieTable(d10, utils.flatten_range(system_data['orbiting_stars_table']))
space_stations_table = utils.DieTable(d100, utils.flatten_range(system_data['space_stations_table']))
space_outposts_table = utils.DieTable(d100, utils.flatten_range(system_data['space_outposts_table']))
ground_settlements_table = utils.DieTable(d100, utils.flatten_range(system_data['ground_settlements_table']))
government_table = utils.DieTable(d100, utils.flatten_range(system_data['government_table']))
economy_table = utils.DieTable(d100, utils.flatten_range(system_data['economy_table']))
allegiance_table = utils.DieTable(d10, utils.flatten_range(system_data['allegiance_table']))
first_name_table = utils.DieTable(d100, utils.flatten_range(system_data['first_name_table']))
second_name_table = utils.DieTable(d100, utils.flatten_range(system_data['second_name_table']))
planet_discovery_rewards = system_data['planet_discovery_reward']
non_habitable_map = system_data['non_habitable_mappings']
inhabited_probability = system_data['inhabited_probability']
def roll_star_type():
star_type = primary_table.roll()[0]
if star_type == "Rare":
star_type = rare_table.roll()[0]
elif star_type == "Special":
star_type = special_table.roll()[0]
return star_type
def roll_allegiance():
return allegiance_table.roll()[0]
invalid_allegiance = {'Federation': ['Imperial', 'Patronage'],
'Empire': ['Democracy'],
'Alliance': ['Imperial'],
'Independent': ['Imperial']}
class System:
def __init__(self, inhabited = True):
self.stars = [] # List of Star objects
self.inhabited = inhabited
self.society = "Anarchy"
self.security = "Low"
self.economy = []
# implement logic for inhabited systems later
# systems without settlements are uninhabited
def generate_system(self):
# Generate the primary star
primary_star_type = roll_star_type()
main_star = Star(primary_star_type)
main_star.generate_size()
self.stars.append(main_star)
# Generate companion stars
num_companions = 0
while num_companions < 5:
companion_star_type = roll_star_type()
companion_star = Star(companion_star_type)
companion_star.generate_size()
if companion_star.size < main_star.size:
companion_star.generate_planets()
self.stars.append(companion_star)
num_companions += 1
if d10.roll()[0] not in [9, 10]:
break
else:
break
# Generate planets
for star in self.stars:
star.generate_planets()
if self.inhabited:
star.ensure_inhabited_planet()
# Generate society if inhabited
if self.inhabited:
self.generate_economy()
self.generate_government()
def generate_economy(self):
elw_ww_count = 0
mr_metallic_count = 0
for star in self.stars:
for planet in star.planets:
if planet.planet_type == "Earth-like World" or planet.planet_type == "Water World":
elw_ww_count += 1
if planet.planet_type == "Metal Rich" or (planet.planet_type.startswith("Gas Giant")\
and planet.rings.endswith("Metallic")):
mr_metallic_count += 1
if elw_ww_count >= 2:
self.economy.append("Agricultural")
if mr_metallic_count >= 2:
self.economy.append("Extraction")
star_planet_count = len(self.stars)
for star in self.stars:
star_planet_count += len(star.planets)
if len(self.economy) == 0 or star_planet_count < 6:
new_economy = economy_table.roll()[0]
if new_economy not in self.economy:
self.economy.append(new_economy)
def generate_government(self):
government = government_table.roll()[0]
self.society = government['Society']
self.security = government['Security Rating']
allegiance_done = False
while not allegiance_done:
allegiance = roll_allegiance()
if self.society not in invalid_allegiance[allegiance]:
self.allegiance = allegiance
allegiance_done = True
class Star:
def __init__(self, star_type, inhabited = True):
self.star_type = star_type
self.inhabited = inhabited
self.size = None
self.habitable_zone = None
self.planets = [] # List of Planet objects
self.orbiting_stars = [] # List of OrbitingStar objects
self.asteroid_belts = [] # List of AsteroidBelt objects
def generate_size(self):
self.size = sizes_tables[self.star_type].roll()[0]
if star_information[self.star_type]['goldilocks_zone']:
luminosity = calculate_luminosity(self.size, star_information[self.star_type]['alpha'])
self.habitable_zone = calculate_habitable_zone(luminosity)
def generate_orbiting_objects(self, min_distance, max_distance, table = "Inner"):
no_planet_count = 0
distance = min_distance
table = inner_planet_tables[self.star_type] if table == "Inner" else outer_planet_tables[self.star_type]
while distance <= max_distance:
planet_type = table.roll()[0]
if planet_type:
self.generate_orbiting_object(planet_type, distance)
else:
no_planet_count += 1
if no_planet_count == 2:
break
distance += 100
def generate_orbiting_object(self, object_type, distance):
if object_type == "Star":
new_star_type = orbiting_stars_table.roll()[0]
if new_star_type == "Main Sequence":
new_star_type = roll_star_type()
new_star = OrbitingStar(new_star_type, distance)
new_star.generate_size()
if new_star.size < self.size:
self.orbiting_stars.append(new_star)
elif object_type == "Asteroid Belt":
new_asteroid_belt = AsteroidBelt(distance)
self.asteroid_belts.append(new_asteroid_belt)
else:
if object_type in non_habitable_map.keys() and not self.is_in_habitable_zone(distance):
object_type = non_habitable_map[object_type]
new_planet = Planet(object_type, distance, self.inhabited)
self.planets.append(new_planet)
def ensure_inhabited_planet(self):
if self.inhabited and not any(planet.inhabited for planet in self.planets):
while not any(planet.inhabited for planet in self.planets):
self.generate_planets()
def generate_planets(self):
inner_max_distance = 1000 if not self.star_type in rare_table.table else 2000
outer_min_distance = 2000 if not self.star_type in rare_table.table else 3000
outer_max_distance = 10000 if not self.star_type in rare_table.table else 20000
# Generate inner and outer planets
self.generate_orbiting_objects(100, inner_max_distance, "Inner")
self.generate_orbiting_objects(outer_min_distance, outer_max_distance, "Outer")
# Ensure at least one planet for inhabited systems
if self.inhabited and not self.planets:
while not self.planets:
self.generate_orbiting_objects(100, inner_max_distance)
self.generate_orbiting_objects(outer_min_distance, outer_max_distance)
def is_in_habitable_zone(self, distance):
if self.habitable_zone:
return self.habitable_zone[0] <= distance <= self.habitable_zone[1]
else:
return False
def sorted_orbiting_objects(self):
# Combine and sort all orbiting objects by their distance
return sorted(
[(obj, obj.distance) for obj in self.planets + self.orbiting_stars + self.asteroid_belts],
key=lambda x: x[1])
#Extends Star for potential future functionality, like planet generation
class OrbitingStar(Star):
def __init__(self, star_type, distance, inhabited = False):
super().__init__(star_type, inhabited)
self.distance = distance
self.rings = False
self.generate_rings()
def roll_star_type(self):
self.star_type = "placeholder"
def generate_rings(self):
self.rings = gas_giant_rings_table.roll()[0]
class OrbitingBody:
def __init__(self, distance):
self.distance = distance
class Planet(OrbitingBody):
def __init__(self, planet_type, distance, system_inhabited):
super().__init__(distance)
self.planet_type = planet_type
self.inhabited = self.determine_inhabited(system_inhabited)
self.rings = False
self.moons = [] # List of Moon objects
self.stations = [] # List of Station objects
self.outposts = [] # List of Outpost objects
self.settlements = [] # List of Settlement objects
self.generate_all()
def determine_inhabited(self, system_inhabited):
if system_inhabited:
probability = inhabited_probability.get(self.planet_type, 0)
return random.random() < probability
return False
def generate_structures(self, structure_type):
table = {
'station': (space_stations_table, Station),
'outpost': (space_outposts_table, Outpost),
'settlement': (ground_settlements_table, Settlement)
}
num_structures = table[structure_type][0].roll()[0]
for _ in range(num_structures):
new_structure = table[structure_type][1]()
getattr(self, structure_type + 's').append(new_structure)
def generate_moons(self):
if self.planet_type.startswith('Gas Giant'):
num_moons = gas_giant_moons_table.roll()[0]
else:
num_moons = moons_table.roll()[0]
for _ in range(num_moons):
new_moon = Moon(moon_type_table.roll()[0])
self.moons.append(new_moon)
def generate_all(self):
self.generate_moons()
if self.planet_type.startswith('Gas Giant'):
self.rings = gas_giant_rings_table.roll()[0]
if self.rings == 'Metallic' and not self.inhabited:
self.inhabited = random.random() < inhabited_probability['Metallic']
if self.inhabited:
for structure_type in ['station', 'outpost']:
self.generate_structures(structure_type)
if not self.planet_type.startswith('Gas Giant'):
self.generate_structures('settlement')
class AsteroidBelt(OrbitingBody):
def __init__(self, distance):
super().__init__(distance)
class Moon:
def __init__(self, moon_type):
self.moon_type = moon_type
# Add more attributes as needed
class Structure:
def __init__(self, structure_type):
self.structure_type = structure_type
self.generate_name()
def generate_name(self):
first_name = first_name_table.roll()[0]
second_name = second_name_table.roll()[0]
if self.structure_type == "Settlement" and second_name == "Orbital":
while second_name == "Orbital":
second_name = second_name_table.roll()[0]
self.name = f"{first_name} {second_name}"
# Can extend this class for Station, Outpost, and Settlement specific mechanics
class Station(Structure):
def __init__(self):
super().__init__("Station")
class Outpost(Structure):
def __init__(self):
super().__init__("Outpost")
class Settlement(Structure):
def __init__(self):
super().__init__("Settlement")
def calculate_luminosity(mass, alpha):
assert alpha, 'alpha must be non-zero and not None'
return mass ** alpha
def calculate_habitable_zone(luminosity):
# Constants for conversion
au_to_light_seconds = 499.004784
inner_boundary_au = (luminosity / 1.1) ** 0.5
outer_boundary_au = (luminosity / 0.53) ** 0.5
# Convert from AU to light-seconds
inner_boundary_ls = inner_boundary_au * au_to_light_seconds
outer_boundary_ls = outer_boundary_au * au_to_light_seconds
return inner_boundary_ls, outer_boundary_ls
def print_system_summary(system):
print("System Summary:")
print(f"Society: {system.society}")
print(f"Security: {system.security}")
print(f"Allegiance: {system.allegiance}")
print(f"Economy: {', '.join(system.economy) if system.economy else 'None'}")
for star in system.stars:
print(f"\nStar: {star.star_type}, Size: {star.size}")
for obj, distance in star.sorted_orbiting_objects():
if isinstance(obj, Planet):
print(f" Planet at {distance}ls: {obj.planet_type}, Inhabited: {'Yes' if obj.inhabited else 'No'}")
if obj.rings:
print(f" Rings: {obj.rings}")
if obj.moons:
print(f" Moons:")
for moon in obj.moons:
print(f" {moon.moon_type}")
if obj.stations:
print(f" Stations:")
for station in obj.stations:
print(f" {station.name}")
if obj.outposts:
print(f" Outposts:")
for outpost in obj.outposts:
print(f" {outpost.name}")
if obj.settlements:
print(f" Settlements:")
for settlement in obj.settlements:
print(f" {settlement.name}")
elif isinstance(obj, OrbitingStar):
print(f" Orbiting Star at {distance}ls: {obj.star_type}")
# You can add more details for orbiting stars here
elif isinstance(obj, AsteroidBelt):
print(f" Asteroid Belt at {distance}ls")
# Additional details for asteroid belts can be added here
print("\n")
if __name__ == "__main__":
# Generate a solar system
solar_system = System()
solar_system.generate_system()
# The following is to force the generation of a system with an Earth-like World
# elw = False
# while elw == False:
# solar_system = System()
# solar_system.generate_system()
# for star in solar_system.stars:
# for planet in star.planets:
# if planet.planet_type == "Water World" or planet.planet_type == "Earth-like World" or planet.planet_type == "Ammonia World":
# elw = True
# break
# Print the system summary
print_system_summary(solar_system)