Collab_RPG/src/events.c

115 lines
3.3 KiB
C
Raw Normal View History

2024-07-29 11:36:11 +02:00
#include "events.h"
#include <gint/display.h>
#include <gint/keyboard.h> /*debug*/
#include <math.h>
#include <stdint.h>
2024-07-20 16:53:13 +02:00
#include <stdlib.h>
#include <string.h>
2024-07-29 11:36:11 +02:00
void events_init_handler(EventHandler *handler) { handler->vars = 0; }
2024-07-20 16:53:13 +02:00
int events_bind_variable(EventHandler *handler, int *var, char *name) {
2024-07-29 11:36:11 +02:00
if(handler->vars < MAX_VARIABLES) {
2024-07-20 16:53:13 +02:00
handler->variables[handler->vars] = var;
handler->var_names[handler->vars++] = name;
2024-07-29 11:36:11 +02:00
} else {
2024-07-20 16:53:13 +02:00
return 1;
}
return 0;
}
2024-07-29 11:36:11 +02:00
char op_chars[OP_AMOUNT + 1] = " =+-/*%";
2024-07-20 16:53:13 +02:00
int _op_null(GUNUSED int a, GUNUSED int b) { return 0; }
2024-07-20 16:53:13 +02:00
int _op_set(GUNUSED int a, int b) { return b; }
2024-07-20 16:53:13 +02:00
2024-07-29 11:36:11 +02:00
int _op_add(int a, int b) { return a + b; }
2024-07-20 16:53:13 +02:00
2024-07-29 11:36:11 +02:00
int _op_sub(int a, int b) { return a - b; }
2024-07-20 16:53:13 +02:00
int _op_div(int a, int b) {
2024-07-29 11:36:11 +02:00
if(b == 0)
return 0;
return a / b;
2024-07-20 16:53:13 +02:00
}
2024-07-29 11:36:11 +02:00
int _op_mul(int a, int b) { return a * b; }
2024-07-20 16:53:13 +02:00
int _op_mod(int a, int b) {
2024-07-29 11:36:11 +02:00
if(b == 0)
return 0;
return a % b;
2024-07-20 16:53:13 +02:00
}
2024-07-29 21:08:55 +02:00
int (*_operations[OP_AMOUNT])(int, int) = {_op_null, _op_set, _op_add, _op_sub,
_op_div, _op_mul, _op_mod};
2024-07-20 16:53:13 +02:00
2024-07-20 17:44:44 +02:00
#define MIN(a, b) a < b ? a : b
2024-07-20 16:53:13 +02:00
char _message_buffer[MESSAGE_BUFFER_SZ];
char *events_parse_string(EventHandler *handler, char *message) {
2024-07-20 17:44:44 +02:00
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;
2024-07-20 16:53:13 +02:00
int *var;
2024-07-29 11:36:11 +02:00
for(i = 0; i < strlen(message); i++) {
2024-07-20 17:44:44 +02:00
c = message[i];
2024-07-29 11:36:11 +02:00
if(c == '`') {
2024-07-20 17:44:44 +02:00
in_token = !in_token;
2024-07-29 11:36:11 +02:00
if(!in_token) {
if(tok_type == T_VAR_EDIT) {
2024-07-20 17:44:44 +02:00
/* Do the calculation */
var_name[MIN(name_pos, TOKEN_MAX_SZ)] = '\0';
num[MIN(num_pos, TOKEN_MAX_SZ)] = '\0';
2024-07-29 11:36:11 +02:00
for(n = 0; n < handler->vars; n++) {
if(!strcmp(var_name, handler->var_names[n])) {
2024-07-20 17:44:44 +02:00
var = handler->variables[n];
2024-07-29 11:36:11 +02:00
if(var_op) {
2024-07-20 17:44:44 +02:00
*var = _operations[var_op](*var, atoi(num));
}
break;
}
2024-07-20 16:53:13 +02:00
}
}
2024-07-20 17:44:44 +02:00
/* Reset everything */
tok_type = T_NULL;
name_pos = 0;
var_op = OP_NULL;
num_pos = 0;
}
2024-07-29 11:36:11 +02:00
} else if(!in_token) {
if(message_pos < TOKEN_MAX_SZ)
_message_buffer[message_pos++] = c;
2024-07-20 17:44:44 +02:00
}
2024-07-29 11:36:11 +02:00
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;
2024-07-20 17:44:44 +02:00
}
2024-07-29 11:36:11 +02:00
if(strchr(op_chars, c)) {
var_op = (Operation)(strchr(op_chars, c) - op_chars);
2024-07-20 17:44:44 +02:00
}
2024-07-29 11:36:11 +02:00
if(var_op == OP_NULL) {
if(name_pos < TOKEN_MAX_SZ)
var_name[name_pos++] = c;
2024-07-20 16:53:13 +02:00
}
}
2024-07-29 11:36:11 +02:00
if(c == '$') {
2024-07-20 17:44:44 +02:00
tok_type = T_VAR_EDIT;
}
2024-07-20 16:53:13 +02:00
}
}
2024-07-20 17:44:44 +02:00
_message_buffer[MIN(message_pos, MESSAGE_BUFFER_SZ)] = '\0';
2024-07-20 16:53:13 +02:00
return _message_buffer;
}