-
Notifications
You must be signed in to change notification settings - Fork 2
/
level.c
334 lines (272 loc) · 7.27 KB
/
level.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
330
331
332
333
334
#include "level.h"
#include "types.h"
#include <string.h>
#include "params.h"
#include "tilesymbols.h"
#include "board.h"
#include "spmgr.h"
#include "train.h"
#include "colors.h"
#include <string.h>
SrcSnk lev_sources[MAX_LEV_SOURCES];
SrcSnk lev_sinks [MAX_LEV_SINKS];
SrcSnk lev_paints [MAX_LEV_PAINTS];
Obst lev_obsts [MAX_LEV_OBSTS];
ubyte lev_colors [MAX_LEV_COLORS];
ubyte lev_num_sources = 0;
ubyte lev_num_sinks = 0;
ubyte lev_num_paints = 0;
ubyte lev_num_obsts = 0;
ubyte lev_num_colors = 0;
ubyte lev_prov_idx;
ubyte lev_stars;
ubyte lev_name [MAX_LEV_NAME_LEN];
ubyte train_spr_idx;
// void level_pagein_levels()
// {
// __asm
// ; load bank/page for train sprite data
// NEXTREG $50, 34
// NEXTREG $51, 35
// __endasm
// }
ubyte* levrec;
void level_load(ubyte secnum, ubyte levnum)
{
level_init();
levrec = levarr[levnum];
level_parse_levdef();
board_clear(board);
spmgr_clear_level();
level_populate_fixed();
}
void level_init()
{
lev_num_sources = 0;
lev_num_sinks = 0;
lev_num_paints = 0;
lev_num_obsts = 0;
lev_num_colors = 0;
lev_stars = 0;
memset(lev_sources, 0, MAX_LEV_SOURCES * sizeof(SrcSnk));
memset(lev_sinks, 0, MAX_LEV_SINKS * sizeof(SrcSnk));
memset(lev_paints, 0, MAX_LEV_PAINTS * sizeof(SrcSnk));
memset(lev_obsts, 0, MAX_LEV_OBSTS * sizeof(Obst ));
memset(lev_colors, 0, MAX_LEV_COLORS * sizeof(ubyte ));
memset(lev_name, 0, MAX_LEV_NAME_LEN* sizeof(ubyte ));
}
ubyte* level_parse_ptr;
void level_parse_levdef()
{
level_parse_ptr = levrec;
lev_stars = *level_parse_ptr++;
lev_prov_idx = *level_parse_ptr++;
ubyte* name_ptr = &lev_name[0];
ubyte ch;
do {
ch = *level_parse_ptr++;
*name_ptr++ = ch;
}
while (ch != 0);
level_parse_rawdef();
}
void level_parse_rawdef()
{
ubyte token = *level_parse_ptr++;
while (token != 0)
{
ubyte type = HINIB(token);
ubyte dir = LONIB(token);
switch(type) {
case SRC: {
SrcSnk* src = &lev_sources[lev_num_sources++];
level_parse_srcsnk(src, dir);
break;
}
case SNK: {
SrcSnk* snk = &lev_sinks[lev_num_sinks++];
level_parse_srcsnk(snk, dir);
break;
}
case PNT: {
SrcSnk* pnt = &lev_paints[lev_num_paints++];
level_parse_srcsnk(pnt, dir);
break;
}
case OBS: {
level_parse_obst();
break;
}
}
token = *level_parse_ptr++;
}
}
void level_parse_srcsnk(SrcSnk* srcsnk, ubyte dir)
{
srcsnk->dir = dir;
ubyte token = *level_parse_ptr++;
ubyte i = HINIB(token);
ubyte j = LONIB(token);
srcsnk->i = i;
srcsnk->j = j;
token = *level_parse_ptr++;
ubyte ncols = HINIB(token);
ubyte col0 = LONIB(token);
srcsnk->colidx = lev_num_colors;
srcsnk->colcnt = ncols;
level_parse_colors(col0, ncols);
}
void level_parse_colors(ubyte col0, ubyte ncols)
{
lev_colors[lev_num_colors++] = col0;
ncols--;
ubyte token = 0x77;
ubyte col;
while(ncols) {
token = *level_parse_ptr++;
lev_colors[lev_num_colors++] = HINIB(token);
ncols--;
if (!ncols) break;
lev_colors[lev_num_colors++] = LONIB(token);
ncols--;
}
}
void level_parse_obst()
{
ubyte token = *level_parse_ptr++;
ubyte i = HINIB(token);
ubyte j = LONIB(token);
// ubyte tdef = JOINIB(OBS, 0);
// board_set_tile(i, j, tdef);
Obst* obs = &lev_obsts[lev_num_obsts];
obs->i = i;
obs->j = j;
lev_num_obsts++;
}
void level_populate_fixed()
{
for (ubyte i = 0; i < lev_num_sources; i++) {
SrcSnk* src = &lev_sources[i];
ubyte tdef = JOINIB(SRC, i);
board_set_tile(src->i, src->j, tdef);
src->spridx = spmgr_curr_idx;
spmgr_put_src(src->i, src->j, src->colcnt);
spmgr_update_srcsnk(src->spridx, src->colidx, src->colcnt);
spmgr_put_snk_src_mask(src->i, src->j);
}
for (ubyte i = 0; i < lev_num_sinks; i++) {
SrcSnk* snk = &lev_sinks[i];
ubyte tdef = JOINIB(SNK, i);
board_set_tile(snk->i, snk->j, tdef);
snk->chkidx = spmgr_curr_idx;
spmgr_put_check(snk->i, snk->j);
spmgr_hide_check(snk->chkidx);
snk->spridx = spmgr_curr_idx;
spmgr_put_snk(snk->i, snk->j, snk->colcnt);
spmgr_update_srcsnk(snk->spridx, snk->colidx, snk->colcnt);
spmgr_put_snk_src_mask(snk->i, snk->j);
}
for (ubyte i = 0; i < lev_num_paints; i++) {
SrcSnk* pnt = &lev_paints[i];
ubyte tdef = JOINIB(PNT, i);
board_set_tile(pnt->i, pnt->j, tdef);
pnt->spridx = spmgr_curr_idx;
spmgr_put_pnt(pnt->i, pnt->j, pnt->colidx);
spmgr_put_snk_src_mask(pnt->i, pnt->j);
}
for (ubyte i = 0; i < lev_num_obsts; i++) {
Obst* obs = &lev_obsts[i];
ubyte tdef = JOINIB(OBS, 0);
board_set_tile(obs->i, obs->j, tdef);
}
train_spr_idx = spmgr_curr_idx;
}
uword level_frame = 0;
void level_start_trains()
{
if (level_state & LEV_STATE_RUNNING)
return;
level_set_state(LEV_STATE_RUNNING);
level_frame = 0;
train_steps = 0;
train_clear_all();
}
void level_stop_trains()
{
level_set_state(LEV_STATE_DESIGN);
}
sbyte level_speed = 0;
sbyte level_prev_speed = 0;
ubyte level_slow = 0;
ubyte level_slow_target = 0;
ubyte level_frame_inc = 1;
void level_set_speed(sbyte speed)
{
if (speed < -6) speed = -6;
if (speed > 5) speed = 5;
level_speed = speed;
if (speed == 0) {
level_slow_target = 0;
level_frame_inc = 1;
return;
}
if (speed <= 0) {
level_frame_inc = 1;
level_slow_target = 1 << -(1 + speed);
return;
}
if (speed >= 0) {
level_slow_target = 0;
level_frame_inc = 1 << speed;
}
}
void level_update()
{
if (!(level_state & LEV_STATE_RUNNING))
return;
if (level_slow_target > 0)
{
if (level_slow_target > 16) return;
if (level_slow++ < level_slow_target)
return;
else
level_slow = 0;
}
train_update(level_frame);
level_frame += level_frame_inc;
}
ubyte level_sink_matches_color(SrcSnk* snk, ubyte color)
{
for (ubyte i = 0; i < snk->colcnt; i++) {
if (lev_colors[snk->colidx + i] == color) {
return 1;
}
}
return 0;
}
void level_sink_remove_color(SrcSnk* snk, ubyte color)
{
for (ubyte i = 0; i < snk->colcnt; i++) {
if (lev_colors[snk->colidx + i] == color) {
lev_colors[snk->colidx + i] = COLOR_BLK;
spmgr_update_srcsnk(snk->spridx, snk->colidx, snk->colcnt);
return;
}
}
}
void level_sink_check_all_done(SrcSnk* snk)
{
for (ubyte i = 0; i < snk->colcnt; i++) {
if (lev_colors[snk->colidx + i] != COLOR_BLK)
return;
}
spmgr_show_check(snk->chkidx);
}
///////////////////////////////////////////////////////////////////////////////
#include "screen_game.h"
ubyte level_state = LEV_STATE_IDLE;
void level_set_state(ubyte state)
{
if (state == level_state) return;
level_state = state;
}