NetBSD/games/battlestar/fly.c

296 lines
5.8 KiB
C
Raw Normal View History

/* $NetBSD: fly.c,v 1.8 1999/09/08 21:45:25 jsm Exp $ */
1995-03-21 18:03:38 +03:00
1993-03-21 12:45:37 +03:00
/*
1995-03-21 18:03:38 +03:00
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-10-10 15:39:08 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1995-03-21 18:03:38 +03:00
#if 0
1997-01-07 14:56:32 +03:00
static char sccsid[] = "@(#)fly.c 8.2 (Berkeley) 4/28/95";
1995-03-21 18:03:38 +03:00
#else
__RCSID("$NetBSD: fly.c,v 1.8 1999/09/08 21:45:25 jsm Exp $");
1995-03-21 18:03:38 +03:00
#endif
1997-10-11 06:06:55 +04:00
#endif /* not lint */
1993-03-21 12:45:37 +03:00
1997-01-07 14:56:32 +03:00
#include "extern.h"
1993-03-21 12:45:37 +03:00
#undef UP
#include <curses.h>
#define abs(a) ((a) < 0 ? -(a) : (a))
#define MIDR (LINES/2 - 1)
#define MIDC (COLS/2 - 1)
1997-10-11 06:06:55 +04:00
int row, column;
int dr = 0, dc = 0;
char destroyed;
int ourclock = 120; /* time for all the flights in the game */
char cross = 0;
sig_t oldsig;
1993-03-21 12:45:37 +03:00
void
1997-10-10 15:39:08 +04:00
succumb(dummy)
int dummy __attribute__((__unused__));
1993-03-21 12:45:37 +03:00
{
if (oldsig == SIG_DFL) {
endfly();
exit(1);
}
if (oldsig != SIG_IGN) {
endfly();
1997-10-11 06:06:55 +04:00
(*oldsig) (SIGINT);
1993-03-21 12:45:37 +03:00
}
}
1997-10-10 15:39:08 +04:00
int
1993-03-21 12:45:37 +03:00
visual()
{
destroyed = 0;
if (initscr() == NULL) {
1993-03-21 12:45:37 +03:00
puts("Whoops! No more memory...");
1997-10-11 06:06:55 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
oldsig = signal(SIGINT, succumb);
crmode();
noecho();
screen();
1997-10-11 06:06:55 +04:00
row = rnd(LINES - 3) + 1;
column = rnd(COLS - 2) + 1;
1997-10-10 15:39:08 +04:00
moveenemy(0);
1993-03-21 12:45:37 +03:00
for (;;) {
1997-10-11 06:06:55 +04:00
switch (getchar()) {
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case 'h':
case 'r':
dc = -1;
fuel--;
break;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case 'H':
case 'R':
dc = -5;
fuel -= 10;
break;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case 'l':
dc = 1;
fuel--;
break;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case 'L':
dc = 5;
fuel -= 10;
break;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case 'j':
case 'u':
dr = 1;
fuel--;
break;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case 'J':
case 'U':
dr = 5;
fuel -= 10;
break;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case 'k':
case 'd':
dr = -1;
fuel--;
break;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case 'K':
case 'D':
dr = -5;
fuel -= 10;
break;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case '+':
if (cross) {
cross = 0;
notarget();
} else
cross = 1;
break;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case ' ':
case 'f':
if (torps) {
torps -= 2;
blast();
if (row == MIDR && column - MIDC < 2 && MIDC - column < 2) {
destroyed = 1;
alarm(0);
1993-03-21 12:45:37 +03:00
}
1997-10-11 06:06:55 +04:00
} else
mvaddstr(0, 0, "*** Out of torpedoes. ***");
break;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case 'q':
endfly();
return (0);
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
default:
mvaddstr(0, 26, "Commands = r,R,l,L,u,U,d,D,f,+,q");
continue;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
case EOF:
break;
1993-03-21 12:45:37 +03:00
}
1997-10-11 06:06:55 +04:00
if (destroyed) {
1993-03-21 12:45:37 +03:00
endfly();
1997-10-11 06:06:55 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-11 06:06:55 +04:00
if (ourclock <= 0) {
1993-03-21 12:45:37 +03:00
endfly();
die();
}
}
}
1997-10-10 15:39:08 +04:00
void
1993-03-21 12:45:37 +03:00
screen()
{
1997-10-11 06:06:55 +04:00
int r, c, n;
int i;
1993-03-21 12:45:37 +03:00
clear();
i = rnd(100);
1997-10-11 06:06:55 +04:00
for (n = 0; n < i; n++) {
r = rnd(LINES - 3) + 1;
1993-03-21 12:45:37 +03:00
c = rnd(COLS);
mvaddch(r, c, '.');
}
1997-10-11 06:06:55 +04:00
mvaddstr(LINES - 1 - 1, 21, "TORPEDOES FUEL TIME");
1993-03-21 12:45:37 +03:00
refresh();
}
1997-10-10 15:39:08 +04:00
void
1993-03-21 12:45:37 +03:00
target()
{
1997-10-11 06:06:55 +04:00
int n;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
move(MIDR, MIDC - 10);
1993-03-21 12:45:37 +03:00
addstr("------- + -------");
1997-10-11 06:06:55 +04:00
for (n = MIDR - 4; n < MIDR - 1; n++) {
mvaddch(n, MIDC, '|');
mvaddch(n + 6, MIDC, '|');
1993-03-21 12:45:37 +03:00
}
}
1997-10-10 15:39:08 +04:00
void
1993-03-21 12:45:37 +03:00
notarget()
{
1997-10-11 06:06:55 +04:00
int n;
1993-03-21 12:45:37 +03:00
1997-10-11 06:06:55 +04:00
move(MIDR, MIDC - 10);
1993-03-21 12:45:37 +03:00
addstr(" ");
1997-10-11 06:06:55 +04:00
for (n = MIDR - 4; n < MIDR - 1; n++) {
mvaddch(n, MIDC, ' ');
mvaddch(n + 6, MIDC, ' ');
1993-03-21 12:45:37 +03:00
}
}
1997-10-10 15:39:08 +04:00
void
1993-03-21 12:45:37 +03:00
blast()
{
1997-10-11 06:06:55 +04:00
int n;
1993-03-21 12:45:37 +03:00
alarm(0);
1997-10-11 06:06:55 +04:00
move(LINES - 1, 24);
1993-03-21 12:45:37 +03:00
printw("%3d", torps);
1997-10-11 06:06:55 +04:00
for (n = LINES - 1 - 2; n >= MIDR + 1; n--) {
mvaddch(n, MIDC + MIDR - n, '/');
mvaddch(n, MIDC - MIDR + n, '\\');
1993-03-21 12:45:37 +03:00
refresh();
}
1997-10-11 06:06:55 +04:00
mvaddch(MIDR, MIDC, '*');
for (n = LINES - 1 - 2; n >= MIDR + 1; n--) {
mvaddch(n, MIDC + MIDR - n, ' ');
mvaddch(n, MIDC - MIDR + n, ' ');
1993-03-21 12:45:37 +03:00
refresh();
}
alarm(1);
}
void
1997-10-10 15:39:08 +04:00
moveenemy(dummy)
int dummy __attribute__((__unused__));
1993-03-21 12:45:37 +03:00
{
1997-10-11 06:06:55 +04:00
double d;
int oldr, oldc;
1993-03-21 12:45:37 +03:00
oldr = row;
oldc = column;
1997-10-11 06:06:55 +04:00
if (fuel > 0) {
if (row + dr <= LINES - 3 && row + dr > 0)
1993-03-21 12:45:37 +03:00
row += dr;
1997-10-11 06:06:55 +04:00
if (column + dc < COLS - 1 && column + dc > 0)
1993-03-21 12:45:37 +03:00
column += dc;
1997-10-11 06:06:55 +04:00
} else
if (fuel < 0) {
fuel = 0;
mvaddstr(0, 60, "*** Out of fuel ***");
}
d = (double) ((row - MIDR) * (row - MIDR) + (column - MIDC) * (column - MIDC));
if (d < 16) {
1993-03-21 12:45:37 +03:00
row += (rnd(9) - 4) % (4 - abs(row - MIDR));
column += (rnd(9) - 4) % (4 - abs(column - MIDC));
}
1997-10-10 15:39:08 +04:00
ourclock--;
1993-03-21 12:45:37 +03:00
mvaddstr(oldr, oldc - 1, " ");
if (cross)
target();
mvaddstr(row, column - 1, "/-\\");
1997-10-11 06:06:55 +04:00
move(LINES - 1, 24);
1993-03-21 12:45:37 +03:00
printw("%3d", torps);
1997-10-11 06:06:55 +04:00
move(LINES - 1, 42);
1993-03-21 12:45:37 +03:00
printw("%3d", fuel);
1997-10-11 06:06:55 +04:00
move(LINES - 1, 57);
1997-10-10 15:39:08 +04:00
printw("%3d", ourclock);
1993-03-21 12:45:37 +03:00
refresh();
signal(SIGALRM, moveenemy);
alarm(1);
}
1997-10-10 15:39:08 +04:00
void
1993-03-21 12:45:37 +03:00
endfly()
{
alarm(0);
signal(SIGALRM, SIG_DFL);
1997-10-11 06:06:55 +04:00
mvcur(0, COLS - 1, LINES - 1, 0);
1993-03-21 12:45:37 +03:00
endwin();
signal(SIGTSTP, SIG_DFL);
signal(SIGINT, oldsig);
}