Skip to content

Commit

Permalink
Merge pull request #102 from thpatch/thecl/185-diff-format
Browse files Browse the repository at this point in the history
thecl: better support for th185 diffs
  • Loading branch information
Priw8 authored Aug 15, 2023
2 parents a163060 + 08bb5af commit 665a016
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
28 changes: 19 additions & 9 deletions thecl/ecsparse.y
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,7 @@ parse_rank(
const parser_state_t* state,
const char* value)
{
int rank = state->has_overdrive_difficulty ? 0xC0 : 0xF0;
int rank = get_default_none_rank(state->version);

if (check_rank_flag(state, value, '*')) {
if (strlen(value) != 1) {
Expand All @@ -2152,6 +2152,16 @@ parse_rank(
state->current_sub->name);
}
return rank;
} else if (state->has_numeric_difficulties) {
if (check_rank_flag(state, value, '0')) rank |= RANK_ID_0;
if (check_rank_flag(state, value, '1')) rank |= RANK_ID_1;
if (check_rank_flag(state, value, '2')) rank |= RANK_ID_2;
if (check_rank_flag(state, value, '3')) rank |= RANK_ID_3;
if (check_rank_flag(state, value, '4')) rank |= RANK_ID_4;
if (check_rank_flag(state, value, '5')) rank |= RANK_ID_5;
if (check_rank_flag(state, value, '6')) rank |= RANK_ID_6;
if (check_rank_flag(state, value, '7')) rank |= RANK_ID_7;
return rank;
} else {
if (check_rank_flag(state, value, 'E')) rank |= RANK_EASY;
if (check_rank_flag(state, value, 'N')) rank |= RANK_NORMAL;
Expand Down Expand Up @@ -2495,8 +2505,7 @@ expression_output(
instr_add(state->current_sub, instr_new(state, expr->id, ""));
} else if (expr->type == EXPRESSION_RANK_SWITCH) {

const int diff_amt = state->has_overdrive_difficulty ? 5 : 4;
const char* diffs[5] = {"E", "N", "H", "L", "O"};
const int diff_amt = state->has_numeric_difficulties ? 8 : (state->has_overdrive_difficulty ? 5 : 4);

int diff = 0;
int rank_none = parse_rank(state, "-");
Expand All @@ -2506,11 +2515,12 @@ expression_output(
expression_t* last_expr = NULL;
list_for_each(&expr->children, iter_expr) {
if (last_expr != NULL) {
state->instr_rank = rank_org & parse_rank(state, diffs[diff++]);
state->instr_rank = rank_org & (1 << diff);
++diff;
if (state->instr_rank != rank_none) expression_output(state, last_expr, 1);
}

if (diff > 4) {
if (diff > diff_amt) {
yyerror(state, "too many parameters for difficulty switch");
exit(2);
}
Expand All @@ -2519,13 +2529,13 @@ expression_output(
}

/* Set last expr to all remaining difficulties. */
char diff_str[5] = "";
int last_diff = 0;
while(diff < diff_amt) {
const char* next_diff = diffs[diff++];
strcat(diff_str, next_diff);
last_diff = last_diff | (1 << diff);
++diff;
}

state->instr_rank = rank_org & parse_rank(state, diff_str);
state->instr_rank = rank_org & last_diff;
if (state->instr_rank != rank_none) expression_output(state, last_expr, 1);

state->instr_rank = rank_org;
Expand Down
2 changes: 1 addition & 1 deletion thecl/ecsscan.l
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ ins_[0-9]+ {
return INTEGER;
}

![-*ENHLWXYZO4567]+ {
![-*ENHLWXYZO01234567]+ {
yylval.string = strdup(yytext + 1);
return RANK;
}
Expand Down
11 changes: 11 additions & 0 deletions thecl/thecl.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ is_post_th13(unsigned int version) {
}
}

int32_t
get_default_none_rank(unsigned int version) {
if (!is_post_th13(version)) {
return 0xF0;
} else if (version == 185) {
return 0x00;
} else {
return 0xC0;
}
}

static void
print_usage(void)
{
Expand Down
12 changes: 11 additions & 1 deletion thecl/thecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ typedef enum {
#define RANK_EXTRA (1 << 4)
#define RANK_OVERDRIVE (1 << 5)

/* Used to describe unused ranks */
/* Used to describe unused ranks in some games and ranks in general in th185 */
#define RANK_ID_0 (1 << 0)
#define RANK_ID_1 (1 << 1)
#define RANK_ID_2 (1 << 2)
#define RANK_ID_3 (1 << 3)
#define RANK_ID_4 (1 << 4)
#define RANK_ID_5 (1 << 5)
#define RANK_ID_6 (1 << 6)
Expand Down Expand Up @@ -105,6 +109,11 @@ bool is_post_th10(
bool is_post_th13(
unsigned int version);

/* Returns the rank value for the '!-' label. */
int32_t
get_default_none_rank(
unsigned int version);

typedef struct thecl_instr_t {
thecl_instr_type type;
char* string;
Expand Down Expand Up @@ -242,6 +251,7 @@ typedef struct {
unsigned int version;
bool uses_numbered_subs;
bool has_overdrive_difficulty;
bool has_numeric_difficulties;
bool uses_stack_offsets;
bool is_timeline_sub; /* Variable for escparse.y */
list_t expressions;
Expand Down
2 changes: 1 addition & 1 deletion thecl/thecl06.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ th06_dump(
break;
case THECL_INSTR_RANK:
if(instr->rank == 0xFF) fprintf(out, "!*");
else if(instr->rank == 0xF0) fprintf(out, "!-");
else if(instr->rank == get_default_none_rank(ecl->version)) fprintf(out, "!-");
else {
fprintf(out, "!%s%s%s%s%s%s%s%s",
(instr->rank) & RANK_EASY ? "E" : "",
Expand Down
14 changes: 13 additions & 1 deletion thecl/thecl10.c
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,18 @@ th10_dump(
case THECL_INSTR_RANK:
if(instr->rank == 0xFF)
instr->string = strdup("!*");
else if(instr->rank == (is_post_th13(ecl->version) ? 0xC0 : 0xF0))
else if (ecl->version == 185) {
sprintf(temp, "!%s%s%s%s%s%s%s%s",
(instr->rank) & RANK_ID_0 ? "0" : "",
(instr->rank) & RANK_ID_1 ? "1" : "",
(instr->rank) & RANK_ID_2 ? "2" : "",
(instr->rank) & RANK_ID_3 ? "3" : "",
(instr->rank) & RANK_ID_4 ? "4" : "",
(instr->rank) & RANK_ID_5 ? "5" : "",
(instr->rank) & RANK_ID_6 ? "6" : "",
(instr->rank) & RANK_ID_7 ? "7" : "");
instr->string = strdup(temp);
} else if(instr->rank == get_default_none_rank(ecl->version))
instr->string = strdup("!-");
else {
if (is_post_th13(ecl->version)) {
Expand Down Expand Up @@ -2020,6 +2031,7 @@ th10_parse(
state.version = version;
state.uses_numbered_subs = false;
state.has_overdrive_difficulty = is_post_th13(version);
state.has_numeric_difficulties = version == 185;
state.uses_stack_offsets = is_post_th13(version);
list_init(&state.expressions);
list_init(&state.block_stack);
Expand Down

0 comments on commit 665a016

Please sign in to comment.