void loadscores(void) { Sint4 p=0,i,x; readscores(); if (gauntlet) p=111; if (diggers==2) p+=222; if (scorebuf[p++]!='s') for (i=0;i<11;i++) { scorehigh[i+1]=0; strcpy(scoreinit[i],"..."); } else for (i=1;i<11;i++) { for (x=0;x<3;x++) scoreinit[i][x]=scorebuf[p++]; p+=2; for (x=0;x<6;x++) highbuf[x]=scorebuf[p++]; scorehigh[i+1]=atol(highbuf); } }
int main(int argc, char *argv[]) { struct sigaction sa; int ch, i; if (pledge("stdio rpath wpath cpath tty", NULL) == -1) err(1, "pledge"); #ifdef LOGGING const char *home; home = getenv("HOME"); if (home == NULL || *home == '\0') err(1, "getenv"); snprintf(logpath, sizeof(logpath), "%s/%s", home, ".snake.log"); logfile = fopen(logpath, "a"); #endif while ((ch = getopt(argc, argv, "hl:stw:")) != -1) switch ((char)ch) { case 'w': /* width */ ccnt = strtonum(optarg, 1, INT_MAX, NULL); break; case 'l': /* length */ lcnt = strtonum(optarg, 1, INT_MAX, NULL); break; case 's': /* score */ if (readscores(0)) snscore(0); else printf("no scores so far\n"); return 0; break; case 't': /* slow terminal */ fast = 0; break; case 'h': default: fprintf(stderr, "usage: %s [-st] [-l length] " "[-w width]\n", getprogname()); return 1; } readscores(1); penalty = loot = 0; initscr(); #ifdef KEY_LEFT keypad(stdscr, TRUE); #endif nonl(); cbreak(); noecho(); if (!lcnt || lcnt > LINES - 2) lcnt = LINES - 2; if (!ccnt || ccnt > COLS - 3) ccnt = COLS - 3; i = lcnt < ccnt ? lcnt : ccnt; if (i < 4) { endwin(); errx(1, "screen too small for a fair game."); } /* * chunk is the amount of money the user gets for each $. * The formula below tries to be fair for various screen sizes. * We only pay attention to the smaller of the 2 edges, since * that seems to be the bottleneck. * This formula is a hyperbola which includes the following points: * (24, $25) (original scoring algorithm) * (12, $40) (experimentally derived by the "feel") * (48, $15) (a guess) * This will give a 4x4 screen $99/shot. We don't allow anything * smaller than 4x4 because there is a 3x3 game where you can win * an infinite amount of money. */ if (i < 12) i = 12; /* otherwise it isn't fair */ /* * Compensate for border. This really changes the game since * the screen is two squares smaller but we want the default * to be $25, and the high scores on small screens were a bit * much anyway. */ i += 2; chunk = (675.0 / (i + 6)) + 2.5; /* min screen edge */ memset(&sa, 0, sizeof sa); sigemptyset(&sa.sa_mask); sa.sa_handler = stop; sigaction(SIGINT, &sa, NULL); snrand(&finish); snrand(&you); snrand(&money); snrand(&snake[0]); for (i = 1; i < 6; i++) chase(&snake[i], &snake[i - 1]); setup(); mainloop(); return 0; }