64169e73037a023e446c6538bc8f000a2a0f0e0d
[gbajam22] / src / gamescr.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "gbaregs.h"
4 #include "game.h"
5 #include "dma.h"
6 #include "util.h"
7 #include "intr.h"
8 #include "input.h"
9 #include "gba.h"
10 #include "sprite.h"
11 #include "timer.h"
12 #include "debug.h"
13 #include "voxscape.h"
14 #include "data.h"
15 #include "scoredb.h"
16
17 #define POS_MASK        ((VOX_SZ << 16) - 1)
18
19 #define FOV             30
20 #define NEAR    2
21 #define FAR             85
22
23 #define P_RATE  250
24 #define E_RATE  3000
25 #define SHOT_TIME       50
26
27 #define ENEMY_VIS_RANGE (2 * FAR / 3)
28
29 static int gamescr_start(void);
30 static void gamescr_stop(void);
31 static void gamescr_frame(void);
32 static void gamescr_vblank(void);
33
34 static int update(void);
35 static void draw(void);
36 static void victory(void);
37
38 static struct screen gamescr = {
39         "game",
40         gamescr_start,
41         gamescr_stop,
42         gamescr_frame,
43         gamescr_vblank
44 };
45
46 #define ENEMY_ENERGY    4
47 struct enemy {
48         struct vox_object vobj;
49         short hp;
50         unsigned char anm;
51         signed char shot_frame;
52         int last_shot;
53 };
54 #define ENEMY_VALID(e)  ((e)->anm != 0)
55 #define NUM_SHOT_FRAMES 16
56
57 #define SFRM_LVL1       5
58 #define SFRM_LVL2       12
59
60 static uint16_t *framebuf;
61
62 static int nframes, backbuf;
63 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
64
65 static int32_t pos[2], angle, horizon = 80;
66 static long last_shot, hitfrm;
67 static int hit_px, hit_py;
68 static int pheight;
69
70 #define COLOR_HORIZON   192
71 #define COLOR_ZENITH    255
72
73 #define MAX_SPR         48
74 static uint16_t oam[4 * MAX_SPR];
75 static int dynspr_base, dynspr_count;
76
77
78 #define MAX_ENEMIES             (255 - CMAP_SPAWN0)
79 static struct enemy enemies[MAX_ENEMIES];
80 static int num_kills, total_enemies;
81 static int energy;
82 #define MAX_ENERGY      5
83
84 static int score, gameover;
85 static unsigned long total_time, start_time;
86 static int running;
87
88 #define XFORM_PIXEL_X(x, y)     (xform_ca * (x) - xform_sa * (y) + (120 << 8))
89 #define XFORM_PIXEL_Y(x, y)     (xform_sa * (x) + xform_ca * (y) + (80 << 8))
90 static int32_t xform_sa, xform_ca;      /* for viewport bank/zoom */
91 static int xform_s;
92
93 static short vblcount;
94 static void *prev_iwram_top;
95
96 static int hit_frame;
97 static uint16_t color0;
98
99 static inline void xform_pixel(int *xp, int *yp);
100
101
102 struct screen *init_game_screen(void)
103 {
104         return &gamescr;
105 }
106
107 static void setup_palette(void)
108 {
109         int i;
110         unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
111
112         emuprint("setting up %s palette", gba_colors ? "GBA" : "NDS/Emu");
113
114         for(i=0; i<256; i++) {
115                 int r = cmap[i * 3];
116                 int g = cmap[i * 3 + 1];
117                 int b = cmap[i * 3 + 2];
118                 gba_bgpal[i] = RGB555(r, g, b);
119         }
120
121         color0 = gba_bgpal[0];
122 }
123
124 static int gamescr_start(void)
125 {
126         int i, j, sidx;
127         uint8_t *cptr;
128         struct enemy *enemy;
129
130         prev_iwram_top = iwram_sbrk(0);
131
132         gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
133         fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
134
135         vblperf_setcolor(0);
136
137         pos[0] = pos[1] = VOX_SZ << 15;
138         angle = 0x8000;
139         last_shot = -P_RATE - 1;
140
141         vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
142         vox_proj(FOV, NEAR, FAR);
143         pheight = vox_view(pos[0], pos[1], -40, angle);
144
145         /* setup color image palette */
146         setup_palette();
147
148         spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
149         wait_vblank();
150         spr_clear();
151
152         for(i=0; i<MAX_SPR; i++) {
153                 spr_oam_clear(oam, i);
154         }
155
156         sidx = 0;
157         spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
158         spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
159         spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
160         spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
161         spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
162         spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
163         dynspr_base = sidx;
164
165         num_kills = total_enemies = 0;
166         energy = 5;
167
168         memset(enemies, 0, sizeof enemies);
169         cptr = color_pixels;
170         for(i=0; i<VOX_SZ; i++) {
171                 for(j=0; j<VOX_SZ; j++) {
172                         if(*cptr == 255) {
173                                 /* player spawn point */
174                                 pos[0] = j << 16;
175                                 pos[1] = i << 16;
176
177                         } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
178                                 /* enemy spawn point */
179                                 int idx = *cptr - CMAP_SPAWN0;
180                                 enemy = enemies + idx;
181                                 if(enemy->anm) {
182                                         panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
183                                                         j, i, enemy->vobj.x, enemy->vobj.y);
184                                 }
185                                 enemy->vobj.x = j;
186                                 enemy->vobj.y = i;
187                                 enemy->vobj.px = -1;
188                                 enemy->anm = 0xff;
189                                 enemy->hp = ENEMY_ENERGY;
190                                 enemy->last_shot = timer_msec > E_RATE ? timer_msec - E_RATE : 0;
191                                 enemy->shot_frame = -1;
192                                 if(++total_enemies >= MAX_ENEMIES) {
193                                         goto endspawn;
194                                 }
195                         }
196                         cptr++;
197                 }
198         }
199 endspawn:
200         total_enemies = 1;      /* XXX DBG */
201         /* check continuity */
202         for(i=0; i<total_enemies; i++) {
203                 if(enemies[i].anm <= 0) {
204                         panic(get_pc(), "discontinuous enemy list");
205                 }
206                 enemies[i].anm = rand() & 7;
207         }
208
209         vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
210
211         energy = MAX_ENERGY;
212         xform_sa = 0;
213         xform_ca = 0x10000;
214         xform_s = 0x100;
215
216         gameover = 0;
217         score = -1;
218         total_time = 0;
219         start_time = timer_msec;
220
221         hitfrm = 0;
222
223         vblcount = 0;
224         nframes = 0;
225
226         running = 1;
227         return 0;
228 }
229
230 static void gamescr_stop(void)
231 {
232         running = 0;
233
234         iwram_brk(prev_iwram_top);
235
236         wait_vblank();
237         /* clear sprites */
238         spr_clear();
239         /* reset background rot/scale state */
240         REG_BG2X = 0;
241         REG_BG2Y = 0;
242         REG_BG2PA = 0x100;
243         REG_BG2PB = 0;
244         REG_BG2PC = 0;
245         REG_BG2PD = 0x100;
246
247 }
248
249 static void gamescr_frame(void)
250 {
251         backbuf = ++nframes & 1;
252         framebuf = vram[backbuf];
253
254         vox_framebuf(240, 160, framebuf, horizon);
255
256         if(update() == -1) {
257                 return;
258         }
259         draw();
260
261         vblperf_end();
262         wait_vblank();
263         present(backbuf);
264
265         if(!(nframes & 15)) {
266                 emuprint("vbl: %d", vblperf_count);
267         }
268 #ifdef VBLBAR
269         vblperf_begin();
270 #else
271         vblperf_count = 0;
272 #endif
273 }
274
275 #define NS(x)   (SPRID_UINUM + ((x) << 1))
276 static int numspr[][2] = {
277         {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
278         {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
279         {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
280         {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
281         {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
282         {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
283         {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
284         {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
285 };
286
287 #define WALK_SPEED      0x40000
288 #define TURN_SPEED      0x200
289 #define ELEV_SPEED      8
290
291 #define MAX(a, b)       ((a > (b) ? (a) : (b)))
292
293 static int update(void)
294 {
295         int32_t fwd[2], right[2];
296         int i, snum, ledspr;
297         struct enemy *enemy;
298         int did_strafe = 0;
299
300         hit_frame = 0;
301
302         update_keyb();
303
304         if(KEYPRESS(BN_START)) {
305                 /* TODO pause menu */
306                 change_screen(find_screen("menu"));
307                 return -1;
308         }
309
310         if(gameover) {
311                 goto skip_game_logic;
312         }
313
314         if(keystate) {
315                 if(keystate & BN_LEFT) {
316                         angle += TURN_SPEED;
317                 }
318                 if(keystate & BN_RIGHT) {
319                         angle -= TURN_SPEED;
320                 }
321
322                 fwd[0] = -SIN(angle);
323                 fwd[1] = COS(angle);
324                 right[0] = fwd[1];
325                 right[1] = -fwd[0];
326
327                 if(keystate & BN_A) {
328                         pos[0] = (pos[0] + fwd[0]) & POS_MASK;
329                         pos[1] = (pos[1] + fwd[1]) & POS_MASK;
330                         if(pos[0] < 0) pos[0] += VOX_SZ << 16;
331                         if(pos[1] < 0) pos[1] += VOX_SZ << 16;
332                 }
333
334                 if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
335                         last_shot = timer_msec;
336                         for(i=0; i<total_enemies; i++) {
337                                 if(enemies[i].hp && enemies[i].vobj.px >= 0) {
338                                         int dx = enemies[i].vobj.px - 120;
339                                         int dy = enemies[i].vobj.py - 80;
340                                         int rad = enemies[i].vobj.scale >> 5;
341
342                                         if(rad < 1) rad = 1;
343
344                                         if(abs(dx) < rad && abs(dy) < (rad << 1)) {
345                                                 enemies[i].shot_frame = -1;
346                                                 if(--enemies[i].hp <= 0) {
347                                                         if(++num_kills >= total_enemies) {
348                                                                 victory();
349                                                         }
350                                                 }
351                                                 hit_px = enemies[i].vobj.px;
352                                                 hit_py = enemies[i].vobj.py;
353                                                 hitfrm = nframes;
354                                                 break;
355                                         }
356                                 }
357                         }
358                 }
359                 if(keystate & BN_UP) {
360                         if(horizon > 40) horizon -= ELEV_SPEED;
361                 }
362                 if(keystate & BN_DOWN) {
363                         if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
364                 }
365                 if(keystate & BN_RT) {
366                         pos[0] = (pos[0] + right[0]) & POS_MASK;
367                         pos[1] = (pos[1] + right[1]) & POS_MASK;
368                         if(pos[0] < 0) pos[0] += VOX_SZ << 16;
369                         if(pos[1] < 0) pos[1] += VOX_SZ << 16;
370                         did_strafe = 1;
371                 }
372                 if(keystate & BN_LT) {
373                         pos[0] = (pos[0] - right[0]) & POS_MASK;
374                         pos[1] = (pos[1] - right[1]) & POS_MASK;
375                         if(pos[0] < 0) pos[0] += VOX_SZ << 16;
376                         if(pos[1] < 0) pos[1] += VOX_SZ << 16;
377                         did_strafe = 1;
378                 }
379
380                 pheight = vox_view(pos[0], pos[1], -40, angle);
381         }
382
383         /* enemy logic */
384         enemy = enemies;
385         for(i=0; i<total_enemies; i++) {
386                 /* only consider enemies which are not dead */
387                 if(enemy->hp <= 0) {
388                         enemy++;
389                         continue;
390                 }
391
392                 if(enemy->shot_frame >= 0) {
393                         /* in the process of charging a shot */
394                         if(++enemy->shot_frame >= NUM_SHOT_FRAMES - 1) {
395                                 /* only get hit if we can see the enemy and we didn't strafe */
396                                 if(!did_strafe && enemy->vobj.px >= 0) {
397                                         hit_frame = 1;
398                                         if(--energy <= 0) {
399                                                 gameover = 1;
400                                         }
401                                 }
402                                 enemy->shot_frame = -1;
403                         }
404                 } else if(enemy->vobj.px >= 0) {
405                         /* check rate of fire and start a shot if necessary */
406                         if(timer_msec - enemy->last_shot >= E_RATE) {
407                                 enemy->last_shot = timer_msec;
408                                 enemy->shot_frame = 0;
409                         }
410                 }
411                 enemy++;
412         }
413
414 skip_game_logic:
415
416         snum = 0;
417         /* turrets number */
418         spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
419         spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
420         spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
421         spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
422         /* energy bar */
423         if(energy == MAX_ENERGY) {
424                 ledspr = SPRID_LEDBLU;
425         } else {
426                 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
427         }
428         for(i=0; i<5; i++) {
429                 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
430                                 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
431         }
432         /* blaster sprites */
433         if(timer_msec - last_shot <= SHOT_TIME) {
434                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, -8, 118, SPR_SZ32 | SPR_256COL);
435                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 22, 103, SPR_SZ32 | SPR_256COL);
436                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 54, 88, SPR_SZ32 | SPR_256COL);
437                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 86, 72, SPR_SZ32 | SPR_256COL);
438
439                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, 240 + 8 - 32, 118, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
440                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 240 - 22 - 32, 103, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
441                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 240 - 54 - 32, 88, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
442                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 240 - 86 - 32, 72, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
443         }
444         /* hit sparks */
445         if(nframes - hitfrm < 5) {
446                 int id = SPRID_SPARK0 + (nframes - hitfrm);
447                 spr_oam(oam, dynspr_base + snum++, id, hit_px - 16, hit_py - 16,
448                                 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
449         }
450         /* enemy sprites */
451         /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
452         enemy = enemies;
453         for(i=0; i<total_enemies; i++) {
454                 int sid, anm, px, py, yoffs;
455                 unsigned int flags;
456                 int16_t mat[4];
457                 int32_t sa, ca, scale;
458
459                 if(enemy->vobj.px >= 0) {
460                         flags = SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
461                         if(enemy->hp > 0) {
462                                 anm = (enemy->anm + (vblcount >> 3)) & 0xf;
463                                 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
464                                 flags |= SPR_SZ32 | SPR_VRECT;
465                                 yoffs = 32;
466                         } else {
467                                 anm = 0;
468                                 sid = SPRID_HUSK;
469                                 flags |= SPR_SZ16;
470                                 yoffs = 16;
471                         }
472
473                         px = enemy->vobj.px - 120;
474                         py = enemy->vobj.py - 80;
475                         xform_pixel(&px, &py);
476
477
478                         if(enemy->shot_frame >= 0) {
479                                 if(enemy->shot_frame < SFRM_LVL1) {
480                                         spr_oam(oam, dynspr_base + snum++, SPRID_SHOT0, px - 16, py - 16,
481                                                         SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
482                                 } else if(enemy->shot_frame < SFRM_LVL2) {
483                                         spr_oam(oam, dynspr_base + snum++, SPRID_SHOT1, px - 16, py - 16,
484                                                         SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
485                                 } else {
486                                         spr_oam(oam, dynspr_base + snum++, SPRID_SHOT2, px - 16, py - 16,
487                                                         SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
488                                 }
489                         }
490
491
492                         spr_oam(oam, dynspr_base + snum++, sid, px - 16, py - yoffs, flags);
493
494                         scale = enemy->vobj.scale;
495                         if(scale > 0x10000) scale = 0x10000;
496                         sa = xform_sa / scale;
497                         ca = xform_ca / scale;
498                         mat[0] = anm >= 8 ? -ca : ca;
499                         mat[1] = sa;
500                         mat[2] = -sa;
501                         mat[3] = ca;
502
503                         spr_transform(oam, 0, mat);
504                         enemy->vobj.px = -1;
505                 }
506                 enemy++;
507         }
508         for(i=snum; i<dynspr_count; i++) {
509                 spr_oam_clear(oam, dynspr_base + i);
510         }
511
512         mask(INTR_VBLANK);
513         dynspr_count = snum;
514         unmask(INTR_VBLANK);
515
516         return 0;
517 }
518
519 static void draw(void)
520 {
521         //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
522         fillblock_16byte(framebuf, 0, 240 * 160 / 16);
523
524         if(hit_frame) {
525                 gba_bgpal[0] = 0x7fff;
526         } else {
527                 gba_bgpal[0] = color0;
528                 vox_render();
529         }
530         //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
531         //vox_sky_solid(COLOR_ZENITH);
532
533         if(score >= 0 || energy <= 0) {
534                 int sec = total_time / 1000;
535
536                 fillblock_16byte(framebuf + 8 * 240 / 2, 199 | (199 << 8) | (199 << 16) | (199 << 24), 40 * 240 / 16);
537
538                 glyphfb = framebuf;
539                 glyphbg = 199;
540                 glyphcolor = 197;
541                 if(energy > 0) {
542                         dbg_drawstr(80, 10, "Victory!");
543                         glyphcolor = 200;
544                         dbg_drawstr(30, 20, "       Score: %d", score);
545                         dbg_drawstr(30, 28, "Completed in: %lum.%lus", sec / 60, sec % 60);
546                 } else {
547                         dbg_drawstr(80, 10, "Game Over!");
548                 }
549                 glyphcolor = 198;
550                 dbg_drawstr(85, 40, "Press start to exit");
551         }
552 }
553
554 static void victory(void)
555 {
556         total_time = timer_msec - start_time;
557         score = 42;
558
559         /* TODO enter name */
560         save_score("???", score, total_time, 0);
561         save_scores();
562 }
563
564 static inline void xform_pixel(int *xp, int *yp)
565 {
566         int32_t sa = xform_sa >> 8;
567         int32_t ca = xform_ca >> 8;
568         int x = *xp;
569         int y = *yp;
570
571         *xp = (ca * x - sa * y + (120 << 8)) >> 8;
572         *yp = (sa * x + ca * y + (80 << 8)) >> 8;
573 }
574
575 #define MAXBANK         0x100
576
577 ARM_IWRAM
578 static void gamescr_vblank(void)
579 {
580         static int bank, bankdir, theta;
581         int32_t sa, ca;
582
583         if(!running) return;
584
585         vblcount++;
586
587         /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
588          * DMA them from cartridge easily
589          */
590
591         /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
592         dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
593
594         if(gameover) return;
595
596         theta = -(bank << 3);
597         xform_sa = SIN(theta);
598         xform_ca = COS(theta);
599 #if 0
600         xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
601         sa = (((xform_sa) >> 8) * xform_s) >> 12;
602         ca = (((xform_ca) >> 8) * xform_s) >> 12;
603 #else
604         xform_s = (MAXBANK + (abs(bank) >> 3));
605         sa = xform_sa / xform_s;
606         ca = xform_ca / xform_s;
607 #endif
608
609         REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
610         REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
611
612         REG_BG2PA = ca;
613         REG_BG2PB = sa;
614         REG_BG2PC = -sa;
615         REG_BG2PD = ca;
616
617         if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
618                 if(bank) {
619                         bank -= bankdir << 4;
620                 }
621         } else if(keystate & BN_LEFT) {
622                 bankdir = -1;
623                 if(bank > -MAXBANK) bank -= 16;
624         } else if(keystate & BN_RIGHT) {
625                 bankdir = 1;
626                 if(bank < MAXBANK) bank += 16;
627         }
628 }