Decoded: Sopwith (1984) by David L. Clark
Source file: SWEND.C
Beginner friendly, line-by-line code walkthrough by MaiZure

SWEND.C contains procedures for handling the end of a game, win or lose.

Original code:
http://davidlclark.com/page/sopwith-source-code

Original code with line numbers
http://www.maizure.org/projects/decoded-sopwith/SWEND_linenum.txt

1     COMMENT
2     COMMENT
3     BLANK
4     COMMENT
5     COMMENT
6     COMMENT
7     COMMENT
8     COMMENT
9     COMMENT
10    COMMENT
11    COMMENT
12    COMMENT
13    COMMENT
14    BLANK
15    COMMENT
16    BLANK
17    COMMENT
18    COMMENT
19    COMMENT
20    COMMENT
21    COMMENT
22    COMMENT
23    Include the main game header file (sw.h)
24    BLANK
25    BLANK
26    BLANK
27    BLANK
28    Import the playmode, declared in SWMAIN and initialized in SWINIT
29    COMMENT
30    Imports the previously stored video mode determined at init time
31    Imports the high resolution flag
32    Imports the object list
33    Imports the muliplayer buffer
34    Imports the player object index number
35    Imports the object ending status array
36    Imports the end sequence timer
37    Imports the flag for the victory sequence
38    Imports the object pointer to the end of the object list
39    Imports the debug report output flag
40    Imports the flag for being in the game play
41    Imports the maximum number of lives a player has
42    BLANK
43    BLANK
44    BLANK

PROCEDURE FOR ENDING THE GAME OUTSIDE OF GAME PLAY
45    Declares and defines swend with 2 Arguments
46    Argument 1 is a message to show at the end, usually an error message
47    Argument 2 is a flag for updating the multiplayer game status
48    BLOCK START - swend, procedure for ending the game gracefully
49    Declares a strings and initializes it to NULL
50    Declares two local function pointers
51    BLANK
52    BLANK
53    Sets the screen mode type to the previously stored mode (text/console)
54    Clears the high res graphics flags
55    BLANK
56    Clears sound parameteres
57    Invokes a sound operation, which stops all sound in this case
58    BLANK
59    Checks if the debug report flag is set (Never true in this build)
60    Prints the debug report
61    BLANK
62    If we're in network multiplayer...
63    Close the connection with updates and save the returned message
64    If we're in async multiplayer...
65    Close the network connection and save the returned message
66    BLANK
67    Disable interrupts
68    Restore DOS interrupt handler that we hijacked at initialization
69    Enable interrupts
70    Stop logging user input
71    BLANK
72    Move console cursor to a new line
73    If there was a closing message from the network close..
74    Print the message
75    Move cursor to a new line
76    BLOCK END - closing message
77    If there was an error message...
78    Print the error message
79    Move cursor to a new line
80    BLOCK START - error message
81    BLANK
82    Set the gameplay flag to false
83    Clear the keyboard buffer
84    If there was any message during shutdown...
85    Exit with an error value
86    Otherwise...
87    Exit 0
88    BLOCK END - swend, procedure for ending the game gracefully
89    BLANK
90    BLANK
91    BLANK
92    BLANK
93    BLANK

PROCEDURE FOR ENDING GAME DURING GAME PLAY
94    Declare and define endgame with 1 Argument
95    Argument 1 is the color of the object that triggered end game
96    BLOCK START - endgame, performs the end game sequence
97    Declares a local variable for the winning team color
98    Declares a local object pointer
99    BLANK
100   If we're not in multiplayer...
101   ...or any other situation with more than 1 player
102   Then the only winning color could be player 1
103   Otherwise we need to determine the winner...
104   If the scores of the top two players are equal...
105   Then the winner must be the other play who triggered this procedure (because
      this target just got destroyed)
106   Otherwise...
107   If player 2 score is greater than player 1, then the winner is true +
      1 
108   Otherwise, it's false + 1. What a confounding check, dave!
109   BLANK
110   Point to the top object (player 1 plane)
111   BLOCK START - Checking all planes
112   If the plane's status hasn't been determined yet...
113   If it's color matches the winning color...
114   And it still has a life left...
115   Or it's on it's last life...
116   And it's still flying...
117   Or sort of flying...
118   ...but may be damaged...
119   ...and NOT DEAD YET...
120   ......got all that?
121   Then this guy must be the winner, no doubt!
122   Otherwise...
123   This guy lost.
124   Go to the next plane
125   BLOCK END - Checking all planes
126   BLOCK END - endgame
127   BLANK
128   BLANK
129   BLANK
130   BLANK
131   BLANK

PERFORM WINNING END GAME SEQUENCE
132   Declare and define winner with 1 argument
133   Argument 1 is a pointer to the winning plane
134   BLOCK START - winner, the winner flies in to the sunset
135   Declare a local integer
136   Declare a local pointer to the winner
137   BLANK
138   Set the winning object's end game status to winner and save its index
139   If the winner is the current player of this console session...
140   Set the end game counter to 72 ticks
141   Set the winning sequence flag to true
142   Set the plane's motion variables to o
143   Set the plane's state to regular flying (could have been stalled)
144   Set the fuel to max
145   St the speed to min
146   End case for current player
147   BLOCK END - winner, player is now flying in to the sunset automatically
148   BLANK
149   BLANK
150   BLANK
151   BLANK

PERFORM LOSING END GAME SEQUENCE
152   Declare and define the loser with 1 argument
153   Argument 1 is a pointer to the losing plane
154   BLOCK START - loser, the end game sequence for the loser
155   Declare a local integer
156   BLANK
157   Save the current object's index and set its end status to loser
158   If the current player is the loser...
159   Set the color to magenta with the 7th bit aserted (it is ignored)
160   Move the cursor to the middle of the screen
161   Print THE END
162   Set the end game timer to 20 ticks
163   End case for player loser
164   BLOCK END - loser
165   BLANK
166   BLANK
167   BLANK
168   BLANK

PRINT DEBUG REPORT
169   Declare and define swreport with zero arguments
170   BLOCK START - swreport, print a debug report on exit
171   Print a new line and a banner with 2 new lines
172   Print "Objects Used: "
173   Print a count of objects based on max list size and position of top
174   Print a new line
175   BLOCK END - swreport
176   EOF