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.
 
 
 
 
 
 

121 lines
2.2 KiB

  1. #include <stdlib.h>
  2. #include <stdio.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 "UI.h"
  11. #define STD_MAX_COMPUTERS 20
  12. static Computer *computers;
  13. static int ncomputers;
  14. static Cable **cables;
  15. static int ncables;
  16. static int counters[NETWORK_COUNTER_MAX + 1]; /* number in each state */
  17. static int
  18. on(int level) {
  19. int normal = MIN(8 + level, STD_MAX_COMPUTERS);
  20. return (int)(normal * Game_scale(2));
  21. }
  22. /* sets up network for each level */
  23. void
  24. Network_setup() {
  25. int i;
  26. ncomputers = on(Game_level());
  27. if (computers != NULL)
  28. free(computers);
  29. if (cables != NULL) {
  30. for (i = 0; i < ncables; i++)
  31. if (cables[i] != NULL)
  32. free(cables[i]);
  33. free(cables);
  34. }
  35. computers = xalloc(ncomputers * sizeof(Computer));
  36. for (i = 0; i < ncomputers; i++)
  37. if (!Computer_setup(&computers[i], i)) {
  38. ncomputers = i - 1;
  39. break;
  40. }
  41. counters[NETWORK_COUNTER_OFF] = 0;
  42. counters[NETWORK_COUNTER_BASE] = ncomputers;
  43. counters[NETWORK_COUNTER_WIN] = 0;
  44. ncables = MIN(Game_level(), ncomputers/2);
  45. cables = xalloc(ncables * sizeof(Cable *));
  46. for (i = 0; i < ncables; i++)
  47. Cable_setup(&cables[i]);
  48. }
  49. /* redraws the computers at their location with the proper image */
  50. void
  51. Network_draw () {
  52. int i;
  53. for (i = 0; i < ncables; i++)
  54. Cable_draw(cables[i]);
  55. for (i = 0; i < ncomputers; i++)
  56. Computer_draw(&computers[i]);
  57. }
  58. void
  59. Network_update () {
  60. int i;
  61. for (i = 0; i < ncables; i++)
  62. Cable_update(cables[i]);
  63. }
  64. void
  65. Network_toasters () {
  66. int i;
  67. for (i = 0; i < ncomputers; i++) {
  68. computers[i].type = COMPUTER_TOASTER;
  69. computers[i].os = OS_OFF;
  70. }
  71. ncables = 0;
  72. }
  73. Computer *
  74. Network_get_computer(int index) {
  75. return &computers[index];
  76. }
  77. int
  78. Network_num_computers() {
  79. return ncomputers;
  80. }
  81. Cable *
  82. Network_get_cable(int index) {
  83. return cables[index];
  84. }
  85. int
  86. Network_num_cables() {
  87. return ncables;
  88. }
  89. void
  90. Network_clear_stray(Bill *bill) {
  91. int i;
  92. for (i = 0; i < ncomputers; i++) {
  93. if (computers[i].stray == bill)
  94. computers[i].stray = NULL;
  95. }
  96. }
  97. void
  98. Network_inc_counter(int counter, int val) {
  99. counters[counter] += val;
  100. }
  101. int
  102. Network_get_counter(int counter) {
  103. return counters[counter];
  104. }