示例#1
0
static void
strike(struct ship *ship, struct ship *from)
{
    int points;

    if (ship->file->struck)
        return;
    Write(W_STRUCK, ship, 1, 0, 0, 0);
    points = ship->specs->pts + from->file->points;
    Write(W_POINTS, from, points, 0, 0, 0);
    unboard(ship, ship, 0);		/* all offense */
    unboard(ship, ship, 1);		/* all defense */
    switch (die()) {
    case 3:
    case 4:		/* ship may sink */
        Write(W_SINK, ship, 1, 0, 0, 0);
        break;
    case 5:
    case 6:		/* ship may explode */
        Write(W_EXPLODE, ship, 1, 0, 0, 0);
        break;
    }
    Writestr(W_SIGNAL, ship, "striking her colours!");
}
示例#2
0
void acceptmove(void)
{
    int ta;
    int ma;
    char af;
    int moved = 0;
    int vma, dir;
    char prompt[60];
    char buf[60], last = '\0';
    char *p;

    if (!mc->crew3 || snagged(ms) || !windspeed) {
	Msg("Unable to move");
	return;
    }

    ta = maxturns(ms, &af);
    ma = maxmove(ms, mf->dir, 0);
    sprintf(prompt, "move (%d,%c%d): ", ma, af ? '\'' : ' ', ta);
    sgetstr(prompt, buf, sizeof buf);
    dir = mf->dir;
    vma = ma;
    for (p = buf; *p; p++)
	switch (*p) {
	    case 'l':
		dir -= 2;
	    case 'r':
		if (++dir == 0)
		    dir = 8;
		else if (dir == 9)
		    dir = 1;
		if (last == 't') {
		    Msg("Ship can't turn that fast.");
		    *p-- = '\0';
		}
		last = 't';
		ma--;
		ta--;
		vma = min(ma, maxmove(ms, dir, 0));
		if ((ta < 0 && moved) || (vma < 0 && moved))
		    *p-- = '\0';
		break;
	    case 'b':
		ma--;
		vma--;
		last = 'b';
		if ((ta < 0 && moved) || (vma < 0 && moved))
		    *p-- = '\0';
		break;
	    case '0':
	    case 'd':
		*p-- = '\0';
		break;
	    case '\n':
		*p-- = '\0';
		break;
	    case '1':
	    case '2':
	    case '3':
	    case '4':
	    case '5':
	    case '6':
	    case '7':
		if (last == '0') {
		    Msg("Can't move that fast.");
		    *p-- = '\0';
		}
		last = '0';
		moved = 1;
		ma -= *p - '0';
		vma -= *p - '0';
		if ((ta < 0 && moved) || (vma < 0 && moved))
		    *p-- = '\0';
		break;
	    default:
		if (!isspace((unsigned char) *p)) {
		    Msg("Input error.");
		    *p-- = '\0';
		}
	}
    if ((ta < 0 && moved) || (vma < 0 && moved)
	|| (af && turnfirst(buf) && moved)) {
	Msg("Movement error.");
	if (ta < 0 && moved) {
	    if (mf->FS == 1) {
		Write(W_FS, ms, 0, 0, 0, 0);
		Msg("No hands to set full sails.");
	    }
	} else if (ma >= 0)
	    buf[1] = '\0';
    }
    if (af && !moved) {
	if (mf->FS == 1) {
	    Write(W_FS, ms, 0, 0, 0, 0);
	    Msg("No hands to set full sails.");
	}
    }
    if (*buf)
	strcpy(movebuf, buf);
    else
	strcpy(movebuf, "d");
    Writestr(W_MOVE, ms, movebuf);
    Msg("Helm: %s.", movebuf);
}
示例#3
0
static void
initialize(void)
{
    struct File *fp;
    struct ship *sp;
    char captain[80];
    char message[60];
    int load;
    int n;
    char *nameptr;
    int nat[NNATION];

    if (game < 0) {
        puts("Choose a scenario:\n");
        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? ");
        fflush(stdout);
        scanf("%d", &game);
        while (getchar() != '\n')
            ;
    }
    if (game < 0 || game >= NSCENE) {
        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) {
            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;

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

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

    if (hasdriver) {
        puts("Synchronizing with the other players...");
        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) {
            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);
            fflush(stdout);
            if (scanf("%d", &player) != 1 || player < 0
                    || player >= cc->vessels) {
                while (getchar() != '\n')
                    ;
                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)
            puts("That ship is taken.");
        else
            break;
    }

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

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

    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)
        strncpy(captain, nameptr, sizeof captain);
    else {
        printf("Your name, Captain? ");
        fflush(stdout);
        fgets(captain, sizeof captain, stdin);
        if (!*captain)
            strcpy(captain, "no name");
        else
            captain[strlen(captain) - 1] = '\0';
    }
    captain[sizeof captain - 1] = '\0';
    Writestr(W_CAPTAIN, ms, captain);
    for (n = 0; n < 2; n++) {
        char buf[10];

        printf("\nInitial broadside %s (grape, chain, round, double): ",
               n ? "right" : "left");
        fflush(stdout);
        scanf("%9s", 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();
    snprintf(message, sizeof message, "Captain %s assuming command",
             captain);
    Writestr(W_SIGNAL, ms, message);
    newturn(0);
}