-
Notifications
You must be signed in to change notification settings - Fork 2
/
MBHorde.m
179 lines (153 loc) · 3.32 KB
/
MBHorde.m
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
176
177
178
179
#import "MBtypes.h"
#import "MBHorde.h"
#import "MButil.h"
#import "MBBill.h"
#import "MBComputer.h"
#import "MBGame.h"
#import "MBNetwork.h"
#import "MBUI.h"
static MBBill *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
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) {
MBBill *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 = [MBBill Bill_enter];
PREPEND(bill, alive);
}
}
@implementation MBHorde
// private
- (int)on:(unsigned int)lev
{
int perlevel = (int)((8 + 3 * lev) * [game Game_scale:2]);
return MIN(perlevel, MAX_BILLS);
}
- (void)Horde_setup
{
MBBill *bill;
while (alive != NULL) {
bill = alive;
UNLINK(bill, alive);
[bill release];
}
while (strays != NULL) {
bill = strays;
UNLINK(bill, strays);
[bill release];
}
counters[HORDE_COUNTER_OFF] = [self on:[game Game_level]];
counters[HORDE_COUNTER_ON] = 0;
}
- (void)Horde_update:(int)iteration
{
MBBill *bill, *next;
int level = [game Game_level];
if (iteration % between(level) == 0)
launch(max_at_once(level));
for (bill = alive; bill != NULL; bill = next) {
next = bill->next;
[bill Bill_update];
}
}
- (void)Horde_draw
{
MBBill *bill;
for (bill = strays; bill != NULL; bill = bill->next)
[bill Bill_draw];
for (bill = alive; bill != NULL; bill = bill->next)
[bill Bill_draw];
}
- (void)Horde_move_bill:(MBBill *)bill
{
UNLINK(bill, alive);
PREPEND(bill, strays);
}
- (void)Horde_remove_bill:(MBBill *)bill
{
if (bill->state == BILL_STATE_STRAY)
UNLINK(bill, strays);
else
UNLINK(bill, alive);
[network Network_clear_stray:bill];
[bill release];
}
- (void)Horde_add_bill:(MBBill *)bill
{
if (bill->state == BILL_STATE_STRAY)
PREPEND(bill, strays);
else
PREPEND(bill, alive);
}
- (MBBill *)Horde_clicked_stray:(int)x :(int)y
{
MBBill *bill;
for (bill = strays; bill != NULL; bill = bill->next) {
if (![bill Bill_clickedstray:x :y])
continue;
UNLINK(bill, strays);
return bill;
}
return NULL;
}
- (int)Horde_process_click:(int)x :(int)y
{
MBBill *bill;
int counter = 0;
for (bill = alive; bill != NULL; bill = bill->next) {
if (bill->state == BILL_STATE_DYING ||
![bill Bill_clicked:x :y])
continue;
if (bill->state == BILL_STATE_AT) {
MBComputer *comp;
comp = [network Network_get_computer:bill->target_c];
comp->busy = 0;
comp->stray = bill;
}
[bill Bill_set_dying];
counter++;
}
return counter;
}
- (void)Horde_inc_counter:(int)counter :(int)val
{
counters[counter] += val;
}
- (int)Horde_get_counter:(int)counter
{
return counters[counter];
}
@end