1 /*
2 	swend	 -	SW end of game
3 
4 	Copyright (C) 1984-2003 David L. Clark.
5 	This program is free software; you can redistribute it and/or modify it under
6 	the terms of the GNU General Public License as published by the Free Software
7 	Foundation; either version 2 of the License, or (at your option) any later
8 	version. This program is distributed in the hope that it will be useful,
9 	but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 	more details. You should have received a copy of the GNU General Public
12 	License along with this program; if not, write to the Free Software Foundation,
13 	Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
14 
15 			Author: Dave Clark
16 
17 	Modification History:
18 			84-02-02	Development
19 			87-03-09	Microsoft compiler.
20 			87-03-12	Wounded airplanes.
21 			2003-01-27	GNU General Public License
22 */
23 #include	"sw.h"
24 
25 
26 
27 
28 extern	int	playmode;		/* Mode of play ( SINGLE, MULTIPLE, */
29 					/*		  or COMPUTER )     */
30 extern	int	savemode;		/* Saved PC video mode		    */
31 extern	BOOL	hires;			/* High res debug/mono flag	    */
32 extern	OBJECTS *objtop;		/*  Start of object list.	    */
33 extern	MULTIO	*multbuff;		/*  Communications buffer	    */
34 extern	int	player;
35 extern	int	endsts[];		/* End of game status and move count*/
36 extern	int	endcount;
37 extern	BOOL	goingsun;		/* Heading for the sun flag	    */
38 extern	OBJECTS *objsmax;		/* Maximum object allocated	    */
39 extern	BOOL	repflag;		/* Report statictics flag	    */
40 extern	BOOL	inplay; 		/*  Currently playing flag	    */
41 extern	int	maxcrash;		/* Maximum number of crashes	    */
42 
43 
44 
45 swend( msg, update )
46 char	*msg;
47 BOOL	update;
48 {
49 register char	*closmsg = NULL;
50 char		*multclos(), *asynclos();
51 
52 
53 	set_type( savemode );
54 	hires = FALSE;
55 
56 	sound( 0, 0, NULL );
57 	swsound();
58 
59 	if ( repflag )
60 		swreport();
61 
62 	if ( playmode == MULTIPLE )
63 		closmsg = multclos( update );
64 	else if ( playmode == ASYNCH )
65 		closmsg = asynclos();
66 
67 	intsoff();
68 	_intterm();
69 	intson();
70 	histend();
71 
72 	puts( "\r\n" );
73 	if ( closmsg ) {
74 		puts( closmsg );
75 		puts( "\r\n" );
76 	}
77 	if ( msg ) {
78 		puts( msg );
79 		puts( "\r\n" );
80 	}
81 
82 	inplay = FALSE;
83 	swflush();
84 	if ( msg || closmsg )
85 		exit( YES );
86 	else
87 		exit( NO );
88 }
89 
90 
91 
92 
93 
94 endgame( targclr )
95 int	targclr;
96 {
97 register int	 winclr;
98 register OBJECTS *ob;
99 
100 	if ( ( ( playmode != MULTIPLE ) && ( playmode != ASYNCH ) )
101 		|| ( multbuff->mu_maxplyr == 1 ) )
102 		winclr = 1;
103 	else
104 		if ( ( objtop +1 )->ob_score == objtop->ob_score )
105 			winclr = 3 - targclr;
106 		else
107 			winclr = ( ( objtop + 1 )->ob_score > objtop->ob_score )
108 				 + 1;
109 
110 	ob = objtop;
111 	while ( ob->ob_type == PLANE ) {
112 		if ( !endsts[ob->ob_index] )
113 			if ( ( ob->ob_clr == winclr )
114 				  && ( ( ob->ob_crashcnt < ( MAXCRASH - 1 ) )
115 			       || ( ( ob->ob_crashcnt < MAXCRASH )
116 				  && ( ( ob->ob_state == FLYING )
117 				    || ( ob->ob_state == STALLED )
118 				    || ( ob->ob_state == WOUNDED )
119 				    || ( ob->ob_state == WOUNDSTALL )
120 				) ) ) )
121 				winner( ob );
122 			else
123 				loser( ob );
124 		ob = ob->ob_next;
125 	}
126 }
127 
128 
129 
130 
131 
132 winner( obp )
133 OBJECTS *obp;
134 {
135 register int	 n;
136 register OBJECTS *ob;
137 
138 	endsts[n = ( ob = obp )->ob_index] = WINNER;
139 	if ( n == player ) {
140 		endcount = 72;
141 		goingsun = TRUE;
142 		ob->ob_dx = ob->ob_dy = ob->ob_ldx = ob->ob_ldy = 0;
143 		ob->ob_state = FLYING;
144 		ob->ob_life = MAXFUEL;
145 		ob->ob_speed = MIN_SPEED;
146 	}
147 }
148 
149 
150 
151 
152 loser( ob )
153 OBJECTS *ob;
154 {
155 register int	 n;
156 
157 	endsts[n = ob->ob_index] = LOSER;
158 	if ( n == player ) {
159 		swcolour( 0x82 );
160 		swposcur( 16, 12 );
161 		puts( "THE END" );
162 		endcount = 20;
163 	}
164 }
165 
166 
167 
168 
169 swreport()
170 {
171 	puts( "\r\nEnd of game statictics\r\n\r\n" );
172 	puts( "Objects used: " );
173 	dispd( ( (int) objsmax - (int) objtop + 1 ) / sizeof( OBJECTS ) );
174 	puts( "\r\n" );
175 }
176 