Skip to content

Commit

Permalink
Stayed home this weekend
Browse files Browse the repository at this point in the history
- Start Fog Of War #52
- Finish indirect fire
- Implement an Event Relay
- Improve LOS algorithm
- Japanese environment
  • Loading branch information
layagyasz committed Jun 5, 2018
1 parent 83ae7d8 commit 660f6f7
Show file tree
Hide file tree
Showing 66 changed files with 766 additions and 280 deletions.
2 changes: 1 addition & 1 deletion Controller/ArmyBuilder/ArmyBuilderStateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void HandleFinished(object Sender, EventArgs E)
if (builder.Armies.All(i => i.Validate()))
transition = new ProgramStateTransitionEventArgs(
ProgramState.MATCH,
new MatchContext(new Match(builder.BuildScenario(), FullOrderAutomater.PROVIDER)));
new MatchContext(new Match(builder.BuildScenario(), new FullOrderAutomater())));
else transition = new ProgramStateTransitionEventArgs(ProgramState.BUILD_ARMY, _Context);
OnProgramStateTransition(this, transition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void HandleStartScenario(object Sender, ValuedEventArgs<Scenario> E)
{
OnProgramStateTransition(
this, new ProgramStateTransitionEventArgs(
ProgramState.MATCH, new MatchContext(new Match(E.Value, FullOrderAutomater.PROVIDER))));
ProgramState.MATCH, new MatchContext(new Match(E.Value, new FullOrderAutomater()))));
}
}
}
2 changes: 1 addition & 1 deletion Controller/Match/HumanMatchPlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public HumanMatchPlayerController(

_MatchScreen = MatchScreen;
_MatchScreen.OnFinishClicked += EndTurn;
_MatchScreen.OnUnitAdded += _NewUnitBuffer.Hook(AddUnit).Invoke;
_MatchScreen.OnUnitAdded += _NewUnitBuffer.Hook<ValuedEventArgs<UnitView>>(AddUnit).Invoke;
_MatchScreen.OnPulse += (sender, e) => _NewUnitBuffer.DispatchEvents();

_Controllers = new Dictionary<TurnComponent, Subcontroller>
Expand Down
2 changes: 1 addition & 1 deletion Controller/Match/MatchStateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override Pod SetupState(ProgramContext ProgramContext, ProgramStateContex
}
_MatchController = new MatchController(_Context.Match, playerControllers);
screen.OnPulse += HandlePulse;
_Context.Match.OnMatchEnded += _MatchEndBuffer.Hook(HandleMatchEnd).Invoke;
_Context.Match.OnMatchEnded += _MatchEndBuffer.Hook<EventArgs>(HandleMatchEnd).Invoke;
_Context.Match.Start();

return screen;
Expand Down
4 changes: 3 additions & 1 deletion Controller/Match/Subcontroller/BaseAttackController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public override void HandleKeyPress(Keyboard.Key Key) { }
protected void AddAttack(Tile Tile, SingleAttackOrder NewAttack)
{
AttackOrder attack = null;
if (_AttackBuilder == null || _AttackBuilder.TargetTile != Tile)
if (_AttackBuilder == null
|| _AttackBuilder.TargetTile != Tile
|| !_AttackBuilder.IsCompatible(NewAttack))
attack = NewAttack.GenerateNewAttackOrder();
else attack = _AttackBuilder;
var r = attack.AddAttacker(NewAttack);
Expand Down
2 changes: 1 addition & 1 deletion Controller/MatchLobby/MatchLobbyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ChatAdapter MakeChatAdapter()

public MatchContext MakeMatchContext()
{
var match = new Match(Lobby.Scenario, IsHost ? FullOrderAutomater.PROVIDER : i => null);
var match = new Match(Lobby.Scenario, IsHost ? new FullOrderAutomater() : null);
var serializer = new OrderSerializer(match);

if (IsHost)
Expand Down
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, Front.EAST, GameData.MatchSettings.Values.First(), 8, new Coordinate(33, 33));
1939, GameData.MatchSettings.Values.First(), 8, new Coordinate(33, 33), true);
_ScenarioBuilder = new ScenarioBuilder(defaultParameters);

var screen = new ScenarioBuilderScreen(ProgramContext.ScreenResolution, _ScenarioBuilder);
Expand Down
4 changes: 2 additions & 2 deletions EventBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public void QueueEvent(Action<object, T> Handler, object Sender, T E)
}
}

public Action<object, T> Hook(Action<object, T> Handler)
public Action<object, T> Hook<K>(Action<object, K> Handler) where K : T
{
return (Sender, E) => QueueEvent(Handler, Sender, E);
return (Sender, E) => QueueEvent((s, e) => Handler(s, (K)e), Sender, E);
}

public void DispatchEvents()
Expand Down
92 changes: 16 additions & 76 deletions Model/Army.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Army : GameObject
public EventHandler<NewUnitEventArgs> OnUnitAdded;

public readonly Match Match;
public readonly SightFinder SightFinder;
public readonly ArmyConfiguration Configuration;
public readonly List<Deployment> Deployments;

Expand All @@ -19,7 +20,6 @@ public class Army : GameObject

List<Unit> _CapturedUnits = new List<Unit>();
HashSet<Tile> _AttackedTiles = new HashSet<Tile>();
HashSet<Unit> _OverrideVisibleUnits = new HashSet<Unit>();

public int Id
{
Expand All @@ -36,18 +36,21 @@ public IEnumerable<Unit> Units
}
}

public Army(Match Match, ArmyConfiguration ArmyConfiguration, IdGenerator IdGenerator)
public Army(Match Match, SightFinder SightFinder, ArmyConfiguration ArmyConfiguration, IdGenerator IdGenerator)
{
_Id = IdGenerator.GenerateId();
this.Match = Match;
this.SightFinder = SightFinder;

Configuration = ArmyConfiguration;
Deployments = ArmyConfiguration.DeploymentConfigurations.Select(
i => i.GenerateDeployment(this, IdGenerator)).ToList();
foreach (Unit u in Units)
{
u.OnDestroy += UnitDestroyed;
u.OnCapture += UnitCaptured;
}

Match.Relay.OnUnitDestroy += UnitDestroyed;
Match.Relay.OnUnitCapture += UnitCaptured;

SightFinder.SetTrackingArmy(this);
SightFinder.Hook(Match.Relay);
_IdGenerator = IdGenerator;
}

Expand Down Expand Up @@ -97,72 +100,11 @@ public bool MustMove(bool Vehicle)
return Units.Any(i => i.MustMove() && i.CanMove(Vehicle, false) == OrderInvalidReason.NONE);
}

public bool CanSeeUnit(Unit Unit)
{
if (Unit.Position == null) return false;
if (Unit.Configuration.IsAircraft()) return true;

bool lowProfile = Unit.Configuration.HasLowProfile
|| Unit.Position.Units.Any(
i => i.Configuration.UnitClass == UnitClass.FORT
&& i.Army == Unit.Army
&& i.Configuration.HasLowProfile);
bool concealed = Unit.Position.Rules.Concealing
|| (Unit.Position.Rules.LowProfileConcealing && lowProfile);
return !concealed || CanSpotTile(Unit.Position) || _OverrideVisibleUnits.Contains(Unit);
}

public bool CanSeeTile(Tile Tile, bool OverrideConcealment = false)
{
if (Tile == null) return false;
if (Tile.Rules.Concealing && !OverrideConcealment) return CanSpotTile(Tile);
foreach (Unit u in Units.Where(
i => i.Status == UnitStatus.ACTIVE && i.Configuration.CanSpot))
{
var s = u.GetLineOfSight(Tile);
if (s != null && s.Validate() == NoLineOfSightReason.NONE && u.Configuration.SpotRange >= s.Range)
return true;
}
return false;
}

public bool CanSpotTile(Tile Tile)
{
if (Tile == null) return false;
return Units.Any(
i => i.Position != null
&& i.Status == UnitStatus.ACTIVE
&& i.Configuration.CanSpot
&& (i.Position == Tile || i.Position.Neighbors().Contains(Tile)));
}

public void SetUnitVisibility(Unit Unit, bool Visible)
{
if (Unit.Army == this || Unit.Position == null || CanSeeUnit(Unit)) return;

if (Unit.Position != null && CanSeeTile(Unit.Position, true))
{
if (Visible) _OverrideVisibleUnits.Add(Unit);
else _OverrideVisibleUnits.Remove(Unit);
}
else _OverrideVisibleUnits.Remove(Unit);
}

public void UpdateUnitVisibility(Unit Unit, Tile MovedFrom, Tile MovedTo)
{
if (Unit.Army == this || !MovedTo.Rules.Concealing) return;
if (CanSeeTile(MovedFrom)) _OverrideVisibleUnits.Add(Unit);
else _OverrideVisibleUnits.Remove(Unit);
}

public void UpdateUnitVisibility(Unit Unit, Tile MovedTo)
{
UpdateUnitVisibility(Unit, null, MovedTo);
}

void UnitDestroyed(object Sender, EventArgs E)
{
var unit = (Unit)Sender;
if (unit.Army != this) return;

if (unit.Configuration.LeavesWreckWhenDestroyed)
{
var wreckage = new Unit(this, GameData.Wreckage, _IdGenerator);
Expand All @@ -173,17 +115,15 @@ void UnitDestroyed(object Sender, EventArgs E)

void UnitCaptured(object Sender, ValuedEventArgs<Army> E)
{
E.Value.CaptureUnit((Unit)Sender);
}
Unit unit = (Unit)Sender;
if (unit.Army != this) return;

void CaptureUnit(Unit Unit)
{
var newUnit = new Unit(this, Unit.Configuration, _IdGenerator);
var newUnit = new Unit(this, unit.Configuration, _IdGenerator);
newUnit.OnDestroy += UnitDestroyed;
newUnit.OnCapture += UnitCaptured;
_CapturedUnits.Add(newUnit);
if (OnUnitAdded != null) OnUnitAdded(this, new NewUnitEventArgs(newUnit));
newUnit.Place(Unit.Position);
newUnit.Place(unit.Position);
}

public override string ToString()
Expand Down
90 changes: 90 additions & 0 deletions Model/EventRelay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using Cardamom.Utilities;

namespace PanzerBlitz
{
public class EventRelay
{
// Unit Events
public EventHandler<EventArgs> OnUnitLoad;
public EventHandler<ValuedEventArgs<Unit>> OnUnitUnload;
public EventHandler<ValuedEventArgs<UnitConfiguration>> OnUnitConfigurationChange;
public EventHandler<MovementEventArgs> OnUnitMove;
public EventHandler<EventArgs> OnUnitFire;
public EventHandler<ValuedEventArgs<Tile>> OnUnitRemove;
public EventHandler<EventArgs> OnUnitDestroy;
public EventHandler<ValuedEventArgs<Army>> OnUnitCapture;

// Army Events
public EventHandler<NewUnitEventArgs> OnUnitAdded;

public void Hook(Match Match)
{
foreach (var army in Match.Armies) Hook(army);
}

public void Hook(Army Army)
{
Army.OnUnitAdded += HandleUnitAdded;
foreach (var unit in Army.Units) Hook(unit);
}

public void Hook(Unit Unit)
{
Unit.OnLoad += HandleUnitLoad;
Unit.OnUnload += HandleUnitUnload;
Unit.OnConfigurationChange += HandleUnitConfigurationChange;
Unit.OnMove += HandleUnitMove;
Unit.OnFire += HandleUnitFire;
Unit.OnRemove += HandleUnitRemove;
Unit.OnDestroy += HandleUnitDestroy;
Unit.OnCapture += HandleUnitCapture;
}

void HandleUnitAdded(object Sender, NewUnitEventArgs E)
{
Hook(E.Unit);
OnUnitAdded?.Invoke(Sender, E);
}

void HandleUnitLoad(object Sender, EventArgs E)
{
OnUnitLoad?.Invoke(Sender, E);
}

void HandleUnitUnload(object Sender, ValuedEventArgs<Unit> E)
{
OnUnitUnload?.Invoke(Sender, E);
}

void HandleUnitConfigurationChange(object Sender, ValuedEventArgs<UnitConfiguration> E)
{
OnUnitConfigurationChange?.Invoke(Sender, E);
}

void HandleUnitMove(object Sender, MovementEventArgs E)
{
OnUnitMove?.Invoke(Sender, E);
}

void HandleUnitFire(object Sender, EventArgs E)
{
OnUnitFire?.Invoke(Sender, E);
}

void HandleUnitRemove(object Sender, ValuedEventArgs<Tile> E)
{
OnUnitRemove?.Invoke(Sender, E);
}

void HandleUnitDestroy(object Sender, EventArgs E)
{
OnUnitDestroy?.Invoke(Sender, E);
}

void HandleUnitCapture(object Sender, ValuedEventArgs<Army> E)
{
OnUnitCapture?.Invoke(Sender, E);
}
}
}
11 changes: 11 additions & 0 deletions Model/Map/Coordinate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public Coordinate(int X, int Y)
this.Y = Y;
}

public Coordinate(HexCoordinate From)
{
X = From.X + (From.Z + 1) / 2;
Y = From.Z;
}

public Coordinate(SerializationInputStream Stream)
{
X = Stream.ReadInt32();
Expand All @@ -34,6 +40,11 @@ public void Serialize(SerializationOutputStream Stream)
Stream.Write(Y);
}

public HexCoordinate ToHexCoordinate()
{
return new HexCoordinate(this);
}

public override bool Equals(object obj)
{
if (obj == null) return false;
Expand Down
32 changes: 32 additions & 0 deletions Model/Map/HexCoordinate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@ namespace PanzerBlitz
{
public class HexCoordinate
{
public static HexCoordinate Interpolate(HexCoordinate c1, HexCoordinate c2, double t, double Dither = 0)
{
return Round(c1.X * (1 - t) + c2.X * t, c1.Y * (1 - t) + c2.Y * t, c1.Z * (1 - t) + c2.Z * t, Dither);
}

public static HexCoordinate Round(double X, double Y, double Z, double Dither = 0)
{
int x = (int)Math.Floor(X + .5 + Dither);
int y = (int)Math.Floor(Y + .5 + Dither);
int z = (int)Math.Floor(Z + .5 + Dither);

double dX = Math.Abs(X - x);
double dY = Math.Abs(Y - y);
double dZ = Math.Abs(Z - z);

if (dX > dY && dX > dZ) x = -(y + z);
else if (dY > dZ) y = -(x + z);
else z = -(x + y);

return new HexCoordinate(x, y, z);
}

public readonly int X;
public readonly int Y;
public readonly int Z;
Expand All @@ -21,9 +43,19 @@ public HexCoordinate(Coordinate From)
Z = From.Y;
}

public Coordinate ToCoordinate()
{
return new Coordinate(this);
}

public int Distance(HexCoordinate To)
{
return Math.Max(Math.Abs(X - To.X), Math.Max(Math.Abs(Y - To.Y), Math.Abs(Z - To.Z)));
}

public override string ToString()
{
return string.Format("[HexCoordinate: X={0}, Y={1}, Z={2}]", X, Y, Z);
}
}
}
2 changes: 1 addition & 1 deletion Model/Map/RandomMapConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Map GenerateMap(Environment Environment, IdGenerator IdGenerator)
{
var name =
new string(_Configuration.NameGenerator.Generate(_Random).ToArray());
name = ObjectDescriber.CapitalizeAll(name);
name = ObjectDescriber.Namify(name);
map.Regions.Add(new MapRegion(name, town));
var tiles = town.ToList();
for (int i = 0; i < Math.Max(1, tiles.Count / 4); ++i)
Expand Down
Loading

0 comments on commit 660f6f7

Please sign in to comment.