/* 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 /*** actors ***/ int kon_rmActorStart(kon_party_t *party, int index, const kon_rmActor_t *actor, int weaponBase, int armorBase) { int slot; if (!party || !actor || !actor->name) return 0; if (!kon_actorInit(party, index, actor->name, actor->classId, actor->initialLevel)) return 0; for (slot = 0; slot < KON_EQUIP_SLOTS; slot++) { int id = actor->equips[slot], item; if (id <= 0 || id > 1000000) continue; item = (slot == 0 ? weaponBase : armorBase) + id; if (kon_bagAdd(party, item, 1) != 1) continue; if (!kon_actorEquip(party, index, slot, item)) kon_bagRemove(party, item, 1); } return 1; } /*** 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; }