-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplayer.c
329 lines (313 loc) · 9.45 KB
/
multiplayer.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#define TARGET_SCORE (70)
#define DROP_SCORE (10)
#define MAX_NUISANCE_ROWS (5)
#define MAX_PLAYERS (16)
typedef struct player
{
state state;
int deal_index;
int chain;
int chain_score;
int total_score;
int chain_all_clear_bonus;
int all_clear_bonus;
int game_overs;
int pending_nuisance;
int leftover_score;
int nuisance_x;
} player;
typedef struct practice_game
{
player player;
int time;
int delay;
int incoming;
int num_deals;
content_t deals[MAX_DEALS];
} practice_game;
typedef struct game
{
player *players;
content_t *deals;
int time;
int num_players;
int num_deals;
int total_num_deals;
} game;
void print_player(player *p) {
print_state(&p->state);
double sending = (p->chain_score + p->leftover_score) / (double) TARGET_SCORE;
printf(
"score=%d chain=%d sending=%.2f receiving=%d all clear=%d/%d game_overs=%d\n",
p->total_score, p->chain, sending, p->pending_nuisance, p->chain_all_clear_bonus, p->all_clear_bonus, p->game_overs
);
}
void print_practice(practice_game *pg) {
print_player(&pg->player);
printf(
"incoming=%d/%d\n",
pg->incoming, pg->delay
);
}
game* new_game(int num_players, int num_deals) {
game *g = calloc(1, sizeof(game));
g->num_players = num_players;
g->players = calloc(num_players, sizeof(player));
g->num_deals = num_deals;
g->total_num_deals = 1 << 4;
while (g->total_num_deals < num_deals) {
g->total_num_deals <<= 1;
}
g->deals = malloc(g->total_num_deals * sizeof(content_t));
for (int i = 0; i < g->total_num_deals; ++i) {
g->deals[i] = rand_piece();
}
return g;
}
void free_game(game *g) {
free(g->players);
free(g->deals);
free(g);
}
void clear_player(player *p) {
clear_state(&p->state);
p->chain = 0;
p->chain_score = 0;
p->total_score = 0;
p->all_clear_bonus = 0;
p->chain_all_clear_bonus = 0;
p->pending_nuisance = 0;
p->leftover_score = 0;
p->nuisance_x = 0;
}
int send_nuisance(player *p) {
// Send garbage
int nuisance_sent = (p->chain_score + p->leftover_score) / TARGET_SCORE;
p->leftover_score = (p->chain_score + p->leftover_score) % TARGET_SCORE;
p->chain_score = 0;
// Offset garbage
if (p->pending_nuisance >= nuisance_sent) {
p->pending_nuisance -= nuisance_sent;
nuisance_sent = 0;
} else {
nuisance_sent -= p->pending_nuisance;
p->pending_nuisance = 0;
}
return nuisance_sent;
}
void receive_nuisance(player *p) {
// Receive garbage
int nuisance_received = 0;
if (p->pending_nuisance) {
nuisance_received = p->pending_nuisance;
if (nuisance_received > MAX_NUISANCE_ROWS * WIDTH) {
nuisance_received = MAX_NUISANCE_ROWS * WIDTH;
}
p->pending_nuisance -= nuisance_received;
int row = 0;
while (nuisance_received) {
int nuisance_placed = nuisance_received;
if (nuisance_placed > WIDTH) {
nuisance_placed = WIDTH;
}
nuisance_received -= nuisance_placed;
while(nuisance_placed) {
puyos_t n = 1ULL << (p->nuisance_x + row * V_SHIFT);
p->state.floors[0][GARBAGE] |= n;
p->nuisance_x = (p->nuisance_x + 1) % WIDTH;
--nuisance_placed;
}
++row;
}
handle_gravity(&p->state);
kill_puyos(&p->state);
}
}
int step_player(player *p) {
handle_gravity(&p->state);
kill_puyos(&p->state);
int score = clear_groups(&p->state, p->chain);
if (score) {
++p->chain;
p->chain_score += score;
p->total_score += score;
int all_clear_bonus = 1;
for (int i = 0; i < NUM_COLORS; ++i) {
if (p->state.floors[NUM_FLOORS-1][i]) {
all_clear_bonus = 0;
break;
}
}
if (all_clear_bonus) {
p->chain_all_clear_bonus = 1;
}
} else {
if (p->chain) {
if (p->all_clear_bonus) {
p->chain_score += MAX_NUISANCE_ROWS * WIDTH * TARGET_SCORE;
p->total_score += MAX_NUISANCE_ROWS * WIDTH * TARGET_SCORE;
}
p->all_clear_bonus = p->chain_all_clear_bonus;
p->chain_all_clear_bonus = 0;
}
p->chain = 0;
}
return score;
}
void step_game(game *g, content_t *choices) {
int nuisance_receivers[MAX_PLAYERS] = {0};
int has_stepped[MAX_PLAYERS] = {0};
int game_over = 0;
for (int i = 0; i < g->num_players; ++i) {
player *p = g->players + i;
if (p->chain) {
step_player(p);
has_stepped[i] = 1;
if (!p->chain) {
int nuisance_sent = send_nuisance(p);
for (int j = 0; j < g->num_players; ++j) {
if (j != i) {
g->players[j].pending_nuisance += nuisance_sent;
}
}
nuisance_receivers[i] = 1;
}
}
}
// Separate loop to mitigate iteration order issues.
for (int i = 0; i < g->num_players; ++i) {
if (has_stepped[i]) {
continue;
}
player *p = g->players + i;
int valid = apply_deal_and_choice(&p->state, g->deals[p->deal_index], choices[i]);
p->chain_score += DROP_SCORE;
p->total_score += DROP_SCORE;
handle_gravity(&p->state);
++p->deal_index;
step_player(p);
if (!p->chain) {
receive_nuisance(p);
}
if (!valid) {
game_over = 1;
++p->game_overs;
}
// Just some memory management.
if (p->deal_index + g->num_deals >= g->total_num_deals) {
g->deals = realloc(g->deals, (g->total_num_deals << 1) * sizeof(content_t));
for (int i = 0; i < g->total_num_deals; ++i) {
g->deals[g->total_num_deals + i] = rand_piece();
}
g->total_num_deals <<= 1;
}
}
// Separate loop to mitigate iteration order issues.
for (int i = 0; i < g->num_players; ++i) {
if (nuisance_receivers[i]) {
receive_nuisance(g->players + i);
}
}
if (game_over) {
int max_index = 0;
for (int i = 0; i < g->num_players; ++i) {
clear_player(g->players + i);
if (g->players[i].deal_index > max_index) {
max_index = g->players[i].deal_index;
}
}
for (int i = 0; i < g->num_players; ++i) {
g->players[i].deal_index = max_index;
}
}
++g->time;
}
practice_game* game_as_practice(game *g, int player_index) {
// If the player is stuck chaining there is no practice state the reflects that.
if (g->players[player_index].chain) {
return NULL;
}
// Multiple opponents would require multiple incomming channels for practice.
assert(g->num_players == 2); // Limit to two for now.
practice_game *pg = calloc(1, sizeof(practice_game));
pg->time = g->time;
pg->num_deals = g->num_deals;
pg->player = g->players[player_index];
for (int j = 0; j < g->num_deals; ++j) {
pg->deals[j] = g->deals[j + g->players[player_index].deal_index];
}
for (int j = g->num_deals; j < MAX_DEALS; ++j) {
pg->deals[j] = rand_piece();
}
handle_gravity(&pg->player.state);
for (int i = 0; i < g->num_players; ++i) {
if (i == player_index) {
continue;
}
player opponent = g->players[i];
if (opponent.chain) {
int delay = 0;
do {
step_player(&opponent);
++delay;
} while(opponent.chain);
if (opponent.all_clear_bonus) {
opponent.chain_score += MAX_NUISANCE_ROWS * WIDTH * TARGET_SCORE;
}
// All clear reset omitted.
// opponent.all_clear_bonus = opponent.chain_all_clear_bonus;
// opponent.chain_all_clear_bonus = 0;
int nuisance = send_nuisance(&opponent);
pg->delay = delay;
pg->incoming = nuisance;
}
return pg;
}
assert(0);
}
void* copy_practice(void *pg) {
void *c = malloc(sizeof(practice_game));
memcpy(c, pg, sizeof(practice_game));
return c;
}
void append_practice_deal(practice_game *pg, content_t deal) {
for (int i = 0; i < MAX_DEALS - 1; ++i) {
pg->deals[i] = pg->deals[i + 1];
}
pg->deals[MAX_DEALS - 1] = deal;
}
double step_practice(void *_pg, content_t deal, content_t choice) {
practice_game *pg = _pg;
double score = 0;
state *s = &pg->player.state;
if (pg->delay == 0) {
pg->player.pending_nuisance += pg->incoming;
pg->incoming = 0;
}
// Assume deal is pg->deals[0] for now.
int valid = apply_deal_and_choice(s, deal, choice);
do {
step_player(&pg->player);
++pg->time;
--pg->delay;
} while (pg->player.chain);
score += pg->player.chain_score;
int outgoing = send_nuisance(&pg->player);
receive_nuisance(&pg->player);
handle_gravity(s);
pg->incoming -= outgoing;
if (pg->incoming < 0) {
pg->incoming = 0;
pg->delay = 0;
}
if (!valid) {
clear_player(&pg->player);
++pg->player.game_overs;
score = -DEATH_SCORE;
}
append_practice_deal(pg, rand_piece());
if (pg->delay < 0) {
pg->delay = 0;
}
return score;
}