Fixed the Python help command.
[super-star-trek.git] / src / sst.py
index 5e0aa69269ee0503fcf9d07ccb6217ea545fa8aa..cfdfe383e254b43119b4b57a5a74f9b2b407969d 100644 (file)
@@ -13,8 +13,7 @@ ion how to modify (and how not to modify!) this code.
 """
 import os, sys, math, curses, time, readline, cPickle, random, copy, gettext, getpass
 
 """
 import os, sys, math, curses, time, readline, cPickle, random, copy, gettext, getpass
 
-SSTDOC         = "/usr/share/doc/sst/sst.doc"
-DOC_NAME       = "sst.doc"
+docpath        = (".", "../doc", "/usr/share/doc/sst")
 
 def _(str): return gettext.gettext(str)
 
 
 def _(str): return gettext.gettext(str)
 
@@ -33,6 +32,24 @@ 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
 
 DOCKFAC                = 0.25          # Repair faster when docked
 PHASEFAC       = 2.0           # Unclear what this is, it was in the C version
 
+DEFAULT      = -1
+BLACK        = 0
+BLUE         = 1
+GREEN        = 2
+CYAN         = 3
+RED          = 4
+MAGENTA      = 5
+BROWN        = 6
+LIGHTGRAY    = 7
+DARKGRAY     = 8
+LIGHTBLUE    = 9
+LIGHTGREEN   = 10
+LIGHTCYAN    = 11
+LIGHTRED     = 12
+LIGHTMAGENTA = 13
+YELLOW       = 14
+WHITE        = 15
+
 class TrekError:
     pass
 
 class TrekError:
     pass
 
@@ -183,6 +200,7 @@ OPTION_WORLDS       = 0x00000800    # logic for inhabited worlds (ESR, 2006)
 OPTION_AUTOSCAN        = 0x00001000    # automatic LRSCAN before CHART (ESR, 2006)
 OPTION_PLAIN   = 0x01000000    # user chose plain game 
 OPTION_ALMY    = 0x02000000    # user chose Almy variant 
 OPTION_AUTOSCAN        = 0x00001000    # automatic LRSCAN before CHART (ESR, 2006)
 OPTION_PLAIN   = 0x01000000    # user chose plain game 
 OPTION_ALMY    = 0x02000000    # user chose Almy variant 
+OPTION_COLOR    = 0x04000000   # enable color display (experimental, ESR, 2010)
 
 # Define devices 
 DSRSENS        = 0
 
 # Define devices 
 DSRSENS        = 0
@@ -3058,6 +3076,17 @@ def iostart():
        stdscr.keypad(True)
        curses.nonl()
        curses.cbreak()
        stdscr.keypad(True)
        curses.nonl()
        curses.cbreak()
+        if game.options & OPTION_COLOR:
+            curses.start_color();
+            curses.use_default_colors()
+            curses.init_pair(curses.COLOR_BLACK,   curses.COLOR_BLACK, -1);
+            curses.init_pair(curses.COLOR_GREEN,   curses.COLOR_GREEN, -1);
+            curses.init_pair(curses.COLOR_RED,     curses.COLOR_RED, -1);
+            curses.init_pair(curses.COLOR_CYAN,    curses.COLOR_CYAN, -1);
+            curses.init_pair(curses.COLOR_WHITE,   curses.COLOR_WHITE, -1);
+            curses.init_pair(curses.COLOR_MAGENTA, curses.COLOR_MAGENTA, -1);
+            curses.init_pair(curses.COLOR_BLUE,    curses.COLOR_BLUE, -1);
+            curses.init_pair(curses.COLOR_YELLOW,  curses.COLOR_YELLOW, -1);
         global fullscreen_window, srscan_window, report_window, status_window
         global lrscan_window, message_window, prompt_window
         (rows, columns)   = stdscr.getmaxyx()
         global fullscreen_window, srscan_window, report_window, status_window
         global lrscan_window, message_window, prompt_window
         (rows, columns)   = stdscr.getmaxyx()
@@ -3204,7 +3233,48 @@ def clrscr():
        curwnd.move(0, 0)
        curwnd.refresh()
     linecount = 0
        curwnd.move(0, 0)
        curwnd.refresh()
     linecount = 0
-    
+
+def textcolor(color=DEFAULT):
+    if game.options & OPTION_COLOR:
+       if color == DEFAULT: 
+           curwnd.attrset(0);
+       elif color ==  BLACK: 
+           curwnd.attron(curses.color_pair(curses.COLOR_BLACK));
+       elif color ==  BLUE: 
+           curwnd.attron(curses.color_pair(curses.COLOR_BLUE));
+       elif color ==  GREEN: 
+           curwnd.attron(curses.color_pair(curses.COLOR_GREEN));
+       elif color ==  CYAN: 
+           curwnd.attron(curses.color_pair(curses.COLOR_CYAN));
+       elif color ==  RED: 
+           curwnd.attron(curses.color_pair(curses.COLOR_RED));
+       elif color ==  MAGENTA: 
+           curwnd.attron(curses.color_pair(curses.COLOR_MAGENTA));
+       elif color ==  BROWN: 
+           curwnd.attron(curses.color_pair(curses.COLOR_YELLOW));
+       elif color ==  LIGHTGRAY: 
+           curwnd.attron(curses.color_pair(curses.COLOR_WHITE));
+       elif color ==  DARKGRAY: 
+           curwnd.attron(curses.color_pair(curses.COLOR_BLACK) | curses.A_BOLD);
+       elif color ==  LIGHTBLUE: 
+           curwnd.attron(curses.color_pair(curses.COLOR_BLUE) | curses.A_BOLD);
+       elif color ==  LIGHTGREEN: 
+           curwnd.attron(curses.color_pair(curses.COLOR_GREEN) | curses.A_BOLD);
+       elif color ==  LIGHTCYAN: 
+           curwnd.attron(curses.color_pair(curses.COLOR_CYAN) | curses.A_BOLD);
+       elif color ==  LIGHTRED: 
+           curwnd.attron(curses.color_pair(curses.COLOR_RED) | curses.A_BOLD);
+       elif color ==  LIGHTMAGENTA: 
+           curwnd.attron(curses.color_pair(curses.COLOR_MAGENTA) | curses.A_BOLD);
+       elif color ==  YELLOW: 
+           curwnd.attron(curses.color_pair(curses.COLOR_YELLOW) | curses.A_BOLD);
+       elif color ==  WHITE:
+           curwnd.attron(curses.color_pair(curses.COLOR_WHITE) | curses.A_BOLD);
+
+def highvideo():
+    if game.options & OPTION_COLOR:
+        curwnd.attron(curses.A_REVERSE)
+
 #
 # Things past this point have policy implications.
 # 
 #
 # Things past this point have policy implications.
 # 
@@ -4107,10 +4177,12 @@ def mayday():
        elif m == 3: proutn(_("3rd"))
        proutn(_(" attempt to re-materialize ") + crmshp())
        game.quad[ix][iy]=('-','o','O')[m-1]
        elif m == 3: proutn(_("3rd"))
        proutn(_(" attempt to re-materialize ") + crmshp())
        game.quad[ix][iy]=('-','o','O')[m-1]
+        textcolor(RED)
        warble()
        if randreal() > probf:
            break
        prout(_("fails."))
        warble()
        if randreal() > probf:
            break
        prout(_("fails."))
+        textcolor(DEFAULT)
        curses.delay_output(500)
     if m > 3:
        game.quad[ix][iy]='?'
        curses.delay_output(500)
     if m > 3:
        game.quad[ix][iy]='?'
@@ -4120,7 +4192,9 @@ def mayday():
        finish(FMATERIALIZE)
        return
     game.quad[ix][iy]=game.ship
        finish(FMATERIALIZE)
        return
     game.quad[ix][iy]=game.ship
+    textcolor(GREEN);
     prout(_("succeeds."))
     prout(_("succeeds."))
+    textcolor(DEFAULT);
     dock(False)
     skip(1)
     prout(_("Lt. Uhura-  \"Captain, we made it!\""))
     dock(False)
     skip(1)
     prout(_("Lt. Uhura-  \"Captain, we made it!\""))
@@ -4868,7 +4942,15 @@ def chart():
 def sectscan(goodScan, i, j):
     "Light up an individual dot in a sector."
     if goodScan or (abs(i-game.sector.i)<= 1 and abs(j-game.sector.j) <= 1):
 def sectscan(goodScan, i, j):
     "Light up an individual dot in a sector."
     if goodScan or (abs(i-game.sector.i)<= 1 and abs(j-game.sector.j) <= 1):
+        textcolor({"green":GREEN,
+                   "yellow":YELLOW,
+                   "red":RED,
+                   "docked":CYAN,
+                   "dead":BROWN}[game.condition]) 
+        if game.quad[i][j] != game.ship: 
+            highvideo();
        proutn("%c " % game.quad[i][j])
        proutn("%c " % game.quad[i][j])
+        textcolor(DEFAULT)
     else:
        proutn("- ")
 
     else:
        proutn("- ")
 
@@ -5486,6 +5568,7 @@ def choose():
        pass
     elif len(scanner.token):
         proutn(_("What is \"%s\"?") % scanner.token)
        pass
     elif len(scanner.token):
         proutn(_("What is \"%s\"?") % scanner.token)
+    game.options &=~ OPTION_COLOR
     setpassword()
     if game.passwd == "debug":
        idebug = True
     setpassword()
     if game.passwd == "debug":
        idebug = True
@@ -5728,7 +5811,7 @@ def helpme():
        setwnd(message_window)
        if key == "IHEOL":
            return
        setwnd(message_window)
        if key == "IHEOL":
            return
-        if scanner.token in commands or scanner.token == "ABBREV":
+        if scanner.token.upper() in commands or scanner.token == "ABBREV":
            break
        skip(1)
        listCommands()
            break
        skip(1)
        listCommands()
@@ -5736,22 +5819,20 @@ def helpme():
        scanner.chew()
        skip(1)
     cmd = scanner.token.upper()
        scanner.chew()
        skip(1)
     cmd = scanner.token.upper()
-    try:
-        fp = open(SSTDOC, "r")
-    except IOError:
+    for directory in docpath:
         try:
         try:
-            fp = open(DOC_NAME, "r")
+            fp = open(os.path.join(directory, "sst.doc"), "r")
+            break
         except IOError:
         except IOError:
-            prout(_("Spock-  \"Captain, that information is missing from the"))
-            proutn(_("   computer. You need to find "))
-            proutn(DOC_NAME)
-            prout(_(" and put it in the"))
-            proutn(_("   current directory or to "))
-            proutn(SSTDOC)
-            prout(".\"")
-            # This used to continue: "You need to find SST.DOC and put 
-            # it in the current directory."
-            return
+            pass
+    else:
+        prout(_("Spock-  \"Captain, that information is missing from the"))
+        prout(_("   computer. You need to find sst.doc and put it somewhere"))
+        proutn(_("   in these directories: %s") % ":".join(docpath))
+        prout(".\"")
+        # This used to continue: "You need to find SST.DOC and put 
+        # it in the current directory."
+        return
     while True:
         linebuf = fp.readline()
        if linebuf == '':
     while True:
         linebuf = fp.readline()
        if linebuf == '':
@@ -5760,12 +5841,13 @@ def helpme():
            return
        if linebuf[0] == '%' and linebuf[1] == '%' and linebuf[2] == ' ':
             linebuf = linebuf[3:].strip()
            return
        if linebuf[0] == '%' and linebuf[1] == '%' and linebuf[2] == ' ':
             linebuf = linebuf[3:].strip()
-            if cmd == linebuf:
+            if cmd.upper() == linebuf:
                break
     skip(1)
     prout(_("Spock- \"Captain, I've found the following information:\""))
     skip(1)
                break
     skip(1)
     prout(_("Spock- \"Captain, I've found the following information:\""))
     skip(1)
-    while linebuf in fp:
+    while True:
+        linebuf = fp.readline()
         if "******" in linebuf:
            break
        proutn(linebuf)
         if "******" in linebuf:
            break
        proutn(linebuf)