Skip to content

Commit d81e67a

Browse files
committed
fix critical bug that prevented variety when generating multiple flags
1 parent 98db151 commit d81e67a

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

FlagGeneration/FlagGeneration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<Compile Include="Scripts\CoatOfArms\Coa_Prefab.cs" />
7676
<Compile Include="Scripts\Helper\DictionaryExtensions.cs" />
7777
<Compile Include="Scripts\Helper\Helper.cs" />
78+
<Compile Include="Scripts\Patterns\MainPatternId.cs" />
7879
<Compile Include="Scripts\Patterns\Pattern_Checkers.cs" />
7980
<Compile Include="Scripts\Patterns\Pattern_Rays.cs" />
8081
<Compile Include="Scripts\Patterns\Pattern_ScatteredStripesAndSymbols.cs" />

FlagGeneration/Scripts/FlagGenerator.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ public class FlagGenerator
1414

1515
static Random R = new Random();
1616

17-
private Dictionary<FlagMainPattern, int> MainPatterns = new Dictionary<FlagMainPattern, int>()
17+
private Dictionary<MainPatternId, int> MainPatterns = new Dictionary<MainPatternId, int>() // New patterns also need to be handled in GetMainPatternFromId()
1818
{
19-
{ new Pattern_Stripes(), 9000 },
20-
{ new Pattern_CoaOnly(), 60 },
21-
{ new Pattern_Diagonal(), 15 },
22-
{ new Pattern_Cross(), 75 },
23-
{ new Pattern_ScatteredStripesAndSymbols(), 50 },
24-
{ new Pattern_Checkers(), 40 },
25-
{ new Pattern_Rays(), 40 }
19+
{ MainPatternId.Stripes, 90 },
20+
{ MainPatternId.CoaOnly, 60 },
21+
{ MainPatternId.Diagonal, 15 },
22+
{ MainPatternId.Cross, 75 },
23+
{ MainPatternId.ScatteredStripesAndSymbols, 50 },
24+
{ MainPatternId.Checkers, 40 },
25+
{ MainPatternId.Rays, 40 }
2626
};
2727

2828
public SvgDocument GenerateFlag()
@@ -33,7 +33,8 @@ public SvgDocument GenerateFlag()
3333
Height = FLAG_HEIGHT
3434
};
3535

36-
FlagMainPattern mainPattern = MainPatterns.GetWeightedRandomElement(R);
36+
MainPatternId mainPatternId = MainPatterns.GetWeightedRandomElement(R);
37+
FlagMainPattern mainPattern = GetMainPatternFromId(mainPatternId);
3738
mainPattern.ApplyPattern(SvgDoc, R);
3839

3940
return SvgDoc;
@@ -44,5 +45,28 @@ public SvgDocument GenerateFlag(int seed)
4445
R = new Random(seed);
4546
return GenerateFlag();
4647
}
48+
49+
private FlagMainPattern GetMainPatternFromId(MainPatternId id)
50+
{
51+
switch(id)
52+
{
53+
case MainPatternId.Checkers:
54+
return new Pattern_Checkers();
55+
case MainPatternId.CoaOnly:
56+
return new Pattern_CoaOnly();
57+
case MainPatternId.Cross:
58+
return new Pattern_Cross();
59+
case MainPatternId.Diagonal:
60+
return new Pattern_Diagonal();
61+
case MainPatternId.Rays:
62+
return new Pattern_Rays();
63+
case MainPatternId.ScatteredStripesAndSymbols:
64+
return new Pattern_ScatteredStripesAndSymbols();
65+
case MainPatternId.Stripes:
66+
return new Pattern_Stripes();
67+
default:
68+
throw new Exception("Unknown MainPatternId: " + id.ToString());
69+
}
70+
}
4771
}
4872
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace FlagGeneration
8+
{
9+
public enum MainPatternId
10+
{
11+
Checkers,
12+
CoaOnly,
13+
Cross,
14+
Diagonal,
15+
Rays,
16+
ScatteredStripesAndSymbols,
17+
Stripes,
18+
}
19+
}

FlagGeneration/Scripts/Patterns/Pattern_Stripes.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public enum StripeDirectionType
3232
}
3333
private Dictionary<StripeDirectionType, int> StripeDirectionProbabilities = new Dictionary<StripeDirectionType, int>()
3434
{
35-
{StripeDirectionType.Horizontal, 6000 },
35+
{StripeDirectionType.Horizontal, 60 },
3636
{StripeDirectionType.Vertical, 40 }
3737
};
3838

@@ -46,7 +46,7 @@ public enum OverlayType
4646
private Dictionary<OverlayType, int> OverlaysProbabilities = new Dictionary<OverlayType, int>()
4747
{
4848
{OverlayType.None, 100 },
49-
{OverlayType.TopLeftRect, 5000 },
49+
{OverlayType.TopLeftRect, 50 },
5050
{OverlayType.LeftTriangle, 50 },
5151
{OverlayType.Antigua, 5 }
5252
};
@@ -114,7 +114,8 @@ public override void DoApply()
114114
}
115115

116116
float[] stripeSizes = new float[NumStripes]; // Stripe size (0-1)
117-
117+
118+
// Check if a wide mid stripe should be applied
118119
if (!EvenStripes && R.NextDouble() < WIDE_MID_STRIPE_CHANCE)
119120
{
120121
HasWideMidStripe = true;
@@ -381,16 +382,19 @@ private bool CanApplyOverlayType(OverlayType type)
381382
return true;
382383

383384
case OverlayType.LeftTriangle:
384-
return StripeDirection == StripeDirectionType.Horizontal;
385+
if (StripeDirection != StripeDirectionType.Horizontal) return false;
386+
return true;
385387

386388
case OverlayType.TopLeftRect:
387-
return !HasWideMidStripe && !(StripeDirection == StripeDirectionType.Vertical && NumStripes == 3);
389+
if (HasWideMidStripe) return false;
390+
if (StripeDirection == StripeDirectionType.Vertical && NumStripes == 3) return false;
391+
return true;
388392

389393
case OverlayType.Antigua:
390394
return true;
391395

392396
default:
393-
throw new Exception("Overlaytype not handled");
397+
throw new Exception("OverlayType not handled");
394398
}
395399
}
396400

0 commit comments

Comments
 (0)