Skip to content

Commit

Permalink
scale prices
Browse files Browse the repository at this point in the history
  • Loading branch information
Entroper committed Aug 28, 2016
1 parent a3d5383 commit 47ea660
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 29 deletions.
28 changes: 26 additions & 2 deletions FF1Randomizer/FF1Rom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public class FF1Rom : NesRom
public const int ArmorSize = 4;
public const int ArmorCount = 40;

public const int PriceOffset = 0x37C00;
public const int PriceSize = 2;
public const int PriceCount = 240;

public FF1Rom(string filename) : base(filename)
{}

Expand All @@ -66,10 +70,15 @@ public override bool Validate()
public void WriteSeedAndFlags(string seed, string flags)
{
var seedBytes = FF1Text.TextToBytes($"SEED {seed}");
var flagBytes = FF1Text.TextToBytes($" {flags}");
var flagBytes = FF1Text.TextToBytes($"{flags}");
var padding = new byte[15 - flagBytes.Length];
for (int i = 0; i < padding.Length; i++)
{
padding[i] = 0xFF;
}

Put(CopyrightOffset1, seedBytes);
Put(CopyrightOffset2, flagBytes);
Put(CopyrightOffset2, flagBytes + padding);
}

public void ShuffleTreasures(MT19337 rng)
Expand Down Expand Up @@ -411,6 +420,21 @@ public void ShuffleMagicLevels(MT19337 rng, bool keepPermissions)
}
}

public void ScalePrices(double scale, MT19337 rng)
{
var prices = Get(PriceOffset, PriceSize*PriceCount).Chunk(PriceSize);
foreach (var price in prices)
{
var priceValue = BitConverter.ToUInt16(price, 0);
priceValue = (ushort)Min(Scale(priceValue, scale, 1, rng), 0xFFFF);

var priceBytes = BitConverter.GetBytes(priceValue);
Array.Copy(priceBytes, 0, price, 0, 2);
}

Put(PriceOffset, prices.SelectMany(price => price.ToBytes()).ToArray());
}

public void ScaleEnemyStats(double scale, MT19337 rng)
{
var enemies = Get(EnemyOffset, EnemySize*EnemyCount).Chunk(EnemySize);
Expand Down
3 changes: 0 additions & 3 deletions FF1Randomizer/FF1Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ static FF1Text()
BytesByText[sides[1]] = b;
TextByBytes[b] = sides[1];
}

BytesByText["\0"] = 0xFF;
TextByBytes[0xFF] = "\0";
}

public static string BytesToText(byte[] bytes)
Expand Down
10 changes: 6 additions & 4 deletions FF1Randomizer/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FF1Randomizer"
mc:Ignorable="d"
Title="FF1Randomizer 0.5.6" Height="397.727" Width="553.836">
Title="FF1Randomizer 0.6.0" Height="397.727" Width="553.836">
<Grid Margin="0,0,0,0">
<TextBox x:Name="RomTextBox" HorizontalAlignment="Left" Height="23" Margin="10,36,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" IsEnabled="False"/>
<Label x:Name="RomLabel" Content="Original ROM" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.228,0.141" FontWeight="Bold"/>
Expand All @@ -27,9 +27,11 @@
<CheckBox x:Name="EnemyStatusAttacksCheckBox" IsEnabled="False" Content="Enemy Status Attacks" HorizontalAlignment="Left" Margin="10,177,0,0" VerticalAlignment="Top" ToolTip="Status attacks such as stun, sleep, and death are shuffled." Checked="SetFlagsText" Unchecked="SetFlagsText"/>

<Label x:Name="ScaleLabel" Content="Scale:" HorizontalAlignment="Left" Margin="237,10,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<Label x:Name="ScaleFactorLabel" Content="" HorizontalAlignment="Left" Margin="282,10,0,0" VerticalAlignment="Top"/>
<Slider x:Name="ScaleFactorSlider" HorizontalAlignment="Left" Margin="410,18,0,0" VerticalAlignment="Top" Minimum="1.5" Maximum="5" Value="2" Width="100" TickPlacement="BottomRight" TickFrequency="0.5" ValueChanged="ScaleFactorSlider_ValueChanged" ToolTip="For each value to be scaled, a random exponent is selected between -1 and +1. The value is scaled by the scale factor raised to that power."/>
<CheckBox x:Name="PricesCheckBox" IsEnabled="False" Content="Prices" HorizontalAlignment="Left" Margin="237,36,0,0" VerticalAlignment="Top" ToolTip="Prices of items, weapons, armor, and magic are all scaled." Checked="SetFlagsText" Unchecked="SetFlagsText"/>
<Label x:Name="PriceScaleFactorLabel" Content="" HorizontalAlignment="Left" Margin="329,25,0,0" VerticalAlignment="Top"/>
<Label x:Name="EnemyScaleFactorLabel" Content="" HorizontalAlignment="Left" Margin="329,54,0,0" VerticalAlignment="Top"/>
<Slider x:Name="PriceScaleFactorSlider" HorizontalAlignment="Left" Margin="410,27,0,0" VerticalAlignment="Top" Minimum="1.5" Maximum="5" Value="2" Width="100" TickPlacement="BottomRight" TickFrequency="0.5" ValueChanged="PriceScaleFactorSlider_ValueChanged" ToolTip="For each value to be scaled, a random exponent is selected between -1 and +1. The value is scaled by the scale factor raised to that power."/>
<Slider x:Name="EnemyScaleFactorSlider" HorizontalAlignment="Left" Margin="410,56,0,0" VerticalAlignment="Top" Minimum="1.5" Maximum="5" Value="2" Width="100" TickPlacement="BottomRight" TickFrequency="0.5" ValueChanged="EnemyScaleFactorSlider_ValueChanged" ToolTip="For each value to be scaled, a random exponent is selected between -1 and +1. The value is scaled by the scale factor raised to that power."/>
<CheckBox x:Name="PricesCheckBox" IsEnabled="True" IsChecked="True" Content="Prices" HorizontalAlignment="Left" Margin="237,36,0,0" VerticalAlignment="Top" ToolTip="Prices of items, weapons, armor, and magic are all scaled." Checked="SetFlagsText" Unchecked="SetFlagsText"/>
<CheckBox x:Name="EnemyStatsCheckBox" IsEnabled="True" IsChecked="True" Content="Enemy Stats" HorizontalAlignment="Left" Margin="237,56,0,0" VerticalAlignment="Top" ToolTip="Enemy stats are scaled: HP, attack power, # of hits, accuracy, critical %, defense, evade, and magic defense." Checked="SetFlagsText" Unchecked="SetFlagsText"/>
<CheckBox x:Name="ExpGoldBoostCheckBox" Content="Exp/Gold Boost" HorizontalAlignment="Left" Margin="237,136,0,0" VerticalAlignment="Top" IsChecked="True" Checked="SetFlagsText" Unchecked="SetFlagsText"/>

Expand Down
51 changes: 31 additions & 20 deletions FF1Randomizer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public MainWindow()

GenerateSeed();

SetScaleFactorLabel();
SetScaleFactorLabel(PriceScaleFactorSlider, PriceScaleFactorLabel);
SetScaleFactorLabel(EnemyScaleFactorSlider, EnemyScaleFactorLabel);
SetExpLabel();
SetFlagsText(null, null);
}
Expand Down Expand Up @@ -112,9 +113,14 @@ private void GenerateButton_Click(object sender, RoutedEventArgs e)
rom.ShuffleMagicLevels(rng, MagicPermissionsCheckBox.IsChecked ?? false);
}

if (PricesCheckBox.IsChecked == true)
{
rom.ScalePrices(PriceScaleFactorSlider.Value, rng);
}

if (EnemyStatsCheckBox.IsChecked == true)
{
rom.ScaleEnemyStats(ScaleFactorSlider.Value, rng);
rom.ScaleEnemyStats(EnemyScaleFactorSlider.Value, rng);
}

if (ExpGoldBoostCheckBox.IsChecked == true)
Expand Down Expand Up @@ -152,20 +158,28 @@ private void MagicLevelsCheckBox_OnUnchecked(object sender, RoutedEventArgs e)
SetFlagsText(sender, e);
}

private void ScaleFactorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
private void PriceScaleFactorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
PriceScaleFactorSlider.Value = Math.Round(PriceScaleFactorSlider.Value, 1);

SetScaleFactorLabel(PriceScaleFactorSlider, PriceScaleFactorLabel);
SetFlagsText(sender, e);
}

private void EnemyScaleFactorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
ScaleFactorSlider.Value = Math.Round(ScaleFactorSlider.Value, 1);
EnemyScaleFactorSlider.Value = Math.Round(EnemyScaleFactorSlider.Value, 1);

SetScaleFactorLabel();
SetScaleFactorLabel(EnemyScaleFactorSlider, EnemyScaleFactorLabel);
SetFlagsText(sender, e);
}

private void SetScaleFactorLabel()
private void SetScaleFactorLabel(Slider slider, Label label)
{
var lower = Math.Round(100 / ScaleFactorSlider.Value);
var upper = Math.Round(100 * ScaleFactorSlider.Value);
var lower = Math.Round(100 / slider.Value);
var upper = Math.Round(100 * slider.Value);

ScaleFactorLabel.Content = $"{lower}% - {upper}%";
label.Content = $"{lower}% - {upper}%";
}

private void ExpMultiplierSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
Expand Down Expand Up @@ -207,18 +221,15 @@ private void SetFlagsText(object sender, RoutedEventArgs e)
FlagsTextBox.Text += MagicLevelsCheckBox.IsChecked == true ? "L" : "l";
FlagsTextBox.Text += MagicPermissionsCheckBox.IsChecked == true ? "P" : "p";

if (EnemyStatsCheckBox.IsChecked == true || PricesCheckBox.IsChecked == true)
if (PricesCheckBox.IsChecked == true)
{
if (EnemyStatsCheckBox.IsChecked == true)
{
FlagsTextBox.Text += "S";
}
if (PricesCheckBox.IsChecked == true)
{
FlagsTextBox.Text += "P";
}

FlagsTextBox.Text += SliderToBase64((int)(10*ScaleFactorSlider.Value));
FlagsTextBox.Text += "P";
FlagsTextBox.Text += SliderToBase64((int)(10 * PriceScaleFactorSlider.Value));
}
if (EnemyStatsCheckBox.IsChecked == true)
{
FlagsTextBox.Text += "S";
FlagsTextBox.Text += SliderToBase64((int)(10 * EnemyScaleFactorSlider.Value));
}

if (ExpGoldBoostCheckBox.IsChecked == true)
Expand Down

0 comments on commit 47ea660

Please sign in to comment.