home *** CD-ROM | disk | FTP | other *** search
- Path: uunet!ogicse!zephyr.ens.tek.com!master!saab!billr
- From: billr@saab.CNA.TEK.COM (Bill Randle)
- Newsgroups: comp.sources.games
- Subject: v15i004: ufo - an object shooting game, Part01/01
- Message-ID: <3732@master.CNA.TEK.COM>
- Date: 9 Oct 92 00:42:12 GMT
- Article-I.D.: master.3732
- Sender: news@master.CNA.TEK.COM
- Lines: 542
- Approved: billr@saab.CNA.TEK.COM
-
- Submitted-by: vmm@bruny.cc.utas.edu.au (Vishv Malhotra)
- Posting-number: Volume 15, Issue 4
- Archive-name: ufo/Part01
- Environment: ANSI C, curses
-
- [I haven't tested this as I don't have a set of ANSI C libraries
- (it uses raise()). -br]
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 1 (of 1)."
- # Contents: README MANIFEST Makefile ufo.c
- # Wrapped by billr@saab on Thu Oct 8 17:40:36 1992
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'README' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'README'\"
- else
- echo shar: Extracting \"'README'\" \(288 characters\)
- sed "s/^X//" >'README' <<'END_OF_FILE'
- X * To compile use a ANSI standard C compiler
- X * cc filename -lcurses -ltermcap; a.out *
- X * To run use
- X * a.out -T 200 -D 4 -N 4
- X * T in ms tells how frequently ufo moves
- X * D in characters tells how far the ufo
- X can hop
- X * N Speed of the torpedos as a multiple of
- X the plane speed
- END_OF_FILE
- if test 288 -ne `wc -c <'README'`; then
- echo shar: \"'README'\" unpacked with wrong size!
- fi
- # end of 'README'
- fi
- if test -f 'MANIFEST' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'MANIFEST'\"
- else
- echo shar: Extracting \"'MANIFEST'\" \(238 characters\)
- sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
- X File Name Archive # Description
- X-----------------------------------------------------------
- X MANIFEST 1 This shipping list
- X Makefile 1
- X README 1
- X ufo.c 1
- END_OF_FILE
- if test 238 -ne `wc -c <'MANIFEST'`; then
- echo shar: \"'MANIFEST'\" unpacked with wrong size!
- fi
- # end of 'MANIFEST'
- fi
- if test -f 'Makefile' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'Makefile'\"
- else
- echo shar: Extracting \"'Makefile'\" \(247 characters\)
- sed "s/^X//" >'Makefile' <<'END_OF_FILE'
- X# Simple Makefile for ufo
- XSRCS = ufo.c
- XOBJS = ufo.o
- X# On SysV you don't need the -ltermcap part
- XLIBS = -lcurses -ltermcap
- X
- X# requires ANSI standard C compiler (and library)
- XCC = gcc
- XCFLAGS = -ansi
- X
- Xufo: ufo.c
- X $(CC) -o ufo $(CFLAGS) ufo.c $(LIBS)
- END_OF_FILE
- if test 247 -ne `wc -c <'Makefile'`; then
- echo shar: \"'Makefile'\" unpacked with wrong size!
- fi
- # end of 'Makefile'
- fi
- if test -f 'ufo.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'ufo.c'\"
- else
- echo shar: Extracting \"'ufo.c'\" \(10690 characters\)
- sed "s/^X//" >'ufo.c' <<'END_OF_FILE'
- X
- X#include <stdio.h>
- X#include <curses.h>
- X#include <string.h>
- X#include <signal.h>
- X#include <sys/types.h>
- X#include <sys/time.h>
- X#include <sys/timeb.h>
- X#include <fcntl.h>
- X
- X#define RAND_MAX 077777
- X#define INF 03777777777
- X#define MAX_TORS 25
- X
- X/******************************************
- X * To compile use a standard_C compiler
- X * cc filename -lcurses -ltermcap; a.out *
- X * To run use
- X * a.out -T 200 -D 4 -N 4
- X * T in ms tells how frequently ufo moves
- X * D in characters tells how far the ufo
- X can hop
- X * N Speed of the torpedos as a multiple of
- X the plane speed
- X*********************************************/
- X
- Xint T = 700000,
- X D = 5,
- X N=9;
- X
- Xint nxt_ufo_alarm = 100000,
- X nxt_plane_alarm=100000,
- X nxt_tor_alarm=INF,
- X nxt_int_alarm=INF,
- X NO_ACTIVITY=INF,
- X state_ufo=0;
- X
- Xint slowt[] ={1,1,2},
- X slowd[]={1,2,2};
- X
- Xchar pch[]={'^','>','V','<'},
- X *ufostr[]={"UFOOFU",
- X "UF FO",
- X "U "};
- X
- Xchar BELL[] = {7, 7, 0};
- X
- Xint pspeed, /* How many micro second per move */
- X pchidx = 0; /* direction of the plane */
- X
- Xint px, py, ux, uy;
- X
- Xstruct tor { /* torpedos */
- X int tspeed; /* How much time before next position */
- X int tdir; /* Direction of travel */
- X int talarm; /* When is the next alarm due */
- X int x; /* X cordinate */
- X int y; /* y cordinate */
- X} torpedo [MAX_TORS];
- Xint active = -1; /* Number of active torpedo */
- X
- X
- X
- XWINDOW *ufo, *msg;
- X
- Xlong big_bang;
- Xstruct timeb mytime; /* Since Jan. 1, 1971 */
- X
- Xvoid alarm_ufo (int);
- Xvoid alarm_plane (int);
- Xvoid alarm_torpedo (int);
- Xvoid alarm_interrupt (int);
- Xvoid over (int);
- X
- Xvoid show(int now, int next_show) {
- X static int last_show =0;
- X static int last_clear = 0;
- X
- X int i=-1;
- X
- X if ((next_show - last_show)< 30000) /* Too close */
- X return; /* Do not show */
- X last_show = now;
- X while (i++<active)
- X mvaddch(torpedo[i].y,torpedo[i].x,'*');
- X overlay(ufo,stdscr);
- X mvaddch(py,px,pch[pchidx]);
- X mvwprintw(msg, 0, 0, " %3d", 100-now/1000000);
- X overlay(msg, stdscr);
- X refresh();
- X if ((now-last_clear)>9000000) { /* clear screen every 9 sec. */
- X last_clear = now;
- X clear();
- X }
- X else erase();
- X}
- X
- X
- Xint find_time () {
- X struct timeb now;
- X ftime(&now);
- X return (1000*(now.time-big_bang)+ now.millitm)*1000;
- X}
- X
- X
- Xvoid nxt_sched(void) {
- X /* Determine who is due for change of position
- X next. Is the change overdue? If yes
- X then do not waste time in refresh().
- X Hurry and get the change now.
- X */
- X
- X int alarm = nxt_plane_alarm;
- X int now;
- X
- X signal(SIGALRM, alarm_plane);
- X if (nxt_ufo_alarm<alarm) {
- X signal(SIGALRM, alarm_ufo);
- X alarm = nxt_ufo_alarm;
- X }
- X if (nxt_int_alarm<alarm) {
- X signal(SIGALRM, alarm_interrupt);
- X alarm = nxt_int_alarm;
- X }
- X if (active>-1)
- X if (nxt_tor_alarm<alarm) {
- X signal(SIGALRM, alarm_torpedo);
- X alarm = nxt_tor_alarm;
- X }
- X
- X now = find_time();
- X if (now>=alarm)
- X raise(SIGALRM);
- X else {ualarm(alarm-now,0);
- X show(now, alarm);
- X }
- X if ((now>100000000) || (now>NO_ACTIVITY))
- X signal(SIGALRM, over);
- X}
- X
- X
- Xint in_ufo(int y, int x) {/* Is the position in UFO */
- X /* Torpedo ready to damage */
- X switch (state_ufo) {
- X case 0:
- X if ((x>=ux) && (x<(ux+3)) && (y>=uy) && (y<(uy+2)))
- X return 1;
- X else return 0;
- X case 1: if ((x==ux) && (y==uy)) return 1;
- X if ((x==(ux+2)) && (y==(uy+1))) return 1;
- X if ((x==(ux+1)) && (y>=uy) && (y<(uy+2))) return 1;
- X return 0;
- X case 2: if ((x==ux) && (y==uy)) return 1;
- X return 0;
- X }
- X}
- X
- Xint sitting_duck(void) {
- X /* Is the new position too bad for ufo */
- X if (in_ufo(py, px)) return 1;
- X
- X switch (pchidx) {
- X case 0: if (uy > py) return 0;
- X if (px < ux) return 0;
- X if (px > (ux+2)) return 0;
- X return 1;
- X case 1: if ((2+ux) < px) return 0;
- X if (py < uy) return 0;
- X if (py > (uy+1)) return 0;
- X return 1;
- X case 2: if ((uy+1) < py) return 0;
- X if (px < ux) return 0;
- X if (px > (ux+2)) return 0;
- X return 1;
- X case 3: if (ux > px) return 0;
- X if (py < uy) return 0;
- X if (py > (uy+1)) return 0;
- X return 1;
- X }
- X}
- X
- Xvoid over(int sig) {/* Game is over */
- X signal(SIGINT, SIG_IGN);
- X nocbreak(); echo();
- X unlink("____tmp.c");
- X endwin();
- X printf("\n\nYour rating as a fighter pilot is:%d",state_ufo*
- X (3+(D*1000000/T)+1000*1000000/find_time()));
- X printf("\nAdvise: Please avoid wars.\n");
- X exit();
- X}
- X
- Xvoid alarm_ufo(int sig) { /* Reposition the ufo */
- X int oldx=ux, oldy=uy;
- X int d, x, y, a;
- X nxt_ufo_alarm += T/slowt[state_ufo];
- X d= D*rand()/RAND_MAX/slowd[state_ufo];
- X x= rand()-RAND_MAX/2;
- X y= rand()-RAND_MAX/2;
- X a = abs(x)+abs(y);
- X ux = d*x/a+ux;
- X uy = d*y/a+uy;
- X if (ux<0) ux = -ux;
- X if (uy<0) uy = -uy;
- X if (ux>(COLS-3)) ux = 2*COLS-3-ux;
- X if (uy>(LINES-2)) uy = 2*LINES-2-uy;
- X /* Try to avoid plane, if possible */
- X if (sitting_duck()) {
- X switch (pchidx) {
- X case 0:
- X case 2:
- X ux = oldx -
- X ((ux>(COLS/2))?D:-D)/slowd[state_ufo];
- X if (ux<0) ux =0;
- X uy = oldy;
- X if (sitting_duck()) {
- X ux = oldx +
- X ((ux>(COLS/2))?D:-D)/slowd[state_ufo];
- X if (ux>(COLS-3)) ux = COLS -3;
- X }
- X break;
- X case 1:
- X case 3:
- X uy = oldy -
- X ((uy>(LINES/2))?D:-D)/slowd[state_ufo];
- X if (uy<0) uy =0;
- X ux = oldx;
- X if (sitting_duck()) {
- X uy = oldy +
- X ((uy>(LINES/2))?D:-D)/slowd[state_ufo];
- X if (uy>(LINES-2)) uy = LINES -2;
- X }
- X }
- X }
- X mvwin(ufo, uy, ux);
- X nxt_ufo_alarm += T/slowt[state_ufo];
- X nxt_sched();
- X}
- X
- Xvoid alarm_plane (int sig) { /* reposition the plane */
- X switch (pchidx) {
- X case 0: {py--;
- X if (py<0) {
- X py =0; pchidx =2;}
- X break;
- X }
- X case 1: {px++;
- X if (px>=COLS) {
- X px =COLS-1; pchidx=3;}
- X break;
- X }
- X case 2: {py++;
- X if (py>=LINES) {
- X py=LINES-1; pchidx=0;}
- X break;
- X }
- X case 3: {px--;
- X if (px<0) {
- X px=0; pchidx=1;}
- X break;
- X }
- X }
- X nxt_plane_alarm += pspeed;
- X nxt_sched();
- X}
- X
- X
- Xvoid alarm_torpedo (int sig) {
- X /* Assumption is that no more then 10 torpedos are in
- X action at a time
- X */
- X
- X int now = find_time();
- X
- X int first, i;
- X int mask;
- X
- X mask = sigblock(sigmask(SIGINT));
- X i = -1; first = i;
- X while (++i<=active) {
- X if (torpedo[i].talarm<now) {
- X /*mvaddch(torpedo[i].y, torpedo[i].x,' ');*/
- X torpedo[i].talarm += torpedo[i].tspeed;
- X switch (torpedo[i].tdir) {
- X case 0: {torpedo[i].y -= 1;
- X if (torpedo[i].y<0)
- X torpedo[i]=torpedo[active--];
- X break;
- X }
- X case 1: {torpedo[i].x += 1;
- X if (torpedo[i].x>COLS)
- X torpedo[i]=torpedo[active--];
- X break;
- X }
- X case 2: {torpedo[i].y += 1;
- X if (torpedo[i].y>LINES)
- X torpedo[i]=torpedo[active--];
- X break;
- X }
- X case 3: {torpedo[i].x -= 1;
- X if (torpedo[i].x<0)
- X torpedo[i]=torpedo[active--];
- X break;
- X }
- X }
- X if (in_ufo(torpedo[i].y, torpedo[i].x)) {
- X if (state_ufo++==2) over(0);
- X addstr(BELL); refresh();
- X mvwaddstr(ufo,0,0,ufostr[state_ufo]);
- X torpedo[i] = torpedo[active--];
- X }
- X }
- X /*mvaddch(torpedo[i].y, torpedo[i].x,'*');*/
- X if (first==-1) first = i;
- X if (torpedo[i].talarm<torpedo[first].talarm)
- X first =i;
- X }
- X nxt_tor_alarm = torpedo[first].talarm;
- X sigsetmask(mask);
- X nxt_sched();
- X}
- X
- X
- Xvoid fire (int sig) { /* Fire a torpedo */
- X NO_ACTIVITY = 20000000+ find_time();
- X if (active >= (MAX_TORS-1)) return; /* Too many torpedos */
- X signal(SIGINT, SIG_IGN);
- X ualarm(0,0);
- X nxt_int_alarm = find_time()+T;
- X active++;
- X torpedo[active].x = px;
- X torpedo[active].y = py;
- X torpedo[active].tspeed = pspeed/N;
- X torpedo[active].talarm =find_time()+pspeed/N;
- X torpedo[active].tdir = pchidx;
- X if (torpedo[active].talarm<nxt_tor_alarm)
- X nxt_tor_alarm = torpedo[active].talarm;
- X mvaddch(torpedo[active].y, torpedo[active].x,'#');
- X nxt_sched();
- X}
- X
- X
- Xvoid alarm_interrupt(int sig) { /* Set up the gun */
- X NO_ACTIVITY = 20000000+ find_time();
- X nxt_int_alarm = INF;
- X signal(SIGINT, fire);
- X nxt_sched();
- X}
- X
- Xmain (int argc, char *argv[]) {
- X int t, mask;
- X char cmd;
- X
- X /* Read command line parameters */
- X t =1;
- X while (argc>1) {
- X if (!strcmp(argv[t],"-T"))
- X T=atoi(argv[t+1])*1000;
- X if (!strcmp(argv[t],"-D"))
- X D=atoi(argv[t+1]);
- X if (!strcmp(argv[t],"-N"))
- X N=atoi(argv[t+1]);
- X t +=2; argc -=2;
- X }
- X printf("\n\n Input Effect\n");
- X printf(" a or A Accelerate the plane 25%%\n");
- X printf("cntl-c, c or C Fire a torpedo\n");
- X printf(" d or D Decelerate the plane 20%%\n");
- X printf("<, >, ^, v or V Turn the plane as specified\n");
- X printf("j, l, i, k Same as the line above\n");
- X printf("J, L, I, K Same as the line above\n");
- X printf(" - or _ Turn plane clockwise\n");
- X printf(" + or = Turn plane anticlockwise\n");
- X printf(" s or S Stops the game\n");
- X printf("\n\n Runs for 100 seconds only\n");
- X printf(" Stops if no activity for 20 seconds.\n");
- X
- X/* To detect unauthorised players */
- X sleep(2);
- X ftime(&mytime);
- X big_bang = mytime.time;
- X initscr();
- X refresh();
- X msg = newwin(1,COLS,LINES-1,0);
- X ufo = newwin(2,3,0,0);
- X cbreak(); noecho();
- X clear();
- X signal(SIGINT, fire);
- X waddstr(ufo,ufostr[0]);
- X px = COLS/2; py = LINES/2;
- X pspeed = T/D;
- X ux = 0; uy = 0;
- X nxt_sched();
- X NO_ACTIVITY = 20000000+ find_time();
- X while((cmd=getch()) != 's') {int oldspeed;
- X /* Avoid any interference */
- X mask=sigblock(sigmask(SIGINT));
- X ualarm(0,0);
- X oldspeed =pspeed;
- X switch (cmd) {
- X case '=':
- X case '+': {pchidx = (pchidx+1)%4;
- X break;
- X }
- X case '_':
- X case '-': {pchidx = (pchidx+3)%4;
- X break;
- X }
- X case 'D':
- X case 'd': {pspeed *= 1.25;
- X break;
- X }
- X case 'A':
- X case 'a': pspeed *= 0.8; break;
- X case 'c':
- X case 'C': raise(SIGINT); break;
- X case '<': case 'j': case 'J':
- X case ',': pchidx = 3; break;
- X case '>': case 'l': case 'L':
- X case '.': pchidx = 1; break;
- X case '6': case 'i': case 'I':
- X case '^': pchidx = 0; break;
- X case 'V': case 'k': case 'K':
- X case 'v': pchidx = 2; break;
- X default:;
- X }
- X NO_ACTIVITY = 20000000+ find_time();
- X nxt_plane_alarm += pspeed -oldspeed;
- X sigsetmask(mask);
- X nxt_sched();
- X }
- X nocbreak(); echo();
- X endwin();
- X unlink("____tmp.c");
- X}
- X
- END_OF_FILE
- if test 10690 -ne `wc -c <'ufo.c'`; then
- echo shar: \"'ufo.c'\" unpacked with wrong size!
- fi
- # end of 'ufo.c'
- fi
- echo shar: End of archive 1 \(of 1\).
- cp /dev/null ark1isdone
- MISSING=""
- for I in 1 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have the archive.
- rm -f ark[1-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-