Skip to content

Commit

Permalink
Added missing interference artifacts; Added button to generate random…
Browse files Browse the repository at this point in the history
… guards to help map editing.
  • Loading branch information
zomle committed May 10, 2020
1 parent b00500f commit 9f0d5a2
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Homm3.WPF/Homm3.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Authors>zomle</Authors>
<PackageId>Homm3.Calculator</PackageId>
<AssemblyName>Homm3.Calculator</AssemblyName>
<Version>1.0.2</Version>
<Version>1.0.4</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 13 additions & 1 deletion Homm3.WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,20 @@
<Label Grid.Row="5" Grid.Column="3" Content="Effective AI Value"/>
<Label Grid.Row="5" Grid.Column="4" Content="" x:Name="lblMapObject6AiValue"/>
</Grid>

<Grid Grid.Row="4" Grid.Column="0" Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="17"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Guard"/>
<Button Grid.Column="1" Margin="-3,3,3,3" x:Name="btnRandomGuard" Content="?" >
<Button.ToolTip>
<Label Content="Generate possible guard for the selected objects, from the factions that has a value bigger than 0 set on the right side (plus neutral)."/>
</Button.ToolTip>
</Button>
</Grid>

<Label Grid.Row="4" Grid.Column="0" Content="Guard"/>
<Grid Grid.Row="4" Grid.Column="1" Margin="0" Width="227">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
Expand Down
35 changes: 30 additions & 5 deletions Homm3.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public MainWindow()
btnClearMapObject5.Click += BtnClearMapObject5_Click;
btnClearMapObject6.Click += BtnClearMapObject6_Click;
btnClearGuard.Click += BtnClearGuard_Click;
btnRandomGuard.Click += BtnRandomGuard_Click;

cmbMonsterStrengthZone.SelectionChanged += CmbSelectionChanged;
cmbMonsterStrengthMap.SelectionChanged += CmbSelectionChanged;
Expand Down Expand Up @@ -166,6 +167,23 @@ private void ClearFilteredComboBox(FilteredComboBox filteredComboBox)
filteredComboBox.SelectedIndex = -1;
RefreshUi();
}

private void BtnRandomGuard_Click(object sender, RoutedEventArgs e)
{
var userInput = GetUserInput();
var result = Calculator.GenerateRandomGuard(userInput);

if (result == null)
{
lblResult.DataContext = $"Couldn't find any possible random guard for the selected criteria.";
}
else
{
lblResult.DataContext = $"Possible random guard for the selection: <b>{result.AverageMonsterCount}</b> <b>{result.Monster.DisplayName}</b> on week 1.";
}

}

private void BtnClearGuard_Click(object sender, RoutedEventArgs e)
{
ClearFilteredComboBox(cmbMonster);
Expand Down Expand Up @@ -295,7 +313,7 @@ private void NumberValidationTextBox_Pasting(object sender, DataObjectPastingEve
}
}

private void RefreshUi()
private UserInput GetUserInput()
{
var userInput = new UserInput();
userInput.SelectedMonster = cmbMonster.SelectedItem as Monster;
Expand Down Expand Up @@ -348,9 +366,16 @@ private void RefreshUi()
int.TryParse(txtZoneCoveCount.Text, out tmpInt);
userInput.TownZoneCounts[Town.Cove] = tmpInt;

return userInput;
}

private void RefreshUi()
{
var userInput = GetUserInput();

grdZoneGuard.IsEnabled = userInput.IsZoneGuard;
grdMapObjects.IsEnabled = !userInput.IsZoneGuard;
userInput.HasDwellingMapObject();
//userInput.HasDwellingMapObject();
string monsterAiValue = string.Empty;
if (userInput.HasUnknownMapObject())
{
Expand Down Expand Up @@ -393,7 +418,7 @@ private void RefreshUi()
return;
}

var resultMessage = string.Empty;
string resultMessage;
if (userInput.HasUnknownMapObject())
{
lblTotalAiValue.Content = "";
Expand Down Expand Up @@ -537,12 +562,12 @@ private List<Preset> LoadPresets(bool silent = false)

private static List<Monster> LoadMonsters()
{
return MonsterFactory.CreateMonsters();
return MonsterFactory.ListMonsters();
}

private static List<MapObject> LoadMapObjects()
{
return MapObjectFactory.CreateMapObjects();
return MapObjectFactory.ListMapObjects();
}
}
}
39 changes: 36 additions & 3 deletions Homm3.WPF/Model/Calculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public CalculatedValues()

public class CalculatedMonsterValues
{
public Monster Monster { get; internal set; }
public int AverageMonsterCount { get; set; }
public int MonsterCountDeviation { get; set; }
public int MinimalCount { get { return AverageMonsterCount - MonsterCountDeviation; } }
Expand Down Expand Up @@ -101,11 +102,42 @@ public static CalculatedValues CalculateValues(UserInput userInput, MapObject ad

if (userInput.SelectedMonster != null)
{
result.MonsterValues = CalculatedMonsterValues(result.AiValues.TotalAiValue, userInput.SelectedMonster.AiValue);
result.MonsterValues = CalculatedMonsterValues(result.AiValues.TotalAiValue, userInput.SelectedMonster);
}
return result;
}


public static CalculatedMonsterValues GenerateRandomGuard(UserInput userInput)
{
var monsters = MonsterFactory.ListMonsters();
var rnd = new Random((int)DateTime.Now.Ticks);

var towns = userInput.TownZoneCounts.Where(tzc => tzc.Value > 0).Select(tzc => tzc.Key).ToList();
towns.Add(Town.Neutral);

int monsterStrengthMap = userInput.SelectedMonsterStrengthMap?.Value ?? 0;
int monsterStrengthZone = userInput.SelectedMonsterStrengthZone?.Value ?? 0;
var protectionIndex = monsterStrengthMap + monsterStrengthZone;

var aiValues = CalculateAiValues(userInput, protectionIndex);
CalculatedMonsterValues monsterValues = new CalculatedMonsterValues();

Monster monster;
while (monsterValues.Monster == null || !towns.Contains(monsterValues.Monster.Town) || monsterValues.AverageMonsterCount > 100 || monsterValues.AverageMonsterCount == 0)
{
var ix = rnd.Next(0, monsters.Count - 1);
monster = monsters[ix];
monsterValues = CalculatedMonsterValues(aiValues.TotalAiValue, monster);
monsters.RemoveAt(ix);
if (monsters.Count == 0)
{
return null;
}
}
return monsterValues;
}

private static CalculatedAiValues CalculateAiValues(UserInput userInput, int protectionIndex, int additionalAiValue = 0)
{
var result = new CalculatedAiValues();
Expand Down Expand Up @@ -192,12 +224,13 @@ private static CalculatedAiValues CalculateAiValues(UserInput userInput, int pro
return result;
}

private static CalculatedMonsterValues CalculatedMonsterValues(int totalAiValue, int monsterAiValue)
private static CalculatedMonsterValues CalculatedMonsterValues(int totalAiValue, Monster monster)
{
var result = new CalculatedMonsterValues();

result.AverageMonsterCount = (int)Math.Round((double)totalAiValue / monsterAiValue);
result.AverageMonsterCount = (int)Math.Round((double)totalAiValue / monster.AiValue);
result.MonsterCountDeviation = result.AverageMonsterCount >= 4 ? result.AverageMonsterCount / 4 : 0;
result.Monster = monster;

return result;
}
Expand Down
4 changes: 3 additions & 1 deletion Homm3.WPF/Model/Factories/MapObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static MapObjectFactory()
mapObjects.Sort();
}

public static List<MapObject> CreateMapObjects()
public static List<MapObject> ListMapObjects()
{
return mapObjects.ToList();
}
Expand Down Expand Up @@ -136,6 +136,7 @@ private static IEnumerable<MapObject> ListAdventureMapObjects()
yield return new MapObject("Pandora's Box (tier 7 monster)", 27000, "Pandora's Box monster 7");
yield return new MapObject("Pillar of Fire", 750);
yield return new MapObject("Pirate Cavern", 3500);
yield return new MapObject("Plate of Dying Light", 20000);
yield return new GuessableMapObject("Prison (lvl 1 hero)", 2500, "Prison 01", "", "lvl 1");
yield return new GuessableMapObject("Prison (lvl 5 hero)", 5000, "Prison 05", "", "lvl 5");
yield return new GuessableMapObject("Prison (lvl 10 hero)", 10000, "Prison 10", "", "lvl 10");
Expand All @@ -158,6 +159,7 @@ private static IEnumerable<MapObject> ListAdventureMapObjects()
yield return new MapObject("Sea Barrel", 500);
yield return new MapObject("Sea Chest", 1500);
yield return new MapObject("Seafaring Academy", 8000);
yield return new MapObject("Seal of Sunset", 5000);
yield return new GuessableMapObject("Seer's Hut (5k exp)", 2000, "Seer's Hut exp 05", "exp", "5k");
yield return new GuessableMapObject("Seer's Hut (10k exp)", 5333, "Seer's Hut exp 10", "exp", "10k");
yield return new GuessableMapObject("Seer's Hut (15k exp)", 8666, "Seer's Hut exp 15", "exp", "15k");
Expand Down
8 changes: 4 additions & 4 deletions Homm3.WPF/Model/Factories/MonsterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ static MonsterFactory()
{
monsters = new Dictionary<string, Monster>();

foreach (var monster in ListMonsters())
foreach (var monster in CreateMonsters())
{
monsters.Add(monster.Name, monster);
}
}

public static List<Monster> CreateMonsters()
public static List<Monster> ListMonsters()
{
return monsters.Values.ToList();
}
Expand All @@ -28,7 +28,7 @@ public static Monster GetMonster(string name)
return monsters[name];
}

private static IEnumerable<Monster> ListMonsters()
private static IEnumerable<Monster> CreateMonsters()
{
yield return new Monster(Town.Conflux, "Air Elemental", 356, 6);
yield return new Monster(Town.Stronghold, "Ancient Behemoth", 6168, 1, 1);
Expand All @@ -51,7 +51,7 @@ private static IEnumerable<Monster> ListMonsters()
yield return new Monster(Town.Rampart, "Centaur Captain", 138, 14, 1);
yield return new Monster(Town.Rampart, "Centaur", 100, 14);
yield return new Monster(Town.Inferno, "Cerberus", 392, 5, 1);
yield return new Monster(Town.Castle, "Champion", 2100, 2);
yield return new Monster(Town.Castle, "Champion", 2100, 2, 1);
yield return new Monster(Town.Fortress, "Chaos Hydra", 5931, 1, 1);
yield return new Monster(Town.Cove, "Corsair", 407, 7, 1);
yield return new Monster(Town.Cove, "Crew Mate", 155, 9);
Expand Down

0 comments on commit 9f0d5a2

Please sign in to comment.