Merge branch 'pr/378' into 420

This commit is contained in:
RogueMaster
2022-10-25 00:14:09 -04:00
5 changed files with 264 additions and 52 deletions
@@ -4,8 +4,12 @@ App(
apptype=FlipperAppType.EXTERNAL,
entry_point="snake_game_app",
cdefines=["APP_SNAKE_GAME"],
requires=["gui"],
stack_size=1 * 1024,
requires=[
"gui",
"notification",
"storage",
],
stack_size=2 * 1024,
order=210,
fap_icon="snake_10px.png",
fap_category="Games",
@@ -0,0 +1,169 @@
#include "snake_file_handler.h"
#include <furi.h>
#include <flipper_format/flipper_format.h>
static void snake_game_close_file(FlipperFormat* file) {
if(file == NULL){
furi_record_close(RECORD_STORAGE);
return;
}
flipper_format_file_close(file);
flipper_format_free(file);
furi_record_close(RECORD_STORAGE);
}
static FlipperFormat* snake_game_open_file(Storage* storage) {
FlipperFormat* file = flipper_format_file_alloc(storage);
if(storage_common_stat(storage, SNAKE_GAME_FILE_PATH, NULL) == FSE_OK) {
if(!flipper_format_file_open_existing(file, SNAKE_GAME_FILE_PATH)) {
snake_game_close_file(file);
return NULL;
}
} else {
if(!flipper_format_file_open_new(file, SNAKE_GAME_FILE_PATH)) {
snake_game_close_file(file);
return NULL;
}
flipper_format_write_header_cstr(file, SNAKE_GAME_FILE_HEADER, SNAKE_GAME_FILE_ACTUAL_VERSION);
flipper_format_rewind(file);
}
return file;
}
int16_t snake_game_save_score_to_file(int32_t len) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = snake_game_open_file(storage);
len = len - 7;
int32_t temp;
if(!flipper_format_read_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)){
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)){
snake_game_close_file(file);
return -1;
}
}else{
if(len > temp){
flipper_format_rewind(file);
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)){
snake_game_close_file(file);
return -1;
}
}else{
len = temp;
}
}
snake_game_close_file(file);
return len;
}
void snake_game_save_game_to_file(SnakeState* const snake_state) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = snake_game_open_file(storage);
uint32_t temp = snake_state->len;
if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN,&temp, 1)){
snake_game_close_file(file);
return;
}
uint16_t array_size = snake_state->len * 2;
uint32_t temp_array[array_size];
for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++){
temp_array[a++] = snake_state->points[i].x;
temp_array[a++] = snake_state->points[i].y;
}
if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)){
snake_game_close_file(file);
return;
}
temp = snake_state->currentMovement;
if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT,&temp, 1)){
snake_game_close_file(file);
return;
}
temp = snake_state->nextMovement;
if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT,&temp, 1)){
snake_game_close_file(file);
return;
}
array_size = 2;
uint32_t temp_point_array[array_size];
temp_point_array[0] = snake_state->fruit.x;
temp_point_array[1] = snake_state->fruit.y;
if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)){
snake_game_close_file(file);
return;
}
snake_game_close_file(file);
}
bool snake_game_init_game_from_file(SnakeState* const snake_state) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = snake_game_open_file(storage);
FuriString* file_type = furi_string_alloc();
uint32_t version = 1;
if(!flipper_format_read_header(file, file_type, &version)) {
furi_string_free(file_type);
snake_game_close_file(file);
return false;
}
furi_string_free(file_type);
uint32_t temp;
if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)){
snake_game_close_file(file);
return false;
}
snake_state->len = temp;
flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_LEN);
uint16_t array_size = snake_state->len * 2;
uint32_t temp_array[array_size];
if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)){
snake_game_close_file(file);
return false;
}
for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++){
snake_state->points[i].x = temp_array[a++];
snake_state->points[i].y = temp_array[a++];
}
flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_POINTS);
if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)){
snake_game_close_file(file);
return false;
}
snake_state->currentMovement = temp;
flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT);
if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)){
snake_game_close_file(file);
return false;
}
snake_state->nextMovement = temp;
flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT);
array_size = 2;
uint32_t temp_point_array[array_size];
if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)){
snake_game_close_file(file);
return false;
}
snake_state->fruit.x = temp_point_array[0];
snake_state->fruit.y = temp_point_array[1];
flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS);
snake_game_close_file(file);
return true;
}
@@ -0,0 +1,23 @@
#pragma once
#include "snake_types.h"
#include <furi.h>
#include <flipper_format/flipper_format.h>
#define SNAKE_GAME_FILE_PATH "/ext/apps/games/snake.stat"
#define SNAKE_GAME_FILE_HEADER "Flipper Snake plugin run file"
#define SNAKE_GAME_FILE_ACTUAL_VERSION 1
#define SNAKE_GAME_CONFIG_KEY_POINTS "SnakePoints"
#define SNAKE_GAME_CONFIG_KEY_LEN "SnakeLen"
#define SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT "CurrentMovement"
#define SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT "NextMovement"
#define SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS "FruitPoints"
#define SNAKE_GAME_CONFIG_HIGHSCORE "Highscore"
int16_t snake_game_save_score_to_file(int32_t length);
void snake_game_save_game_to_file(SnakeState* const snake_state);
bool snake_game_init_game_from_file(SnakeState* const snake_state);
@@ -0,0 +1,50 @@
#pragma once
#include <furi.h>
typedef struct {
// +-----x
// |
// |
// y
uint8_t x;
uint8_t y;
} Point;
typedef enum {
GameStateLife,
// https://melmagazine.com/en-us/story/snake-nokia-6110-oral-history-taneli-armanto
// Armanto: While testing the early versions of the game, I noticed it was hard
// to control the snake upon getting close to and edge but not crashing — especially
// in the highest speed levels. I wanted the highest level to be as fast as I could
// possibly make the device "run," but on the other hand, I wanted to be friendly
// and help the player manage that level. Otherwise it might not be fun to play. So
// I implemented a little delay. A few milliseconds of extra time right before
// the player crashes, during which she can still change the directions. And if
// she does, the game continues.
GameStateLastChance,
GameStateGameOver,
} GameState;
// Note: do not change without purpose. Current values are used in smart
// orthogonality calculation in `snake_game_get_turn_snake`.
typedef enum {
DirectionUp,
DirectionRight,
DirectionDown,
DirectionLeft,
} Direction;
#define MAX_SNAKE_LEN 253
typedef struct {
Point points[MAX_SNAKE_LEN];
uint16_t len;
int16_t highscore;
Direction currentMovement;
Direction nextMovement; // if backward of currentMovement, ignore
Point fruit;
GameState state;
} SnakeState;
+16 -50
View File
@@ -1,3 +1,6 @@
#include "helpers/snake_file_handler.h"
#include "helpers/snake_types.h"
#include <furi.h>
#include <gui/gui.h>
#include <input/input.h>
@@ -7,52 +10,6 @@
#include <notification/notification_messages.h>
#include <dolphin/dolphin.h>
typedef struct {
// +-----x
// |
// |
// y
uint8_t x;
uint8_t y;
} Point;
typedef enum {
GameStateLife,
// https://melmagazine.com/en-us/story/snake-nokia-6110-oral-history-taneli-armanto
// Armanto: While testing the early versions of the game, I noticed it was hard
// to control the snake upon getting close to and edge but not crashing — especially
// in the highest speed levels. I wanted the highest level to be as fast as I could
// possibly make the device "run," but on the other hand, I wanted to be friendly
// and help the player manage that level. Otherwise it might not be fun to play. So
// I implemented a little delay. A few milliseconds of extra time right before
// the player crashes, during which she can still change the directions. And if
// she does, the game continues.
GameStateLastChance,
GameStateGameOver,
} GameState;
// Note: do not change without purpose. Current values are used in smart
// orthogonality calculation in `snake_game_get_turn_snake`.
typedef enum {
DirectionUp,
DirectionRight,
DirectionDown,
DirectionLeft,
} Direction;
#define MAX_SNAKE_LEN 128*64/4
typedef struct {
Point points[MAX_SNAKE_LEN];
uint16_t len;
Direction currentMovement;
Direction nextMovement; // if backward of currentMovement, ignore
Point fruit;
GameState state;
} SnakeState;
typedef enum {
EventTypeTick,
EventTypeKey,
@@ -121,10 +78,10 @@ static void snake_game_render_callback(Canvas* const canvas, void* ctx) {
if(snake_state->state == GameStateGameOver) {
// Screen is 128x64 px
canvas_set_color(canvas, ColorWhite);
canvas_draw_box(canvas, 34, 20, 62, 24);
canvas_draw_box(canvas, 32, 20, 64, 34);
canvas_set_color(canvas, ColorBlack);
canvas_draw_frame(canvas, 34, 20, 62, 24);
canvas_draw_frame(canvas, 32, 20, 64, 34);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 37, 31, "Game Over");
@@ -134,9 +91,12 @@ static void snake_game_render_callback(Canvas* const canvas, void* ctx) {
}
canvas_set_font(canvas, FontSecondary);
char buffer[12];
char buffer[18];
snprintf(buffer, sizeof(buffer), "Score: %u", snake_state->len - 7);
canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignBottom, buffer);
snprintf(buffer, sizeof(buffer), "Highscore: %d", snake_state->highscore);
canvas_draw_str_aligned(canvas, 64, 51, AlignCenter, AlignBottom, buffer);
}
release_mutex((ValueMutex*)ctx, snake_state);
@@ -285,6 +245,7 @@ static void
return;
} else if(snake_state->state == GameStateLastChance) {
snake_state->state = GameStateGameOver;
snake_state->highscore = snake_game_save_score_to_file(snake_state->len);
notification_message_block(notification, &sequence_fail);
return;
}
@@ -297,6 +258,7 @@ static void
crush = snake_game_collision_with_tail(snake_state, next_step);
if(crush) {
snake_state->state = GameStateGameOver;
snake_state->highscore = snake_game_save_score_to_file(snake_state->len);
notification_message_block(notification, &sequence_fail);
return;
}
@@ -306,6 +268,7 @@ static void
snake_state->len++;
if(snake_state->len >= MAX_SNAKE_LEN) {
snake_state->state = GameStateGameOver;
snake_state->highscore = snake_game_save_score_to_file(snake_state->len);
notification_message_block(notification, &sequence_fail);
return;
}
@@ -326,7 +289,8 @@ int32_t snake_game_app(void* p) {
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(SnakeEvent));
SnakeState* snake_state = malloc(sizeof(SnakeState));
snake_game_init_game(snake_state);
if(!snake_game_init_game_from_file(snake_state))
snake_game_init_game(snake_state);
ValueMutex state_mutex;
if(!init_mutex(&state_mutex, snake_state, sizeof(SnakeState))) {
@@ -382,6 +346,8 @@ int32_t snake_game_app(void* p) {
}
break;
case InputKeyBack:
if(snake_state->state == GameStateLife)
snake_game_save_game_to_file(snake_state);
processing = false;
break;
}