main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* $NetBSD: main.c,v 1.21 2004/11/05 21:30:32 dsl Exp $ */
  2. /*
  3. * Copyright (c) 1980, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
  33. The Regents of the University of California. All rights reserved.\n");
  34. #endif /* not lint */
  35. #ifndef lint
  36. #if 0
  37. static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93";
  38. #else
  39. __RCSID("$NetBSD: main.c,v 1.21 2004/11/05 21:30:32 dsl Exp $");
  40. #endif
  41. #endif /* not lint */
  42. # include "robots.h"
  43. int main(int, char **);
  44. extern const char *Scorefile;
  45. extern int Max_per_uid;
  46. int
  47. main(ac, av)
  48. int ac;
  49. char **av;
  50. {
  51. const char *sp;
  52. bool bad_arg;
  53. bool show_only;
  54. int score_wfd; /* high score writable file descriptor */
  55. int score_err = 0; /* hold errno from score file open */
  56. score_wfd = open(Scorefile, O_RDWR);
  57. if (score_wfd < 0)
  58. score_err = errno;
  59. else if (score_wfd < 3)
  60. exit(1);
  61. /* Revoke setgid privileges */
  62. setregid(getgid(), getgid());
  63. show_only = FALSE;
  64. Num_games = 1;
  65. if (ac > 1) {
  66. bad_arg = FALSE;
  67. for (++av; ac > 1 && *av[0]; av++, ac--)
  68. if (av[0][0] != '-')
  69. if (isdigit((unsigned char)av[0][0]))
  70. Max_per_uid = atoi(av[0]);
  71. else {
  72. Scorefile = av[0];
  73. if (score_wfd >= 0)
  74. close(score_wfd);
  75. score_wfd = open(Scorefile, O_RDWR);
  76. if (score_wfd < 0)
  77. score_err = errno;
  78. # ifdef FANCY
  79. sp = strrchr(Scorefile, '/');
  80. if (sp == NULL)
  81. sp = Scorefile;
  82. if (strcmp(sp, "pattern_roll") == 0)
  83. Pattern_roll = TRUE;
  84. else if (strcmp(sp, "stand_still") == 0)
  85. Stand_still = TRUE;
  86. if (Pattern_roll || Stand_still)
  87. Teleport = TRUE;
  88. # endif
  89. }
  90. else
  91. for (sp = &av[0][1]; *sp; sp++)
  92. switch (*sp) {
  93. case 'A':
  94. Auto_bot = TRUE;
  95. break;
  96. case 's':
  97. show_only = TRUE;
  98. break;
  99. case 'r':
  100. Real_time = TRUE;
  101. break;
  102. case 'a':
  103. Start_level = 4;
  104. break;
  105. case 'n':
  106. Num_games++;
  107. break;
  108. case 'j':
  109. Jump = TRUE;
  110. break;
  111. case 't':
  112. Teleport = TRUE;
  113. break;
  114. default:
  115. fprintf(stderr, "robots: unknown option: %c\n", *sp);
  116. bad_arg = TRUE;
  117. break;
  118. }
  119. if (bad_arg) {
  120. exit(1);
  121. /* NOTREACHED */
  122. }
  123. }
  124. if (show_only) {
  125. show_score();
  126. exit(0);
  127. /* NOTREACHED */
  128. }
  129. if (score_wfd < 0) {
  130. errno = score_err;
  131. warn("%s", Scorefile);
  132. warnx("High scores will not be recorded!");
  133. sleep(2);
  134. }
  135. initscr();
  136. signal(SIGINT, quit);
  137. cbreak();
  138. noecho();
  139. nonl();
  140. if (LINES != Y_SIZE || COLS != X_SIZE) {
  141. if (LINES < Y_SIZE || COLS < X_SIZE) {
  142. endwin();
  143. printf("Need at least a %dx%d screen\n",
  144. Y_SIZE, X_SIZE);
  145. exit(1);
  146. }
  147. delwin(stdscr);
  148. stdscr = newwin(Y_SIZE, X_SIZE, 0, 0);
  149. }
  150. srand(getpid());
  151. if (Real_time)
  152. signal(SIGALRM, move_robots);
  153. do {
  154. while (Num_games--) {
  155. init_field();
  156. for (Level = Start_level; !Dead; Level++) {
  157. make_level();
  158. play_level();
  159. if (Auto_bot)
  160. sleep(1);
  161. }
  162. move(My_pos.y, My_pos.x);
  163. printw("AARRrrgghhhh....");
  164. refresh();
  165. if (Auto_bot)
  166. sleep(1);
  167. score(score_wfd);
  168. if (Auto_bot)
  169. sleep(1);
  170. refresh();
  171. }
  172. Num_games = 1;
  173. } while (!Auto_bot && another());
  174. quit(0);
  175. /* NOTREACHED */
  176. return(0);
  177. }
  178. /*
  179. * quit:
  180. * Leave the program elegantly.
  181. */
  182. void
  183. quit(dummy)
  184. int dummy __attribute__((__unused__));
  185. {
  186. endwin();
  187. exit(0);
  188. /* NOTREACHED */
  189. }
  190. /*
  191. * another:
  192. * See if another game is desired
  193. */
  194. bool
  195. another()
  196. {
  197. int y;
  198. #ifdef FANCY
  199. if ((Stand_still || Pattern_roll) && !Newscore)
  200. return TRUE;
  201. #endif
  202. if (query("Another game?")) {
  203. if (Full_clear) {
  204. for (y = 1; y <= Num_scores; y++) {
  205. move(y, 1);
  206. clrtoeol();
  207. }
  208. refresh();
  209. }
  210. return TRUE;
  211. }
  212. return FALSE;
  213. }