Use /tmp for logs, as /usr/tmp is not available under Ubuntu.
[super-star-trek.git] / src / sst.py
index ebcf75f8a297f9575b8889f47dfc9e0931a2016d..920fbd13b66c5d24165a92a3d415f69bb3a0315e 100644 (file)
@@ -18,20 +18,20 @@ DOC_NAME    = "sst.doc"
 
 def _(str): return gettext.gettext(str)
 
-PHASEFAC       = 2.0
-GALSIZE        = 8
-NINHAB         = (GALSIZE * GALSIZE / 2)
-MAXUNINHAB     = 10
-PLNETMAX       = (NINHAB + MAXUNINHAB)
-QUADSIZE       = 10
-BASEMIN                = 2
-BASEMAX        = (GALSIZE * GALSIZE / 12)
-MAXKLGAME      = 127
-MAXKLQUAD      = 9
-FULLCREW       = 428   # BSD Trek was 387, that's wrong 
-FOREVER        = 1e30
-MAXBURST       = 3
-MINCMDR        = 10
+GALSIZE        = 8             # Galaxy size in quadrants
+NINHAB         = (GALSIZE * GALSIZE / 2)       # Number of inhabited worlds
+MAXUNINHAB     = 10            # Maximum uninhabited worlds
+QUADSIZE       = 10            # Quadrant size in sectors
+BASEMIN                = 2                             # Minimum starbases
+BASEMAX        = (GALSIZE * GALSIZE / 12)      # Maximum starbases
+MAXKLGAME      = 127           # Maximum Klingons per game
+MAXKLQUAD      = 9             # Maximum Klingons per quadrant
+FULLCREW       = 428           # Crew size. BSD Trek was 387, that's wrong 
+FOREVER        = 1e30          # Time for the indefinite future
+MAXBURST       = 3             # Max # of torps you can launch in one turn
+MINCMDR        = 10            # Minimum number of Klingon commanders
+DOCKFAC                = 0.25          # Repair faster when docked
+PHASEFAC       = 2.0           # Unclear what this is, it was in the C version
 
 class TrekError:
     pass
@@ -331,10 +331,8 @@ class gamestate:
         self.energy = 0.0      # energy level
         self.shield = 0.0      # shield level
         self.warpfac = 0.0     # warp speed
-        self.wfacsq = 0.0      # squared warp factor
         self.lsupres = 0.0     # life support reserves
         self.optime = 0.0      # time taken by current operation
-        self.docfac = 0.0      # repair factor when docking (constant?)
         self.damfac = 0.0      # damage factor
         self.lastchart = 0.0   # time star chart was last updated
         self.cryprob = 0.0     # probability that crystal will work
@@ -373,22 +371,17 @@ FHOLE = 20
 FCREW = 21
 
 def withprob(p):
-    v = random.random()
-    #logfp.write("# withprob(%s) -> %f (%s) at %s\n" % (p, v, v<p, traceback.extract_stack()[-2][1:]))
-    return v < p
+    return random.random() < p
 
 def randrange(*args):
-    v = random.randrange(*args)
-    #logfp.write("# randrange%s -> %s at %s\n" % (args, v, traceback.extract_stack()[-2][1:]))
-    return v
+    return random.randrange(*args)
 
 def randreal(*args):
     v = random.random()
     if len(args) == 1:
-        v *= args[0]           # returns from [0, args[0])
+        v *= args[0]           # from [0, args[0])
     elif len(args) == 2:
-        v = args[0] + v*(args[1]-args[0])      # returns from [args[0], args[1])
-    #logfp.write("# randreal%s -> %s at %s\n" % (args, v, traceback.extract_stack()[-2][1:]))
+        v = args[0] + v*(args[1]-args[0])      # from [args[0], args[1])
     return v
 
 # Code from ai.c begins here
@@ -963,34 +956,12 @@ def doshield(shraise):
 
 def randdevice():
     "Choose a device to damage, at random."
-    # Quoth Eric Allman in the code of BSD-Trek:
-    # "Under certain conditions you can get a critical hit.  This
-    # sort of hit damages devices.  The probability that a given
-    # device is damaged depends on the device.  Well protected
-    # devices (such as the computer, which is in the core of the
-    # ship and has considerable redundancy) almost never get
-    # damaged, whereas devices which are exposed (such as the
-    # warp engines) or which are particularly delicate (such as
-    # the transporter) have a much higher probability of being
-    # damaged."
-    # 
-    # This is one place where OPTION_PLAIN does not restore the
-    # original behavior, which was equiprobable damage across
-    # all devices.  If we wanted that, we'd return randrange(NDEVICES)
-    # and have done with it.  Also, in the original game, DNAVYS
-    # and DCOMPTR were the same device. 
-    # 
-    # Instead, we use a table of weights similar to the one from BSD Trek.
-    # BSD doesn't have the shuttle, shield controller, death ray, or probes. 
-    # We don't have a cloaking device.  The shuttle got the allocation
-    # for the cloaking device, then we shaved a half-percent off
-    # everything to have some weight to give DSHCTRL/DDRAY/DDSP.
     weights = (
        105,    # DSRSENS: short range scanners 10.5% 
        105,    # DLRSENS: long range scanners          10.5% 
        120,    # DPHASER: phasers                      12.0% 
        120,    # DPHOTON: photon torpedoes             12.0% 
-       25,     # DLIFSUP: life support          2.5% 
+       25,     # DLIFSUP: life support                  2.5% 
        65,     # DWARPEN: warp drive                    6.5% 
        70,     # DIMPULS: impulse engines               6.5% 
        145,    # DSHIELD: deflector shields            14.5% 
@@ -2106,7 +2077,7 @@ def events():
        # Fix devices 
        repair = xtime
        if game.condition == "docked":
-           repair /= game.docfac
+           repair /= DOCKFAC
        # Don't fix Deathray here 
        for l in range(NDEVICES):
            if game.damage[l] > 0.0 and l != DDRAY:
@@ -4022,7 +3993,7 @@ def timwrp():
        prout(_("Spock has reconstructed a correct star chart from memory"))
     else:
        # Go forward in time 
-       game.optime = -0.5*game.intime*math.log(randreal())
+       game.optime = expran(0.5*game.intime)
        prout(_("You are traveling forward in time %d stardates.") % int(game.optime))
        # cheat to make sure no tractor beams occur during time warp 
        postpone(FTBEAM, game.optime)
@@ -4843,7 +4814,7 @@ def damagereport():
                jdam = True
            prout("  %-26s\t%8.2f\t\t%8.2f" % (device[i],
                                                game.damage[i]+0.05,
-                                               game.docfac*game.damage[i]+0.005))
+                                               DOCKFAC*game.damage[i]+0.005))
     if not jdam:
        prout(_("All devices functional."))
 
@@ -5273,7 +5244,6 @@ def setup():
     game.iscraft = "onship"
     game.landed = False
     game.alive = True
-    game.docfac = 0.25
     # Starchart is functional but we've never seen it
     game.lastchart = FOREVER
     # Put stars in the galaxy
@@ -5439,12 +5409,10 @@ def setup():
 
 def choose():
     "Choose your game type."
-    global thing
     while True:
-       game.tourn = 0
+       game.tourn = game.length = 0
        game.thawed = False
        game.skill = SKILL_NONE
-       game.length = 0
        if not scanner.inqueue: # Can start with command line options 
            proutn(_("Would you like a regular, tournament, or saved game? "))
         scanner.next()
@@ -5812,8 +5780,7 @@ def makemoves():
        drawmaps(1)
         while True:    # get a command 
            hitme = False
-           game.justin = False
-           game.optime = 0.0
+           game.optime = game.justin = False
            scanner.chew()
            setwnd(prompt_window)
            clrscr()
@@ -6233,9 +6200,10 @@ if __name__ == '__main__':
                 raise SystemExit, 1
         # where to save the input in case of bugs
         try:
-            logfp = open("/usr/tmp/sst-input.log", "w")
+            logfp = open("/tmp/sst-input.log", "w")
         except IOError:
             sys.stderr.write("sst: warning, can't open logfile\n")
+            sys.exit(1)
         if logfp:
             logfp.write("# seed %s\n" % seed)
             logfp.write("# options %s\n" % " ".join(arguments))