Skip to content

Commit

Permalink
chore: add documentation on more complex methods
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Feb 13, 2024
1 parent 90418fb commit 53744e3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lc-hax/Scripts/Commands/BombardCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ JetpackItem[] GetAvailableJetpacks() =>
.Where(jetpack => !jetpack.Reflect().GetInternalField<bool>("jetpackBroken"))
.ToArray();

/// <summary>
/// Grab and discard jetpacks to a random location of the same elevation near the target player.
/// If the target player is far away, it may take a while for the jetpacks to reach the player.
/// The jetpacks will only explode if they within 5 units of the target player.
/// </summary>
IEnumerator BombardAsync(PlayerControllerB player, Transform targetTransform, JetpackItem[] jetpacks) {
float currentWeight = player.carryWeight;

Expand Down
4 changes: 2 additions & 2 deletions lc-hax/Scripts/Commands/ChibakuTenseiCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
internal class ChibakuTenseiCommand : ICommand {
Vector3 spinningY = new(0, 2, 0);

Result TeleportPlayerToRandom(StringArray args) {
Result AttractAndSpin(StringArray args) {
if (Helper.GetActivePlayer(args[0]) is not PlayerControllerB targetPlayer) {
return new Result(message: "Target player is not alive or found!");
}
Expand Down Expand Up @@ -99,7 +99,7 @@ public void Execute(StringArray args) {
return;
}

Result result = this.TeleportPlayerToRandom(args);
Result result = this.AttractAndSpin(args);

if (!result.Success) {
Chat.Print(result.Message);
Expand Down
5 changes: 5 additions & 0 deletions lc-hax/Scripts/Commands/FatalityCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

[Command("fatality")]
internal class FatalityCommand : ICommand {
/// <summary>
/// Teleports the enemy to the target player and perform the fatality.
/// Teleporting certain enemies outside of the factory can lag the user, so this burden is passed to the target player.
/// </summary>
/// <returns>an error string if any</returns>
string? HandleEnemy<T>(PlayerControllerB targetPlayer, Action<PlayerControllerB, T> enemyHandler) where T : EnemyAI {
if (Helper.LocalPlayer is not PlayerControllerB localPlayer || Helper.GetEnemy<T>() is not T enemy) {
return "Enemy has not yet spawned!";
Expand Down
5 changes: 5 additions & 0 deletions lc-hax/Scripts/Commands/SellCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ void SellEverything(DepositItemsDesk depositItemsDesk, PlayerControllerB player)
this.SellObject(depositItemsDesk, player, grabbableObject);
});

/// <summary>
/// Uses the 0-1 knapsack algorithm to sell scraps to reach the target value.
/// The actual scrap value is usually lower than the displayed value due to the company buying rate.
/// </summary>
/// <returns>the remaining amount left to reach the target value</returns>
ulong SellScrapValue(DepositItemsDesk depositItemsDesk, PlayerControllerB player, StartOfRound startOfRound, ulong targetValue) {
ReadOnlySpan<GrabbableObject> sellableScraps = Helper.Grabbables.WhereIsNotNull().Where(this.CanBeSold).ToArray();

Expand Down
14 changes: 14 additions & 0 deletions lc-hax/Scripts/Helpers/FuzzyMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace Hax;

internal static partial class Helper {
/// <summary>
/// Find the longest common substring between two strings.
/// </summary>
/// <returns>the length of the longest common substring</returns>
static int LongestCommonSubstring(ReadOnlySpan<char> query, ReadOnlySpan<char> original) {
int originalLength = original.Length;
int queryLength = query.Length;
Expand All @@ -31,13 +35,23 @@ static int LongestCommonSubstring(ReadOnlySpan<char> query, ReadOnlySpan<char> o
return result;
}

/// <summary>
/// The similarity is calculated by first, penalising the Levenshtein distance between the two strings.
/// This alone is often not enough, so a reward is given for each character in the longest common substring.
/// </summary>
/// <returns>the total similarity weight</returns>
static int GetSimilarityWeight(string query, string original) {
int distancePenalty = Levenshtein.GetDistance(query, original);
int commonalityReward = Helper.LongestCommonSubstring(query, original) * -2;

return distancePenalty + commonalityReward;
}

/// <summary>
/// Find the closest match to the query from a list of strings.
/// If the string is empty or the list is empty, we return null.
/// </summary>
/// <returns>the string with closest match to the query or null</returns>
internal static string? FuzzyMatch(string? query, ReadOnlySpan<string> strings) {
if (strings.Length is 0 || string.IsNullOrWhiteSpace(query)) return null;

Expand Down

0 comments on commit 53744e3

Please sign in to comment.