<commit_before>#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>

#include "utils/log.h"
#include "utils/random.h"
#include "audio/music.h"
#include "audio/sound.h"
#include "video/video.h"
#include "resources/ids.h"
#include "resources/bk.h"
#include "resources/pilots.h"
#include "resources/animation.h"
#include "resources/sprite.h"
#include "game/gui/text_render.h"
#include "resources/languages.h"
#include "game/utils/settings.h"
#include "game/game_state.h"
#include "game/protos/scene.h"
#include "game/protos/object.h"
#include "game/scenes/melee.h"
#include "game/gui/progressbar.h"
#include "game/gui/menu_background.h"

#define MAX_STAT 20

typedef struct melee_local_t {

    int selection; // 0 for player, 1 for HAR
    int row_a, row_b; // 0 or 1
    int column_a, column_b; // 0-4
    int done_a, done_b; // 0-1

    object bigportrait1;
    object bigportrait2;
    object player2_placeholder;
    object unselected_har_portraits;

    object pilots[10];

    object harportraits_player1[10];
    object harportraits_player2[10];

    object har_player1[10];
    object har_player2[10];

    component *bar_power[2];
    component *bar_agility[2];
    component *bar_endurance[2];

    int pilot_id_a;
    int pilot_id_b;

    surface feh;
    surface bleh;
    surface select_hilight;
    unsigned int ticks;
    unsigned int hartick;
    unsigned int pulsedir;

    object *harplayer_a;
    object *harplayer_b;

    // nova selection cheat
    unsigned char har_selected[2][10];
    unsigned char katana_down_count[2];
} melee_local;

void handle_action(scene *scene, int player, int action);

void mask_sprite(surface *vga, int x, int y, int w, int h) {
    for(int i = 0; i < vga->h; i++) {
        for(int j = 0; j < vga->w; j++) {
            int offset = (i * vga->w) + j;
            if ((i < y || i > y+h) || (j < x || j > x+w)) {
                vga->stencil[offset] = 0;
            } else {
                if (vga->data[offset] == -48) {
                    // strip out the black pixels
                    vga->stencil[offset] = 0;
                } else {
                    vga->stencil[offset] = 1;
                }
            }
        }
    }
}

void melee_free(scene *scene) {
    melee_local *local = scene_get_userdata(scene);
    game_player *player2 = game_state_get_player(scene->gs, 1);

    surface_free(&local->feh);
    surface_free(&local->bleh);
    surface_free(&local->select_hilight);
    for(int i = 0; i < 2; i++) {
        component_free(local->bar_power[i]);
        component_free(local->bar_agility[i]);
        component_free(local->bar_endurance[i]);
    }

    for(int i = 0; i < 10; i++) {
        object_free(&local->pilots[i]);
        object_free(&local->harportraits_player1[i]);
        object_free(&local->har_player1[i]);
        if (player2->selectable) {
            object_free(&local->harportraits_player2[i]);
            object_free(&local->har_player2[i]);
        }
    }

    object_free(&local->player2_placeholder);
    object_free(&local->unselected_har_portraits);
    object_free(&local->bigportrait1);
    if (player2->selectable) {
        object_free(&local->bigportrait2);
    }
    free(local);
}

void melee_tick(scene *scene, int paused) {
    melee_local *local = scene_get_userdata(scene);
    game_player *player1 = game_state_get_player(scene->gs, 0);
    game_player *player2 = game_state_get_player(scene->gs, 1);
    ctrl_event *i = NULL;

    // Handle extra controller inputs
    i = player1->ctrl->extra_events;
    if (i) {
        do {
            if(i->type == EVENT_TYPE_ACTION) {
                handle_action(scene, 1, i->event_data.action);
            } else if (i->type == EVENT_TYPE_CLOSE) {
                game_state_set_next(scene->gs, SCENE_MENU);
                return;
            }
        } while((i = i->next));
    }
    i = player2->ctrl->extra_events;
    if (i) {
        do {
            if(i->type == EVENT_TYPE_ACTION) {
                handle_action(scene, 2, i->event_data.action);
            } else if (i->type == EVENT_TYPE_CLOSE) {
                game_state_set_next(scene->gs, SCENE_MENU);
                return;
            }
        } while((i = i->next));
    }

    if(!local->pulsedir) {
        local->ticks++;
    } else {
        local->ticks--;
    }
    if(local->ticks > 120) {
        local->pulsedir = 1;
    }
    if(local->ticks == 0) {
        local->pulsedir = 0;
    }
    local->hartick++;
    if (local->selection == 1) {
        if(local->hartick > 10) {
            local->hartick = 0;
            object_dynamic_tick(&local->har_player1[5*local->row_a + local->column_a]);
            if (player2->selectable) {
                object_dynamic_tick(&local->har_player2[5*local->row_b + local->column_b]);
            }
        }
    }

}

void refresh_pilot_stats(melee_local *local) {
    int current_a = 5*local->row_a + local->column_a;
    int current_b = 5*local->row_b + local->column_b;
    pilot p_a, p_b;

    pilot_get_info(&p_a, current_a);
    pilot_get_info(&p_b, current_b);
    progressbar_set_progress(local->bar_power[0], (p_a.power*100)/MAX_STAT);
    progressbar_set_progress(local->bar_agility[0], (p_a.agility*100)/MAX_STAT);
    progressbar_set_progress(local->bar_endurance[0], (p_a.endurance*100)/MAX_STAT);
    progressbar_set_progress(local->bar_power[1], (p_b.power*100)/MAX_STAT);
    progressbar_set_progress(local->bar_agility[1], (p_b.agility*100)/MAX_STAT);
    progressbar_set_progress(local->bar_endurance[1], (p_b.endurance*100)/MAX_STAT);
}

void handle_action(scene *scene, int player, int action) {
    game_player *player1 = game_state_get_player(scene->gs, 0);
    game_player *player2 = game_state_get_player(scene->gs, 1);
    melee_local *local = scene_get_userdata(scene);
    int *row, *column, *done;
    if (player == 1) {
        row = &local->row_a;<commit_msg>game/scenes: Merge melee_tick() if statements<commit_after>#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>

#include "utils/log.h"
#include "utils/random.h"
#include "audio/music.h"
#include "audio/sound.h"
#include "video/video.h"
#include "resources/ids.h"
#include "resources/bk.h"
#include "resources/pilots.h"
#include "resources/animation.h"
#include "resources/sprite.h"
#include "game/gui/text_render.h"
#include "resources/languages.h"
#include "game/utils/settings.h"
#include "game/game_state.h"
#include "game/protos/scene.h"
#include "game/protos/object.h"
#include "game/scenes/melee.h"
#include "game/gui/progressbar.h"
#include "game/gui/menu_background.h"

#define MAX_STAT 20

typedef struct melee_local_t {

    int selection; // 0 for player, 1 for HAR
    int row_a, row_b; // 0 or 1
    int column_a, column_b; // 0-4
    int done_a, done_b; // 0-1

    object bigportrait1;
    object bigportrait2;
    object player2_placeholder;
    object unselected_har_portraits;

    object pilots[10];

    object harportraits_player1[10];
    object harportraits_player2[10];

    object har_player1[10];
    object har_player2[10];

    component *bar_power[2];
    component *bar_agility[2];
    component *bar_endurance[2];

    int pilot_id_a;
    int pilot_id_b;

    surface feh;
    surface bleh;
    surface select_hilight;
    unsigned int ticks;
    unsigned int hartick;
    unsigned int pulsedir;

    object *harplayer_a;
    object *harplayer_b;

    // nova selection cheat
    unsigned char har_selected[2][10];
    unsigned char katana_down_count[2];
} melee_local;

void handle_action(scene *scene, int player, int action);

void mask_sprite(surface *vga, int x, int y, int w, int h) {
    for(int i = 0; i < vga->h; i++) {
        for(int j = 0; j < vga->w; j++) {
            int offset = (i * vga->w) + j;
            if ((i < y || i > y+h) || (j < x || j > x+w)) {
                vga->stencil[offset] = 0;
            } else {
                if (vga->data[offset] == -48) {
                    // strip out the black pixels
                    vga->stencil[offset] = 0;
                } else {
                    vga->stencil[offset] = 1;
                }
            }
        }
    }
}

void melee_free(scene *scene) {
    melee_local *local = scene_get_userdata(scene);
    game_player *player2 = game_state_get_player(scene->gs, 1);

    surface_free(&local->feh);
    surface_free(&local->bleh);
    surface_free(&local->select_hilight);
    for(int i = 0; i < 2; i++) {
        component_free(local->bar_power[i]);
        component_free(local->bar_agility[i]);
        component_free(local->bar_endurance[i]);
    }

    for(int i = 0; i < 10; i++) {
        object_free(&local->pilots[i]);
        object_free(&local->harportraits_player1[i]);
        object_free(&local->har_player1[i]);
        if (player2->selectable) {
            object_free(&local->harportraits_player2[i]);
            object_free(&local->har_player2[i]);
        }
    }

    object_free(&local->player2_placeholder);
    object_free(&local->unselected_har_portraits);
    object_free(&local->bigportrait1);
    if (player2->selectable) {
        object_free(&local->bigportrait2);
    }
    free(local);
}

void melee_tick(scene *scene, int paused) {
    melee_local *local = scene_get_userdata(scene);
    game_player *player1 = game_state_get_player(scene->gs, 0);
    game_player *player2 = game_state_get_player(scene->gs, 1);
    ctrl_event *i = NULL;

    // Handle extra controller inputs
    i = player1->ctrl->extra_events;
    if (i) {
        do {
            if(i->type == EVENT_TYPE_ACTION) {
                handle_action(scene, 1, i->event_data.action);
            } else if (i->type == EVENT_TYPE_CLOSE) {
                game_state_set_next(scene->gs, SCENE_MENU);
                return;
            }
        } while((i = i->next));
    }
    i = player2->ctrl->extra_events;
    if (i) {
        do {
            if(i->type == EVENT_TYPE_ACTION) {
                handle_action(scene, 2, i->event_data.action);
            } else if (i->type == EVENT_TYPE_CLOSE) {
                game_state_set_next(scene->gs, SCENE_MENU);
                return;
            }
        } while((i = i->next));
    }

    if(!local->pulsedir) {
        local->ticks++;
    } else {
        local->ticks--;
    }
    if(local->ticks > 120) {
        local->pulsedir = 1;
    }
    if(local->ticks == 0) {
        local->pulsedir = 0;
    }
    local->hartick++;
    if (local->selection == 1 && local->hartick > 10) {
        local->hartick = 0;
        object_dynamic_tick(&local->har_player1[5*local->row_a + local->column_a]);
        if (player2->selectable) {
            object_dynamic_tick(&local->har_player2[5*local->row_b + local->column_b]);
        }
    }

}

void refresh_pilot_stats(melee_local *local) {
    int current_a = 5*local->row_a + local->column_a;
    int current_b = 5*local->row_b + local->column_b;
    pilot p_a, p_b;

    pilot_get_info(&p_a, current_a);
    pilot_get_info(&p_b, current_b);
    progressbar_set_progress(local->bar_power[0], (p_a.power*100)/MAX_STAT);
    progressbar_set_progress(local->bar_agility[0], (p_a.agility*100)/MAX_STAT);
    progressbar_set_progress(local->bar_endurance[0], (p_a.endurance*100)/MAX_STAT);
    progressbar_set_progress(local->bar_power[1], (p_b.power*100)/MAX_STAT);
    progressbar_set_progress(local->bar_agility[1], (p_b.agility*100)/MAX_STAT);
    progressbar_set_progress(local->bar_endurance[1], (p_b.endurance*100)/MAX_STAT);
}

void handle_action(scene *scene, int player, int action) {
    game_player *player1 = game_state_get_player(scene->gs, 0);
    game_player *player2 = game_state_get_player(scene->gs, 1);
    melee_local *local = scene_get_userdata(scene);
    int *row, *column, *done;
    if (player == 1) {
        row = &local->row_a;
{'fraction_non_alphanumeric': 0.14325581395348838, 'fraction_numerical': 0.018773784355179703, 'mean_word_length': 2.6898595943837753, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 165, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3359722', 'n_tokens_mistral': 4375, 'n_tokens_neox': 4152, 'n_words': 761}