/
Run.c
343 lines (316 loc) · 11.4 KB
/
Run.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
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
#include "Functions.h"
/*
takes in a list, the map array, and the level counter (current level).
Moves the units through the level.
*/
int Run(List * L, char map[22][80], int levelCount) {
int i, cnt;
/*X is set to 79 because the x coordinate of the starting location
never changes.*/
int X = 79;
int Y;
int check = 1;
/*direction checks whether the unit should move up or down.*/
int direction = -1;
int win = 0;
cnt = L->size;
/*find the y coordinate of the starting position.*/
for(i = 0; i < 22; i++) {
if(map[i][79] == 'o')
Y = i;
}
/*set the initial x and y of each unit to the
starting locations.*/
for(i = 0; i < L->size; i++) {
L->items[i].y = Y;
L->items[i].x = X;
}
/*check is 1 if there are units remaining, and 0 if all units are killed.*/
while(check == 1) {
for(i = 0; i < L->size; i++) {
check = MoveUnit(i, L, &cnt, &direction, &win, levelCount);
}
usleep(100000);
}
return win;
}
/*
takes in an int i (current position in list), a list, the count of how many items are in the list(cnt),
an integer to check if the user has won the level (win), and the level counter.
Moves the unit, applies any damage dealt, removes dead units from the list and checks if the
user wins the level.
*/
int MoveUnit(int i, List * L, int * cnt, int * direction, int * win, int levelCount) {
/*ch holds the farmer avatar value*/
char ch;
/*check is set if the unit is travelling vertically
(code to check for damage is the same travelling up or down, eliminates
redundant code)*/
int check = 0;
mvwprintw(stdscr, 0, 0, "Level %d", levelCount);
/*Checks which direction to head when travelling vertically, so the unit
doesn't jump between the path behind it and in front of it. 1 = up,
0 = down*/
if(mvinch(L->items[i].y, (L->items[i].x + 1)) != '.' && mvinch(L->items[i].y, (L->items[i].x + 1)) != 'B' && mvinch(L->items[i].y, (L->items[i].x + 1)) != 'Y' && mvinch(L->items[i].y, (L->items[i].x + 1)) != 'S' && mvinch((L->items[i].y - 1), L->items[i].x) == '+')
L->items[i].direction = 1;
else if(mvinch(L->items[i].y, (L->items[i].x + 1)) != '.' && mvinch(L->items[i].y, (L->items[i].x + 1)) != 'B' && mvinch(L->items[i].y, (L->items[i].x + 1)) != 'Y' && mvinch(L->items[i].y, (L->items[i].x + 1)) != 'S' && mvinch((L->items[i].y + 1), L->items[i].x) == '+')
L->items[i].direction = 0;
/*check if the unit should move to the left*/
if(mvinch(L->items[i].y, (L->items[i].x - 1)) == '+') {
/*move avatar to the left, replace its old spot with a '+',
alter the unit's coordinates.*/
mvaddch(L->items[i].y, (L->items[i].x - 1), L->items[i].avatar);
mvaddch(L->items[i].y, L->items[i].x, '+');
L->items[i].x = L->items[i].x - 1;
/*damage calculations*/
ch = (char)mvinch((L->items[i].y - 1), (L->items[i].x - 1));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
/*if damage is taken, replace the character, but in red*/
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
ch = (char)mvinch((L->items[i].y - 1), (L->items[i].x));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
ch = (char)mvinch((L->items[i].y - 1), (L->items[i].x + 1));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
ch = (char)mvinch((L->items[i].y + 1), (L->items[i].x - 1));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
ch = (char)mvinch((L->items[i].y + 1), (L->items[i].x));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
ch = (char)mvinch((L->items[i].y + 1), (L->items[i].x + 1));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
/*check if unit moves up.*/
}else if(mvinch((L->items[i].y - 1), L->items[i].x) == '+' && L->items[i].direction == 1) {
mvaddch((L->items[i].y - 1), L->items[i].x, L->items[i].avatar);
mvaddch(L->items[i].y, L->items[i].x, '+');
L->items[i].y = L->items[i].y - 1;
check = 1;
/*check if unit moves down.*/
} else if(mvinch((L->items[i].y + 1), L->items[i].x) == '+' && L->items[i].direction == 0) {
mvaddch((L->items[i].y + 1), L->items[i].x, L->items[i].avatar);
mvaddch(L->items[i].y, L->items[i].x, '+');
L->items[i].y = L->items[i].y + 1;
check = 1;
/*following code blocks check if the unit has reached the end of the map.
If so, removes unit from the map (not the list!), sets win condition to 1,
and subtracts 1 from the count of units on the board.*/
}else if(mvinch(L->items[i].y, (L->items[i].x - 1)) == 'O') {
mvaddch(L->items[i].y, L->items[i].x, '+');
if(*cnt != 0)
*cnt = *cnt - 1;
*win = 1;
} else if(mvinch((L->items[i].y - 1), L->items[i].x) == 'O') {
mvaddch(L->items[i].y, L->items[i].x, '+');
if(*cnt != 0)
*cnt = *cnt - 1;
*win = 1;
} else if(mvinch((L->items[i].y + 1), L->items[i].x) == 'O') {
mvaddch(L->items[i].y, L->items[i].x, '+');
if(*cnt != 0)
*cnt = *cnt - 1;
*win = 1;
}
/*if the unit is travellint vertically, check for damage*/
if(check == 1) {
/*damage calculations*/
ch = (char)mvinch((L->items[i].y - 1), (L->items[i].x - 1));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
ch = (char)mvinch((L->items[i].y), (L->items[i].x - 1));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
ch = (char)mvinch((L->items[i].y + 1), (L->items[i].x - 1));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
ch = (char)mvinch((L->items[i].y - 1), (L->items[i].x + 1));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
ch = (char)mvinch((L->items[i].y), (L->items[i].x + 1));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
ch = (char)mvinch((L->items[i].y + 1), (L->items[i].x + 1));
if(ch == 'B') {
L->items[i].hp = L->items[i].hp - BDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'Y') {
L->items[i].hp = L->items[i].hp - YDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}else if(ch == 'S') {
L->items[i].hp = L->items[i].hp - SDAM;
attron(COLOR_PAIR(1));
mvaddch(L->items[i].y, L->items[i].x, L->items[i].avatar);
attroff(COLOR_PAIR(1));
}
}
/*remove the unit from the map and the list if it has no hp left*/
if(L->items[i].hp <= 0) {
mvaddch(L->items[i].y, L->items[i].x, '+');
Remove(i, L);
*cnt = *cnt - 1;
}
refresh();
if(*cnt == 0)
return 0;
return 1;
}