fixed # 新增药水机制

This commit is contained in:
2026-11-23 23:45:37 +08:00
parent f955716c97
commit 0eeba640ae
45 changed files with 1053 additions and 235 deletions
+99
View File
@@ -0,0 +1,99 @@
import { campEnemy } from "../battle/camps.js";
import { createOpeningEffects } from "../relics/combat.js";
import { getPotion, potionIds, potionSlots, removePotion } from "./state.js";
const selectedSlots = new WeakMap();
const skillId = id => "mengsan_use_" + id.toLowerCase() + "_shuying";
export const potionSkillIds = potionIds.map(skillId);
export const potionSkillTranslations = Object.fromEntries(
potionIds.map(id => [skillId(id), getPotion(id).name]));
export const potionBlockType = "mengsanPotionBlock";
const openingEffects = {
ENERGY_POTION: "energy",
FIRE_POTION: "targetDamage",
STRENGTH_POTION: "openingStrength",
};
function phaseChoice(event, player, battle) {
return battle?.session.active && player === battle.owner && player.isAlive() &&
event?.name === "chooseToUse" && event.type === "phase" &&
event.player === player && event.parent?.name === "phaseUse" && !event.respondTo;
}
const legalEnemy = (game, player, enemy) => game.players.includes(enemy) &&
enemy.isAlive() && campEnemy(player, enemy);
export function createPotionSkills(getBattle, game) {
return Object.fromEntries(potionIds.map(id => {
const { amount, enemy, name } = getPotion(id);
const skill = {
enable: "phaseUse",
manualConfirm: true,
discard: false,
lose: false,
log: false,
filter(event, player) {
const battle = getBattle();
return phaseChoice(event, player, battle) && potionSlots(battle.run.player).includes(id) &&
(!enemy || game.players.some(target => legalEnemy(game, player, target)));
},
async content(event, trigger, player) {
const battle = getBattle();
const choice = event.getParent("chooseToUse");
if (!phaseChoice(choice, player, battle)) return;
const target = event.targets?.[0];
if (enemy && !legalEnemy(game, player, target)) return;
const inventory = battle.run.player;
const request = selectedSlots.get(choice);
selectedSlots.delete(choice);
const index = request?.id === id ? request.index : potionSlots(inventory).indexOf(id);
if (!removePotion(inventory, index, id)) return;
battle.potionUI?.refresh();
game.log(player, "使用了" + name);
if (id === "SWIFT_POTION") {
await game.mengsanDraw_shuying(player, amount);
} else if (id === "BLOCK_POTION") {
await player.changeHujia(amount, potionBlockType);
} else {
const applyEffect = createOpeningEffects(game, player, () => battle.energyUI.get(player)?.());
await applyEffect({ effect: openingEffects[id], amount, target });
}
},
};
if (enemy) {
skill.selectTarget = 1;
skill.filterTarget = (card, player, target) => legalEnemy(game, player, target);
}
return [skillId(id), skill];
}));
}
export function createPotionActions(battle, { lib, game, ui, _status }) {
const { owner, run } = battle;
const ready = () => {
const event = _status.event;
return phaseChoice(event, owner, battle) && event.isMine() && !event.finished &&
!event.skill && !_status.paused2 && !ui.intro &&
!ui.selected.cards.length && !ui.selected.targets.length && !ui.selected.buttons.length;
};
const canUse = (index, id) => ready() && potionSlots(run.player)[index] === id &&
lib.filter.filterEnable(_status.event, owner, skillId(id));
return {
canUse,
canDiscard: ready,
use(index, id) {
if (!canUse(index, id)) return;
selectedSlots.set(_status.event, { index, id });
ui.click.skill(skillId(id));
},
discard(index, id) {
if (!ready() || !removePotion(run.player, index, id)) return;
selectedSlots.delete(_status.event);
battle.potionUI?.refresh();
game.log(owner, "丢弃了" + getPotion(id).name);
delete _status.event._skillChoice;
game.check();
},
};
}
+65
View File
@@ -0,0 +1,65 @@
import { getPotion, potionIds } from "./state.js";
const configKey = "mengsan_potion_localization_shuying";
const assetBase = new URL("../assets/sts2/potions/", import.meta.url);
const referenceRoot = "D:/workspace/术樱包-本地资料/参考资料/";
const localText = referenceRoot + "Slay the Spire 2/localization/zhs/potions.json";
const localIcons = referenceRoot +
"梦三/STS2拆分图集/Godot_Atlas_Sprites_v0.111.0/images/atlases/potion_atlas.sprites/";
let translations = null;
const localImages = new Map();
export function setPotionText(source) {
const entries = potionIds.flatMap(id => [id + ".title", id + ".description"]);
if (entries.some(key => typeof source?.[key] !== "string" || !source[key].trim())) {
throw new Error("请选择包含首批五种药水名称和说明的 potions.json");
}
translations = Object.fromEntries(entries.map(key => [key, source[key]]));
return translations;
}
export const hasPotionText = () => translations !== null;
export async function loadPotionAssets(lib) {
const fs = lib.node?.fs?.promises;
if (fs) {
await Promise.allSettled([
fs.readFile(localText, "utf8").then(source => setPotionText(JSON.parse(source))),
...potionIds.map(async id => {
const bytes = await fs.readFile(localIcons + id.toLowerCase() + ".png");
localImages.set(id, "data:image/png;base64," + bytes.toString("base64"));
}),
]);
}
if (hasPotionText()) return;
if (lib.config[configKey]) {
try { setPotionText(lib.config[configKey]); } catch { /* Try the resource directory. */ }
}
if (!hasPotionText()) {
try {
const response = await fetch(new URL("potions.json", assetBase));
if (response.ok) setPotionText(await response.json());
} catch { /* The player can load their JSON from the map. */ }
}
}
export async function importPotionText(file, game) {
const text = setPotionText(JSON.parse(await file.text()));
game.saveConfig(configKey, text);
}
export function potionView(id) {
const definition = getPotion(id);
if (!definition) return null;
const template = translations?.[id + ".description"];
const rendered = template?.replace(/\{(\w+)(?::([^}]+))?\}/g, (token, key, formatter) => {
if (key !== definition.variable) return token;
return formatter === "energyIcons()" ? "⚡".repeat(definition.amount) : String(definition.amount);
}) ?? (definition.stat + " " + definition.amount);
return {
name: translations?.[id + ".title"] ?? definition.name,
description: rendered.replace(/\[\/?(?:blue|gold|green|red)\]/g, ""),
richDescription: rendered,
image: localImages.get(id) ?? new URL(id.toLowerCase() + ".png", assetBase).href,
};
}
+63
View File
@@ -0,0 +1,63 @@
import { nextRandom } from "../progression/state.js";
export const potions = Object.freeze({
ENERGY_POTION: { name: "能量药水", stat: "能量", variable: "Energy", amount: 2 },
BLOCK_POTION: { name: "格挡药水", stat: "格挡", variable: "Block", amount: 12 },
SWIFT_POTION: { name: "迅捷药水", stat: "抽牌", variable: "Cards", amount: 3 },
FIRE_POTION: { name: "火焰药水", stat: "伤害", variable: "Damage", amount: 20, enemy: true },
STRENGTH_POTION: { name: "力量药水", stat: "力量", variable: "StrengthPower", amount: 2 },
});
export const potionIds = Object.freeze(Object.keys(potions));
export const getPotion = id => Object.hasOwn(potions, id) ? potions[id] : null;
export const potionCapacity = player => 3 + (player.potionSlotBonus ?? 0);
export const potionSlots = player => Array.from({ length: potionCapacity(player) }, (_, index) =>
getPotion(player.potions?.[index]) ? player.potions[index] : null);
export function samplePotions(run, count, exclude = []) {
const pool = potionIds.filter(id => !exclude.includes(id));
const result = [];
while (result.length < count && pool.length) {
result.push(pool.splice(Math.floor(nextRandom(run) * pool.length), 1)[0]);
}
return result;
}
export function rollPotionReward(run, elite) {
const chance = run.potionDropChance ?? 40;
const dropped = nextRandom(run) * 100 < chance + (elite ? 12.5 : 0);
run.potionDropChance = chance + (dropped ? -10 : 10);
return dropped ? samplePotions(run, 1) : [];
}
export function createPotionOffer(run, excluded) {
const [id] = samplePotions(run, 1, excluded);
return id ? { id, price: Math.round(50 * (0.95 + nextRandom(run) * 0.1)) } : null;
}
export function acquirePotion(player, id, replaceIndex = null) {
if (!getPotion(id)) return false;
const slots = potionSlots(player);
const empty = slots.indexOf(null);
const index = replaceIndex ?? empty;
if (!Number.isInteger(index) || index < 0 || index >= slots.length ||
(replaceIndex !== null && (empty >= 0 || !slots[index]))) return false;
slots[index] = id;
player.potions = slots;
return true;
}
export function removePotion(player, index, id) {
const slots = potionSlots(player);
if (!getPotion(id) || slots[index] !== id) return false;
slots[index] = null;
player.potions = slots;
return true;
}
export function applyPotionReward(player, candidates, selection) {
if (selection == null) return;
if (!candidates.includes(selection.id) || !acquirePotion(player, selection.id, selection.replaceIndex)) {
throw new Error("药水奖励选择无效");
}
}