Files
Quadrapassel/main.c
T

404 lines
17 KiB
C

/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <coco.h>
#include <cardgame.h>
#include <Player.h>
#include "rules.h"
#include "input.h"
#include "keyboard.h"
#include "preferences.h"
#include "video.h"
#include "disk_store.h"
#define KEY(name) isKeyPressed(KEY_PROBE_##name,KEY_BIT_##name)
enum { TITLE,PLAY,PAUSED,SETTINGS,CONTROLS,SCORES,CREDITS,NEW_CONFIRM,QUIT_CONFIRM,RULE_CHANGE,SEED_EDIT,INITIALS };
static QGame game;
static QPreferences preferences;
static QInput input;
static Q8 scene=TITLE,selected,parent=TITLE,active,score_eligible=1,rule_pending;
static Q8 joystick_axes[4],joystick_buttons,dirty=1,draw_flags=3,soft_before;
static Q16 gravity_ms;
static Q8 ui_scene=255;
static Q16 clock_fraction;
static Q8 seed_digit,save_status,score_category,initial_guard=1,last_initial;
static char initials[3]={'A','A','A'};
static QScore score_table[10];
static const Q8 title_items[]={37,38,39,40,41,42};
static const Q8 pause_items[]={43,44,45,38,46,42,47};
static const Q8 setting_names[]={48,49,50,51,52,53,54,55,56,57,8,58,59,47};
static void enter(Q8 next)
{
ui_scene=255;scene=next;selected=0;dirty=1;q_input_release(&input);
gravity_ms=0;soft_before=0;
if(next==PLAY)draw_flags=3;
if(next==INITIALS){initial_guard=1;last_initial=0;}
}
static void tone(Q8 event)
{
/* Half the desired Hz compensates this API for stock CoCo 3 fast mode. */
static const Q16 half_frequencies[7]={210,440,120,330,440,550,90};
static Q8 durations[7]={5,5,5,6,8,10,12};
if(preferences.sound){
/* JOYSTK and audio share the mux: select DAC only for this effect. */
initSound();
beep(durations[event],getTicksForFrequency(half_frequencies[event]),event?20:7);
Player_disableSound();
}
}
static void new_game(void)
{
QOptions options=preferences.game;
if(!preferences.custom_seed)options.seed=((Q32)getTimer()<<16)|((Q32)getTimer()+1);
q_start(&game,&options);active=1;score_eligible=1;enter(PLAY);
}
static QActions sample(Q16 ms)
{
const byte *axes;
const Q8 *center,*position;
Q16 keys;
Q8 i,port=preferences.port,index=port==1?2:0,button=0;
/* BASIC's IRQ and analog/keyboard polling share the PIA selector. */
disableInterrupts();
Player_disableSound();
axes=readJoystickPositions();for(i=0;i<4;++i)joystick_axes[i]=axes[i];
keys=keyboard_read(&joystick_buttons,scene==PLAY?(Q8)(KEYBOARD_PLAY|(preferences.clockwise<<1)):scene==INITIALS?(Q8)KEYBOARD_INITIALS:(Q8)0,port);
Player_disableSound();
enableInterrupts();
if(port<2)button=!(joystick_buttons&(port==1?JOYSTK_BUTTON_1_LEFT:JOYSTK_BUTTON_1_RIGHT));
/* Keep independent base pointers: CMOC -O2 can remove the index reload
* between adjacent byte-array arguments and use the prior byte as index. */
center=preferences.centers+index;position=joystick_axes+index;
return q_input_step(&input,keys,button,position[0],position[1],
center[0],center[1],port<2,ms,scene==PLAY,preferences.clockwise);
}
static const char *ui_label(Q8 id)
{
Q8 *data=video_menu_buffer();Q8 i;Q16 offset;
if(ui_scene!=scene){
if(disk_store_menu(scene,data))return "TEXT ERROR";
ui_scene=scene;
}
for(i=0;i<data[5];++i)if(data[8+i*3]==id){
offset=((Q16)data[9+i*3]<<8)|data[10+i*3];return (char *)data+offset;
}
return "TEXT ERROR";
}
static void menu_line(Q8 row,Q8 n,const char *name)
{
video_text(2,row,n==selected?">":" ");video_text(4,row,name);
}
static Q8 save_changes(signed char category,const QScore *scores,Q8 reset_all)
{
video_text(1,23,"SAVING TO DISK... ");
save_status=q_store_save(&preferences,category,scores,reset_all);
return save_status;
}
static void show_store_status(void)
{
if(save_status==Q_STORE_RECOVERED)video_text(1,23,"RECOVERED PREVIOUS SAVE");
else if(save_status==Q_STORE_CORRUPT)video_text(1,23,"BAD SAVE - CHECK DISK");
else if(save_status)video_text(1,23,"DISK ERROR - NOT SAVED");
}
static void game_over(void)
{
video_game(&game,preferences.shadow,draw_flags|2);
score_category=q_score_category(game.options.difficulty,game.options.difficult);
save_status=q_store_scores(score_category,score_table);
if(score_eligible&&game.score&&(save_status||game.score>score_table[9].score)){
initials[0]=initials[1]=initials[2]='A';enter(INITIALS);
}else enter(PAUSED);
}
static void settings_picture(void)
{
Q8 i,row;
video_text(2,0,ui_label(38));video_text(1,1,ui_label(60));
video_text(1,2,ui_label(61));
for(i=0;i<14;++i){
row=i+4;menu_line(row,i,ui_label(setting_names[i]));
if(i==0){if(preferences.game.difficulty<0)video_text(26,row,ui_label(62));else video_number(27,row,preferences.game.difficulty);}
if(i==1)video_text(18,row,preferences.game.scoring?ui_label(63):ui_label(64));
if(i==2)video_text(24,row,preferences.style==0?ui_label(65):preferences.style==1?ui_label(64):ui_label(66));
if(i==3)video_text(27,row,preferences.grid?ui_label(67):ui_label(68));
if(i==4)video_text(27,row,preferences.shadow?ui_label(67):ui_label(68));
if(i==5)video_text(27,row,preferences.sound?ui_label(67):ui_label(68));
if(i==6)video_text(27,row,preferences.clockwise?ui_label(69):ui_label(70));
if(i==7)video_text(27,row,preferences.game.difficult?ui_label(67):ui_label(68));
if(i==8)video_text(27,row,preferences.custom_seed?ui_label(67):ui_label(68));
if(i==10)video_text(24,row,preferences.port==0?ui_label(71):preferences.port==1?ui_label(72):ui_label(68));
if(i==12)video_number(27,row,preferences.hz);
}
video_text(1,20,ui_label(73));
video_text(1,22,ui_label(74));
}
/* Caption columns fit three bits; keep scene and column in one byte. */
static const Q8 captions[][3]={
{5,20,1},
{6,22,2},
{17,20,3},
{33,1,4},
{33,3,5},
{33,5,6},
{33,7,7},
{33,10,8},
{33,12,9},
{33,14,10},
{33,16,11},
{33,18,12},
{33,19,13},
{33,20,14},
{49,1,15},
{49,3,16},
{49,5,17},
{49,6,18},
{49,7,19},
{49,9,20},
{49,11,21},
{49,12,22},
{49,14,23},
{49,16,24},
{73,2,25},
{73,5,26},
{73,7,94},
{81,2,27},
{81,14,28},
{81,16,29},
{41,0,30},
{41,4,31},
{41,18,32},
{41,20,33},
{90,1,34},
{89,19,35},
{89,21,36},
};
static void fixed_captions(void)
{
const Q8 *p=(const Q8 *)&captions,*end=p+sizeof(captions);
for(;p!=end;p+=3)if((*p>>3)==scene)video_text(*p&7,p[1],ui_label(p[2]));
}
static void picture(void)
{
Q8 i;char ch[2];
if(scene==PLAY){video_game(&game,preferences.shadow,draw_flags);draw_flags=0;return;}
video_clear();
if(scene!=TITLE)fixed_captions();
if(scene==TITLE){
video_menu_buffer();video_title_art();video_big_title();fixed_captions();
for(i=0;i<6;++i)menu_line(12+i,i,ui_label(title_items[i]));
}else if(scene==PAUSED){
video_text(2,1,game.over?ui_label(75):ui_label(76));
for(i=0;i<7;++i)menu_line(4+i*2,i,
i==1&&(game.hold_used||game.over)?ui_label(77):
i==2&&game.over?ui_label(78):ui_label(pause_items[i]));
}else if(scene==SETTINGS)settings_picture();
else if(scene==CONTROLS){
video_number(11,18,joystick_axes[0]);video_number(16,18,joystick_axes[1]);
video_number(11,19,joystick_axes[2]);video_number(16,19,joystick_axes[3]);
video_number(11,20,joystick_buttons);menu_line(23,0,ui_label(47));
}else if(scene==CREDITS){
menu_line(22,0,ui_label(47));
}else if(scene==NEW_CONFIRM||scene==QUIT_CONFIRM){
video_text(2,3,scene==NEW_CONFIRM?ui_label(79):ui_label(80));video_text(2,6,ui_label(81));
menu_line(10,0,ui_label(82));menu_line(12,1,ui_label(83));
}else if(scene==RULE_CHANGE){
menu_line(9,0,ui_label(84));menu_line(11,1,ui_label(85));menu_line(13,2,ui_label(82));
}else if(scene==SEED_EDIT){
for(i=0;i<8;++i){Q8 n=(Q8)((preferences.game.seed>>((7-i)*4))&15);ch[0]="0123456789ABCDEF"[n];ch[1]=0;video_text(7+i*2,8,ch);}
video_text(7+seed_digit*2,10,ui_label(86));
}else if(scene==SCORES){
if(score_category<2)video_text(1,2,ui_label(87));else {video_text(1,2,ui_label(48));video_number(12,2,score_category/2-1);}
video_text(17,2,(score_category&1)?ui_label(88):ui_label(89));
for(i=0;i<10;++i){
video_number(1,6+i,i+1);
if(score_table[i].score){
char name[4];name[0]=score_table[i].initials[0];name[1]=score_table[i].initials[1];name[2]=score_table[i].initials[2];name[3]=0;
video_text(6,6+i,name);video_number(11,6+i,score_table[i].score);
video_text(25,6+i,score_table[i].rule==Q_CLASSIC?ui_label(90):ui_label(91));
}
}
menu_line(22,0,ui_label(47));
}else if(scene==INITIALS){
video_number(2,3,game.score);
for(i=0;i<3;++i){
menu_line(7+i*2,i,ui_label(92));video_number(12,7+i*2,i+1);
ch[0]=initials[i];ch[1]=0;video_text(20,7+i*2,ch);
}
menu_line(14,3,ui_label(93));menu_line(16,4,ui_label(82));
}
show_store_status();
}
static Q8 item_count(void)
{
if(scene==TITLE)return 6;if(scene==PAUSED)return 7;if(scene==SETTINGS)return 14;
if(scene==INITIALS)return 5;
if(scene==NEW_CONFIRM||scene==QUIT_CONFIRM)return 2;if(scene==RULE_CHANGE)return 3;return 1;
}
/* Focus-only movement keeps menus responsive without clearing or reloading text. */
static void select_item(signed char delta)
{
static const Q8 starts[12]={12,0,4,4,23,22,22,10,10,9,0,7};
static const Q8 spacing[12]={1,0,2,1,0,0,0,2,2,2,0,2};
Q8 count=item_count(),old=selected,row;
selected=(selected+count+delta)%count;
row=starts[scene]+old*spacing[scene];if(scene==INITIALS&&old>=3)++row;
video_text(2,row," ");
row=starts[scene]+selected*spacing[scene];if(scene==INITIALS&&selected>=3)++row;
video_text(2,row,">");
}
static void change_setting(signed char delta)
{
int n;
switch(selected){
case 0:n=preferences.game.difficulty+delta;if(n<-1)n=15;if(n>15)n=-1;preferences.game.difficulty=(signed char)n;break;
case 1:rule_pending=!preferences.game.scoring;enter(RULE_CHANGE);return;
case 2:preferences.style=(preferences.style+(delta>0?1:2))%3;break;
case 3:preferences.grid=!preferences.grid;break;
case 4:preferences.shadow=!preferences.shadow;break;
case 5:preferences.sound=!preferences.sound;break;
case 6:preferences.clockwise=!preferences.clockwise;break;
case 7:preferences.game.difficult=!preferences.game.difficult;break;
case 8:preferences.custom_seed=!preferences.custom_seed;break;
case 9:seed_digit=0;enter(SEED_EDIT);return;
case 10:preferences.port=(preferences.port+(delta>0?1:2))%3;q_input_reset(&input);break;
case 11:memcpy(preferences.centers,joystick_axes,4);q_input_reset(&input);break;
case 12:preferences.hz=preferences.hz==60?50:60;clock_fraction=0;break;
default:save_changes(-1,0,0);enter(parent);return;
}
video_style(preferences.style,preferences.grid);dirty=1;
}
static Q8 choose(void)
{
if(scene==TITLE){
if(selected==0)new_game();else if(selected==1){parent=TITLE;enter(SETTINGS);}
else if(selected==2)enter(CONTROLS);else if(selected==3){
parent=TITLE;score_category=q_score_category(preferences.game.difficulty,preferences.game.difficult);
save_status=q_store_scores(score_category,score_table);enter(SCORES);
}
else if(selected==4)enter(CREDITS);else return 0;
}else if(scene==PAUSED){
if(selected==0||selected==6)enter(game.over?TITLE:PLAY);
else if(selected==1){if(!game.over&&q_hold(&game)){tone(1);enter(PLAY);}else dirty=1;}
else if(selected==2){if(!game.over){if(q_rotate(&game,!preferences.clockwise))tone(1);enter(PLAY);}else dirty=1;}
else if(selected==3){parent=PAUSED;enter(SETTINGS);}
else if(selected==4)enter(NEW_CONFIRM);else enter(QUIT_CONFIRM);
}else if(scene==SETTINGS)change_setting(1);
else if(scene==NEW_CONFIRM){if(selected)new_game();else enter(PAUSED);}
else if(scene==QUIT_CONFIRM){if(selected){active=0;enter(TITLE);}else enter(PAUSED);}
else if(scene==RULE_CHANGE){
if(selected<2){
Q8 old_rule=preferences.game.scoring;preferences.game.scoring=rule_pending;
if(selected==1){
if(!save_changes(-1,0,1)){if(active)score_eligible=0;}
else preferences.game.scoring=old_rule;
}else save_changes(-1,0,0);
}
enter(SETTINGS);
}else if(scene==SEED_EDIT)enter(SETTINGS);
else if(scene==INITIALS){
if(selected<3){initials[selected]=initials[selected]=='Z'?'A':initials[selected]+1;dirty=1;}
else if(selected==3){
save_status=q_store_scores(score_category,score_table);
if(!save_status){
q_score_insert(score_table,game.score,initials,game.options.scoring);
save_changes((signed char)score_category,score_table,0);
}
if(!save_status){score_eligible=0;parent=PAUSED;enter(SCORES);}else dirty=1;
}else {score_eligible=0;enter(PAUSED);}
}else if(scene==SCORES)enter(parent);
else enter(TITLE);
return 1;
}
int main(void)
{
Q16 now,last,ms,scaled,ticks;QActions action;Q8 i,changed,info,old_over,full_scene;
initCoCoSupport();setHighSpeed(FALSE);
if(!isCoCo3){putstr("COCO 3 - 128 KB MINIMUM\n",sizeof("COCO 3 - 128 KB MINIMUM\n")-1);return 1;}
if(!video_open(1))return 2;
q_preferences_default(&preferences);
setHighSpeed(TRUE);Player_disableSound();video_text(4,12,"READING SCORES");
save_status=disk_store_open(&preferences);
video_style(preferences.style,preferences.grid);q_input_reset(&input);last=getTimer();
for(;;){
now=getTimer();ticks=(Q16)(now-last);last=now;
if(ticks>preferences.hz)ticks=preferences.hz;
scaled=ticks*1000+clock_fraction;ms=scaled/preferences.hz;clock_fraction=scaled%preferences.hz;
if(ms>1000)ms=1000;
action=sample(ms);
if(scene==PLAY){
if(action.pressed&(Q_PAUSE|Q_BACK)){enter(PAUSED);continue;}
changed=info=0;old_over=game.over;
for(i=0;i<action.repeats;++i)if(q_move(&game,action.move,0)){changed=1;tone(0);}
if(action.pressed&Q_CCW){if(q_rotate(&game,0)){changed=1;tone(1);}}
else if(action.pressed&Q_CW){if(q_rotate(&game,1)){changed=1;tone(1);}}
if(action.pressed&Q_HOLD){if(q_hold(&game)){changed=info=1;tone(1);}}
if(action.pressed&Q_HARD){if(q_drop(&game)){gravity_ms=0;changed=info=1;tone(game.last_clear>=3?5:game.last_clear?2+game.last_clear:2);}}
else {
if((action.held&Q_DOWN)&&!soft_before){changed|=q_move(&game,0,1);gravity_ms=0;}
soft_before=(action.held&Q_DOWN)!=0;
gravity_ms+=ms;
while(gravity_ms>=(soft_before?(Q16)80:q_gravity_ms(game.level))&&!game.over){
gravity_ms-=soft_before?(Q16)80:q_gravity_ms(game.level);
if(!q_move(&game,0,1)){q_lock(&game);info=1;tone(game.last_clear>=3?5:game.last_clear?2+game.last_clear:2);}changed=1;
}
}
if(game.over&&!old_over){tone(6);changed=1;}
if(changed){draw_flags|=info?2:0;dirty=1;}
if(game.over)game_over();
}else{
if(scene==INITIALS){
Q8 c=inkey();
if(!c){initial_guard=0;last_initial=0;}
else if(!initial_guard&&c!=last_initial&&c>='A'&&c<='Z'&&selected<3){
initials[selected]=c;selected=(selected+1)%3;last_initial=c;dirty=1;
}
if(selected<3&&(action.pressed&(Q_LEFT|Q_RIGHT))){
initials[selected]=(char)('A'+(initials[selected]-'A'+((action.pressed&Q_LEFT)?25:1))%26);dirty=1;
}
}
if(scene==SEED_EDIT){
Q8 shift=(7-seed_digit)*4;Q32 mask=(Q32)15<<shift,n=(preferences.game.seed>>shift)&15;
if(action.pressed&Q_LEFT){seed_digit=(seed_digit+7)&7;dirty=1;}
if(action.pressed&Q_RIGHT){seed_digit=(seed_digit+1)&7;dirty=1;}
if(action.pressed&(Q_UP|Q_DOWN)){n=(n+((action.pressed&Q_UP)?1:15))&15;preferences.game.seed=(preferences.game.seed&~mask)|(n<<shift);dirty=1;}
}else{
if(action.pressed&Q_UP)select_item(-1);
if(action.pressed&Q_DOWN)select_item(1);
if(scene==SETTINGS&&(action.pressed&(Q_LEFT|Q_RIGHT)))change_setting(action.pressed&Q_LEFT?-1:1);
if(scene==SCORES&&(action.pressed&(Q_LEFT|Q_RIGHT))){
score_category=(score_category+((action.pressed&Q_LEFT)?33:1))%34;
save_status=q_store_scores(score_category,score_table);dirty=1;
}
}
if(action.pressed&Q_CONFIRM){if(!choose())break;}
if(action.pressed&(Q_BACK|Q_PAUSE)){
if(scene==PAUSED)enter(game.over?TITLE:PLAY);
else if(scene==SETTINGS){save_changes(-1,0,0);enter(parent);}
else if(scene==INITIALS){score_eligible=0;enter(PAUSED);}
else if(scene==RULE_CHANGE||scene==SEED_EDIT)enter(SETTINGS);
else if(scene==NEW_CONFIRM||scene==QUIT_CONFIRM)enter(PAUSED);
else enter(TITLE);
}
}
if(dirty){full_scene=scene!=PLAY||(draw_flags&1);picture();dirty=0;if(full_scene)last=getTimer();}
}
disk_store_close();video_close();return 0;
}