repl.it
@PlanetStargazer/

Cepheus Subsector Generator

Python

A simple script that generates subsectors following the rules of the Cepheus Engine SRD

fork
loading
Files
  • main.py
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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import random, sys, string, math


keys = ["Name", "Hex", "UPP", "B", "Notes", "Z", "PBG", "Al"]
dictSubSector = []

def name_generator(): # Generate a random name

    name = ""
    namepart = ["en" , "la" , "can", "be" ,
                "and", "phi", "eth", "ol" ,
                "ve" , "ho" , "a"  , "lia",
                "an" , "ar" , "ur" , "mi" ,
                "in" , "ti" , "qu" , "so" ,
                "ed" , "ess", "ex" , "io" ,
                "ce" , "ze" , "fa" , "ay" ,
                "wa" , "da" , "ack", "gre"];

    name += random.choice(namepart)
    name += random.choice(namepart)
    name += random.choice(namepart)
    name = name.capitalize()
    return name


def millify(n):
    millnames = ['',' Thousand',' Million',' Billion',' Trillion']
    n = float(n)
    millidx = max(0,min(len(millnames)-1,
                        int(math.floor(0 if n == 0 else math.log10(abs(n))/3))))

    return '{:.0f}{}'.format(n / 10**(3 * millidx), millnames[millidx])


def roll() : # Roll 2d6
    return random.randrange(1, 6, 1) + random.randrange(1, 6, 1)

def worldsize():  # Determines World Size
    size = roll() - 2
    return size

def worldsize_text(n): # Returns worldsize, gravitation as tuple of strings

    worldsizes = {0 : "800 km",
                  1: "1,600 km",
                  2: "3,200 km",
                  3: "4,800 km",
                  4: "6,400 km",
                  5: "8,000 km",
                  6: "9,600 km",
                  7: "11,200 km",
                  8: "12,800 km",
                  9: "14,400 km",
                  10: "16,000 km"}

    gravitation = {0 : "Negligible",
                   1 : "0.05 g",
                   2 : "0.15 g",
                   3 : "0.25 g",
                   4 : "0.35 g",
                   5 : "0.45 g",
                   6 : "0.7 g",
                   7 : "0.9 g",
                   8 : "1.0 g",
                   9 : "1.25 g",
                   10: "1.4 g"}

    return worldsizes[n], gravitation[n]

def atmosphere(size): # Determines Atmosphere
    atm = roll()-7 + size
    if (size == 0) : atm = 0
    if (size > 15) : atm = 15
    if (atm < 0) : atm=0
    return atm


def atmosphere_text(n): # Returns atmosphere as string
    atm_text = {0 : "None",
                1 : "Trace",
                2 : "Very Thin, Tainted",
                3 : "Very Thin",
                4 : "Thin, Tainted",
                5 : "Thin",
                6 : "Standard",
                7 : "Standard, Tainted",
                8 : "Dense",
                9 : "Dense, Tainted",
                10: "Exotic",
                11: "Corrosive",
                12: "Insidious",
                13: "Dense, High",
                14: "Thin, Low",
                15: "Unusual"}
    return atm_text[n]


def hydrographics(sz, at): # Returns Hydrographics based on Size and Atmosphere
    hydro = roll() - 7 + sz
    if (at == 15) : hydro -= 2
    if (at in [0,1,10,11,12]): hydro-= 4
    if (sz <= 1) : hydro=0
    if (hydro <0) : hydro=0
    if (hydro >10): hydro =10
    return hydro

def population(sz, atm, hydro): # Returns Population size based on Size, Atmosphere and Hydrographics
    pp = roll()-2
    if (sz<=2) : pp -= 1
    if (atm > 10) : pp -= 2
    else:
        if (atm == 6) : pp += 3
        else:
            if (atm == 5) or (atm==8) : pp += 1
            else:
                if (atm < 3) and (hydro==0) : pp -= 2
    if (pp<0) : pp=0
    if (pp>10): pp=10
    return pp

def hydro_text(n): #return Hydrographics description as string
    hyd_text = {0 : "Desert world (0% - 5%)",
                1 : "Dry world (6% - 15%)",
                2 : "A few small seas (16% - 25%)",
                3 : "Small seas and oceans (26% - 35%)",
                4 : "Wet world (36% - 45%)",
                5 : "Large Oceans (45% - 55%)",
                6 : "Large Oceans (56% - 65%)",
                7 : "Earth-like world (66% - 75%)",
                8 : "Water world (76% - 85%)",
                9 : "Only a few small islands and archipelagos (86% - 95%)",
                10: "Almost entirely water (96-100%)"}
    return hyd_text[n]

def starport(pp): # Returns Starport based on Population
    s = roll()-7 + pp
    if (s <=2) : return "X"
    if (s == 3) or (s == 4) : return "E"
    if (s == 5) or (s == 6) : return "D"
    if (s == 7) or (s == 8) : return "C"
    if (s == 9) or (s == 10) : return "B"
    else :return "A"

def worldgov(pp): # returns Government type and descriptive text based on Population
    govn = roll()-7+pp
    if (pp==0) : govn=0
    if (govn < 0): govn=0
    if (govn>15) : govn=15
    gov_text = {0 : "None",
                1 : "Company/Corporation",
                2 : "Participating Democracy",
                3 : "Self-Perpetuating Oligarchy",
                4 : "Representative Democracy",
                5 : "Feudal Technocracy",
                6 : "Captive Government",
                7 : "Balkanization",
                8 : "Civil Service Bureaucracy",
                9 : "Impersonal Bureaucracy",
                10: "Charismatic Dictator",
                11: "Non-Charismatic Leader",
                12: "Charismatic Oligarchy",
                13: "Religious Dictatorship",
                14: "Religious Autocracy",
                15: "Totalitarian Oligarchy"}
    return govn, gov_text[govn]

def gov_text(i): #return government description
    govtext = {0 : "None",
                1 : "Company/Corporation",
                2 : "Participating Democracy",
                3 : "Self-Perpetuating Oligarchy",
                4 : "Representative Democracy",
                5 : "Feudal Technocracy",
                6 : "Captive Government",
                7 : "Balkanization",
                8 : "Civil Service Bureaucracy",
                9 : "Impersonal Bureaucracy",
                10: "Charismatic Dictator",
                11: "Non-Charismatic Leader",
                12: "Charismatic Oligarchy",
                13: "Religious Dictatorship",
                14: "Religious Autocracy",
                15: "Totalitarian Oligarchy"}
    return govtext[i]

def lawlevel(gv): # returns lawlevel and descriptive text based on government
    lw = roll()-7+gv
    if (gv==0) : lw=0
    if (lw<0) : lw=0
    if (lw>10) : lw=10
    lwdesc = lw
    
    lawtext = {0 : "No Law. No restrictions; candidate for Amber Zone status",
                1 : "Low Law. Not allowed: Poison gas, explosives, undetectable weapons, weapons or mass destruction",
                2 : "Low Law. Not allowed: Portable energy weapons (except ship-mounted weapons)",
                3 : "Low Law. Not allowed: Heavy weapons",
                4 : "Medium Law. Not allowed: Light assault weapons and submachine guns",
                5 : "Medium Law. Not allowed: Personal concealable weapons",
                6 : "Medium Law. Not allowed: All firearms except shotguns and stunners; carrying weapons discouraged",
                7 : "High Law. Not allowed: Shotguns",
                8 : "High Law. Not allowed: All bladed weapons, stunners",
                9 : "High Law. Not allowed: Any weapons outside one’s residence; candidate for Amber Zone status",
                10: "Extreme Law. Not allowed: Any weapons allowed at all; candidate for Amber Zone status"
                }
    return lw, lawtext[lwdesc]

def law_text(i): #return law description
    lawtext =  {0 : "No Law. No restrictions; candidate for Amber Zone status",
                1 : "Low Law. Not allowed: Poison gas, explosives, undetectable weapons, weapons or mass destruction",
                2 : "Low Law. Not allowed: Portable energy weapons (except ship-mounted weapons)",
                3 : "Low Law. Not allowed: Heavy weapons",
                4 : "Medium Law. Not allowed: Light assault weapons and submachine guns",
                5 : "Medium Law. Not allowed: Personal concealable weapons",
                6 : "Medium Law. Not allowed: All firearms except shotguns and stunners; carrying weapons discouraged",
                7 : "High Law. Not allowed: Shotguns",
                8 : "High Law. Not allowed: All bladed weapons, stunners",
                9 : "High Law. Not allowed: Any weapons outside one’s residence; candidate for Amber Zone status",
                10: "Extreme Law. Not allowed: Any weapons allowed at all; candidate for Amber Zone status"
                }
    return lawtext[i]


def pseudohex(number): # returns a Traveller-style pseudohex number
    if (number < 10) : hex = str(number)
    else: hex = chr(ord('A')+number-10)
    return hex

def de_pseudohex(hex): # translates a hexcode back into a number
    if (hex in string.digits) :
        number = ord(hex)-ord("0")
    else:
        number = ord(hex) - ord("A") +10
    return number

def uwp(starport, size, atm, hydro, pop, gov, law, tech):
    size_t = pseudohex(size)
    atm_t = pseudohex(atm)
    hydro_t = pseudohex(hydro)
    pop_t=pseudohex(pop)
    gov_t=pseudohex(gov)
    law_t=pseudohex(law)
    tech_t=pseudohex(tech)

    uwp_string = starport + size_t + atm_t + hydro_t + pop_t + gov_t + law_t + "-" + tech_t
    return uwp_string

def stp_description(spc):
    spc_text = {"A" : "Excellent; Refined Fuel",
                "B" : "Good; Refined Fuel",
                "C" : "Routine; Unrefined Fuel",
                "D" : "Poor; Unrefined Fuel",
                "E" : "Frontier; No Fuel",
                "X" : "None."}
    return spc_text[spc]

def techlevel(sl, sz, a, h, p, g):
    tl = random.randrange(1, 6, 1)

    # Starport
    if (sl=="A") : tl += 6
    else:
        if (sl=="B") : tl += 4
        else:
            if (sl=="C") : tl += 2
            else:
                if (sl=="X") : tl -= 4

    # Size
    if (sz<=1) : tl +=2
    else:
        if (sz in [2,3,4]) : tl +=1

    #  Atmosphere
    if (a <=3) or (a>=10) : tl +=1

    # Hydrographics
    if (h in [0,9]) :tl +=1
    else:
        if (h == 10) : tl +=2

    # Population
    if (p in [1,2,3,4,5,9]) : tl +=1
    else:
        if (p == 10) : tl +=2
        else:
            if (p == 11) : tl +=3
            else:
                if (p == 12) : tl +=4
        

    # Government
    if (g in [0,5]) : tl+=1
    else:
        if (g==7) : tl+=2
        else:
            if (g in [13,14]) : tl-=2

    # Respect minimums
    if (h in [0,10]) and (p>=6) : tl = max(4,tl)
    if (a in [4,7,9]) : tl = max(5,tl)
    if (a in [0,1,2,3,10,11,12]) : tl = max(7,tl)
    if (a in [13,14]) and (h==10) : tl = max(7,tl)

    
    return tl

def tech_text(tl):  # returns short and verbose tech level descriptions as strings
    verbose_text = {0 : "No technology.",
            1 : "Roughly on a par with Bronze or Iron age technology.",
            2 : "Renaissance technology.",
            3 : "Mass production allows for product standardization, bringing the germ of industrial revolution and steam power.",
            4 : "Transition to industrial revolution is complete, bringing plastics, radio and other such inventions.",
            5 : "Widespread electrification, tele-communications and internal combustion.",
            6 : "Development of fission power and more advanced computing.",
            7 : "Can reach orbit reliably and has telecommunications satellites.",
            8 : "Possible to reach other worlds in the same system, although terraforming or full colonization is not within the culture’s capacity.",
            9 : "Development of gravity manipulation, which makes space travel vastly safer and faster; first steps into Jump Drive technology.",
            10: "With the advent of Jump, nearby systems are opened up.",
            11: "The first primitive (non-creative) artificial intelligences become possible in the form of \“low autonomous\” interfaces, as computers begin to model synaptic networks.",
            12: "Weather control revolutionizes terraforming and agriculture.",
            13: "The battle dress appears on the battlefield in response to the new weapons. \“High autonomous\” interfaces allow computers to become self-actuating and self-teaching.",
            14: "Fusion weapons become man-portable.",
            15: "Black globe generators suggest a new direction for defensive technologies, while the development of synthetic anagathics means that the human lifespan is now vastly increased."}
    short_text = {0 : "Primitive",
                1 : "Primitive",
                2 : "Primitive",
                3 : "Primitive",
                4 : "Industrial",
                5 : "Industrial",
                6 : "Industrial",
                7 : "Pre-Stellar",
                8 : "Pre-Stellar",
                9 : "Pre-Stellar",
                10: "Early Stellar",
                11: "Early Stellar",
                12: "Average Stellar",
                13: "Average Stellar",
                14: "Average Stellar",
                15: "High Stellar"}
    return short_text[tl], verbose_text[tl]

def trade_codes(s,a,h,p,g,l,tl):
    t=""

    if (a in [4,5,6,7,8,9]) and (h in [4,5,6,7,8]) and (p in [5,6,7]) : t +="Ag "
    if (s==0) and (a==0) and (h==0) : t +="As "
    if (p==0) : t +="Ba "
    if (a>=2) and (h==0) : t +="De "
    if (a>=10) and (h>=0) : t +="Fl "
    if (a in [5,6,8]) and (h in [4,5,6,7,8,9]) and (p in [4,5,6,7,8]) : t +="Ga "
    if (p>=9) : t+= "Hi "
    if (tl>=12) : t+="Ht "
    if (a<=1) and (h>=1) : t+="Ic "
    if (a in [0,1,2,4,7,9]) and (p>=9) : t+="In "
    if (p in [1,2,3]) : t+="Lo "
    if (tl<=5) : t+="Lt "
    if (a in [0,1,2,3]) and (h<=3) and (p>=6) : t+="Na "
    if (p in [4,5,6]) : t+="Ni "
    if (a in [6,8]) and (p in [6,7,8]) : t+="Ri "
    if (a==0) : t+="Va "
        
    return t

def planetoid(s):  #Determines number of planetoid belts
    number_of_belts=0
    proll=roll()
    if (proll>=4) :
        number_of_belts=max(1,random.randrange(1,6,1)-3)
    if (s==0) : number_of_belts=max(1,number_of_belts)

    return number_of_belts

def gasgiants(): #Determines number of gas giants
    number_of_giants=0
    groll=roll()
    if (groll>=5) :
        number_of_giants=max(1,random.randrange(1,6,1)-2)

    return number_of_giants

def basecode(sp):  #Determines type of bases present and return the basecode

    navy=False
    scout=False
    pirate=False

    # Naval bases
    broll = roll()
    if (broll>=8) and (sp in ["A","B"]) : navy=True

    # Scout bases
    if (sp not in ["E", "X"]) :
        sroll = roll()
        if (sp=="C") : sroll -=1
        else:
            if (sp=="B") : sroll -=2
            else:
                if (sp=="A") : sroll -=3
        if (sroll>=7) : scout=True

    #Pirate Base
    proll=roll()
    if (navy==True) or (sp!="A") :
        if (proll >=12) : pirate=True

    if (navy==True) and (scout==True) : return "A"
    if (scout==True) and (pirate==True) : return "G"
    if (navy==True) and (scout==False) : return "N"
    if (pirate==True) and (scout==False) : return "P"
    if (scout==True) and (navy==False) and (pirate==False) : return "S"
    if (scout==False) and (navy==False) and (pirate==False) : return ""



    



def generate_world(x,y): # Generates planet and outputs information

    name = name_generator()
    size = worldsize()
    diameter = worldsize_text(size)[0]
    gravitation = worldsize_text(size)[1]
    atm = atmosphere(size)
    hydro = hydrographics(size,atm)
    pop = population(size,atm,hydro)
    popmod=(roll()-2)
    if popmod < 1 : popmod=1
    pop_number = popmod * pow (10,pop)
    if (pop == 0) : pop_number = 0
    stp=starport(pop)
    gov=worldgov(pop)
    govdesc=gov[1]
    law=lawlevel(gov[0])
    tech=techlevel(stp, size, atm, hydro, pop, gov)
    tc=trade_codes(size, atm, hydro, pop, gov, law, tech)
    nob = planetoid(size)
    nog = gasgiants()
    base = basecode(stp)

# Generate TravellerMap-compatible string ... at least somewhat compatible ^_^
    
    str_name = name.ljust(12)
    str_hex = " "+format(x, '02d') + format(y, '02d')
    str_uwp = uwp(stp,size,atm,hydro,pop,gov[0],law[0],tech).ljust(10," ")
    str_base = basecode(stp).ljust(1," ")
    str_tc = tc.ljust(15," ")
    str_tc = str_tc[:15]
    str_travelzone = "X "
    str_pbg = str(popmod)+str(nob)+str(nog)
    
# Legacy SEC Format 
    # traderformatstring = str_name + "   " +str_hex + " " + str_uwp + " " + str_base + "  " + str_tc + "  " + str_travelzone + "  " + str_pbg + "  "+"Im"
    # print (traderformatstring)
    system = [str_name, str_hex, str_uwp, str_base, str_tc, str_travelzone, str_pbg, "Im"]
    dictSubSector.append(dict(zip(keys,system)))


def SEC_style_output():
    str_sectorname = name_generator()
    str_subsectorname = name_generator()
    str_sector = "@SUB-SECTOR: "+str_sectorname+" SECTOR: "+str_subsectorname
    print (str_sector)
    print ("#")
    print ("#--------1---------2---------3---------4---------5-------")
    print ("#PlanetName   Loc. UPP Code   B   Notes         Z  PBG Al")
    print ("#----------   ---- ---------  - --------------- -  --- --")
    for item in dictSubSector:
        print (" ".join([item[key] for key in keys]))


# keys = ["Name", "Hex", "UPP", "B", "Notes", "Z", "PBG", "Al"]

def Human_readable_output():
    for item in dictSubSector:
        

        starport = item["UPP"][0]
        worldsize = item["UPP"][1]
        atmosphere = item["UPP"][2]
        hydrosphere = item["UPP"][3]
        population = item["UPP"][4]
        government = item["UPP"][5]
        lawlevel = item["UPP"][6]
        techlevel = item["UPP"][8]
        pop_modifier = int(item["PBG"][0])
        planetoid_belts = int(item["PBG"][1])
        gas_giants = int(item["PBG"][2])
        diameter = worldsize_text(de_pseudohex(worldsize))[0]
        gravitation = worldsize_text(de_pseudohex(worldsize))[1]
        people = pop_modifier * pow(10,de_pseudohex(population))
        if (de_pseudohex(population) == 0) : people = 0
        basecode = item["B"]
        basetext = {" ": "no bases present", "A": "Naval and Scout Base", "G":"Scout Base and Pirate Base", "N":"Naval Base","P":"Pirate Base", "S":"Scout Base"}

        
        
        print ("- System Data ------------------------------------------------------------------------------------------------------------------------------------")
        print ("Name:        ", item["Name"])
        print ("Hex:        ", item["Hex"])
        print ("UPP:         ", item["UPP"])
        print()
        print ("- Primary World ----------------------------------------------------------------------------------------------------------------------------------")
        print ("Size:        ",worldsize, "/", "Diameter:", diameter,"/","Gravitation:",gravitation) 
        print ("Atmosphere:  ", atmosphere, "/", atmosphere_text(de_pseudohex(atmosphere)))
        print ("Hydrosphere: ", hydrosphere, "/", hydro_text(de_pseudohex(hydrosphere)))
        print()
        print ("- Population Data --------------------------------------------------------------------------------------------------------------------------------")
        print ("Population:  ", population, "/", millify(people))
        print ("Government:  ",government, "/", gov_text(de_pseudohex(government)))
        print ("Law Level:   ",lawlevel,"/", law_text(de_pseudohex(lawlevel)))
        print ("Trade Codes: ", item["Notes"])
        print ("Tech Level:  ",techlevel,"/", tech_text(de_pseudohex(techlevel))[0])
        print ("                 ",tech_text(de_pseudohex(techlevel))[1])
        print()
        print ("- Starports & Bases ------------------------------------------------------------------------------------------------------------------------------")
        print ("Starport:    ",starport, "/", stp_description(starport))
        print ("Bases:       ", basetext[basecode])
        print()
        print ("- Miscellaneous ----------------------------------------------------------------------------------------------------------------------------------")
        # print ("Travel Zone: ", item["Z"])
        if (planetoid_belts == 0) : print ("Pl. belts:   None")
        if (planetoid_belts > 0) : print ("Pl. belts:   ",planetoid_belts)
        if (gas_giants == 0) : print ("Gas giants:  None")
        if (gas_giants > 0) : print ("Gas giants:  ",planetoid_belts)
        print ()
        print ()
        print ()



def Generate_SubSector(): # Generate Subsector and write everything into dictSubSector
    for xx in range (1,9) :
        for yy in range (1,11) :
            sysroll=random.randrange(1,6,1)
            if (sysroll>=4) :
                generate_world(xx,yy)



    
#Main Program

Generate_SubSector()
SEC_style_output()
Human_readable_output()
input("Press Enter to exit...")