diff --git a/CMakeLists.txt b/CMakeLists.txt index aa873c1..eaca920 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ set(SOURCES src/game.c src/dialogs.c src/npc.c + src/events.c # ... ) # Shared assets, fx-9860G-only assets and fx-CG-50-only assets diff --git a/src/dialogs.c b/src/dialogs.c index 23a7890..ce26a57 100644 --- a/src/dialogs.c +++ b/src/dialogs.c @@ -8,6 +8,7 @@ #include "config.h" #include "game.h" #include "npc.h" +#include "events.h" #define BOX_HEIGHT (F_HEIGHT/PXSIZE+8) diff --git a/src/events.c b/src/events.c new file mode 100644 index 0000000..aee4472 --- /dev/null +++ b/src/events.c @@ -0,0 +1,123 @@ +#include +#include +#include "events.h" + +void events_init_handler(EventHandler *handler) { + handler->vars = 0; +} + +int events_bind_variable(EventHandler *handler, int *var, char *name) { + if(handler->vars < MAX_VARIABLES){ + handler->variables[handler->vars] = var; + handler->var_names[handler->vars++] = name; + }else{ + return 1; + } + return 0; +} + +char op_chars[OP_AMOUNT+1] = " =+-/*%"; + +int _op_null(int a, int b) { + return 0; +} + +int _op_set(int a, int b) { + return b; +} + +int _op_add(int a, int b) { + return a+b; +} + +int _op_sub(int a, int b) { + return a-b; +} + +int _op_div(int a, int b) { + if(b == 0) return 0; + return a/b; +} + +int _op_mul(int a, int b) { + return a*b; +} + +int _op_mod(int a, int b) { + if(b == 0) return 0; + return a%b; +} + +int (*_operations[OP_AMOUNT])(int, int) = { + _op_null, + _op_set, + _op_add, + _op_sub, + _op_div, + _op_mul, + _op_mod +}; + +#define MIN(a, b) a < b ? a : b + +char _message_buffer[MESSAGE_BUFFER_SZ]; +char *events_parse_string(EventHandler *handler, char *message) { + size_t message_pos = 0; + char in_token = 0; + char var_name[TOKEN_MAX_SZ]; + size_t name_pos = 0; + Operation var_op = OP_NULL; + char num[TOKEN_MAX_SZ]; + size_t num_pos = 0; + Token tok_type = T_NULL; + char c; + size_t i, n; + int *var; + for(i=0;ivars;n++){ + if(!strcmp(var_name, handler->var_names[n])){ + var = handler->variables[n]; + if(var_op){ + *var = _operations[var_op](*var, atoi(num)); + } + break; + } + } + } + /* Reset everything */ + tok_type = T_NULL; + name_pos = 0; + var_op = OP_NULL; + num_pos = 0; + } + }else if(!in_token){ + if(message_pos < TOKEN_MAX_SZ) _message_buffer[message_pos++] = c; + } + if(in_token && c != ' '){ + if(tok_type == T_VAR_EDIT){ + if(var_op != OP_NULL){ + if(num_pos < TOKEN_MAX_SZ) num[num_pos++] = c; + } + if(strchr(op_chars, c)){ + var_op = (Operation)(strchr(op_chars, c)-op_chars); + } + if(var_op == OP_NULL){ + if(name_pos < TOKEN_MAX_SZ) var_name[name_pos++] = c; + } + } + if(c == '$'){ + tok_type = T_VAR_EDIT; + } + } + } + _message_buffer[MIN(message_pos, MESSAGE_BUFFER_SZ)] = '\0'; + return _message_buffer; +} diff --git a/src/game.c b/src/game.c index 32b4104..9a9b0d6 100644 --- a/src/game.c +++ b/src/game.c @@ -94,7 +94,6 @@ void game_draw(Game *game) { map_render_by_layer(game, FOREGROUND); game_render_indicator(game); dprint(8, 8, C_BLACK, "Lifes: %d", game->player.life); - dprint(8, 16, C_BLACK, "Mana: %d", game->mana); } /* Key management */ diff --git a/src/game.h b/src/game.h index 8b0eb3a..940ae61 100644 --- a/src/game.h +++ b/src/game.h @@ -5,7 +5,7 @@ #include #include - +#include "events.h" /* The direction where the player is going to. */ typedef enum { @@ -132,6 +132,7 @@ typedef struct { typedef struct { Map *map_level; /* The level that the player is currently playing */ Player player; /* The player data (see player.h). */ + EventHandler handler; /* Some global variables */ /* Set to true when asked for exit */ bool exittoOS;