X-Git-Url: https://jxself.org/git/?p=super-star-trek.git;a=blobdiff_plain;f=src%2Fsst.py;h=7eb5c938da4879ef6dda8ec2f86da88b6f4089ac;hp=a1c08bbaf1cdd87b0c8c2d5ae7530807455586a5;hb=ab4d7f5dd82fc30c4db6597dd6dd68b4dc33f98b;hpb=9cb2b7b1f79edde7e927a54b32c33d64973b7c67 diff --git a/src/sst.py b/src/sst.py index a1c08bb..7eb5c93 100644 --- a/src/sst.py +++ b/src/sst.py @@ -206,11 +206,6 @@ MAXBURST = 3 def VALID_QUADRANT(x, y): return ((x)>=0 and (x)=0 and (y)=0 and (x)=0 and (y) %s at %s\n" % (args, v, traceback.extract_stack()[-2][1:])) return v @@ -741,9 +743,9 @@ def movebaddy(enemy): motion = ((forces + randreal(200))/150.0) - 5.0 else: if forces > 1000.0: # Very strong -- move in for kill - motion = (1.0-square(randreal()))*dist1 + 1.0 + motion = (1.0 - randreal())**2 * dist1 + 1.0 if game.condition=="docked" and (game.options & OPTION_BASE): # protected by base -- back off ! - motion -= game.skill*(2.0-square(randreal())) + motion -= game.skill*(2.0-randreal()**2) if idebug: proutn("=== MOTION = %d, FORCES = %1.2f, " % (motion, forces)) # don't move if no motion @@ -947,7 +949,7 @@ def supercommander(): return sc = game.state.kscmdr for i in range(game.state.rembase): - basetbl.append((i, distance(game.state.baseq[i], sc))) + basetbl.append((i, (game.state.baseq[i] - sc).distance())) if game.state.rembase > 1: basetbl.sort(lambda x, y: cmp(x[1]. y[1])) # look for nearest base without a commander, no Enterprise, and @@ -1073,7 +1075,7 @@ def movetholian(): while here.x != id.x: #print "Moving in X", delta here.x += delta.x - if game.quad[here.kloc.x][here.y]==IHDOT: + if game.quad[here.x][here.y]==IHDOT: game.tholian.move(here) # move in y axis while here.y != id.y: @@ -1114,7 +1116,6 @@ def doshield(shraise): if scanner.sees("transfer"): action = "NRG" else: - scanner.chew() if damaged(DSHIELD): prout(_("Shields damaged and down.")) return @@ -1336,7 +1337,7 @@ def torpedo(course, dispersion, origin, number, nburst): if not VALID_SECTOR(w.x, w.y): break iquad=game.quad[w.x][w.y] - tracktorpedo(w, step, number, nburst, iquad) + tracktorpedo(origin, w, step, number, nburst, iquad) if iquad==IHDOT: continue # hit something @@ -1349,7 +1350,7 @@ def torpedo(course, dispersion, origin, number, nburst): crmshp() prout(".") hit = 700.0 + randreal(100) - \ - 1000.0 * distance(w, origin) * math.fabs(math.sin(bullseye-angle)) + 1000.0 * (w-origin).distance() * math.fabs(math.sin(bullseye-angle)) newcnd(); # we're blown out of dock # We may be displaced. if game.landed or game.condition=="docked": @@ -1386,7 +1387,7 @@ def torpedo(course, dispersion, origin, number, nburst): break kp = math.fabs(e.kpower) h1 = 700.0 + randrange(100) - \ - 1000.0 * distance(w, origin) * math.fabs(math.sin(bullseye-angle)) + 1000.0 * (w-origin).distance() * math.fabs(math.sin(bullseye-angle)) h1 = math.fabs(h1) if kp < h1: h1 = kp @@ -1444,7 +1445,7 @@ def torpedo(course, dispersion, origin, number, nburst): game.state.galaxy[game.quadrant.x][game.quadrant.y].planet = None game.iplnet.pclass = "destroyed" game.iplnet = None - invalidate(game.plnet) + game.plnet.invalidate() game.quad[w.x][w.y] = IHDOT if game.landed: # captain perishes on planet @@ -1457,7 +1458,7 @@ def torpedo(course, dispersion, origin, number, nburst): game.state.galaxy[game.quadrant.x][game.quadrant.y].planet = None game.iplnet.pclass = "destroyed" game.iplnet = None - invalidate(game.plnet) + game.plnet.invalidate() game.quad[w.x][w.y] = IHDOT if game.landed: # captain perishes on planet @@ -1503,7 +1504,7 @@ def torpedo(course, dispersion, origin, number, nburst): return None elif iquad == IHT: # Hit a Tholian h1 = 700.0 + randrange(100) - \ - 1000.0 * distance(w, origin) * math.fabs(math.sin(bullseye-angle)) + 1000.0 * (w-origin).distance() * math.fabs(math.sin(bullseye-angle)) h1 = math.fabs(h1) if h1 >= 600: game.quad[w.x][w.y] = IHDOT @@ -1534,7 +1535,7 @@ def torpedo(course, dispersion, origin, number, nburst): game.quad[jw.x][jw.y]=iquad prout(_(" displaced by blast to Sector %s ") % jw) for ll in range(len(game.enemies)): - game.enemies[ll].kdist = game.enemies[ll].kavgd = distance(game.sector,game.enemies[ll].kloc) + game.enemies[ll].kdist = game.enemies[ll].kavgd = (game.sector-game.enemies[ll].kloc).distance() game.enemies.sort(lambda x, y: cmp(x.kdist, y.kdist)) return None skip(1) @@ -1562,7 +1563,7 @@ def fry(hit): for (i, j) in enumerate(cdam): proutn(device[j]) if skipcount % 3 == 2 and i < len(cdam)-1: - skip() + skip(1) skipcount += 1 if i < len(cdam)-1: proutn(_(" and ")) @@ -1626,7 +1627,7 @@ def attack(torps_ok): if game.condition == "docked": continue; # Don't waste the effort! attempt = True; # Attempt to attack - dustfac = 0.8 + randreal(0.5) + dustfac = randreal(0.8, 0.85) hit = enemy.kpower*math.pow(dustfac,enemy.kavgd) enemy.kpower *= 0.75 else: # Enemy uses photon torpedo @@ -1844,17 +1845,17 @@ def photon(): if key != IHREAL: huh() return - targ[i].x = scanner.real + targ[i].x = scanner.int() key = scanner.next() if key != IHREAL: huh() return - targ[i].y = scanner.real + targ[i].y = scanner.int() course[i] = targetcheck(targ[i]) if course[i] == None: return scanner.chew() - if i == 1 and key == IHEOL: + if i == 0 and key == IHEOL: # prompt for each one for i in range(n): proutn(_("Target sector for torpedo number %d- ") % (i+1)) @@ -1862,12 +1863,12 @@ def photon(): if key != IHREAL: huh() return - targ[i].x = int(scanner.real-0.5) + targ[i].x = scanner.int() key = scanner.next() if key != IHREAL: huh() return - targ[i].y = int(scanner.real-0.5) + targ[i].y = scanner.int() scanner.chew() course[i] = targetcheck(targ[i]) if course[i] == None: @@ -1949,6 +1950,7 @@ def hittem(hits): nenhr2 = len(game.enemies); kk=0 w = coord() skip(1) + print "Hits are:", hits for (k, wham) in enumerate(hits): if wham==0: continue @@ -2371,7 +2373,7 @@ def events(): game.battle = hold game.isatb = 0 else: - invalidate(game.battle) + game.battle.invalidate() if idebug: prout("=== EVENTS from %.2f to %.2f:" % (game.state.date, fintim)) @@ -2468,7 +2470,7 @@ def events(): (game.torps < 5 or damaged(DPHOTON))): # Tractor-beam her! istract = ictbeam = True - tractorbeam(distance(game.state.kscmdr, game.quadrant)) + tractorbeam((game.state.kscmdr-game.quadrant).distance()) else: return elif evcode == FTBEAM: # Tractor beam @@ -2476,7 +2478,7 @@ def events(): unschedule(FTBEAM) continue i = randrange(game.state.remcom) - yank = distance(game.state.kcmdr[i], game.quadrant) + yank = (game.state.kcmdr[i]-game.quadrant).distance() if istract or game.condition == "docked" or yank == 0: # Drats! Have to reschedule schedule(FTBEAM, @@ -2543,7 +2545,7 @@ def events(): if i > game.state.remcom or game.state.rembase == 0 or \ not game.state.galaxy[game.battle.x][game.battle.y].starbase: # No action to take after all - invalidate(game.battle) + game.battle.invalidate() continue destroybase() elif evcode == FSCMOVE: # Supercommander moves @@ -2803,7 +2805,7 @@ def nova(nov): prout(_(" destroyed.")) game.iplnet.pclass = "destroyed" game.iplnet = None - invalidate(game.plnet) + game.plnet.invalidate() if game.landed: finish(FPNOVA) return @@ -2815,7 +2817,7 @@ def nova(nov): break game.state.baseq[i] = game.state.baseq[game.state.rembase] game.state.rembase -= 1 - invalidate(game.base) + game.base.invalidate() game.state.basekl += 1 newcnd() crmena(True, IHB, "sector", neighbor) @@ -2936,13 +2938,12 @@ def supernova(induced, w=None): prouts(_("***RED ALERT! RED ALERT!")) skip(1) prout(_("***Incipient supernova detected at Sector %s") % ns) - if square(ns.x-game.sector.x) + square(ns.y-game.sector.y) <= 2.1: + if (ns.x-game.sector.x)**2 + (ns.y-game.sector.y)**2 <= 2.1: proutn(_("Emergency override attempts t")) prouts("***************") skip(1) stars() game.alldone = True - # destroy any Klingons in supernovaed quadrant kldead = game.state.galaxy[nq.x][nq.y].klingons game.state.galaxy[nq.x][nq.y].klingons = 0 @@ -2952,17 +2953,13 @@ def supernova(induced, w=None): game.iscate = False unschedule(FSCMOVE) unschedule(FSCDBAS) - if game.state.remcom: - maxloop = game.state.remcom - for l in range(maxloop): - if game.state.kcmdr[l] == nq: - game.state.kcmdr[l] = game.state.kcmdr[game.state.remcom] - invalidate(game.state.kcmdr[game.state.remcom]) - game.state.remcom -= 1 - kldead -= 1 - if game.state.remcom==0: - unschedule(FTBEAM) - break + survivors = filter(lambda w: w != nq, game.state.kcmdr) + comkills = len(game.state.kcmdr) - len(survivors) + game.state.kcmdr = survivors + kldead -= comkills + game.state.remcom -= comkills + if game.state.remcom==0: + unschedule(FTBEAM) game.state.remkl -= kldead # destroy Romulans and planets in supernovaed quadrant nrmdead = game.state.galaxy[nq.x][nq.y].romulans @@ -2973,15 +2970,9 @@ def supernova(induced, w=None): if game.state.planets[loop].w == nq: game.state.planets[loop].pclass = "destroyed" npdead += 1 - # Destroy any base in supernovaed quadrant - if game.state.rembase: - maxloop = game.state.rembase - for loop in range(maxloop): - if game.state.baseq[loop] == nq: - game.state.baseq[loop] = game.state.baseq[game.state.rembase] - invalidate(game.state.baseq[game.state.rembase]) - game.state.rembase -= 1 - break + # Destroy any base in supernovaed quadrant + game.state.baseq = filter(lambda x: x != nq, game.state.baseq) + game.state.rembase = len(game.state.baseq) # If starship caused supernova, tally up destruction if induced: game.state.starkl += game.state.galaxy[nq.x][nq.y].stars @@ -3707,13 +3698,13 @@ def warble(): #nosound() pass -def tracktorpedo(w, step, i, n, iquad): +def tracktorpedo(origin, w, step, i, n, iquad): "Torpedo-track animation." if not game.options & OPTION_CURSES: if step == 1: if n != 1: skip(1) - proutn(_("Track for torpedo number %d- ") % i) + proutn(_("Track for %s torpedo number %d- ") % (game.quad[origin.x][origin.y],i+1)) else: skip(1) proutn(_("Torpedo track- ")) @@ -3775,16 +3766,16 @@ def imove(novapush): def no_quad_change(): # No quadrant change -- compute new average enemy distances game.quad[game.sector.x][game.sector.y] = game.ship - if len(game.enemies): - for m in range(len(game.enemies)): - finald = distance(w, game.enemies[m].kloc) - game.enemies[m].kavgd = 0.5 * (finald+game.enemies[m].kdist) - game.enemies[m].kdist = finald + if game.enemies: + for enemy in game.enemies: + finald = (w-game.enemy.kloc).distance() + enemy.kavgd = 0.5 * (finald + ememy.kdist) + enemy.kdist = finald game.enemies.sort(lambda x, y: cmp(x.kdist, y.kdist)) if not game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova: attack(torps_ok=False) - for m in range(len(game.enemies)): - game.enemies[m].kavgd = game.enemies[m].kdist + for enemy in game.enemies: + enemy.kavgd = enemy.kdist newcnd() drawmaps(0) setwnd(message_window) @@ -3823,9 +3814,9 @@ def imove(novapush): # Don't do it if being pushed by Nova if len(game.enemies) != 0 and not novapush: newcnd() - for m in range(len(game.enemies)): - finald = distance(w, game.enemies[m].kloc) - game.enemies[m].kavgd = 0.5 * (finald + game.enemies[m].kdist) + for enemy in game.enemies: + finald = (w - enemy.kloc).distance() + enemy.kavgd = 0.5 * (finald + enemy.kdist) # # Stas Sergeev added the condition # that attacks only happen if Klingons @@ -3888,7 +3879,7 @@ def imove(novapush): if iquad != IHDOT: # object encountered in flight path stopegy = 50.0*game.dist/game.optime - game.dist = distance(game.sector, w) / (QUADSIZE * 1.0) + game.dist = (game.sector - w).distance() / (QUADSIZE * 1.0) if iquad in (IHT, IHK, IHC, IHS, IHR, IHQUEST): game.sector = w for enemy in game.enemies: @@ -3938,7 +3929,7 @@ def imove(novapush): # We're here! no_quad_change() return - game.dist = distance(game.sector, w) / (QUADSIZE * 1.0) + game.dist = (game.sector - w).distance() / (QUADSIZE * 1.0) game.sector = w final = game.sector no_quad_change() @@ -3953,7 +3944,7 @@ def dock(verbose): if game.inorbit: prout(_("You must first leave standard orbit.")) return - if not is_valid(game.base) or abs(game.sector.x-game.base.x) > 1 or abs(game.sector.y-game.base.y) > 1: + if not game.base.is_valid() or abs(game.sector.x-game.base.x) > 1 or abs(game.sector.y-game.base.y) > 1: crmshp() prout(_(" not adjacent to base.")) return @@ -4227,7 +4218,7 @@ def warp(timewarp): if game.warpfac > 6.0: # Decide if engine damage will occur # ESR: Seems wrong. Probability of damage goes *down* with distance? - prob = game.dist*square(6.0-game.warpfac)/66.666666666 + prob = game.dist*(6.0-game.warpfac)**2/66.666666666 if prob > randreal(): blooey = True game.dist = randreal(game.dist) @@ -4429,7 +4420,7 @@ def timwrp(): game.isatb = 0 unschedule(FCDBAS) unschedule(FSCDBAS) - invalidate(game.battle) + game.battle.invalidate() # Make sure Galileo is consistant -- Snapshot may have been taken # when on planet, which would give us two Galileos! @@ -4564,11 +4555,11 @@ def mayday(): game.nhelp += 1 if game.base.x!=0: # There's one in this quadrant - ddist = distance(game.base, game.sector) + ddist = (game.base - game.sector).distance() else: ddist = FOREVER for m in range(game.state.rembase): - xdist = QUADSIZE * distance(game.state.baseq[m], game.quadrant) + xdist = QUADSIZE * (game.state.baseq[m] - game.quadrant).distance() if xdist < ddist: ddist = xdist line = m @@ -4587,7 +4578,7 @@ def mayday(): # found one -- finish up game.sector = w break - if not is_valid(game.sector): + if not game.sector.is_valid(): prout(_("You have been lost in space...")) finish(FMATERIALIZE) return @@ -4786,7 +4777,7 @@ def orbit(): if damaged(DWARPEN) and damaged(DIMPULS): prout(_("Both warp and impulse engines damaged.")) return - if not is_valid(game.plnet) or abs(game.sector.x-game.plnet.x) > 1 or abs(game.sector.y-game.plnet.y) > 1: + if not game.plnet.is_valid() or 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) @@ -5536,8 +5527,8 @@ def eta(): if not VALID_QUADRANT(w1.x, w1.y) or not VALID_SECTOR(w2.x, w2.y): huh() return - game.dist = math.sqrt(square(w1.y-game.quadrant.y+0.1*(w2.y-game.sector.y))+ - square(w1.x-game.quadrant.x+0.1*(w2.x-game.sector.x))) + game.dist = math.sqrt((w1.y-game.quadrant.y+0.1*(w2.y-game.sector.y))**2+ + (w1.x-game.quadrant.x+0.1*(w2.x-game.sector.x))**2) wfl = False if prompt: prout(_("Answer \"no\" if you don't know the value:")) @@ -5568,7 +5559,7 @@ def eta(): prout(_("Captain, certainly you can give me one of these.")) while True: scanner.chew() - ttime = (10.0*game.dist)/square(twarp) + ttime = (10.0*game.dist)/twarp**2 tpower = game.dist*twarp*twarp*twarp*(game.shldup+1) if tpower >= game.energy: prout(_("Insufficient energy, sir.")) @@ -5832,7 +5823,7 @@ def setup(): # so it did them in the opposite order. for j in range(1, i): # Improved placement algorithm to spread out bases - distq = w.distance(game.state.baseq[j]) + distq = (w - game.state.baseq[j]).distance() if distq < 6.0*(BASEMAX+1-game.inbase) and withprob(0.75): contflag = True if idebug: @@ -5866,7 +5857,7 @@ def setup(): if krem <= 0: break # Position Klingon Commander Ships - for i in range(1, game.incom+1): + 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 \ @@ -5990,7 +5981,7 @@ def choose(): return True if scanner.sees("regular"): break - proutn(_("What is \"%s\"?"), scanner.token) + proutn(_("What is \"%s\"?") % scanner.token) scanner.chew() while game.length==0 or game.skill==SKILL_NONE: if scanner.next() == IHALPHA: @@ -6365,6 +6356,8 @@ def makemoves(): if game.options & OPTION_CURSES: makechart() continue + elif scanner.token == "": + continue game.ididit = False clrscr() setwnd(message_window) @@ -6375,7 +6368,7 @@ def makemoves(): cmd = candidates[0] break elif candidates and not (game.options & OPTION_PLAIN): - prout("Commands with that prefix: " + " ".join(candidates)) + prout("Commands with prefix '%s': %s" % (scanner.token, " ".join(candidates))) else: listCommands() continue @@ -6568,24 +6561,21 @@ class sstscanner: # Get a token from the user self.real = 0.0 self.token = '' - # Read a line if nothing here - if self.inqueue == None: + # Fill the token quue if nothing here + while not self.inqueue: line = cgetline() if curwnd==prompt_window: clrscr() setwnd(message_window) clrscr() + if line == '': + return None # Skip leading white space line = line.lstrip() - if line: - self.inqueue = line.split() + if not line: + continue else: - self.inqueue = [] - self.type = IHEOL - return IHEOL - elif not self.inqueue: - self.type = IHEOL - return IHEOL + self.inqueue = line.lstrip().split() + [IHEOL] # From here on in it's all looking at the queue self.token = self.inqueue.pop(0) if self.token == IHEOL: @@ -6617,7 +6607,7 @@ class sstscanner: return s.startswith(self.token) def int(self): # Round token value to nearest integer - return int(round(scanner.real + 0.5)) + return int(round(scanner.real)) def ja(): # yes-or-no confirmation