Annotation of 43BSDReno/games/sail/pl_main.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1983 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)pl_main.c  5.4 (Berkeley) 6/1/90";
                     22: #endif /* not lint */
                     23: 
                     24: #include "player.h"
                     25: #include <sys/types.h>
                     26: #include <sys/wait.h>
                     27: 
                     28: int choke(), child();
                     29: 
                     30: /*ARGSUSED*/
                     31: pl_main()
                     32: {
                     33: 
                     34:        if (!SCREENTEST()) {
                     35:                printf("Can't sail on this terminal.\n");
                     36:                exit(1);
                     37:        }
                     38:        initialize();
                     39:        Signal("Aye aye, Sir", (struct ship *)0);
                     40:        play();
                     41:        return 0;                       /* for lint,  play() never returns */
                     42: }
                     43: 
                     44: initialize()
                     45: {
                     46:        register struct File *fp;
                     47:        register struct ship *sp;
                     48:        char captain[80];
                     49:        char message[60];
                     50:        int load;
                     51:        register int n;
                     52:        char *nameptr;
                     53:        int nat[NNATION];
                     54: 
                     55:        if (game < 0) {
                     56:                (void) puts("Choose a scenario:\n");
                     57:                (void) puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
                     58:                for (n = 0; n < NSCENE; n++) {
                     59:                        /* ( */
                     60:                        printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
                     61:                                sync_exists(n) ? "YES" : "no",
                     62:                                scene[n].name);
                     63:                }
                     64: reprint:
                     65:                printf("\nScenario number? ");
                     66:                (void) fflush(stdout);
                     67:                (void) scanf("%d", &game);
                     68:                while (getchar() != '\n')
                     69:                        ;
                     70:        }
                     71:        if (game < 0 || game >= NSCENE) {
                     72:                (void) puts("Very funny.");
                     73:                exit(1);
                     74:        }
                     75:        cc = &scene[game];
                     76:        ls = SHIP(cc->vessels);
                     77: 
                     78:        for (n = 0; n < NNATION; n++)
                     79:                nat[n] = 0;
                     80:        foreachship(sp) {
                     81:                if (sp->file == NULL &&
                     82:                    (sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
                     83:                        (void) puts("OUT OF MEMORY");
                     84:                        exit(1);
                     85:                }
                     86:                sp->file->index = sp - SHIP(0);
                     87:                sp->file->stern = nat[sp->nationality]++;
                     88:                sp->file->dir = sp->shipdir;
                     89:                sp->file->row = sp->shiprow;
                     90:                sp->file->col = sp->shipcol;
                     91:        }
                     92:        windspeed = cc->windspeed;
                     93:        winddir = cc->winddir;
                     94: 
                     95:        (void) signal(SIGHUP, choke);
                     96:        (void) signal(SIGINT, choke);
                     97: 
                     98:        hasdriver = sync_exists(game);
                     99:        if (sync_open() < 0) {
                    100:                perror("sail: syncfile");
                    101:                exit(1);
                    102:        }
                    103: 
                    104:        if (hasdriver) {
                    105:                (void) puts("Synchronizing with the other players...");
                    106:                (void) fflush(stdout);
                    107:                if (Sync() < 0)
                    108:                        leave(LEAVE_SYNC);
                    109:        }
                    110:        for (;;) {
                    111:                foreachship(sp)
                    112:                        if (sp->file->captain[0] == 0 && !sp->file->struck
                    113:                            && sp->file->captured == 0)
                    114:                                break;
                    115:                if (sp >= ls) {
                    116:                        (void) puts("All ships taken in that scenario.");
                    117:                        foreachship(sp)
                    118:                                free((char *)sp->file);
                    119:                        sync_close(0);
                    120:                        people = 0;
                    121:                        goto reprint;
                    122:                }
                    123:                if (randomize) {
                    124:                        player = sp - SHIP(0);
                    125:                } else {
                    126:                        printf("%s\n\n", cc->name);
                    127:                        foreachship(sp)
                    128:                                printf("  %2d:  %-10s %-15s  (%-2d pts)   %s\n",
                    129:                                        sp->file->index,
                    130:                                        countryname[sp->nationality],
                    131:                                        sp->shipname,
                    132:                                        sp->specs->pts,
                    133:                                        saywhat(sp, 1));
                    134:                        printf("\nWhich ship (0-%d)? ", cc->vessels-1);
                    135:                        (void) fflush(stdout);
                    136:                        if (scanf("%d", &player) != 1 || player < 0
                    137:                            || player >= cc->vessels) {
                    138:                                while (getchar() != '\n')
                    139:                                        ;
                    140:                                (void) puts("Say what?");
                    141:                                player = -1;
                    142:                        } else
                    143:                                while (getchar() != '\n')
                    144:                                        ;
                    145:                }
                    146:                if (player < 0)
                    147:                        continue;
                    148:                if (Sync() < 0)
                    149:                        leave(LEAVE_SYNC);
                    150:                fp = SHIP(player)->file;
                    151:                if (fp->captain[0] || fp->struck || fp->captured != 0)
                    152:                        (void) puts("That ship is taken.");
                    153:                else
                    154:                        break;
                    155:        }
                    156: 
                    157:        ms = SHIP(player);
                    158:        mf = ms->file;
                    159:        mc = ms->specs;
                    160: 
                    161:        Write(W_BEGIN, ms, 0, 0, 0, 0, 0);
                    162:        if (Sync() < 0)
                    163:                leave(LEAVE_SYNC);
                    164: 
                    165:        (void) signal(SIGCHLD, child);
                    166:        if (!hasdriver)
                    167:                switch (fork()) {
                    168:                case 0:
                    169:                        longjmp(restart, MODE_DRIVER);
                    170:                        /*NOTREACHED*/
                    171:                case -1:
                    172:                        perror("fork");
                    173:                        leave(LEAVE_FORK);
                    174:                        break;
                    175:                default:
                    176:                        hasdriver++;
                    177:                }
                    178: 
                    179:        printf("Your ship is the %s, a %d gun %s (%s crew).\n",
                    180:                ms->shipname, mc->guns, classname[mc->class],
                    181:                qualname[mc->qual]);
                    182:        if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
                    183:                (void) strncpy(captain, nameptr, sizeof captain);
                    184:        else {
                    185:                (void) printf("Your name, Captain? ");
                    186:                (void) fflush(stdout);
                    187:                (void) gets(captain);
                    188:                if (!*captain)
                    189:                        (void) strcpy(captain, "no name");
                    190:        }
                    191:        captain[sizeof captain - 1] = '\0';
                    192:        Write(W_CAPTAIN, ms, 1, (int)captain, 0, 0, 0);
                    193:        for (n = 0; n < 2; n++) {
                    194:                char buf[10];
                    195: 
                    196:                printf("\nInitial broadside %s (grape, chain, round, double): ",
                    197:                        n ? "right" : "left");
                    198:                (void) fflush(stdout);
                    199:                (void) scanf("%s", buf);
                    200:                switch (*buf) {
                    201:                case 'g':
                    202:                        load = L_GRAPE;
                    203:                        break;
                    204:                case 'c':
                    205:                        load = L_CHAIN;
                    206:                        break;
                    207:                case 'r':
                    208:                        load = L_ROUND;
                    209:                        break;
                    210:                case 'd':
                    211:                        load = L_DOUBLE;
                    212:                        break;
                    213:                default:
                    214:                        load = L_ROUND;
                    215:                }
                    216:                if (n) {
                    217:                        mf->loadR = load;
                    218:                        mf->readyR = R_LOADED|R_INITIAL;
                    219:                } else {
                    220:                        mf->loadL = load;
                    221:                        mf->readyL = R_LOADED|R_INITIAL;
                    222:                }
                    223:        }
                    224: 
                    225:        initscreen();
                    226:        draw_board();
                    227:        (void) sprintf(message, "Captain %s assuming command", captain);
                    228:        Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0);
                    229:        newturn();
                    230: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.