Codebase list lbreakout2 / HEAD game / extras.c
HEAD

Tree @HEAD (Download .tar.gz)

extras.c @HEADraw · history · blame

  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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/***************************************************************************
                          extras.c  -  description
                             -------------------
    begin                : Sun Sep 9 2001
    copyright            : (C) 2001 by Michael Speck
    email                : kulkanie@gmx.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "../client/lbreakout.h"
#include "levels.h"
#include "paddle.h"
#include "bricks.h"
#include "balls.h"
#include "mathfuncs.h"
#include "extras.h"

extern int ball_w, ball_dia;
extern Game *cur_game;

/*
====================================================================
Locals
====================================================================
*/

/*
====================================================================
Public
====================================================================
*/

/*
====================================================================
Create new extra at position
====================================================================
*/
Extra *extra_create( int type, int x, int y, int dir )
{
	Extra *e = salloc( 1, sizeof( Extra ) );
	e->type = type;
	e->offset = type * BRICK_WIDTH;
	e->x = x; e->y = y;
	e->dir = dir;
	e->alpha = 0;
	return e;
}

/*
====================================================================
Use extra when paddle collected it
====================================================================
*/
void extra_use( Paddle *paddle, int extra_type )
{
	Ball 	*b;
	int 	i, j;
	int 	tm = cur_game->diff->time_mod; /* time modifier */

	printf("%d\n",tm);

	if ( cur_game->diff->allow_maluses ) {
		while( extra_type == EX_RANDOM )
			extra_type = rand() % (EX_NUMBER);
	} else {
		while ( extra_type == EX_RANDOM || extra_is_malus( extra_type ) )
			extra_type = rand() % (EX_NUMBER);
	}

	/* store modification */
	i = cur_game->paddles[0]==paddle?0:1;
	if ( cur_game->mod.collected_extra_count[i] < MAX_MODS )
		cur_game->mod.collected_extras[i][cur_game->mod.collected_extra_count[i]++] = 
			extra_type;
	/* statistics */
	paddle->extras_collected++;

	switch (extra_type) {
		case EX_SCORE200:
			paddle->score += cur_game->diff->score_mod * 200 / 10;
			break;
		case EX_SCORE500:
			paddle->score += cur_game->diff->score_mod * 500 / 10;
			break;
		case EX_SCORE1000:
			paddle->score += cur_game->diff->score_mod * 1000 / 10;
			break;
		case EX_SCORE2000:
			paddle->score += cur_game->diff->score_mod * 2000 / 10;
			break;
		case EX_SCORE5000:
			paddle->score += cur_game->diff->score_mod * 5000 / 10;
			break;
		case EX_SCORE10000:
			paddle->score += cur_game->diff->score_mod * 10000 / 10;
			break;
		case EX_GOLDSHOWER:
			paddle->extra_time[EX_GOLDSHOWER] += TIME_GOLDSHOWER * tm;
			paddle->extra_active[EX_GOLDSHOWER] = 1;
			break;
		case EX_LIFE:
			/* adding life is handled by client */
			break;
		case EX_SHORTEN:
			paddle_init_resize( paddle, -1);
			break;
		case EX_LENGTHEN:
			paddle_init_resize( paddle, 1);
			break;
		case EX_BALL:
			b = ball_create(
				paddle->x + (paddle->w - ball_w) / 2, 
				paddle->y + ((paddle->type == PADDLE_TOP)?paddle->h:-ball_dia) );
			b->paddle = paddle;
			ball_set_random_angle( b, cur_game->ball_v );
			b->get_target = 1;
			list_add( cur_game->balls, b );
			break;
		case EX_WALL:
			paddle->extra_time[EX_WALL] += TIME_WALL * tm;
			if ( paddle->extra_active[EX_WALL] ) break;
			paddle->extra_active[extra_type] = 1;
			if ( paddle->wall_y == 0 ) {
				for (i = 1; i < MAP_WIDTH - 1; i++) {
					cur_game->bricks[i][0].type = MAP_WALL;
					cur_game->bricks[i][0].id = 0;
				}
			}
			else
				for (i = 1; i < MAP_WIDTH - 1; i++) {
					cur_game->bricks[i][MAP_HEIGHT - 1].type = MAP_WALL;
					cur_game->bricks[i][MAP_HEIGHT - 1].id = 0;
				}
			paddle->wall_alpha = 0;
			balls_check_targets( -1, 0 );
			break;
		case EX_METAL:
			cur_game->extra_time[EX_METAL] += TIME_METAL * tm;
			cur_game->extra_active[extra_type] = 1;
			balls_set_type( BALL_METAL );
			/* other ball extras are disabled */
			if ( cur_game->extra_active[EX_EXPL_BALL] ) {
				cur_game->extra_active[EX_EXPL_BALL] = 0;
				cur_game->extra_time[EX_EXPL_BALL] = 0;
			}
			if ( cur_game->extra_active[EX_WEAK_BALL] ) {
				cur_game->extra_active[EX_WEAK_BALL] = 0;
				cur_game->extra_time[EX_WEAK_BALL] = 0;
			}
			break;
		case EX_FROZEN:
			paddle->extra_time[EX_FROZEN] = TIME_FROZEN * tm;
			paddle->extra_active[extra_type] = 1;
			paddle_freeze( paddle, 1 );
			break;
		case EX_WEAPON:
			paddle->extra_time[EX_WEAPON] += TIME_WEAPON * tm;
			paddle->extra_active[extra_type] = 1;
			weapon_install( paddle, 1 );
			break;
		case EX_SLIME:
			paddle->extra_time[EX_SLIME] += TIME_SLIME * tm;
			paddle->extra_active[extra_type] = 1;
			paddle_set_slime( paddle, 1 );
			break;
		case EX_FAST:
			if ( cur_game->extra_active[EX_SLOW] ) {
				cur_game->extra_time[EX_SLOW] = 0;
				cur_game->extra_active[EX_SLOW] = 0;
			}
			cur_game->extra_time[EX_FAST] += TIME_FAST * tm;
			cur_game->extra_active[extra_type] = 1;
                        cur_game->ball_v = cur_game->ball_v_max;
                        balls_set_velocity( cur_game->balls, cur_game->ball_v );
			break;
		case EX_SLOW:
			if ( cur_game->extra_active[EX_FAST] ) {
				cur_game->extra_time[EX_FAST] = 0;
				cur_game->extra_active[EX_FAST] = 0;
			}
			cur_game->extra_time[EX_SLOW] += TIME_SLOW * tm;
			cur_game->extra_active[extra_type] = 1;
			cur_game->ball_v = cur_game->ball_v_min;
                        balls_set_velocity( cur_game->balls, cur_game->ball_v );
			break;
		case EX_CHAOS:
			cur_game->extra_time[EX_CHAOS] += TIME_CHAOS * tm;
			cur_game->extra_active[extra_type] = 1;
			balls_set_chaos( 1 );
			break;
		case EX_DARKNESS:
			cur_game->extra_time[EX_DARKNESS] += TIME_DARKNESS * tm;
			cur_game->extra_active[extra_type] = 1;
			break;
		case EX_GHOST_PADDLE:
			paddle->extra_time[EX_GHOST_PADDLE] += TIME_GHOST_PADDLE * tm;
			paddle->extra_active[extra_type] = 1;
			paddle_set_invis( paddle, 1 );
			break;
		case EX_TIME_ADD:
			for ( i = 0; i < EX_NUMBER; i++ )
				if ( cur_game->extra_time[i] )
					cur_game->extra_time[i] += 7000 * tm;
			for ( i = 0; i < EX_NUMBER; i++ ) {
				for ( j = 0; j < cur_game->paddle_count; j++ )
					if ( cur_game->paddles[j]->extra_time[i] )
						cur_game->paddles[j]->extra_time[i] += 7000 * tm;
			}
			break;
		case EX_EXPL_BALL:
			balls_set_type( BALL_EXPL );
			cur_game->extra_time[EX_EXPL_BALL] += TIME_EXPL_BALL * tm;
			cur_game->extra_active[extra_type] = 1;
			/* other ball extras are disabled */
			if ( cur_game->extra_active[EX_METAL] ) {
				cur_game->extra_active[EX_METAL] = 0;
				cur_game->extra_time[EX_METAL] = 0;
			}
			if ( cur_game->extra_active[EX_WEAK_BALL] ) {
				cur_game->extra_active[EX_WEAK_BALL] = 0;
				cur_game->extra_time[EX_WEAK_BALL] = 0;
			}
			break;
		case EX_WEAK_BALL:
			balls_set_type( BALL_WEAK );
			cur_game->extra_time[EX_WEAK_BALL] += TIME_WEAK_BALL * tm;
			cur_game->extra_active[extra_type] = 1;
			/* other ball extras are disabled */
			if ( cur_game->extra_active[EX_METAL] ) {
				cur_game->extra_active[EX_METAL] = 0;
				cur_game->extra_time[EX_METAL] = 0;
			}
			if ( cur_game->extra_active[EX_EXPL_BALL] ) {
				cur_game->extra_active[EX_EXPL_BALL] = 0;
				cur_game->extra_time[EX_EXPL_BALL] = 0;
			}
			break;
		case EX_BONUS_MAGNET:
			paddle_set_attract( paddle, ATTRACT_BONUS );
			paddle->extra_time[EX_BONUS_MAGNET] += TIME_BONUS_MAGNET * tm;
			paddle->extra_active[extra_type] = 1;
			if ( paddle->extra_active[EX_MALUS_MAGNET] ) {
				paddle->extra_active[EX_MALUS_MAGNET] = 0;
				paddle->extra_time[EX_MALUS_MAGNET] = 0;
			}
			break;
		case EX_MALUS_MAGNET:
			paddle_set_attract( paddle, ATTRACT_MALUS );
			paddle->extra_time[EX_MALUS_MAGNET] += TIME_MALUS_MAGNET * tm;
			paddle->extra_active[extra_type] = 1;
			if ( paddle->extra_active[EX_BONUS_MAGNET] ) {
				paddle->extra_active[EX_BONUS_MAGNET] = 0;
				paddle->extra_time[EX_BONUS_MAGNET] = 0;
			}
			break;
		case EX_DISABLE:
			/* set all active extra times to 1 so they will expire next
			   prog cycle */ 
			for ( i = 0; i < EX_NUMBER; i++ )
				if ( cur_game->extra_time[i] )
					cur_game->extra_time[i] = 1;
			for ( i = 0; i < EX_NUMBER; i++ ) {
				for ( j = 0; j < cur_game->paddle_count; j++ )
					if ( cur_game->paddles[j]->extra_time[i] )
						cur_game->paddles[j]->extra_time[i] = 1;
			}
			break;
		default:
			/* it wasn't used so delete mod */
			i = cur_game->paddles[0]==paddle?0:1;
			cur_game->mod.collected_extra_count[i]--;
			break;
			
	}
}
/*
====================================================================
Update extras
====================================================================
*/
void extras_update( int ms )
{
	Extra       *ex;
	int i, j;
	int magnets;
	Paddle *magnet;

	/* check extra_time of limited extras */

	/* general extras */
	for ( i = 0; i < EX_NUMBER; i++ )
		if ( cur_game->extra_time[i] )
		if ( (cur_game->extra_time[i] -= ms) <= 0 ) {
			cur_game->extra_time[i] = 0;
			/* expired */
			switch ( i ) {
				case EX_EXPL_BALL:
				case EX_WEAK_BALL:
				case EX_METAL: 
					balls_set_type( BALL_NORMAL ); 
					break;
				case EX_SLOW:
				case EX_FAST:
					cur_game->ball_v = cur_game->diff->v_start + 
						cur_game->diff->v_add * cur_game->speedup_level;
                                        balls_set_velocity( cur_game->balls, cur_game->ball_v );
					break;
				case EX_CHAOS:
					balls_set_chaos( 0 );
					break;
			}
			/* set deactivated */
			cur_game->extra_active[i] = 0; 
		}

	/* paddlized extras */
	for ( j = 0; j < cur_game->paddle_count; j++ )
	for ( i = 0; i < EX_NUMBER; i++ )
		/* extra_time of wall is updated in wall_update() */
		if ( cur_game->paddles[j]->extra_time[i] && i != EX_WALL )
		if ( (cur_game->paddles[j]->extra_time[i] -= ms) <= 0 ) {
			cur_game->paddles[j]->extra_time[i] = 0;
			/* expired */
			switch ( i ) {
				case EX_SLIME:
				        paddle_set_slime( cur_game->paddles[j], 0 );
					/* release all balls from paddle */
					balls_detach_from_paddle( cur_game->paddles[j],
								  ((rand()%2==1)?-1:1) );
					break;
				case EX_WEAPON: weapon_install( cur_game->paddles[j], 0 ); break;
				case EX_FROZEN:
					paddle_freeze( cur_game->paddles[j], 0 );
					break;
				case EX_GHOST_PADDLE:
					paddle_set_invis( cur_game->paddles[j], 0 );
					break;
				case EX_BONUS_MAGNET:
				case EX_MALUS_MAGNET:
					paddle_set_attract( cur_game->paddles[j], ATTRACT_NONE );
					break;
			}
			/* set deactivated */
			cur_game->paddles[j]->extra_active[i] = 0; /* wall is handled in wall_...() */
		}

	/* move extras and check if paddle was hit */
	list_reset( cur_game->extras );
	while ( ( ex = list_next( cur_game->extras ) ) ) {
		/* if only one paddle has a magnet active all extras will 
		 * be attracted by this paddle else the extras 'dir' is used 
		 */
		magnets = 0; magnet = 0;
		for ( i = 0; i < cur_game->paddle_count; i++ )
			if ( paddle_check_attract( cur_game->paddles[i], ex->type ) ) {
				magnets++;
				magnet = cur_game->paddles[i]; /* last magnet */
			}
		if ( magnets != 1 ) {
			/* either no or more than one magnet so use default */
			if ( ex->dir > 0 )
				ex->y += 0.05 * ms;
			else
				ex->y -= 0.05 * ms;
		}
		else {
			/* 'magnet' is the paddle that will attract this extra */
			if ( magnet->type == PADDLE_TOP )
				ex->y -= 0.05 * ms;
			else
				ex->y += 0.05 * ms;
			if ( ex->x + ( BRICK_WIDTH >> 1 ) < magnet->x + ( magnet->w >> 1 ) ) {
				ex->x += 0.05 * ms;
				if ( ex->x + ( BRICK_WIDTH >> 1 ) > magnet->x + ( magnet->w >> 1 ) )
					ex->x = magnet->x + ( magnet->w >> 1 ) - ( BRICK_WIDTH >> 1 );
			}
			else {
				ex->x -= 0.05 * ms;
				if ( ex->x + ( BRICK_WIDTH >> 1 ) < magnet->x + ( magnet->w >> 1 ) )
					ex->x = magnet->x + ( magnet->w >> 1 ) - ( BRICK_WIDTH >> 1 );
			}
		}
		/* if out of screen, kill this extra */
		if ( ex->y >= 480 || ex->y + BRICK_HEIGHT < 0 ) {
			list_delete_current( cur_game->extras );
			continue;
		}
		for ( j = 0; j < cur_game->paddle_count; j++ ) {
			/* contact with paddle core ? */
			if ( paddle_solid( cur_game->paddles[j] ) )
			if ( ex->x + BRICK_WIDTH > cur_game->paddles[j]->x )
			if ( ex->x < cur_game->paddles[j]->x + cur_game->paddles[j]->w - 1 )
			if ( ex->y + BRICK_HEIGHT > cur_game->paddles[j]->y )
			if ( ex->y < cur_game->paddles[j]->y + cur_game->paddles[j]->h ) {
				/* any extra except EX_JOKER is simply used */
				if ( ex->type != EX_JOKER ) {
					extra_use( cur_game->paddles[j], ex->type );
					list_delete_current( cur_game->extras );
					break;
				}
				/* use EX_JOKER and work through all active extras */
				/* the mod is only stored to play the sound */
				if ( cur_game->mod.collected_extra_count[j] < MAX_MODS )
					cur_game->mod.collected_extras[j][cur_game->mod.collected_extra_count[j]++] = EX_JOKER;
				list_reset( cur_game->extras );
				while ( ( ex = list_next( cur_game->extras ) ) ) {
					if ( ex->type != EX_JOKER )
					if ( ex->type != EX_SHORTEN )
					if ( ex->type != EX_FROZEN )
					if ( ex->type != EX_FAST )
					if ( ex->type != EX_RANDOM )
					if ( ex->type != EX_DARKNESS )
					if ( ex->type != EX_GHOST_PADDLE )
					if ( ex->type != EX_CHAOS )
					if ( ex->type != EX_DISABLE )
					if ( ex->type != EX_MALUS_MAGNET )
					if ( ex->type != EX_WEAK_BALL ) {
						extra_use( cur_game->paddles[j], ex->type );
						extra_use( cur_game->paddles[j], ex->type );
					}
					list_delete_current( cur_game->extras );
				}
				break;
			}
		}
	}
}

/* wall */
void walls_update( int ms )
{
	int i, j;

	for ( j = 0; j < cur_game->paddle_count; j++ )
		if ( cur_game->paddles[j]->extra_active[EX_WALL] ) {
			if ( cur_game->paddles[j]->extra_time[EX_WALL] > 0 ) {
				if ( (cur_game->paddles[j]->extra_time[EX_WALL] -= ms) < 0 )
					cur_game->paddles[j]->extra_time[EX_WALL] = 0;
				/* still appearing? */
				if (cur_game->paddles[j]->wall_alpha < 255)
				if ( (cur_game->paddles[j]->wall_alpha += 0.25 * ms) > 255 ) 
					cur_game->paddles[j]->wall_alpha = 255;
			}
			else
			if ( (cur_game->paddles[j]->wall_alpha -= 0.25 * ms) < 0 ) {
				cur_game->paddles[j]->wall_alpha = 0;
				cur_game->paddles[j]->extra_active[EX_WALL] = 0;
				if ( cur_game->paddles[j]->wall_y == 0 )
					for (i = 1; i < MAP_WIDTH - 1; i++) 
						cur_game->bricks[i][0].type = MAP_EMPTY;
				else
					for (i = 1; i < MAP_WIDTH - 1; i++) 
						cur_game->bricks[i][MAP_HEIGHT - 1].type = MAP_EMPTY;
				balls_check_targets( -1, 0 );
			}
		}
}

int extra_is_malus( int type )
{
	if ( type == EX_SHORTEN ) return 1;
	if ( type == EX_FROZEN ) return 1;
	if ( type == EX_FAST ) return 1;
	if ( type == EX_DARKNESS ) return 1;
	if ( type == EX_GHOST_PADDLE ) return 1;
	if ( type == EX_CHAOS ) return 1;
	if ( type == EX_DISABLE ) return 1;
	if ( type == EX_MALUS_MAGNET ) return 1;
	if ( type == EX_WEAK_BALL ) return 1;
	return 0;
}