pure-full-lily/js/plugins/SkillVariableCondition.js
2025-09-16 10:12:44 -05:00

123 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*:
* @target MZ
* @plugindesc v1.1 スキル使用条件拡張 変数+プレイヤーステート │ SkillVariableCondition.js
* @author IvI
*
* @help
* ■更新点 v1.1
* ① <RequirePlayerState: 1,5,...> … プレイヤー側の誰かが 指定ステートを有 していると使用可
* ② <ForbidPlayerState : 3,7,...> … プレイヤー側の誰かが 指定ステートを有 していると使用不可
*
* ※いずれもカンマ区切りで複数指定可。空白は無視されます。
* ※敵スキル向けに想定していますが、アクター側スキルにも同様に効きます。
*
* ■既存機能
* <SkillCondition: JavaScript式> … $gameVariables 等を用いた自由判定
*
* 例変数11 が 'ND' または 'NDH' 以外のときのみ
* <SkillCondition:
* !['ND','NDH'].includes($gameVariables.value(11))
* >
*
* ■プラグインコマンド
* なし
*
* ■利用規約
* MIT ライセンス
* --------------------------------------------------------
*/
(() => {
"use strict";
// ---------- 定数 ----------
const META_VAR_COND = /<SkillCondition\s*:\s*([\s\S]*?)>/i;
const META_REQ_P_STATE = /<RequirePlayerState\s*:\s*([\d,\s]+)>/i;
const META_FORBID_P_STATE = /<ForbidPlayerState\s*:\s*([\d,\s]+)>/i;
// ---------- DBロード時にメタタグ解析 ----------
const _DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function() {
if (!_DataManager_isDatabaseLoaded.call(this)) return false;
if (!this._skillVarCond_initialized) {
this._skillVarCond_initialized = true;
parseSkillConditions($dataSkills);
}
return true;
};
/** @param {RPG.DataSkill[]} arr */
function parseSkillConditions(arr) {
arr.forEach(skill => {
if (!skill || !skill.note) return;
const note = skill.note;
// 1) 任意の JS 式
const varMatch = note.match(META_VAR_COND);
if (varMatch) skill._varConditionFormula = varMatch[1].trim();
// 2) 必須プレイヤーステート
const reqMatch = note.match(META_REQ_P_STATE);
if (reqMatch) {
skill._requirePlayerStates = reqMatch[1]
.split(",")
.map(s => Number(s.trim()))
.filter(n => n);
}
// 3) 禁止プレイヤーステート
const forbidMatch = note.match(META_FORBID_P_STATE);
if (forbidMatch) {
skill._forbidPlayerStates = forbidMatch[1]
.split(",")
.map(s => Number(s.trim()))
.filter(n => n);
}
});
}
// ---------- 追加判定 ----------
const _Game_BattlerBase_meetsSkillConditions =
Game_BattlerBase.prototype.meetsSkillConditions;
Game_BattlerBase.prototype.meetsSkillConditions = function(skill) {
// 標準判定
if (!_Game_BattlerBase_meetsSkillConditions.call(this, skill)) {
return false;
}
// ① 任意の JS 式
if (skill._varConditionFormula) {
try {
/* eslint-disable no-eval */
if (!eval(skill._varConditionFormula)) return false;
/* eslint-enable no-eval */
} catch (e) {
console.error(
"SkillVariableCondition: 式評価エラー",
`スキルID:${skill.id} 式:${skill._varConditionFormula}`,
e
);
return false;
}
}
// ② プレイヤー必須ステート
if (skill._requirePlayerStates?.length) {
const ok = skill._requirePlayerStates.some(stateId =>
$gameParty.aliveMembers().some(m => m.isStateAffected(stateId))
);
if (!ok) return false;
}
// ③ プレイヤー禁止ステート
if (skill._forbidPlayerStates?.length) {
const ng = skill._forbidPlayerStates.some(stateId =>
$gameParty.aliveMembers().some(m => m.isStateAffected(stateId))
);
if (ng) return false;
}
return true;
};
})();