/** nescoundrel.c: a NES port of the Scoundrel roguelike card game using the official ruleset only Created by Luxferre in 2026, released into public domain */ #include #include #include #include #include #include /* Constants section */ #define SCREEN_WIDTH 32 #define SCREEN_HEIGHT 28 #define SCREEN_CENTER_X (SCREEN_WIDTH >> 1) #define SCREEN_CENTER_Y (SCREEN_HEIGHT >> 1) #define KEY_UP 1 #define KEY_DN 2 #define KEY_LT 3 #define KEY_RT 4 #define KEY_A 5 #define KEY_B 6 #define KEY_SL 7 #define KEY_ST 8 #define GAME_WHITE 0 #define GAME_RED 1 #define GAME_YELLOW 2 #define GAME_GREEN 3 /* Graphics section */ void ppu_off() { PPU.mask &= 0xE7; } void ppu_on() { waitvsync(); PPU.scroll = 0; PPU.mask |= 0x1A; } int joy_input_wait() { unsigned char last_joy = 0, joy, pressed; /* wait until the previous key is released */ while(joy_read(JOY_1)); /* wait for a new press */ while(1) { joy = joy_read(JOY_1); pressed = joy & ~last_joy; if(pressed & JOY_UP_MASK) return KEY_UP; if(pressed & JOY_DOWN_MASK) return KEY_DN; if(pressed & JOY_LEFT_MASK) return KEY_LT; if(pressed & JOY_RIGHT_MASK) return KEY_RT; if(pressed & JOY_BTN_1_MASK) return KEY_A; if(pressed & JOY_BTN_2_MASK) return KEY_B; if(pressed & JOY_BTN_3_MASK) return KEY_SL; if(pressed & JOY_BTN_4_MASK) return KEY_ST; last_joy = joy; waitvsync(); } } /* palette loader */ void init_palette() { unsigned char i; const unsigned char my_palette[16] = { 0x0f, 0, 0, 0x30, /* white */ 0x0f, 0, 0, 0x15, /* red */ 0x0f, 0, 0, 0x28, /* yellow */ 0x0f, 0, 0, 0x2A /* green */ }; PPU.vram.address = 0x3F; PPU.vram.address = 0; for(i=0; i<16; i++) PPU.vram.data = my_palette[i]; } /* 4x4 char block colorization method (palette is 0 to 3) */ void colorxy(unsigned char x, unsigned char y, unsigned char palette) { unsigned int addr; unsigned char attr_byte; addr = 0x23C0 + ((y >> 2) << 3) + (x >> 2); attr_byte = palette | (palette << 2) | (palette << 4) | (palette << 6); waitvsync(); PPU.vram.address = (unsigned char) (addr >> 8); PPU.vram.address = (unsigned char) (addr & 255); PPU.vram.data = attr_byte; } /* Sound section */ #define CPU 1789773 /* NTSC */ unsigned int get_timer(int freq) { return (unsigned int) ((CPU / 16 / freq) - 1); } void play_pulse(unsigned char idx, unsigned int freq, unsigned char len) { unsigned int tm = get_timer(freq); if(len > 31) len = 31; if(idx > 1) idx = 0; APU.pulse[idx].period_low = tm & 255; APU.pulse[idx].len_period_high = (tm >> 8) | (len << 3); } void play_jingle(unsigned int n1f1, unsigned int n1f2, unsigned int n2f1, unsigned int n2f2, unsigned int n3f1, unsigned int n3f2) { unsigned int i, dl = 2000; play_pulse(0, n1f1, 2); play_pulse(1, n1f2, 2); for(i=0;i>1) - 1, room_mid_y = room_start_y + ((room_end_y - room_start_y)>>1) - 1; for(i=0;i 1) { switch(joy_input_wait()) { case KEY_A: res = 1; break; case KEY_B: res = 0; break; default: res = 2; } } beep_interact(); return res; } /* Output method */ void print_msg(char *s) { memset(status_msg, 0, sizeof(status_msg)); strncpy(status_msg, s, sizeof(status_msg)); render_main_scene(); } /* Array shuffle method */ void shuffle(char *array, unsigned int n) { unsigned int i, j, t; if(n > 1) { for(i=n-1;i>0;i--) { j = (unsigned int) (rand()%(i+1)); t = array[j]; array[j] = array[i]; array[i] = t; } } } /* Room item handling methods */ void handle_monster(int val) { int healthlost = val; char monster_msg[(SCREEN_WIDTH-1)]; beep_enemy(); if(weapon > 0 && val <= durability) { int weapondiff = val - weapon; if(weapondiff < 0) weapondiff = 0; snprintf(monster_msg, sizeof(status_msg), "%d-level monster: use weapon?", val); if(confirm(monster_msg)) { healthlost = weapondiff; durability = val - 1; if(durability < 2) { durability = weapon = 0; } } } else { snprintf(status_msg, sizeof(status_msg), "You fight a %d-level monster.", val); } hp -= healthlost; if(hp < 0) hp = 0; snprintf(status_msg, sizeof(status_msg), "You lost %d HP. New HP: %d", healthlost, hp); } void handle_weapon(int val) { weapon = val; snprintf(status_msg, sizeof(status_msg), "You equip a %d-level weapon.", weapon); durability = 14; /* reset durability to max limit */ beep_weapon(); } void handle_potion(unsigned char val) { if(potion_drank) { print_msg("Already drank a potion here."); beep_err(); } else { potion_drank = 1; hp += val; if(hp > 20) hp = 20; snprintf(status_msg, sizeof(status_msg), "You drink a %d-level potion.", val); if(deck_idx >= sizeof(deck) || deck[deck_idx] == 0) last_potion_val = val; beep_potion(); } } /* Main gameplay */ int game() { unsigned int i; int score = 0; /* score storage */ int item, room_sum; /* true item value */ int last_room = 0, last_room_clr = 0; /* last room markers */ char ch; deck_idx = 0; /* reset the deck index */ /* Prefill the deck */ for(i=0;i<13;i++) deck[i] = deck[i+13] = i+1; for(i=26;i<44;i++) deck[i] = i - 12; /* Shuffle the deck */ shuffle(deck, sizeof(deck)); /* prepare all other parameters */ hp = 20; can_run = 1; weapon = durability = engaged = rooms_cleared = 0; potion_drank = last_potion_val = 0; for(i=0;i 0 && !last_room_clr) { /* fill in the new room unless engaged */ if(!engaged) { for(i=0;i= sizeof(deck) || deck[deck_idx] == 0) { last_room = 1; can_run = 0; } else { room[i] = deck[deck_idx]; deck[deck_idx] = 0; /* remove from deck */ deck_idx++; /* Check immediately if that was the last card */ if(deck_idx >= sizeof(deck) || deck[deck_idx] == 0) { last_room = 1; can_run = 0; } } } } } /* display the stats and room */ render_main_scene(); ch = get_choice(); /* Logic validation */ if(ch == CH_R) { if(!can_run || engaged) ch = CH_INVAL; /* Cannot run if disabled or engaged */ } else if(ch < CH_R && !room[(int)ch]) { ch = CH_INVAL; /* empty item */ } if(ch == CH_Q) { hp = 0; break; } else if(ch == CH_INVAL) { print_msg("Invalid input!"); beep_err(); continue; } else if(ch == CH_R) { /* run mechanics */ /* Compact and rearrange the deck */ char temp_deck[sizeof(deck)]; int t_idx = 0; /* 1. Collect all valid remaining cards from deck */ for(i=0; i 0) { if(room_sum == 0) { if(last_room) last_room_clr = 1; else { engaged = 0; potion_drank = 0; can_run = 1; rooms_cleared++; } } else if(!last_room && engaged == 3) { engaged = 0; potion_drank = 0; can_run = 1; rooms_cleared++; } } } } /* Score the result */ if(hp > 0) { /* win */ score = hp; if(hp == 20) score += last_potion_val; } else { /* fail */ for(i=0;i 0) { win_screen(score); break; } else { beep_lose(); snprintf(status_msg, sizeof(status_msg), "RIP! Score: %d. Restart?", score); if(!confirm(status_msg)) break; } } return 0; }