wizard.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* $NetBSD: wizard.c,v 1.11 2003/08/07 09:36:51 agc Exp $ */
  2. /*-
  3. * Copyright (c) 1991, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * The game adventure was originally written in Fortran by Will Crowther
  7. * and Don Woods. It was later translated to C and enhanced by Jim
  8. * Gillogly. This code is derived from software contributed to Berkeley
  9. * by Jim Gillogly at The Rand Corporation.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. */
  35. #include <sys/cdefs.h>
  36. #ifndef lint
  37. #if 0
  38. static char sccsid[] = "@(#)wizard.c 8.1 (Berkeley) 6/2/93";
  39. #else
  40. __RCSID("$NetBSD: wizard.c,v 1.11 2003/08/07 09:36:51 agc Exp $");
  41. #endif
  42. #endif /* not lint */
  43. /* Re-coding of advent in C: privileged operations */
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <stdlib.h>
  47. #include <time.h>
  48. #include "hdr.h"
  49. #include "extern.h"
  50. void
  51. datime(d, t)
  52. int *d, *t;
  53. {
  54. time_t tvec;
  55. struct tm *tptr;
  56. time(&tvec);
  57. tptr = localtime(&tvec);
  58. /* day since 1977 (mod leap) */
  59. *d = (tptr->tm_yday + 365 * (tptr->tm_year - 77)
  60. + (tptr->tm_year - 77) / 4 - (tptr->tm_year - 1) / 100
  61. + (tptr->tm_year + 299) / 400);
  62. /* bug: this will overflow in the year 2066 AD (with 16 bit int) */
  63. /* it will be attributed to Wm the C's millenial celebration */
  64. /* and minutes since midnite */
  65. *t = tptr->tm_hour * 60 + tptr->tm_min;
  66. } /* pretty painless */
  67. char magic[6];
  68. void
  69. poof()
  70. {
  71. strcpy(magic, DECR('d', 'w', 'a', 'r', 'f'));
  72. latncy = 45;
  73. }
  74. int
  75. Start()
  76. {
  77. int d, t, delay;
  78. datime(&d, &t);
  79. delay = (d - saveday) * 1440 + (t - savet); /* good for about a
  80. * month */
  81. if (delay >= latncy) {
  82. saved = -1;
  83. return (FALSE);
  84. }
  85. printf("This adventure was suspended a mere %d minute%s ago.",
  86. delay, delay == 1 ? "" : "s");
  87. if (delay <= latncy / 3) {
  88. mspeak(2);
  89. exit(0);
  90. }
  91. mspeak(8);
  92. if (!wizard()) {
  93. mspeak(9);
  94. exit(0);
  95. }
  96. saved = -1;
  97. return (FALSE);
  98. }
  99. int
  100. wizard()
  101. { /* not as complex as advent/10 (for now) */
  102. char *word, *x;
  103. if (!yesm(16, 0, 7))
  104. return (FALSE);
  105. mspeak(17);
  106. getin(&word, &x);
  107. if (!weq(word, magic)) {
  108. mspeak(20);
  109. return (FALSE);
  110. }
  111. mspeak(19);
  112. return (TRUE);
  113. }
  114. void
  115. ciao()
  116. {
  117. char *c;
  118. char fname[80];
  119. printf("What would you like to call the saved version?\n");
  120. /* XXX - should use fgetln to avoid arbitrary limit */
  121. for (c = fname; c < fname + sizeof fname - 1; c++) {
  122. int ch;
  123. ch = getchar();
  124. if (ch == '\n' || ch == EOF)
  125. break;
  126. *c = ch;
  127. }
  128. *c = 0;
  129. if (save(fname) != 0)
  130. return; /* Save failed */
  131. printf("To resume, say \"adventure %s\".\n", fname);
  132. printf("\"With these rooms I might now have been familiarly acquainted.\"\n");
  133. exit(0);
  134. }
  135. int
  136. ran(range)
  137. int range;
  138. {
  139. long i;
  140. i = rand() % range;
  141. return (i);
  142. }