-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommbat.c
491 lines (423 loc) · 10.3 KB
/
commbat.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
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
480
481
482
483
484
485
486
487
488
489
490
491
/* Comm-Bat a two player tank game by Jim Veneskey 12-18-89 (c) */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <curses.h>
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
/* All externally accessed variables are defined here */
char bigmap[86][86],twas[9];
int tankx[9],tanky[9],tankm[9];
int decoyx[4],decoyy[4],decoym[4];
int shields[10],tank=1,decoy=1,map=1;
int basex,basey,basem;
int realx,realy;
int zx=0; /* testing only */
int main(void)
{
/* init ncurses */
WINDOW * mainwin;
if (( mainwin = initscr()) == NULL) {
fprintf(stderr,"ERROR initializing ncurses.\n");
exit(1);
}
/* Define all internally used variables */
int loopy,loopx;
int keyin=0;
/* Initialize BigMap array to all blanks */
for(loopy=1; loopy<=84; loopy++)
for(loopx=1; loopx<=84; loopx++)
bigmap[loopy][loopx]=' ';
for(loopy=1; loopy<=2; loopy++)
for(loopx=1; loopx<=84; loopx++)
{
bigmap[loopy][loopx]='#';
bigmap[loopy+82][loopx]='#';
}
for(loopx=1; loopx<=2; loopx++)
for(loopy=1; loopy<=84; loopy++)
{
bigmap[loopy][loopx]='#';
bigmap[loopy][loopx+82]='#';
}
for(loopx=1; loopx<=8; loopx++)
{
shields[loopx]=100;
twas[loopx]='B';
}
/*********** Main programming starts here ***********/
curs_set(0);
drawscr();
initgame();
views();
while(keyin != 27)
{
keyin = check_keybrd();
switch(keyin)
{
case 172:movetank(UP);break;
case 180:movetank(DOWN);break;
case 175:movetank(LEFT);break;
case 177:movetank(RIGHT);break;
case 182:clrtext();break;
case 49:tank=1;views();break;
case 50:tank=2;views();break;
case 51:tank=3;views();break;
case 52:tank=4;views();break;
case 53:tank=5;views();break;
case 54:tank=6;views();break;
case 55:tank=7;views();break;
case 56:tank=8;views();break;
case 33:decoy=1;views();break;
case 64:decoy=2;views();break;
case 35:decoy=3;views();break;
}
remote();
refresh();
}
curs_set(1); /* Show Cursor */
return(0);
}
/* What follows are function declarations */
/* BOXIT() uses upper left and lower right corners as parameters */
boxit(y1,x1,y2,x2)
int y1,x1,y2,x2;
{
int temp1;
for(temp1=x1; temp1<=x2; temp1++)
{
move(temp1,y1);
wprintw(stdscr,"#");
move(temp1,y2);
wprintw(stdscr,"#");
}
for(temp1=y1; temp1<=y2; temp1++)
{
move(x1,temp1);
wprintw(stdscr,"#");
move(x2,temp1);
wprintw(stdscr,"#");
}
move(x1,y1);
wprintw(stdscr,"#");
move(x1,y2);
wprintw(stdscr,"#");
move(x2,y1);
wprintw(stdscr,"#");
move(x2,y2);
wprintw(stdscr,"#");
return(0);
}
/* a print @ type of routine */
printat(row,column,text)
int row,column;
char text[40];
{
move(column,row);
wprintw(stdscr, text);
return(0);
}
/* This routine simply clears out the old messages */
clrtext()
{
move(3,24);
wprintw(stdscr," ");
return(0);
}
/* This routine calls other routines to set up the main game screen */
drawscr()
{
boxit(1,39,22,80);
boxit(16,4,22,10);
boxit(16,16,22,22);
boxit(16,28,22,34);
boxit(1,1,15,38);
boxit(23,2,25,79);
printat(1,53,"[ Main Map ]");
printat(22,54,"[ Map # 1 ]");
printat(1,6,"[ Comm-Bat Command Listing ]");
printat(23,34,"[ Messages ]");
printat(16,5,"[ D ]");
printat(16,17,"[ B ]");
printat(16,29,"[ T ]");
return(0);
}
/* this routine polls the keyboard to see if a key has been struck.
if not, it returns a zero.
if so, it returns a value either 1-127 for normal keys or 201-209 for
keypad. non valid keys are not supported and ignored. */
check_keybrd(c)
int c;
{
if (! kbhit() ) return (0); /* if no keystruck return with "0" */
c = getch(); /* Otherwise check for normal key */
if (c) return (c); /* if it is normal, return it. */
c = getch(); /* Must be an extended key - again */
return (c+100); /* add 100 to act as a flag. */
}
/* this routine is responsible for moving the tank if it is ok to do so */
movetank(direction)
int direction;
{
int zq;
if(!shields[tank])
{
printat(24,3,"\x7");
printat(24,3,"That Tank is dead - immobile! ");
return(0);
}
erasetank(tank);
zq=badmove(direction); /* 0 = Normal, 1 = RIP, 2 = New Map */
if(zq == 1)
{
shields[tank]=0;
printat(24,3,"Tank has hit map edge and exploded!");
printat(24,3,"\x7");
convert(tanky[tank],tankx[tank],tankm[tank]);
if((bigmap[realy][realx] !='B') && (bigmap[realy][realx] != 'D'))
{
bigmap[realy][realx] = 'X';
if(tankm[tank] == map)
{
move(tankx[tank]+39,tanky[tank]+1);
wprintw(stdscr,"X");
}
}
views();
return(0);
}
if(zq == 2)
{
convert(tanky[tank],tankx[tank],tankm[tank]);
twas[tank]=bigmap[realy] [realx];
if((twas[tank] !='B') && (twas[tank] !='D'))
bigmap[realy] [realx] = 48+tank;
views();
return(0);
}
switch(direction)
{
case UP:tanky[tank]-=1;break;
case DOWN:tanky[tank]+=1;break;
case LEFT:tankx[tank]-=1;break;
case RIGHT:tankx[tank]+=1;break;
}
update(tank);
views();
return(0);
}
/* This function tests the legality of a move - if it will hit a wall. */
badmove(direction)
int direction;
{
if(direction == LEFT && tankx[tank] > 1)
return(0); /* Tank was not on the edge no further test */
else
if(direction == LEFT && (! (tankm[tank] % 2)))
{
tankx[tank]=40;
tankm[tank]=tankm[tank]-1;
return(2); /* Tank was on edge but EVEN map - adjust and ok */
}
else
if(direction == LEFT)
return(1); /* Tank was on edge of an ODD map - bad news! */
if(direction == RIGHT && tankx[tank] < 40)
return(0); /* Tank was not on edge. */
else
if(direction == RIGHT && (tankm[tank] % 2))
{
tankx[tank]=1;
tankm[tank]=tankm[tank]+1;
return(2); /* Tank was on ODD map edge - adjust and ok */
}
else
if(direction == RIGHT)
return(1); /* Tank was on edge of EVEN map - bad news! */
if(direction == UP && tanky[tank] > 1)
return(0); /* Tank was not at edge. */
else
if(direction == UP && tankm[tank] > 2)
{
tanky[tank]=20;
tankm[tank]=tankm[tank]-2;
return(2); /* tank rose a map - adjust and ok */
}
else
if(direction == UP)
return(1); /* Tank tried to go off top of map */
if(direction == DOWN && tanky[tank] < 20)
return(0); /* Tank was not at edge. */
else
if(direction == DOWN && tankm[tank] < 7)
{
tanky[tank]=1;
tankm[tank]=tankm[tank]+2;
return(2); /* tank descended a map */
}
else
if(direction == DOWN)
return(1); /* Tank was on bottom.... */
return(0); /* this line should not be needed logically */
}
/* UpdateTank(tank #) - updates display of tanks moves */
update(tank)
int tank;
{
convert(tanky[tank],tankx[tank],tankm[tank]);
twas[tank]=bigmap[realy] [realx];
if((twas[tank] !='B') && (twas[tank] != 'D'))
bigmap[realy] [realx] = 48+tank;
move(10,6);
wprintw(stdscr,"Tank X is %d ",tankx[tank]);
move(10,7);
wprintw(stdscr,"Tank Y is %d ",tanky[tank]);
move(10,8);
wprintw(stdscr,"Tank M is %d",tankm[tank]);
move(10,9);
wprintw(stdscr,"Contents of TWAS is %c",twas[tank]);
move(10,10);
wprintw(stdscr,"Tank # = %d",tank);
return(0);
}
/* ERASETANK(tank #) - removes old image of tanks from screen */
erasetank(tank)
int tank;
{
convert(tanky[tank],tankx[tank],tankm[tank]);
bigmap[realy] [realx]=twas[tank];
if(tankm[tank] == map)
{
move(tankx[tank]+39,tanky[tank]+1);
wprintw(stdscr,"%c",twas[tank]);
}
return(0);
}
/* ShowTank(tank #) - Displays currently selected tank IF on current map */
showtank(tank)
int tank;
{
int a,b,c,d;
if(tankm[tank] == map)
{
if(tankx[tank]==1)
a=0;
else if(tankx[tank]==2)
a=1;
else a=2;
if(tankx[tank]==40)
c=0;
else if(tankx[tank]==39)
c=1;
else c=2;
if(tanky[tank]==1)
b=0;
else if(tanky[tank]==2)
b=1;
else b=2;
if(tanky[tank]==20)
d=0;
else if(tanky[tank]==19)
d=1;
else d=2;
movetext(31-a,19-b,31+c,19+d,tankx[tank]+39-a,tanky[tank]+1-b);
}
return(0);
}
/* Remote - this simulates tasking in background */
remote()
{
zx=zx+1;
if (zx==100)
zx=0;
move(10,11);
wprintw(stdscr,"Count is %d and counting.",zx);
return(0);
}
/* Initialize game - get base co-ordinates */
initgame()
{
int loop;
move(2,5);
printf("Enter base co-ords X,Y,Map ");
scanf("%d,%d,%d",&basex,&basey,&basem);
convert(basey,basex,basem);
bigmap[realy][realx]='B';
for(loop=1; loop<=8; loop++)
{
tankx[loop]=basex;
tanky[loop]=basey;
tankm[loop]=basem;
if(loop<4)
{
decoyx[loop]=basex+(4*(loop-1))+2;
decoyy[loop]=basey+(4*(loop-1))+2;
decoym[loop]=basem;
convert(decoyy[loop],decoyx[loop],decoym[loop]);
bigmap[realy][realx]='D';
}
}
return(0);
}
/* Update Tank viewport */
tankview(tank)
int tank;
{
int x,y;
convert(tanky[tank],tankx[tank],tankm[tank]);
for(x=0; x<=4; x++)
for(y=0; y<=4; y++)
{
move(29+x,17+y);
wprintw(stdscr,"%c",bigmap[realy-2+y] [realx-2+x]);
}
showtank(tank);
return(0);
}
/* Update Base viewport */
baseview()
{
int x,y;
convert(basey,basex,basem);
for(x=0; x<=4; x++)
for(y=0; y<=4; y++)
{
move(17+x,17+y);
wprintw(stdscr,"%c",bigmap[realy-2+y][realx-2+x]);
}
return(0);
}
/* Update Decoy viewport */
decoyview(decoy)
int decoy;
{
int x,y;
convert(decoyy[decoy],decoyx[decoy],decoym[decoy]);
for(x=0; x<=4; x++)
for(y=0; y<=4; y++)
{
move(5+x,17+y);
wprintw(stdscr,"%c",bigmap[realy-2+y][realx-2+x]);
}
return(0);
}
/* Convert Tank Psuedo co-ords into actual BigMap Co-ords */
convert(y,x,map)
int y,x,map;
{
if(map % 2)
realx = x+2;
else realx = x+42;
realy=y+2+((map+1)/2-1)*20;
return(0);
}
/* Package all view handling nice and neat */
views()
{
tankview(tank);
baseview();
decoyview(decoy);
return(0);
}