/* SPDX-License-Identifier: MIT */ /* * konbattle.h * * Copyright (c) 2026 KonAki * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #ifndef KONBATTLE_H #define KONBATTLE_H #include #include #ifdef __cplusplus extern "C" { #endif /*** definition ***/ /* turn based fights with the rules of the common RPG makers: stats, states, skills with a damage formula, hit and critical rolls, element rates, turn order by agility and escaping. nothing here draws or reads input, the program asks for actions and shows the results. everything lives in tables you own */ enum { KON_STAT_MHP, KON_STAT_MMP, KON_STAT_ATK, KON_STAT_DEF, KON_STAT_MAT, KON_STAT_MDF, KON_STAT_AGI, KON_STAT_LUK, /* the first eight can be changed by states */ KON_STAT_HP, KON_STAT_MP, KON_STAT_TP, KON_STAT_LEVEL, KON_STATS }; #define KON_PARAMS 8 #define KON_BATTLER_STATES 8 #define KON_ELEMENTS 16 #define KON_SKILL_STATES 4 #define KON_BATTLE_QUEUE 32 #define KON_BATTLE_MAX 32 enum { KON_PARTY, KON_ENEMIES }; typedef struct kon_battler { const char *name; int team; int level; int base[KON_PARAMS], plus[KON_PARAMS]; /* what it is, and what equipment adds */ int hp, mp, tp; int hit, eva, cri, cev, mev, mrf; /* percents */ int pdr, mdr; /* damage taken from physical and magical hits, percent */ int hrg, mrg, trg; /* per turn, percent of the most, tp is points */ int elementRate[KON_ELEMENTS]; /* percent, the element numbers are yours, 0 is no element */ struct { int id; int turns; } states[KON_BATTLER_STATES]; /* turns is 0 for one that does not wear off */ int stateCount; int guarding; } kon_battler_t; /* death is the state that is on while hp is 0. restriction: 0 nothing, 1 attacks an enemy, 2 attacks anyone, 3 attacks a friend, 4 cannot act. percent changes the first eight stats, -50 is half and 100 is double */ typedef struct kon_state { const char *name; int death; int restriction; int turnsMin, turnsMax; /* both 0 for until it is removed */ int removeAt; /* 0 never, 1 after its owner's action, 2 at the end of the turn */ int removeAtBattleEnd; int removeByDamage; /* percent chance when hit */ int percent[KON_PARAMS]; } kon_state_t; /* the numbers here are what the stat is worth (a.atk) and are never negative */ typedef struct kon_formula { struct { int op; double n; } ops[96]; int count; int errorAt; /* where it went wrong, or -1 */ } kon_formula_t; typedef enum { KON_SCOPE_NONE, KON_SCOPE_ONE_ENEMY, KON_SCOPE_ALL_ENEMIES, KON_SCOPE_RANDOM_ENEMY, KON_SCOPE_ONE_ALLY, KON_SCOPE_ALL_ALLIES, KON_SCOPE_ONE_ALLY_DEAD, KON_SCOPE_ALL_ALLIES_DEAD, KON_SCOPE_USER } kon_scope_t; typedef enum { KON_DAMAGE_NONE, KON_DAMAGE_HP, KON_DAMAGE_MP, KON_DAMAGE_HP_RECOVER, KON_DAMAGE_MP_RECOVER, KON_DAMAGE_HP_DRAIN, KON_DAMAGE_MP_DRAIN } kon_damageType_t; typedef enum { KON_HIT_CERTAIN, KON_HIT_PHYSICAL, KON_HIT_MAGICAL } kon_hitType_t; /* skills and items are the same thing here. a zero in successRate, repeats or randomCount means 100, 1 and 1, so a table can leave them out */ typedef struct kon_skill { const char *name; int mpCost, tpCost, tpGain; kon_scope_t scope; int randomCount; int speed; int successRate; int repeats; kon_hitType_t hitType; kon_damageType_t damageType; int element; const char *formulaText; /* like "a.atk * 4 - b.def * 2", a is the user and b the target. see kon_skillCompile */ kon_formula_t formula; int variance; /* percent */ int critical; /* can it be a critical hit */ struct { int id; int chance; } addState[KON_SKILL_STATES]; int addStateCount; int removeState[KON_SKILL_STATES]; int removeStateCount; } kon_skill_t; enum { KON_ACT_NONE, KON_ACT_SKILL, KON_ACT_GUARD, KON_ACT_ESCAPE }; typedef struct kon_action { int kind; int battler; const kon_skill_t *skill; int target; /* a battler number, ignored by the scopes that pick themselves */ int speed; } kon_action_t; /* what happened to one target, for the program to show */ typedef struct kon_result { int user, target; int escape; /* 0 not an escape, 1 failed, 2 got away */ int missed, evaded, critical; int hp, mp; /* change to the target, a hit is negative */ int userHp, userMp; /* change to the user, from draining */ int died, revived; int added[KON_SKILL_STATES], addedCount; int removed[KON_SKILL_STATES], removedCount; } kon_result_t; typedef enum { KON_BATTLE_ONGOING, KON_BATTLE_VICTORY, KON_BATTLE_DEFEAT, KON_BATTLE_ESCAPED } kon_outcome_t; typedef struct kon_battle { kon_battler_t *battlers; int count; const kon_state_t *states; int stateCount; const kon_skill_t *attack; /* what a confused battler does */ int32_t *vars; /* for v[n] in formulas, can be a script's variables */ int varCount; kon_action_t queue[KON_BATTLE_QUEUE]; kon_action_t last; /* what kon_battleNext just did, with the skill it really used */ int queued, next, sorted; int turn; float escapeRatio; int escaped; uint32_t seed; int forced; /* private: the battler a confused action must hit, plus one */ } kon_battle_t; /*** formulas ***/ /* a stat of the user is a.atk, of the target b.def, v[3] is a variable. + - * / % ** < > <= >= == != && || ! ?: parentheses and Math.max min pow floor ceil round abs sqrt. 0 and errorAt set if it is something else */ int kon_formulaCompile(kon_formula_t *f, const char *text); double kon_formulaEval(const kon_formula_t *f, const kon_battle_t *battle, const kon_battler_t *a, const kon_battler_t *b); /*** battlers ***/ /* the usual start: 95 hit, 100 element rates, 100 damage rates */ void kon_battlerInit(kon_battler_t *b, const char *name, int team, int level); int kon_battlerStat(const kon_battle_t *battle, const kon_battler_t *b, int stat); int kon_battlerAlive(const kon_battle_t *battle, const kon_battler_t *b); int kon_battlerHasState(const kon_battler_t *b, int id); /*** skills ***/ int kon_skillCompile(kon_skill_t *skill); /* 0 if the formula is bad, it then does nothing */ /*** battle ***/ void kon_battleInit(kon_battle_t *bt, kon_battler_t *battlers, int count, const kon_state_t *states, int stateCount, uint32_t seed); /* a whole turn is: queue what everyone does, call kon_battleNext until it is -1 showing each result, then kon_battleEndTurn */ int kon_battleQueue(kon_battle_t *bt, int kind, int battler, const kon_skill_t *skill, int target); /* does the next action, in order of speed. gives how many results it wrote (0 for a guard or an action that could not happen) or -1 when the turn's actions are used up */ int kon_battleNext(kon_battle_t *bt, kon_result_t *out, int cap); /* regeneration and states wearing off, one result for each battler it touched */ int kon_battleEndTurn(kon_battle_t *bt, kon_result_t *out, int cap); void kon_battleEnd(kon_battle_t *bt); kon_outcome_t kon_battleOutcome(const kon_battle_t *bt); /* one skill right now, the other pieces use this. -1 if it cannot be paid for */ int kon_battleUse(kon_battle_t *bt, int user, const kon_skill_t *skill, int target, kon_result_t *out, int cap); #ifdef __cplusplus } #endif #endif /* KONBATTLE_H */