More timeline stuff.
[super-star-trek.git] / doc / HACKING
1 This is the hackers' guide to SST2K.  Read it before messing with the code.
2
3 It consists of an introduction, a history, suggestions for regression testing,
4 and some notes on the Python translation.  For a to-do list, see TODO in the
5 top-level directory.
6
7 INTRODUCTION:
8
9 SST2K is a Python translation of a C translation of a FORTRAN
10 original dating back to 1973.  Beautiful Python it is not, but it
11 works. 
12
13 The intention of SST2K is to be able to replicate precisely the 
14 experience of the original game, while allowing new features to be
15 added under option control.  Therefore, be very conservative about
16 what changes are visible under the 'plain' option.
17
18 HISTORY:
19
20 Dave Matuszek, one of the two original authors, says:
21
22 SRSCAN, MOVE, PHASERS, CALL, STATUS, IMPULSE, PHOTONS, ABANDON,
23 LRSCAN, WARP, SHIELDS, DESTRUCT, CHART, REST, DOCK, QUIT, and DAMAGE
24 were in the original non-"super" version of UT FORTRAN Star Trek.
25
26 Tholians were not in the original. Dave is dubious about their merits.
27 (They are now controlled by OPTION_THOLIAN and turned off if the game
28 type is "plain".)
29
30 Planets and dilithium crystals were not in the original.  Dave is OK
31 with this idea. (It's now controlled by OPTION_PLANETS and turned 
32 off if the game type is "plain".)
33
34 Dave says the bit about the Galileo getting turned into a
35 McDonald's is "consistant with our original vision".  (This has been
36 left permanently enabled, as it can only happen if OPTION_PLANETS
37 is on.)
38
39 Dave also says the Space Thingy should not be preserved across saved
40 games, so you can't prove to others that you've seen it.  He says it
41 shouldn't fire back, either.  It should do nothing except scream and
42 disappear when hit by photon torpedos.  It's OK that it may move
43 when attacked, but it didn't in the original. (The no-save behavior
44 has been restored.)
45
46 Dave says the Faerie Queen, black holes, and time warping were in the original.
47 However, eithrt he is misremembering about time watping or
48 the original implementation was lost during the FORTRAN-to-C translation, as
49 Stas Sergeev added a time-warping feature in 2006.
50
51 Here are Tom Almy's changes:
52
53 In early 1997, I got the bright idea to look for references to
54 "Super Star Trek" on the World Wide Web. There weren't many hits,
55 but there was one that came up with 1979 Fortran sources! This
56 version had a few additional features that mine didn't have,
57 however mine had some feature it didn't have. So I merged its
58 features that I liked. I also took a peek at the DECUS version (a
59 port, less sources, to the PDP-10), and some other variations.
60
61 1. Compared to the original UT version, I've changed the "help"
62 command to "call" and the "terminate" command to "quit" to better
63 match user expectations. The DECUS version apparently made those
64 changes as well as changing "freeze" to "save". However I like
65 "freeze".  (Both "freeze" and "save" work in SST2K.)
66
67 2. The experimental deathray originally had only a 5% chance of
68 success, but could be used repeatedly. I guess after a couple
69 years of use, it was less "experimental" because the 1979
70 version had a 70% success rate. However it was prone to breaking
71 after use. I upgraded the deathray, but kept the original set of
72 failure modes (great humor!).  (Now controlled by OPTION_DEATHRAY
73 and turned off if game type is "plain".)
74
75 3. The 1979 version also mentions srscan and lrscan working when
76 docked (using the starbase's scanners), so I made some changes here
77 to do this (and indicating that fact to the player), and then realized
78 the base would have a subspace radio as well -- doing a Chart when docked
79 updates the star chart, and all radio reports will be heard. The Dock
80 command will also give a report if a base is under attack.
81
82 4. Tholian Web from the 1979 version.  (Now controlled by
83 OPTION_THOLIAN and turned off if game type is "plain".)
84
85 5. Enemies can ram the Enterprise. (Now controlled by OPTION_RAMMING
86 and turned off if game type is "plain".)
87
88 6. Regular Klingons and Romulans can move in Expert and Emeritus games. 
89 This code could use improvement. (Now controlled by OPTION_MVBADDY
90 and turned off if game type is "plain".)
91
92 7. The deep-space probe feature from the DECUS version.  (Now controlled
93 by OPTION_PROBE and turned off if game type is "plain").
94
95 8. 'emexit' command from the 1979 version.
96
97 9. Bugfix: Klingon commander movements are no longer reported if long-range 
98 sensors are damaged.
99
100 10. Bugfix: Better base positioning at startup (more spread out).
101 That made sense to add because most people abort games with 
102 bad base placement.
103
104 In June 2002, I fixed two known bugs and a documentation typo.
105 In June 2004 I fixed a number of bugs involving: 1) parsing invalid
106 numbers, 2) manual phasers when SR scan is damaged and commander is
107 present, 3) time warping into the future, 4) hang when moving
108 klingons in crowded quadrants.  (These fixes are in SST2K.)
109
110 Here are Stas Sergeev's changes:
111
112 1. The Space Thingy can be shoved, if you ram it, and can fire back if 
113 fired upon. (Now controlled by OPTION_THINGY and turned off if game 
114 type is "plain" or "almy".)
115
116 2. When you are docked, base covers you with an almost invincible shield. 
117 (A commander can still ram you, or a Romulan can destroy the base,
118 or a SCom can even succeed with direct attack IIRC, but this rarely 
119 happens.)  (Now controlled by OPTION_BASE and turned off if game 
120 type is "plain" or "almy".)
121
122 3. Ramming a black hole is no longer instant death.  There is a
123 chance you might get timewarped instead. (Now controlled by 
124 OPTION_BLKHOLE and turned off if game type is "plain" or "almy".)
125
126 4. The Tholian can be hit with phasers.
127
128 5. SCom can't escape from you if no more enemies remain 
129 (without this, chasing SCom can take an eternity).
130
131 6. Probe target you enter is now the destination quadrant. Before I don't 
132 remember what it was, but it was something I had difficulty using.
133
134 7. Secret password is now autogenerated.
135
136 8. "Plaque" is adjusted for A4 paper :-)
137
138 9. Phasers now tells you how much energy needed, but only if the computer 
139 is alive.
140
141 10. Planets are auto-scanned when you enter the quadrant.
142
143 11. Mining or using crystals in presense of enemy now yields an attack.
144 There are other minor adjustments to what yields an attack
145 and what does not.
146
147 12. "freeze" command reverts to "save", most people will understand this
148 better anyway. (SST2K recognizes both.)
149
150 13. Screen-oriented interface, with sensor scans always up.  (SST2K
151 supports both screen-oriented and TTY modes.)
152
153 Eric Raymond's changes:
154
155 Mainly, I translated this C code out of FORTRAN into C -- created #defines
156 for a lot of magic numbers and refactored the heck out of it.
157
158 1. "sos" and "call" becomes "mayday", "freeze" and "save" are both good.
159
160 2. Status report now indicates when dilithium crystals are on board.
161
162 3. Per Dave Matuszek's remarks, Thingy state is never saved across games.
163
164 4. Added game option selection so you can play a close (but not bug-for-
165 bug identical) approximation of older versions.
166
167 5. Half the quadrants now have inhabited planets, from which one 
168 cannot mine dilithium (there will still be the same additional number
169 of dilithium-bearing planets).  Torpedoing an inhabited world is *bad*.
170 There is BSD-Trek-like logic for Klingons to attack and enslave 
171 inhabited worlds, producing more ships (only is skill is 'good' or 
172 better). (Controlled by OPTION_WORLDS and turned off if game 
173 type is "plain" or "almy".)
174
175 6. User input is now logged so we can do regression testing.
176
177 7. More BSD-Trek features: You can now lose if your entire crew
178 dies in battle.  When abandoning ship in a game with inhabited
179 worlds enabled, they must have one in the quadrant to beam down
180 to; otherwise they die in space and this counts heavily against
181 your score.  Docking at a starbase replenishes your crew.
182
183 8. Still more BSD-Trek: we now have a weighted damage table.  Quoth
184 Eric Allman in the code of BSD-Trek: "Under certain conditions you can
185 get a critical hit.  This sort of hit damages devices.  The
186 probability that a given device is damaged depends on the device.
187 Well protected devices (such as the computer, which is in the core of
188 the ship and has considerable redundancy) almost never get damaged,
189 whereas devices which are exposed (such as the warp engines) or which
190 are particularly delicate (such as the transporter) have a much higher
191 probability of being damaged."
192
193 This is one place where OPTION_PLAIN does not restore the original
194 behavior, which was equiprobable damage across all devices.  If we
195 wanted that, we'd return randrange(NDEVICES) and have done with it.
196 Also, in the original game, DNAVSYS and DCOMPTR were the same device.
197
198 Instead, we use a table of weights similar to the one from BSD Trek.
199 BSD doesn't have the shuttle, shield controller, death ray, or probes.
200 We don't have a cloaking device.  The shuttle got the allocation for
201 the cloaking device, then we shaved a half-percent off everything to
202 have some weight to give DSHCTRL/DDRAY/DDSP.
203
204 Also, the nav subsystem (enabling automatic course
205 setting) can be damaged separately from the main computer (which
206 handles weapons targeting, ETA calculation, and self-destruct).
207
208 After these features were added, I translated this program into Python
209 and added more:
210
211 9. A long-range scan is done silently whenever you call CHART; thus
212 the LRSCAN command is no longer needed.  (Controlled by OPTION_AUTOSCAN
213 and turned off if game type is "plain" or "almy".)
214
215 10. I imported Tom Almy's SCORE, CAPTURE, and CLOAK command from his
216 2013 version.
217
218 11. I added color.
219
220 TESTING:
221
222 This code has been designed to be tested.  A simple shellscript
223 included in the distribution, 'replay', automatically reruns the
224 last game you played.
225
226 See the "test" directory for regression-test logs and checkfiles.
227 Please run 'make check' every time you change this game to be sure
228 you haven't broken anything.  
229
230 When you fix a bug, add the log of the game that triggered it (and
231 a checkfile) to the regression tests. This is how we improve the code
232 coverage of the suite. 
233
234 Two things to do to the log before dropping it in the test directory:
235
236 (a) Strip out all lines beginning with "#curses:" - these are for debugging
237 the curses interface and are not needed for regression testing; removing
238 them makes it easier to see the game commands.
239
240 (b) Add a header comment line explaining what the log is a test for.
241 It should begin with "# Tests". This is what gets echoed before each
242 regression test is run.
243
244 Then drop it into the test directory and "make buildregress" to create
245 the check file. (Don't forget to commit and push the log and checkfile
246 to the repository.)
247
248 NOTES ON THE PYTHON TRANSLATION:
249
250 The Python translation was done with a regexp-based C-to-Python
251 translator I wrote for the purpose (I expect to release this as a
252 separate project).  I then hand-tuned and refactored the result.
253
254 The LOC count dropped by almost exactly 20% during this process, from
255 a bit over 8100 lines to a bit over 6500 lines.  If the code is still
256 shorter than that when you read this, it's because this file contains
257 most of what used to be a huge header comment.  
258
259 SST is not a data-structure- intensive program, so it compresses less
260 under translation to Python than the 50% drop in LOC I've found to be
261 more typical.  The gain in readability, though impossible to quantify,
262 is much greater than the drop in line count would suggest.
263
264 Some parts of the code, such as the finish() and score() functions, 
265 have barely been touched.  Code in the general category of report 
266 generators has tended to change little, especially since we've tried
267 to preserve the look and feel of the original.
268
269 On the other hand, the vector-arithmetic code around navigation and
270 torpedos, and deep-space-probe handling changed a lot.  All that 
271 stuff is now centralized in a 'course' object that hides the
272 trigonometric calculations.  
273
274 The course object builds on a 'coord' object, which I actually had
275 introduced while refactoring the C version.  Large parts of SST2K are,
276 perforce, an exercise in 2D vector arithmetic.  In the original
277 FORTRAN all the vector representation was done with parallel arrays;
278 in C, I introduced a struct; in Python, the class has a complete
279 repertoire of vector-algebra operations.
280
281 There's one weird archeological detail about the nav code that
282 deserves a remark.  This program originally required input in terms of
283 a (clock) direction and distance -- essentially, directions were real
284 numbers modulo 12 with zero being north.  Somewhere in history, it was
285 changed to Cartesian coordinates.  But the bearing method still computes
286 polar coordinates in clockface units -- that's the reason for the
287 wacky constant 1.90985, inherited straight from the ancestral FORTRAN.
288 Elsewhere, there were a bunch of computations, now centralized in the
289 course object, that looked like (15.0 - bearing)*0.5235988; this is a 
290 conversion from clockface units to radians with zero on the X axis.
291
292 As a previous maintainer, probably Tom Almy, observed: Probably
293 "manual" input should still be done this way -- it's a real pain if
294 the computer isn't working! Manual mode is still confusing because it
295 involves giving x and y motions, yet the coordinates are always
296 displayed y - x, where +y is downward!
297
298 Because I think he's arguably right, I haven't ripped out all the
299 clockface-to-radian conversions.  For this reason, and others, the 
300 trig code is still a bit wacky and obscure.  Modify with caution
301 and test thoroughly.
302