Blame view

games/ninvaders/aliens.c 8.66 KB
8e1b403a   pifou   ajout des sources...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  /**
   * nInvaders - a space invaders clone for ncurses
   * Copyright (C) 2002-2003 Dettus
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   * homepage: http://ninvaders.sourceforge.net
   * mailto: ninvaders-devel@lists.sourceforge.net
   *
   */
  
   
  #include "aliens.h"
  #include "player.h"
  #include "nInvaders.h"
  
  /**
   * initialize aliens attributes
   */
  void aliensReset()
  {
  	int i,j;
  	
  	// three different types of aliens [5], [10]
  	int level[ALIENS_MAX_NUMBER_Y][ALIENS_MAX_NUMBER_X]={
  		{1,1,1,1,1,1,1,1,1,1},
  		{2,2,2,2,2,2,2,2,2,2},
  		{2,2,2,2,2,2,2,2,2,2},
  		{3,3,3,3,3,3,3,3,3,3},
  		{3,3,3,3,3,3,3,3,3,3}
  	};
  
  	aliensClear(aliens.posX, aliens.posY, aliens.right, aliens.bottom);	// clear old position of aliens
  	
  	// reset alien position
  	aliens.posX = 0;
  	aliens.posY = 0;
  	aliens.right = 0;
  	aliens.bottom = 0;
  	aliens.left = 0;
  	aliens.speed = 1;
  	
  	// copy level-array to enemy-array 
  	for (i=0;i<ALIENS_MAX_NUMBER_X;i++) {
  		for (j=0;j<ALIENS_MAX_NUMBER_Y;j++) {
  			alienBlock[j][i]=level[j][i];
  		}
  	}
  	
  	// reset missiles
  	for (i = 0; i < ALIENS_MAX_MISSILES; i++) {
  		if (alienshotx[i] != 0) {
  			aliensMissileClear(alienshotx[i],alienshoty[i]);	// clear old position
  		}
  		alienshotx[i] = 0;  // start with zero values
  		alienshoty[i] = 0;  // start with zero values
  	}
  	alienshotnum = 1;	    // one missile at the same time
  	alienshotx[0] = 5;	    // x position of first alienshot
  	alienshoty[0] = 1;	    // y position of first alienshot
  
  }
  
  /**
   * initialize bunkers attributes
   */
  void bunkersReset()
  {
  	int a, b;
  
  	// set position of bunker sprites. user graphical char bunkerd for better visual overview
  	// and set according to this the bunker-array
  	char bunkerd[BUNKERHEIGHT][BUNKERWIDTH+1] = {
  		"        ###                 ###                 ###                 ###         ",
  		"       #####               #####               #####               #####        ",
  		"      #######             #######             #######             #######       ",
  		"      ##   ##             ##   ##             ##   ##             ##   ##       "
  	};
  	//       12345678901234567890123456789012345678901234567890123456789012345678901234567890
  	// 80 characters wide
  
  	// copy graphical "bunkerd" to binary "bunker"
  	for (a = 0; a < BUNKERWIDTH; a++) {
  		for (b = 0; b < BUNKERHEIGHT; b++) {
  			if (bunkerd[b][a] == '#')
  				bunker[b][a] = 1;
  			else
  				bunker[b][a] = 0;
  		}
  	}
  	
  	// display bunkers sprite
  	bunkersDisplay(&bunker[0][0]);	
  }
  
  /**
   * move aliens and test if they've reached the
   * bottom of the windows or the bunkers.
   */
  int aliensMove()
  {
  	
  	int cx,cy;
  	int fReachedPlayer=0; 				// return value
  
  	render();	
  	aliensClear(aliens.posX, aliens.posY, aliens.right, aliens.bottom);	// clear old position of aliens
  
  	aliens.posX = aliens.posX + aliens.speed;			// move aliens left/ right
  	
  	// when aliens reached left or right screen-border
  	if (aliens.posX == (BUNKERWIDTH + BUNKERX - 5 - aliens.right) || aliens.posX == (BUNKERX + aliens.left)) {
  		
  		aliens.posY++;				// move aliens downwards
  		
  		// aliens reached player
  		if (aliens.posY == SCREENHEIGHT - 2 - aliens.bottom) {
  			fReachedPlayer = 1;		// set return value
  		}
  		
  		// aliens reached bunkers //funzt nicht ganz: todo
  		if (aliens.posY == BUNKERY - aliens.bottom) {
  			// clear bunkers
  			for(cx=0;cx<BUNKERWIDTH;cx++) {
  				for(cy=0;cy<BUNKERHEIGHT;cy++) { 
  					bunker[cy][cx]=0;	
  				}
  			}
  			bunkersClear();	// clear bunkers sprites
  		}
  		
  		aliens.speed = aliens.speed * (-1);		  // change direction of aliens' movements
  	}
  
  	aliensDisplay(aliens.posX, aliens.posY, aliens.right, aliens.bottom); // display aliens at new position
  	
  	return fReachedPlayer;				  // return if aliens reached player
  }
  
  
  /**
   * display alien animation, display remaining parts of aliens and bunker 
   */
  void render()
  {
  	int k,row;
  	int c=0;
  
  	// calculate left, right, bottom, lowest_ship	
  	aliens.left=1;
  	aliens.right=-1;
  	aliens.bottom=-1;
  	shipnum=0;
  	for (k=0;k<ALIENS_MAX_NUMBER_X;k++) {
  		lowest_ship[k]=-1;
  	}
  	
  	for (row=0;row<ALIENS_MAX_NUMBER_Y*2;row++) {
  		if ((row%2)==0){
  			for (k=0;k<ALIENS_MAX_NUMBER_X;k++) {
  				if (alienBlock[c][k] != 0) {
  					lowest_ship[k]=row;
  					shipnum++;
  					if (aliens.left==1 || -k>aliens.left) {aliens.left=-k;}
  					if (aliens.right==-1 || k>aliens.right) {aliens.right=k;}
  					if (aliens.bottom==-1 || c>aliens.bottom) {aliens.bottom=c;}
  				} 
  			}
  		} else {
  			c++;
  		}
  	}
  	aliens.bottom=aliens.bottom*2;	// every 2nd row is an empty row
  	aliens.left=aliens.left*3; // alien sprite is 3 chars wide
  	aliens.right=aliens.right*3; // alien sprite is 3 chars wide
  	
  	// display remaining aliens with animation
  	aliensRefresh(level, &alienBlock[0][0]);
  
  }
  
  
  /**
   * move aliens' missiles and do player/bunker hit testing
   * a zero value in alienshotx indicates that the appropriate missile is loaded, but not fired
   */
  int aliensMissileMove()
  {
  	int i, tmp;
  	int fPlayerWasHit = 0;
  	int shootThreshold;
  	static int alienshot_counter = 0;
  
  	
  	// calculate threshold when next missile should be fired
  	// it is done here to save calculation time in for-instruction 
  	shootThreshold = (skill_level * 8) * (shipnum + 2);
  	alienshot_counter = alienshot_counter + 10 ;
  	
  	// loop all possible missiles
  	for (i = 0; i < ALIENS_MAX_MISSILES; i++) {
  		
  		// if the current missile is flying we should do movements
  		if (alienshotx[i] != 0) {
  			
  			aliensMissileClear(alienshotx[i],alienshoty[i]);	// clear old position
  				
  			// if missile hit the bunkers	
  			if (bunkersHitCheck(alienshotx[i], alienshoty[i]) == 1) {
  				alienshotx[i] = 0;		// value of zero reloads missile
  			}
  			
  			alienshoty[i]++;			// move missile downwards
  			
  			// check if player was hit by an alien missile
  			if (playerHitCheck(alienshotx[i], alienshoty[i]) == 1) {
  				alienshotx[i] = 0;		// value of zero reloads missile
  				fPlayerWasHit = 1;
  			}
  			
  			
  		} else {					// missile not launched yet
  			
  			// start new missile if counter says so
  			if (alienshot_counter > shootThreshold && shipnum > 0) {// only shot if there's an alien left
  				alienshot_counter = 0;				// reset counter				
  				tmp = random() % ALIENS_MAX_NUMBER_X;  		// randomly select one of the ...
  				while (lowest_ship[tmp] == -1) {		// ...aliens at the bottom of ...
  					tmp = random() % ALIENS_MAX_NUMBER_X;	// ...a column to launch missile
  				}
  				alienshoty[i]=aliens.posY+lowest_ship[tmp];		// set y position of missile
  				alienshotx[i]=aliens.posX+tmp*3;			// set x position of missile
  			}
  		} // if 
  		
  		// display missiles if still running or just launched; could have been modified in the above code
  		if (alienshotx[i] != 0) {
  			// if missile is out of battlefield 
  			if (alienshoty[i] == SCREENHEIGHT - 1) {
  				alienshotx[i] = 0;	// reload missile	
  			} else {
  				aliensMissileDisplay(alienshotx[i], alienshoty[i]); // display Missile at new position
  			}
  		}		
  		
  	} // for
  
  
  	return fPlayerWasHit;
  }
  
  
  
  /**
   * check if missile hit an alien
   */
  int aliensHitCheck(int shotx, int shoty)
  {
  	int alienType = 0;
  	int shipx, shipy;
  	// if missile is within alien-rectangle 
  	if (shotx >= aliens.posX && shotx <= aliens.posX + ALIENS_MAX_NUMBER_X * 3 - 1
  	    && shoty >= aliens.posY && shoty <= aliens.posY + (ALIENS_MAX_NUMBER_Y - 1) * 2) {
  		// calculate the ship that was hit
  		shipx = (shotx - aliens.posX) / 3;
  		shipy = (shoty - aliens.posY) / 2;
  		// if there is still a ship at this position
  		alienType = alienBlock[shipy][shipx];
  		if (alienType != 0) {
  			alienBlock[shipy][shipx] = 0;	// delete alien ship
  		}
  	}
  	return alienType; 	// returns 0 if no alien was hit, else returns type-code of alien
  }
  
  /**
   * check if missile hit an element of bunker
   */
  int bunkersHitCheck(int shotx, int shoty)
  {
  	int adjx, adjy;
  	int fBunkerWasHit = 0;
  	// if missile is within bunker-rectangle
  	if (shotx >= BUNKERX && shotx < BUNKERX + BUNKERWIDTH
  	    && shoty >= BUNKERY && shoty < BUNKERY + BUNKERHEIGHT) {
  		// calculate the element of the bunker that was hit
  		adjy = shoty - BUNKERY; 
  		adjx = shotx - BUNKERX;
  		// if there is still an element
  		if(bunker[adjy][adjx] == 1){
  			bunker[adjy][adjx] = 0;	// delete element
  			fBunkerWasHit = 1; 		// bunker was hit!
  		}
  	}
  	return fBunkerWasHit;
  }