Author Topic: Item Sorting II (Final Fantasy VII)  (Read 6079 times)

P3k

  • *
  • Posts: 10
    • View Profile
Item Sorting II (Final Fantasy VII)
« on: 2021-03-03 02:50:03 »
Hey,
I didn't want to necro this post: https://forums.qhimm.com/index.php?topic=16079.msg226836#msg226836 so thought I'd make a new one.

I hope anyone can help me understand specifically what NFITC1 means about the bubble sort and field / battle condition.

If I do the bubble sort based on the field condition, I do not get the same results as the game.

Can someone please explain how NFITC1's answer would function? Do I check the condition once then BS from there or am i missing something?

Here's my code;

Code: [Select]
for (var i = player.inventory.items.Length; i > 0; i--)
{
  for (var j = player.inventory.items.Length - 1; j >= 0; j--)
  {
    if (j != 0)
    {
      var i1 = player.inventory.items[j];
      var i2 = player.inventory.items[j - 1];

      var swapID = ShouldSwap(i1, i2);

      if (swapID == 1)
      {
        player.inventory.items[j] = i2;
        player.inventory.items[j - 1] = i1;
      }
    }
  }
}

private static int ShouldSwap(
  Player.PlayerInventory.InventoryItem item1,
  Player.PlayerInventory.InventoryItem item2)
{
  if (item1.sortTag.Contains(SortingTag.Field))
  {
    return item2.sortTag.Contains(SortingTag.Field) ?0 : 1;
  }
  else
  {
    return item2.sortTag.Contains(SortingTag.Field) ?-1 : 0;
  }
}

Cheers
« Last Edit: 2021-03-03 02:54:20 by P3k »

P3k

  • *
  • Posts: 10
    • View Profile
Re: Item Sorting II (Final Fantasy VII)
« Reply #1 on: 2021-03-03 23:33:32 »
I figured it out.

Quote
NFITC1: Item1 starts at 319 and item2 starts at 318

The above is incorrect. item1 always starts from 319 while item2 is the next inventory index to search.
E.G;
item1 = 319 to 0, item2 = 0
item1 = 319 to 0, item2 = 1

Once item1's inventory index is < than item2's, end the sorting function for that index.

Code: [Select]
var length = player.inventory.items.Length - 1;

/// Start at first inventory index (item2)
for (var i = 0; i <= length; i++)
{
 
  /// Cycle through inventory from end to start (item1), searching for an item that can be swapped with item2.
  for (var j = length ; j >= 0; j--)
  {
      var i1 = player.inventory.items[j];
      var i2 = player.inventory.items[i];

      var swapID = ShouldSwap(i1, i2);
     
      /// As long as the search index is greater than the index of that which is being replaced,
      /// run condition to see if a swap is required
      if(j > i)
      { 
         if (swapID == 1)
         {
   player.inventory.items[i] = i1;
     player.inventory.items[j] = i2;
         }
      }
      /// If the search index becomes less than the index of which is being replaced, end the sorting of this index
      else
      { break; }
  }
}

So essentially, starting from the top index, search for an item to replace it with by searching all inventory index's starting from the bottom. If the search index gets < than that of what it is trying to replace, end the sort for that index, then start again from the next top index.
 :P ;D :'(
« Last Edit: 2021-03-04 00:06:47 by P3k »

P3k

  • *
  • Posts: 10
    • View Profile
Re: Item Sorting II (Final Fantasy VII)
« Reply #2 on: 2021-03-16 00:56:55 »
I was able to decompile FF7 and find the function that arranges the Inventory.
It took me a few days (as I'm a newb at assembly) but I got there in the end and managed to perfectly replicate the sorting methods.

If anyone is interested, you can find my script here: https://github.com/p3k22/FF7-Csharp/blob/main/FF7%20Arrange%20Inventory.cs
« Last Edit: 2021-03-16 19:44:53 by P3k »

kerihobo

  • *
  • Posts: 7
    • View Profile
Re: Item Sorting II (Final Fantasy VII)
« Reply #3 on: 2024-03-01 14:45:50 »
Fascinating. Thank you for sharing this. I am trying to replicate it myself but there are some really strange things when I sort in the actual game.

Firstly, I noticed "Field" and "Battle" don't also organize by Type, and I always assumed they did. They actually just grab all the items that match criteria and pull them to the front, in the same order they were in before the sort. You can observe this when you first sort by Type, then Field, then Name, then Field again. You'll see the results of Field were different both times. Cos they just grab the items that qualify, but leave those qualifying items in the order they were already in.

Wondering if their sorting is actually broken though, because if you sort by Name, "All Creation" ends up in the 'A' section, but if you sort by Field, I expect knowing that it just grabs all qualifying items in current order and pulls them to the front, that the result would be alphabetized, and it is, except "All Creation" ends up in the 'P' section instead of 'A'. FF7 truly is full of mysteries.