BIM/UIM become traits on World; Added World.WorldActor
This commit is contained in:
@@ -57,13 +57,13 @@ namespace OpenRa.Traits.Activities
|
||||
|
||||
bool CanEnterCell( int2 c, Actor self )
|
||||
{
|
||||
if (!self.World.BuildingInfluence.CanMoveHere(c)
|
||||
&& self.World.BuildingInfluence.GetBuildingAt(c) != ignoreBuilding)
|
||||
if (!self.World.WorldActor.traits.Get<BuildingInfluence>().CanMoveHere(c)
|
||||
&& self.World.WorldActor.traits.Get<BuildingInfluence>().GetBuildingAt(c) != ignoreBuilding)
|
||||
return false;
|
||||
|
||||
// Cannot enter a cell if any unit inside is uncrushable
|
||||
// This will need to be updated for multiple-infantry-in-a-cell
|
||||
return (!self.World.UnitInfluence.GetUnitsAt(c).Any(a => a != self && !self.World.IsActorCrushableByActor(a, self)));
|
||||
return (!self.World.WorldActor.traits.Get<UnitInfluence>().GetUnitsAt(c).Any(a => a != self && !self.World.IsActorCrushableByActor(a, self)));
|
||||
}
|
||||
|
||||
public IActivity Tick( Actor self )
|
||||
@@ -144,10 +144,10 @@ namespace OpenRa.Traits.Activities
|
||||
return null;
|
||||
}
|
||||
|
||||
self.World.UnitInfluence.Remove( self, mobile );
|
||||
self.World.WorldActor.traits.Get<UnitInfluence>().Remove( self, mobile );
|
||||
var newPath = getPath(self, mobile).TakeWhile(a => a != self.Location).ToList();
|
||||
|
||||
self.World.UnitInfluence.Add( self, mobile );
|
||||
self.World.WorldActor.traits.Get<UnitInfluence>().Add( self, mobile );
|
||||
if (newPath.Count != 0)
|
||||
path = newPath;
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace OpenRa.Traits.Activities
|
||||
int2? ChooseExitTile(Actor self)
|
||||
{
|
||||
// is anyone still hogging this tile?
|
||||
if (self.World.UnitInfluence.GetUnitsAt(self.Location).Count() > 1)
|
||||
if (self.World.WorldActor.traits.Get<UnitInfluence>().GetUnitsAt(self.Location).Count() > 1)
|
||||
return null;
|
||||
|
||||
for (var i = -1; i < 2; i++)
|
||||
|
||||
55
OpenRa.Game/Traits/BuildingInfluence.cs
Executable file
55
OpenRa.Game/Traits/BuildingInfluence.cs
Executable file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using OpenRa.GameRules;
|
||||
|
||||
namespace OpenRa.Traits
|
||||
{
|
||||
public class BuildingInfluenceInfo : ITraitInfo
|
||||
{
|
||||
public object Create( Actor self ) { return new BuildingInfluence( self ); }
|
||||
}
|
||||
|
||||
public class BuildingInfluence
|
||||
{
|
||||
bool[,] blocked = new bool[128, 128];
|
||||
Actor[,] influence = new Actor[128, 128];
|
||||
|
||||
public BuildingInfluence( Actor self )
|
||||
{
|
||||
self.World.ActorAdded +=
|
||||
a => { if (a.traits.Contains<Building>())
|
||||
ChangeInfluence(a, a.traits.Get<Building>(), true); };
|
||||
self.World.ActorRemoved +=
|
||||
a => { if (a.traits.Contains<Building>())
|
||||
ChangeInfluence(a, a.traits.Get<Building>(), false); };
|
||||
}
|
||||
|
||||
void ChangeInfluence( Actor a, Building building, bool isAdd )
|
||||
{
|
||||
foreach( var u in Footprint.UnpathableTiles( a.Info.Name, a.Info.Traits.Get<BuildingInfo>(), a.Location ) )
|
||||
if( IsValid( u ) )
|
||||
blocked[ u.X, u.Y ] = isAdd;
|
||||
|
||||
foreach( var u in Footprint.Tiles( a.Info.Name, a.Info.Traits.Get<BuildingInfo>(), a.Location ) )
|
||||
if( IsValid( u ) )
|
||||
influence[ u.X, u.Y ] = isAdd ? a : null;
|
||||
}
|
||||
|
||||
bool IsValid(int2 t)
|
||||
{
|
||||
return !(t.X < 0 || t.Y < 0 || t.X >= 128 || t.Y >= 128);
|
||||
}
|
||||
|
||||
public Actor GetBuildingAt(int2 cell)
|
||||
{
|
||||
if (!IsValid(cell)) return null;
|
||||
return influence[cell.X, cell.Y];
|
||||
}
|
||||
|
||||
public bool CanMoveHere(int2 cell)
|
||||
{
|
||||
return IsValid(cell) && !blocked[cell.X, cell.Y];
|
||||
}
|
||||
}}
|
||||
@@ -52,10 +52,10 @@ namespace OpenRa.Traits
|
||||
|
||||
public bool CanEnterCell(int2 a)
|
||||
{
|
||||
if (!self.World.BuildingInfluence.CanMoveHere(a)) return false;
|
||||
if (!self.World.WorldActor.traits.Get<BuildingInfluence>().CanMoveHere(a)) return false;
|
||||
|
||||
var crushable = true;
|
||||
foreach (Actor actor in self.World.UnitInfluence.GetUnitsAt(a))
|
||||
foreach (Actor actor in self.World.WorldActor.traits.Get<UnitInfluence>().GetUnitsAt(a))
|
||||
{
|
||||
if (actor == self) continue;
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace OpenRa.Traits
|
||||
public Crate(Actor self)
|
||||
{
|
||||
this.self = self;
|
||||
self.World.UnitInfluence.Add(self, this);
|
||||
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
|
||||
}
|
||||
|
||||
public void OnCrush(Actor crusher)
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace OpenRa.Traits
|
||||
public int2 fromCell
|
||||
{
|
||||
get { return __fromCell; }
|
||||
set { self.World.UnitInfluence.Remove(self, this); __fromCell = value; self.World.UnitInfluence.Add(self, this); }
|
||||
set { self.World.WorldActor.traits.Get<UnitInfluence>().Remove(self, this); __fromCell = value; self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this); }
|
||||
}
|
||||
public int2 toCell
|
||||
{
|
||||
@@ -28,11 +28,11 @@ namespace OpenRa.Traits
|
||||
{
|
||||
if (self.Location != value)
|
||||
{
|
||||
self.World.UnitInfluence.Remove(self, this);
|
||||
self.World.WorldActor.traits.Get<UnitInfluence>().Remove(self, this);
|
||||
self.Location = value;
|
||||
self.Owner.Shroud.Explore(self);
|
||||
}
|
||||
self.World.UnitInfluence.Add(self, this);
|
||||
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace OpenRa.Traits
|
||||
{
|
||||
this.self = self;
|
||||
__fromCell = toCell;
|
||||
self.World.UnitInfluence.Add(self, this);
|
||||
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
|
||||
}
|
||||
|
||||
public void TeleportTo(Actor self, int2 xy)
|
||||
@@ -91,10 +91,10 @@ namespace OpenRa.Traits
|
||||
|
||||
public bool CanEnterCell(int2 a)
|
||||
{
|
||||
if (!self.World.BuildingInfluence.CanMoveHere(a)) return false;
|
||||
if (!self.World.WorldActor.traits.Get<BuildingInfluence>().CanMoveHere(a)) return false;
|
||||
|
||||
var crushable = true;
|
||||
foreach (Actor actor in self.World.UnitInfluence.GetUnitsAt(a))
|
||||
foreach (Actor actor in self.World.WorldActor.traits.Get<UnitInfluence>().GetUnitsAt(a))
|
||||
{
|
||||
if (actor == self) continue;
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace OpenRa.Traits
|
||||
public bool Produce( Actor self, ActorInfo producee )
|
||||
{
|
||||
var location = CreationLocation( self, producee );
|
||||
if( location == null || self.World.UnitInfluence.GetUnitsAt( location.Value ).Any() )
|
||||
if( location == null || self.World.WorldActor.traits.Get<UnitInfluence>().GetUnitsAt( location.Value ).Any() )
|
||||
return false;
|
||||
|
||||
var newUnit = self.World.CreateActor( producee.Name, location.Value, self.Owner );
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace OpenRa.Traits
|
||||
public void Tick(Actor self)
|
||||
{
|
||||
var b = self.GetBounds(false);
|
||||
if (isOpen && !self.World.UnitInfluence.GetUnitsAt(((1/24f) * self.CenterLocation).ToInt2()).Any())
|
||||
if (isOpen && !self.World.WorldActor.traits.Get<UnitInfluence>().GetUnitsAt(((1/24f) * self.CenterLocation).ToInt2()).Any())
|
||||
{
|
||||
isOpen = false;
|
||||
roof.PlayBackwardsThen(GetPrefix(self) + "build-top", () => roof.Play(GetPrefix(self) + "idle-top"));
|
||||
|
||||
95
OpenRa.Game/Traits/UnitInfluence.cs
Executable file
95
OpenRa.Game/Traits/UnitInfluence.cs
Executable file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace OpenRa.Traits
|
||||
{
|
||||
public class UnitInfluenceInfo : ITraitInfo
|
||||
{
|
||||
public object Create( Actor self ) { return new UnitInfluence( self ); }
|
||||
}
|
||||
|
||||
public class UnitInfluence : ITick
|
||||
{
|
||||
List<Actor>[,] influence = new List<Actor>[128, 128];
|
||||
readonly int2 searchDistance = new int2(2,2);
|
||||
|
||||
public UnitInfluence( Actor self )
|
||||
{
|
||||
for (int i = 0; i < 128; i++)
|
||||
for (int j = 0; j < 128; j++)
|
||||
influence[ i, j ] = new List<Actor>();
|
||||
|
||||
self.World.ActorRemoved += a => Remove( a, a.traits.GetOrDefault<IOccupySpace>() );
|
||||
}
|
||||
|
||||
public void Tick( Actor self )
|
||||
{
|
||||
// Does this belong here? NO, but it's your mess.
|
||||
|
||||
// Get the crushable actors
|
||||
foreach (var a in self.World.Actors.Where(b => b.traits.Contains<ICrushable>()))
|
||||
{
|
||||
// Are there any units in the same cell that can crush this?
|
||||
foreach( var ios in a.traits.WithInterface<IOccupySpace>() )
|
||||
foreach( var cell in ios.OccupiedCells() )
|
||||
{
|
||||
// There should only be one (counterexample: An infantry and a tank try to pick up a crate at the same time.)
|
||||
// If there is more than one, do action on the first crusher
|
||||
var crusher = GetUnitsAt(cell).Where(b => a != b && self.World.IsActorCrushableByActor(a, b)).FirstOrDefault();
|
||||
if (crusher != null)
|
||||
{
|
||||
Log.Write("{0} crushes {1}", crusher.Info.Name, a.Info.Name);
|
||||
// Apply the crush action
|
||||
foreach (var crush in a.traits.WithInterface<ICrushable>())
|
||||
crush.OnCrush(crusher);
|
||||
}
|
||||
}
|
||||
}
|
||||
SanityCheck( self );
|
||||
}
|
||||
|
||||
[Conditional( "SANITY_CHECKS" )]
|
||||
void SanityCheck( Actor self )
|
||||
{
|
||||
for( int y = 0 ; y < 128 ; y++ )
|
||||
for( int x = 0 ; x < 128 ; x++ )
|
||||
if( influence[ x, y ] != null )
|
||||
foreach (var a in influence[ x, y ])
|
||||
if (!a.traits.Get<IOccupySpace>().OccupiedCells().Contains( new int2( x, y ) ) )
|
||||
throw new InvalidOperationException( "UIM: Sanity check failed A" );
|
||||
|
||||
foreach( Actor a in self.World.Actors )
|
||||
foreach( var ios in a.traits.WithInterface<IOccupySpace>() )
|
||||
foreach( var cell in ios.OccupiedCells() )
|
||||
if (!influence[cell.X, cell.Y].Contains(a))
|
||||
throw new InvalidOperationException( "UIM: Sanity check failed B" );
|
||||
}
|
||||
|
||||
public IEnumerable<Actor> GetUnitsAt( int2 a )
|
||||
{
|
||||
return influence[ a.X, a.Y ];
|
||||
}
|
||||
|
||||
public void Add( Actor self, IOccupySpace unit )
|
||||
{
|
||||
foreach( var c in unit.OccupiedCells() )
|
||||
influence[c.X, c.Y].Add(self);
|
||||
}
|
||||
|
||||
public void Remove( Actor self, IOccupySpace unit )
|
||||
{
|
||||
if (unit != null)
|
||||
foreach (var c in unit.OccupiedCells())
|
||||
influence[c.X, c.Y].Remove(self);
|
||||
}
|
||||
|
||||
public void Update(Actor self, IOccupySpace unit)
|
||||
{
|
||||
Remove(self, unit);
|
||||
if (!self.IsDead) Add(self, unit);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user