-
Notifications
You must be signed in to change notification settings - Fork 3
/
Horde.c
175 lines (151 loc) · 3.18 KB
/
Horde.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdlib.h>
#include "types.h"
#include "util.h"
#include "Bill.h"
#include "Computer.h"
#include "Game.h"
#include "Horde.h"
#include "Network.h"
#include "UI.h"
static Bill *alive, *strays;
static int counters[HORDE_COUNTER_MAX + 1];
#define MAX_BILLS 100 /* max Bills per level */
#define UNLINK(bill, list) \
do { \
if ((bill)->next != NULL) \
(bill)->next->prev = (bill)->prev; \
if ((bill)->prev != NULL) \
(bill)->prev->next = (bill)->next; \
else if ((bill) == list) \
(list) = (bill)->next; \
(bill)->prev = NULL; \
(bill)->next = NULL; \
} while (0)
#define PREPEND(bill, list) \
do { \
(bill)->next = (list); \
if ((list) != NULL) \
(list)->prev = (bill); \
(list) = (bill); \
} while (0)
static int
on(unsigned int lev) {
int perlevel = (int)((8 + 3 * lev) * Game_scale(2));
return MIN(perlevel, MAX_BILLS);
}
static int
max_at_once(unsigned int lev) {
return MIN(2 + lev / 4, 12);
}
static int
between(unsigned int lev) {
return MAX(14 - lev / 3, 10);
}
/* Launches Bills whenever called */
static void
launch(int max) {
Bill *bill;
int n;
int off_screen = counters[HORDE_COUNTER_OFF];
if (max == 0 || off_screen == 0)
return;
n = RAND(1, MIN(max, off_screen));
for (; n > 0; n--) {
Bill_enter(&bill);
PREPEND(bill, alive);
}
}
void
Horde_setup() {
Bill *bill;
while (alive != NULL) {
bill = alive;
UNLINK(bill, alive);
free(bill);
}
while (strays != NULL) {
bill = strays;
UNLINK(bill, strays);
free(bill);
}
counters[HORDE_COUNTER_OFF] = on(Game_level());
counters[HORDE_COUNTER_ON] = 0;
}
void
Horde_update(int iteration) {
Bill *bill, *next;
int level = Game_level();
if (iteration % between(level) == 0)
launch(max_at_once(level));
for (bill = alive; bill != NULL; bill = next) {
next = bill->next;
Bill_update(bill);
}
}
void
Horde_draw() {
Bill *bill;
for (bill = strays; bill != NULL; bill = bill->next)
Bill_draw(bill);
for (bill = alive; bill != NULL; bill = bill->next)
Bill_draw(bill);
}
void
Horde_move_bill(Bill *bill) {
UNLINK(bill, alive);
PREPEND(bill, strays);
}
void
Horde_remove_bill(Bill *bill) {
if (bill->state == BILL_STATE_STRAY)
UNLINK(bill, strays);
else
UNLINK(bill, alive);
Network_clear_stray(bill);
free(bill);
}
void
Horde_add_bill(Bill *bill) {
if (bill->state == BILL_STATE_STRAY)
PREPEND(bill, strays);
else
PREPEND(bill, alive);
}
Bill *
Horde_clicked_stray(int x, int y) {
Bill *bill;
for (bill = strays; bill != NULL; bill = bill->next) {
if (!Bill_clickedstray(bill, x, y))
continue;
UNLINK(bill, strays);
return bill;
}
return NULL;
}
int
Horde_process_click(int x, int y) {
Bill *bill;
int counter = 0;
for (bill = alive; bill != NULL; bill = bill->next) {
if (bill->state == BILL_STATE_DYING ||
!Bill_clicked(bill, x, y))
continue;
if (bill->state == BILL_STATE_AT) {
Computer *comp;
comp = Network_get_computer(bill->target_c);
comp->busy = 0;
comp->stray = bill;
}
Bill_set_dying(bill);
counter++;
}
return counter;
}
void
Horde_inc_counter(int counter, int val) {
counters[counter] += val;
}
int
Horde_get_counter(int counter) {
return counters[counter];
}