Skip to content

Commit

Permalink
added quest commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaksuhn committed Dec 12, 2023
1 parent a40cde6 commit 930c2bf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
15 changes: 14 additions & 1 deletion SomethingNeedDoing/Interface/HelpWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,14 @@ static void DisplayChangelog(string date, string changes, bool separator = true)
ImGui.PushFont(UiBuilder.MonoFont);

DisplayChangelog(
"2023-06-23",
"2023-12-12",
"- Added IsQuestAccepted()\n" +
"- Added IsQuestComplete()\n" +
"- Added GetQuestSequence()\n" +
"- Added GetQuestIDByName()\n");

DisplayChangelog(
"2023-11-06",
"- Added GetLevel()\n" +
"- Added \"Game Data\" tab to the help menu.\n" +
"- Added GetGp() and GetMaxGp() (thanks nihilistzsche)\n" +
Expand Down Expand Up @@ -947,6 +954,12 @@ float GetPlayerRawZPos()
float GetDistanceToPoint(float x, float y, float z))
int GetLevel(uint ExpArrayIndex = -1)
string GetQuestNameByID(ushort id)
bool IsQuestAccepted(ushort id)
bool IsQuestComplete(ushort id)
byte GetQuestSequence(ushort id)
uint? GetQuestIDByName(string name)
".Trim();

ImGui.TextWrapped(text);
Expand Down
45 changes: 44 additions & 1 deletion SomethingNeedDoing/Misc/CommandInterface.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;

using System.Text.RegularExpressions;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Logging;
using ECommons.DalamudServices;
Expand All @@ -15,6 +16,7 @@
using FFXIVClientStructs.FFXIV.Client.UI.Info;
using FFXIVClientStructs.FFXIV.Component.GUI;
using Lumina.Excel.GeneratedSheets;
using Lumina.Text;
using SomethingNeedDoing.Exceptions;
using SomethingNeedDoing.Interface;
using SomethingNeedDoing.IPC;
Expand Down Expand Up @@ -557,6 +559,47 @@ public unsafe int GetLevel(int expArrayIndex = -1)

public unsafe int GetFCRank() => ((InfoProxyFreeCompany*)Framework.Instance()->UIModule->GetInfoModule()->GetInfoProxyById(InfoProxyId.FreeCompany))->Rank;

private static readonly Dictionary<uint, Quest>? QuestSheet = Svc.Data?.GetExcelSheet<Quest>()?.Where(x => x.Id.RawString.Length > 0).ToDictionary(i => i.RowId, i => i);
public static string GetQuestNameByID(ushort id)
{
if (id > 0)
{
var digits = id.ToString().Length;
if (QuestSheet!.Any(x => Convert.ToInt16(x.Value.Id.RawString.GetLast(digits)) == id))
{
return QuestSheet!.First(x => Convert.ToInt16(x.Value.Id.RawString.GetLast(digits)) == id).Value.Name.RawString.Replace("", "").Trim();
}
}
return "";
}

public unsafe bool IsQuestAccepted(ushort id) => QuestManager.Instance()->IsQuestAccepted(id);
public unsafe bool IsQuestComplete(ushort id) => QuestManager.IsQuestComplete(id);
public unsafe byte GetQuestSequence(ushort id) => QuestManager.GetQuestSequence(id);

private readonly List<SeString> questNames = Svc.Data.GetExcelSheet<Quest>(Svc.ClientState.ClientLanguage)!.Select(x => x.Name).ToList();
public uint? GetQuestIDByName(string name)
{
var matchingRows = questNames.Select((n, i) => (n, i)).Where(t => !string.IsNullOrEmpty(t.n) && IsMatch(name, t.n)).ToList();
if (matchingRows.Count > 1)
{
matchingRows = matchingRows.OrderByDescending(t => MatchingScore(t.n, name)).ToList();
}
return matchingRows.Count > 0 ? Svc.Data.GetExcelSheet<Quest>(Svc.ClientState.ClientLanguage)!.GetRow((uint)matchingRows.First().i)!.RowId : null;
}

private static bool IsMatch(string x, string y) => Regex.IsMatch(x, $@"\b{Regex.Escape(y)}\b");
private static object MatchingScore(string item, string line)
{
var score = 0;

// primitive matching based on how long the string matches. Enough for now but could need expanding later
if (line.Contains(item))
score += item.Length;

return score;
}

private unsafe int GetNodeTextAsInt(AtkTextNode* node, string error)
{
try
Expand Down
7 changes: 7 additions & 0 deletions SomethingNeedDoing/Misc/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public static string Join(this IEnumerable<string> values, char separator)
/// <inheritdoc cref="string.Join(string, string?[])"/>
public static string Join(this IEnumerable<string> values, string separator)
=> string.Join(separator, values);

public static string GetLast(this string source, int tail_length)
{
if (tail_length >= source.Length)
return source;
return source.Substring(source.Length - tail_length);
}
}

0 comments on commit 930c2bf

Please sign in to comment.