int otto(int y, int x, char face, char *buf, size_t buflen) { int i; if (usleep(Otto_pause) < 0) panic("usleep"); /* save away parameters so other functions may use/update info */ switch (face) { case '^': facing = NORTH; break; case '<': facing = WEST; break; case 'v': facing = SOUTH; break; case '>': facing = EAST; break; default: panic("unknown face"); } row = y; col = x; been_there[row][col] |= 1 << facing; /* initially no commands to be sent */ comlen = 0; /* find something to do */ look_around(); for (i = 0; i < NUMDIRECTIONS; i++) { if (strchr(OPPONENT, flbr[i].what) != NULL) { attack(i, &flbr[i]); memset(been_there, 0, sizeof been_there); goto done; } } if (strchr(SHOTS, bitem.what) != NULL && !(bitem.what & ON_SIDE)) { duck(BACK); memset(been_there, 0, sizeof been_there); } else if (go_for_ammo(BOOT_PAIR)) { memset(been_there, 0, sizeof been_there); } else if (go_for_ammo(BOOT)) { memset(been_there, 0, sizeof been_there); } else if (go_for_ammo(GMINE)) memset(been_there, 0, sizeof been_there); else if (go_for_ammo(MINE)) memset(been_there, 0, sizeof been_there); else wander(); done: if (comlen) { if (comlen > buflen) panic("not enough buffer space"); memcpy(buf, command, comlen); } return comlen; }
/** * Implements the game logic of a otto-matic player located at a * given coordinate with a given orientation. * @param[in] y A coordinate. * @param[in] x A coordinate. * @param[in] face The orientation of the player. * [PSR] */ void otto(int y,int x,char face) { int i; struct sigaction handler, old_handler; /* Added to substitute deprecated signals functions. [PSR] */ sigset_t old_mask, sig_mask; /* Added to substitute deprecated signals functions. [PSR] */ bool done; # ifdef DEBUG if (debug == NULL) { debug = fopen("bug", "w"); setbuf(debug, NULL); } fprintf(debug, "\n%c(%d,%d)", face, y, x); # endif handler.sa_handler=&empty_handler; /* Added to substitute deprecated signals functions. [PSR] */ sigaction(SIGALRM, &handler, &old_handler);/* Added to substitute deprecated signals functions. [PSR] */ sigemptyset(&sig_mask);/* Added to substitute deprecated signals functions. [PSR] */ sigaddset(&sig_mask, SIGALRM);/* Added to substitute deprecated signals functions. [PSR] */ sigprocmask(SIG_BLOCK, &sig_mask, &old_mask);/* Added to substitute deprecated signals functions. [PSR] */ setitimer(ITIMER_REAL, &pause_time, NULL);/* Added to substitute deprecated signals functions. [PSR] */ sigsuspend(&old_mask);/* Added to substitute deprecated signals functions. [PSR] */ sigprocmask(SIG_SETMASK, &old_mask, NULL);/* Added to substitute deprecated signals functions. [PSR] */ /* save away parameters so other functions may use/update info */ switch (face) { case '^': facing = NORTH; break; case '<': facing = WEST; break; case 'v': facing = SOUTH; break; case '>': facing = EAST; break; default: abort(); } row = y; col = x; been_there[row][col] |= 1 << facing; /* initially no commands to be sent */ comlen = 0; /* find something to do */ look_around(); done=false; for (i = 0; i < NUMDIRECTIONS; i++) { if (strchr(OPPONENT, flbr[i].what) != NULL) { attack(i, &flbr[i]); memset(been_there, 0, sizeof been_there); done=true; break; } } if(!done) { if (strchr(SHOTS, bitem.what) != NULL && !(bitem.what & ON_SIDE)) { duck(BACK); memset(been_there, 0, sizeof been_there); # ifdef BOOTS } else if (go_for_ammo(BOOT_PAIR)) { memset(been_there, 0, sizeof been_there); } else if (go_for_ammo(BOOT)) { memset(been_there, 0, sizeof been_there); # endif } else if (go_for_ammo(GMINE)) { memset(been_there, 0, sizeof been_there); } else if (go_for_ammo(MINE)) { memset(been_there, 0, sizeof been_there); } else { wander(); } } write_and_push(main_socket, command, comlen); otto_count += comlen; # ifdef DEBUG (void) fwrite(command, 1, comlen, debug); # endif }