play() { register struct ship *sp; for (;;) { switch (sgetch("~\b", (struct ship *)0, 0)) { case 'm': acceptmove(); break; case 's': acceptsignal(); break; case 'g': grapungrap(); break; case 'u': unfoulplayer(); break; case 'v': Signal("%s", (struct ship *)0, version); break; case 'b': acceptboard(); break; case 'f': acceptcombat(); break; case 'l': loadplayer(); break; case 'c': changesail(); break; case 'r': repair(); break; case 'B': Signal("'Hands to stations!'", (struct ship *)0); unboard(ms, ms, 1); /* cancel DBP's */ unboard(ms, ms, 0); /* cancel offense */ break; case '\f': centerview(); blockalarm(); draw_board(); draw_screen(); unblockalarm(); break; case 'L': mf->loadL = L_EMPTY; mf->loadR = L_EMPTY; mf->readyL = R_EMPTY; mf->readyR = R_EMPTY; Signal("Broadsides unloaded", (struct ship *)0); break; case 'q': Signal("Type 'Q' to quit", (struct ship *)0); break; case 'Q': leave(LEAVE_QUIT); break; case 'I': foreachship(sp) if (sp != ms) eyeball(sp); break; case 'i': if ((sp = closestenemy(ms, 0, 1)) == 0) Signal("No more ships left."); else eyeball(sp); break; case 'C': centerview(); blockalarm(); draw_view(); unblockalarm(); break; case 'U': upview(); blockalarm(); draw_view(); unblockalarm(); break; case 'D': case 'N': downview(); blockalarm(); draw_view(); unblockalarm(); break; case 'H': leftview(); blockalarm(); draw_view(); unblockalarm(); break; case 'J': rightview(); blockalarm(); draw_view(); unblockalarm(); break; case 'F': lookout(); break; case 'S': dont_adjust = !dont_adjust; blockalarm(); draw_turn(); unblockalarm(); break; } } }
void moveall() /* move all comp ships */ { struct ship *sp, *sq; /* r11, r10 */ int n; /* r9 */ int k, l; /* r8, r7 */ int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP]; char moved[NSHIP]; /* * first try to create moves for OUR ships */ foreachship(sp) { struct ship *closest; int ma, ta; char af; if (sp->file->captain[0] || sp->file->dir == 0) continue; if (!sp->file->struck && windspeed && !snagged(sp) && sp->specs->crew3) { ta = maxturns(sp, &af); ma = maxmove(sp, sp->file->dir, 0); closest = closestenemy(sp, 0, 0); if (closest == 0) *sp->file->movebuf = '\0'; else closeon(sp, closest, sp->file->movebuf, sizeof sp->file->movebuf, ta, ma, af); } else *sp->file->movebuf = '\0'; } /* * Then execute the moves for ALL ships (dead ones too), * checking for collisions and snags at each step. * The old positions are saved in row[], col[], dir[]. * At the end, we compare and write out the changes. */ n = 0; foreachship(sp) { if (snagged(sp)) (void) strlcpy(sp->file->movebuf, "d", sizeof sp->file->movebuf); else if (*sp->file->movebuf != 'd') (void) strlcat(sp->file->movebuf, "d", sizeof sp->file->movebuf); row[n] = sp->file->row; col[n] = sp->file->col; dir[n] = sp->file->dir; drift[n] = sp->file->drift; moved[n] = 0; n++; } /* * Now resolve collisions. * This is the tough part. */ for (k = 0; stillmoving(k); k++) { /* * Step once. * And propagate the nulls at the end of sp->file->movebuf. */ n = 0; foreachship(sp) { if (!sp->file->movebuf[k]) sp->file->movebuf[k+1] = '\0'; else if (sp->file->dir) step(sp->file->movebuf[k], sp, &moved[n]); n++; } /* * The real stuff. */ n = 0; foreachship(sp) { if (sp->file->dir == 0 || is_isolated(sp)) goto cont1; l = 0; foreachship(sq) { char snap = 0; if (sp == sq) goto cont2; if (sq->file->dir == 0) goto cont2; if (!push(sp, sq)) goto cont2; if (snagged2(sp, sq) && range(sp, sq) > 1) snap++; if (!range(sp, sq) && !fouled2(sp, sq)) { makesignal(sp, "collision with $$", sq); if (die() < 4) { makesignal(sp, "fouled with $$", sq); Write(W_FOUL, sp, l, 0, 0, 0); Write(W_FOUL, sq, n, 0, 0, 0); } snap++; } if (snap) { sp->file->movebuf[k + 1] = 0; sq->file->movebuf[k + 1] = 0; sq->file->row = sp->file->row - 1; if (sp->file->dir == 1 || sp->file->dir == 5) sq->file->col = sp->file->col - 1; else sq->file->col = sp->file->col; sq->file->dir = sp->file->dir; } cont2: l++; } cont1: n++; } } /* * Clear old moves. And write out new pos. */ n = 0; foreachship(sp) { if (sp->file->dir != 0) { *sp->file->movebuf = 0; if (row[n] != sp->file->row) Write(W_ROW, sp, sp->file->row, 0, 0, 0); if (col[n] != sp->file->col) Write(W_COL, sp, sp->file->col, 0, 0, 0); if (dir[n] != sp->file->dir) Write(W_DIR, sp, sp->file->dir, 0, 0, 0); if (drift[n] != sp->file->drift) Write(W_DRIFT, sp, sp->file->drift, 0, 0, 0); } n++; } }