Skip to content

Commit

Permalink
Item & Villager Updates
Browse files Browse the repository at this point in the history
- Refactor Item class to support all flags (item count is a value for certain items only)
- Add other flag editors to bottom bar
- Allow villager house furniture to be edited
  • Loading branch information
Cuyler36 committed Mar 29, 2020
1 parent c8f0fd4 commit e0a2d9b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 24 deletions.
7 changes: 1 addition & 6 deletions MyHorizons.Avalonia/Controls/ItemGrid.cs
Expand Up @@ -162,12 +162,7 @@ private void ShowTip(PointerEventArgs e, bool updateText = false)
var point = e.GetPosition(grid as IVisual);

if (updateText)
{
if (items[currentIdx].Count > 0)
(ItemToolTip.Child as TextBlock).Text = $"{ItemDatabaseLoader.GetNameForItem(items[currentIdx])} - x{items[currentIdx].Count + 1}";
else
(ItemToolTip.Child as TextBlock).Text = ItemDatabaseLoader.GetNameForItem(items[currentIdx]);
}
(ItemToolTip.Child as TextBlock).Text = ItemDatabaseLoader.GetNameForItem(items[currentIdx]);

ItemToolTip.Margin = new Thickness(point.X + 15, point.Y + 10, 0, 0);
if (ItemToolTip.Parent == null)
Expand Down
13 changes: 11 additions & 2 deletions MyHorizons.Avalonia/MainWindow.xaml
Expand Up @@ -136,6 +136,9 @@
<TextBlock Height="26" FontWeight="SemiBold" FontSize="16">Catchphrase:</TextBlock>
<TextBox x:Name="CatchphraseBox" MaxLength="12" MinWidth="130" FontSize="16"/>
</StackPanel>
<StackPanel x:Name="VillagerFurniturePanel" Margin="10, 10, 0, 0" Spacing="5" VerticalAlignment="Top" Orientation="Vertical">
<TextBlock Height="26" FontWeight="SemiBold" FontSize="16">Furniture</TextBlock>
</StackPanel>
</StackPanel>
<StackPanel x:Name="VillagerPanel" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="10, 0, 0, 0" Orientation="Horizontal" Spacing="4"/>
</Grid>
Expand All @@ -152,8 +155,14 @@
<TextBlock x:Name="SaveInfoText" VerticalAlignment="Center" Margin="10,0,0,0" Foreground="White" FontSize="14" FontWeight="Bold">No Save File Loaded</TextBlock>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
<ComboBox x:Name="ItemSelectBox" Width="350" Height="26" Background="White" VirtualizationMode="Simple"/>
<TextBlock Height="26" FontWeight="SemiBold" FontSize="16" Foreground="White" Margin="10,0,0,0">Count:</TextBlock>
<NumericUpDown x:Name="CountBox" Margin="5,0,0,0" Height="34" Minimum="1" Maximum="65536"/>
<TextBlock Height="26" FontWeight="SemiBold" FontSize="16" Foreground="White" Margin="10,0,0,0">Flag0:</TextBlock>
<NumericUpDown x:Name="Flag0Box" Margin="5,0,0,0" Height="34" Minimum="0" Maximum="255"/>
<TextBlock Height="26" FontWeight="SemiBold" FontSize="16" Foreground="White" Margin="10,0,0,0">Flag1:</TextBlock>
<NumericUpDown x:Name="Flag1Box" Margin="5,0,0,0" Height="34" Minimum="0" Maximum="255"/>
<TextBlock Height="26" FontWeight="SemiBold" FontSize="16" Foreground="White" Margin="10,0,0,0">Flag2:</TextBlock>
<NumericUpDown x:Name="Flag2Box" Margin="5,0,0,0" Height="34" Minimum="0" Maximum="255"/>
<TextBlock Height="26" FontWeight="SemiBold" FontSize="16" Foreground="White" Margin="10,0,0,0">Flag3:</TextBlock>
<NumericUpDown x:Name="Flag3Box" Margin="5,0,0,0" Height="34" Minimum="0" Maximum="255"/>
</StackPanel>
<Button Classes="rounded" x:Name="SaveButton" HorizontalAlignment="Right" Width="140" Background="#648589" VerticalAlignment="Center" Foreground="White" FontSize="14" FontWeight="Bold" Height="24">Save Changes</Button>
</Grid>
Expand Down
35 changes: 31 additions & 4 deletions MyHorizons.Avalonia/MainWindow.xaml.cs
Expand Up @@ -2,6 +2,7 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Media.Imaging;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class MainWindow : Window

private ItemGrid playerPocketsGrid;
private ItemGrid playerStorageGrid;
private ItemGrid villagerFurnitureGrid;

public static Item SelectedItem;

Expand Down Expand Up @@ -102,12 +104,18 @@ private void InitializeComponent()

playerPocketsGrid = new ItemGrid(40, 10, 4, 16);
playerStorageGrid = new ItemGrid(5000, 50, 100, 16);
villagerFurnitureGrid = new ItemGrid(16, 8, 2, 16)
{
HorizontalAlignment = HorizontalAlignment.Left
};

var playersGrid = this.FindControl<StackPanel>("PocketsPanel");
playersGrid.Children.Add(playerPocketsGrid);

this.FindControl<ScrollViewer>("StorageScroller").Content = playerStorageGrid;

this.FindControl<StackPanel>("VillagerFurniturePanel").Children.Add(villagerFurnitureGrid);

openBtn.IsVisible = true;
this.FindControl<TabControl>("EditorTabControl").IsVisible = false;
this.FindControl<Grid>("BottomBar").IsVisible = false;
Expand All @@ -125,7 +133,10 @@ public void SetItem(Item item)
{
settingItem = true;
this.FindControl<ComboBox>("ItemSelectBox").SelectedIndex = i;
this.FindControl<NumericUpDown>("CountBox").Value = item.Count + 1;
this.FindControl<NumericUpDown>("Flag0Box").Value = item.Flags0;
this.FindControl<NumericUpDown>("Flag1Box").Value = item.Flags1;
this.FindControl<NumericUpDown>("Flag2Box").Value = item.Flags2;
this.FindControl<NumericUpDown>("Flag3Box").Value = item.Flags3;
settingItem = false;
return;
}
Expand All @@ -141,13 +152,28 @@ private void SetupUniversalConnections()
{
if (!settingItem && selectBox.SelectedIndex > -1)
SelectedItem = new Item(itemDatabase.Keys.ElementAt(selectBox.SelectedIndex),
SelectedItem.Flags0, SelectedItem.Flags1, SelectedItem.Count, SelectedItem.UseCount);
SelectedItem.Flags0, SelectedItem.Flags1, SelectedItem.Flags2, SelectedItem.Flags3, SelectedItem.UseCount);
};

this.FindControl<NumericUpDown>("CountBox").ValueChanged += (o, e) =>
this.FindControl<NumericUpDown>("Flag0Box").ValueChanged += (o, e) =>
{
if (!settingItem)
SelectedItem.Flags0 = (byte)e.NewValue;
};
this.FindControl<NumericUpDown>("Flag1Box").ValueChanged += (o, e) =>
{
if (!settingItem)
SelectedItem.Flags1 = (byte)e.NewValue;
};
this.FindControl<NumericUpDown>("Flag2Box").ValueChanged += (o, e) =>
{
if (!settingItem)
SelectedItem.Flags2 = (byte)e.NewValue;
};
this.FindControl<NumericUpDown>("Flag3Box").ValueChanged += (o, e) =>
{
if (!settingItem)
SelectedItem.Count = (ushort)(e.NewValue - 1);
SelectedItem.Flags3 = (byte)e.NewValue;
};
}

Expand Down Expand Up @@ -282,6 +308,7 @@ private void LoadVillager(Villager villager)
}
this.FindControl<ComboBox>("PersonalityBox").SelectedIndex = villager.Personality;
this.FindControl<TextBox>("CatchphraseBox").Text = villager.Catchphrase;
villagerFurnitureGrid.Items = villager.Furniture;
(villagerPanel.Children[villager.Index] as Button).Background = Brushes.LightGray;
selectedVillager = villager;
}
Expand Down
22 changes: 13 additions & 9 deletions MyHorizons/Data/Item.cs
Expand Up @@ -4,7 +4,7 @@ namespace MyHorizons.Data
{
public sealed class Item
{
public static readonly Item NO_ITEM = new Item(0xFFFE, 0, 0, 0, 0);
public static readonly Item NO_ITEM = new Item(0xFFFE, 0, 0, 0, 0, 0);

private static readonly ushort[] resolvedItemIdArray =
{
Expand All @@ -23,28 +23,31 @@ public enum Type
public ushort ItemId;
public byte Flags0;
public byte Flags1;
public ushort Count;
public byte Flags2;
public byte Flags3;
public ushort UseCount;

public Item(ushort itemId, byte flags0, byte flags1, ushort count, ushort useCount)
public Item(ushort itemId, byte flags0, byte flags1, byte flags2, byte flags3, ushort useCount)
{
ItemId = itemId;
Flags0 = flags0;
Flags1 = flags1;
Count = count;
Flags2 = flags2;
Flags3 = flags3;
UseCount = useCount;
}

public Item(ISaveFile save, int offset)
: this(save.ReadU16(offset + 0), save.ReadU8(offset + 2), save.ReadU8(offset + 3),
save.ReadU16(offset + 4), save.ReadU16(offset + 6)) { }
save.ReadU8(offset + 4), save.ReadU8(offset + 5), save.ReadU16(offset + 6)) { }

public void Save(ISaveFile save, int offset)
{
save.WriteU16(offset, ItemId);
save.WriteU8(offset + 2, Flags0);
save.WriteU8(offset + 3, Flags1);
save.WriteU16(offset + 4, Count);
save.WriteU8(offset + 4, Flags3);
save.WriteU8(offset + 5, Flags2);
save.WriteU16(offset + 6, UseCount);
}

Expand All @@ -65,7 +68,7 @@ public ushort GetInventoryNameFromFlags()
return ItemId;
}

public Item Clone() => new Item(ItemId, Flags0, Flags1, Count, UseCount);
public Item Clone() => new Item(ItemId, Flags0, Flags1, Flags2, Flags3, UseCount);

public override bool Equals(object obj)
{
Expand All @@ -80,14 +83,15 @@ public override bool Equals(object obj)
}

if (obj is Item other)
return ItemId == other.ItemId && Flags0 == other.Flags0 && Flags1 == other.Flags1 && Count == other.Count && UseCount == other.UseCount;
return ItemId == other.ItemId && Flags0 == other.Flags0 && Flags1 == other.Flags1
&& Flags3 == other.Flags3 && Flags2 == other.Flags2 && UseCount == other.UseCount;
return false;
}

public static bool operator ==(Item a, Item b) => a?.Equals(b) ?? false;

public static bool operator !=(Item a, Item b) => !(a == b);

public override int GetHashCode() => ((base.GetHashCode() << 2) ^ ItemId) << ((Flags0 + Flags1) & 7) ^ (Count << UseCount & 0x1F);
public override int GetHashCode() => ((base.GetHashCode() << 2) ^ ItemId) << ((Flags0 + Flags1 + Flags3) & 7) ^ (Flags2 << UseCount & 0x1F);
}
}
14 changes: 11 additions & 3 deletions MyHorizons/Data/Villager.cs
Expand Up @@ -13,6 +13,7 @@ public sealed class Villager
public byte VariantIdx;
public byte Personality;
public string Catchphrase;
public ItemCollection Furniture;

private readonly struct Offsets
{
Expand All @@ -23,22 +24,24 @@ public sealed class Villager
public readonly int Variant;
public readonly int Personality;
public readonly int Catchphrase;
public readonly int Furniture;

public Offsets(int baseOffset, int size, int species, int variant, int personality, int catchphrase)
public Offsets(int baseOffset, int size, int species, int variant, int personality, int catchphrase, int furniture)
{
BaseOffset = baseOffset;
Size = size;
Species = species;
Variant = variant;
Personality = personality;
Catchphrase = catchphrase;
Furniture = furniture;
}
}

private static readonly Offsets[] VillagerOffsetsByRevision =
{
new Offsets(0x110, 0x12AB0, 0, 1, 2, 0x10014),
new Offsets(0x120, 0x12AB0, 0, 1, 2, 0x10014)
new Offsets(0x110, 0x12AB0, 0, 1, 2, 0x10014, 0x105EC),
new Offsets(0x120, 0x12AB0, 0, 1, 2, 0x10014, 0x105EC)
};

private static Offsets GetOffsetsFromRevision() => VillagerOffsetsByRevision[MainSaveFile.Singleton().GetRevision()];
Expand All @@ -54,6 +57,11 @@ public Villager(int idx)
VariantIdx = save.ReadU8(Offset + offsets.Variant);
Personality = save.ReadU8(Offset + offsets.Personality);
Catchphrase = save.ReadString(Offset + offsets.Catchphrase, 12); // Not sure about the size.

var ftr = new Item[16];
for (var i = 0; i < 16; i++)
ftr[i] = new Item(save, Offset + offsets.Furniture + i * 0x2C);
Furniture = new ItemCollection(ftr);
}

public void Save()
Expand Down

0 comments on commit e0a2d9b

Please sign in to comment.