-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib_Speculative.py
164 lines (131 loc) · 5.18 KB
/
lib_Speculative.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
'''
InterstellarTrader: Automating interstellar economics for the Traveller Universe.
Copyright (C) 2016 Christian Blouin ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
## Imports #######################################################################
##
# Internal imports
import lib_RandomTable
# External imports
from random import choice
## Functions #####################################################################
##
def RandomTonnage( s ):
''' Determine a random tonnage from a substring in the format Yd(XZ)
INPUT: s (string) a substring from the speculative table
OUTPUT: A dT (float)
'''
# dice
n = int(s[: s.find('d') ])
out = lib_RandomTable.Nd6(n)
# Multiplier (optional)
if 'X' in s:
mult = float( s[ s.find('X')+1 : ] )
out *= mult
return out
def Mass( dT, density ):
''' Determine the mass from a cargo, given a dT and a density
INPUT: dT (float) A size of a cargo in displacement ton
density (float) The number of tons per dT
OUTPUT: A mass in ton (float)
'''
return dT * float(density)
def Legality( s ):
''' Infer the legality class from a selection of classes as ASCII characters
INPUT: s (string) a substring from the speculative table
OUTPUT: A fully formed string with a qualitative descriptor.
'''
x = int(choice(s))
temp = ['WMD', 'Restricted', 'Military', 'Controlled', 'Hazardous', 'safe', 'unrestricted']
out = 'LC%d - (%s)'%(x, temp[x])
return out
def CargoType( s ):
''' Determine the type of cargo against a list of allowed typed
INPUT: s (string) a substring from the speculative table
OUTPUT: A cargo type (strong)
'''
out = lib_RandomTable.Random3d6('CargoType', lib_RandomTable.Nd6(3))[0]
# RO/RO
if out == 'RO/RO':
if 'RO' in s:
return 'RO/RO'
else:
out = 'break bulk'
# Container
if out == 'container':
return out
# Break bulk
if out == 'break bulk':
return 'break bulk'
# shortcode
t = out.split()
scode = t[0][0] + t[1][0]
if not scode in s:
out = 'break bulk'
return out
def Cost( qunt, price ):
''' Determine the base cost of a speculative cargo
INPUT: qnty (float) A cargo size in dT
price (float) A base price per dT from the table
OUTPUT: A cost as a string (string)
'''
price = qunt * price
kilo = price / 1000.
if kilo < 1000.:
return '%.1f KCr'%(kilo)
kilo /= 1000.
return '%.1f MCr'%(kilo)
def SpecialHandling( legal ):
''' Determine a cargo quirk from a custom table, also triggers the selection of a legality class
INPUT: legal (string) a substring itemizing list a possible LC for this cargo from the speculative table
OUTPUT: A tuple of ( handling , legality )
'''
out = lib_RandomTable.Random3d6('SpeculativeHandling', lib_RandomTable.Nd6())
temp = set(legal).intersection(set(out[1]))
if temp:
return out[0], Legality(choice(list(temp)))
else:
return 'none', Legality(choice(legal))
def GetOneCargo():
''' Determine one speculative cargo with all of the possible metadata
OUTPUT: A cargo as a dictionary.
'''
# Roll on the main table once
x = lib_RandomTable.Randomd66('speculative', lib_RandomTable.d66())
out = {}
out['Type'] = x[0]
out['dT'] = RandomTonnage( x[3] )
out['Mass'] = Mass(out['dT'], x[4])
out['Cost'] = Cost(out['dT'] , int( x[1] ))
out['Reaction modifiers'] = x[2].split(',')
out['Cargo type'] = CargoType( x[5] )
out['special'], out['Legality'] = SpecialHandling( x[6] )
# VOlatility
out['Volatility'] = lib_RandomTable.Random3d6('volatility', lib_RandomTable.Nd6(3))
return out
def PrettyPrint( x ):
''' Determine an ASCII output representation for a cargo
INPUT: x (dictionary) a cargo's metadata
OUTPUT: A string representation (string)
'''
out = 'Type: %s [%d dT (%.1f T)]\nCost: %s [%s]\n'%(x['Type'], x['dT'], x['Mass'], x['Cost'], ','.join(x['Reaction modifiers']))
out += 'Handling: %s\nLegality: %s\nPrice Volatility: %s'%(x['Cargo type'], x['Legality'], x['Volatility'][0])
if x['special'] != 'none':
out += '\nQuirk: %s'%(x['special'])
return out
if __name__ == '__main__':
fout = open('500cargoes.txt', 'w')
for i in range(500):
stock = GetOneCargo()
fout.write('Cargo %d -------------------------\n'%(i+1) + PrettyPrint(stock) + '\n\n')
fout.close()