def VALID_QUADRANT(x, y): return ((x)>=0 and (x)<GALSIZE and (y)>=0 and (y)<GALSIZE)
def VALID_SECTOR(x, y): return ((x)>=0 and (x)<QUADSIZE and (y)>=0 and (y)<QUADSIZE)
-def square(i): return ((i)*(i))
-def distance(c1, c2): return math.sqrt(square(c1.x - c2.x) + square(c1.y - c2.y))
-def invalidate(w): w.x = w.y = 0
-def is_valid(w): return (w.x != 0 and w.y != 0)
-
# How to represent features
IHR = 'R',
IHK = 'K',
return coord(self.x+self.x, self.y+self.y)
def __sub__(self, other):
return coord(self.x-other.x, self.y-other.y)
- def distance(self, other):
+ def __mul__(self, other):
+ return coord(self.x*other, self.y*other)
+ def __rmul__(self, other):
+ return coord(self.x*other, self.y*other)
+ def distance(self, other=None):
+ if not other: other = coord(0, 0)
return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
def sgn(self):
s = coord()
else:
s.y = self.y / abs(self.y)
return s
+ def course(self):
+ return 1.90985*math.atan2(self.y, self.x)
def scatter(self):
s = coord()
s.x = self.x + randrange(-1, 2)
if loc:
self.kloc = copy.copy(loc)
game.quad[self.kloc.x][self.kloc.y] = self.type
- self.kdist = self.kavgd = distance(game.sector, loc)
+ self.kdist = self.kavgd = (game.sector - loc).distance()
else:
self.kloc = coord()
self.kdist = self.kavgd = None
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
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
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":
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
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
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
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
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)
hit = enemy.kpower*math.pow(dustfac,enemy.kavgd)
enemy.kpower *= 0.75
else: # Enemy uses photon torpedo
- course = 1.90985*math.atan2(game.sector.y-enemy.kloc.y, enemy.kloc.x-game.sector.x)
+ course = (enemy.kloc - game.sector).course()
hit = 0
proutn(_("***TORPEDO INCOMING"))
if not damaged(DSRSENS):
if not VALID_SECTOR(w.x, w.y):
huh()
return None
- deltx = 0.1*(w.y - game.sector.y)
- delty = 0.1*(w.x - game.sector.x)
- if deltx==0 and delty== 0:
+ delt = 0.1*(w - game.sector)
+ if delt.x==0 and delt.y==0:
skip(1)
prout(_("Spock- \"Bridge to sickbay. Dr. McCoy,"))
prout(_(" I recommend an immediate review of"))
prout(_(" the Captain's psychological profile.\""))
scanner.chew()
return None
- return 1.90985932*math.atan2(deltx, delty)
+ return delt.course()
def photon():
# launch photon torpedo
msgflag = False
rpow = 0.0
if damaged(DSRSENS) and \
- not game.sector.distance(aim)<2**0.5 and ienm in (IHC, IHS):
+ (aim-game.sector).distance()>2**0.5 and ienm in (IHC, IHS):
cramen(ienm)
prout(_(" can't be located without short range scan."))
scanner.chew()
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))
(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
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,
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
prout(_(" destroyed."))
game.iplnet.pclass = "destroyed"
game.iplnet = None
- invalidate(game.plnet)
+ game.plnet.invalidate()
if game.landed:
finish(FPNOVA)
return
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)
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-game - sector).distance() <= 2.1**0.5:
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
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
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.baseq[game.state.rembase].invalidate()
game.state.rembase -= 1
break
# If starship caused supernova, tally up destruction
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)
# 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
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:
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
skip(1)
prout(_("Helmsman Sulu- \"Aye, Sir.\""))
# Course actually laid in.
- game.dist = math.sqrt(deltax*deltax + deltay*deltay)
- game.direc = math.atan2(deltax, deltay)*1.90985932
+ game.dist = coord(deltax, deltay).distance()
+ game.direc = coord(deltax, deltay).course()
if game.direc < 0.0:
game.direc += 12.0
scanner.chew()
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)
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!
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
# 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
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)
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 = ((w1 - game.quadrant) + 0.1 * (w2 - game.sector)).distance()
wfl = False
if prompt:
prout(_("Answer \"no\" if you don't know the value:"))
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."))
# 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: