Use magic-comment pylint suppressions.
[super-star-trek.git] / sst.py
1 #!/usr/bin/env python3
2 """
3 sst.py -- Super Star Trek 2K
4
5 SST2K is a Python translation of a C translation of a FORTRAN
6 original dating back to 1973.  Beautiful Python it is not, but it
7 works.  Translation by Eric S. Raymond; original game by David Matuszek
8 and Paul Reynolds, with modifications by Don Smith, Tom Almy,
9 Stas Sergeev, and Eric S. Raymond.
10
11 See the doc/HACKING file in the distribution for designers notes and advice
12 on how to modify (and how not to modify!) this code.
13 """
14 from __future__ import print_function, division
15 # Runs under Python 2 an Python 3. Preserve this property!
16 # SPDX-License-Identifier: BSD-2-clause
17
18 # pylint: disable=line-too-long,superfluous-parens,too-many-lines,invalid-name,missing-function-docstring,missing-class-docstring,multiple-statements,too-many-branches,too-many-statements,too-many-locals,too-many-nested-blocks,too-many-return-statements,too-many-instance-attributes,global-statement,no-else-break,no-else-return,no-else-continue,too-few-public-methods,too-many-boolean-expressions
19
20 # pylint: disable=multiple-imports
21 import os, sys, math, curses, time, pickle, copy, gettext, getpass
22 import getopt, socket, locale
23 import codecs
24
25 # This import only works on Unixes.  The intention is to enable
26 # Ctrl-P, Ctrl-N, and friends in Cmd.
27 try:
28     # pylint: disable=unused-import
29     import readline
30 except ImportError:
31     pass
32
33 # Prevent lossage under Python 3
34 try:
35     my_input = raw_input
36 except NameError:
37     my_input = input
38
39 version = "2.7"
40
41 docpath         = (".", "doc/", "/usr/share/doc/sst/")
42
43 def _(st):
44     return gettext.gettext(st)
45
46 # Rolling our own LCG because Python changed its incompatibly in 3.2.
47 # Thus, we need to have our own to be 2/3 polyglot, which will also
48 # be helpful when we forwrard-port.
49
50 class randomizer:
51     # LCG PRNG parameters tested against
52     # Knuth vol. 2. by the authors of ADVENT
53     LCG_A = 1093
54     LCG_C = 221587
55     LCG_M = 1048576
56
57     @staticmethod
58     def random():
59         old_x = game.lcg_x
60         game.lcg_x = (randomizer.LCG_A * game.lcg_x + randomizer.LCG_C) % randomizer.LCG_M
61         return old_x / randomizer.LCG_M
62
63     @staticmethod
64     def withprob(p):
65         v = randomizer.random()
66         #if logfp:
67         #    logfp.write("#withprob(%.2f) -> %s\n" % (p, v < p))
68         return v < p
69
70     @staticmethod
71     def integer(*args):
72         v = randomizer.random()
73         if len(args) == 1:
74             v = int(v * args[0])
75         else:
76             v = args[0] + int(v * (args[1] - args[0]))
77         #if logfp:
78         #    logfp.write("#integer%s -> %s\n" % (args, v))
79         return int(v)
80
81     @staticmethod
82     def real(*args):
83         v = randomizer.random()
84         if len(args) == 1:
85             v *= args[0]                 # from [0, args[0])
86         elif len(args) == 2:
87             v = args[0] + v*(args[1]-args[0])        # from [args[0], args[1])
88         #if logfp:
89         #    logfp.write("#real%s -> %f\n" % (args, v))
90         return v
91
92     @staticmethod
93     def seed(n):
94         #if logfp:
95         #    logfp.write("#seed(%d)\n" % n)
96         game.lcg_x = n % randomizer.LCG_M
97
98 GALSIZE         = 8             # Galaxy size in quadrants
99 NINHAB          = (GALSIZE * GALSIZE // 2)      # Number of inhabited worlds
100 MAXUNINHAB      = 10            # Maximum uninhabited worlds
101 QUADSIZE        = 10            # Quadrant size in sectors
102 BASEMIN         = 2                             # Minimum starbases
103 BASEMAX         = (GALSIZE * GALSIZE // 12)     # Maximum starbases
104 MAXKLGAME       = 127           # Maximum Klingons per game
105 MAXKLQUAD       = 9             # Maximum Klingons per quadrant
106 FULLCREW        = 428           # Crew size. BSD Trek was 387, that's wrong
107 FOREVER         = 1e30          # Time for the indefinite future
108 MAXBURST        = 3             # Max # of torps you can launch in one turn
109 MINCMDR         = 10            # Minimum number of Klingon commanders
110 DOCKFAC         = 0.25          # Repair faster when docked
111 PHASEFAC        = 2.0           # Unclear what this is, it was in the C version
112
113 ALGERON         = 2311          # Date of the Treaty of Algeron
114
115
116 DEFAULT      = -1
117 BLACK        = 0
118 BLUE         = 1
119 GREEN        = 2
120 CYAN         = 3
121 RED          = 4
122 MAGENTA      = 5
123 BROWN        = 6
124 LIGHTGRAY    = 7
125 DARKGRAY     = 8
126 LIGHTBLUE    = 9
127 LIGHTGREEN   = 10
128 LIGHTCYAN    = 11
129 LIGHTRED     = 12
130 LIGHTMAGENTA = 13
131 YELLOW       = 14
132 WHITE        = 15
133
134 class TrekError(Exception):
135     pass
136
137 class JumpOut(Exception):
138     pass
139
140 class Coord:
141     def __init__(self, x=None, y=None):
142         self.i = x      # Row
143         self.j = y      # Column
144     def valid_quadrant(self):
145         return self.i >= 0 and self.i < GALSIZE and self.j >= 0 and self.j < GALSIZE
146     def valid_sector(self):
147         return self.i >= 0 and self.i < QUADSIZE and self.j >= 0 and self.j < QUADSIZE
148     def invalidate(self):
149         self.i = self.j = None
150     def __eq__(self, other):
151         return other is not None and self.i == other.i and self.j == other.j
152     def __ne__(self, other):
153         return other is None or self.i != other.i or self.j != other.j
154     def __add__(self, other):
155         return Coord(self.i+other.i, self.j+other.j)
156     def __sub__(self, other):
157         return Coord(self.i-other.i, self.j-other.j)
158     def __mul__(self, other):
159         return Coord(self.i*other, self.j*other)
160     def __rmul__(self, other):
161         return Coord(self.i*other, self.j*other)
162     def __div__(self, other):
163         return Coord(self.i/other, self.j/other)
164     def __truediv__(self, other):
165         return Coord(self.i/other, self.j/other)
166     def __floordiv__(self, other):
167         return Coord(self.i//other, self.j//other)
168     def __mod__(self, other):
169         return Coord(self.i % other, self.j % other)
170     def __rtruediv__(self, other):
171         return Coord(self.i/other, self.j/other)
172     def __rfloordiv__(self, other):
173         return Coord(self.i//other, self.j//other)
174     def roundtogrid(self):
175         return Coord(int(round(self.i)), int(round(self.j)))
176     def distance(self, other=None):
177         if not other:
178             other = Coord(0, 0)
179         return math.sqrt((self.i - other.i)**2 + (self.j - other.j)**2)
180     def bearing(self):
181         return 1.90985*math.atan2(self.j, self.i)
182     def sgn(self):
183         s = Coord()
184         if self.i == 0:
185             s.i = 0
186         elif self.i < 0:
187             s.i = -1
188         else:
189             s.i = 1
190         if self.j == 0:
191             s.j = 0
192         elif self.j < 0:
193             s.j = -1
194         else:
195             s.j = 1
196         return s
197     def quadrant(self):
198         #print "Location %s -> %s" % (self, (self / QUADSIZE).roundtogrid())
199         return self.roundtogrid() // QUADSIZE
200     def sector(self):
201         return self.roundtogrid() % QUADSIZE
202     def scatter(self):
203         s = Coord()
204         s.i = self.i + rnd.integer(-1, 2)
205         s.j = self.j + rnd.integer(-1, 2)
206         return s
207     def __str__(self):
208         if self.i is None or self.j is None:
209             return "Nowhere"
210         return "%s - %s" % (self.i+1, self.j+1)
211     __repr__ = __str__
212
213 class Thingy(Coord):
214     "Do not anger the Space Thingy!"
215     def __init__(self):
216         Coord.__init__(self)
217         self.angered = False
218     def angry(self):
219         self.angered = True
220     def at(self, q):
221         return (q.i, q.j) == (self.i, self.j)
222
223 class Planet:
224     def __init__(self):
225         self.name = None        # string-valued if inhabited
226         self.quadrant = Coord()        # quadrant located
227         self.pclass = None        # could be ""M", "N", "O", or "destroyed"
228         self.crystals = "absent"# could be "mined", "present", "absent"
229         self.known = "unknown"        # could be "unknown", "known", "shuttle_down"
230         self.inhabited = False        # is it inhabited?
231     def __str__(self):
232         return self.name
233
234 class Quadrant:
235     def __init__(self):
236         self.stars = 0
237         self.planet = None
238         self.starbase = False
239         self.klingons = 0
240         self.romulans = 0
241         self.supernova = False
242         self.charted = False
243         self.status = "secure"        # Could be "secure", "distressed", "enslaved"
244     def __str__(self):
245         return "<Quadrant: %(klingons)d>" % self.__dict__
246     __repr__ = __str__
247
248 class Page:
249     def __init__(self):
250         self.stars = None
251         self.starbase = False
252         self.klingons = None
253     def __repr__(self):
254         return "<%s,%s,%s>" % (self.klingons, self.starbase, self.stars)
255
256 def fill2d(size, fillfun):
257     "Fill an empty list in 2D."
258     lst = []
259     for i in range(size):
260         lst.append([])
261         for j in range(size):
262             lst[i].append(fillfun(i, j))
263     return lst
264
265 class Snapshot:
266     def __init__(self):
267         self.snap = False       # snapshot taken
268         self.crew = 0           # crew complement
269         self.nscrem = 0         # remaining super commanders
270         self.starkl = 0         # destroyed stars
271         self.basekl = 0         # destroyed bases
272         self.nromrem = 0        # Romulans remaining
273         self.nplankl = 0        # destroyed uninhabited planets
274         self.nworldkl = 0        # destroyed inhabited planets
275         self.planets = []        # Planet information
276         self.date = 0.0           # stardate
277         self.remres = 0         # remaining resources
278         self.remtime = 0        # remaining time
279         self.baseq = []         # Base quadrant coordinates
280         self.kcmdr = []         # Commander quadrant coordinates
281         self.kscmdr = Coord()        # Supercommander quadrant coordinates
282         # the galaxy
283         self.galaxy = fill2d(GALSIZE, lambda i_unused, j_unused: Quadrant())
284         # the starchart
285         self.chart = fill2d(GALSIZE, lambda i_unused, j_unused: Page())
286     def traverse(self):
287         for i in range(GALSIZE):
288             for j in range(GALSIZE):
289                 yield (i, j, self.galaxy[i][j])
290
291 class Event:
292     def __init__(self):
293         self.date = None        # A real number
294         self.quadrant = None        # A coord structure
295
296 # game options
297 OPTION_ALL        = 0xffffffff
298 OPTION_TTY        = 0x00000001        # old interface
299 OPTION_CURSES     = 0x00000002        # new interface
300 OPTION_IOMODES    = 0x00000003        # cover both interfaces
301 OPTION_PLANETS    = 0x00000004        # planets and mining
302 OPTION_THOLIAN    = 0x00000008        # Tholians and their webs (UT 1979 version)
303 OPTION_THINGY     = 0x00000010        # Space Thingy can shoot back (Stas, 2005)
304 OPTION_PROBE      = 0x00000020        # deep-space probes (DECUS version, 1980)
305 OPTION_SHOWME     = 0x00000040        # bracket Enterprise in chart
306 OPTION_RAMMING    = 0x00000080        # enemies may ram Enterprise (Almy)
307 OPTION_MVBADDY    = 0x00000100        # more enemies can move (Almy)
308 OPTION_BLKHOLE    = 0x00000200        # black hole may timewarp you (Stas, 2005)
309 OPTION_BASE       = 0x00000400        # bases have good shields (Stas, 2005)
310 OPTION_WORLDS     = 0x00000800        # logic for inhabited worlds (ESR, 2006)
311 OPTION_AUTOSCAN   = 0x00001000        # automatic LRSCAN before CHART (ESR, 2006)
312 OPTION_CAPTURE    = 0x00002000        # Enable BSD-Trek capture (Almy, 2013).
313 OPTION_CLOAK      = 0x80004000        # Enable BSD-Trek capture (Almy, 2013).
314 OPTION_PLAIN      = 0x01000000        # user chose plain game
315 OPTION_ALMY       = 0x02000000        # user chose Almy variant
316 OPTION_COLOR      = 0x04000000        # enable color display (ESR, 2010)
317 OPTION_DOTFILL    = 0x08000000        # fix dotfill glitch in chart (ESR, 2019)
318
319 # Define devices
320 DSRSENS         = 0
321 DLRSENS         = 1
322 DPHASER         = 2
323 DPHOTON         = 3
324 DLIFSUP         = 4
325 DWARPEN         = 5
326 DIMPULS         = 6
327 DSHIELD         = 7
328 DRADIO          = 8
329 DSHUTTL         = 9
330 DCOMPTR         = 10
331 DNAVSYS         = 11
332 DTRANSP         = 12
333 DSHCTRL         = 13
334 DDRAY           = 14
335 DDSP            = 15
336 DCLOAK          = 16
337 NDEVICES        = 17        # Number of devices
338
339 SKILL_NONE      = 0
340 SKILL_NOVICE    = 1
341 SKILL_FAIR      = 2
342 SKILL_GOOD      = 3
343 SKILL_EXPERT    = 4
344 SKILL_EMERITUS  = 5
345
346 def damaged(dev):
347     return (game.damage[dev] != 0.0)
348 def communicating():
349     return not damaged(DRADIO) or game.condition=="docked"
350
351 # Define future events
352 FSPY    = 0        # Spy event happens always (no future[] entry)
353                    # can cause SC to tractor beam Enterprise
354 FSNOVA  = 1        # Supernova
355 FTBEAM  = 2        # Commander tractor beams Enterprise
356 FSNAP   = 3        # Snapshot for time warp
357 FBATTAK = 4        # Commander attacks base
358 FCDBAS  = 5        # Commander destroys base
359 FSCMOVE = 6        # Supercommander moves (might attack base)
360 FSCDBAS = 7        # Supercommander destroys base
361 FDSPROB = 8        # Move deep space probe
362 FDISTR  = 9        # Emit distress call from an inhabited world
363 FENSLV  = 10       # Inhabited word is enslaved
364 FREPRO  = 11       # Klingons build a ship in an enslaved system
365 NEVENTS = 12
366
367 # Abstract out the event handling -- underlying data structures will change
368 # when we implement stateful events
369 def findevent(evtype):
370     return game.future[evtype]
371
372 class Enemy:
373     def __init__(self, etype=None, loc=None, power=None):
374         self.type = etype
375         self.location = Coord()
376         self.kdist = None
377         self.kavgd = None
378         if loc:
379             self.move(loc)
380         self.power = power        # enemy energy level
381         game.enemies.append(self)
382     def move(self, loc):
383         motion = (loc != self.location)
384         if self.location.i is not None and self.location.j is not None:
385             if motion:
386                 if self.type == 'T':
387                     game.quad[self.location.i][self.location.j] = '#'
388                 else:
389                     game.quad[self.location.i][self.location.j] = '.'
390         if loc:
391             self.location = copy.copy(loc)
392             game.quad[self.location.i][self.location.j] = self.type
393             self.kdist = self.kavgd = (game.sector - loc).distance()
394         else:
395             self.location = Coord()
396             self.kdist = self.kavgd = None
397             # Guard prevents failure on Tholian or thingy
398             if self in game.enemies:
399                 game.enemies.remove(self)
400         return motion
401     def __repr__(self):
402         return "<%s,%s.%f>" % (self.type, self.location, self.power)        # For debugging
403
404 class Gamestate:
405     def __init__(self):
406         self.options = None        # Game options
407         self.state = Snapshot()        # A snapshot structure
408         self.snapsht = Snapshot()        # Last snapshot taken for time-travel purposes
409         self.quad = None        # contents of our quadrant
410         self.damage = [0.0] * NDEVICES        # damage encountered
411         self.future = []        # future events
412         i = NEVENTS
413         while i > 0:
414             i -= 1
415             self.future.append(Event())
416         self.passwd  = None        # Self Destruct password
417         self.enemies = []
418         self.quadrant = None        # where we are in the large
419         self.sector = None        # where we are in the small
420         self.tholian = None        # Tholian enemy object
421         self.base = None        # position of base in current quadrant
422         self.battle = None        # base coordinates being attacked
423         self.plnet = None        # location of planet in quadrant
424         self.gamewon = False        # Finished!
425         self.ididit = False        # action taken -- allows enemy to attack
426         self.alive = False        # we are alive (not killed)
427         self.justin = False        # just entered quadrant
428         self.shldup = False        # shields are up
429         self.shldchg = False        # shield is changing (affects efficiency)
430         self.iscate = False        # super commander is here
431         self.ientesc = False        # attempted escape from supercommander
432         self.resting = False        # rest time
433         self.icraft = False        # Kirk in Galileo
434         self.landed = False        # party on planet (true), on ship (false)
435         self.alldone = False        # game is now finished
436         self.neutz = False        # Romulan Neutral Zone
437         self.isarmed = False        # probe is armed
438         self.inorbit = False        # orbiting a planet
439         self.imine = False        # mining
440         self.icrystl = False        # dilithium crystals aboard
441         self.iseenit = False        # seen base attack report
442         self.thawed = False        # thawed game
443         self.condition = None        # "green", "yellow", "red", "docked", "dead"
444         self.iscraft = None        # "onship", "offship", "removed"
445         self.skill = SKILL_NONE        # Player skill level
446         self.inkling = 0        # initial number of klingons
447         self.inbase = 0                # initial number of bases
448         self.incom = 0                # initial number of commanders
449         self.inscom = 0                # initial number of commanders
450         self.inrom = 0                # initial number of commanders
451         self.instar = 0                # initial stars
452         self.intorps = 0        # initial/max torpedoes
453         self.torps = 0                # number of torpedoes
454         self.ship = 0                # ship type -- 'E' is Enterprise
455         self.abandoned = 0        # count of crew abandoned in space
456         self.length = 0                # length of game
457         self.klhere = 0                # klingons here
458         self.casual = 0                # causalties
459         self.nhelp = 0                # calls for help
460         self.nkinks = 0                # count of energy-barrier crossings
461         self.iplnet = None        # planet # in quadrant
462         self.inplan = 0                # initial planets
463         self.irhere = 0                # Romulans in quadrant
464         self.isatb = 0                # =2 if super commander is attacking base
465         self.tourn = None        # tournament number
466         self.nprobes = 0        # number of probes available
467         self.inresor = 0.0        # initial resources
468         self.intime = 0.0        # initial time
469         self.inenrg = 0.0        # initial/max energy
470         self.inshld = 0.0        # initial/max shield
471         self.inlsr = 0.0        # initial life support resources
472         self.indate = 0.0        # initial date
473         self.energy = 0.0        # energy level
474         self.shield = 0.0        # shield level
475         self.warpfac = 0.0        # warp speed
476         self.lsupres = 0.0        # life support reserves
477         self.optime = 0.0        # time taken by current operation
478         self.damfac = 0.0        # damage factor
479         self.lastchart = 0.0        # time star chart was last updated
480         self.cryprob = 0.0        # probability that crystal will work
481         self.probe = None        # object holding probe course info
482         self.height = 0.0        # height of orbit around planet
483         self.score = 0.0        # overall score
484         self.perdate = 0.0        # rate of kills
485         self.idebug = False        # Debugging instrumentation enabled?
486         self.cdebug = False        # Debugging instrumentation for curses enabled?
487         self.statekscmdr = None # No SuperCommander coordinates yet.
488         self.brigcapacity = 400     # Enterprise brig capacity
489         self.brigfree = 400       # How many klingons can we put in the brig?
490         self.kcaptured = 0      # Total Klingons captured, for scoring.
491         self.iscloaked = False  # Cloaking device on?
492         self.ncviol = 0         # Algreon treaty violations
493         self.isviolreported = False # We have been warned
494         self.lcg_x = 0          # LCG generator value
495     def remkl(self):
496         return sum([q.klingons for (_i, _j, q) in list(self.state.traverse())])
497     def recompute(self):
498         # Stas thinks this should be (C expression):
499         # game.remkl() + len(game.state.kcmdr) > 0 ?
500         #        game.state.remres/(game.remkl() + 4*len(game.state.kcmdr)) : 99
501         # He says the existing expression is prone to divide-by-zero errors
502         # after killing the last klingon when score is shown -- perhaps also
503         # if the only remaining klingon is SCOM.
504         self.state.remtime = self.state.remres/(self.remkl() + 4*len(self.state.kcmdr))
505     def unwon(self):
506         "Are there Klingons remaining?"
507         return self.remkl()
508
509 FWON = 0
510 FDEPLETE = 1
511 FLIFESUP = 2
512 FNRG = 3
513 FBATTLE = 4
514 FNEG3 = 5
515 FNOVA = 6
516 FSNOVAED = 7
517 FABANDN = 8
518 FDILITHIUM = 9
519 FMATERIALIZE = 10
520 FPHASER = 11
521 FLOST = 12
522 FMINING = 13
523 FDPLANET = 14
524 FPNOVA = 15
525 FSSC = 16
526 FSTRACTOR = 17
527 FDRAY = 18
528 FTRIBBLE = 19
529 FHOLE = 20
530 FCREW = 21
531 FCLOAK = 22
532
533 # Code from ai.c begins here
534
535 def welcoming(iq):
536     "Would this quadrant welcome another Klingon?"
537     return iq.valid_quadrant() and \
538         not game.state.galaxy[iq.i][iq.j].supernova and \
539         game.state.galaxy[iq.i][iq.j].klingons < MAXKLQUAD
540
541 def tryexit(enemy, look, irun):
542     "A bad guy attempts to bug out."
543     iq = Coord()
544     iq.i = game.quadrant.i+(look.i+(QUADSIZE-1))//QUADSIZE - 1
545     iq.j = game.quadrant.j+(look.j+(QUADSIZE-1))//QUADSIZE - 1
546     if not welcoming(iq):
547         return False
548     if enemy.type == 'R':
549         return False # Romulans cannot escape!
550     if not irun:
551         # avoid intruding on another commander's territory
552         if enemy.type == 'C':
553             if iq in game.state.kcmdr:
554                 return []
555             # refuse to leave if currently attacking starbase
556             if game.battle == game.quadrant:
557                 return []
558         # don't leave if over 1000 units of energy
559         if enemy.power > 1000.0:
560             return []
561     oldloc = copy.copy(enemy.location)
562     # handle local matters related to escape
563     enemy.move(None)
564     game.klhere -= 1
565     if game.condition != "docked":
566         newcnd()
567     # Handle global matters related to escape
568     game.state.galaxy[game.quadrant.i][game.quadrant.j].klingons -= 1
569     game.state.galaxy[iq.i][iq.j].klingons += 1
570     if enemy.type == 'S':
571         game.iscate = False
572         game.ientesc = False
573         game.isatb = 0
574         schedule(FSCMOVE, 0.2777)
575         unschedule(FSCDBAS)
576         game.state.kscmdr = iq
577     else:
578         for cmdr in game.state.kcmdr:
579             if cmdr == game.quadrant:
580                 game.state.kcmdr.append(iq)
581                 break
582     # report move out of quadrant.
583     return [(True, enemy, oldloc, iq)]
584
585 # The bad-guy movement algorithm:
586 #
587 # 1. Enterprise has "force" based on condition of phaser and photon torpedoes.
588 # If both are operating full strength, force is 1000. If both are damaged,
589 # force is -1000. Having shields down subtracts an additional 1000.
590 #
591 # 2. Enemy has forces equal to the energy of the attacker plus
592 # 100*(K+R) + 500*(C+S) - 400 for novice through good levels OR
593 # 346*K + 400*R + 500*(C+S) - 400 for expert and emeritus.
594 #
595 # Attacker Initial energy levels (nominal):
596 # Klingon   Romulan   Commander   Super-Commander
597 # Novice    400        700        1200
598 # Fair      425        750        1250
599 # Good      450        800        1300        1750
600 # Expert    475        850        1350        1875
601 # Emeritus  500        900        1400        2000
602 # VARIANCE   75        200         200         200
603 #
604 # Enemy vessels only move prior to their attack. In Novice - Good games
605 # only commanders move. In Expert games, all enemy vessels move if there
606 # is a commander present. In Emeritus games all enemy vessels move.
607 #
608 # 3. If Enterprise is not docked, an aggressive action is taken if enemy
609 # forces are 1000 greater than Enterprise.
610 #
611 # Agressive action on average cuts the distance between the ship and
612 # the enemy to 1/4 the original.
613 #
614 # 4.  At lower energy advantage, movement units are proportional to the
615 # advantage with a 650 advantage being to hold ground, 800 to move forward
616 # 1, 950 for two, 150 for back 4, etc. Variance of 100.
617 #
618 # If docked, is reduced by roughly 1.75*game.skill, generally forcing a
619 # retreat, especially at high skill levels.
620 #
621 # 5.  Motion is limited to skill level, except for SC hi-tailing it out.
622
623 def movebaddy(enemy):
624     "Tactical movement for the bad guys."
625     goto = Coord()
626     look = Coord()
627     irun = False
628     # This should probably be just (game.quadrant in game.state.kcmdr) + (game.state.kscmdr==game.quadrant)
629     if game.skill >= SKILL_EXPERT:
630         nbaddys = int(((game.quadrant in game.state.kcmdr)*2 + (game.state.kscmdr==game.quadrant)*2+game.klhere*1.23+game.irhere*1.5)/2.0)
631     else:
632         nbaddys = (game.quadrant in game.state.kcmdr) + (game.state.kscmdr==game.quadrant)
633     old_dist = enemy.kdist
634     mdist = int(old_dist + 0.5) # Nearest integer distance
635     # If SC, check with spy to see if should hi-tail it
636     if enemy.type == 'S' and \
637         (enemy.power <= 500.0 or (game.condition=="docked" and not damaged(DPHOTON))):
638         irun = True
639         motion = -QUADSIZE
640     else:
641         # decide whether to advance, retreat, or hold position
642         forces = enemy.power+100.0*len(game.enemies)+400*(nbaddys-1)
643         if not game.shldup:
644             forces += 1000 # Good for enemy if shield is down!
645         if not damaged(DPHASER) or not damaged(DPHOTON):
646             if damaged(DPHASER): # phasers damaged
647                 forces += 300.0
648             else:
649                 forces -= 0.2*(game.energy - 2500.0)
650             if damaged(DPHOTON): # photon torpedoes damaged
651                 forces += 300.0
652             else:
653                 forces -= 50.0*game.torps
654         else:
655             # phasers and photon tubes both out!
656             forces += 1000.0
657         motion = 0
658         if forces <= 1000.0 and game.condition != "docked": # Typical situation
659             motion = ((forces + rnd.real(200))/150.0) - 5.0
660         else:
661             if forces > 1000.0: # Very strong -- move in for kill
662                 motion = (1.0 - rnd.real())**2 * old_dist + 1.0
663             if game.condition == "docked" and (game.options & OPTION_BASE): # protected by base -- back off !
664                 motion -= game.skill*(2.0-rnd.real()**2)
665         if game.idebug:
666             proutn("=== MOTION = %d, FORCES = %1.2f, " % (motion, forces))
667         # don't move if no motion
668         if motion == 0:
669             return []
670         # Limit motion according to skill
671         if abs(motion) > game.skill:
672             if motion < 0:
673                 motion = -game.skill
674             else:
675                 motion = game.skill
676     # calculate preferred number of steps
677     nsteps = abs(int(motion))
678     if motion > 0 and nsteps > mdist:
679         nsteps = mdist # don't overshoot
680     if nsteps > QUADSIZE:
681         nsteps = QUADSIZE # This shouldn't be necessary
682     if nsteps < 1:
683         nsteps = 1 # This shouldn't be necessary
684     if game.idebug:
685         proutn("NSTEPS = %d:" % nsteps)
686     # Compute preferred values of delta X and Y
687     m = game.sector - enemy.location
688     if 2.0 * abs(m.i) < abs(m.j):
689         m.i = 0
690     if 2.0 * abs(m.j) < abs(game.sector.i-enemy.location.i):
691         m.j = 0
692     m = (motion * m).sgn()
693     goto = enemy.location
694     # main move loop
695     for ll in range(nsteps):
696         if game.idebug:
697             proutn(" %d" % (ll+1))
698         # Check if preferred position available
699         look = goto + m
700         if m.i < 0:
701             krawli = 1
702         else:
703             krawli = -1
704         if m.j < 0:
705             krawlj = 1
706         else:
707             krawlj = -1
708         success = False
709         attempts = 0 # Settle mysterious hang problem
710         while attempts < 20 and not success:
711             attempts += 1
712             if look.i < 0 or look.i >= QUADSIZE:
713                 if motion < 0:
714                     return tryexit(enemy, look, irun)
715                 if krawli == m.i or m.j == 0:
716                     break
717                 look.i = goto.i + krawli
718                 krawli = -krawli
719             elif look.j < 0 or look.j >= QUADSIZE:
720                 if motion < 0:
721                     return tryexit(enemy, look, irun)
722                 if krawlj == m.j or m.i == 0:
723                     break
724                 look.j = goto.j + krawlj
725                 krawlj = -krawlj
726             elif (game.options & OPTION_RAMMING) and game.quad[look.i][look.j] != '.':
727                 # See if enemy should ram ship
728                 if game.quad[look.i][look.j] == game.ship and \
729                     (enemy.type == 'C' or enemy.type == 'S'):
730                     collision(rammed=True, enemy=enemy)
731                     return []
732                 if krawli != m.i and m.j != 0:
733                     look.i = goto.i + krawli
734                     krawli = -krawli
735                 elif krawlj != m.j and m.i != 0:
736                     look.j = goto.j + krawlj
737                     krawlj = -krawlj
738                 else:
739                     break # we have failed
740             else:
741                 success = True
742         if success:
743             goto = look
744             if game.idebug:
745                 proutn(repr(goto))
746         else:
747             break # done early
748     if game.idebug:
749         skip(1)
750     # Enemy moved, but is still in sector
751     return [(False, enemy, old_dist, goto)]
752
753 def moveklings():
754     "Sequence Klingon tactical movement."
755     if game.idebug:
756         prout("== MOVCOM")
757     # Figure out which Klingon is the commander (or Supercommander)
758     # and do move
759     tacmoves = []
760     if game.quadrant in game.state.kcmdr:
761         for enemy in game.enemies:
762             if enemy.type == 'C':
763                 tacmoves += movebaddy(enemy)
764     if game.state.kscmdr == game.quadrant:
765         for enemy in game.enemies:
766             if enemy.type == 'S':
767                 tacmoves += movebaddy(enemy)
768                 break
769     # If skill level is high, move other Klingons and Romulans too!
770     # Move these last so they can base their actions on what the
771     # commander(s) do.
772     if game.skill >= SKILL_EXPERT and (game.options & OPTION_MVBADDY):
773         for enemy in game.enemies:
774             if enemy.type in ('K', 'R'):
775                 tacmoves += movebaddy(enemy)
776     return tacmoves
777
778 def movescom(iq, avoid):
779     "Supercommander movement helper."
780     # Avoid quadrants with bases if we want to avoid Enterprise
781     if not welcoming(iq) or (avoid and iq in game.state.baseq):
782         return False
783     if game.justin and not game.iscate:
784         return False
785     # do the move
786     game.state.galaxy[game.state.kscmdr.i][game.state.kscmdr.j].klingons -= 1
787     game.state.kscmdr = iq
788     game.state.galaxy[game.state.kscmdr.i][game.state.kscmdr.j].klingons += 1
789     if game.state.kscmdr == game.quadrant:
790         # SC has scooted, remove him from current quadrant
791         game.iscate = False
792         game.isatb = 0
793         game.ientesc = False
794         unschedule(FSCDBAS)
795         for enemy in game.enemies:
796             if enemy.type == 'S':
797                 enemy.move(None)
798         game.klhere -= 1
799         if game.condition != "docked":
800             newcnd()
801         sortenemies()
802     # check for a helpful planet
803     for i in range(game.inplan):
804         if game.state.planets[i].quadrant == game.state.kscmdr and \
805             game.state.planets[i].crystals == "present":
806             # destroy the planet
807             game.state.planets[i].pclass = "destroyed"
808             game.state.galaxy[game.state.kscmdr.i][game.state.kscmdr.j].planet = None
809             if communicating():
810                 announce()
811                 prout(_("Lt. Uhura-  \"Captain, Starfleet Intelligence reports"))
812                 prout(_("   a planet in Quadrant %s has been destroyed") % game.state.kscmdr)
813                 prout(_("   by the Super-commander.\""))
814             break
815     return True # looks good!
816
817 def supercommander():
818     "Move the Super Commander."
819     iq = Coord()
820     sc = Coord()
821     ibq = Coord()
822     idelta = Coord()
823     basetbl = []
824     if game.idebug:
825         prout("== SUPERCOMMANDER")
826     # Decide on being active or passive
827     avoid = ((game.incom - len(game.state.kcmdr) + game.inkling - game.remkl())/(game.state.date+0.01-game.indate) < 0.1*game.skill*(game.skill+1.0) or \
828             (game.state.date-game.indate) < 3.0)
829     if not game.iscate and avoid:
830         # compute move away from Enterprise
831         idelta = game.state.kscmdr-game.quadrant
832         if idelta.distance() > 2.0:
833             # circulate in space
834             idelta.i = game.state.kscmdr.j-game.quadrant.j
835             idelta.j = game.quadrant.i-game.state.kscmdr.i
836     else:
837         # compute distances to starbases
838         if not game.state.baseq:
839             # nothing left to do
840             unschedule(FSCMOVE)
841             return
842         sc = game.state.kscmdr
843         for (i, base) in enumerate(game.state.baseq):
844             basetbl.append((i, (base - sc).distance()))
845         if len(game.state.baseq) > 1:
846             basetbl.sort(key=lambda x: x[1])
847         # look for nearest base without a commander, no Enterprise, and
848         # without too many Klingons, and not already under attack.
849         ifindit = iwhichb = 0
850         for (i2, base) in enumerate(game.state.baseq):
851             i = basetbl[i2][0]        # bug in original had it not finding nearest
852             if base == game.quadrant or base == game.battle or not welcoming(base):
853                 continue
854             # if there is a commander, and no other base is appropriate,
855             # we will take the one with the commander
856             for cmdr in game.state.kcmdr:
857                 if base == cmdr and ifindit != 2:
858                     ifindit = 2
859                     iwhichb = i
860                     break
861             else:        # no commander -- use this one
862                 ifindit = 1
863                 iwhichb = i
864                 break
865         if ifindit == 0:
866             return # Nothing suitable -- wait until next time
867         ibq = game.state.baseq[iwhichb]
868         # decide how to move toward base
869         idelta = ibq - game.state.kscmdr
870     # Maximum movement is 1 quadrant in either or both axes
871     idelta = idelta.sgn()
872     # try moving in both x and y directions
873     # there was what looked like a bug in the Almy C code here,
874     # but it might be this translation is just wrong.
875     iq = game.state.kscmdr + idelta
876     if not movescom(iq, avoid):
877         # failed -- try some other maneuvers
878         if idelta.i == 0 or idelta.j == 0:
879             # attempt angle move
880             if idelta.i != 0:
881                 iq.j = game.state.kscmdr.j + 1
882                 if not movescom(iq, avoid):
883                     iq.j = game.state.kscmdr.j - 1
884                     movescom(iq, avoid)
885             elif idelta.j != 0:
886                 iq.i = game.state.kscmdr.i + 1
887                 if not movescom(iq, avoid):
888                     iq.i = game.state.kscmdr.i - 1
889                     movescom(iq, avoid)
890         else:
891             # try moving just in x or y
892             iq.j = game.state.kscmdr.j
893             if not movescom(iq, avoid):
894                 iq.j = game.state.kscmdr.j + idelta.j
895                 iq.i = game.state.kscmdr.i
896                 movescom(iq, avoid)
897     # check for a base
898     if len(game.state.baseq) == 0:
899         unschedule(FSCMOVE)
900     else:
901         for ibq in game.state.baseq:
902             if ibq == game.state.kscmdr and game.state.kscmdr == game.battle:
903                 # attack the base
904                 if avoid:
905                     return # no, don't attack base!
906                 game.iseenit = False
907                 game.isatb = 1
908                 schedule(FSCDBAS, rnd.real(1.0, 3.0))
909                 if is_scheduled(FCDBAS):
910                     postpone(FSCDBAS, scheduled(FCDBAS)-game.state.date)
911                 if not communicating():
912                     return # no warning
913                 game.iseenit = True
914                 announce()
915                 prout(_("Lt. Uhura-  \"Captain, the starbase in Quadrant %s") \
916                       % game.state.kscmdr)
917                 prout(_("   reports that it is under attack from the Klingon Super-commander."))
918                 prout(_("   It can survive until stardate %d.\"") \
919                        % int(scheduled(FSCDBAS)))
920                 if not game.resting:
921                     return
922                 prout(_("Mr. Spock-  \"Captain, shall we cancel the rest period?\""))
923                 if not ja():
924                     return
925                 game.resting = False
926                 game.optime = 0.0 # actually finished
927                 return
928     # Check for intelligence report
929     if not game.idebug and \
930         (rnd.withprob(0.8) or \
931          (not communicating()) or \
932          not game.state.galaxy[game.state.kscmdr.i][game.state.kscmdr.j].charted):
933         return
934     announce()
935     prout(_("Lt. Uhura-  \"Captain, Starfleet Intelligence reports"))
936     prout(_("   the Super-commander is in Quadrant %s.") % game.state.kscmdr)
937     return
938
939 def movetholian():
940     "Move the Tholian."
941     if not game.tholian or game.justin:
942         return
943     tid = Coord()
944     if game.tholian.location.i == 0 and game.tholian.location.j == 0:
945         tid.i = 0
946         tid.j = QUADSIZE-1
947     elif game.tholian.location.i == 0 and game.tholian.location.j == QUADSIZE-1:
948         tid.i = QUADSIZE-1
949         tid.j = QUADSIZE-1
950     elif game.tholian.location.i == QUADSIZE-1 and game.tholian.location.j == QUADSIZE-1:
951         tid.i = QUADSIZE-1
952         tid.j = 0
953     elif game.tholian.location.i == QUADSIZE-1 and game.tholian.location.j == 0:
954         tid.i = 0
955         tid.j = 0
956     else:
957         # something is wrong!
958         game.tholian.move(None)
959         prout("***Internal error: Tholian in a bad spot.")
960         return
961     # do nothing if we are blocked
962     if game.quad[tid.i][tid.j] not in ('.', '#'):
963         return
964     here = copy.copy(game.tholian.location)
965     delta = (tid - game.tholian.location).sgn()
966     # move in x axis
967     while here.i != tid.i:
968         here.i += delta.i
969         if game.quad[here.i][here.j] == '.':
970             game.tholian.move(here)
971     # move in y axis
972     while here.j != tid.j:
973         here.j += delta.j
974         if game.quad[here.i][here.j] == '.':
975             game.tholian.move(here)
976     # check to see if all holes plugged
977     for i in range(QUADSIZE):
978         if game.quad[0][i] != '#' and game.quad[0][i] != 'T':
979             return
980         if game.quad[QUADSIZE-1][i] != '#' and game.quad[QUADSIZE-1][i] != 'T':
981             return
982         if game.quad[i][0] != '#' and game.quad[i][0] != 'T':
983             return
984         if game.quad[i][QUADSIZE-1] != '#' and game.quad[i][QUADSIZE-1] != 'T':
985             return
986     # All plugged up -- Tholian splits
987     game.quad[game.tholian.location.i][game.tholian.location.j] = '#'
988     dropin(' ')
989     prout(crmena(True, 'T', "sector", game.tholian) + _(" completes web."))
990     game.tholian.move(None)
991     return
992
993 # Code from battle.c begins here
994
995 def cloak():
996     "Change cloaking-device status."
997     if game.ship == 'F':
998         prout(_("Ye Faerie Queene hath no cloaking device."))
999         return
1000
1001     key = scanner.nexttok()
1002
1003     if key == "IHREAL":
1004         huh()
1005         return
1006
1007     action = None
1008     if key == "IHALPHA":
1009         if scanner.sees("on"):
1010             if game.iscloaked:
1011                 prout(_("The cloaking device has already been switched on."))
1012                 return
1013             action = "CLON"
1014         elif scanner.sees("off"):
1015             if not game.iscloaked:
1016                 prout(_("The cloaking device has already been switched off."))
1017                 return
1018             action = "CLOFF"
1019         else:
1020             huh()
1021             return
1022     else:
1023         if not game.iscloaked:
1024             proutn(_("Switch cloaking device on? "))
1025             if not ja():
1026                 return
1027             action = "CLON"
1028         else:
1029             proutn(_("Switch cloaking device off? "))
1030             if not ja():
1031                 return
1032             action = "CLOFF"
1033     if action is None:
1034         return
1035
1036     if action == "CLOFF":
1037         if game.irhere and game.state.date >= ALGERON and not game.isviolreported:
1038             prout(_("Spock- \"Captain, the Treaty of Algeron is in effect.\n   Are you sure this is wise?\""))
1039             if not ja():
1040                 return
1041         prout("Engineer Scott- \"Aye, Sir.\"")
1042         game.iscloaked = False
1043         if game.irhere and game.state.date >= ALGERON and not game.isviolreported:
1044             prout(_("The Romulan ship discovers you are breaking the Treaty of Algeron!"))
1045             game.ncviol += 1
1046             game.isviolreported = True
1047
1048             #if (neutz and game.state.date >= ALGERON) finish(FCLOAK);
1049             return
1050
1051     if action == "CLON":
1052         if damaged(DCLOAK):
1053             prout(_("Engineer Scott- \"The cloaking device is damaged, Sir.\""))
1054             return
1055
1056         if game.condition == "docked":
1057             prout(_("You cannot cloak while docked."))
1058
1059         if game.state.date >= ALGERON and not game.isviolreported:
1060             prout(_("Spock- \"Captain, using the cloaking device is a violation"))
1061             prout(_("  of the Treaty of Algeron. Considering the alternatives,"))
1062             proutn(_("  are you sure this is wise? "))
1063             if not ja():
1064                 return
1065         prout(_("Engineer Scott- \"Cloaking device has engaging, Sir...\""))
1066         attack(True)
1067         prout(_("Engineer Scott- \"Cloaking device has engaged, Sir.\""))
1068         game.iscloaked = True
1069
1070         if game.irhere and game.state.date >= ALGERON and not game.isviolreported:
1071             prout(_("The Romulan ship discovers you are breaking the Treaty of Algeron!"))
1072             game.ncviol += 1
1073             game.isviolreported = True
1074
1075 def doshield(shraise):
1076     "Change shield status."
1077     action = "NONE"
1078     game.ididit = False
1079     if shraise:
1080         action = "SHUP"
1081     else:
1082         key = scanner.nexttok()
1083         if key == "IHALPHA":
1084             if scanner.sees("transfer"):
1085                 action = "NRG"
1086             else:
1087                 if damaged(DSHIELD):
1088                     prout(_("Shields damaged and down."))
1089                     return
1090                 if scanner.sees("up"):
1091                     action = "SHUP"
1092                 elif scanner.sees("down"):
1093                     action = "SHDN"
1094         if action == "NONE":
1095             proutn(_("Do you wish to change shield energy? "))
1096             if ja():
1097                 action = "NRG"
1098             elif damaged(DSHIELD):
1099                 prout(_("Shields damaged and down."))
1100                 return
1101             elif game.shldup:
1102                 proutn(_("Shields are up. Do you want them down? "))
1103                 if ja():
1104                     action = "SHDN"
1105                 else:
1106                     scanner.chew()
1107                     return
1108             else:
1109                 proutn(_("Shields are down. Do you want them up? "))
1110                 if ja():
1111                     action = "SHUP"
1112                 else:
1113                     scanner.chew()
1114                     return
1115     if action == "SHUP": # raise shields
1116         if game.shldup:
1117             prout(_("Shields already up."))
1118             return
1119         game.shldup = True
1120         game.shldchg = True
1121         if game.condition != "docked":
1122             game.energy -= 50.0
1123         prout(_("Shields raised."))
1124         if game.energy <= 0:
1125             skip(1)
1126             prout(_("Shields raising uses up last of energy."))
1127             finish(FNRG)
1128             return
1129         game.ididit = True
1130         return
1131     elif action == "SHDN":
1132         if not game.shldup:
1133             prout(_("Shields already down."))
1134             return
1135         game.shldup = False
1136         game.shldchg = True
1137         prout(_("Shields lowered."))
1138         game.ididit = True
1139         return
1140     elif action == "NRG":
1141         while scanner.nexttok() != "IHREAL":
1142             scanner.chew()
1143             proutn(_("Energy to transfer to shields- "))
1144         nrg = scanner.real
1145         scanner.chew()
1146         if nrg == 0:
1147             return
1148         if nrg > game.energy:
1149             prout(_("Insufficient ship energy."))
1150             return
1151         game.ididit = True
1152         if game.shield+nrg >= game.inshld:
1153             prout(_("Shield energy maximized."))
1154             if game.shield+nrg > game.inshld:
1155                 prout(_("Excess energy requested returned to ship energy"))
1156             game.energy -= game.inshld-game.shield
1157             game.shield = game.inshld
1158             return
1159         if nrg < 0.0 and game.energy-nrg > game.inenrg:
1160             # Prevent shield drain loophole
1161             skip(1)
1162             prout(_("Engineering to bridge--"))
1163             prout(_("  Scott here. Power circuit problem, Captain."))
1164             prout(_("  I can't drain the shields."))
1165             game.ididit = False
1166             return
1167         if game.shield+nrg < 0:
1168             prout(_("All shield energy transferred to ship."))
1169             game.energy += game.shield
1170             game.shield = 0.0
1171             return
1172         proutn(_("Scotty- \""))
1173         if nrg > 0:
1174             prout(_("Transferring energy to shields.\""))
1175         else:
1176             prout(_("Draining energy from shields.\""))
1177         game.shield += nrg
1178         game.energy -= nrg
1179         return
1180
1181 def randdevice():
1182     "Choose a device to damage, at random."
1183     weights = (
1184         105,       # DSRSENS: short range scanners         10.5%
1185         105,       # DLRSENS: long range scanners          10.5%
1186         120,       # DPHASER: phasers                      12.0%
1187         120,       # DPHOTON: photon torpedoes             12.0%
1188         25,        # DLIFSUP: life support                  2.5%
1189         65,        # DWARPEN: warp drive                    6.5%
1190         70,        # DIMPULS: impulse engines               6.5%
1191         135,       # DSHIELD: deflector shields            13.5%
1192         30,        # DRADIO:  subspace radio                3.0%
1193         45,        # DSHUTTL: shuttle                       4.5%
1194         15,        # DCOMPTR: computer                      1.5%
1195         20,        # NAVCOMP: navigation system             2.0%
1196         75,        # DTRANSP: transporter                   7.5%
1197         20,        # DSHCTRL: high-speed shield controller  2.0%
1198         10,        # DDRAY: death ray                       1.0%
1199         30,        # DDSP: deep-space probes                3.0%
1200         10,        # DCLOAK: the cloaking device            1.0
1201     )
1202     assert(sum(weights) == 1000)
1203     idx = rnd.integer(1000)
1204     wsum = 0
1205     for (i, w) in enumerate(weights):
1206         wsum += w
1207         if idx < wsum:
1208             return i
1209     return None        # we should never get here
1210
1211 def collision(rammed, enemy):
1212     "Collision handling for rammong events."
1213     prouts(_("***RED ALERT!  RED ALERT!"))
1214     skip(1)
1215     prout(_("***COLLISION IMMINENT."))
1216     skip(2)
1217     proutn("***")
1218     proutn(crmshp())
1219     hardness = {'R':1.5, 'C':2.0, 'S':2.5, 'T':0.5, '?':4.0}.get(enemy.type, 1.0)
1220     if rammed:
1221         proutn(_(" rammed by "))
1222     else:
1223         proutn(_(" rams "))
1224     proutn(crmena(False, enemy.type, "sector", enemy.location))
1225     if rammed:
1226         proutn(_(" (original position)"))
1227     skip(1)
1228     deadkl(enemy.location, enemy.type, game.sector)
1229     proutn("***" + crmshp() + " heavily damaged.")
1230     icas = rnd.integer(10, 30)
1231     prout(_("***Sickbay reports %d casualties") % icas)
1232     game.casual += icas
1233     game.state.crew -= icas
1234     # In the pre-SST2K version, all devices got equiprobably damaged,
1235     # which was silly.  Instead, pick up to half the devices at
1236     # random according to our weighting table,
1237     ncrits = rnd.integer(NDEVICES//2)
1238     while ncrits > 0:
1239         ncrits -= 1
1240         dev = randdevice()
1241         if game.damage[dev] < 0:
1242             continue
1243         extradm = (10.0*hardness*rnd.real()+1.0)*game.damfac
1244         # Damage for at least time of travel!
1245         game.damage[dev] += game.optime + extradm
1246     game.shldup = False
1247     prout(_("***Shields are down."))
1248     if game.unwon():
1249         announce()
1250         damagereport()
1251     else:
1252         finish(FWON)
1253
1254 def torpedo(origin, bearing, dispersion, number, nburst):
1255     "Let a photon torpedo fly"
1256     if not damaged(DSRSENS) or game.condition == "docked":
1257         setwnd(srscan_window)
1258     else:
1259         setwnd(message_window)
1260     ac = bearing + 0.25*dispersion        # dispersion is a random variable
1261     bullseye = (15.0 - bearing)*0.5235988
1262     track = course(bearing=ac, distance=QUADSIZE, origin=cartesian(origin))
1263     bumpto = Coord(0, 0)
1264     # Loop to move a single torpedo
1265     setwnd(message_window)
1266     for step in range(1, QUADSIZE*2):
1267         if not track.nexttok():
1268             break
1269         w = track.sector()
1270         if not w.valid_sector():
1271             break
1272         iquad = game.quad[w.i][w.j]
1273         tracktorpedo(w, step, number, nburst, iquad)
1274         if iquad == '.':
1275             continue
1276         # hit something
1277         setwnd(message_window)
1278         if not damaged(DSRSENS) or game.condition == "docked":
1279             skip(1)        # start new line after text track
1280         if iquad in ('E', 'F'): # Hit our ship
1281             skip(1)
1282             prout(_("Torpedo hits %s.") % crmshp())
1283             hit = 700.0 + rnd.real(100) - \
1284                 1000.0 * (w-origin).distance() * math.fabs(math.sin(bullseye-track.angle))
1285             newcnd() # we're blown out of dock
1286             if game.landed or game.condition == "docked":
1287                 return hit # Cheat if on a planet
1288             # In the C/FORTRAN version, dispersion was 2.5 radians, which
1289             # is 143 degrees, which is almost exactly 4.8 clockface units
1290             displacement = course(track.bearing+rnd.real(-2.4, 2.4), distance=2**0.5)
1291             displacement.nexttok()
1292             bumpto = displacement.sector()
1293             if not bumpto.valid_sector():
1294                 return hit
1295             if game.quad[bumpto.i][bumpto.j] == ' ':
1296                 finish(FHOLE)
1297                 return hit
1298             if game.quad[bumpto.i][bumpto.j] != '.':
1299                 # can't move into object
1300                 return hit
1301             game.sector = bumpto
1302             proutn(crmshp())
1303             game.quad[w.i][w.j] = '.'
1304             game.quad[bumpto.i][bumpto.j] = iquad
1305             prout(_(" displaced by blast to Sector %s ") % bumpto)
1306             for enemy in game.enemies:
1307                 enemy.kdist = enemy.kavgd = (game.sector-enemy.location).distance()
1308             sortenemies()
1309             return None
1310         elif iquad in ('C', 'S', 'R', 'K'): # Hit a regular enemy
1311             # find the enemy
1312             if iquad in ('C', 'S') and rnd.withprob(0.05):
1313                 prout(crmena(True, iquad, "sector", w) + _(" uses anti-photon device;"))
1314                 prout(_("   torpedo neutralized."))
1315                 return None
1316             for enemy in game.enemies:
1317                 if w == enemy.location:
1318                     kp = math.fabs(enemy.power)
1319                     h1 = 700.0 + rnd.integer(100) - \
1320                         1000.0 * (w-origin).distance() * math.fabs(math.sin(bullseye-track.angle))
1321                     h1 = math.fabs(h1)
1322                     if kp < h1:
1323                         h1 = kp
1324                     if enemy.power < 0:
1325                         enemy.power -= -h1
1326                     else:
1327                         enemy.power -= h1
1328                     if enemy.power == 0:
1329                         deadkl(w, iquad, w)
1330                         return None
1331                     proutn(crmena(True, iquad, "sector", w))
1332                     displacement = course(track.bearing+rnd.real(-2.4, 2.4), distance=2**0.5)
1333                     displacement.nexttok()
1334                     bumpto = displacement.sector()
1335                     if not bumpto.valid_sector():
1336                         prout(_(" damaged but not destroyed."))
1337                         return None
1338                     if game.quad[bumpto.i][bumpto.j] == ' ':
1339                         prout(_(" buffeted into black hole."))
1340                         deadkl(w, iquad, bumpto)
1341                     if game.quad[bumpto.i][bumpto.j] != '.':
1342                         prout(_(" damaged but not destroyed."))
1343                     else:
1344                         prout(_(" damaged-- displaced by blast to Sector %s ")%bumpto)
1345                         enemy.location = bumpto
1346                         game.quad[w.i][w.j] = '.'
1347                         game.quad[bumpto.i][bumpto.j] = iquad
1348                         for tenemy in game.enemies:
1349                             tenemy.kdist = tenemy.kavgd = (game.sector-tenemy.location).distance()
1350                         sortenemies()
1351                     break
1352             else:
1353                 prout("Internal error, no enemy where expected!")
1354                 raise SystemExit(1)
1355             return None
1356         elif iquad == 'B': # Hit a base
1357             skip(1)
1358             prout(_("***STARBASE DESTROYED.."))
1359             game.state.baseq = [x for x in game.state.baseq if x != game.quadrant]
1360             game.quad[w.i][w.j] = '.'
1361             game.base.invalidate()
1362             game.state.galaxy[game.quadrant.i][game.quadrant.j].starbase = False
1363             game.state.chart[game.quadrant.i][game.quadrant.j].starbase = False
1364             game.state.basekl += 1
1365             newcnd()
1366             return None
1367         elif iquad == 'P': # Hit a planet
1368             prout(crmena(True, iquad, "sector", w) + _(" destroyed."))
1369             game.state.nplankl += 1
1370             game.state.galaxy[game.quadrant.i][game.quadrant.j].planet = None
1371             game.iplnet.pclass = "destroyed"
1372             game.iplnet = None
1373             game.plnet.invalidate()
1374             game.quad[w.i][w.j] = '.'
1375             if game.landed:
1376                 # captain perishes on planet
1377                 finish(FDPLANET)
1378             return None
1379         elif iquad == '@': # Hit an inhabited world -- very bad!
1380             prout(crmena(True, iquad, "sector", w) + _(" destroyed."))
1381             game.state.nworldkl += 1
1382             game.state.galaxy[game.quadrant.i][game.quadrant.j].planet = None
1383             game.iplnet.pclass = "destroyed"
1384             game.iplnet = None
1385             game.plnet.invalidate()
1386             game.quad[w.i][w.j] = '.'
1387             if game.landed:
1388                 # captain perishes on planet
1389                 finish(FDPLANET)
1390             prout(_("The torpedo destroyed an inhabited planet."))
1391             return None
1392         elif iquad == '*': # Hit a star
1393             if rnd.withprob(0.9):
1394                 nova(w)
1395             else:
1396                 prout(crmena(True, '*', "sector", w) + _(" unaffected by photon blast."))
1397             return None
1398         elif iquad == '?': # Hit a thingy
1399             if not (game.options & OPTION_THINGY) or rnd.withprob(0.3):
1400                 skip(1)
1401                 prouts(_("AAAAIIIIEEEEEEEEAAAAAAAAUUUUUGGGGGHHHHHHHHHHHH!!!"))
1402                 skip(1)
1403                 prouts(_("    HACK!     HACK!    HACK!        *CHOKE!*  "))
1404                 skip(1)
1405                 proutn(_("Mr. Spock-"))
1406                 prouts(_("  \"Fascinating!\""))
1407                 skip(1)
1408                 deadkl(w, iquad, w)
1409             else:
1410                 # Stas Sergeev added the possibility that
1411                 # you can shove the Thingy and piss it off.
1412                 # It then becomes an enemy and may fire at you.
1413                 thing.angry()
1414             return None
1415         elif iquad == ' ': # Black hole
1416             skip(1)
1417             prout(crmena(True, ' ', "sector", w) + _(" swallows torpedo."))
1418             return None
1419         elif iquad == '#': # hit the web
1420             skip(1)
1421             prout(_("***Torpedo absorbed by Tholian web."))
1422             return None
1423         elif iquad == 'T':  # Hit a Tholian
1424             h1 = 700.0 + rnd.integer(100) - \
1425                 1000.0 * (w-origin).distance() * math.fabs(math.sin(bullseye-track.angle))
1426             h1 = math.fabs(h1)
1427             if h1 >= 600:
1428                 game.quad[w.i][w.j] = '.'
1429                 deadkl(w, iquad, w)
1430                 game.tholian = None
1431                 return None
1432             skip(1)
1433             proutn(crmena(True, 'T', "sector", w))
1434             if rnd.withprob(0.05):
1435                 prout(_(" survives photon blast."))
1436                 return None
1437             prout(_(" disappears."))
1438             game.tholian.move(None)
1439             game.quad[w.i][w.j] = '#'
1440             dropin(' ')
1441             return None
1442         else: # Problem!
1443             skip(1)
1444             proutn("Don't know how to handle torpedo collision with ")
1445             proutn(crmena(True, iquad, "sector", w))
1446             skip(1)
1447             return None
1448         break
1449     skip(1)
1450     setwnd(message_window)
1451     prout(_("Torpedo missed."))
1452     return None
1453
1454 def fry(hit):
1455     "Critical-hit resolution."
1456     if hit < (275.0-25.0*game.skill)*rnd.real(1.0, 1.5):
1457         return
1458     ncrit = int(1.0 + hit/(500.0+rnd.real(100)))
1459     proutn(_("***CRITICAL HIT--"))
1460     # Select devices and cause damage
1461     cdam = []
1462     while ncrit > 0:
1463         while True:
1464             j = randdevice()
1465             # Cheat to prevent shuttle damage unless on ship
1466             if not (game.damage[j]<0.0 or (j == DSHUTTL and game.iscraft != "onship") or (j == DCLOAK and game.ship != 'E' or j == DDRAY)):
1467                 break
1468         cdam.append(j)
1469         extradm = (hit*game.damfac)/(ncrit*rnd.real(75, 100))
1470         game.damage[j] += extradm
1471         ncrit -= 1
1472     skipcount = 0
1473     for (i, j) in enumerate(cdam):
1474         proutn(device[j])
1475         if skipcount % 3 == 2 and i < len(cdam)-1:
1476             skip(1)
1477         skipcount += 1
1478         if i < len(cdam)-1:
1479             proutn(_(" and "))
1480     prout(_(" damaged."))
1481     if damaged(DSHIELD) and game.shldup:
1482         prout(_("***Shields knocked down."))
1483         game.shldup = False
1484     if damaged(DCLOAK) and game.iscloaked:
1485         prout(_("***Cloaking device rendered inoperative."))
1486         game.iscloaked = False
1487
1488 def attack(torps_ok):
1489     # bad guy attacks us
1490     # torps_ok == False forces use of phasers in an attack
1491     if game.iscloaked:
1492         return
1493     # game could be over at this point, check
1494     if game.alldone:
1495         return
1496     attempt = False
1497     ihurt = False
1498     hitmax = 0.0
1499     hittot = 0.0
1500     chgfac = 1.0
1501     where = "neither"
1502     if game.idebug:
1503         prout("=== ATTACK!")
1504     # Tholian gets to move before attacking
1505     if game.tholian:
1506         movetholian()
1507     # if you have just entered the RNZ, you'll get a warning
1508     if game.neutz: # The one chance not to be attacked
1509         game.neutz = False
1510         return
1511     # commanders get a chance to tac-move towards you
1512     if (((game.quadrant in game.state.kcmdr or game.state.kscmdr == game.quadrant) and not game.justin) or game.skill == SKILL_EMERITUS) and torps_ok:
1513         for (bugout, enemy, old, goto) in  moveklings():
1514             if bugout:
1515                 # we know about this if either short or long range
1516                 # sensors are working
1517                 if damaged(DSRSENS) and damaged(DLRSENS) \
1518                        and game.condition != "docked":
1519                     prout(crmena(True, enemy.type, "sector", old) + \
1520                           (_(" escapes to Quadrant %s (and regains strength).") % goto))
1521             else: # Enemy still in-sector
1522                 if enemy.move(goto):
1523                     if not damaged(DSRSENS) or game.condition == "docked":
1524                         proutn(_("*** %s from Sector %s") % (cramen(enemy.type), enemy.location))
1525                         if enemy.kdist < old:
1526                             proutn(_(" advances to "))
1527                         else:
1528                             proutn(_(" retreats to "))
1529                         prout("Sector %s." % goto)
1530         sortenemies()
1531     # if no enemies remain after movement, we're done
1532     if len(game.enemies) == 0 or (len(game.enemies) == 1 and thing.at(game.quadrant) and not thing.angered):
1533         return
1534     # set up partial hits if attack happens during shield status change
1535     pfac = 1.0/game.inshld
1536     if game.shldchg:
1537         chgfac = 0.25 + rnd.real(0.5)
1538     skip(1)
1539     # message verbosity control
1540     if game.skill <= SKILL_FAIR:
1541         where = "sector"
1542     for enemy in game.enemies:
1543         if enemy.power < 0:
1544             continue        # too weak to attack
1545         # compute hit strength and diminish shield power
1546         r = rnd.real()
1547         # Increase chance of photon torpedos if docked or enemy energy is low
1548         if game.condition == "docked":
1549             r *= 0.25
1550         if enemy.power < 500:
1551             r *= 0.25
1552         if enemy.type == 'T' or (enemy.type == '?' and not thing.angered):
1553             continue
1554         # different enemies have different probabilities of throwing a torp
1555         usephasers = not torps_ok or \
1556             (enemy.type == 'K' and r > 0.0005) or \
1557             (enemy.type == 'C' and r > 0.015) or \
1558             (enemy.type == 'R' and r > 0.3) or \
1559             (enemy.type == 'S' and r > 0.07) or \
1560             (enemy.type == '?' and r > 0.05)
1561         if usephasers:            # Enemy uses phasers
1562             if game.condition == "docked":
1563                 continue # Don't waste the effort!
1564             attempt = True # Attempt to attack
1565             dustfac = rnd.real(0.8, 0.85)
1566             hit = enemy.power*math.pow(dustfac, enemy.kavgd)
1567             enemy.power *= 0.75
1568         else: # Enemy uses photon torpedo
1569             # We should be able to make the bearing() method work here
1570             pcourse = 1.90985*math.atan2(game.sector.j-enemy.location.j, enemy.location.i-game.sector.i)
1571             hit = 0
1572             proutn(_("***TORPEDO INCOMING"))
1573             if not damaged(DSRSENS):
1574                 proutn(_(" From ") + crmena(False, enemy.type, where, enemy.location))
1575             attempt = True
1576             prout("  ")
1577             dispersion = (rnd.real()+rnd.real())*0.5 - 0.5
1578             dispersion += 0.002*enemy.power*dispersion
1579             hit = torpedo(enemy.location, pcourse, dispersion, number=1, nburst=1)
1580             if game.unwon() == 0:
1581                 finish(FWON) # Klingons did themselves in!
1582             if game.state.galaxy[game.quadrant.i][game.quadrant.j].supernova or game.alldone:
1583                 return # Supernova or finished
1584             if hit is None:
1585                 continue
1586         # incoming phaser or torpedo, shields may dissipate it
1587         if game.shldup or game.shldchg or game.condition == "docked":
1588             # shields will take hits
1589             propor = pfac * game.shield
1590             if game.condition == "docked":
1591                 propor *= 2.1
1592             if propor < 0.1:
1593                 propor = 0.1
1594             hitsh = propor*chgfac*hit+1.0
1595             absorb = 0.8*hitsh
1596             if absorb > game.shield:
1597                 absorb = game.shield
1598             game.shield -= absorb
1599             hit -= hitsh
1600             # taking a hit blasts us out of a starbase dock
1601             if game.condition == "docked":
1602                 dock(False)
1603             # but the shields may take care of it
1604             if propor > 0.1 and hit < 0.005*game.energy:
1605                 continue
1606         # hit from this opponent got through shields, so take damage
1607         ihurt = True
1608         proutn(_("%d unit hit") % int(hit))
1609         if (damaged(DSRSENS) and usephasers) or game.skill<=SKILL_FAIR:
1610             proutn(_(" on the ") + crmshp())
1611         if not damaged(DSRSENS) and usephasers:
1612             prout(_(" from ") + crmena(False, enemy.type, where, enemy.location))
1613         skip(1)
1614         # Decide if hit is critical
1615         if hit > hitmax:
1616             hitmax = hit
1617         hittot += hit
1618         fry(hit)
1619         game.energy -= hit
1620     if game.energy <= 0:
1621         # Returning home upon your shield, not with it...
1622         finish(FBATTLE)
1623         return
1624     if not attempt and game.condition == "docked":
1625         prout(_("***Enemies decide against attacking your ship."))
1626     percent = 100.0*pfac*game.shield+0.5
1627     if not ihurt:
1628         # Shields fully protect ship
1629         proutn(_("Enemy attack reduces shield strength to "))
1630     else:
1631         # Emit message if starship suffered hit(s)
1632         skip(1)
1633         proutn(_("Energy left %2d    shields ") % int(game.energy))
1634         if game.shldup:
1635             proutn(_("up "))
1636         elif not damaged(DSHIELD):
1637             proutn(_("down "))
1638         else:
1639             proutn(_("damaged, "))
1640     prout(_("%d%%,   torpedoes left %d") % (percent, game.torps))
1641     # Check if anyone was hurt
1642     if hitmax >= 200 or hittot >= 500:
1643         icas = rnd.integer(int(hittot * 0.015))
1644         if icas >= 2:
1645             skip(1)
1646             prout(_("Mc Coy-  \"Sickbay to bridge.  We suffered %d casualties") % icas)
1647             prout(_("   in that last attack.\""))
1648             game.casual += icas
1649             game.state.crew -= icas
1650     # After attack, reset average distance to enemies
1651     for enemy in game.enemies:
1652         enemy.kavgd = enemy.kdist
1653     sortenemies()
1654     return
1655
1656 def deadkl(w, etype, mv):
1657     "Kill a Klingon, Tholian, Romulan, or Thingy."
1658     # Added mv to allow enemy to "move" before dying
1659     proutn(crmena(True, etype, "sector", mv))
1660     # Decide what kind of enemy it is and update appropriately
1661     if etype == 'R':
1662         # Chalk up a Romulan
1663         game.state.galaxy[game.quadrant.i][game.quadrant.j].romulans -= 1
1664         game.irhere -= 1
1665         game.state.nromrem -= 1
1666     elif etype == 'T':
1667         # Killed a Tholian
1668         game.tholian = None
1669     elif etype == '?':
1670         # Killed a Thingy
1671         global thing
1672         thing = None
1673     else:
1674         # Killed some type of Klingon
1675         game.state.galaxy[game.quadrant.i][game.quadrant.j].klingons -= 1
1676         game.klhere -= 1
1677         if etype == 'C':
1678             game.state.kcmdr.remove(game.quadrant)
1679             unschedule(FTBEAM)
1680             if game.state.kcmdr:
1681                 schedule(FTBEAM, expran(1.0*game.incom/len(game.state.kcmdr)))
1682             if is_scheduled(FCDBAS) and game.battle == game.quadrant:
1683                 unschedule(FCDBAS)
1684         elif etype ==  'K':
1685             pass
1686         elif etype ==  'S':
1687             game.state.nscrem -= 1
1688             game.state.kscmdr.invalidate()
1689             game.isatb = 0
1690             game.iscate = False
1691             unschedule(FSCMOVE)
1692             unschedule(FSCDBAS)
1693     # For each kind of enemy, finish message to player
1694     prout(_(" destroyed."))
1695     if game.unwon() == 0:
1696         return
1697     game.recompute()
1698     # Remove enemy ship from arrays describing local conditions
1699     for e in game.enemies:
1700         if e.location == w:
1701             e.move(None)
1702             break
1703     return
1704
1705 def targetcheck(w):
1706     "Return None if target is invalid, otherwise return a course angle."
1707     if not w.valid_sector():
1708         huh()
1709         return None
1710     delta = Coord()
1711     # C code this was translated from is wacky -- why the sign reversal?
1712     delta.j = (w.j - game.sector.j)
1713     delta.i = (game.sector.i - w.i)
1714     if delta == Coord(0, 0):
1715         skip(1)
1716         prout(_("Spock-  \"Bridge to sickbay.  Dr. McCoy,"))
1717         prout(_("  I recommend an immediate review of"))
1718         prout(_("  the Captain's psychological profile.\""))
1719         scanner.chew()
1720         return None
1721     return delta.bearing()
1722
1723 def torps():
1724     "Launch photon torpedo salvo."
1725     tcourse = []
1726     game.ididit = False
1727     if damaged(DPHOTON):
1728         prout(_("Photon tubes damaged."))
1729         scanner.chew()
1730         return
1731     if game.torps == 0:
1732         prout(_("No torpedoes left."))
1733         scanner.chew()
1734         return
1735     # First, get torpedo count
1736     while True:
1737         scanner.nexttok()
1738         if scanner.token == "IHALPHA":
1739             huh()
1740             return
1741         elif scanner.token == "IHEOL" or not scanner.waiting():
1742             prout(_("%d torpedoes left.") % game.torps)
1743             scanner.chew()
1744             proutn(_("Number of torpedoes to fire- "))
1745             continue        # Go back around to get a number
1746         else: # key == "IHREAL"
1747             try:
1748                 n = scanner.int()
1749             except TypeError:
1750                 huh()
1751                 return
1752             if n <= 0: # abort command
1753                 scanner.chew()
1754                 return
1755             if n > MAXBURST:
1756                 scanner.chew()
1757                 prout(_("Maximum of %d torpedoes per burst.") % MAXBURST)
1758                 return
1759             if n > game.torps:
1760                 scanner.chew()        # User requested more torps than available
1761                 continue        # Go back around
1762             break        # All is good, go to next stage
1763     # Next, get targets
1764     target = []
1765     for i in range(n):
1766         key = scanner.nexttok()
1767         if i == 0 and key == "IHEOL":
1768             break        # no coordinate waiting, we will try prompting
1769         if i == 1 and key == "IHEOL":
1770             # direct all torpedoes at one target
1771             while i < n:
1772                 target.append(target[0])
1773                 tcourse.append(tcourse[0])
1774                 i += 1
1775             break
1776         scanner.push(scanner.token)
1777         target.append(scanner.getcoord())
1778         if target[-1] is None:
1779             return
1780         tcourse.append(targetcheck(target[-1]))
1781         if tcourse[-1] is None:
1782             return
1783     scanner.chew()
1784     if len(target) == 0:
1785         # prompt for each one
1786         for i in range(n):
1787             proutn(_("Target sector for torpedo number %d- ") % (i+1))
1788             scanner.chew()
1789             target.append(scanner.getcoord())
1790             if target[-1] is None:
1791                 return
1792             tcourse.append(targetcheck(target[-1]))
1793             if tcourse[-1] is None:
1794                 return
1795     game.ididit = True
1796     # Loop for moving <n> torpedoes
1797     for i in range(n):
1798         if game.condition != "docked":
1799             game.torps -= 1
1800         dispersion = (rnd.real()+rnd.real())*0.5 -0.5
1801         if math.fabs(dispersion) >= 0.47:
1802             # misfire!
1803             dispersion *= rnd.real(1.2, 2.2)
1804             if n > 0:
1805                 prouts(_("***TORPEDO NUMBER %d MISFIRES") % (i+1))
1806             else:
1807                 prouts(_("***TORPEDO MISFIRES."))
1808             skip(1)
1809             if i < n:
1810                 prout(_("  Remainder of burst aborted."))
1811             if rnd.withprob(0.2):
1812                 prout(_("***Photon tubes damaged by misfire."))
1813                 game.damage[DPHOTON] = game.damfac * rnd.real(1.0, 3.0)
1814             break
1815         if game.iscloaked:
1816             dispersion *= 1.2
1817         elif game.shldup or game.condition == "docked":
1818             dispersion *= 1.0 + 0.0001*game.shield
1819         torpedo(game.sector, tcourse[i], dispersion, number=i, nburst=n)
1820         if game.alldone or game.state.galaxy[game.quadrant.i][game.quadrant.j].supernova:
1821             return
1822     if game.unwon()<=0:
1823         finish(FWON)
1824
1825 def overheat(rpow):
1826     "Check for phasers overheating."
1827     if rpow > 1500:
1828         checkburn = (rpow-1500.0)*0.00038
1829         if rnd.withprob(checkburn):
1830             prout(_("Weapons officer Sulu-  \"Phasers overheated, sir.\""))
1831             game.damage[DPHASER] = game.damfac* rnd.real(1.0, 2.0) * (1.0+checkburn)
1832
1833 def checkshctrl(rpow):
1834     "Check shield control."
1835     skip(1)
1836     if rnd.withprob(0.998):
1837         prout(_("Shields lowered."))
1838         return False
1839     # Something bad has happened
1840     prouts(_("***RED ALERT!  RED ALERT!"))
1841     skip(2)
1842     hit = rpow*game.shield/game.inshld
1843     game.energy -= rpow+hit*0.8
1844     game.shield -= hit*0.2
1845     if game.energy <= 0.0:
1846         prouts(_("Sulu-  \"Captain! Shield malf***********************\""))
1847         skip(1)
1848         stars()
1849         finish(FPHASER)
1850         return True
1851     prouts(_("Sulu-  \"Captain! Shield malfunction! Phaser fire contained!\""))
1852     skip(2)
1853     prout(_("Lt. Uhura-  \"Sir, all decks reporting damage.\""))
1854     icas = rnd.integer(int(hit*0.012))
1855     skip(1)
1856     fry(0.8*hit)
1857     if icas:
1858         skip(1)
1859         prout(_("McCoy to bridge- \"Severe radiation burns, Jim."))
1860         prout(_("  %d casualties so far.\"") % icas)
1861         game.casual += icas
1862         game.state.crew -= icas
1863     skip(1)
1864     prout(_("Phaser energy dispersed by shields."))
1865     prout(_("Enemy unaffected."))
1866     overheat(rpow)
1867     return True
1868
1869 def hittem(hits):
1870     "Register a phaser hit on Klingons and Romulans."
1871     w = Coord()
1872     skip(1)
1873     kk = 0
1874     for wham in hits:
1875         if wham == 0:
1876             continue
1877         dustfac = rnd.real(0.9, 1.0)
1878         hit = wham*math.pow(dustfac, game.enemies[kk].kdist)
1879         kpini = game.enemies[kk].power
1880         kp = math.fabs(kpini)
1881         if PHASEFAC*hit < kp:
1882             kp = PHASEFAC*hit
1883         if game.enemies[kk].power < 0:
1884             game.enemies[kk].power -= -kp
1885         else:
1886             game.enemies[kk].power -= kp
1887         kpow = game.enemies[kk].power
1888         w = game.enemies[kk].location
1889         if hit > 0.005:
1890             if not damaged(DSRSENS):
1891                 boom(w)
1892             proutn(_("%d unit hit on ") % int(hit))
1893         else:
1894             proutn(_("Very small hit on "))
1895         ienm = game.quad[w.i][w.j]
1896         if ienm == '?':
1897             thing.angry()
1898         proutn(crmena(False, ienm, "sector", w))
1899         skip(1)
1900         if kpow == 0:
1901             deadkl(w, ienm, w)
1902             if game.unwon()==0:
1903                 finish(FWON)
1904             if game.alldone:
1905                 return
1906             continue
1907         else: # decide whether or not to emasculate klingon
1908             # pylint: disable=chained-comparison
1909             if kpow > 0 and rnd.withprob(0.9) and kpow <= rnd.real(0.4, 0.8)*kpini:
1910                 prout(_("***Mr. Spock-  \"Captain, the vessel at Sector %s")%w)
1911                 prout(_("   has just lost its firepower.\""))
1912                 game.enemies[kk].power = -kpow
1913         kk += 1
1914     return
1915
1916 def phasers():
1917     "Fire phasers at bad guys."
1918     hits = []
1919     kz = 0
1920     k = 1
1921     irec = 0 # Cheating inhibitor
1922     ifast = False
1923     no = False
1924     itarg = True
1925     msgflag = True
1926     rpow = 0.0
1927     automode = "NOTSET"
1928     key = ""
1929     skip(1)
1930     # SR sensors and Computer are needed for automode
1931     if damaged(DSRSENS) or damaged(DCOMPTR):
1932         itarg = False
1933     if game.condition == "docked":
1934         prout(_("Phasers can't be fired through base shields."))
1935         scanner.chew()
1936         return
1937     if damaged(DPHASER):
1938         prout(_("Phaser control damaged."))
1939         scanner.chew()
1940         return
1941     if game.shldup:
1942         if damaged(DSHCTRL):
1943             prout(_("High speed shield control damaged."))
1944             scanner.chew()
1945             return
1946         if game.energy <= 200.0:
1947             prout(_("Insufficient energy to activate high-speed shield control."))
1948             scanner.chew()
1949             return
1950         prout(_("Weapons Officer Sulu-  \"High-speed shield control enabled, sir.\""))
1951         ifast = True
1952     # Original code so convoluted, I re-did it all
1953     # (That was Tom Almy talking about the C code, I think -- ESR)
1954     while automode == "NOTSET":
1955         key = scanner.nexttok()
1956         if key == "IHALPHA":
1957             if scanner.sees("manual"):
1958                 if len(game.enemies)==0:
1959                     prout(_("There is no enemy present to select."))
1960                     scanner.chew()
1961                     key = "IHEOL"
1962                     automode = "AUTOMATIC"
1963                 else:
1964                     automode = "MANUAL"
1965                     key = scanner.nexttok()
1966             elif scanner.sees("automatic"):
1967                 if (not itarg) and len(game.enemies) != 0:
1968                     automode = "FORCEMAN"
1969                 else:
1970                     if len(game.enemies)==0:
1971                         prout(_("Energy will be expended into space."))
1972                     automode = "AUTOMATIC"
1973                     key = scanner.nexttok()
1974             elif scanner.sees("no"):
1975                 no = True
1976             else:
1977                 huh()
1978                 return
1979         elif key == "IHREAL":
1980             if len(game.enemies)==0:
1981                 prout(_("Energy will be expended into space."))
1982                 automode = "AUTOMATIC"
1983             elif not itarg:
1984                 automode = "FORCEMAN"
1985             else:
1986                 automode = "AUTOMATIC"
1987         else:
1988             # "IHEOL"
1989             if len(game.enemies)==0:
1990                 prout(_("Energy will be expended into space."))
1991                 automode = "AUTOMATIC"
1992             elif not itarg:
1993                 automode = "FORCEMAN"
1994             else:
1995                 proutn(_("Manual or automatic? "))
1996                 scanner.chew()
1997     avail = game.energy
1998     if ifast:
1999         avail -= 200.0
2000     if automode == "AUTOMATIC":
2001         if key == "IHALPHA" and scanner.sees("no"):
2002             no = True
2003             key = scanner.nexttok()
2004         if key != "IHREAL" and len(game.enemies) != 0:
2005             prout(_("Phasers locked on target. Energy available: %.2f")%avail)
2006         irec = 0
2007         while True:
2008             scanner.chew()
2009             if not kz:
2010                 for i in range(len(game.enemies)):
2011                     irec += math.fabs(game.enemies[i].power)/(PHASEFAC*math.pow(0.90, game.enemies[i].kdist))*rnd.real(1.01, 1.06) + 1.0
2012             kz = 1
2013             proutn(_("%d units required. ") % irec)
2014             scanner.chew()
2015             proutn(_("Units to fire= "))
2016             key = scanner.nexttok()
2017             if key != "IHREAL":
2018                 return
2019             rpow = scanner.real
2020             if rpow > avail:
2021                 proutn(_("Energy available= %.2f") % avail)
2022                 skip(1)
2023                 key = "IHEOL"
2024             if not rpow > avail:
2025                 break
2026         if rpow <= 0:
2027             # chicken out
2028             scanner.chew()
2029             return
2030         key = scanner.nexttok()
2031         if key == "IHALPHA" and scanner.sees("no"):
2032             no = True
2033         if ifast:
2034             game.energy -= 200 # Go and do it!
2035             if checkshctrl(rpow):
2036                 return
2037         scanner.chew()
2038         game.energy -= rpow
2039         extra = rpow
2040         if len(game.enemies):
2041             extra = 0.0
2042             powrem = rpow
2043             for i in range(len(game.enemies)):
2044                 hits.append(0.0)
2045                 if powrem <= 0:
2046                     continue
2047                 hits[i] = math.fabs(game.enemies[i].power)/(PHASEFAC*math.pow(0.90, game.enemies[i].kdist))
2048                 over = rnd.real(1.01, 1.06) * hits[i]
2049                 temp = powrem
2050                 powrem -= hits[i] + over
2051                 if powrem <= 0 and temp < hits[i]:
2052                     hits[i] = temp
2053                 if powrem <= 0:
2054                     over = 0.0
2055                 extra += over
2056             if powrem > 0.0:
2057                 extra += powrem
2058             hittem(hits)
2059             game.ididit = True
2060         if extra > 0 and not game.alldone:
2061             if game.tholian:
2062                 proutn(_("*** Tholian web absorbs "))
2063                 if len(game.enemies)>0:
2064                     proutn(_("excess "))
2065                 prout(_("phaser energy."))
2066             else:
2067                 prout(_("%d expended on empty space.") % int(extra))
2068     elif automode == "FORCEMAN":
2069         scanner.chew()
2070         key = "IHEOL"
2071         if damaged(DCOMPTR):
2072             prout(_("Battle computer damaged, manual fire only."))
2073         else:
2074             skip(1)
2075             prouts(_("---WORKING---"))
2076             skip(1)
2077             prout(_("Short-range-sensors-damaged"))
2078             prout(_("Insufficient-data-for-automatic-phaser-fire"))
2079             prout(_("Manual-fire-must-be-used"))
2080             skip(1)
2081     elif automode == "MANUAL":
2082         rpow = 0.0
2083         for k in range(len(game.enemies)):
2084             aim = game.enemies[k].location
2085             ienm = game.quad[aim.i][aim.j]
2086             if msgflag:
2087                 proutn(_("Energy available= %.2f") % (avail-0.006))
2088                 skip(1)
2089                 msgflag = False
2090                 rpow = 0.0
2091             if damaged(DSRSENS) and \
2092                not game.sector.distance(aim)<2**0.5 and ienm in ('C', 'S'):
2093                 prout(cramen(ienm) + _(" can't be located without short range scan."))
2094                 scanner.chew()
2095                 key = "IHEOL"
2096                 hits[k] = 0 # prevent overflow -- thanks to Alexei Voitenko
2097                 continue
2098             if key == "IHEOL":
2099                 scanner.chew()
2100                 if itarg and k > kz:
2101                     irec = (abs(game.enemies[k].power)/(PHASEFAC*math.pow(0.9, game.enemies[k].kdist))) *        rnd.real(1.01, 1.06) + 1.0
2102                 kz = k
2103                 proutn("(")
2104                 if not damaged(DCOMPTR):
2105                     proutn("%d" % irec)
2106                 else:
2107                     proutn("??")
2108                 proutn(")  ")
2109                 proutn(_("units to fire at %s-  ") % crmena(False, ienm, "sector", aim))
2110                 key = scanner.nexttok()
2111             if key == "IHALPHA" and scanner.sees("no"):
2112                 no = True
2113                 key = scanner.nexttok()
2114                 continue
2115             if key == "IHALPHA":
2116                 huh()
2117                 return
2118             if key == "IHEOL":
2119                 if k == 1: # Let me say I'm baffled by this
2120                     msgflag = True
2121                 continue
2122             if scanner.real < 0:
2123                 # abort out
2124                 scanner.chew()
2125                 return
2126             hits.append(scanner.real)
2127             rpow += scanner.real
2128             # If total requested is too much, inform and start over
2129             if rpow > avail:
2130                 prout(_("Available energy exceeded -- try again."))
2131                 scanner.chew()
2132                 return
2133             key = scanner.nexttok() # scan for next value
2134         if rpow == 0.0:
2135             # zero energy -- abort
2136             scanner.chew()
2137             return
2138         if key == "IHALPHA" and scanner.sees("no"):
2139             no = True
2140         game.energy -= rpow
2141         scanner.chew()
2142         if ifast:
2143             game.energy -= 200.0
2144             if checkshctrl(rpow):
2145                 return
2146         hittem(hits)
2147         game.ididit = True
2148      # Say shield raised or malfunction, if necessary
2149     if game.alldone:
2150         return
2151     if ifast:
2152         skip(1)
2153         if no == 0:
2154             if rnd.withprob(0.01):
2155                 prout(_("Sulu-  \"Sir, the high-speed shield control has malfunctioned . . ."))
2156                 prouts(_("         CLICK   CLICK   POP  . . ."))
2157                 prout(_(" No response, sir!"))
2158                 game.shldup = False
2159             else:
2160                 prout(_("Shields raised."))
2161         else:
2162             game.shldup = False
2163     overheat(rpow)
2164
2165
2166 def capture():
2167     game.ididit = False # Nothing if we fail
2168     game.optime = 0.0
2169
2170     # Make sure there is room in the brig
2171     if game.brigfree == 0:
2172         prout(_("Security reports the brig is already full."))
2173         return
2174
2175     if damaged(DRADIO):
2176         prout(_("Uhura- \"We have no subspace radio communication, sir.\""))
2177         return
2178
2179     if damaged(DTRANSP):
2180         prout(_("Scotty- \"Transporter damaged, sir.\""))
2181         return
2182
2183     # find out if there are any at all
2184     if game.klhere < 1:
2185         prout(_("Uhura- \"Getting no response, sir.\""))
2186         return
2187
2188     # if there is more than one Klingon, find out which one
2189     #   Cruddy, just takes one at random.  Should ask the captain.
2190     #   Nah, just select the weakest one since it is most likely to
2191     #   surrender (Tom Almy mod)
2192     klingons = [e for e in game.enemies if e.type == 'K']
2193     weakest = sorted(klingons, key=lambda e: e.power)[0]
2194     game.optime = 0.05          # This action will take some time
2195     game.ididit = True #  So any others can strike back
2196
2197     # check out that Klingon
2198     # The algorithm isn't that great and could use some more
2199     # intelligent design
2200     # x = 300 + 25*skill;
2201     x = game.energy / (weakest.power * len(klingons))
2202     #prout(_("Stats: energy = %s, kpower = %s, klingons = %s")
2203     #      % (game.energy, weakest.power, len(klingons)))
2204     x *= 2.5    # would originally have been equivalent of 1.4,
2205                 # but we want command to work more often, more humanely
2206     #prout(_("Prob = %.4f" % x))
2207     #   x = 100; // For testing, of course!
2208     if x < rnd.real(100):
2209         # guess what, he surrendered!!!
2210         prout(_("Klingon captain at %s surrenders.") % weakest.location)
2211         i = rnd.real(200)
2212         if i > 0:
2213             prout(_("%d Klingons commit suicide rather than be taken captive.") % (200 - i))
2214         if i > game.brigfree:
2215             prout(_("%d Klingons die because there is no room for them in the brig.") % (i-game.brigfree))
2216             i = game.brigfree
2217         game.brigfree -= i
2218         prout(_("%d captives taken") % i)
2219         deadkl(weakest.location, weakest.type, game.sector)
2220         if game.unwon()<=0:
2221             finish(FWON)
2222         return
2223
2224         # big surprise, he refuses to surrender
2225     prout(_("Fat chance, captain!"))
2226
2227 # Code from events.c begins here.
2228
2229 # This isn't a real event queue a la BSD Trek yet -- you can only have one
2230 # event of each type active at any given time.  Mostly these means we can
2231 # only have one FDISTR/FENSLV/FREPRO sequence going at any given time
2232 # BSD Trek, from which we swiped the idea, can have up to 5.
2233
2234 def unschedule(evtype):
2235     "Remove an event from the schedule."
2236     game.future[evtype].date = FOREVER
2237     return game.future[evtype]
2238
2239 def is_scheduled(evtype):
2240     "Is an event of specified type scheduled."
2241     return game.future[evtype].date != FOREVER
2242
2243 def scheduled(evtype):
2244     "When will this event happen?"
2245     return game.future[evtype].date
2246
2247 def schedule(evtype, offset):
2248     "Schedule an event of specified type."
2249     game.future[evtype].date = game.state.date + offset
2250     return game.future[evtype]
2251
2252 def postpone(evtype, offset):
2253     "Postpone a scheduled event."
2254     game.future[evtype].date += offset
2255
2256 def cancelrest():
2257     "Rest period is interrupted by event."
2258     if game.resting:
2259         skip(1)
2260         proutn(_("Mr. Spock-  \"Captain, shall we cancel the rest period?\""))
2261         if ja():
2262             game.resting = False
2263             game.optime = 0.0
2264             return True
2265     return False
2266
2267 def events():
2268     "Run through the event queue looking for things to do."
2269     i = 0
2270     fintim = game.state.date + game.optime
2271     yank = 0
2272     ictbeam = False
2273     istract = False
2274     w = Coord()
2275     hold = Coord()
2276     ev = Event()
2277     ev2 = Event()
2278
2279     def tractorbeam(yank):
2280         "Tractor-beaming cases merge here."
2281         announce()
2282         game.optime = (10.0/(7.5*7.5))*yank # 7.5 is yank rate (warp 7.5)
2283         skip(1)
2284         prout("***" + crmshp() + _(" caught in long range tractor beam--"))
2285         # If Kirk & Co. screwing around on planet, handle
2286         atover(True) # atover(true) is Grab
2287         if game.alldone:
2288             return
2289         if game.icraft: # Caught in Galileo?
2290             finish(FSTRACTOR)
2291             return
2292         # Check to see if shuttle is aboard
2293         if game.iscraft == "offship":
2294             skip(1)
2295             if rnd.withprob(0.5):
2296                 prout(_("Galileo, left on the planet surface, is captured"))
2297                 prout(_("by aliens and made into a flying McDonald's."))
2298                 game.damage[DSHUTTL] = -10
2299                 game.iscraft = "removed"
2300             else:
2301                 prout(_("Galileo, left on the planet surface, is well hidden."))
2302         if evcode == FSPY:
2303             game.quadrant = game.state.kscmdr
2304         else:
2305             game.quadrant = game.state.kcmdr[i]
2306         game.sector = randplace(QUADSIZE)
2307         prout(crmshp() + _(" is pulled to Quadrant %s, Sector %s") \
2308                % (game.quadrant, game.sector))
2309         if game.resting:
2310             prout(_("(Remainder of rest/repair period cancelled.)"))
2311             game.resting = False
2312         if not game.shldup:
2313             if not damaged(DSHIELD) and game.shield > 0:
2314                 doshield(shraise=True) # raise shields
2315                 game.shldchg = False
2316             else:
2317                 prout(_("(Shields not currently useable.)"))
2318         newqad()
2319         # Adjust finish time to time of tractor beaming?
2320         # fintim = game.state.date+game.optime
2321         attack(torps_ok=False)
2322         if not game.state.kcmdr:
2323             unschedule(FTBEAM)
2324         else:
2325             schedule(FTBEAM, game.optime+expran(1.5*game.intime/len(game.state.kcmdr)))
2326
2327     def destroybase():
2328         "Code merges here for any commander destroying a starbase."
2329         # Not perfect, but will have to do
2330         # Handle case where base is in same quadrant as starship
2331         if game.battle == game.quadrant:
2332             game.state.chart[game.battle.i][game.battle.j].starbase = False
2333             game.quad[game.base.i][game.base.j] = '.'
2334             game.base.invalidate()
2335             newcnd()
2336             skip(1)
2337             prout(_("Spock-  \"Captain, I believe the starbase has been destroyed.\""))
2338         elif game.state.baseq and communicating():
2339             # Get word via subspace radio
2340             announce()
2341             skip(1)
2342             prout(_("Lt. Uhura-  \"Captain, Starfleet Command reports that"))
2343             proutn(_("   the starbase in Quadrant %s has been destroyed by") % game.battle)
2344             if game.isatb == 2:
2345                 prout(_("the Klingon Super-Commander"))
2346             else:
2347                 prout(_("a Klingon Commander"))
2348             game.state.chart[game.battle.i][game.battle.j].starbase = False
2349         # Remove Starbase from galaxy
2350         game.state.galaxy[game.battle.i][game.battle.j].starbase = False
2351         game.state.baseq = [x for x in game.state.baseq if x != game.battle]
2352         if game.isatb == 2:
2353             # reinstate a commander's base attack
2354             game.battle = hold
2355             game.isatb = 0
2356         else:
2357             game.battle.invalidate()
2358     if game.idebug:
2359         prout("=== EVENTS from %.2f to %.2f:" % (game.state.date, fintim))
2360         for i in range(1, NEVENTS):
2361             if   i == FSNOVA:  proutn("=== Supernova       ")
2362             elif i == FTBEAM:  proutn("=== T Beam          ")
2363             elif i == FSNAP:   proutn("=== Snapshot        ")
2364             elif i == FBATTAK: proutn("=== Base Attack     ")
2365             elif i == FCDBAS:  proutn("=== Base Destroy    ")
2366             elif i == FSCMOVE: proutn("=== SC Move         ")
2367             elif i == FSCDBAS: proutn("=== SC Base Destroy ")
2368             elif i == FDSPROB: proutn("=== Probe Move      ")
2369             elif i == FDISTR:  proutn("=== Distress Call   ")
2370             elif i == FENSLV:  proutn("=== Enslavement     ")
2371             elif i == FREPRO:  proutn("=== Klingon Build   ")
2372             if is_scheduled(i):
2373                 prout("%.2f" % (scheduled(i)))
2374             else:
2375                 prout("never")
2376     radio_was_broken = damaged(DRADIO)
2377     hold.i = hold.j = 0
2378     while True:
2379         # Select earliest extraneous event, evcode==0 if no events
2380         evcode = FSPY
2381         if game.alldone:
2382             return
2383         datemin = fintim
2384         for l in range(1, NEVENTS):
2385             if game.future[l].date < datemin:
2386                 evcode = l
2387                 if game.idebug:
2388                     prout("== Event %d fires" % evcode)
2389                 datemin = game.future[l].date
2390         xtime = datemin-game.state.date
2391         if game.iscloaked:
2392             game.energy -= xtime*500.0
2393             if game.energy <= 0:
2394                 finish(FNRG)
2395                 return
2396         game.state.date = datemin
2397         # Decrement Federation resources and recompute remaining time
2398         game.state.remres -= (game.remkl()+4*len(game.state.kcmdr))*xtime
2399         game.recompute()
2400         if game.state.remtime <= 0:
2401             finish(FDEPLETE)
2402             return
2403         # Any crew left alive?
2404         if game.state.crew <= 0:
2405             finish(FCREW)
2406             return
2407         # Is life support adequate?
2408         if damaged(DLIFSUP) and game.condition != "docked":
2409             if game.lsupres < xtime and game.damage[DLIFSUP] > game.lsupres:
2410                 finish(FLIFESUP)
2411                 return
2412             game.lsupres -= xtime
2413             if game.damage[DLIFSUP] <= xtime:
2414                 game.lsupres = game.inlsr
2415         # Fix devices
2416         repair = xtime
2417         if game.condition == "docked":
2418             repair /= DOCKFAC
2419         # Don't fix Deathray here
2420         for l in range(NDEVICES):
2421             if game.damage[l] > 0.0 and l != DDRAY:
2422                 if game.damage[l]-repair > 0.0:
2423                     game.damage[l] -= repair
2424                 else:
2425                     game.damage[l] = 0.0
2426         # If radio repaired, update star chart and attack reports
2427         if radio_was_broken and not damaged(DRADIO):
2428             prout(_("Lt. Uhura- \"Captain, the sub-space radio is working and"))
2429             prout(_("   surveillance reports are coming in."))
2430             skip(1)
2431             if not game.iseenit:
2432                 attackreport(False)
2433                 game.iseenit = True
2434             rechart()
2435             prout(_("   The star chart is now up to date.\""))
2436             skip(1)
2437         # Cause extraneous event EVCODE to occur
2438         game.optime -= xtime
2439         if evcode == FSNOVA: # Supernova
2440             announce()
2441             supernova(None)
2442             schedule(FSNOVA, expran(0.5*game.intime))
2443             if game.state.galaxy[game.quadrant.i][game.quadrant.j].supernova:
2444                 return
2445         elif evcode == FSPY: # Check with spy to see if SC should tractor beam
2446             if game.state.nscrem == 0 or game.iscloaked or \
2447                 ictbeam or istract or \
2448                 game.condition == "docked" or game.isatb == 1 or game.iscate:
2449                 return
2450             if game.ientesc or \
2451                 (game.energy<2000 and game.torps<4 and game.shield < 1250) or \
2452                 (damaged(DPHASER) and (damaged(DPHOTON) or game.torps<4)) or \
2453                 (damaged(DSHIELD) and \
2454                  (game.energy < 2500 or damaged(DPHASER)) and \
2455                  (game.torps < 5 or damaged(DPHOTON))):
2456                 # Tractor-beam her!
2457                 istract = ictbeam = True
2458                 tractorbeam((game.state.kscmdr-game.quadrant).distance())
2459             else:
2460                 return
2461         elif evcode == FTBEAM: # Tractor beam
2462             if not game.state.kcmdr:
2463                 unschedule(FTBEAM)
2464                 continue
2465             i = rnd.integer(len(game.state.kcmdr))
2466             yank = (game.state.kcmdr[i]-game.quadrant).distance()
2467             if istract or game.condition == "docked" or game.iscloaked or yank == 0:
2468                 # Drats! Have to reschedule
2469                 schedule(FTBEAM,
2470                          game.optime + expran(1.5*game.intime/len(game.state.kcmdr)))
2471                 continue
2472             ictbeam = True
2473             tractorbeam(yank)
2474         elif evcode == FSNAP: # Snapshot of the universe (for time warp)
2475             game.snapsht = copy.deepcopy(game.state)
2476             game.state.snap = True
2477             schedule(FSNAP, expran(0.5 * game.intime))
2478         elif evcode == FBATTAK: # Commander attacks starbase
2479             if not game.state.kcmdr or not game.state.baseq:
2480                 # no can do
2481                 unschedule(FBATTAK)
2482                 unschedule(FCDBAS)
2483                 continue
2484             ibq = None  # Force battle location to persist past loop
2485             try:
2486                 for ibq in game.state.baseq:
2487                     for cmdr in game.state.kcmdr:
2488                         if ibq == cmdr and ibq != game.quadrant and ibq != game.state.kscmdr:
2489                             raise JumpOut
2490                 # no match found -- try later
2491                 schedule(FBATTAK, expran(0.3*game.intime))
2492                 unschedule(FCDBAS)
2493                 continue
2494             except JumpOut:
2495                 pass
2496             # commander + starbase combination found -- launch attack
2497             game.battle = ibq
2498             schedule(FCDBAS, rnd.real(1.0, 4.0))
2499             if game.isatb: # extra time if SC already attacking
2500                 postpone(FCDBAS, scheduled(FSCDBAS)-game.state.date)
2501             game.future[FBATTAK].date = game.future[FCDBAS].date + expran(0.3*game.intime)
2502             game.iseenit = False
2503             if not communicating():
2504                 continue # No warning :-(
2505             game.iseenit = True
2506             announce()
2507             skip(1)
2508             prout(_("Lt. Uhura-  \"Captain, the starbase in Quadrant %s") % game.battle)
2509             prout(_("   reports that it is under attack and that it can"))
2510             prout(_("   hold out only until stardate %d.\"") % (int(scheduled(FCDBAS))))
2511             if cancelrest():
2512                 return
2513         elif evcode == FSCDBAS: # Supercommander destroys base
2514             unschedule(FSCDBAS)
2515             game.isatb = 2
2516             if not game.state.galaxy[game.state.kscmdr.i][game.state.kscmdr.j].starbase:
2517                 continue # WAS RETURN!
2518             hold = game.battle
2519             game.battle = game.state.kscmdr
2520             destroybase()
2521         elif evcode == FCDBAS: # Commander succeeds in destroying base
2522             if evcode == FCDBAS:
2523                 unschedule(FCDBAS)
2524                 if not game.state.baseq() \
2525                        or not game.state.galaxy[game.battle.i][game.battle.j].starbase:
2526                     game.battle.invalidate()
2527                     continue
2528                 # find the lucky pair
2529                 for cmdr in game.state.kcmdr:
2530                     if cmdr == game.battle:
2531                         break
2532                 else:
2533                     # No action to take after all
2534                     continue
2535             destroybase()
2536         elif evcode == FSCMOVE: # Supercommander moves
2537             schedule(FSCMOVE, 0.2777)
2538             if not game.ientesc and not istract and game.isatb != 1 and \
2539                    (not game.iscate or not game.justin):
2540                 supercommander()
2541         elif evcode == FDSPROB: # Move deep space probe
2542             schedule(FDSPROB, 0.01)
2543             if not game.probe.nexttok():
2544                 if not game.probe.quadrant().valid_quadrant() or \
2545                     game.state.galaxy[game.probe.quadrant().i][game.probe.quadrant().j].supernova:
2546                     # Left galaxy or ran into supernova
2547                     if communicating():
2548                         announce()
2549                         skip(1)
2550                         proutn(_("Lt. Uhura-  \"The deep space probe "))
2551                         if not game.probe.quadrant().valid_quadrant():
2552                             prout(_("has left the galaxy.\""))
2553                         else:
2554                             prout(_("is no longer transmitting.\""))
2555                     unschedule(FDSPROB)
2556                     continue
2557                 if communicating():
2558                     #announce()
2559                     skip(1)
2560                     prout(_("Lt. Uhura-  \"The deep space probe is now in Quadrant %s.\"") % game.probe.quadrant())
2561             pquad = game.probe.quadrant()
2562             pdest = game.state.galaxy[pquad.i][pquad.j]
2563             if communicating():
2564                 game.state.chart[pquad.i][pquad.j].klingons = pdest.klingons
2565                 game.state.chart[pquad.i][pquad.j].starbase = pdest.starbase
2566                 game.state.chart[pquad.i][pquad.j].stars = pdest.stars
2567                 pdest.charted = True
2568             game.probe.moves -= 1 # One less to travel
2569             if game.probe.arrived() and game.isarmed and pdest.stars:
2570                 supernova(game.probe)                # fire in the hole!
2571                 unschedule(FDSPROB)
2572                 if game.state.galaxy[pquad.i][pquad.j].supernova:
2573                     return
2574         elif evcode == FDISTR: # inhabited system issues distress call
2575             unschedule(FDISTR)
2576             # try a whole bunch of times to find something suitable
2577             for i in range(100):
2578                 # need a quadrant which is not the current one,
2579                 # which has some stars which are inhabited and
2580                 # not already under attack, which is not
2581                 # supernova'ed, and which has some Klingons in it
2582                 w = randplace(GALSIZE)
2583                 q = game.state.galaxy[w.i][w.j]
2584                 if not (game.quadrant == w or q.planet is None or \
2585                       not q.planet.inhabited or \
2586                       q.supernova or q.status!="secure" or q.klingons<=0):
2587                     break
2588             else:
2589                 # can't seem to find one; ignore this call
2590                 if game.idebug:
2591                     prout("=== Couldn't find location for distress event.")
2592                 continue
2593             # got one!!  Schedule its enslavement
2594             ev = schedule(FENSLV, expran(game.intime))
2595             ev.quadrant = w
2596             q.status = "distressed"
2597             # tell the captain about it if we can
2598             if communicating():
2599                 prout(_("Uhura- Captain, %s in Quadrant %s reports it is under attack") \
2600                         % (q.planet, repr(w)))
2601                 prout(_("by a Klingon invasion fleet."))
2602                 if cancelrest():
2603                     return
2604         elif evcode == FENSLV:                # starsystem is enslaved
2605             ev = unschedule(FENSLV)
2606             # see if current distress call still active
2607             q = game.state.galaxy[ev.quadrant.i][ev.quadrant.j]
2608             if q.klingons <= 0:
2609                 q.status = "secure"
2610                 continue
2611             q.status = "enslaved"
2612
2613             # play stork and schedule the first baby
2614             ev2 = schedule(FREPRO, expran(2.0 * game.intime))
2615             ev2.quadrant = ev.quadrant
2616
2617             # report the disaster if we can
2618             if communicating():
2619                 prout(_("Uhura- We've lost contact with starsystem %s") % \
2620                         q.planet)
2621                 prout(_("in Quadrant %s.\n") % ev.quadrant)
2622         elif evcode == FREPRO:                # Klingon reproduces
2623             # If we ever switch to a real event queue, we'll need to
2624             # explicitly retrieve and restore the x and y.
2625             ev = schedule(FREPRO, expran(1.0 * game.intime))
2626             # see if current distress call still active
2627             q = game.state.galaxy[ev.quadrant.i][ev.quadrant.j]
2628             if q.klingons <= 0:
2629                 q.status = "secure"
2630                 continue
2631             if game.remkl() >= MAXKLGAME:
2632                 continue                # full right now
2633             # reproduce one Klingon
2634             w = ev.quadrant
2635             m = Coord()
2636             if game.klhere >= MAXKLQUAD:
2637                 try:
2638                     # this quadrant not ok, pick an adjacent one
2639                     for m.i in range(w.i - 1, w.i + 2):
2640                         for m.j in range(w.j - 1, w.j + 2):
2641                             if not m.valid_quadrant():
2642                                 continue
2643                             q = game.state.galaxy[m.i][m.j]
2644                             # check for this quad ok (not full & no snova)
2645                             if q.klingons >= MAXKLQUAD or q.supernova:
2646                                 continue
2647                             raise JumpOut
2648                     # search for eligible quadrant failed
2649                     continue
2650                 except JumpOut:
2651                     w = m
2652             # deliver the child
2653             q.klingons += 1
2654             if game.quadrant == w:
2655                 game.klhere += 1
2656                 newkling() # also adds it to game.enemies
2657             # recompute time left
2658             game.recompute()
2659             if communicating():
2660                 if game.quadrant == w:
2661                     prout(_("Spock- sensors indicate the Klingons have"))
2662                     prout(_("launched a warship from %s.") % q.planet)
2663                 else:
2664                     prout(_("Uhura- Starfleet reports increased Klingon activity"))
2665                     if q.planet is not None:
2666                         proutn(_("near %s ") % q.planet)
2667                     prout(_("in Quadrant %s.") % w)
2668
2669 def wait():
2670     "Wait on events."
2671     game.ididit = False
2672     while True:
2673         key = scanner.nexttok()
2674         if key  != "IHEOL":
2675             break
2676         proutn(_("How long? "))
2677         scanner.chew()
2678     if key != "IHREAL":
2679         huh()
2680         return
2681     origTime = delay = scanner.real
2682     if delay <= 0.0:
2683         return
2684     if delay >= game.state.remtime or len(game.enemies) != 0:
2685         proutn(_("Are you sure? "))
2686         if not ja():
2687             return
2688     # Alternate resting periods (events) with attacks
2689     game.resting = True
2690     while True:
2691         if delay <= 0:
2692             game.resting = False
2693         if not game.resting:
2694             prout(_("%d stardates left.") % int(game.state.remtime))
2695             return
2696         temp = game.optime = delay
2697         if len(game.enemies):
2698             rtime = rnd.real(1.0, 2.0)
2699             if rtime < temp:
2700                 temp = rtime
2701             game.optime = temp
2702         if game.optime < delay:
2703             attack(torps_ok=False)
2704         if game.alldone:
2705             return
2706         events()
2707         game.ididit = True
2708         if game.alldone:
2709             return
2710         delay -= temp
2711         # Repair Deathray if long rest at starbase
2712         if origTime-delay >= 9.99 and game.condition == "docked":
2713             game.damage[DDRAY] = 0.0
2714         # leave if quadrant supernovas
2715         if game.state.galaxy[game.quadrant.i][game.quadrant.j].supernova:
2716             break
2717     game.resting = False
2718     game.optime = 0.0
2719
2720 def nova(nov):
2721     "Star goes nova."
2722     ncourse = (0.0, 10.5, 12.0, 1.5, 9.0, 0.0, 3.0, 7.5, 6.0, 4.5)
2723     newc = Coord(); neighbor = Coord(); bump = Coord(0, 0)
2724     if rnd.withprob(0.05):
2725         # Wow! We've supernova'ed
2726         supernova(game.quadrant)
2727         return
2728     # handle initial nova
2729     game.quad[nov.i][nov.j] = '.'
2730     prout(crmena(False, '*', "sector", nov) + _(" novas."))
2731     game.state.galaxy[game.quadrant.i][game.quadrant.j].stars -= 1
2732     game.state.starkl += 1
2733     # Set up queue to recursively trigger adjacent stars
2734     hits = [nov]
2735     kount = 0
2736     while hits:
2737         offset = Coord()
2738         start = hits.pop()
2739         for offset.i in range(-1, 1+1):
2740             for offset.j in range(-1, 1+1):
2741                 if offset.j == 0 and offset.i == 0:
2742                     continue
2743                 neighbor = start + offset
2744                 if not neighbor.valid_sector():
2745                     continue
2746                 iquad = game.quad[neighbor.i][neighbor.j]
2747                 # Empty space ends reaction
2748                 if iquad in ('.', '?', ' ', 'T', '#'):
2749                     pass
2750                 elif iquad == '*': # Affect another star
2751                     if rnd.withprob(0.05):
2752                         # This star supernovas
2753                         supernova(game.quadrant)
2754                         return
2755                     else:
2756                         hits.append(neighbor)
2757                         game.state.galaxy[game.quadrant.i][game.quadrant.j].stars -= 1
2758                         game.state.starkl += 1
2759                         proutn(crmena(True, '*', "sector", neighbor))
2760                         prout(_(" novas."))
2761                         game.quad[neighbor.i][neighbor.j] = '.'
2762                         kount += 1
2763                 elif iquad in ('P', '@'): # Destroy planet
2764                     game.state.galaxy[game.quadrant.i][game.quadrant.j].planet = None
2765                     if iquad == 'P':
2766                         game.state.nplankl += 1
2767                     else:
2768                         game.state.nworldkl += 1
2769                     prout(crmena(True, 'B', "sector", neighbor) + _(" destroyed."))
2770                     game.iplnet.pclass = "destroyed"
2771                     game.iplnet = None
2772                     game.plnet.invalidate()
2773                     if game.landed:
2774                         finish(FPNOVA)
2775                         return
2776                     game.quad[neighbor.i][neighbor.j] = '.'
2777                 elif iquad == 'B': # Destroy base
2778                     game.state.galaxy[game.quadrant.i][game.quadrant.j].starbase = False
2779                     game.state.baseq = [x for x in game.state.baseq if x!= game.quadrant]
2780                     game.base.invalidate()
2781                     game.state.basekl += 1
2782                     newcnd()
2783                     prout(crmena(True, 'B', "sector", neighbor) + _(" destroyed."))
2784                     game.quad[neighbor.i][neighbor.j] = '.'
2785                 elif iquad in ('E', 'F'): # Buffet ship
2786                     prout(_("***Starship buffeted by nova."))
2787                     if game.shldup:
2788                         if game.shield >= 2000.0:
2789                             game.shield -= 2000.0
2790                         else:
2791                             diff = 2000.0 - game.shield
2792                             game.energy -= diff
2793                             game.shield = 0.0
2794                             game.shldup = False
2795                             prout(_("***Shields knocked out."))
2796                             game.damage[DSHIELD] += 0.005*game.damfac*rnd.real()*diff
2797                     else:
2798                         game.energy -= 2000.0
2799                     if game.energy <= 0:
2800                         finish(FNOVA)
2801                         return
2802                     # add in course nova contributes to kicking starship
2803                     if hits:
2804                         bump += (game.sector-hits[-1]).sgn()
2805                 elif iquad == 'K': # kill klingon
2806                     deadkl(neighbor, iquad, neighbor)
2807                 elif iquad in ('C','S','R'): # Damage/destroy big enemies
2808                     target = None
2809                     for ll in range(len(game.enemies)):
2810                         if game.enemies[ll].location == neighbor:
2811                             target = game.enemies[ll]
2812                             break
2813                     if target is not None:
2814                         target.power -= 800.0 # If firepower is lost, die
2815                         if target.power <= 0.0:
2816                             deadkl(neighbor, iquad, neighbor)
2817                             continue    # neighbor loop
2818                         # Else enemy gets flung by the blast wave
2819                         newc = neighbor + neighbor - start
2820                         proutn(crmena(True, iquad, "sector", neighbor) + _(" damaged"))
2821                         if not newc.valid_sector():
2822                             # can't leave quadrant
2823                             skip(1)
2824                             continue
2825                         iquad1 = game.quad[newc.i][newc.j]
2826                         if iquad1 == ' ':
2827                             proutn(_(", blasted into ") + crmena(False, ' ', "sector", newc))
2828                             skip(1)
2829                             deadkl(neighbor, iquad, newc)
2830                             continue
2831                         if iquad1 != '.':
2832                             # can't move into something else
2833                             skip(1)
2834                             continue
2835                         proutn(_(", buffeted to Sector %s") % newc)
2836                         game.quad[neighbor.i][neighbor.j] = '.'
2837                         game.quad[newc.i][newc.j] = iquad
2838                         target.move(newc)
2839     # Starship affected by nova -- kick it away.
2840     dist = kount*0.1
2841     direc = ncourse[3*(bump.i+1)+bump.j+2]
2842     if direc == 0.0:
2843         dist = 0.0
2844     if dist == 0.0:
2845         return
2846     scourse = course(bearing=direc, distance=dist)
2847     game.optime = scourse.time(w=4)
2848     skip(1)
2849     prout(_("Force of nova displaces starship."))
2850     imove(scourse, noattack=True)
2851     game.optime = scourse.time(w=4)
2852     return
2853
2854 def supernova(w):
2855     "Star goes supernova."
2856     num = 0; npdead = 0
2857     if w is not None:
2858         nq = copy.copy(w)
2859     else:
2860         # Scheduled supernova -- select star at random.
2861         nstars = 0
2862         nq = Coord()
2863         for nq.i in range(GALSIZE):
2864             for nq.j in range(GALSIZE):
2865                 nstars += game.state.galaxy[nq.i][nq.j].stars
2866         if nstars == 0:
2867             return # nothing to supernova exists
2868         num = rnd.integer(nstars) + 1
2869         for nq.i in range(GALSIZE):
2870             for nq.j in range(GALSIZE):
2871                 num -= game.state.galaxy[nq.i][nq.j].stars
2872                 if num <= 0:
2873                     break
2874             if num <=0:
2875                 break
2876         if game.idebug:
2877             proutn("=== Super nova here?")
2878             if ja():
2879                 nq = game.quadrant
2880     if nq != game.quadrant or game.justin:
2881         # it isn't here, or we just entered (treat as enroute)
2882         if communicating():
2883             skip(1)
2884             prout(_("Message from Starfleet Command       Stardate %.2f") % game.state.date)
2885             prout(_("     Supernova in Quadrant %s; caution advised.") % nq)
2886     else:
2887         ns = Coord()
2888         # we are in the quadrant!
2889         num = rnd.integer(game.state.galaxy[nq.i][nq.j].stars) + 1
2890         for ns.i in range(QUADSIZE):
2891             for ns.j in range(QUADSIZE):
2892                 if game.quad[ns.i][ns.j]=='*':
2893                     num -= 1
2894                     if num==0:
2895                         break
2896             if num==0:
2897                 break
2898         skip(1)
2899         prouts(_("***RED ALERT!  RED ALERT!"))
2900         skip(1)
2901         prout(_("***Incipient supernova detected at Sector %s") % ns)
2902         if (ns.i-game.sector.i)**2 + (ns.j-game.sector.j)**2 <= 2.1:
2903             proutn(_("Emergency override attempts t"))
2904             prouts("***************")
2905             skip(1)
2906             stars()
2907             game.alldone = True
2908     # destroy any Klingons in supernovaed quadrant
2909     game.state.galaxy[nq.i][nq.j].klingons = 0
2910     if nq == game.state.kscmdr:
2911         # did in the Supercommander!
2912         game.state.nscrem = game.state.kscmdr.i = game.state.kscmdr.j = game.isatb =  0
2913         game.iscate = False
2914         unschedule(FSCMOVE)
2915         unschedule(FSCDBAS)
2916     # Changing this to [w for w in game.state.kcmdr if w != nq]
2917     # causes regression-test failure
2918     survivors = list(filter(lambda w: w != nq, game.state.kcmdr))
2919     #comkills = len(game.state.kcmdr) - len(survivors)
2920     game.state.kcmdr = survivors
2921     if not game.state.kcmdr:
2922         unschedule(FTBEAM)
2923     # destroy Romulans and planets in supernovaed quadrant
2924     nrmdead = game.state.galaxy[nq.i][nq.j].romulans
2925     game.state.galaxy[nq.i][nq.j].romulans = 0
2926     game.state.nromrem -= nrmdead
2927     # Destroy planets
2928     for loop in range(game.inplan):
2929         if game.state.planets[loop].quadrant == nq:
2930             game.state.planets[loop].pclass = "destroyed"
2931             npdead += 1
2932     # Destroy any base in supernovaed quadrant
2933     game.state.baseq = [x for x in game.state.baseq if x != nq]
2934     # If starship caused supernova, tally up destruction
2935     if w is not None:
2936         game.state.starkl += game.state.galaxy[nq.i][nq.j].stars
2937         game.state.basekl += game.state.galaxy[nq.i][nq.j].starbase
2938         game.state.nplankl += npdead
2939     # mark supernova in galaxy and in star chart
2940     if game.quadrant == nq or communicating():
2941         game.state.galaxy[nq.i][nq.j].supernova = True
2942     # If supernova destroys last Klingons give special message
2943     if game.unwon()==0 and nq != game.quadrant:
2944         skip(2)
2945         if w is None:
2946             prout(_("Lucky you!"))
2947         proutn(_("A supernova in %s has just destroyed the last Klingons.") % nq)
2948         finish(FWON)
2949         return
2950     # if some Klingons remain, continue or die in supernova
2951     if game.alldone:
2952         finish(FSNOVAED)
2953     return
2954
2955 # Code from finish.c ends here.
2956
2957 def selfdestruct():
2958     "Self-destruct maneuver. Finish with a BANG!"
2959     scanner.chew()
2960     if damaged(DCOMPTR):
2961         prout(_("Computer damaged; cannot execute destruct sequence."))
2962         return
2963     prouts(_("---WORKING---")); skip(1)
2964     prouts(_("SELF-DESTRUCT-SEQUENCE-ACTIVATED")); skip(1)
2965     prouts("   10"); skip(1)
2966     prouts("       9"); skip(1)
2967     prouts("          8"); skip(1)
2968     prouts("             7"); skip(1)
2969     prouts("                6"); skip(1)
2970     skip(1)
2971     prout(_("ENTER-CORRECT-PASSWORD-TO-CONTINUE-"))
2972     skip(1)
2973     prout(_("SELF-DESTRUCT-SEQUENCE-OTHERWISE-"))
2974     skip(1)
2975     prout(_("SELF-DESTRUCT-SEQUENCE-WILL-BE-ABORTED"))
2976     skip(1)
2977     scanner.nexttok()
2978     if game.passwd != scanner.token:
2979         prouts(_("PASSWORD-REJECTED;"))
2980         skip(1)
2981         prouts(_("CONTINUITY-EFFECTED"))
2982         skip(2)
2983         return
2984     prouts(_("PASSWORD-ACCEPTED")); skip(1)
2985     prouts("                   5"); skip(1)
2986     prouts("                      4"); skip(1)
2987     prouts("                         3"); skip(1)
2988     prouts("                            2"); skip(1)
2989     prouts("                              1"); skip(1)
2990     if rnd.withprob(0.15):
2991         prouts(_("GOODBYE-CRUEL-WORLD"))
2992         skip(1)
2993     kaboom()
2994
2995 def kaboom():
2996     stars()
2997     if game.ship=='E':
2998         prouts("***")
2999     prouts(_("********* Entropy of %s maximized *********") % crmshp())
3000     skip(1)
3001     stars()
3002     skip(1)
3003     if len(game.enemies) != 0:
3004         whammo = 25.0 * game.energy
3005         for e in game.enemies[::-1]:
3006             if e.power*e.kdist <= whammo:
3007                 deadkl(e.location, game.quad[e.location.i][e.location.j], e.location)
3008     finish(FDILITHIUM)
3009
3010 def killrate():
3011     "Compute our rate of kils over time."
3012     elapsed = game.state.date - game.indate
3013     if elapsed == 0:        # Avoid divide-by-zero error if calculated on turn 0
3014         return 0
3015     else:
3016         starting = (game.inkling + game.incom + game.inscom)
3017         remaining = game.unwon()
3018         return (starting - remaining)/elapsed
3019
3020 def badpoints():
3021     "Compute demerits."
3022     badpt = 5.0*game.state.starkl + \
3023             game.casual + \
3024             10.0*game.state.nplankl + \
3025             300*game.state.nworldkl + \
3026             45.0*game.nhelp +\
3027             100.0*game.state.basekl +\
3028             3.0*game.abandoned +\
3029             100*game.ncviol
3030     if game.ship == 'F':
3031         badpt += 100.0
3032     elif game.ship is None:
3033         badpt += 200.0
3034     return badpt
3035
3036 def finish(ifin):
3037     # end the game, with appropriate notifications
3038     igotit = False
3039     game.alldone = True
3040     skip(3)
3041     prout(_("It is stardate %.1f.") % game.state.date)
3042     skip(1)
3043     if ifin == FWON: # Game has been won
3044         if game.state.nromrem != 0:
3045             prout(_("The remaining %d Romulans surrender to Starfleet Command.") %
3046                   game.state.nromrem)
3047
3048         prout(_("You have smashed the Klingon invasion fleet and saved"))
3049         prout(_("the Federation."))
3050         if game.alive and game.brigcapacity-game.brigfree > 0:
3051             game.kcaptured += game.brigcapacity-game.brigfree
3052             prout(_("The %d captured Klingons are transferred to Star Fleet Command.") % (game.brigcapacity-game.brigfree))
3053         game.gamewon = True
3054         if game.alive:
3055             badpt = badpoints()
3056             if badpt < 100.0:
3057                 badpt = 0.0        # Close enough!
3058             # killsPerDate >= RateMax
3059             if game.state.date-game.indate < 5.0 or \
3060                 killrate() >= 0.1*game.skill*(game.skill+1.0) + 0.1 + 0.008*badpt:
3061                 skip(1)
3062                 prout(_("In fact, you have done so well that Starfleet Command"))
3063                 if game.skill == SKILL_NOVICE:
3064                     prout(_("promotes you one step in rank from \"Novice\" to \"Fair\"."))
3065                 elif game.skill == SKILL_FAIR:
3066                     prout(_("promotes you one step in rank from \"Fair\" to \"Good\"."))
3067                 elif game.skill == SKILL_GOOD:
3068                     prout(_("promotes you one step in rank from \"Good\" to \"Expert\"."))
3069                 elif game.skill == SKILL_EXPERT:
3070                     prout(_("promotes you to Commodore Emeritus."))
3071                     skip(1)
3072                     prout(_("Now that you think you're really good, try playing"))
3073                     prout(_("the \"Emeritus\" game. It will splatter your ego."))
3074                 elif game.skill == SKILL_EMERITUS:
3075                     skip(1)
3076                     proutn(_("Computer-  "))
3077                     prouts(_("ERROR-ERROR-ERROR-ERROR"))
3078                     skip(2)
3079                     prouts(_("  YOUR-SKILL-HAS-EXCEEDED-THE-CAPACITY-OF-THIS-PROGRAM"))
3080                     skip(1)
3081                     prouts(_("  THIS-PROGRAM-MUST-SURVIVE"))
3082                     skip(1)
3083                     prouts(_("  THIS-PROGRAM-MUST-SURVIVE"))
3084                     skip(1)
3085                     prouts(_("  THIS-PROGRAM-MUST-SURVIVE"))
3086                     skip(1)
3087                     prouts(_("  THIS-PROGRAM-MUST?- MUST ? - SUR? ? -?  VI"))
3088                     skip(2)
3089                     prout(_("Now you can retire and write your own Star Trek game!"))
3090                     skip(1)
3091                 elif game.skill >= SKILL_EXPERT:
3092                     if game.thawed and not game.idebug:
3093                         prout(_("You cannot get a citation, so..."))
3094                     else:
3095                         proutn(_("Do you want your Commodore Emeritus Citation printed? "))
3096                         scanner.chew()
3097                         if ja():
3098                             igotit = True
3099             # Only grant long life if alive (original didn't!)
3100             skip(1)
3101             prout(_("LIVE LONG AND PROSPER."))
3102         score()
3103         if igotit:
3104             plaque()
3105         return
3106     elif ifin == FDEPLETE: # Federation Resources Depleted
3107         prout(_("Your time has run out and the Federation has been"))
3108         prout(_("conquered.  Your starship is now Klingon property,"))
3109         prout(_("and you are put on trial as a war criminal.  On the"))
3110         proutn(_("basis of your record, you are "))
3111         if game.unwon()*3.0 > (game.inkling + game.incom + game.inscom):
3112             prout(_("acquitted."))
3113             skip(1)
3114             prout(_("LIVE LONG AND PROSPER."))
3115         else:
3116             prout(_("found guilty and"))
3117             prout(_("sentenced to death by slow torture."))
3118             game.alive = False
3119         score()
3120         return
3121     elif ifin == FLIFESUP:
3122         prout(_("Your life support reserves have run out, and"))
3123         prout(_("you die of thirst, starvation, and asphyxiation."))
3124         prout(_("Your starship is a derelict in space."))
3125     elif ifin == FNRG:
3126         prout(_("Your energy supply is exhausted."))
3127         skip(1)
3128         prout(_("Your starship is a derelict in space."))
3129     elif ifin == FBATTLE:
3130         prout(_("The %s has been destroyed in battle.") % crmshp())
3131         skip(1)
3132         prout(_("Dulce et decorum est pro patria mori."))
3133     elif ifin == FNEG3:
3134         prout(_("You have made three attempts to cross the negative energy"))
3135         prout(_("barrier which surrounds the galaxy."))
3136         skip(1)
3137         prout(_("Your navigation is abominable."))
3138         score()
3139     elif ifin == FNOVA:
3140         prout(_("Your starship has been destroyed by a nova."))
3141         prout(_("That was a great shot."))
3142         skip(1)
3143     elif ifin == FSNOVAED:
3144         prout(_("The %s has been fried by a supernova.") % crmshp())
3145         prout(_("...Not even cinders remain..."))
3146     elif ifin == FABANDN:
3147         prout(_("You have been captured by the Klingons. If you still"))
3148         prout(_("had a starbase to be returned to, you would have been"))
3149         prout(_("repatriated and given another chance. Since you have"))
3150         prout(_("no starbases, you will be mercilessly tortured to death."))
3151     elif ifin == FDILITHIUM:
3152         prout(_("Your starship is now an expanding cloud of subatomic particles"))
3153     elif ifin == FMATERIALIZE:
3154         prout(_("Starbase was unable to re-materialize your starship."))
3155         prout(_("Sic transit gloria mundi"))
3156     elif ifin == FPHASER:
3157         prout(_("The %s has been cremated by its own phasers.") % crmshp())
3158     elif ifin == FLOST:
3159         prout(_("You and your landing party have been"))
3160         prout(_("converted to energy, dissipating through space."))
3161     elif ifin == FMINING:
3162         prout(_("You are left with your landing party on"))
3163         prout(_("a wild jungle planet inhabited by primitive cannibals."))
3164         skip(1)
3165         prout(_("They are very fond of \"Captain Kirk\" soup."))
3166         skip(1)
3167         prout(_("Without your leadership, the %s is destroyed.") % crmshp())
3168     elif ifin == FDPLANET:
3169         prout(_("You and your mining party perish."))
3170         skip(1)
3171         prout(_("That was a great shot."))
3172         skip(1)
3173     elif ifin == FSSC:
3174         prout(_("The Galileo is instantly annihilated by the supernova."))
3175         prout(_("You and your mining party are atomized."))
3176         skip(1)
3177         prout(_("Mr. Spock takes command of the %s and") % crmshp())
3178         prout(_("joins the Romulans, wreaking terror on the Federation."))
3179     elif ifin == FPNOVA:
3180         prout(_("You and your mining party are atomized."))
3181         skip(1)
3182         prout(_("Mr. Spock takes command of the %s and") % crmshp())
3183         prout(_("joins the Romulans, wreaking terror on the Federation."))
3184     elif ifin == FSTRACTOR:
3185         prout(_("The shuttle craft Galileo is also caught,"))
3186         prout(_("and breaks up under the strain."))
3187         skip(1)
3188         prout(_("Your debris is scattered for millions of miles."))
3189         prout(_("Without your leadership, the %s is destroyed.") % crmshp())
3190     elif ifin == FDRAY:
3191         prout(_("The mutants attack and kill Spock."))
3192         prout(_("Your ship is captured by Klingons, and"))
3193         prout(_("your crew is put on display in a Klingon zoo."))
3194     elif ifin == FTRIBBLE:
3195         prout(_("Tribbles consume all remaining water,"))
3196         prout(_("food, and oxygen on your ship."))
3197         skip(1)
3198         prout(_("You die of thirst, starvation, and asphyxiation."))
3199         prout(_("Your starship is a derelict in space."))
3200     elif ifin == FHOLE:
3201         prout(_("Your ship is drawn to the center of the black hole."))
3202         prout(_("You are crushed into extremely dense matter."))
3203     elif ifin == FCLOAK:
3204         game.ncviol += 1
3205         prout(_("You have violated the Treaty of Algeron."))
3206         prout(_("The Romulan Empire can never trust you again."))
3207     elif ifin == FCREW:
3208         prout(_("Your last crew member has died."))
3209     if ifin != FWON and ifin != FCLOAK and game.iscloaked:
3210         prout(_("Your ship was cloaked so your subspace radio did not receive anything."))
3211         prout(_("You may have missed some warning messages."))
3212         skip(1)
3213     if game.ship == 'F':
3214         game.ship = None
3215     elif game.ship == 'E':
3216         game.ship = 'F'
3217     game.alive = False
3218     if game.unwon() != 0:
3219         goodies = game.state.remres/game.inresor
3220         baddies = (game.remkl() + 2.0*len(game.state.kcmdr))/(game.inkling+2.0*game.incom)
3221         if goodies/baddies >= rnd.real(1.0, 1.5):
3222             prout(_("As a result of your actions, a treaty with the Klingon"))
3223             prout(_("Empire has been signed. The terms of the treaty are"))
3224             if goodies/baddies >= rnd.real(3.0):
3225                 prout(_("favorable to the Federation."))
3226                 skip(1)
3227                 prout(_("Congratulations!"))
3228             else:
3229                 prout(_("highly unfavorable to the Federation."))
3230         else:
3231             prout(_("The Federation will be destroyed."))
3232     else:
3233         prout(_("Since you took the last Klingon with you, you are a"))
3234         prout(_("martyr and a hero. Someday maybe they'll erect a"))
3235         prout(_("statue in your memory. Rest in peace, and try not"))
3236         prout(_("to think about pigeons."))
3237         game.gamewon = True
3238     score()
3239     scanner.chew()      # Clean up leftovers
3240
3241 def score():
3242     "Compute player's score."
3243     timused = game.state.date - game.indate
3244     if (timused == 0 or game.unwon() != 0) and timused < 5.0:
3245         timused = 5.0
3246     game.perdate = killrate()
3247     ithperd = 500*game.perdate + 0.5
3248     iwon = 0
3249     if game.gamewon:
3250         iwon = 100*game.skill
3251     if game.ship == 'E':
3252         klship = 0
3253     elif game.ship == 'F':
3254         klship = 1
3255     else:
3256         klship = 2
3257     dead_ordinaries= game.inkling - game.remkl() + len(game.state.kcmdr) + game.state.nscrem
3258     game.score = 10*(dead_ordinaries)\
3259              + 50*(game.incom - len(game.state.kcmdr)) \
3260              + ithperd + iwon \
3261              + 20*(game.inrom - game.state.nromrem) \
3262              + 200*(game.inscom - game.state.nscrem) \
3263                  - game.state.nromrem \
3264              + 3 * game.kcaptured \
3265              - badpoints()
3266     if not game.alive:
3267         game.score -= 200
3268     skip(2)
3269     prout(_("Your score --"))
3270     if game.inrom - game.state.nromrem:
3271         prout(_("%6d Romulans destroyed                 %5d") %
3272               (game.inrom - game.state.nromrem, 20*(game.inrom - game.state.nromrem)))
3273     if game.state.nromrem and game.gamewon:
3274         prout(_("%6d Romulans captured                  %5d") %
3275               (game.state.nromrem, game.state.nromrem))
3276     if dead_ordinaries:
3277         prout(_("%6d ordinary Klingons destroyed        %5d") %
3278               (dead_ordinaries, 10*dead_ordinaries))
3279     if game.incom - len(game.state.kcmdr):
3280         prout(_("%6d Klingon commanders destroyed       %5d") %
3281               (game.incom - len(game.state.kcmdr), 50*(game.incom - len(game.state.kcmdr))))
3282     if game.kcaptured:
3283         prout(_("%d Klingons captured                   %5d") %
3284               (game.kcaptured, 3 * game.kcaptured))
3285     if game.inscom - game.state.nscrem:
3286         prout(_("%6d Super-Commander destroyed          %5d") %
3287               (game.inscom - game.state.nscrem, 200*(game.inscom - game.state.nscrem)))
3288     if ithperd:
3289         prout(_("%6.2f Klingons per stardate              %5d") %
3290               (game.perdate, ithperd))
3291     if game.state.starkl:
3292         prout(_("%6d stars destroyed by your action     %5d") %
3293               (game.state.starkl, -5*game.state.starkl))
3294     if game.state.nplankl:
3295         prout(_("%6d planets destroyed by your action   %5d") %
3296               (game.state.nplankl, -10*game.state.nplankl))
3297     if (game.options & OPTION_WORLDS) and game.state.nworldkl:
3298         prout(_("%6d inhabited planets destroyed by your action   %5d") %
3299               (game.state.nworldkl, -300*game.state.nworldkl))
3300     if game.state.basekl:
3301         prout(_("%6d bases destroyed by your action     %5d") %
3302               (game.state.basekl, -100*game.state.basekl))
3303     if game.nhelp:
3304         prout(_("%6d calls for help from starbase       %5d") %
3305               (game.nhelp, -45*game.nhelp))
3306     if game.casual:
3307         prout(_("%6d casualties incurred                %5d") %
3308               (game.casual, -game.casual))
3309     if game.abandoned:
3310         prout(_("%6d crew abandoned in space            %5d") %
3311               (game.abandoned, -3*game.abandoned))
3312     if klship:
3313         prout(_("%6d ship(s) lost or destroyed          %5d") %
3314               (klship, -100*klship))
3315     if game.ncviol > 0:
3316         if game.ncviol == 1:
3317             prout(_("1 Treaty of Algeron violation          -100"))
3318         else:
3319             prout(_("%6d Treaty of Algeron violations       %5d\n") %
3320                   (game.ncviol, -100*game.ncviol))
3321     if not game.alive:
3322         prout(_("Penalty for getting yourself killed        -200"))
3323     if game.gamewon:
3324         proutn(_("Bonus for winning "))
3325         if game.skill   == SKILL_NOVICE:        proutn(_("Novice game  "))
3326         elif game.skill == SKILL_FAIR:          proutn(_("Fair game    "))
3327         elif game.skill ==  SKILL_GOOD:         proutn(_("Good game    "))
3328         elif game.skill ==  SKILL_EXPERT:        proutn(_("Expert game  "))
3329         elif game.skill ==  SKILL_EMERITUS:        proutn(_("Emeritus game"))
3330         prout("           %5d" % iwon)
3331     skip(1)
3332     prout(_("TOTAL SCORE                               %5d") % game.score)
3333
3334 def plaque():
3335     "Emit winner's commemmorative plaque."
3336     skip(2)
3337     while True:
3338         proutn(_("File or device name for your plaque: "))
3339         winner = cgetline()
3340         try:
3341             fp = open(winner, "w")
3342             break
3343         except IOError:
3344             prout(_("Invalid name."))
3345
3346     proutn(_("Enter name to go on plaque (up to 30 characters): "))
3347     winner = cgetline()
3348     # The 38 below must be 64 for 132-column paper
3349     nskip = 38 - len(winner)/2
3350     # This is where the ASCII art picture was emitted.
3351     # It got garbled somewhere in the chain of transmission to the Almy version.
3352     # We should restore it if we can find old enough FORTRAN sources.
3353     fp.write("\n\n\n")
3354     fp.write(_("                                                       U. S. S. ENTERPRISE\n"))
3355     fp.write("\n\n\n\n")
3356     fp.write(_("                                  For demonstrating outstanding ability as a starship captain\n"))
3357     fp.write("\n")
3358     fp.write(_("                                                Starfleet Command bestows to you\n"))
3359     fp.write("\n")
3360     fp.write("%*s%s\n\n" % (nskip, "", winner))
3361     fp.write(_("                                                           the rank of\n\n"))
3362     fp.write(_("                                                       \"Commodore Emeritus\"\n\n"))
3363     fp.write("                                                          ")
3364     if game.skill ==  SKILL_EXPERT:
3365         fp.write(_(" Expert level\n\n"))
3366     elif game.skill == SKILL_EMERITUS:
3367         fp.write(_("Emeritus level\n\n"))
3368     else:
3369         fp.write(_(" Cheat level\n\n"))
3370     timestring = time.ctime()
3371     fp.write(_("                                                 This day of %.6s %.4s, %.8s\n\n") %
3372              (timestring+4, timestring+20, timestring+11))
3373     fp.write(_("                                                        Your score:  %d\n\n") % game.score)
3374     fp.write(_("                                                    Klingons per stardate:  %.2f\n") % game.perdate)
3375     fp.close()
3376
3377 # Code from io.c begins here
3378
3379 rows = linecount = 0        # for paging
3380 stdscr = None
3381 replayfp = None
3382 fullscreen_window = None
3383 srscan_window     = None   # Short range scan
3384 report_window     = None   # Report legends for status window
3385 status_window     = None   # The status window itself
3386 lrscan_window     = None   # Long range scan
3387 message_window    = None   # Main window for scrolling text
3388 prompt_window     = None   # Prompt window at bottom of display
3389 curwnd = None
3390
3391 def iostart():
3392     global stdscr, rows
3393     # for some recent versions of python2, the following enables UTF8
3394     # for the older ones we probably need to set C locale, and python3
3395     # has no problems at all
3396     if sys.version_info[0] < 3:
3397         locale.setlocale(locale.LC_ALL, "")
3398     gettext.bindtextdomain("sst", "/usr/local/share/locale")
3399     gettext.textdomain("sst")
3400     if not (game.options & OPTION_CURSES):
3401         ln_env = os.getenv("LINES")
3402         if ln_env:
3403             rows = ln_env
3404         else:
3405             rows = 25
3406     else:
3407         stdscr = curses.initscr()
3408         stdscr.keypad(True)
3409         curses.nonl()
3410         curses.cbreak()
3411         if game.options & OPTION_COLOR:
3412             curses.start_color()
3413             curses.use_default_colors()
3414             curses.init_pair(curses.COLOR_BLACK,   curses.COLOR_BLACK, -1)
3415             curses.init_pair(curses.COLOR_GREEN,   curses.COLOR_GREEN, -1)
3416             curses.init_pair(curses.COLOR_RED,     curses.COLOR_RED, -1)
3417             curses.init_pair(curses.COLOR_CYAN,    curses.COLOR_CYAN, -1)
3418             curses.init_pair(curses.COLOR_WHITE,   curses.COLOR_WHITE, -1)
3419             curses.init_pair(curses.COLOR_MAGENTA, curses.COLOR_MAGENTA, -1)
3420             curses.init_pair(curses.COLOR_BLUE,    curses.COLOR_BLUE, -1)
3421             curses.init_pair(curses.COLOR_YELLOW,  curses.COLOR_YELLOW, -1)
3422         global fullscreen_window, srscan_window, report_window, status_window
3423         global lrscan_window, message_window, prompt_window
3424         (rows, _columns)   = stdscr.getmaxyx()
3425         fullscreen_window = stdscr
3426         srscan_window     = curses.newwin(12, 25, 0,       0)
3427         report_window     = curses.newwin(11, 0,  1,       25)
3428         status_window     = curses.newwin(10, 0,  1,       39)
3429         lrscan_window     = curses.newwin(5,  0,  0,       64)
3430         message_window    = curses.newwin(0,  0,  12,      0)
3431         prompt_window     = curses.newwin(1,  0,  rows-2,  0)
3432         message_window.scrollok(True)
3433         setwnd(fullscreen_window)
3434
3435 def ioend():
3436     "Wrap up I/O."
3437     if game.options & OPTION_CURSES:
3438         stdscr.keypad(False)
3439         curses.echo()
3440         curses.nocbreak()
3441         curses.endwin()
3442
3443 def waitfor():
3444     "Wait for user action -- OK to do nothing if on a TTY"
3445     if game.options & OPTION_CURSES:
3446         stdscr.getch()
3447
3448 def announce():
3449     skip(1)
3450     prouts(_("[ANNOUNCEMENT ARRIVING...]"))
3451     skip(1)
3452
3453 def pause_game():
3454     if game.skill > SKILL_FAIR:
3455         prompt = _("[CONTINUE?]")
3456     else:
3457         prompt = _("[PRESS ENTER TO CONTINUE]")
3458
3459     if game.options & OPTION_CURSES:
3460         drawmaps(0)
3461         setwnd(prompt_window)
3462         prompt_window.clear()
3463         prompt_window.addstr(prompt)
3464         prompt_window.getstr()
3465         prompt_window.clear()
3466         prompt_window.refresh()
3467         setwnd(message_window)
3468     else:
3469         global linecount
3470         sys.stdout.write('\n')
3471         proutn(prompt)
3472         if not replayfp:
3473             my_input()
3474         sys.stdout.write('\n' * rows)
3475         linecount = 0
3476
3477 def skip(i):
3478     "Skip i lines.  Pause game if this would cause a scrolling event."
3479     for _dummy in range(i):
3480         if game.options & OPTION_CURSES:
3481             (y, _x) = curwnd.getyx()
3482             try:
3483                 curwnd.move(y+1, 0)
3484             except curses.error:
3485                 pass
3486         else:
3487             global linecount
3488             linecount += 1
3489             if rows and linecount >= rows:
3490                 pause_game()
3491             else:
3492                 sys.stdout.write('\n')
3493
3494 def proutn(proutntline):
3495     "Utter a line with no following line feed."
3496     if game.options & OPTION_CURSES:
3497         (y, x) = curwnd.getyx()
3498         (my, _mx) = curwnd.getmaxyx()
3499         if curwnd == message_window and y >= my - 2:
3500             pause_game()
3501             clrscr()
3502         if logfp and game.cdebug:
3503             logfp.write("#curses: at %s proutn(%s)\n" % ((y, x), repr(proutntline)))
3504         curwnd.addstr(proutntline)
3505         curwnd.refresh()
3506     else:
3507         sys.stdout.write(proutntline)
3508         sys.stdout.flush()
3509
3510 def prout(proutline):
3511     proutn(proutline)
3512     skip(1)
3513
3514 def prouts(proutsline):
3515     "Emit slowly!"
3516     for c in proutsline:
3517         if not replayfp or replayfp.closed:        # Don't slow down replays
3518             time.sleep(0.03)
3519         proutn(c)
3520         if game.options & OPTION_CURSES:
3521             curwnd.refresh()
3522         else:
3523             sys.stdout.flush()
3524     if not replayfp or replayfp.closed:
3525         time.sleep(0.03)
3526
3527 def cgetline():
3528     "Get a line of input."
3529     if game.options & OPTION_CURSES:
3530         linein = codecs.decode(curwnd.getstr()) + "\n"
3531         curwnd.refresh()
3532     else:
3533         if replayfp and not replayfp.closed:
3534             while True:
3535                 linein = replayfp.readline()
3536                 proutn(linein)
3537                 if linein == '':
3538                     prout("*** Replay finished")
3539                     replayfp.close()
3540                     break
3541                 elif linein[0] != "#":
3542                     break
3543         else:
3544             try:
3545                 linein = my_input() + "\n"
3546             except EOFError:
3547                 prout("")
3548                 sys.exit(0)
3549     if logfp:
3550         logfp.write(linein)
3551     return linein
3552
3553 def setwnd(wnd):
3554     "Change windows -- OK for this to be a no-op in tty mode."
3555     global curwnd
3556     if game.options & OPTION_CURSES:
3557         if game.cdebug and logfp:
3558             if wnd == fullscreen_window:
3559                 legend = "fullscreen"
3560             elif wnd == srscan_window:
3561                 legend = "srscan"
3562             elif wnd == report_window:
3563                 legend = "report"
3564             elif wnd == status_window:
3565                 legend = "status"
3566             elif wnd == lrscan_window:
3567                 legend = "lrscan"
3568             elif wnd == message_window:
3569                 legend = "message"
3570             elif wnd == prompt_window:
3571                 legend = "prompt"
3572             else:
3573                 legend = "unknown"
3574             logfp.write("#curses: setwnd(%s)\n" % legend)
3575         curwnd = wnd
3576         # Some curses implementations get confused when you try this.
3577         try:
3578             curses.curs_set(wnd in (fullscreen_window, message_window, prompt_window))
3579         except curses.error:
3580             pass
3581
3582 def clreol():
3583     "Clear to end of line -- can be a no-op in tty mode"
3584     if game.options & OPTION_CURSES:
3585         curwnd.clrtoeol()
3586         curwnd.refresh()
3587
3588 def clrscr():
3589     "Clear screen -- can be a no-op in tty mode."
3590     global linecount
3591     if game.options & OPTION_CURSES:
3592         curwnd.clear()
3593         curwnd.move(0, 0)
3594         curwnd.refresh()
3595     linecount = 0
3596
3597 def textcolor(color=DEFAULT):
3598     if (game.options & OPTION_COLOR) and (game.options & OPTION_CURSES):
3599         if color == DEFAULT:
3600             curwnd.attrset(0)
3601         elif color ==  BLACK:
3602             curwnd.attron(curses.color_pair(curses.COLOR_BLACK))
3603         elif color ==  BLUE:
3604             curwnd.attron(curses.color_pair(curses.COLOR_BLUE))
3605         elif color ==  GREEN:
3606             curwnd.attron(curses.color_pair(curses.COLOR_GREEN))
3607         elif color ==  CYAN:
3608             curwnd.attron(curses.color_pair(curses.COLOR_CYAN))
3609         elif color ==  RED:
3610             curwnd.attron(curses.color_pair(curses.COLOR_RED))
3611         elif color ==  MAGENTA:
3612             curwnd.attron(curses.color_pair(curses.COLOR_MAGENTA))
3613         elif color ==  BROWN:
3614             curwnd.attron(curses.color_pair(curses.COLOR_YELLOW))
3615         elif color ==  LIGHTGRAY:
3616             curwnd.attron(curses.color_pair(curses.COLOR_WHITE))
3617         elif color ==  DARKGRAY:
3618             curwnd.attron(curses.color_pair(curses.COLOR_BLACK) | curses.A_BOLD)
3619         elif color ==  LIGHTBLUE:
3620             curwnd.attron(curses.color_pair(curses.COLOR_BLUE) | curses.A_BOLD)
3621         elif color ==  LIGHTGREEN:
3622             curwnd.attron(curses.color_pair(curses.COLOR_GREEN) | curses.A_BOLD)
3623         elif color ==  LIGHTCYAN:
3624             curwnd.attron(curses.color_pair(curses.COLOR_CYAN) | curses.A_BOLD)
3625         elif color ==  LIGHTRED:
3626             curwnd.attron(curses.color_pair(curses.COLOR_RED) | curses.A_BOLD)
3627         elif color ==  LIGHTMAGENTA:
3628             curwnd.attron(curses.color_pair(curses.COLOR_MAGENTA) | curses.A_BOLD)
3629         elif color ==  YELLOW:
3630             curwnd.attron(curses.color_pair(curses.COLOR_YELLOW) | curses.A_BOLD)
3631         elif color ==  WHITE:
3632             curwnd.attron(curses.color_pair(curses.COLOR_WHITE) | curses.A_BOLD)
3633
3634 def highvideo():
3635     if (game.options & OPTION_COLOR) and (game.options & OPTION_CURSES):
3636         curwnd.attron(curses.A_REVERSE)
3637
3638 #
3639 # Things past this point have policy implications.
3640 #
3641
3642 def drawmaps(mode):
3643     "Hook to be called after moving to redraw maps."
3644     if game.options & OPTION_CURSES:
3645         if mode == 1:
3646             sensor()
3647         setwnd(srscan_window)
3648         curwnd.move(0, 0)
3649         srscan()
3650         if mode != 2:
3651             setwnd(status_window)
3652             status_window.clear()
3653             status_window.move(0, 0)
3654             setwnd(report_window)
3655             report_window.clear()
3656             report_window.move(0, 0)
3657             status()
3658             setwnd(lrscan_window)
3659             lrscan_window.clear()
3660             lrscan_window.move(0, 0)
3661             lrscan(silent=False)
3662
3663 def put_srscan_sym(w, sym):
3664     "Emit symbol for short-range scan."
3665     srscan_window.move(w.i+1, w.j*2+2)
3666     srscan_window.addch(sym)
3667     srscan_window.refresh()
3668
3669 def boom(w):
3670     "Enemy fall down, go boom."
3671     if game.options & OPTION_CURSES:
3672         drawmaps(0)
3673         setwnd(srscan_window)
3674         srscan_window.attron(curses.A_REVERSE)
3675         put_srscan_sym(w, game.quad[w.i][w.j])
3676         #sound(500)
3677         #time.sleep(1.0)
3678         #nosound()
3679         srscan_window.attroff(curses.A_REVERSE)
3680         put_srscan_sym(w, game.quad[w.i][w.j])
3681         curses.delay_output(500)
3682         setwnd(message_window)
3683
3684 def warble():
3685     "Sound and visual effects for teleportation."
3686     if game.options & OPTION_CURSES:
3687         drawmaps(2)
3688         setwnd(message_window)
3689         #sound(50)
3690     prouts("     . . . . .     ")
3691     if game.options & OPTION_CURSES:
3692         #curses.delay_output(1000)
3693         #nosound()
3694         pass
3695
3696 def tracktorpedo(w, step, i, n, iquad):
3697     "Torpedo-track animation."
3698     if not game.options & OPTION_CURSES:
3699         if step == 1:
3700             if n != 1:
3701                 skip(1)
3702                 proutn(_("Track for torpedo number %d-  ") % (i+1))
3703             else:
3704                 skip(1)
3705                 proutn(_("Torpedo track- "))
3706         elif step in {4, 9}:
3707             skip(1)
3708         proutn("%s   " % w)
3709     else:
3710         if not damaged(DSRSENS) or game.condition=="docked":
3711             if i != 0 and step == 1:
3712                 drawmaps(2)
3713                 time.sleep(0.4)
3714             if iquad in {'.', ' '}:
3715                 put_srscan_sym(w, '+')
3716                 #sound(step*10)
3717                 #time.sleep(0.1)
3718                 #nosound()
3719                 put_srscan_sym(w, iquad)
3720             else:
3721                 curwnd.attron(curses.A_REVERSE)
3722                 put_srscan_sym(w, iquad)
3723                 #sound(500)
3724                 #time.sleep(1.0)
3725                 #nosound()
3726                 curwnd.attroff(curses.A_REVERSE)
3727                 put_srscan_sym(w, iquad)
3728         else:
3729             proutn("%s   " % w)
3730
3731 def makechart():
3732     "Display the current galaxy chart."
3733     if game.options & OPTION_CURSES:
3734         setwnd(message_window)
3735         message_window.clear()
3736     chart()
3737     if game.options & OPTION_TTY:
3738         skip(1)
3739
3740 NSYM        = 14
3741
3742 def prstat(txt, data):
3743     proutn(txt)
3744     if game.options & OPTION_CURSES:
3745         skip(1)
3746         setwnd(status_window)
3747     else:
3748         proutn(" " * (NSYM - len(txt)))
3749     proutn(data)
3750     skip(1)
3751     if game.options & OPTION_CURSES:
3752         setwnd(report_window)
3753
3754 # Code from moving.c begins here
3755
3756 def imove(icourse=None, noattack=False):
3757     "Movement execution for warp, impulse, supernova, and tractor-beam events."
3758     w = Coord()
3759
3760     def newquadrant(noattack):
3761         # Leaving quadrant -- allow final enemy attack
3762         # Don't set up attack if being pushed by nova or cloaked
3763         if len(game.enemies) != 0 and not noattack and not game.iscloaked:
3764             newcnd()
3765             for enemy in game.enemies:
3766                 finald = (w - enemy.location).distance()
3767                 enemy.kavgd = 0.5 * (finald + enemy.kdist)
3768             # Stas Sergeev added the condition
3769             # that attacks only happen if Klingons
3770             # are present and your skill is good.
3771             if game.skill > SKILL_GOOD and game.klhere > 0 and not game.state.galaxy[game.quadrant.i][game.quadrant.j].supernova:
3772                 attack(torps_ok=False)
3773             if game.alldone:
3774                 return
3775         # check for edge of galaxy
3776         kinks = 0
3777         while True:
3778             kink = False
3779             if icourse.final.i < 0:
3780                 icourse.final.i = -icourse.final.i
3781                 kink = True
3782             if icourse.final.j < 0:
3783                 icourse.final.j = -icourse.final.j
3784                 kink = True
3785             if icourse.final.i >= GALSIZE*QUADSIZE:
3786                 icourse.final.i = (GALSIZE*QUADSIZE*2) - icourse.final.i
3787                 kink = True
3788             if icourse.final.j >= GALSIZE*QUADSIZE:
3789                 icourse.final.j = (GALSIZE*QUADSIZE*2) - icourse.final.j
3790                 kink = True
3791             if kink:
3792                 kinks += 1
3793             else:
3794                 break
3795         if kinks:
3796             game.nkinks += 1
3797             if game.nkinks == 3: