Skip to content

Commit

Permalink
Adjust helper functions to convert waypoints longer than 2 letters
Browse files Browse the repository at this point in the history
  • Loading branch information
ZivDero authored Sep 13, 2024
1 parent 77158f1 commit 4614ee6
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/TSMapEditor/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using TSMapEditor.GameMath;
using TSMapEditor.Models;
using TSMapEditor.Models.Enums;
Expand Down Expand Up @@ -174,38 +175,44 @@ public static int GetWaypointNumberFromAlphabeticalString(string str)
if (string.IsNullOrEmpty(str))
return -1;

if (str.Length < 1 || str.Length > 2 ||
str[0] < 'A' || str[0] > 'Z' || (str.Length == 2 && (str[1] < 'A' || str[1] > 'Z')))
throw new InvalidOperationException("Waypoint values are only valid between A and ZZ. Invalid value: " + str);
const int charCount = 26;
str = str.ToUpperInvariant();

if (str.Length == 1)
return str[0] - 'A';
int n = 0;
for (int i = str.Length - 1, j = 1; i >= 0; i--, j *= charCount)
{
int c = str[i];

if (c is < 'A' or > 'Z')
throw new InvalidOperationException("Waypoints may only contain characters A through Z, invalid input: " + str);

const int CharCount = 26;
n += (c - '@') * j; // '@' = 'A' - 1
}

int multiplier = (str[0] - 'A' + 1);
return (multiplier * CharCount) + (str[1] - 'A');
return (n - 1);
}

public static string WaypointNumberToAlphabeticalString(int waypointNumber)
{
if (waypointNumber < 0)
return string.Empty;

const int WAYPOINT_MAX = 701;
Span<char> buffer = stackalloc char[8];
const int charCount = 26;

if (waypointNumber > WAYPOINT_MAX)
return "A"; // matches 0

const int CharCount = 26;
if (waypointNumber < 0)
return string.Empty;

int firstLetterValue = (waypointNumber / CharCount);
int secondLetterValue = waypointNumber % CharCount;
waypointNumber++;
int pos = buffer.Length;

if (firstLetterValue == 0)
return ((char)('A' + secondLetterValue)).ToString();
while (waypointNumber > 0)
{
pos--;
int m = waypointNumber % charCount;
if (m == 0) m = charCount;
buffer[pos] = (char)(m + '@'); // '@' = 'A' - 1
waypointNumber = (waypointNumber - m) / charCount;
}

return ((char)('A' + (firstLetterValue - 1))).ToString() + ((char)('A' + secondLetterValue)).ToString();
return buffer.Slice(pos).ToString();
}

private static Point2D[] visualDirectionToPointTable = new Point2D[]
Expand Down

0 comments on commit 4614ee6

Please sign in to comment.