move_robs.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* $NetBSD: move_robs.c,v 1.7 2003/08/07 09:37:37 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[] = "@(#)move_robs.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: move_robs.c,v 1.7 2003/08/07 09:37:37 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. # include "robots.h"
  39. /*
  40. * move_robots:
  41. * Move the robots around
  42. */
  43. void
  44. move_robots(was_sig)
  45. int was_sig;
  46. {
  47. COORD *rp;
  48. if (Real_time)
  49. signal(SIGALRM, move_robots);
  50. # ifdef DEBUG
  51. move(Min.y, Min.x);
  52. addch(inch());
  53. move(Max.y, Max.x);
  54. addch(inch());
  55. # endif /* DEBUG */
  56. for (rp = Robots; rp < &Robots[MAXROBOTS]; rp++) {
  57. if (rp->y < 0)
  58. continue;
  59. mvaddch(rp->y, rp->x, ' ');
  60. Field[rp->y][rp->x]--;
  61. rp->y += sign(My_pos.y - rp->y);
  62. rp->x += sign(My_pos.x - rp->x);
  63. if (rp->y <= 0)
  64. rp->y = 0;
  65. else if (rp->y >= Y_FIELDSIZE)
  66. rp->y = Y_FIELDSIZE - 1;
  67. if (rp->x <= 0)
  68. rp->x = 0;
  69. else if (rp->x >= X_FIELDSIZE)
  70. rp->x = X_FIELDSIZE - 1;
  71. Field[rp->y][rp->x]++;
  72. }
  73. Min.y = Y_FIELDSIZE;
  74. Min.x = X_FIELDSIZE;
  75. Max.y = 0;
  76. Max.x = 0;
  77. for (rp = Robots; rp < &Robots[MAXROBOTS]; rp++)
  78. if (rp->y < 0)
  79. continue;
  80. else if (rp->y == My_pos.y && rp->x == My_pos.x)
  81. Dead = TRUE;
  82. else if (Field[rp->y][rp->x] > 1) {
  83. mvaddch(rp->y, rp->x, HEAP);
  84. Scrap[Num_scrap++] = *rp;
  85. rp->y = -1;
  86. Num_robots--;
  87. if (Waiting)
  88. Wait_bonus++;
  89. add_score(ROB_SCORE);
  90. }
  91. else {
  92. mvaddch(rp->y, rp->x, ROBOT);
  93. if (rp->y < Min.y)
  94. Min.y = rp->y;
  95. if (rp->x < Min.x)
  96. Min.x = rp->x;
  97. if (rp->y > Max.y)
  98. Max.y = rp->y;
  99. if (rp->x > Max.x)
  100. Max.x = rp->x;
  101. }
  102. if (was_sig) {
  103. refresh();
  104. if (Dead || Num_robots <= 0)
  105. longjmp(End_move, 0);
  106. }
  107. # ifdef DEBUG
  108. standout();
  109. move(Min.y, Min.x);
  110. addch(inch());
  111. move(Max.y, Max.x);
  112. addch(inch());
  113. standend();
  114. # endif /* DEBUG */
  115. if (Real_time)
  116. alarm(3);
  117. }
  118. /*
  119. * add_score:
  120. * Add a score to the overall point total
  121. */
  122. void
  123. add_score(add)
  124. int add;
  125. {
  126. Score += add;
  127. move(Y_SCORE, X_SCORE);
  128. printw("%d", Score);
  129. }
  130. /*
  131. * sign:
  132. * Return the sign of the number
  133. */
  134. int
  135. sign(n)
  136. int n;
  137. {
  138. if (n < 0)
  139. return -1;
  140. else if (n > 0)
  141. return 1;
  142. else
  143. return 0;
  144. }