2 sst.py =-- Super Star Trek in Python
4 Control flow of this translation is pretty much identical to the C version
5 (and thus like the ancestral FORTRAN) but the data structures are
6 radically different -- the Python code makes heavy use of objects.
8 Note that the game.quad, game.snap.galaxy and game.snap.chart members
9 are not actually arrays but dictioaries indixed by coord tuples. Be setting
10 the hash of a coord equal to the hash of a literal tuple containing its
11 coordinate data, we ensure these can be indexed both ways.
18 NINHAB = GALSIZE * GALSIZE / 2
20 PLNETMAB = NINHAB + MAXUNINHAB
23 FULLCREW = 428 # BSD Trek was 387, that's wrong
28 # These macros hide the difference between 0-origin and 1-origin addressing.
29 # They're a step towards de-FORTRANizing the code.
30 def VALID_QUADRANT(x,y): ((x)>=1 and (x)<=GALSIZE and (y)>=1 and (y)<=GALSIZE)
31 def VALID_SECTOR(x, y): ((x)>=1 and (x)<=QUADSIZE and (y)>=1 and (y)<=QUADSIZE)
33 # These types have not been dealt with yet
41 def __init(self, x=None, y=None):
45 self.x = self.y = None
47 return self.x != None and self.y != None
48 def __eq__(self, other):
49 return self.x == other.y and self.x == other.y
50 def __add__(self, other):
51 return coord(self.x+self.x, self.y+self.y)
52 def __sub__(self, other):
53 return coord(self.x-self.x, self.y-self.y)
54 def distance(self, other):
55 return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
57 return coord(self.x / abs(x), self.y / abs(y));
61 return "%d - %d" % (self.x, self.y)
64 "A feature in the current quadrant (ship, star, black hole, base, etc)."
66 self.type = None # name of feature type
67 self.sector = None # sector location
69 return self.sector.distance(game.sector)
71 "This will be overridden by subclasses."
73 def sectormove(self, dest):
74 "Move this feature within the current quadrant."
76 game.quad[self.sector] = None
77 game.quad[dest] = self
81 "A starship, frindly or enemy."
82 def __init__(self, type, power):
83 feature.__init__(self)
84 self.type = type # klingon, romulan, commander,
85 # supercommander, tholian,
86 # enterprise, faerie queene.
87 self.power = power # power
88 if self.type in ("Klingon", "Commander", "Super-Commander"):
90 elif self.type == "Romulan":
93 if self.type in ("Klingon", "Commander", "Super-Commander"):
95 elif self.type == "Romulan":
99 "Empty space. Has no state, just knows how to identify iself."
104 "A star. Has no state, just knows how to identify iself."
108 class planet(feature):
109 "A planet. May be inhabited or not, may hold dilithium crystals or not."
111 feature.__init__(self)
113 self.crystals = None # "absent", "present", or "mined"
114 self.inhabited = False
115 self.known = "unknown" # Other values: "known" and "shuttle down"
116 game.state.planets.append(self)
118 game.state.planets.remove(self)
126 "A bit of Tholian web. Has no state, just knows how to identify iself."
130 class blackhole(feature):
131 "A black hole. Has no hair, just knows how to identify iself."
135 class starbase(feature):
136 "Starbases also have no features, just a location."
137 def __init(self, quadrant):
138 feature.__init__(self)
139 self.quadrant = quadrant
140 game.state.bases.append(self)
142 game.state.bases.remove(self)
153 self.supernova = None
155 self.status = "secure" # Other valuues: "distressed", "enslaved"
157 "List enemies in this quadrant."
159 for feature in self.quad.values:
160 if not isinstance(feature, ship):
162 if feature.name not in ("Enterprise", "Faerie Queene"):
167 "A chart page. The starchart is a 2D array of these."
169 self.stars = None # Will hold a number
170 self.starbase = None # Will hold a bool
171 self.klingons = None # Will hold a number
174 "State of the universe. The galaxy is a 2D array of these."
176 self.crew = None # crew complement
177 self.remkl = None # remaining klingons
178 self.remcom = None # remaining commanders
179 self.nscrem = None # remaining super commanders
180 self.starkl = None # destroyed stars
181 self.basekl = None # destroyed bases
182 self.nromrem = None # Romulans remaining
183 self.nplankl = None # destroyed uninhabited planets self.nworldkl = None # destroyed inhabited planets
184 self.planets = []; # List of planets known
185 self.date = None # stardate
186 self.remres = None # remaining resources
187 self. remtime = None # remaining time
188 self.bases = [] # Base quadrant coordinates
189 self.kcmdr = [] # Commander quadrant coordinates
190 self.kscmdr = None # Supercommander quadrant coordinates
191 self.galaxy = {} # Dictionary of quadrant objects
192 self.chart = {} # Dictionary of page objects
195 return game.damage[dev] != 0.0
199 self.date = None # The only mandatory attribute
203 self.options = [] # List of option strings
204 self.state = snapshot() # State of the universe
205 self.snapsht = snapshot() # For backwards timetravel
206 self.quad = {} # contents of our quadrant
207 self.kpower = {} # enemy energy levels
208 self.kdist = {} # enemy distances
209 self.kavgd = {} # average distances
210 self.damage = {} # damage encountered
211 self.future = [] # future events
212 self.passwd = None # Self Destruct password
213 # Coordinate members start here
214 self.enemies = {} # enemy sector locations
215 self.quadrant = None # where we are
217 self.tholian = None # coordinates of Tholian
218 self.base = None # position of base in current quadrant
219 self.battle = None # base coordinates being attacked
220 self.plnet = None # location of planet in quadrant
221 self.probec = None # current probe quadrant
222 # Flag members start here
223 self.gamewon = None # Finished!
224 self.ididit = None # action taken -- allows enemy to attack
225 self.alive = None # we are alive (not killed)
226 self.justin = None # just entered quadrant
227 self.shldup = None # shields are up
228 self.shldchg = None # shield changing (affects efficiency)
229 self.comhere = None # commander here
230 self.ishere = None # super-commander in quadrant
231 self.iscate = None # super commander is here
232 self.ientesc = None # attempted escape from supercommander
233 self.ithere = None # Tholian is here
234 self.resting = None # rest time
235 self.icraft = None # Kirk in Galileo
236 self.landed = None # party on planet or on ship
237 self.alldone = None # game is now finished
238 self.neutz = None # Romulan Neutral Zone
239 self.isarmed = None # probe is armed
240 self.inorbit = None # orbiting a planet
241 self.imine = None # mining
242 self.icrystl = None # dilithium crystals aboard
243 self.iseenit = None # seen base attack report
244 self.thawed = None # thawed game
245 # String members start here
246 self.condition = None # green, yellow, red, docked, dead,
247 self.iscraft = None # onship, offship, removed
248 self.skill = None # levels: none, novice, fair, good,
250 # Integer nembers sart here
251 x self.inkling = None # initial number of klingons
252 self.inbase = None # initial number of bases
253 self.incom = None # initial number of commanders
254 self.inscom = None # initial number of commanders
255 self.inrom = None # initial number of commanders
256 self.instar = None # initial stars
257 self.intorps = None # initial/max torpedoes
258 self.torps = None # number of torpedoes
259 self.ship = None # ship type -- 'E' is Enterprise
260 self.abandoned = None # count of crew abandoned in space
261 self.length = None # length of game
262 self.klhere = None # klingons here
263 self.casual = None # causalties
264 self.nhelp = None # calls for help
265 self.nkinks = None # count of energy-barrier crossings
266 self.iplnet = None # planet # in quadrant
267 self.inplan = None # initial planets
268 self.irhere = None # Romulans in quadrant
269 self.isatb = None # =1 if super commander is attacking base
270 self.tourn = None # tournament number
271 self.proben = None # number of moves for probe
272 self.nprobes = None # number of probes available
273 # Float members start here
274 self.inresor = None # initial resources
275 self.intime = None # initial time
276 self.inenrg = None # initial/max energy
277 self.inshld = None # initial/max shield
278 self.inlsr = None # initial life support resources
279 self.indate = None # initial date
280 self.energy = None # energy level
281 self.shield = None # shield level
282 self.warpfac = None # warp speed
283 self.wfacsq = None # squared warp factor
284 self.lsupres = None # life support reserves
285 self.dist = None # movement distance
286 self.direc = None # movement direction
287 self.optime = None # time taken by current operation
288 self.docfac = None # repair factor when docking (constant?)
289 self.damfac = None # damage factor
290 self.lastchart = None # time star chart was last updated
291 self.cryprob = None # probability that crystal will work
292 self.probex = None # location of probe
294 self.probeinx = None # probe x,y increment
295 self.probeiny = None #
296 self.height = None # height of orbit around planet