-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworldgen.py
286 lines (252 loc) · 9.53 KB
/
worldgen.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
# worldgenlib.py
# Library file for the 3-Book Classic Traveller Subsector Generator by Omer Golan-Joel.
# This is open source code, feel free to use it for any purpose.
# For any questions, contact me at [email protected].
# Import modules
import agama
import os
# Functions
def atmo_gen(size):
if size == 0:
atmosphere = 0
else:
atmosphere = agama.dice(2, 6) - 7 + size
if atmosphere > 12:
atmosphere = 12
if atmosphere < 0:
atmosphere = 0
return atmosphere
def hydro_gen(size, atmosphere):
hydrographics = agama.dice(2, 6) - 7 + size
if size <= 1:
hydrographics = 0
elif atmosphere in [0, 1, 10, 11, 12]:
hydrographics -= 4
elif atmosphere == 14:
hydrographics -= 2
if hydrographics < 0:
hydrographics = 0
if hydrographics > 10:
hydrographics = 10
return hydrographics
def gov_gen(population):
government = agama.dice(2, 6) - 7 + population
if population == 0:
government = 0
if government < 0:
government = 0
if government > 15:
government = 15
return government
def law_gen(government):
law = agama.dice(2, 6) - 7 + government
if government == 0:
law = 0
if law < 0:
law = 0
if law > 10:
law = 10
return law
def starport_gen(population):
starport_roll = agama.dice(2, 6) - 7 + population
starport = "X"
if starport_roll <= 4:
starport = "A"
elif starport_roll in [5, 6]:
starport = "B"
elif starport_roll in [7, 8]:
starport = "C"
elif starport_roll == 9:
starport = "D"
elif starport_roll in [10, 11]:
starport = "E"
elif starport_roll >= 11:
starport = "X"
if population == 0:
starport = "X"
return starport
def tech_gen(uwp_dict):
tech = agama.dice(1, 6)
if uwp_dict["starport"] == "A":
tech += 6
elif uwp_dict["starport"] == "B":
tech += 4
elif uwp_dict["starport"] == "C":
tech += 2
elif uwp_dict["starport"] == "X":
tech -= 4
if uwp_dict["size"] in [0, 1]:
tech += 2
elif uwp_dict["size"] in [2, 3, 4]:
tech += 1
if uwp_dict["atmosphere"] <= 3:
tech += 1
elif uwp_dict["atmosphere"] >= 10:
tech += 1
elif uwp_dict["hydrographics"] in [9, 10]:
tech += 1
if uwp_dict["population"] in range(0, 6):
tech += 1
elif uwp_dict["population"] == 9:
tech += 2
elif uwp_dict["population"] == 10:
tech += 4
if uwp_dict["government"] in [1, 5]:
tech += 1
elif uwp_dict["government"] == 13:
tech -= 2
elif uwp_dict["population"] == 0:
tech = 0
return tech
def base_gen(starport):
naval = False
scout = False
base = " "
if starport in ["A", "B"] and agama.dice(2, 6) >= 8:
naval = True
if starport in ["A", "B", "C", "D"]:
scout_presence = agama.dice(2, 6)
if starport == "C":
scout_presence -= 1
elif starport == "B":
scout_presence -= 2
elif starport == "A":
scout_presence -= 3
if scout_presence >= 7:
scout = True
if naval and not scout:
base = "N"
elif scout and not naval:
base = "S"
elif scout and naval:
base = "A"
return base
def trade_gen(uwp_dict):
trade_list = []
if uwp_dict["atmosphere"] in range(4, 10) and uwp_dict["hydrographics"] in range(4, 9) and uwp_dict[
"population"] in range(5, 8):
trade_list.append("Ag")
if uwp_dict["size"] == 0:
trade_list.append("As")
if uwp_dict["atmosphere"] >= 2 and uwp_dict["hydrographics"] == 0:
trade_list.append("De")
if uwp_dict["atmosphere"] <= 1 and uwp_dict["hydrographics"] >= 1:
trade_list.append("Ic")
if uwp_dict["atmosphere"] in [0, 1, 2, 4, 7, 9] and uwp_dict["population"] >= 9:
trade_list.append("In")
if uwp_dict["atmosphere"] <= 3 and uwp_dict["hydrographics"] <= 3 and uwp_dict["population"] >= 6:
trade_list.append("Na")
if uwp_dict["population"] in range(4, 7):
trade_list.append("Ni")
if uwp_dict["atmosphere"] in range(2, 6) and uwp_dict["hydrographics"] <= 3:
trade_list.append("Po")
if uwp_dict["atmosphere"] in [6, 8] and uwp_dict["population"] in range(6, 9):
trade_list.append("Ri")
if uwp_dict["hydrographics"] >= 10:
trade_list.append("Wa")
if uwp_dict["atmosphere"] <= 0:
trade_list.append("Va")
return trade_list
def name_converter(name):
new_name = f"{name: <{7}}".upper()
new_name = (new_name[:7]) if len(new_name) > 7 else new_name
return new_name
def generate_subsector():
stars = {}
for column in range (1, 9):
stars[column] = {}
for row in range (1, 11):
if agama.dice(1, 6) >= 4:
stars[column][row] = World(column, row)
else:
pass
return stars
def string_subsector(stars):
subsector_string = ""
for column in range (0, 9):
for row in range (0,11):
if column in stars:
if row in stars[column]:
subsector_string += stars[column][row].get_world_row() + "\n"
else:
pass
else:
pass
return subsector_string
# Classes
class World:
def __init__(self, column, row):
self.name = agama.random_line(os.path.join('data', 'worlds.txt'))
self.column = column
self.row = row
self.uwp_dict = {"starport": "X", "size": agama.dice(2, 6) - 2, "atmosphere": 0, "hydrographics": 0,
"population": agama.dice(2, 6) - 2, "government": 0, "law": 0, "tl": 0}
self.uwp_dict["atmosphere"] = atmo_gen(self.uwp_dict["size"])
self.uwp_dict["hydrographics"] = hydro_gen(self.uwp_dict["size"], self.uwp_dict["atmosphere"])
self.uwp_dict["government"] = gov_gen(self.uwp_dict["population"])
self.uwp_dict["law"] = law_gen(self.uwp_dict["government"])
self.uwp_dict["starport"] = starport_gen(self.uwp_dict["population"])
self.uwp_dict["tl"] = tech_gen(self.uwp_dict)
self.hex_uwp = {"starport": self.uwp_dict["starport"], "size": agama.pseudo_hex(self.uwp_dict["size"]),
"atmosphere": agama.pseudo_hex(self.uwp_dict["atmosphere"]),
"hydrographics": agama.pseudo_hex(self.uwp_dict["hydrographics"]),
"population": agama.pseudo_hex(self.uwp_dict["population"]),
"government": agama.pseudo_hex(self.uwp_dict["government"]),
"law": agama.pseudo_hex(self.uwp_dict["law"]),
"tl": agama.pseudo_hex(self.uwp_dict["tl"])}
if agama.dice(2, 6) < 10:
self.gas_giant = "G"
else:
self.gas_giant = " "
self.base = base_gen(self.uwp_dict["starport"])
self.trade_list = trade_gen(self.uwp_dict)
self.trade_string = " ".join(self.trade_list)
self.allegiance = "Na"
if self.column == 10 and self.row != 10:
self.hex = f"100{self.row}"
elif self.column != 10 and self.row == 10:
self.hex = f"0{self.column}10"
elif self.column == 10 and self.row == 10:
self.hex = "1010"
else:
self.hex = f"0{self.column}0{self.row}"
self.world_type = " "
if self.uwp_dict["hydrographics"] == "A":
self.uwp_dict["hydrographics"] = 10
if int(self.uwp_dict["hydrographics"]) > 0:
self.world_type = "@"
elif int(self.uwp_dict["size"]) == 0:
self.world_type = "#"
else:
self.world_type = "O"
self.scout = " "
self.naval = " "
if self.base == "A":
self.scout = "^"
self.naval = "*"
elif self.base == "N":
self.naval = "*"
elif self.base == "S":
self.scout = "^"
else:
self.scout = "_"
self.naval = " "
if self.gas_giant == "G":
self.gas_giant_display = "*"
else:
self.gas_giant_display = " "
self.name_display = name_converter(self.name)
def print_raw_uwp(self):
print(self.uwp_dict["starport"], self.uwp_dict["size"], self.uwp_dict["atmosphere"],
self.uwp_dict["hydrographics"], self.uwp_dict["population"], self.uwp_dict["government"],
self.uwp_dict["law"], "-", self.uwp_dict["tl"])
def print_uwp(self):
print(
f"{self.hex_uwp['starport']}{self.hex_uwp['size']}{self.hex_uwp['atmosphere']}{self.hex_uwp['hydrographics']}{self.hex_uwp['population']}{self.hex_uwp['government']}{self.hex_uwp['law']}-{self.hex_uwp['tl']} {self.base} {self.zone} {self.gas_giants} {self.trade_string} ")
def get_uwp_string(self):
return (
f"{self.hex_uwp['starport']}{self.hex_uwp['size']}{self.hex_uwp['atmosphere']}{self.hex_uwp['hydrographics']}{self.hex_uwp['population']}{self.hex_uwp['government']}{self.hex_uwp['law']}-{self.hex_uwp['tl']}")
def get_world_row(self):
uwp_string = self.get_uwp_string()
return f"{self.hex: <{5}}{self.name: <{13}}{uwp_string: <{10}}{self.gas_giant: <{3}}{self.trade_string: <{10}}{self.base: <{3}}"
# Test area