home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / games / volume15 / ufo / part01 < prev    next >
Encoding:
Internet Message Format  |  1993-01-26  |  14.4 KB

  1. Path: uunet!ogicse!zephyr.ens.tek.com!master!saab!billr
  2. From: billr@saab.CNA.TEK.COM (Bill Randle)
  3. Newsgroups: comp.sources.games
  4. Subject: v15i004:  ufo - an object shooting game, Part01/01
  5. Message-ID: <3732@master.CNA.TEK.COM>
  6. Date: 9 Oct 92 00:42:12 GMT
  7. Article-I.D.: master.3732
  8. Sender: news@master.CNA.TEK.COM
  9. Lines: 542
  10. Approved: billr@saab.CNA.TEK.COM
  11.  
  12. Submitted-by: vmm@bruny.cc.utas.edu.au (Vishv Malhotra)
  13. Posting-number: Volume 15, Issue 4
  14. Archive-name: ufo/Part01
  15. Environment: ANSI C, curses
  16.  
  17.     [I haven't tested this as I don't have a set of ANSI C libraries
  18.      (it uses raise()).  -br]
  19.  
  20. #! /bin/sh
  21. # This is a shell archive.  Remove anything before this line, then unpack
  22. # it by saving it into a file and typing "sh file".  To overwrite existing
  23. # files, type "sh file -c".  You can also feed this as standard input via
  24. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  25. # will see the following message at the end:
  26. #        "End of archive 1 (of 1)."
  27. # Contents:  README MANIFEST Makefile ufo.c
  28. # Wrapped by billr@saab on Thu Oct  8 17:40:36 1992
  29. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  30. if test -f 'README' -a "${1}" != "-c" ; then 
  31.   echo shar: Will not clobber existing file \"'README'\"
  32. else
  33. echo shar: Extracting \"'README'\" \(288 characters\)
  34. sed "s/^X//" >'README' <<'END_OF_FILE'
  35. X *    To compile use a ANSI standard C compiler
  36. X *  cc filename -lcurses -ltermcap; a.out  *
  37. X *    To run use 
  38. X *  a.out -T 200 -D 4 -N 4
  39. X *    T in ms tells how frequently ufo moves
  40. X *    D in characters tells how far the ufo
  41. X        can hop
  42. X *    N Speed of the torpedos as a multiple of 
  43. X        the plane speed
  44. END_OF_FILE
  45. if test 288 -ne `wc -c <'README'`; then
  46.     echo shar: \"'README'\" unpacked with wrong size!
  47. fi
  48. # end of 'README'
  49. fi
  50. if test -f 'MANIFEST' -a "${1}" != "-c" ; then 
  51.   echo shar: Will not clobber existing file \"'MANIFEST'\"
  52. else
  53. echo shar: Extracting \"'MANIFEST'\" \(238 characters\)
  54. sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
  55. X   File Name        Archive #    Description
  56. X-----------------------------------------------------------
  57. X MANIFEST                   1    This shipping list
  58. X Makefile                   1    
  59. X README                     1    
  60. X ufo.c                      1    
  61. END_OF_FILE
  62. if test 238 -ne `wc -c <'MANIFEST'`; then
  63.     echo shar: \"'MANIFEST'\" unpacked with wrong size!
  64. fi
  65. # end of 'MANIFEST'
  66. fi
  67. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  68.   echo shar: Will not clobber existing file \"'Makefile'\"
  69. else
  70. echo shar: Extracting \"'Makefile'\" \(247 characters\)
  71. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  72. X# Simple Makefile for ufo
  73. XSRCS = ufo.c
  74. XOBJS = ufo.o
  75. X# On SysV you don't need the -ltermcap part
  76. XLIBS = -lcurses -ltermcap
  77. X
  78. X# requires ANSI standard C compiler (and library)
  79. XCC = gcc
  80. XCFLAGS = -ansi
  81. X
  82. Xufo: ufo.c
  83. X    $(CC) -o ufo $(CFLAGS) ufo.c $(LIBS)
  84. END_OF_FILE
  85. if test 247 -ne `wc -c <'Makefile'`; then
  86.     echo shar: \"'Makefile'\" unpacked with wrong size!
  87. fi
  88. # end of 'Makefile'
  89. fi
  90. if test -f 'ufo.c' -a "${1}" != "-c" ; then 
  91.   echo shar: Will not clobber existing file \"'ufo.c'\"
  92. else
  93. echo shar: Extracting \"'ufo.c'\" \(10690 characters\)
  94. sed "s/^X//" >'ufo.c' <<'END_OF_FILE'
  95. X
  96. X#include <stdio.h>
  97. X#include <curses.h>
  98. X#include <string.h>
  99. X#include <signal.h>
  100. X#include <sys/types.h>
  101. X#include <sys/time.h>
  102. X#include <sys/timeb.h>
  103. X#include <fcntl.h>
  104. X
  105. X#define RAND_MAX 077777
  106. X#define INF 03777777777
  107. X#define MAX_TORS 25
  108. X
  109. X/******************************************
  110. X *    To compile use a standard_C compiler
  111. X *  cc filename -lcurses -ltermcap; a.out  *
  112. X *    To run use 
  113. X *  a.out -T 200 -D 4 -N 4
  114. X *    T in ms tells how frequently ufo moves
  115. X *    D in characters tells how far the ufo
  116. X        can hop
  117. X *    N Speed of the torpedos as a multiple of 
  118. X        the plane speed
  119. X*********************************************/
  120. X
  121. Xint T = 700000,
  122. X    D = 5,
  123. X    N=9;
  124. X
  125. Xint nxt_ufo_alarm = 100000,
  126. X     nxt_plane_alarm=100000,
  127. X    nxt_tor_alarm=INF,
  128. X    nxt_int_alarm=INF,
  129. X    NO_ACTIVITY=INF,
  130. X    state_ufo=0;
  131. X
  132. Xint slowt[] ={1,1,2},
  133. X    slowd[]={1,2,2};
  134. X
  135. Xchar pch[]={'^','>','V','<'},
  136. X    *ufostr[]={"UFOOFU",
  137. X        "UF  FO",
  138. X        "U     "};
  139. X
  140. Xchar BELL[] = {7, 7, 0};
  141. X
  142. Xint pspeed, /* How many micro second per move */
  143. X    pchidx = 0; /* direction of the plane */
  144. X
  145. Xint px, py, ux, uy;
  146. X
  147. Xstruct tor { /* torpedos  */
  148. X      int tspeed;  /* How much time before next position */
  149. X      int tdir; /* Direction of travel */
  150. X      int talarm; /* When is the next alarm due */
  151. X      int x; /* X cordinate */
  152. X      int y; /* y cordinate */
  153. X} torpedo [MAX_TORS];
  154. Xint active = -1; /* Number of active torpedo */
  155. X
  156. X
  157. X
  158. XWINDOW *ufo, *msg;
  159. X
  160. Xlong big_bang;  
  161. Xstruct timeb mytime;  /* Since Jan. 1, 1971  */
  162. X
  163. Xvoid alarm_ufo (int);
  164. Xvoid alarm_plane (int);
  165. Xvoid alarm_torpedo (int);
  166. Xvoid alarm_interrupt (int);
  167. Xvoid over (int);
  168. X
  169. Xvoid show(int now, int next_show) { 
  170. X    static int last_show =0;
  171. X    static int last_clear = 0;
  172. X
  173. X    int i=-1;
  174. X
  175. X    if ((next_show - last_show)< 30000) /* Too close */
  176. X        return; /* Do not show */
  177. X    last_show = now;
  178. X    while (i++<active)
  179. X        mvaddch(torpedo[i].y,torpedo[i].x,'*');
  180. X    overlay(ufo,stdscr);
  181. X    mvaddch(py,px,pch[pchidx]);
  182. X    mvwprintw(msg, 0, 0, " %3d", 100-now/1000000); 
  183. X     overlay(msg, stdscr); 
  184. X    refresh();
  185. X    if ((now-last_clear)>9000000) {  /* clear screen every 9 sec. */
  186. X        last_clear = now;
  187. X        clear();
  188. X    }
  189. X    else erase();
  190. X}
  191. X
  192. X
  193. Xint find_time () {
  194. X    struct timeb now;
  195. X    ftime(&now);
  196. X    return (1000*(now.time-big_bang)+ now.millitm)*1000;
  197. X}
  198. X
  199. X
  200. Xvoid nxt_sched(void) {
  201. X    /* Determine who is due for change of position
  202. X        next. Is the change overdue? If yes
  203. X        then do not waste time in refresh().
  204. X        Hurry and get the change now.
  205. X    */
  206. X    
  207. X    int alarm = nxt_plane_alarm;
  208. X    int now;
  209. X
  210. X        signal(SIGALRM, alarm_plane);
  211. X        if (nxt_ufo_alarm<alarm) {
  212. X            signal(SIGALRM, alarm_ufo);
  213. X            alarm = nxt_ufo_alarm;
  214. X        }
  215. X                if (nxt_int_alarm<alarm) {
  216. X                        signal(SIGALRM, alarm_interrupt);
  217. X                        alarm = nxt_int_alarm;
  218. X                }
  219. X        if (active>-1)
  220. X                    if (nxt_tor_alarm<alarm) {
  221. X                            signal(SIGALRM, alarm_torpedo);
  222. X                            alarm = nxt_tor_alarm;
  223. X                    }
  224. X
  225. X        now = find_time();
  226. X        if (now>=alarm)
  227. X            raise(SIGALRM); 
  228. X        else {ualarm(alarm-now,0);
  229. X                show(now, alarm);
  230. X        }
  231. X    if ((now>100000000) || (now>NO_ACTIVITY))
  232. X         signal(SIGALRM, over); 
  233. X}
  234. X    
  235. X
  236. Xint in_ufo(int y, int x) {/* Is the position in UFO */
  237. X    /* Torpedo ready to damage */
  238. X    switch (state_ufo) {
  239. X    case 0:
  240. X        if ((x>=ux) && (x<(ux+3)) && (y>=uy) && (y<(uy+2)))
  241. X            return 1;
  242. X        else return 0;
  243. X    case 1: if ((x==ux) && (y==uy)) return 1;
  244. X        if ((x==(ux+2)) && (y==(uy+1))) return 1;
  245. X        if ((x==(ux+1)) && (y>=uy) && (y<(uy+2))) return 1;
  246. X        return 0;
  247. X    case 2: if ((x==ux) && (y==uy)) return 1;
  248. X        return 0;
  249. X    }
  250. X}
  251. X
  252. Xint sitting_duck(void) {
  253. X    /* Is the new position too bad for ufo */
  254. X    if (in_ufo(py, px)) return 1;
  255. X
  256. X    switch (pchidx) {
  257. X        case 0: if (uy > py) return 0;
  258. X            if (px < ux) return 0;
  259. X            if (px > (ux+2)) return 0;
  260. X            return 1;
  261. X                case 1: if ((2+ux) < px) return 0;
  262. X                        if (py < uy) return 0;
  263. X                        if (py > (uy+1)) return 0;
  264. X                        return 1;  
  265. X                case 2: if ((uy+1) < py) return 0;
  266. X                        if (px < ux) return 0;
  267. X                        if (px > (ux+2)) return 0;
  268. X                        return 1;
  269. X                case 3: if (ux > px) return 0;
  270. X                        if (py < uy) return 0;
  271. X                        if (py > (uy+1)) return 0;
  272. X                        return 1;
  273. X    }
  274. X}
  275. X
  276. Xvoid over(int sig) {/* Game is over  */
  277. X    signal(SIGINT, SIG_IGN);
  278. X    nocbreak(); echo();
  279. X    unlink("____tmp.c");
  280. X        endwin();
  281. X    printf("\n\nYour rating as a fighter pilot is:%d",state_ufo*
  282. X                 (3+(D*1000000/T)+1000*1000000/find_time()));
  283. X    printf("\nAdvise: Please avoid wars.\n");
  284. X    exit();
  285. X}
  286. X
  287. Xvoid alarm_ufo(int sig) { /* Reposition the ufo */
  288. X    int oldx=ux, oldy=uy;
  289. X    int d, x, y, a;
  290. X    nxt_ufo_alarm += T/slowt[state_ufo];
  291. X    d= D*rand()/RAND_MAX/slowd[state_ufo];
  292. X    x= rand()-RAND_MAX/2; 
  293. X    y= rand()-RAND_MAX/2;
  294. X    a = abs(x)+abs(y);
  295. X    ux = d*x/a+ux;
  296. X    uy = d*y/a+uy;
  297. X    if (ux<0) ux = -ux;
  298. X    if (uy<0) uy = -uy;
  299. X    if (ux>(COLS-3)) ux = 2*COLS-3-ux;
  300. X    if (uy>(LINES-2)) uy = 2*LINES-2-uy;
  301. X    /* Try to avoid plane, if possible */
  302. X    if (sitting_duck())  {
  303. X        switch (pchidx)  {
  304. X            case 0: 
  305. X            case 2:
  306. X                ux = oldx - 
  307. X                    ((ux>(COLS/2))?D:-D)/slowd[state_ufo];
  308. X                if (ux<0) ux =0;
  309. X                uy = oldy;
  310. X                if (sitting_duck()) {
  311. X                    ux = oldx + 
  312. X                      ((ux>(COLS/2))?D:-D)/slowd[state_ufo];
  313. X                    if (ux>(COLS-3)) ux = COLS -3;
  314. X                }
  315. X                break;
  316. X                        case 1: 
  317. X            case 3:
  318. X                                uy = oldy - 
  319. X                    ((uy>(LINES/2))?D:-D)/slowd[state_ufo];
  320. X                                if (uy<0) uy =0;
  321. X                                ux = oldx;
  322. X                                if (sitting_duck()) {
  323. X                                        uy = oldy +
  324. X                     ((uy>(LINES/2))?D:-D)/slowd[state_ufo];
  325. X                                        if (uy>(LINES-2)) uy = LINES -2;
  326. X                                }
  327. X        }
  328. X    }
  329. X    mvwin(ufo, uy, ux);
  330. X    nxt_ufo_alarm += T/slowt[state_ufo];
  331. X    nxt_sched();
  332. X}
  333. X
  334. Xvoid alarm_plane (int sig) { /* reposition the plane */
  335. X    switch (pchidx) {
  336. X        case 0: {py--; 
  337. X             if (py<0) {
  338. X                py =0; pchidx =2;}
  339. X             break;
  340. X        }
  341. X        case 1: {px++; 
  342. X            if (px>=COLS) {
  343. X                px =COLS-1; pchidx=3;}
  344. X            break;
  345. X        }
  346. X        case 2: {py++;
  347. X            if (py>=LINES) {
  348. X                py=LINES-1; pchidx=0;}
  349. X             break;
  350. X        }
  351. X        case 3: {px--;
  352. X            if (px<0) {
  353. X                px=0; pchidx=1;}
  354. X             break;
  355. X        }
  356. X    }
  357. X    nxt_plane_alarm += pspeed;
  358. X    nxt_sched();
  359. X}
  360. X
  361. X
  362. Xvoid alarm_torpedo (int sig) { 
  363. X    /* Assumption is that no more then 10 torpedos are in 
  364. X    action at a time 
  365. X    */
  366. X
  367. X    int now = find_time();
  368. X
  369. X    int first, i;
  370. X    int mask;
  371. X
  372. X    mask = sigblock(sigmask(SIGINT));
  373. X    i = -1; first = i;
  374. X    while (++i<=active) {
  375. X        if (torpedo[i].talarm<now) {
  376. X            /*mvaddch(torpedo[i].y, torpedo[i].x,' ');*/
  377. X            torpedo[i].talarm += torpedo[i].tspeed;
  378. X            switch (torpedo[i].tdir) {
  379. X            case 0: {torpedo[i].y -= 1;
  380. X                if (torpedo[i].y<0)
  381. X                    torpedo[i]=torpedo[active--];
  382. X                break;
  383. X            }
  384. X                        case 1: {torpedo[i].x += 1;
  385. X                                if (torpedo[i].x>COLS)
  386. X                                        torpedo[i]=torpedo[active--];
  387. X                break;
  388. X                        }
  389. X                        case 2: {torpedo[i].y += 1;
  390. X                                if (torpedo[i].y>LINES)
  391. X                                        torpedo[i]=torpedo[active--];
  392. X                break;
  393. X                        }
  394. X                        case 3: {torpedo[i].x -= 1;
  395. X                                if (torpedo[i].x<0)
  396. X                                        torpedo[i]=torpedo[active--];
  397. X                break;
  398. X                        }
  399. X            }
  400. X            if (in_ufo(torpedo[i].y, torpedo[i].x)) {
  401. X                if (state_ufo++==2) over(0);
  402. X                addstr(BELL); refresh();
  403. X                mvwaddstr(ufo,0,0,ufostr[state_ufo]);
  404. X                torpedo[i] = torpedo[active--];
  405. X            } 
  406. X        }
  407. X        /*mvaddch(torpedo[i].y, torpedo[i].x,'*');*/
  408. X        if (first==-1) first = i;
  409. X        if (torpedo[i].talarm<torpedo[first].talarm)
  410. X            first =i;
  411. X    }
  412. X    nxt_tor_alarm = torpedo[first].talarm;
  413. X    sigsetmask(mask);
  414. X    nxt_sched();
  415. X}
  416. X
  417. X    
  418. Xvoid fire (int sig) { /* Fire a torpedo */
  419. X    NO_ACTIVITY = 20000000+ find_time();
  420. X    if (active >= (MAX_TORS-1)) return; /* Too many torpedos */
  421. X    signal(SIGINT, SIG_IGN);
  422. X    ualarm(0,0);
  423. X    nxt_int_alarm = find_time()+T;
  424. X    active++;
  425. X    torpedo[active].x = px;
  426. X        torpedo[active].y = py;
  427. X        torpedo[active].tspeed = pspeed/N;
  428. X    torpedo[active].talarm =find_time()+pspeed/N;
  429. X    torpedo[active].tdir = pchidx;
  430. X    if (torpedo[active].talarm<nxt_tor_alarm)
  431. X        nxt_tor_alarm = torpedo[active].talarm;
  432. X    mvaddch(torpedo[active].y, torpedo[active].x,'#');
  433. X    nxt_sched(); 
  434. X}
  435. X
  436. X
  437. Xvoid alarm_interrupt(int sig) { /* Set up the gun */
  438. X    NO_ACTIVITY = 20000000+ find_time();
  439. X    nxt_int_alarm = INF;
  440. X    signal(SIGINT, fire);
  441. X    nxt_sched();
  442. X}
  443. X
  444. Xmain (int argc, char *argv[]) {
  445. X    int t, mask;
  446. X    char cmd;
  447. X    
  448. X    /* Read command line parameters  */
  449. X    t =1;
  450. X    while (argc>1) {
  451. X        if (!strcmp(argv[t],"-T"))
  452. X            T=atoi(argv[t+1])*1000;
  453. X        if (!strcmp(argv[t],"-D"))
  454. X            D=atoi(argv[t+1]);
  455. X        if (!strcmp(argv[t],"-N"))
  456. X                        N=atoi(argv[t+1]);
  457. X        t +=2; argc -=2;
  458. X    }
  459. X    printf("\n\n   Input        Effect\n");
  460. X    printf("    a or A    Accelerate the plane 25%%\n");
  461. X    printf("cntl-c, c or C    Fire a torpedo\n");
  462. X    printf("    d or D    Decelerate the plane 20%%\n");
  463. X    printf("<, >, ^, v or V    Turn the plane as specified\n");
  464. X    printf("j, l, i, k    Same as the line above\n");
  465. X    printf("J, L, I, K    Same as the line above\n");
  466. X    printf("    - or _    Turn plane clockwise\n");
  467. X    printf("    + or =    Turn plane anticlockwise\n");
  468. X    printf("    s or S  Stops the game\n");
  469. X    printf("\n\n    Runs for 100 seconds only\n");
  470. X    printf("    Stops if no activity for 20 seconds.\n");
  471. X
  472. X/* To detect unauthorised players */
  473. X    sleep(2);
  474. X    ftime(&mytime);
  475. X    big_bang = mytime.time;
  476. X    initscr();
  477. X    refresh();
  478. X    msg = newwin(1,COLS,LINES-1,0);
  479. X    ufo = newwin(2,3,0,0);
  480. X    cbreak(); noecho(); 
  481. X    clear();
  482. X    signal(SIGINT, fire);
  483. X    waddstr(ufo,ufostr[0]);
  484. X    px = COLS/2; py = LINES/2;
  485. X    pspeed = T/D;
  486. X    ux = 0; uy = 0;
  487. X    nxt_sched();    
  488. X    NO_ACTIVITY = 20000000+ find_time();
  489. X    while((cmd=getch()) != 's') {int oldspeed;
  490. X        /* Avoid any interference  */
  491. X        mask=sigblock(sigmask(SIGINT));
  492. X        ualarm(0,0);  
  493. X        oldspeed =pspeed;
  494. X        switch (cmd) {
  495. X            case '=':
  496. X            case '+': {pchidx = (pchidx+1)%4;
  497. X                    break;
  498. X            }
  499. X            case '_':
  500. X            case '-': {pchidx = (pchidx+3)%4;
  501. X                    break;
  502. X            }
  503. X            case 'D':
  504. X            case 'd': {pspeed *= 1.25;
  505. X                    break;
  506. X            }
  507. X            case 'A':
  508. X            case 'a': pspeed *= 0.8; break;
  509. X            case 'c':
  510. X            case 'C': raise(SIGINT); break;
  511. X            case '<': case 'j': case 'J':
  512. X            case ',': pchidx = 3; break;
  513. X            case '>': case 'l': case 'L':
  514. X            case '.': pchidx = 1; break;
  515. X            case '6': case 'i': case 'I':
  516. X            case '^': pchidx = 0; break;
  517. X            case 'V': case 'k': case 'K':
  518. X            case 'v': pchidx = 2; break;
  519. X            default:;
  520. X        }
  521. X        NO_ACTIVITY = 20000000+ find_time();
  522. X        nxt_plane_alarm += pspeed -oldspeed;
  523. X        sigsetmask(mask);
  524. X        nxt_sched();
  525. X    }
  526. X    nocbreak(); echo();
  527. X    endwin();
  528. X    unlink("____tmp.c");
  529. X}
  530. X
  531. END_OF_FILE
  532. if test 10690 -ne `wc -c <'ufo.c'`; then
  533.     echo shar: \"'ufo.c'\" unpacked with wrong size!
  534. fi
  535. # end of 'ufo.c'
  536. fi
  537. echo shar: End of archive 1 \(of 1\).
  538. cp /dev/null ark1isdone
  539. MISSING=""
  540. for I in 1 ; do
  541.     if test ! -f ark${I}isdone ; then
  542.     MISSING="${MISSING} ${I}"
  543.     fi
  544. done
  545. if test "${MISSING}" = "" ; then
  546.     echo You have the archive.
  547.     rm -f ark[1-9]isdone
  548. else
  549.     echo You still need to unpack the following archives:
  550.     echo "        " ${MISSING}
  551. fi
  552. ##  End of shell archive.
  553. exit 0
  554.