Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
302 changes: 263 additions & 39 deletions Ultima/AnimationEdit.cs

Large diffs are not rendered by default.

213 changes: 213 additions & 0 deletions Ultima/MobActionMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/***************************************************************************
*
* $Author: Turley
*
* "THE BEER-WARE LICENSE"
* As long as you retain this notice you can do whatever you want with
* this stuff. If we meet some day, and you think this stuff is worth it,
* you can buy me a beer in return.
*
***************************************************************************/

using System.Collections.Generic;

namespace Ultima
{
/// <summary>
/// What an action shows, independent of the body type's action layout.
/// </summary>
public enum ActionRole
{
None,
Walk,
Run,
Idle,
Fidget,
Alert,
Eat,
LieDown,
CombatIdle,
CombatAdvance,
Attack1,
Attack2,
Attack3,
AttackBow,
AttackCrossbow,
AttackThrow,
Punch,
Pillage,
Stomp,
Cast1,
Cast2,
Block,
GetHit,
GetHitInAir,
Die1,
Die2,
Fly,
TakeOff,
MountWalk,
MountRun,
MountIdle,
MountAttack,
Emote
}

/// <summary>
/// Maps actions between the monster (22), sea monster (9), animal (13) and human (35) action layouts by
/// what each action shows, falling back to the closest action the other layout has (run to walk, a third
/// attack to the first, ...). Equipment uses the human layout.
/// </summary>
public static class MobActionMap
{
private static readonly ActionRole[] _monster =
{
ActionRole.Walk, ActionRole.Idle, ActionRole.Die1, ActionRole.Die2, ActionRole.Attack1, ActionRole.Attack2,
ActionRole.Attack3, ActionRole.AttackBow, ActionRole.AttackCrossbow, ActionRole.AttackThrow, ActionRole.GetHit,
ActionRole.Pillage, ActionRole.Stomp, ActionRole.Cast1, ActionRole.Cast2, ActionRole.Block, ActionRole.Block,
ActionRole.Idle, ActionRole.Fidget, ActionRole.Fly, ActionRole.TakeOff, ActionRole.GetHitInAir
};

private static readonly ActionRole[] _sea =
{
ActionRole.Walk, ActionRole.Run, ActionRole.Idle, ActionRole.Idle, ActionRole.Fidget, ActionRole.Attack1,
ActionRole.Attack2, ActionRole.GetHit, ActionRole.Die1
};

private static readonly ActionRole[] _animal =
{
ActionRole.Walk, ActionRole.Run, ActionRole.Idle, ActionRole.Eat, ActionRole.Alert, ActionRole.Attack1,
ActionRole.Attack2, ActionRole.GetHit, ActionRole.Die1, ActionRole.Idle, ActionRole.Fidget, ActionRole.LieDown,
ActionRole.Die2
};

private static readonly ActionRole[] _human =
{
ActionRole.Walk, ActionRole.Walk, ActionRole.Run, ActionRole.Run, ActionRole.Idle, ActionRole.Idle,
ActionRole.Fidget, ActionRole.CombatIdle, ActionRole.CombatIdle, ActionRole.Attack1, ActionRole.Attack2,
ActionRole.Attack3, ActionRole.Attack3, ActionRole.Attack1, ActionRole.Attack2, ActionRole.CombatAdvance,
ActionRole.Cast1, ActionRole.Cast2, ActionRole.AttackBow, ActionRole.AttackCrossbow, ActionRole.GetHit,
ActionRole.Die1, ActionRole.Die2, ActionRole.MountWalk, ActionRole.MountRun, ActionRole.MountIdle,
ActionRole.MountAttack, ActionRole.MountAttack, ActionRole.MountAttack, ActionRole.MountAttack, ActionRole.Block,
ActionRole.Punch, ActionRole.Emote, ActionRole.Emote, ActionRole.Eat
};

// Roles tried, in order, when a layout has no action with the role itself. A mount action falls back
// to what the mount does under the rider, so a rider's actions map onto the mount's.
private static readonly Dictionary<ActionRole, ActionRole[]> _fallbacks = new()
{
[ActionRole.Run] = new[] { ActionRole.Walk },
[ActionRole.Fidget] = new[] { ActionRole.Idle },
[ActionRole.Alert] = new[] { ActionRole.Fidget, ActionRole.Idle },
[ActionRole.Eat] = new[] { ActionRole.Fidget, ActionRole.Idle },
[ActionRole.LieDown] = new[] { ActionRole.Idle },
[ActionRole.CombatIdle] = new[] { ActionRole.Idle },
[ActionRole.CombatAdvance] = new[] { ActionRole.Walk },
[ActionRole.Attack2] = new[] { ActionRole.Attack1 },
[ActionRole.Attack3] = new[] { ActionRole.Attack2, ActionRole.Attack1 },
[ActionRole.AttackBow] = new[] { ActionRole.AttackThrow, ActionRole.Attack1 },
[ActionRole.AttackCrossbow] = new[] { ActionRole.AttackBow, ActionRole.AttackThrow, ActionRole.Attack1 },
[ActionRole.AttackThrow] = new[] { ActionRole.AttackBow, ActionRole.Attack1 },
[ActionRole.Punch] = new[] { ActionRole.Attack1 },
[ActionRole.Pillage] = new[] { ActionRole.Attack1 },
[ActionRole.Stomp] = new[] { ActionRole.Attack2, ActionRole.Attack1 },
[ActionRole.Cast1] = new[] { ActionRole.Cast2, ActionRole.Attack1 },
[ActionRole.Cast2] = new[] { ActionRole.Cast1, ActionRole.Attack1 },
[ActionRole.Block] = new[] { ActionRole.GetHit },
[ActionRole.GetHitInAir] = new[] { ActionRole.GetHit },
[ActionRole.Die2] = new[] { ActionRole.Die1 },
[ActionRole.Fly] = new[] { ActionRole.Walk },
[ActionRole.TakeOff] = new[] { ActionRole.Fly, ActionRole.Walk },
[ActionRole.MountWalk] = new[] { ActionRole.Walk },
[ActionRole.MountRun] = new[] { ActionRole.Run, ActionRole.Walk },
[ActionRole.MountIdle] = new[] { ActionRole.Idle },
[ActionRole.MountAttack] = new[] { ActionRole.MountIdle, ActionRole.Idle },
[ActionRole.Emote] = new[] { ActionRole.Fidget, ActionRole.Idle },
};

/// <summary>
/// The action roles of a layout, indexed by action.
/// </summary>
public static IReadOnlyList<ActionRole> GetRoles(MobType type)
{
return type switch
{
MobType.Monster => _monster,
MobType.Sea => _sea,
MobType.Animal => _animal,
_ => _human
};
}

public static ActionRole GetRole(MobType type, int action)
{
IReadOnlyList<ActionRole> roles = GetRoles(type);
return action >= 0 && action < roles.Count ? roles[action] : ActionRole.None;
}

/// <summary>
/// The action of layout <paramref name="to"/> that best matches <paramref name="action"/> of layout
/// <paramref name="from"/>, or -1 when the target layout has nothing close. Within one layout an action
/// maps to itself.
/// </summary>
public static int MapAction(MobType from, int action, MobType to)
{
from = Normalize(from);
to = Normalize(to);
if (from == to)
{
return action >= 0 && action < GetRoles(to).Count ? action : -1;
}

return FindAction(to, GetRole(from, action));
}

/// <summary>
/// The first action of layout <paramref name="type"/> with <paramref name="role"/> or, failing that, with
/// the nearest fallback role; -1 when there is none.
/// </summary>
public static int FindAction(MobType type, ActionRole role)
{
if (role == ActionRole.None)
{
return -1;
}

IReadOnlyList<ActionRole> roles = GetRoles(Normalize(type));
int found = IndexOf(roles, role);
if (found >= 0 || !_fallbacks.TryGetValue(role, out ActionRole[] fallbacks))
{
return found;
}

foreach (ActionRole fallback in fallbacks)
{
found = IndexOf(roles, fallback);
if (found >= 0)
{
return found;
}
}

return -1;
}

private static MobType Normalize(MobType type)
{
return type == MobType.Equipment ? MobType.Human : type;
}

private static int IndexOf(IReadOnlyList<ActionRole> roles, ActionRole role)
{
for (int i = 0; i < roles.Count; ++i)
{
if (roles[i] == role)
{
return i;
}
}

return -1;
}
}
}
149 changes: 149 additions & 0 deletions UoFiddler.Controls/Classes/FramesXml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/***************************************************************************
*
* $Author: Turley
*
* "THE BEER-WARE LICENSE"
* As long as you retain this notice you can do whatever you want with
* this stuff. If we meet some day, and you think this stuff is worth it,
* you can buy me a beer in return.
*
***************************************************************************/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace UoFiddler.Controls.Classes
{
/// <summary>
/// One frame listed in a frames XML file.
/// </summary>
public sealed class FramesXmlFrame
{
public int Direction { get; set; }
public int Index { get; set; }
public Point Center { get; set; }

/// <summary>Image file name, in the XML file's folder or its <c>_frames</c> folder.</summary>
public string File { get; set; }
}

/// <summary>
/// Animation frames saved as one PNG per frame plus an XML file listing each frame's direction, index and
/// center. The images go in a <c>{name}_frames</c> folder next to <c>{name}.xml</c>.
/// </summary>
public static class FramesXml
{
public static string GetFramesFolder(string xmlFile)
{
return Path.Combine(Path.GetDirectoryName(xmlFile) ?? string.Empty, Path.GetFileNameWithoutExtension(xmlFile) + "_frames");
}

/// <summary>
/// Saves each frame as <c>{direction}_{index}.png</c> in the frames folder and lists them in <paramref name="xmlFile"/>.
/// </summary>
public static void Write(string xmlFile, IReadOnlyList<(int Direction, int Index, Point Center, Bitmap Image)> frames)
{
string framesFolder = GetFramesFolder(xmlFile);
Directory.CreateDirectory(framesFolder);

var root = new XElement("Animation");
foreach (var (direction, index, center, image) in frames)
{
string fileName = $"{direction}_{index:D2}.png";
image.Save(Path.Combine(framesFolder, fileName), ImageFormat.Png);
root.Add(new XElement("Frame",
new XAttribute("dir", direction),
new XAttribute("index", index),
new XAttribute("centerX", center.X),
new XAttribute("centerY", center.Y),
new XAttribute("file", fileName)));
}

new XDocument(root).Save(xmlFile);
}

/// <exception cref="InvalidDataException">The file is not a frames XML file.</exception>
public static List<FramesXmlFrame> Read(string xmlFile)
{
string name = Path.GetFileName(xmlFile);
XDocument document;
try
{
document = XDocument.Load(xmlFile);
}
catch (XmlException ex)
{
throw new InvalidDataException($"{name} is not a valid frames XML file: {ex.Message}", ex);
}

if (document.Root?.Name.LocalName != "Animation")
{
throw new InvalidDataException($"{name} is not a frames XML file: the root element is not <Animation>.");
}

var frames = new List<FramesXmlFrame>();
foreach (XElement element in document.Root.Elements("Frame"))
{
frames.Add(new FramesXmlFrame
{
Direction = ReadInt(element, "dir", name),
Index = ReadInt(element, "index", name),
Center = new Point(ReadInt(element, "centerX", name), ReadInt(element, "centerY", name)),
File = (string)element.Attribute("file") ?? throw new InvalidDataException($"{name}: a <Frame> has no file attribute.")
});
}

return frames;
}

/// <summary>
/// Path of a frame's image, or null if it is in neither the XML file's folder nor the frames folder.
/// Only the file name is used, so an XML file can't point outside those folders.
/// </summary>
public static string FindImage(string xmlFile, FramesXmlFrame frame)
{
string fileName = Path.GetFileName(frame.File);
if (string.IsNullOrEmpty(fileName))
{
return null;
}

string beside = Path.Combine(Path.GetDirectoryName(xmlFile) ?? string.Empty, fileName);
if (System.IO.File.Exists(beside))
{
return beside;
}

string inFolder = Path.Combine(GetFramesFolder(xmlFile), fileName);
return System.IO.File.Exists(inFolder) ? inFolder : null;
}

/// <summary>
/// Loads an image as 32bpp ARGB without keeping the file locked.
/// </summary>
public static Bitmap LoadImage(string file)
{
using (var loaded = new Bitmap(file))
{
return loaded.Clone(new Rectangle(0, 0, loaded.Width, loaded.Height), PixelFormat.Format32bppArgb);
}
}

private static int ReadInt(XElement element, string attribute, string name)
{
string value = (string)element.Attribute(attribute);
if (!int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int result))
{
throw new InvalidDataException($"{name}: a <Frame> has no valid {attribute} attribute.");
}

return result;
}
}
}
Loading