mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-26 09:28:07 +08:00
* build: migrate C# formatting and add offscreen editor test support Host IMGUI tests on a detached panel without creating a native editor window. * feat(core): decode native battle records, geometry and animation metadata Read the two game variants’ battle tables and model data, with binary-reader and original-resource coverage. * feat(battle): implement deterministic combat rules and action settlement Implement actors, inventory, formation, action timing, status interruption, AI, stage ownership and deterministic replay. Keep the domain and its regression suite independent of Unity. * feat(battle): project native HUD state and input commands Add the presentation assembly and tests for command routing, lineup selection, floating numbers and stage messages. * feat(rendering): add battle lighting, actor appearance and native shadows Support baked and toon lighting, actor and weapon rendering, display refresh rate and resource ownership. * feat(effects): drive battle effects through shared resource previews Bind battle control nodes to action owners. Share preview lighting, geometry and effect resources with the existing Unity editors. * refactor(game): isolate story battle targeting and actor lifetimes Preserve scene ownership, bound story battle targeting and stop debug attacks when their actor leaves the scene. * feat(editor): compile original game data into battle scenarios Build per-variant animation and ability catalogs, encounter music, scene lighting profiles, random parties, equipment and test inventory from the original resources. * feat(editor): play battle sessions with animated previews and synced media Drive the preview scene, camera, effects, hit feedback, result choreography, music and movies from simulation snapshots and events. * feat(editor): add the battle workbench, native HUD and input controls Expose scenario authoring, replay, diagnostics, responsive command menus and mouse, touch and gamepad targeting. Include the editor workflow and HUD integration tests.
108 lines
5.5 KiB
C#
108 lines
5.5 KiB
C#
// ------------------------------------------------------------------------
|
|
// Copyright (c) 2026, Jiaqi (0x7c13) Liu. All rights reserved.
|
|
// See LICENSE file in the project root for license information.
|
|
// ------------------------------------------------------------------------
|
|
|
|
namespace Pal3.BattleEditor
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Battle;
|
|
using Core.Contract.Constants;
|
|
using Core.Contract.Enums;
|
|
|
|
// Uses a separate authoring RNG: generating examples never consumes battle RNG.
|
|
public sealed class BattleRandomExampleBuilder
|
|
{
|
|
private readonly BattleData _data;
|
|
private readonly int[] _allies;
|
|
private readonly IGrouping<int, int>[] _enemyLevels;
|
|
private readonly BattleRandomEquipmentBuilder _equipment;
|
|
|
|
public BattleRandomExampleBuilder(BattleData data)
|
|
{
|
|
_data = data ?? throw new ArgumentNullException(nameof(data));
|
|
_equipment = new BattleRandomEquipmentBuilder(data);
|
|
_allies = ActorConstants.MainActorCombatActorIdMap.Values.OrderBy(id => id)
|
|
.Where(id => Available(id, BattleSide.Ally)).ToArray();
|
|
_enemyLevels = data.Database.CombatActorInfos.OrderBy(pair => pair.Key)
|
|
.Where(pair => pair.Key > 0 && pair.Value.Type != CombatActorType.MainActor &&
|
|
pair.Value.Level is >= 1 and <= 100 && pair.Value.AttributeValues[ActorAttributeType.Hp] > 0 &&
|
|
pair.Value.SpecialActionId == 0 && data.Database.SkillInfos.ContainsKey((int)pair.Value.NormalAttackModeId))
|
|
.Select(pair => pair.Key).Where(id => Available(id, BattleSide.Enemy))
|
|
.GroupBy(id => data.Database.CombatActorInfos[id].Level).ToArray();
|
|
if (_allies.Length == 0 || _enemyLevels.Length == 0 || data.Scenes.Length == 0)
|
|
throw new InvalidOperationException("No complete original-data roster or battle scene is available for random examples.");
|
|
}
|
|
|
|
private bool Available(int id, BattleSide side)
|
|
{
|
|
if (!_data.Database.CombatActorInfos.ContainsKey(id))
|
|
return false;
|
|
BattleParticipant actor = _data.CreateParticipant(id, side, BattleSlot.Water);
|
|
try
|
|
{
|
|
return _data.TryActionPath(actor, _data.IdleAction(actor), out _);
|
|
}
|
|
catch (Exception error) when (error is FileNotFoundException or DirectoryNotFoundException or InvalidDataException or
|
|
NotSupportedException or InvalidOperationException or FormatException)
|
|
{
|
|
// An incomplete resource set can still supply other test participants.
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public BattleScenario Create(Random random)
|
|
{
|
|
if (random == null)
|
|
throw new ArgumentNullException(nameof(random));
|
|
BattleScenario example = BattleTestLoadoutBuilder.CreateScenario(_data);
|
|
example.Scene = _data.Scenes[random.Next(_data.Scenes.Length)];
|
|
example.Seed = random.Next();
|
|
example.SceneElement = (int)_data.SceneMap.CombatSceneElementTypeInfo.GetValueOrDefault(example.Scene);
|
|
IGrouping<int, int> level = _enemyLevels[random.Next(_enemyLevels.Length)];
|
|
int[] enemies = level.ToArray();
|
|
int[] allies = Shuffle(_allies, random);
|
|
BattleSlot[] slots = Shuffle((BattleSlot[])Enum.GetValues(typeof(BattleSlot)), random);
|
|
int allyCount = Math.Min(4, allies.Length);
|
|
for (int index = 0; index < allyCount; index++)
|
|
{
|
|
BattleParticipant actor = _data.CreateTestParticipant(allies[index], BattleSide.Ally, slots[index]);
|
|
actor.Level = Math.Min(100, level.Key + 3);
|
|
actor.AttributeSource = BattleAttributeSource.LevelGrowth;
|
|
// The reader indexes by actual level; the original final level uses INT_MAX.
|
|
actor.Experience = actor.Level < _data.Layout.LevelExperienceTable.Length ? _data.Layout.LevelExperienceTable[actor.Level] : int.MaxValue;
|
|
_equipment.Equip(actor, random);
|
|
example.Participants.Add(actor);
|
|
}
|
|
slots = Shuffle(slots, random);
|
|
int enemyCount = random.Next(1, 7);
|
|
for (int index = 0; index < enemyCount; index++)
|
|
example.Participants.Add(_data.CreateParticipant(enemies[random.Next(enemies.Length)], BattleSide.Enemy, slots[index]));
|
|
|
|
// Compile growth and equipment through the same adapter as a real battle.
|
|
BattleDefinitions definitions = BattleDefinitionCompiler.Build(_data, _data.Capture(example));
|
|
foreach (BattleParticipant actor in example.Participants.Where(actor => actor.Side == BattleSide.Ally))
|
|
{
|
|
BattleActorDefinition definition = definitions.Actors[actor.InstanceId];
|
|
actor.Attributes = BattleAttributeCalculator.Calculate(actor, definition, example.Variant);
|
|
actor.Vitals = actor.Attributes.Take(3).ToArray();
|
|
}
|
|
return example;
|
|
}
|
|
|
|
private static T[] Shuffle<T>(IEnumerable<T> source, Random random)
|
|
{
|
|
T[] values = source.ToArray();
|
|
for (int index = values.Length - 1; index > 0; index--)
|
|
{
|
|
int other = random.Next(index + 1);
|
|
(values[index], values[other]) = (values[other], values[index]);
|
|
}
|
|
return values;
|
|
}
|
|
}
|
|
}
|