binding-of-isaac/src/maeth.c

134 lines
4.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include "../include/glad/glad.h"
#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include "hash.h"
#include "base.h"
#include "menus.h"
#include "maeth.h"
// --------------------------------------------------------------------- //
// between 1 and 20
int mathDiff = 3;
// between 1.0 and 60.0
double mathAnsT = 10.0;
// 0 or 1
int mathDoDmg = 1;
// between 1 and 1000
int mathDmg = 500;
// --------------------------------------------------------------------- //
// min bound for number gen (disregarding difficulty)
int mthInf = 1;
// max bound for number gen (disregarding difficulty)
int mthSup = 100;
// whether or not to allow operations (SUM is always enabled)
bool mthAllow[3];
// --------------------------------------------------------------------- //
int mthDiffSetting, mthAnsTSetting, mthDoDmgSetting, mthDmgSetting;
int mthInfSetting, mthSupSetting;
int mthExitSetting;
int mthInterface;
void rebalance(void* arg) {
if(mthInf > mthSup) {
mthInf = 1;
mthSup = 100;
}
}
void init_math_interfaces() {
mthDiffSetting = button_create_onoff("difficulty", 128, 64, 64, -0.6f, 0.4f, 0.5f, 0.25f, SET_VAR, &mathDiff, 1, 20, INT, NULL, NULL);
mthAnsTSetting = button_create_onoff("answer time", 255, 255, 64, -0.6f, 0.1f, 0.5f, 0.25f, SET_VAR, &mathAnsT, 1.0, 60.0, DOUBLE, NULL, NULL);
mthDoDmgSetting = button_create_onoff("damage if wrong", 255, 64, 64, -0.6f, -0.2f, 0.5f, 0.25f, SET_VAR, &mathDoDmg, 0, 1, INT, NULL, NULL);
mthDmgSetting = button_create_onoff("damage value", 128, 6, 6, -0.6f, -0.5f, 0.5f, 0.25f, SET_VAR, &mathDmg, 1, 1000, INT, NULL, NULL);
mthInfSetting = button_create_onoff("minimum val", 128, 128, 255, 0.1f, 0.4f, 0.5f, 0.25f, SET_VAR, &mthInf, 1, 32767, INT, NULL, NULL);
mthSupSetting = button_create_onoff("maximum val", 128, 128, 255, 0.1f, 0.1f, 0.5f, 0.25f, SET_VAR, &mthSup, 1, 32767, INT, NULL, NULL);
mthExitSetting = button_create_onoff("back", 64, 255, 64,-0.25f, -0.8f, 0.5f, 0.25f, WARP, &welcome_start_i, 0, 1, INT, &rebalance, NULL);
mthInterface = interface_create("math configs", 255, 192, 192, -0.4f , 0.7f, 0.8f, 0.25f);
interface_link_button(mthInterface, mthDiffSetting);
interface_link_button(mthInterface, mthAnsTSetting);
interface_link_button(mthInterface, mthDoDmgSetting);
interface_link_button(mthInterface, mthDmgSetting);
interface_link_button(mthInterface, mthInfSetting);
interface_link_button(mthInterface, mthSupSetting);
interface_link_button(mthInterface, mthExitSetting);
}
void init_maeth() {
mthAllow[0] = true;
mthAllow[1] = true;
mthAllow[2] = true;
}
ctype get_ctype() {
int r = rand()%4;
return (ctype)(1+r);
}
formula create_formula_aux(int depth, int max_depth) {
formula newF = malloc(sizeof(struct calcul));
if(depth == max_depth) {
newF->calc1 = NULL;
newF->calc2 = NULL;
newF->operation = VALUE;
newF->val = mthInf + rand()%(mthSup-mthInf);
} else {
newF->calc1 = create_formula_aux(depth+1, max_depth);
newF->calc2 = create_formula_aux(depth+1, max_depth);
newF->operation = get_ctype();
newF->val = 0; // irrelevant
}
return newF;
}
int solve_formula(formula fm) {
switch (fm->operation) {
case VALUE:
return fm->val;
case SUM:
return (solve_formula(fm->calc1) + solve_formula(fm->calc2));
case DIFF:
return (solve_formula(fm->calc1) - solve_formula(fm->calc2));
case MULT:
return (solve_formula(fm->calc1) * solve_formula(fm->calc2));
case MODULO:
return (solve_formula(fm->calc1) % solve_formula(fm->calc2));
default:
fprintf(stderr, "ERROR : unsupported operation type\n");
return -1;
}
}
void free_formula(formula fm) {
if(fm != NULL) {
free_formula(fm->calc1);
free_formula(fm->calc2);
free(fm);
}
}