Skip to content

Commit

Permalink
More work on #52
Browse files Browse the repository at this point in the history
- Scatter indirect fire
  • Loading branch information
layagyasz committed Jun 7, 2018
1 parent 712d0e7 commit 647e65c
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override Pod SetupState(ProgramContext ProgramContext, ProgramStateContex
{
var defaultParameters =
new ScenarioParameters(
1939, GameData.MatchSettings.Values.First(), 8, new Coordinate(33, 33), true);
1939, GameData.MatchSettings.Values.First(), 8, new Coordinate(33, 33), false);
_ScenarioBuilder = new ScenarioBuilder(defaultParameters);

var screen = new ScenarioBuilderScreen(ProgramContext.ScreenResolution, _ScenarioBuilder);
Expand Down
38 changes: 34 additions & 4 deletions Model/Orders/Attack/IndirectFireAttackOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public override bool AllowNoFurtherAttacks
}

bool _Targeted;
byte _ScatterRoll = 255;
byte _ScatterDirectionRoll = 255;

public IndirectFireAttackOrder(Army Army, Tile TargetTile)
: base(Army, TargetTile) { }
Expand All @@ -48,38 +50,47 @@ public IndirectFireAttackOrder(SerializationInputStream Stream, List<GameObject>
{
_Attackers = Stream.ReadEnumerable(i => new IndirectFireSingleAttackOrder(Stream, Objects)).ToList();
_Targeted = Stream.ReadBoolean();
_ScatterRoll = Stream.ReadByte();
_ScatterDirectionRoll = Stream.ReadByte();
}

public override void Serialize(SerializationOutputStream Stream)
{
base.Serialize(Stream);
Stream.Write(_Attackers);
Stream.Write(_Targeted);
Stream.Write(_ScatterRoll);
Stream.Write(_ScatterDirectionRoll);
}

public override bool MatchesTurnComponent(TurnComponent TurnComponent)
{
return TurnComponent == TurnComponent.ATTACK;
}

protected override void Recalculate()
void Recalculate(Tile Tile)
{
_OddsCalculations.Clear();
if (_Attackers.Count == 0) return;

var defenders =
TargetTile.Units.Where(i => i.CanBeAttackedBy(Army, AttackMethod) == OrderInvalidReason.NONE).ToList();
Tile.Units.Where(i => i.CanBeAttackedBy(Army, AttackMethod) == OrderInvalidReason.NONE).ToList();
if (defenders.Count == 0) return;
foreach (var defender in defenders)
{
_OddsCalculations.Add(
new OddsCalculation(_Attackers, new Unit[] { defender }, AttackMethod, TargetTile));
new OddsCalculation(_Attackers, new Unit[] { defender }, AttackMethod, Tile));
}
// Sync TreatStackAsArmored
foreach (OddsCalculation odds in _OddsCalculations)
odds.AttackFactorCalculations.ForEach(i => i.Item1.TreatStackAsArmored = odds.StackArmored);
}

protected override void Recalculate()
{
Recalculate(TargetTile);
}

public override Order CloneIfStateful()
{
return new IndirectFireAttackOrder(this);
Expand All @@ -95,7 +106,26 @@ public override OrderStatus Execute(Random Random)
{
Recalculate();

if (_Targeted) return DoExecute(Random);
if (_Targeted)
{
Tile tile = TargetTile;
var v = Validate();
if (v == OrderInvalidReason.ATTACK_NO_SPOTTER)
{
if (_ScatterRoll == 255) _ScatterRoll = (byte)Random.Next(0, 6);

if (_ScatterRoll == 5) tile = null;
else if (_ScatterRoll > 1)
{
if (_ScatterDirectionRoll == 255) _ScatterDirectionRoll = (byte)Random.Next(0, 6);
tile = TargetTile.NeighborTiles[_ScatterDirectionRoll];
}
Recalculate(tile);
return DoExecute(Random);
}
if (v != OrderInvalidReason.NONE) return OrderStatus.ILLEGAL;
return DoExecute(Random);
}

if (Validate() == OrderInvalidReason.NONE)
{
Expand Down
39 changes: 34 additions & 5 deletions Model/Sight/FogOfWarUnitTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public List<Tuple<Unit, UnitVisibility>> Update(SightFinder SightFinder, Unit Un
}
return new List<Tuple<Unit, UnitVisibility>>
{
new Tuple<Unit, UnitVisibility>(Unit, MergeVisibility(Unit, SightFinder.IsSighted(Unit), false))
new Tuple<Unit, UnitVisibility>(Unit, MergeVisibility(Unit, SightFinder.IsSighted(Unit)))
};
}

Expand Down Expand Up @@ -59,9 +59,22 @@ public List<Tuple<Unit, UnitVisibility>> ComputeDelta(
new Tuple<Unit, UnitVisibility>(Unit, new UnitVisibility(true, Movement.Tile))
};
}
bool visible = SightFinder.IsSighted(Unit);
Tile lastSeen = null;
if (Movement.Path != null)
{
for (int i = Movement.Path.Count - 2; i >= 0; --i)
{
if (SightFinder.IsSighted(Unit, Movement.Path[i]))
{
lastSeen = Movement.Path[i + 1];
break;
}
}
}
return new List<Tuple<Unit, UnitVisibility>>
{
new Tuple<Unit, UnitVisibility>(Unit, MergeVisibility(Unit, SightFinder.IsSighted(Unit), false))
new Tuple<Unit, UnitVisibility>(Unit, MergeVisibility(Unit, visible, OverrideLastSeen: lastSeen))
};
}

Expand All @@ -75,19 +88,35 @@ public List<Tuple<Unit, UnitVisibility>> ComputeDelta(
{
deltas.Add(
new Tuple<Unit, UnitVisibility>(
unit, MergeVisibility(unit, SightFinder.IsSighted(unit, tileDelta.Item2), false)));
unit, MergeVisibility(unit, SightFinder.IsSighted(unit, tileDelta.Item2))));
}
if (tileDelta.Item2 == TileSightLevel.HARD_SPOTTED
|| (tileDelta.Item2 != TileSightLevel.NONE
&& !tileDelta.Item1.Rules.Concealing
&& !tileDelta.Item1.Rules.LowProfileConcealing))
{
foreach (var unit in _UnitVisibilities.Where(
i => i.Value.LastSeen == tileDelta.Item1 && i.Key.Position != i.Value.LastSeen).ToList())
{
if (tileDelta.Item2 == TileSightLevel.HARD_SPOTTED
|| !SightFinder.TileConceals(unit.Key, tileDelta.Item1))
{
deltas.Add(
new Tuple<Unit, UnitVisibility>(unit.Key, MergeVisibility(unit.Key, false, true)));
}
}
}
}
return deltas;
}

UnitVisibility MergeVisibility(Unit Unit, bool Visible, bool Remove)
UnitVisibility MergeVisibility(Unit Unit, bool Visible, bool Remove = false, Tile OverrideLastSeen = null)
{
UnitVisibility visibility;
_UnitVisibilities.TryGetValue(Unit, out visibility);

if (Visible) visibility = new UnitVisibility(Visible, Unit.Position);
else visibility = new UnitVisibility(Visible, Remove ? null : visibility.LastSeen);
else visibility = new UnitVisibility(Visible, Remove ? null : (OverrideLastSeen ?? visibility.LastSeen));

_UnitVisibilities[Unit] = visibility;
return visibility;
Expand Down
2 changes: 2 additions & 0 deletions Model/Sight/SightFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public interface SightFinder
bool HasTileSightLevel(Tile Tile, TileSightLevel Level);
UnitVisibility GetUnitVisibility(Unit Unit);

bool TileConceals(Unit Unit, Tile Tile);
bool IsSighted(Unit Unit, TileSightLevel Level);
bool IsSighted(Unit Unit, Tile Tile);
bool IsSighted(Unit Unit);
}
}
1 change: 1 addition & 0 deletions Model/Unit/Unit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void UseAmmunition(bool Secondary)

public OrderInvalidReason CanBeAttackedBy(Army Army, AttackMethod AttackMethod, bool IgnoreConcealment = false)
{
IgnoreConcealment |= AttackMethod == AttackMethod.INDIRECT_FIRE;
if (Position == null) return OrderInvalidReason.ILLEGAL;

if (AttackMethod == AttackMethod.MINEFIELD)
Expand Down
2 changes: 1 addition & 1 deletion Model/Unit/UnitConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public UnitConfiguration(ParseBlock Block)
? 30
: Math.Max(GetAdjustedRange(true), GetAdjustedRange(false)))
: 0));
SightRange = IsEmplaceable() ? (byte)0 : Math.Max((byte)20, GetAdjustedRange(false));
SightRange = IsEmplaceable() ? (byte)0 : Math.Max((byte)20, SpotRange);

DismountAs = (UnitConfiguration)attributes[(int)Attribute.DISMOUNT_AS];
CanRemount = (bool)(attributes[(int)Attribute.CAN_REMOUNT] ?? DismountAs != null);
Expand Down
16 changes: 16 additions & 0 deletions Modules/Default/MatchSettings.blk
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
match-setting<>:match-settings {
match-setting:china-summer {
!var:environment { environments.summer-grassland }
front:front { pacific }
map-generator-configuration:map-generator {
!var:name-generator { name-generators.chinese }
!var:terrain-generator { terrain-generators.default }
}
}
match-setting:france-summer {
!var:environment { environments.summer-grassland }
front:front { west }
Expand Down Expand Up @@ -31,6 +39,14 @@
!var:terrain-generator { terrain-generators.default }
}
}
match-setting:north-africa {
!var:environment { environments.desert }
front:front { west }
map-generator-configuration:map-generator {
!var:name-generator { name-generators.north-african }
!var:terrain-generator { terrain-generators.default }
}
}
match-setting:poland-summer {
!var:environment { environments.summer-steppe }
front:front { east }
Expand Down
2 changes: 2 additions & 0 deletions Modules/Default/NameGenerators.blk
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name-generator<>:name-generators {
name-generator:chinese { Chinese.mkvc }
name-generator:french { French.mkvc }
name-generator:german { German.mkvc }
name-generator:hungarian { Hungarian.mkvc }
name-generator:japanese { Japanese.mkvc }
name-generator:north-african { NorthAfrican.mkvc }
name-generator:polish { Polish.mkvc }
name-generator:russian { Russian.mkvc }
}
Binary file added Modules/Default/NameGenerators/Chinese.mkvc
Binary file not shown.
Binary file added Modules/Default/NameGenerators/NorthAfrican.mkvc
Binary file not shown.
4 changes: 2 additions & 2 deletions Modules/Default/Scenarios/Default/Scenario_00.blk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
byte[]:deployment-order { 1 0 }
byte[]:turn-order { 0 1 }
byte:turns { 3 }
bool:fog-of-war { false }
bool:fog-of-war { true }
!environment:environment { environments.summer-steppe }
map-configuration:map-configuration {
board-configuration[]:_ {
Expand Down Expand Up @@ -79,7 +79,7 @@
int:count { 2 }
}
unit-count:_ {
!unit-configuration:unit-configuration { unit-configurations.truck-british }
!unit-configuration:unit-configuration { unit-configurations.kangaroo }
int:count { 1 }
}
}
Expand Down
6 changes: 6 additions & 0 deletions Modules/Default/Theme/Components/ScenarioBuilder.blk
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class:scenario-builder-parameters-section-text-input {
int:width { 272 }
}
}
class:scenario-builder-parameters-section-checkbox {
!class:parent { checkbox }
mode:default {
int:width { 272 }
}
}
class:scenario-builder-army-section-table {
!class:parent { pane-display }
mode:default {
Expand Down
2 changes: 1 addition & 1 deletion Modules/Default/Theme/Components/TextPane.blk
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class:text-pane-display {
!class:parent { pane-display }
mode:default {
int:height { 114 }
int:height { 158 }
}
}
class:text-pane-input {
Expand Down
4 changes: 0 additions & 4 deletions Modules/Default/UnitConfigurationLinks/Hungarian.blk
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ unit-configuration-link:149mm-how-hungarian {
!faction:faction { factions.hungarian }
!unit-configuration:unit-configuration { unit-configurations.149mm-how }
}
unit-configuration-link:210mm-how-hungarian {
!faction:faction { factions.hungarian }
!unit-configuration:unit-configuration { unit-configurations.210mm-how }
}
unit-configuration-link:81mm-mortar-hungarian {
!faction:faction { factions.hungarian }
!unit-configuration:unit-configuration { unit-configurations.81mm-mortar }
Expand Down
10 changes: 9 additions & 1 deletion PanzerBlitz.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@
<Compile Include="View\MapScreenBase.cs" />
<Compile Include="View\Edit\EditScreen.cs" />
<Compile Include="FileUtils.cs" />
<Compile Include="View\Edit\NewMapPane.cs" />
<Compile Include="View\Edit\NewMapPane.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Include="Model\Map\BoardCompositeMapConfiguration.cs" />
<Compile Include="Model\Map\Coordinate.cs" />
<Compile Include="Model\Map\Direction.cs" />
Expand Down Expand Up @@ -2375,6 +2377,12 @@
<None Include="Modules\Default\NameGenerators\Japanese.mkvc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Modules\Default\NameGenerators\NorthAfrican.mkvc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Modules\Default\NameGenerators\Chinese.mkvc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
5 changes: 3 additions & 2 deletions View/Edit/NewMapPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class NewMapPane : Pane
readonly SingleColumnTable _Display = new SingleColumnTable("new-map-display");
readonly TextInput _HeightInput = new TextInput("new-map-text-input");
readonly TextInput _WidthInput = new TextInput("new-map-text-input");
readonly Checkbox _GenerateRandomCheckbox = new Checkbox("new-map-checkbox") { DisplayedString = "Generate Random" };
readonly Checkbox _GenerateRandomCheckbox =
new Checkbox("new-map-checkbox") { DisplayedString = "Generate Random" };
readonly Button _Error = new Button("new-map-error");
readonly Button _CancelButton = new Button("small-button") { DisplayedString = "Cancel" };
readonly Button _CreateButton = new Button("small-button") { DisplayedString = "Create" };
Expand Down Expand Up @@ -70,7 +71,7 @@ void HandleCreateClicked(object Sender, EventArgs E)
if (_GenerateRandomCheckbox.Value)
configuration =
new RandomMapConfiguration(
width, height, GameData.MatchSettings["japan-summer"].MapGenerator);
width, height, GameData.MatchSettings["china-summer"].MapGenerator);
else configuration = new BlankMapConfiguration(width, height);
if (OnCreate != null) OnCreate(this, new ValuedEventArgs<MapConfiguration>(configuration));

Expand Down
8 changes: 7 additions & 1 deletion View/ScenarioBuilder/ScenarioBuilderScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class ScenarioBuilderScreen : ScreenBase
Select<uint> _YearSelect = new Select<uint>("scenario-builder-parameters-section-select");
Select<MatchSetting> _SettingSelect = new Select<MatchSetting>("scenario-builder-parameters-section-select");
Select<byte> _TurnsSelect = new Select<byte>("scenario-builder-parameters-section-select");
Checkbox _FogOfWarCheckbox =
new Checkbox("scenario-builder-parameters-section-checkbox") { DisplayedString = "Enable" };
TextInput _MapWidthInput = new TextInput("scenario-builder-parameters-section-text-input");
TextInput _MapHeightInput = new TextInput("scenario-builder-parameters-section-text-input");

Expand Down Expand Up @@ -76,6 +78,10 @@ public ScenarioBuilderScreen(Vector2f WindowSize, ScenarioBuilder ScenarioBuilde
});
_TurnsSelect.SetValue(i => i.Value == ScenarioBuilder.Parameters.Turns);

MakeSection("Fog of War", _FogOfWarCheckbox, _LeftDisplay);
_FogOfWarCheckbox.OnChange += HandleParametersChanged;
_FogOfWarCheckbox.Value = ScenarioBuilder.Parameters.FogOfWar;

MakeSection("Map Width", _MapWidthInput, _LeftDisplay);
_MapWidthInput.OnChange += HandleParametersChanged;
_MapWidthInput.Value = ScenarioBuilder.Parameters.MapSize.X.ToString();
Expand Down Expand Up @@ -178,7 +184,7 @@ void HandleParametersChanged(object Sender, EventArgs E)
_SettingSelect.Value.Value,
_TurnsSelect.Value.Value,
new Coordinate(width, height),
true);
_FogOfWarCheckbox.Value);
if (OnParametersChanged != null)
OnParametersChanged(this, new ValuedEventArgs<ScenarioParameters>(parameters));
}
Expand Down

0 comments on commit 647e65c

Please sign in to comment.