Bug#1108819: trek: bugs in code (phasers/visual)

Brian E Mays bemays at gmail.com
Sat Jul 5 14:34:08 BST 2025


Package: bsdgames
Version: 2.17-29+b1
Severity: normal
Tags: patch upstream

Dear Maintainer,

This bug report (and fix) concerns the trek (Star Trek themed) game in
this package.  There are two bugs that I report here: (1) calculation
of phaser damage and (2) the output of the "visual" command.  I
outline the problems below.

PHASERS (phaser.c)

The way phasers work never seemed to be right.  After going through
the code, I noticed two problems:

(1) the formula described in the comments wasn't implemented correctly
in the code (a parenthesis was misplaced) and

(2) the formula has the spread exactly backwards.

According to the documentation and the comments in the code, the spread
is supposed to go from a tight shot (spread = 0) to an all-sweeping shot
(spread = 1).  However, the way that it is implemented in the code is
exactly opposite of that.  The way to fix this problem is to set the term
"sigma" in the formula to be sigma = 1 - spread.

Correcting this problem also requires recalculation of the OMEGA
constant, which is the normalization factor that reduces all phaser
hits to be between 0% and 100% of the amount of energy sent to the
phasers.  It is a mystery to me where the original number came from.

VISUAL COMMAND (trek.h)

The data structure `struct xy' contains two items, both of which are
`unsigned char'.  However, in visual.c, this struct is used as the
basis for an array, Visdelta, that specifies offsets as positive and
negative values.  The unsigned integer data type translates the -1 to
255, and the resulting output for the "visual" command in the game
becomes meaningless.  Changing these two items to "signed char" fixes
this problem and doesn't affect the rest of the program.


-- System Information:
Debian Release: 12.0
merged-usr: no
Architecture: amd64 (x86_64)

Kernel: Linux 6.9.7+bpo-amd64 (SMP w/4 CPU threads; PREEMPT)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /bin/dash
Init: sysvinit (via /sbin/init)
LSM: AppArmor: enabled

Versions of packages bsdgames depends on:
ii  libc6                 2.36-9+deb12u7
ii  libfl2                2.6.4-8.2
ii  libgcc-s1             12.2.0-14
ii  libncurses6           6.4-4
ii  libstdc++6            12.2.0-14
ii  libtinfo6             6.4-4
ii  wamerican [wordlist]  2020.12.07-2

bsdgames recommends no packages.

bsdgames suggests no packages.

-- no debconf information
-------------- next part --------------
--- trek/phaser.c.orig	2003-12-16 20:47:37.000000000 -0600
+++ trek/phaser.c	2025-07-04 12:00:33.627791860 -0500
@@ -49,9 +49,9 @@
 # define	BETA		3.0		/* franf() */
 # define	GAMMA		0.30		/* cos(angle) */
 # define	EPSILON		150.0		/* dist ** 2 */
-# define	OMEGA		10.596		/* overall scaling factor */
+# define	OMEGA		7.2596		/* overall scaling factor */
 
-/* OMEGA ~= 100 * (ALPHA + 1) * (BETA + 1) / (EPSILON + 1) */
+/* OMEGA ~= (EPSILON + 1) / ((ALPHA + 1) * (BETA + 1) * (GAMMA + 1)) */
 
 /*
 **  Phaser Control
@@ -325,7 +325,7 @@
 			**	* [cos(delta * sigma) + GAMMA]
 			**	* hit
 			**
-			** where sigma is the spread factor,
+			** where sigma is the spread factor (= 1 - spread),
 			** rho is a random number (0 -> 1),
 			** GAMMA is a crud factor for angle (essentially
 			**	cruds up the spread factor),
@@ -347,7 +347,7 @@
 			** tion applies.
 			*/
 			distfactor = BETA + franf();
-			distfactor *= ALPHA + b->spread;
+			distfactor *= ALPHA + (1.0 - b->spread);
 			distfactor *= OMEGA;
 			anglefactor = k->dist;
 			distfactor /= anglefactor * anglefactor + EPSILON;
@@ -355,7 +355,7 @@
 			dx = Ship.sectx - k->x;
 			dy = k->y - Ship.secty;
 			anglefactor = atan2(dy, dx) - b->angle;
-			anglefactor = cos((anglefactor * b->spread) + GAMMA);
+			anglefactor = (cos(anglefactor * (1.0 - b->spread)) + GAMMA);
 			if (anglefactor < 0.0)
 			{
 				k++;
--- trek/trek.h.orig	2005-02-16 00:24:50.000000000 -0600
+++ trek/trek.h	2025-07-04 09:13:02.999168786 -0500
@@ -201,7 +201,7 @@
 
 struct xy
 {
-	unsigned char	x, y;		/* coordinates */
+	signed char	x, y;		/* coordinates */
 };
 
 


More information about the Pkg-games-devel mailing list