summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonAki <konaki@anokonaki.org>2026-10-16 15:30:41 +0800
committerKonAki <konaki@anokonaki.org>2026-10-16 15:30:41 +0800
commitc4b53a0d1ba903dc94fe93d2fa50367bfbefa385 (patch)
tree64154380a331ccb8ebff3a0e543152eae75ce9ba
parent0e0ca89d74a79dd045197b2878a5a90328c92187 (diff)
add rmdata: enemies, troops, enemy actions
-rw-r--r--kon/konrmdata.c138
-rw-r--r--kon/konrmdata.h124
2 files changed, 262 insertions, 0 deletions
diff --git a/kon/konrmdata.c b/kon/konrmdata.c
new file mode 100644
index 0000000..1957942
--- /dev/null
+++ b/kon/konrmdata.c
@@ -0,0 +1,138 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * konrmdata.c
+ *
+ * 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.
+ */
+
+#include "konrmdata.h"
+
+#include <stddef.h>
+
+/*** enemies ***/
+
+void kon_rmEnemyBattler(const kon_rmEnemy_t *enemy, kon_battler_t *out)
+{
+ int i, n = 1;
+
+ if (!out) return;
+ kon_battlerInit(out, enemy && enemy->name ? enemy->name : "", KON_ENEMIES, 1);
+ if (!enemy) return;
+ for (i = 0; i < KON_PARAMS; i++) out->base[i] = enemy->params[i] < 0 ? 0 : enemy->params[i];
+ out->hp = out->base[KON_STAT_MHP];
+ out->mp = out->base[KON_STAT_MMP];
+ out->hit = enemy->hit;
+ out->eva = enemy->eva;
+ out->cri = enemy->cri;
+ out->cev = enemy->cev;
+ out->mev = enemy->mev;
+ out->mrf = enemy->mrf;
+ out->pdr = enemy->pdr;
+ out->mdr = enemy->mdr;
+ out->hrg = enemy->hrg;
+ out->mrg = enemy->mrg;
+ out->trg = enemy->trg;
+ if (KON_ELEMENTS < KON_RM_ELEMENTS) n = KON_ELEMENTS;
+ else n = KON_RM_ELEMENTS;
+ for (i = 0; i < n; i++) out->elementRate[i] = enemy->elementRate[i];
+}
+
+static uint32_t roll(uint32_t *seed)
+{
+ uint32_t s = *seed ? *seed : 1;
+
+ s ^= s << 13;
+ s ^= s >> 17;
+ s ^= s << 5;
+ *seed = s;
+ return s;
+}
+
+static int turnMatches(int turn, int a, int b)
+{
+ if (b == 0) return turn == a;
+ return turn >= 1 && turn >= a && turn % b == a % b;
+}
+
+static int conditionMet(const kon_rmEnemyAction_t *action, const kon_rmAi_t *ai)
+{
+ const kon_battler_t *self = ai->self;
+ int mhp = self ? self->base[KON_STAT_MHP] + self->plus[KON_STAT_MHP] : 0;
+ int mmp = self ? self->base[KON_STAT_MMP] + self->plus[KON_STAT_MMP] : 0;
+
+ switch (action->conditionType) {
+ case 0: return 1;
+ case 1: return turnMatches(ai->turn, action->param1, action->param2);
+ case 2: return self && mhp > 0 && self->hp * 100 >= action->param1 * mhp && self->hp * 100 <= action->param2 * mhp;
+ case 3: return self && mmp > 0 && self->mp * 100 >= action->param1 * mmp && self->mp * 100 <= action->param2 * mmp;
+ case 4: return self && kon_battlerHasState(self, action->param1);
+ case 5: return ai->partyLevel >= action->param1;
+ case 6: return ai->switchOn && ai->switchOn(action->param1, ai->user);
+ default: return 0;
+ }
+}
+
+int kon_rmEnemyAction(const kon_rmEnemy_t *enemy, const kon_rmAi_t *ai, uint32_t *seed)
+{
+ int valid[64], count = 0, best = 0, i;
+ long long sum = 0, pick;
+
+ if (!enemy || !ai || !seed || !enemy->actions) return 0;
+ for (i = 0; i < enemy->actionCount && count < 64; i++) {
+ const kon_rmEnemyAction_t *a = &enemy->actions[i];
+
+ if (a->skillId <= 0 || !conditionMet(a, ai)) continue;
+ if (ai->usable && !ai->usable(a->skillId, ai->user)) continue;
+ valid[count++] = i;
+ if (a->rating > best) best = a->rating;
+ }
+ if (count == 0) return 0;
+ best -= 3; /* only what is within 2 of the best */
+ for (i = 0; i < count; i++) {
+ int over = enemy->actions[valid[i]].rating - best;
+
+ if (over > 0) sum += over;
+ }
+ if (sum <= 0) return 0;
+ pick = (long long)(roll(seed) % (uint32_t)(sum > 2000000000 ? 2000000000 : sum));
+ for (i = 0; i < count; i++) {
+ int over = enemy->actions[valid[i]].rating - best;
+
+ if (over <= 0) continue;
+ pick -= over;
+ if (pick < 0) return enemy->actions[valid[i]].skillId;
+ }
+ return 0;
+}
+
+/*** troops ***/
+
+int kon_rmTroopPageReady(const kon_rmTroopPage_t *page, int turn, int turnEnd, int enemyHp, int actorHp, int switchOn)
+{
+ if (!page) return 0;
+ if (!page->turnEnding && !page->turnValid && !page->enemyValid && !page->actorValid && !page->switchValid) return 0;
+ if (page->turnEnding && !turnEnd) return 0;
+ if (page->turnValid && !turnMatches(turn, page->turnA, page->turnB)) return 0;
+ if (page->enemyValid && (enemyHp < 0 || enemyHp > page->enemyHp)) return 0;
+ if (page->actorValid && (actorHp < 0 || actorHp > page->actorHp)) return 0;
+ if (page->switchValid && !switchOn) return 0;
+ return 1;
+}
diff --git a/kon/konrmdata.h b/kon/konrmdata.h
new file mode 100644
index 0000000..1f5b8cd
--- /dev/null
+++ b/kon/konrmdata.h
@@ -0,0 +1,124 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * konrmdata.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 KONRMDATA_H
+#define KONRMDATA_H
+
+#include "konbattle.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*** definition ***/
+
+/* what mzdata writes for the database of an RPG Maker MV or MZ game: enemies and troops here, the states, skills and items
+ as the tables konbattle.h already has. all of it is indexed by the id the game gives, entry 0 is empty and so is an id that
+ is not there. the structs are only ever read. a troop's battle events are scripts, the page n of troop t is the label
+ "t<t>p<n>" in the konscript the converter wrote, counting from 1 */
+
+#define KON_RM_ELEMENTS 16
+
+typedef struct kon_rmEnemyAction {
+ int skillId;
+ int conditionType; /* 0 always, 1 turn, 2 hp, 3 mp, 4 state, 5 party level, 6 switch */
+ int param1, param2; /* turn: a and b, hp and mp: from and to in percent, the others the number to look at */
+ int rating;
+} kon_rmEnemyAction_t;
+
+typedef struct kon_rmDrop {
+ int kind; /* 1 item, 2 weapon, 3 armor */
+ int dataId;
+ int denominator; /* one chance in so many */
+} kon_rmDrop_t;
+
+typedef struct kon_rmEnemy {
+ const char *name;
+ const char *battlerName; /* the picture, in img/enemies or img/sv_enemies */
+ int battlerHue;
+ int params[KON_PARAMS]; /* max hp, max mp, atk, def, mat, mdf, agi, luk */
+ int exp, gold;
+ int hit, eva, cri, cev, mev, mrf; /* percents */
+ int pdr, mdr; /* damage taken from physical and magical hits, percent */
+ int hrg, mrg, trg;
+ int elementRate[KON_RM_ELEMENTS]; /* percent, 0 and the ones past the end of the table are no element */
+ const kon_rmEnemyAction_t *actions;
+ int actionCount;
+ const kon_rmDrop_t *drops;
+ int dropCount;
+} kon_rmEnemy_t;
+
+typedef struct kon_rmTroopMember {
+ int enemyId, x, y, hidden;
+} kon_rmTroopMember_t;
+
+/* when a battle event page runs. each condition counts only when its valid flag is 1 */
+typedef struct kon_rmTroopPage {
+ int turnValid, turnA, turnB, turnEnding;
+ int enemyValid, enemyIndex, enemyHp;
+ int actorValid, actorId, actorHp;
+ int switchValid, switchId;
+ int span; /* 0 once in the battle, 1 once a turn, 2 all the time */
+} kon_rmTroopPage_t;
+
+typedef struct kon_rmTroop {
+ const char *name;
+ const kon_rmTroopMember_t *members;
+ int memberCount;
+ const kon_rmTroopPage_t *pages;
+ int pageCount;
+} kon_rmTroop_t;
+
+/*** enemies ***/
+
+/* the battler of an enemy, at full hp and mp. the name is the enemy's */
+void kon_rmEnemyBattler(const kon_rmEnemy_t *enemy, kon_battler_t *out);
+
+/* what the game knows about the enemy and the fight, for the actions that have conditions. any of the callbacks can be NULL, which
+ reads as no, and a skill that is not usable is not picked */
+typedef struct kon_rmAi {
+ int turn;
+ const kon_battler_t *self;
+ int partyLevel;
+ int (*switchOn)(int id, void *user);
+ int (*usable)(int skillId, void *user);
+ void *user;
+} kon_rmAi_t;
+
+/* the skill the enemy does, picked the way RPG Maker does: of the usable actions whose condition is true, the ones within 2 of
+ the best rating are drawn by how much they are above that. returns 0 for none. seed is the state of a random generator, any
+ number but 0 */
+int kon_rmEnemyAction(const kon_rmEnemy_t *enemy, const kon_rmAi_t *ai, uint32_t *seed);
+
+/*** troops ***/
+
+/* is the page's condition true. turnEnd is 1 at the end of a turn. enemyHp and actorHp are the percent of hp the enemy and the actor the condition names have, or -1 when
+ they are not in the fight. turn counts from 1 and a page with a span is the game's to keep track of */
+int kon_rmTroopPageReady(const kon_rmTroopPage_t *page, int turn, int turnEnd, int enemyHp, int actorHp, int switchOn);
+
+#ifdef __cplusplus
+}
+#endif
+#endif