You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

152 lines
3.5 KiB

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "types.h"
  4. #include "util.h"
  5. #include "Cable.h"
  6. #include "Computer.h"
  7. #include "Game.h"
  8. #include "Network.h"
  9. #include "OS.h"
  10. #include "Spark.h"
  11. #include "UI.h"
  12. struct Cable {
  13. int c1, c2; /* computers connected */
  14. int x1, y1, x2, y2; /* endpoints of line representing cable */
  15. int x, y; /* current location of spark */
  16. float fx, fy; /* needed for line drawing */
  17. int delay; /* how much time until spark leaves */
  18. int active; /* is spark moving and from which end */
  19. int index;
  20. };
  21. #define SWAP(x, y) do {int _t; _t = x; x = y; y = _t;} while(0)
  22. static void
  23. reverse(Cable *cable) {
  24. SWAP(cable->c1, cable->c2);
  25. SWAP(cable->x1, cable->x2);
  26. SWAP(cable->y1, cable->y2);
  27. }
  28. void
  29. Cable_setup(Cable **cablep) {
  30. Cable *cable;
  31. Computer *comp1, *comp2;
  32. int cwidth, cheight;
  33. cable = xalloc(sizeof *cable);
  34. cable->c1 = RAND(0, Network_num_computers() - 1);
  35. do {
  36. cable->c2 = RAND(0, Network_num_computers() - 1);
  37. } while (cable->c2 == cable->c1);
  38. cable->active = 0;
  39. cable->index = 0;
  40. cable->delay = SPARK_DELAY(Game_level());
  41. comp1 = Network_get_computer(cable->c1);
  42. comp2 = Network_get_computer(cable->c2);
  43. cwidth = Computer_width();
  44. cheight = Computer_height();
  45. cable->x1 = comp1->x + cwidth/3;
  46. cable->x2 = comp2->x + cwidth/3;
  47. cable->y1 = comp1->y + cheight/2;
  48. cable->y2 = comp2->y + cheight/2;
  49. *cablep = cable;
  50. }
  51. void
  52. Cable_draw(Cable *cable) {
  53. UI_draw_line(cable->x1, cable->y1, cable->x2, cable->y2);
  54. if (cable->active) {
  55. int rx = cable->x - Spark_width()/2;
  56. int ry = cable->y - Spark_height()/2;
  57. Spark_draw(rx, ry, cable->index);
  58. }
  59. }
  60. void
  61. Cable_update(Cable *cable) {
  62. Computer *comp1, *comp2;
  63. comp1 = Network_get_computer(cable->c1);
  64. comp2 = Network_get_computer(cable->c2);
  65. if (cable->active) {
  66. if ((comp1->os == OS_WINGDOWS) == (comp2->os == OS_WINGDOWS))
  67. cable->active = 0;
  68. else if (comp1->os == OS_WINGDOWS || comp2->os == OS_WINGDOWS) {
  69. int xdist, ydist;
  70. float sx, sy;
  71. if (comp2->os == OS_WINGDOWS)
  72. reverse(cable);
  73. xdist = cable->x2 - cable->x;
  74. ydist = cable->y2 - cable->y;
  75. sx = xdist >= 0 ? 1.0 : -1.0;
  76. sy = ydist >= 0 ? 1.0 : -1.0;
  77. xdist = abs(xdist);
  78. ydist = abs(ydist);
  79. if (xdist == 0 && ydist == 0) {
  80. if (!comp2->busy) {
  81. int counter;
  82. if (comp2->os == OS_OFF)
  83. counter = NETWORK_COUNTER_OFF;
  84. else
  85. counter = NETWORK_COUNTER_BASE;
  86. Network_inc_counter(counter, -1);
  87. Network_inc_counter(NETWORK_COUNTER_WIN,
  88. 1);
  89. comp2->os = OS_WINGDOWS;
  90. }
  91. cable->active = 0;
  92. }
  93. else if (MAX(xdist, ydist) < SPARK_SPEED) {
  94. cable->x = cable->x2;
  95. cable->y = cable->y2;
  96. }
  97. else {
  98. cable->fx+=(xdist*SPARK_SPEED*sx)/(xdist+ydist);
  99. cable->fy+=(ydist*SPARK_SPEED*sy)/(xdist+ydist);
  100. cable->x = (int)cable->fx;
  101. cable->y = (int)cable->fy;
  102. }
  103. cable->index = 1 - cable->index;
  104. }
  105. }
  106. else {
  107. if ((comp1->os == OS_WINGDOWS) == (comp2->os == OS_WINGDOWS))
  108. ;
  109. else if (comp1->os == OS_WINGDOWS || comp2->os == OS_WINGDOWS) {
  110. cable->active = 1;
  111. cable->delay = SPARK_DELAY(Game_level());
  112. if (comp2->os == OS_WINGDOWS)
  113. reverse(cable);
  114. cable->x = cable->x1;
  115. cable->fx = cable->x1;
  116. cable->y = cable->y1;
  117. cable->fy = cable->y1;
  118. }
  119. }
  120. }
  121. int
  122. Cable_onspark(Cable *cable, int locx, int locy) {
  123. if (!cable->active)
  124. return 0;
  125. return (abs(locx - cable->x) < Spark_width() &&
  126. abs(locy - cable->y) < Spark_height());
  127. }
  128. void
  129. Cable_reset(Cable *cable) {
  130. cable->active = 0;
  131. cable->delay = SPARK_DELAY(Game_level());
  132. }