Add -s option to Python version. Fix more minor bugs.
[super-star-trek.git] / src / sst.py
index be0aef9ad0b8e746dfe851579d89d5f52c562f03..020e91fefd5c16dad704cbb4fb39daeb5369b431 100644 (file)
@@ -201,6 +201,7 @@ MAXKLQUAD   = 9
 FULLCREW       = 428   # BSD Trek was 387, that's wrong 
 FOREVER        = 1e30
 MAXBURST       = 3
+MINCMDR        = 10
 
 # These functions hide the difference between 0-origin and 1-origin addressing.
 def VALID_QUADRANT(x, y):      return ((x)>=0 and (x)<GALSIZE and (y)>=0 and (y)<GALSIZE)
@@ -291,14 +292,14 @@ class planet:
 
 class quadrant:
     def __init__(self):
-        self.stars = None
+        self.stars = 0
         self.planet = None
-       self.starbase = None
-       self.klingons = None
-       self.romulans = None
-       self.supernova = None
-       self.charted = None
-        self.status = None     # Could be "secure", "distressed", "enslaved"
+       self.starbase = False
+       self.klingons = 0
+       self.romulans = 0
+       self.supernova = False
+       self.charted = False
+        self.status = "secure" # Could be "secure", "distressed", "enslaved"
 
 class page:
     def __init__(self):
@@ -439,7 +440,7 @@ class enemy:
             game.enemies.remove(self)
         return motion
     def __repr__(self):
-        return "<%s=%f>" % (self.kloc, self.kpower)    # For debugging
+        return "<%s,%s.%f>" % (self.type, self.kloc, self.kpower)      # For debugging
 
 class gamestate:
     def __init__(self):
@@ -605,15 +606,20 @@ def randreal(*args):
 
 # Code from ai.c begins here
 
+def welcoming(iq):
+    # Would this quadrant welcome another Klingon?
+    return VALID_QUADRANT(iq.x,iq.y) and \
+       not game.state.galaxy[iq.x][iq.y].supernova or \
+       game.state.galaxy[iq.x][iq.y].klingons < MAXKLQUAD
+
+
 def tryexit(enemy, look, irun):
     # a bad guy attempts to bug out 
     iq = coord()
     iq.x = game.quadrant.x+(look.x+(QUADSIZE-1))/QUADSIZE - 1
     iq.y = game.quadrant.y+(look.y+(QUADSIZE-1))/QUADSIZE - 1
-    if not VALID_QUADRANT(iq.x,iq.y) or \
-       game.state.galaxy[iq.x][iq.y].supernova or \
-       game.state.galaxy[iq.x][iq.y].klingons > MAXKLQUAD-1:
-       return False; # no can do -- neg energy, supernovae, or >MAXKLQUAD-1 Klingons 
+    if not welcoming(iq):
+       return False;
     if enemy.type == IHR:
        return False; # Romulans cannot escape! 
     if not irun:
@@ -870,13 +876,9 @@ def moveklings():
 
 def movescom(iq, avoid):
     # commander movement helper 
-    if iq == game.quadrant or not VALID_QUADRANT(iq.x, iq.y) or \
-       game.state.galaxy[iq.x][iq.y].supernova or \
-       game.state.galaxy[iq.x][iq.y].klingons > MAXKLQUAD-1:
-       return 1
     # Avoid quadrants with bases if we want to avoid Enterprise 
-    if avoid and iq in game.state.baseq:
-        return True
+    if not welcoming(iq) or (avoid and iq in game.state.baseq):
+       return True
     if game.justin and not game.iscate:
        return True
     # do the move 
@@ -944,9 +946,7 @@ def supercommander():
        ifindit = iwhichb = 0
        for (i2, base) in enumerate(game.state.baseq):
            i = basetbl[i2][0]; # bug in original had it not finding nearest
-           if base == game.quadrant or base == game.battle or \
-               game.state.galaxy[base.x][base.y].supernova or \
-               game.state.galaxy[base.x][base.y].klingons > MAXKLQUAD-1:
+           if base==game.quadrant or base==game.battle or not welcoming(base):
                continue
            # if there is a commander, and no other base is appropriate,
            # we will take the one with the commander
@@ -3877,14 +3877,14 @@ def imove(novapush):
                    skip(1)
                    crmshp()
                    if iquad == IHWEB:
-                       proutn(_(" encounters Tholian web at %s;") % w)
+                       prout(_(" encounters Tholian web at %s;") % w)
                    else:
-                       proutn(_(" blocked by object at %s;") % w)
+                       prout(_(" blocked by object at %s;") % w)
                    proutn(_("Emergency stop required "))
                    prout(_("%2d units of energy.") % int(stopegy))
                    game.energy -= stopegy
-                   final.x = x-deltax+0.5
-                   final.y = y-deltay+0.5
+                   final.x = int(round(deltax))
+                   final.y = int(round(deltay))
                    game.sector = final
                    if game.energy <= 0:
                        finish(FNRG)
@@ -4739,7 +4739,10 @@ def orbit():
     if damaged(DWARPEN) and damaged(DIMPULS):
        prout(_("Both warp and impulse engines damaged."))
        return
-    if not game.plnet.is_valid() or abs(game.sector.x-game.plnet.x) > 1 or abs(game.sector.y-game.plnet.y) > 1:
+    if not game.plnet.is_valid():
+        prout("There is no planet in this sector.")
+        return
+    if abs(game.sector.x-game.plnet.x)>1 or abs(game.sector.y-game.plnet.y)>1:
        crmshp()
        prout(_(" not adjacent to planet."))
        skip(1)
@@ -5736,16 +5739,6 @@ def setup():
     game.landed = False
     game.alive = True
     game.docfac = 0.25
-    for i in range(GALSIZE):
-       for j in range(GALSIZE):
-           quad = game.state.galaxy[i][j]
-           quad.charted = 0
-           quad.planet = None
-           quad.romulans = 0
-           quad.klingons = 0
-           quad.starbase = False
-           quad.supernova = False
-           quad.status = "secure"
     # Starchart is functional but we've never seen it
     game.lastchart = FOREVER
     # Put stars in the galaxy
@@ -5779,8 +5772,7 @@ def setup():
             if not contflag:
                 break
        game.state.baseq.append(w)
-       game.state.galaxy[w.x][w.y].starbase = True
-       game.state.chart[w.x][w.y].starbase = True
+       game.state.galaxy[w.x][w.y].starbase = game.state.chart[w.x][w.y].starbase = True
     # Position ordinary Klingon Battle Cruisers
     krem = game.inkling
     klumper = 0.25*game.skill*(9.0-game.length)+1.0
@@ -5804,10 +5796,9 @@ def setup():
     for i in range(game.incom):
         while True:
             w = randplace(GALSIZE)
-           if (game.state.galaxy[w.x][w.y].klingons or withprob(0.25)) and \
-                  not game.state.galaxy[w.x][w.y].supernova and \
-                  game.state.galaxy[w.x][w.y].klingons <= MAXKLQUAD-1 and \
-                   not w in game.state.kcmdr[:i]:
+            if not welcoming(w) or w in game.state.kcmdr:
+                continue
+            if (game.state.galaxy[w.x][w.y].klingons or withprob(0.25)):
                 break
        game.state.galaxy[w.x][w.y].klingons += 1
        game.state.kcmdr.append(w)
@@ -5838,11 +5829,11 @@ def setup():
     for i in range(game.state.nromrem):
        w = randplace(GALSIZE)
        game.state.galaxy[w.x][w.y].romulans += 1
-    # Locate the Super Commander
+    # Place the Super-Commander if needed
     if game.state.nscrem > 0:
         while True:
             w = randplace(GALSIZE)
-            if not game.state.galaxy[w.x][w.y].supernova and game.state.galaxy[w.x][w.y].klingons <= MAXKLQUAD:
+            if welcoming(w):
                 break
        game.state.kscmdr = w
        game.state.galaxy[w.x][w.y].klingons += 1
@@ -6008,7 +5999,7 @@ def choose():
     game.state.remtime = 7.0 * game.length
     game.intime = game.state.remtime
     game.state.remkl = game.inkling = 2.0*game.intime*((game.skill+1 - 2*randreal())*game.skill*0.1+.15)
-    game.incom = min(10, int(game.skill + 0.0625*game.inkling*randreal()))
+    game.incom = min(MINCMDR, int(game.skill + 0.0625*game.inkling*randreal()))
     game.state.remres = (game.inkling+4*game.incom)*game.intime
     game.inresor = game.state.remres
     if game.inkling > 50:
@@ -6085,7 +6076,7 @@ def newqad(shutup):
            e.kpower = randreal(1175.0,  1575.0) + 125.0*game.skill
            game.iscate = (game.state.remkl > 1)
     # Put in Romulans if needed
-    for i in range(game.klhere, len(game.enemies)):
+    for i in range(q.romulans):
         enemy(IHR, loc=dropin(), power=randreal(400.0,850.0)+50.0*game.skill)
     # If quadrant needs a starbase, put it in
     if q.starbase:
@@ -6171,7 +6162,7 @@ def setpassword():
                break
     else:
         game.passwd = ""
-        for i in range(3):
+        for i in range(8):
            game.passwd += chr(ord('a')+randrange(26))
 
 # Code from sst.c begins here
@@ -6216,8 +6207,6 @@ commands = {
     "CALL":            0,      # Synonym for MAYDAY
     "QUIT":            0,
     "HELP":            0,
-    "SEED":            0,
-    "VISUAL":          0,
 }
 
 def ACCEPT(cmd):       return (not commands[cmd] or (commands[cmd] & game.options))
@@ -6581,7 +6570,6 @@ def huh():
     skip(1)
     prout(_("Beg your pardon, Captain?"))
 
-
 def debugme():
     # access to the internals for debugging 
     proutn("Reset levels? ")
@@ -6686,7 +6674,7 @@ if __name__ == '__main__':
         #    else:
         game.options |= OPTION_TTY
         seed = int(time.time())
-        (options, arguments) = getopt.getopt(sys.argv[1:], "r:tx")
+        (options, arguments) = getopt.getopt(sys.argv[1:], "r:s:tx")
         for (switch, val) in options:
             if switch == '-r':
                 try:
@@ -6706,6 +6694,8 @@ if __name__ == '__main__':
                     raise SystemExit(1)
                 game.options |= OPTION_TTY
                 game.options &=~ OPTION_CURSES
+            elif switch == '-s':
+                seed = int(val)
             elif switch == '-t':
                 game.options |= OPTION_TTY
                 game.options &=~ OPTION_CURSES