searching speed issue

This commit is contained in:
Rorikstr | Rust Dev 2025-09-29 21:57:33 +03:00
parent 280cbee0a2
commit 98035f17a2
11 changed files with 69 additions and 64 deletions

View file

@ -2,6 +2,7 @@
#define TETRIS_H
#include <stdbool.h>
#include <stdio.h>
#define FIELD_WIDTH 10
#define FIELD_HEIGHT 20

View file

@ -54,6 +54,8 @@ GameState_t* get_game_state(void);
// Функции состояний
// init
void do_init(void);
int load_high_score();
void save_high_score(int score);
// spawn
void do_spawn(void);

View file

@ -1,27 +1,7 @@
#include "01_automato.h"
#include "../../logging.h"
int load_high_score() {
FILE* file = fopen("high_score.txt", "r");
int high_score = 0;
if (file) {
fscanf(file, "%d", &high_score);
fclose(file);
}
return high_score;
}
void save_high_score(int score) {
FILE* file = fopen("high_score.txt", "w");
if (file) {
fprintf(file, "%d", score);
fclose(file);
}
}
void userInput(UserAction_t action, bool hold) {
LOG_FUNCTION_START("userInput", "action=%d, hold=%d", action, hold);
(void)hold; // заглушка
GameState_t* state = get_game_state();
@ -29,7 +9,6 @@ void userInput(UserAction_t action, bool hold) {
if (state->info->pause &&
(action == Left || action == Right || action == Down || action == Up ||
action == Action || action == Start)) {
LOG_FUNCTION_END("userInput", "ignored movement command during pause, state=%d", state->state);
return;
}
@ -68,9 +47,7 @@ void userInput(UserAction_t action, bool hold) {
break;
default:
break;
}
LOG_FUNCTION_END("userInput", "state=%d", state->state);
}
}
GameInfo_t updateCurrentState() {

View file

@ -2,9 +2,27 @@
#include <stdlib.h>
#include "../../logging.h"
int load_high_score() {
FILE* file = fopen("high_score.txt", "r");
int high_score = 0;
if (file) {
if (fscanf(file, "%d", &high_score) != 1) {
high_score = 0; // Если не удалось прочитать, устанавливаем 0
}
fclose(file);
}
return high_score;
}
void save_high_score(int score) {
FILE* file = fopen("high_score.txt", "w");
if (file) {
fprintf(file, "%d", score);
fclose(file);
}
}
GameState_t* get_game_state(void) {
LOG_FUNCTION_START("get_game_state", "");
static GameState_t state = {0};
static int initialized = 0;
@ -25,10 +43,11 @@ GameState_t* get_game_state(void) {
}
// Инициализируем начальные значения
state.info->speed = 1;
state.info->speed = 100;
state.info->score = 0;
state.info->level = 1;
state.info->pause = 0;
state.info->high_score = load_high_score(); // Загружаем рекорд
// Инициализируем следующую фигуру
state.next.sprite = rand() % FIGURE_COUNT;
@ -42,7 +61,6 @@ GameState_t* get_game_state(void) {
initialized = 1;
}
LOG_FUNCTION_END("get_game_state", "state=%d", state.state);
return &state;
}

View file

@ -12,7 +12,7 @@ void do_init(void) {
state->info->score = 0;
state->info->level = 1;
state->info->speed = 1;
state->info->speed = 100;
state->state = Spawn;
LOG_FUNCTION_END("do_init", "score=%d, level=%d, state=%d",

View file

@ -3,8 +3,6 @@
#include "../../logging.h"
void do_spawn(void) {
LOG_FUNCTION_START("do_spawn", "");
GameState_t* state = get_game_state();
// Устанавливаем текущую фигуру из следующей (или генерируем первую)
@ -23,12 +21,8 @@ void do_spawn(void) {
// Проверка на GameOver
if (check_collision()) {
state->state = GameOver;
LOG_FUNCTION_END("do_spawn", "collision detected, state=%d", state->state);
return;
}
state->state = Move;
LOG_FUNCTION_END("do_spawn", "curr=(%d,%d), next_sprite=%d, state=%d",
state->curr.x, state->curr.y, state->next.sprite, state->state);
}

View file

@ -9,18 +9,19 @@ long long get_time_ms() {
void do_move(void) {
GameState_t* state = get_game_state();
LOG_FUNCTION_START("do_move", "");
LOG_FUNCTION_START("do_move", "speed=%d, moving_type=%d, current_pos=(%d,%d)",
state->info->speed, state->moving_type, state->curr.x, state->curr.y);
// Добавляем проверку, чтобы избежать деления на ноль
if (state->info->speed <= 0) {
state->info->speed = 1; // Устанавливаем минимальное значение
state->info->speed = 100; // Устанавливаем минимальное значение
}
long long current_time = get_time_ms();
int delay = (state->moving_type == ToDown) ? 50 : (1000 / state->info->speed);
if (current_time - state->last_time < delay) {
LOG_FUNCTION_END("do_move", "not enough time passed, delay=%d", delay);
LOG_FUNCTION_END("do_move", "not enough time passed, delay=%d ms", delay);
return; // ещё не время
}
state->last_time = current_time;
@ -32,6 +33,6 @@ void do_move(void) {
state->state = Attaching; // переход в Attaching
}
LOG_FUNCTION_END("do_move", "curr=(%d,%d), state=%d",
state->curr.x, state->curr.y, state->state);
LOG_FUNCTION_END("do_move", "moved to (%d,%d), state=%d, delay=%d ms",
state->curr.x, state->curr.y, state->state, delay);
}

View file

@ -2,8 +2,6 @@
#include "../../logging.h"
void do_attaching(void) {
LOG_FUNCTION_START("do_attaching", "");
GameState_t* state = get_game_state();
// Закрепляем фигуру на поле
place_figure();
@ -12,7 +10,6 @@ void do_attaching(void) {
clear_lines();
// Проверяем GameOver
int is_gameov = 0;
if (is_game_over()) {
state->state = GameOver;
} else {
@ -23,8 +20,6 @@ void do_attaching(void) {
}
int check_collision() {
LOG_FUNCTION_START("check_collision", "");
GameState_t* state = get_game_state();
Figure_t* fig = &state->curr;
@ -35,11 +30,9 @@ int check_collision() {
int y = fig->y + i;
if (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT) {
LOG_FUNCTION_END("check_collision", "collision with boundary, x=%d, y=%d", x, y);
return 1; // коллизия
}
if (y >= 0 && state->field[y][x]) {
LOG_FUNCTION_END("check_collision", "collision with field, x=%d, y=%d", x, y);
return 1; // коллизия с другой фигурой
}
}
@ -53,7 +46,6 @@ int check_collision() {
void place_figure() {
GameState_t* state = get_game_state();
LOG_FUNCTION_START("place_figure", "curr=(%d,%d)", state->curr.x, state->curr.y);
Figure_t* fig = &state->curr;
for (int i = 0; i < 4; ++i) {
@ -67,14 +59,14 @@ void place_figure() {
}
}
}
LOG_FUNCTION_END("place_figure", "");
}
void clear_lines() {
GameState_t* state = get_game_state();
LOG_FUNCTION_START("clear_lines", "score=%d", state->info->score);
int old_level = state->info->level;
int old_speed = state->info->speed;
int lines_cleared = 0;
for (int i = FIELD_HEIGHT - 1; i >= 0; --i) {
@ -104,13 +96,33 @@ void clear_lines() {
// Начисление очков
if (lines_cleared > 0) {
int points[] = {0, 100, 300, 700, 1500};
int old_score = state->info->score;
state->info->score += points[lines_cleared];
if (state->info->score / 600 > state->info->level - 1) {
state->info->level++;
state->info->speed = state->info->level;
// Обновляем рекорд, если нужно
if (state->info->score > state->info->high_score) {
state->info->high_score = state->info->score;
}
// Увеличиваем уровень каждые 600 очков (максимум 10 уровней)
int new_level = (state->info->score / 600) + 1;
if (new_level > 10) new_level = 10;
if (new_level > state->info->level) {
state->info->level = new_level;
// СУПЕР-УСКОРЕНИЕ! В 50 раз быстрее!
state->info->speed = new_level * 50;
LOG_FUNCTION_END("clear_lines", "lines_cleared=%d, score=%d->%d, level=%d->%d, speed=%d->%d",
lines_cleared, old_score, state->info->score, old_level, state->info->level,
old_speed, state->info->speed);
return;
}
}
LOG_FUNCTION_END("clear_lines", "lines_cleared=%d, score=%d, level=%d",
lines_cleared, state->info->score, state->info->level);
// Добавим лог, даже если линии не очищались
LOG_FUNCTION_END("clear_lines", "lines_cleared=%d, score=%d->%d, level=%d->%d, speed=%d->%d",
lines_cleared, state->info->score, state->info->score, old_level, state->info->level,
old_speed, state->info->speed);
}

View file

@ -2,9 +2,14 @@
#include "../../logging.h"
void do_gameover(void) {
LOG_FUNCTION_START("do_gameover", "");
GameState_t* state = get_game_state();
// Сохраняем рекорд, если текущий рекорд побит
if (state->info->score > state->info->high_score) {
state->info->high_score = state->info->score;
save_high_score(state->info->high_score);
}
// Сброс next в пустую фигуру
const int (*shape)[4] = empty_fig();
for (int i = 0; i < 4; ++i)
@ -15,8 +20,6 @@ void do_gameover(void) {
}
int is_game_over() {
LOG_FUNCTION_START("is_game_over", "");
GameState_t* state = get_game_state();
// Проверяем, есть ли блоки в верхних рядах
for (int j = 0; j < FIELD_WIDTH; ++j) {
@ -26,6 +29,5 @@ int is_game_over() {
}
}
LOG_FUNCTION_END("is_game_over", "game not over");
return 0;
}

View file

@ -5,8 +5,6 @@
// display.c
void display_game(GameInfo_t game_state) {
LOG_FUNCTION_START("display_game", "");
clear();
// Отображение игрового поля (всегда, даже во время паузы)

View file

@ -32,7 +32,7 @@ int main() {
}
nodelay(stdscr, TRUE);
timeout(100);
timeout(10);
UserAction_t current_action = {0};
bool action_valid = false;