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