summaryrefslogtreecommitdiffstats
path: root/bullets.c
blob: 3eac94c089ee9e9f0edf800b26a70646a26f73a2 (plain)
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
/*
 * bullets.c
 *
 * Handle bullets (shots and clusterbombs) in flight
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "graphics.h"
#include "grv.h"

struct bullet {
  int x, y;			/* Current location */
  int dx, dy;			/* Direction of movement */
  int color;			/* Color */
};

#define MAX_BULLETS 57		/* 8 clusterbombs*7 shards + 1 player shot */
/* Two lists; flip between loops */
static struct bullet bullet_lists[2][MAX_BULLETS];
static struct bullet *bullets = bullet_lists[0];
static int bullet_list = 0;
static int nbullets = 0;

/*
 * Add a bullet to the list
 */
void add_bullet(int x, int y, int dx, int dy, int color)
{
  int n;

  if ( nbullets >= MAX_BULLETS )
    return;

  n = nbullets++;

  bullets[n].x     = x;
  bullets[n].y     = y;
  bullets[n].dx    = dx;
  bullets[n].dy    = dy;
  bullets[n].color = color;
}

/*
 * Run bullet action
 */
void run_bullets(void)
{
  struct bullet *b, *bb, *eb;
  int x, y, s, f;
  int live;
  int hpp;

  while ( nbullets ) {
    bb = bullets;
    eb = &bullets[nbullets];

    /* Flip lists and initialize to zero */
    bullet_list ^= 1;
    bullets = bullet_lists[bullet_list];
    nbullets = 0;

    for ( b = bb ; b < eb ; b++ ) {
      live = 1;

      x = (b->x += b->dx);
      y = (b->y += b->dy);

      if ( x < 2 || x > 23 || y < 1 || y > 40 ) {
	live = 0;
	b->color = 0;		/* Don't draw */
      } else {
	s = screen(x, y);
	f = bg(x, y);

	if ( strchr("\xfe\xb0\xb1\xe5\xec\x04?" DOORS, s) ) {
	  live = 0;
	  b->color = 0;
	} else {
	  color(b->color, f);
	  lprint(x,y,"\xf9");

	  switch ( s ) {
	  case SYM_CHERRY:
	    take_cherry();
	    live = 0;
	    break;
	  case SYM_DIAMOND:
	    take_diamond();
	    live = 0;
	    break;
	  case SYM_GHOST:
	    kill_ghost(x,y);
	    live = 0;
	    break;
	  case 'H':
	    hpp = 0; goto hyper;
	  case 'Y':
	    hpp = 1; goto hyper;
	  case 'P':
	    hpp = 2; goto hyper;
	  case 'E':
	    hpp = 3; goto hyper;
	  case 'R':
	    hpp = 4; goto hyper;
	  hyper:
	    gp.Hyp++;
	    color(15,gp.c);
	    lprintf(25, 31+hpp*2, "%c", s);
	    live = 0;
	    break;
	  case SYM_CLUSTER:
	    {
	      int di, dj;
	      for ( di = -1 ; di <= 1 ; di++ )
		for ( dj = -1 ; dj <= 1 ; dj++ )
		  if ( (di||dj) && (di!=-b->dx || dj!=-b->dy) )
		    add_bullet(x,y,di,dj,12);
	      live = 0;
	    }
	    break;
	  default:
	    break;
	  }
	}
      }

      if ( live )
	add_bullet(x,y,b->dx,b->dy,b->color);
    }
    update_score();

    mymssleep(10);		/* Allow the user to notice :) */

    /* Erase bullets */
    for ( b = bb ; b < eb ; b++ ) {
      if ( b->color ) {
	color(0,bg(b->x,b->y));
	lprint(b->x,b->y," ");
      }
    }
  }
}