schedule.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* $NetBSD: schedule.c,v 1.5 2003/08/07 09:37:53 agc 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. #if 0
  33. static char sccsid[] = "@(#)schedule.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: schedule.c,v 1.5 2003/08/07 09:37:53 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <stdio.h>
  39. #include <math.h>
  40. #include <err.h>
  41. #include "trek.h"
  42. /*
  43. ** SCHEDULE AN EVENT
  44. **
  45. ** An event of type 'type' is scheduled for time NOW + 'offset'
  46. ** into the first available slot. 'x', 'y', and 'z' are
  47. ** considered the attributes for this event.
  48. **
  49. ** The address of the slot is returned.
  50. */
  51. struct event *schedule(type, offset, x, y, z)
  52. int type;
  53. double offset;
  54. char x, y;
  55. char z;
  56. {
  57. struct event *e;
  58. int i;
  59. double date;
  60. date = Now.date + offset;
  61. for (i = 0; i < MAXEVENTS; i++)
  62. {
  63. e = &Event[i];
  64. if (e->evcode)
  65. continue;
  66. /* got a slot */
  67. # ifdef xTRACE
  68. if (Trace)
  69. printf("schedule: type %d @ %.2f slot %d parm %d %d %d\n",
  70. type, date, i, x, y, z);
  71. # endif
  72. e->evcode = type;
  73. e->date = date;
  74. e->x = x;
  75. e->y = y;
  76. e->systemname = z;
  77. Now.eventptr[type] = e;
  78. return (e);
  79. }
  80. errx(1, "Cannot schedule event %d parm %d %d %d", type, x, y, z);
  81. }
  82. /*
  83. ** RESCHEDULE AN EVENT
  84. **
  85. ** The event pointed to by 'e' is rescheduled to the current
  86. ** time plus 'offset'.
  87. */
  88. void
  89. reschedule(e1, offset)
  90. struct event *e1;
  91. double offset;
  92. {
  93. double date;
  94. struct event *e;
  95. e = e1;
  96. date = Now.date + offset;
  97. e->date = date;
  98. # ifdef xTRACE
  99. if (Trace)
  100. printf("reschedule: type %d parm %d %d %d @ %.2f\n",
  101. e->evcode, e->x, e->y, e->systemname, date);
  102. # endif
  103. return;
  104. }
  105. /*
  106. ** UNSCHEDULE AN EVENT
  107. **
  108. ** The event at slot 'e' is deleted.
  109. */
  110. void
  111. unschedule(e1)
  112. struct event *e1;
  113. {
  114. struct event *e;
  115. e = e1;
  116. # ifdef xTRACE
  117. if (Trace)
  118. printf("unschedule: type %d @ %.2f parm %d %d %d\n",
  119. e->evcode, e->date, e->x, e->y, e->systemname);
  120. # endif
  121. Now.eventptr[e->evcode & E_EVENT] = 0;
  122. e->date = 1e50;
  123. e->evcode = 0;
  124. return;
  125. }
  126. /*
  127. ** Abreviated schedule routine
  128. **
  129. ** Parameters are the event index and a factor for the time
  130. ** figure.
  131. */
  132. struct event *xsched(ev1, factor, x, y, z)
  133. int ev1;
  134. int factor;
  135. int x, y, z;
  136. {
  137. int ev;
  138. ev = ev1;
  139. return (schedule(ev, -Param.eventdly[ev] * Param.time * log(franf()) / factor, x, y, z));
  140. }
  141. /*
  142. ** Simplified reschedule routine
  143. **
  144. ** Parameters are the event index, the initial date, and the
  145. ** division factor. Look at the code to see what really happens.
  146. */
  147. void
  148. xresched(e1, ev1, factor)
  149. struct event *e1;
  150. int ev1;
  151. int factor;
  152. {
  153. int ev;
  154. struct event *e;
  155. ev = ev1;
  156. e = e1;
  157. reschedule(e, -Param.eventdly[ev] * Param.time * log(franf()) / factor);
  158. }