/* SPDX-License-Identifier: GPL-3.0-or-later */ #include #include #include #include "rules.h" #include "input.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_reset(&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 const 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); } typedef struct { Q8 probe,bit;Q16 action; } KeyBinding; #define BIND(name,action) {KEY_PROBE_##name,KEY_BIT_##name,action} static const KeyBinding keys[]={ BIND(LEFT,Q_LEFT),BIND(RIGHT,Q_RIGHT),BIND(UP,Q_UP),BIND(DOWN,Q_DOWN), BIND(ENTER,Q_CONFIRM),BIND(BREAK,Q_BACK), BIND(A,Q_LEFT),BIND(D,Q_RIGHT),BIND(W,Q_UP),BIND(S,Q_DOWN), BIND(Q,Q_CCW),BIND(E,Q_CW),BIND(SPACE,Q_CONFIRM),BIND(H,Q_HOLD),BIND(P,Q_PAUSE) }; static Q16 keyboard(void) { Q16 held=0,action;Q8 i,count=scene==INITIALS?6:sizeof(keys)/sizeof(keys[0]); for(i=0;i":" ");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 DATA - CHECK DISK"); else if(save_status)video_text(1,23,"DISK ERROR - CHANGES 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)); } static const Q8 captions[][4]={ {0,5,20,1}, {0,6,22,2}, {2,1,20,3}, {4,1,1,4}, {4,1,3,5}, {4,1,5,6}, {4,1,7,7}, {4,1,10,8}, {4,1,12,9}, {4,1,14,10}, {4,1,16,11}, {4,1,18,12}, {4,1,19,13}, {4,1,20,14}, {6,1,1,15}, {6,1,3,16}, {6,1,5,17}, {6,1,6,18}, {6,1,7,19}, {6,1,9,20}, {6,1,11,21}, {6,1,12,22}, {6,1,14,23}, {6,1,16,24}, {9,1,2,25}, {9,1,5,26}, {9,1,7,94}, {10,1,2,27}, {10,1,14,28}, {10,1,16,29}, {5,1,0,30}, {5,1,4,31}, {5,1,18,32}, {5,1,20,33}, {11,2,1,34}, {11,1,19,35}, {11,1,21,36}, }; static void fixed_captions(void) { Q8 i;for(i=0;i>((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;break; case 11:preferences.centers[0]=joystick_axes[0];preferences.centers[1]=joystick_axes[1]; preferences.centers[2]=joystick_axes[2];preferences.centers[3]=joystick_axes[3];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 VERSION - 128 KB MINIMUM\n",sizeof("COCO 3 VERSION - 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 SAVED 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=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)&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<