예제 #1
0
void
newturn(void)
{
    repaired = loaded = fired = changed = 0;
    movebuf[0] = '\0';

    alarm(0);
    if (mf->readyL & R_LOADING) {
        if (mf->readyL & R_DOUBLE)
            mf->readyL = R_LOADING;
        else
            mf->readyL = R_LOADED;
    }
    if (mf->readyR & R_LOADING) {
        if (mf->readyR & R_DOUBLE)
            mf->readyR = R_LOADING;
        else
            mf->readyR = R_LOADED;
    }
    if (!hasdriver)
        Write(W_DDEAD, SHIP(0), 0, 0, 0, 0);

    if (sc_hasprompt) {
        wmove(scroll_w, sc_line, 0);
        wclrtoeol(scroll_w);
    }
    if (Sync() < 0)
        leave(LEAVE_SYNC);
    if (!hasdriver)
        leave(LEAVE_DRIVER);
    if (sc_hasprompt)
        wprintw(scroll_w, "%s%s", sc_prompt, sc_buf);

    if (turn % 50 == 0)
        Write(W_ALIVE, SHIP(0), 0, 0, 0, 0);
    if (mf->FS && (!mc->rig1 || windspeed == 6))
        Write(W_FS, ms, 0, 0, 0, 0);
    if (mf->FS == 1)
        Write(W_FS, ms, 2, 0, 0, 0);

    if (mf->struck)
        leave(LEAVE_QUIT);
    if (mf->captured != 0)
        leave(LEAVE_CAPTURED);
    if (windspeed == 7)
        leave(LEAVE_HURRICAN);

    adjustview();
    draw_screen();

    signal(SIGALRM, (sig_t)newturn);
    alarm(7);
}
예제 #2
0
파일: dr_main.c 프로젝트: jonathangray/csrg
dr_main()
{
	register int n;
	register struct ship *sp;
	int nat[NNATION];
	int value = 0;

	(void) signal(SIGINT, SIG_IGN);
	(void) signal(SIGQUIT, SIG_IGN);
	(void) signal(SIGTSTP, SIG_IGN);
	if (issetuid)
		(void) setuid(geteuid());
	if (game < 0 || game >= NSCENE) {
		fprintf(stderr, "DRIVER: Bad game number %d\n", game);
		exit(1);
	}
	cc = &scene[game];
	ls = SHIP(cc->vessels);
	if (sync_open() < 0) {
		perror("driver: syncfile");
		exit(1);
	}
	for (n = 0; n < NNATION; n++)
		nat[n] = 0;
	foreachship(sp) {
		if (sp->file == NULL &&
		    (sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
			(void) fprintf(stderr, "DRIVER: Out of memory.\n");
			exit(1);
		}
		sp->file->index = sp - SHIP(0);
		sp->file->loadL = L_ROUND;
		sp->file->loadR = L_ROUND;
		sp->file->readyR = R_LOADED|R_INITIAL;
		sp->file->readyL = R_LOADED|R_INITIAL;
		sp->file->stern = nat[sp->nationality]++;
		sp->file->dir = sp->shipdir;
		sp->file->row = sp->shiprow;
		sp->file->col = sp->shipcol;
	}
	windspeed = cc->windspeed;
	winddir = cc->winddir;
	people = 0;
	for (;;) {
		sleep(7);
		if (Sync() < 0) {
			value = 1;
			break;
		}
		if (next() < 0)
			break;
		unfoul();
		checkup();
		prizecheck();
		moveall();
		thinkofgrapples();
		boardcomp();
		compcombat();
		resolve();
		reload();
		checksails();
		if (Sync() < 0) {
			value = 1;
			break;
		}
	}
	sync_close(1);
	return value;
}
예제 #3
0
initialize()
{
	register struct File *fp;
	register struct ship *sp;
	char captain[80];
	char message[60];
	int load;
	register int n;
	char *nameptr;
	int nat[NNATION];

	if (game < 0) {
		(void) puts("Choose a scenario:\n");
		(void) puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
		for (n = 0; n < NSCENE; n++) {
			/* ( */
			printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
				sync_exists(n) ? "YES" : "no",
				scene[n].name);
		}
reprint:
		printf("\nScenario number? ");
		(void) fflush(stdout);
		(void) scanf("%d", &game);
		while (getchar() != '\n')
			;
	}
	if (game < 0 || game >= NSCENE) {
		(void) puts("Very funny.");
		exit(1);
	}
	cc = &scene[game];
	ls = SHIP(cc->vessels);

	for (n = 0; n < NNATION; n++)
		nat[n] = 0;
	foreachship(sp) {
		if (sp->file == NULL &&
		    (sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
			(void) puts("OUT OF MEMORY");
			exit(1);
		}
		sp->file->index = sp - SHIP(0);
		sp->file->stern = nat[sp->nationality]++;
		sp->file->dir = sp->shipdir;
		sp->file->row = sp->shiprow;
		sp->file->col = sp->shipcol;
	}
	windspeed = cc->windspeed;
	winddir = cc->winddir;

	(void) signal(SIGHUP, choke);
	(void) signal(SIGINT, choke);

	hasdriver = sync_exists(game);
	if (sync_open() < 0) {
		perror("sail: syncfile");
		exit(1);
	}

	if (hasdriver) {
		(void) puts("Synchronizing with the other players...");
		(void) fflush(stdout);
		if (Sync() < 0)
			leave(LEAVE_SYNC);
	}
	for (;;) {
		foreachship(sp)
			if (sp->file->captain[0] == 0 && !sp->file->struck
			    && sp->file->captured == 0)
				break;
		if (sp >= ls) {
			(void) puts("All ships taken in that scenario.");
			foreachship(sp)
				free((char *)sp->file);
			sync_close(0);
			people = 0;
			goto reprint;
		}
		if (randomize) {
			player = sp - SHIP(0);
		} else {
			printf("%s\n\n", cc->name);
			foreachship(sp)
				printf("  %2d:  %-10s %-15s  (%-2d pts)   %s\n",
					sp->file->index,
					countryname[sp->nationality],
					sp->shipname,
					sp->specs->pts,
					saywhat(sp, 1));
			printf("\nWhich ship (0-%d)? ", cc->vessels-1);
			(void) fflush(stdout);
			if (scanf("%d", &player) != 1 || player < 0
			    || player >= cc->vessels) {
				while (getchar() != '\n')
					;
				(void) puts("Say what?");
				player = -1;
			} else
				while (getchar() != '\n')
					;
		}
		if (player < 0)
			continue;
		if (Sync() < 0)
			leave(LEAVE_SYNC);
		fp = SHIP(player)->file;
		if (fp->captain[0] || fp->struck || fp->captured != 0)
			(void) puts("That ship is taken.");
		else
			break;
	}

	ms = SHIP(player);
	mf = ms->file;
	mc = ms->specs;

	Write(W_BEGIN, ms, 0, 0, 0, 0, 0);
	if (Sync() < 0)
		leave(LEAVE_SYNC);

	(void) signal(SIGCHLD, child);
	if (!hasdriver)
		switch (fork()) {
		case 0:
			longjmp(restart, MODE_DRIVER);
			/*NOTREACHED*/
		case -1:
			perror("fork");
			leave(LEAVE_FORK);
			break;
		default:
			hasdriver++;
		}

	printf("Your ship is the %s, a %d gun %s (%s crew).\n",
		ms->shipname, mc->guns, classname[mc->class],
		qualname[mc->qual]);
	if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
		(void) strncpy(captain, nameptr, sizeof captain);
	else {
		(void) printf("Your name, Captain? ");
		(void) fflush(stdout);
		(void) gets(captain);
		if (!*captain)
			(void) strcpy(captain, "no name");
	}
	captain[sizeof captain - 1] = '\0';
	Write(W_CAPTAIN, ms, 1, (int)captain, 0, 0, 0);
	for (n = 0; n < 2; n++) {
		char buf[10];

		printf("\nInitial broadside %s (grape, chain, round, double): ",
			n ? "right" : "left");
		(void) fflush(stdout);
		(void) scanf("%s", buf);
		switch (*buf) {
		case 'g':
			load = L_GRAPE;
			break;
		case 'c':
			load = L_CHAIN;
			break;
		case 'r':
			load = L_ROUND;
			break;
		case 'd':
			load = L_DOUBLE;
			break;
		default:
			load = L_ROUND;
		}
		if (n) {
			mf->loadR = load;
			mf->readyR = R_LOADED|R_INITIAL;
		} else {
			mf->loadL = load;
			mf->readyL = R_LOADED|R_INITIAL;
		}
	}

	initscreen();
	draw_board();
	(void) sprintf(message, "Captain %s assuming command", captain);
	Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0);
	newturn();
}
예제 #4
0
static char sc_hasprompt;
static const char *sc_prompt;
static const char *sc_buf;
static int sc_line;

WINDOW *view_w;
WINDOW *slot_w;
WINDOW *scroll_w;
WINDOW *stat_w;
WINDOW *turn_w;

char done_curses;
char loaded, fired, changed, repaired;
char dont_adjust;
int viewrow, viewcol;
char movebuf[sizeof SHIP(0)->file->movebuf];
int player;
struct ship *ms;		/* memorial structure, &cc->ship[player] */
struct File *mf;		/* ms->file */
struct shipspecs *mc;		/* ms->specs */

void
initscreen()
{
    if (!SCREENTEST())
        errx(1, "can't sail on this terminal.");
    /* initscr() already done in SCREENTEST() */
    view_w = newwin(VIEW_Y, VIEW_X, VIEW_T, VIEW_L);
    slot_w = newwin(SLOT_Y, SLOT_X, SLOT_T, SLOT_L);
    scroll_w = newwin(SCROLL_Y, SCROLL_X, SCROLL_T, SCROLL_L);
    stat_w = newwin(STAT_Y, STAT_X, STAT_T, STAT_L);
예제 #5
0
fint	ecdset_c( fchar string, fchar set, fint subsets[], fint *nsubs )
{
   ax_struct	*ax = NULL;			/* the ax struct */
   fint		count;				/* counter */
   fint		gerror = 0;			/* gds error return */
   fint		nc = 0;				/* number of characters shipped */
   fint		m, n;				/* loop counters */
   fint		naxis;				/* number of axes */
   fint		ndef = 0;			/* number of defined axes */
   fint		subset;				/* subset number */
   fint		toplevel = 0;			/* whole set */

   for ( n = 0; n < string.l; string.a[n++] = ' ' );
   if ( !tobool( gds_exist_c( set, &gerror ) ) || gerror ) {
      return( -1 );
   }
   naxis = gdsc_ndims_c( set, &toplevel );	/* dimension of set */
   subset = subsets[0];				/* first subset */
   for ( n = 0; n < naxis; n++ ) {		/* loop to get defined axes */
      fint	axnum;				/* the axis number */
      fint	grid;				/* grid number */

      axnum = n + 1;				/* current axis number */
      gerror = 0;				/* reset */
      grid = gdsc_grid_c( set, &axnum, &subset, &gerror );
      if ( gerror == 0 ) {			/* axis defined */
         fchar	ctype;				/* points to buffer for axis name */

         ax = realloc( ax, sizeof( ax_struct ) * ( ndef + 1 ) );
         if ( ax == NULL ) RETURN( -2 );	/* error */
         ax[ndef].axnum = axnum;		/* axis sequence number */
         ax[ndef].nstep = 0;
         ax[ndef].ngrid = 0;
         ax[ndef].grid = NULL;
         ax[ndef].step = NULL;
         ax[ndef].user = NULL;
         ctype.a = ax[ndef].ctype;
         ctype.l = MAXCTYPELEN;
         gdsc_name_c( ctype, set, &axnum, &gerror );
         ctype.a[nelc_c( ctype )] = 0;
         ndef++;
      }
   }
   gerror = 0;
   nc = nelc_c( set );
   if ( nc > string.l ) RETURN( -3 );
   strncpy( string.a, set.a, nc );
   if ( ndef == 0 ) {
      if ( (*nsubs) != 1 ) RETURN( -4 );
      RETURN( 0 );
   }
   for ( m = 0; m < ndef; m++ ) {
      ax[m].grid = calloc( (*nsubs), sizeof( fint ) );
      if ( ax[m].grid == NULL ) RETURN( -2 );
      ax[m].step = calloc( (*nsubs), sizeof( fint ) );
      if ( ax[m].step == NULL ) RETURN( -2 );
      ax[m].user = calloc( (*nsubs), sizeof( fint ) );
      if ( ax[m].user == NULL ) RETURN( -2 );
   }
   for ( n = 0; n < (*nsubs); n++ ) {
      for ( m = 0; m < ndef; m++ ) {
         ax[m].grid[n] = gdsc_grid_c( set, &ax[m].axnum, &subsets[n], &gerror );
      }
   }
   for ( m = 0; m < ndef; m++ ) {
      fint	okay;
      fint	n1, n2;
      fint	ns = 1;
      fint	nt = (*nsubs);

      ax[m].nsequ = ns;
      while ( ns < (*nsubs) ) {
         ns++;
         if ( ( (*nsubs) % ns ) == 0 ) {
            nt = (*nsubs) / ns;
            for ( okay = 1, n1 = 0; n1 < nt && okay; n1++ ) {
               for ( n2 = 1; n2 < ns && okay; n2++ ) {
                  okay = ( ax[m].grid[n1] == ax[m].grid[n1+nt*n2] );
               }
            }
            if ( okay ) ax[m].nsequ = ns;
         }
      }
   }
   qsort( ax, ndef, sizeof( ax_struct ), compar );
   if ( ( ax[0].nsequ == (*nsubs) ) && ( ndef > 1 ) ) {
      ax_struct	save;

      ax[0].nsequ = 1;
      save = ax[0];
      for ( n = 1; n < ndef; n++ ) {
         ax[n-1] = ax[n];
      }
      ax[ndef-1] = save;
   }
   count = (*nsubs);
   ax[0].ngrid = count / ax[0].nsequ;
   count /= ax[0].ngrid;
   for ( n = 0; n < ax[0].ngrid; n++ ) {
      ax[0].user[n] = ax[0].grid[n];
   }
   for ( m = 1; m < ndef; m++ ) {
      ax[m].ngrid = ax[m-1].nsequ / ax[m].nsequ;
      count /= ax[m].ngrid;
      if ( m == ( ndef - 1 ) && count != 1 ) ax[m].ngrid *= count;
      for ( n = 0; n < ax[m].ngrid; n++ ) {
         ax[m].user[n] = ax[m].grid[n*((*nsubs)/ax[m-1].nsequ)];
      }
   }
   for ( m = 0; m < ndef; m++ ) {
      char	text[MAXSTRINGLEN];
      int	l = 0;
      int	ndone = 0;

      SHIP( " " );
      SHIP( ax[m].ctype );
      for ( n = 1; n < ax[m].ngrid; n++ ) {
         ax[m].step[n] = ax[m].user[n] - ax[m].user[n-1];
      }
      while ( ndone < ax[m].ngrid ) {
         l += sprintf( &text[l], " %d", ax[m].user[ndone++] );
         for ( n = ndone+1; n < ax[m].ngrid && ax[m].step[ndone] == ax[m].step[n]; n++ );
         if ( n > ( ndone + 1 ) ) {
            if ( ax[m].step[ndone] == 1 ) {
               l += sprintf( &text[l], ":%d", ax[m].user[n-1] );
            } else if ( ax[m].step[ndone] == 0 ) {
               l += sprintf( &text[l], "::%d", n - ndone + 1 );
            } else {
               l += sprintf( &text[l], ":%d:%d", ax[m].user[n-1], ax[m].step[ndone] );
            }
            ndone = n;
         }
      }
      SHIP( text );
   }
   RETURN( 0 );
}