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