-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp1-given.c
73 lines (67 loc) · 1.59 KB
/
mp1-given.c
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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
/* mp1-given.c
* Code I wrote so that the students don't have to. In the kernel, these
* reside in rtc.c
* Mark Murphy 2007
*/
#include "mp1.h"
/* These are defined in mp1.c */
extern char base_alive[3];
extern struct missile* mp1_missile_list;
extern int mp1_score;
/* Check if an explosion at (x,y) touches any of the three bases */
static int explode_base(int x, int y){
int bases_killed = 0;
if(y >= 23){
if(17 <= x && x <= 23){
if(base_alive[0])
bases_killed++;
base_alive[0] = 0;
}else
if(37 <= x && x <= 43){
if(base_alive[1])
bases_killed++;
base_alive[1] = 0;
}else
if(57 <= x && x <= 63){
if(base_alive[2])
bases_killed++;
base_alive[2] = 0;
}
}
return bases_killed;
}
/* missile_explode()
* Check if an exploding missile has side-effects - killing other missiles,
* killing bases, etc.
* Arguments : struct missile *m - the missile which exploded
* Returns : zero if no game state was changed, nonzero if game state
* (other missiles or bases) has changed
*/
int missile_explode(struct missile *m){
struct missile *i = mp1_missile_list;
int exploded = 0;
if(!m->exploded){
m->exploded = 50;
}
if(m->c == 'e'){
exploded += explode_base(m->x>>16, m->y>>16);
}
while(i){
if(i != m){
int dx, dy;
dx = (m->x>>16) - (i->x>>16);
dy = (m->y>>16) - (i->y>>16);
if(dx >= -2 && dx <= 2 && dy >= -1 && dy <= 1 &&
i->exploded == 0 && i->c == 'e' && m->c == '*'){
mp1_score++;
exploded++;
i->exploded = 50;
}
}
i = i->next;
}
return exploded;
}