Shift movement cost/speed into IMove; regressions in a few areas
This commit is contained in:
@@ -52,15 +52,16 @@ namespace OpenRA
|
||||
|
||||
public List<int2> FindUnitPath(int2 from, int2 target, Actor self)
|
||||
{
|
||||
var umt = self.traits.Get<Mobile>().GetMovementType();
|
||||
// Todo: Reenable cache on something that isn't umt
|
||||
//var umt = self.traits.Get<Mobile>().GetMovementType();
|
||||
using (new PerfSample("find_unit_path"))
|
||||
{
|
||||
var cached = CachedPaths.FirstOrDefault(p => p.from == from && p.to == target && p.umt == umt);
|
||||
if (cached != null)
|
||||
{
|
||||
cached.tick = Game.LocalTick;
|
||||
return new List<int2>(cached.result);
|
||||
}
|
||||
//var cached = CachedPaths.FirstOrDefault(p => p.from == from && p.to == target && p.umt == umt);
|
||||
//if (cached != null)
|
||||
//{
|
||||
// cached.tick = Game.LocalTick;
|
||||
// return new List<int2>(cached.result);
|
||||
//}
|
||||
|
||||
var pb = FindBidiPath(
|
||||
PathSearch.FromPoint(self, target, from, true)
|
||||
@@ -71,8 +72,8 @@ namespace OpenRA
|
||||
|
||||
CheckSanePath2(pb, from, target);
|
||||
|
||||
CachedPaths.RemoveAll(p => Game.LocalTick - p.tick > MaxPathAge);
|
||||
CachedPaths.Add(new CachedPath { from = from, to = target, umt = umt, result = pb, tick = Game.LocalTick });
|
||||
//CachedPaths.RemoveAll(p => Game.LocalTick - p.tick > MaxPathAge);
|
||||
//CachedPaths.Add(new CachedPath { from = from, to = target, umt = umt, result = pb, tick = Game.LocalTick });
|
||||
return new List<int2>(pb);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,8 +81,6 @@ namespace OpenRA
|
||||
|
||||
public int2 Expand( World world )
|
||||
{
|
||||
var umt = self.traits.Get<Mobile>().GetMovementType();
|
||||
|
||||
var p = queue.Pop();
|
||||
cellInfo[ p.Location.X, p.Location.Y ].Seen = true;
|
||||
|
||||
|
||||
@@ -227,8 +227,7 @@ namespace OpenRA.Traits.Activities
|
||||
|
||||
public void TickMove( Actor self, Mobile mobile, Move parent )
|
||||
{
|
||||
var umt = self.Info.Traits.Get<MobileInfo>().MovementType;
|
||||
moveFraction += (int)Util.GetEffectiveSpeed(self, umt);
|
||||
moveFraction += (int)mobile.MovementSpeedForCell(self, self.Location);
|
||||
if( moveFraction >= moveFractionTotal )
|
||||
moveFraction = moveFractionTotal;
|
||||
UpdateCenterLocation( self, mobile );
|
||||
|
||||
@@ -20,22 +20,27 @@
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System;
|
||||
using OpenRA.GameRules;
|
||||
using OpenRA.FileFormats;
|
||||
|
||||
namespace OpenRA.Traits
|
||||
{
|
||||
public class MobileInfo : ITraitInfo, ITraitPrerequisite<UnitInfo>
|
||||
{
|
||||
public readonly UnitMovementType MovementType = UnitMovementType.Wheel;
|
||||
public readonly TerrainType[] TerrainTypes;
|
||||
public readonly float[] TerrainSpeeds;
|
||||
public readonly int WaitAverage = 60;
|
||||
public readonly int WaitSpread = 20;
|
||||
|
||||
public virtual object Create(ActorInitializer init) { return new Mobile(init); }
|
||||
public virtual object Create(ActorInitializer init) { return new Mobile(init, this); }
|
||||
}
|
||||
|
||||
public class Mobile : IIssueOrder, IResolveOrder, IOccupySpace, IMove
|
||||
{
|
||||
public readonly Actor self;
|
||||
public readonly Dictionary<TerrainType,float> TerrainCost;
|
||||
public readonly Dictionary<TerrainType,float> TerrainSpeed;
|
||||
|
||||
[Sync]
|
||||
int2 __fromCell, __toCell;
|
||||
@@ -59,11 +64,23 @@ namespace OpenRA.Traits
|
||||
AddInfluence();
|
||||
}
|
||||
|
||||
public Mobile(ActorInitializer init)
|
||||
public Mobile(ActorInitializer init, MobileInfo info)
|
||||
{
|
||||
this.self = init.self;
|
||||
this.__fromCell = this.__toCell = init.location;
|
||||
AddInfluence();
|
||||
|
||||
TerrainCost = new Dictionary<TerrainType, float>();
|
||||
TerrainSpeed = new Dictionary<TerrainType, float>();
|
||||
|
||||
if (info.TerrainTypes.Count() != info.TerrainSpeeds.Count())
|
||||
throw new InvalidOperationException("Mobile TerrainType/TerrainSpeed length missmatch");
|
||||
|
||||
for (int i = 0; i < info.TerrainTypes.Count(); i++)
|
||||
{
|
||||
TerrainCost.Add(info.TerrainTypes[i], 1f/info.TerrainSpeeds[i]);
|
||||
TerrainSpeed.Add(info.TerrainTypes[i], info.TerrainSpeeds[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetPosition(Actor self, int2 cell)
|
||||
@@ -85,8 +102,7 @@ namespace OpenRA.Traits
|
||||
if (!mi.Modifiers.HasModifier(Modifiers.Alt)) return null;
|
||||
if (!CanEnterCell(underCursor.Location, null, true)) return null;
|
||||
}
|
||||
var umt = self.Info.Traits.Get<MobileInfo>().MovementType;
|
||||
if (Util.GetEffectiveSpeed(self,umt) == 0) return null; /* allow disabling move orders from modifiers */
|
||||
if (MovementSpeedForCell(self, self.Location) == 0) return null; /* allow disabling move orders from modifiers */
|
||||
if (xy == toCell) return null;
|
||||
return new Order("Move", self, xy, mi.Modifiers.HasModifier(Modifiers.Shift));
|
||||
}
|
||||
@@ -114,11 +130,6 @@ namespace OpenRA.Traits
|
||||
: new[] { fromCell, toCell };
|
||||
}
|
||||
|
||||
public UnitMovementType GetMovementType()
|
||||
{
|
||||
return self.Info.Traits.Get<MobileInfo>().MovementType;
|
||||
}
|
||||
|
||||
public bool CanEnterCell(int2 p)
|
||||
{
|
||||
return CanEnterCell(p, null, true);
|
||||
@@ -149,19 +160,37 @@ namespace OpenRA.Traits
|
||||
return MovementCostForCell(self, cell) < float.PositiveInfinity;
|
||||
}
|
||||
|
||||
public float MovementCostForCell(Actor self, int2 cell)
|
||||
public virtual float MovementCostForCell(Actor self, int2 cell)
|
||||
{
|
||||
if (!self.World.Map.IsInMap(cell.X,cell.Y))
|
||||
return float.PositiveInfinity;
|
||||
|
||||
var type = self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[cell.X, cell.Y]);
|
||||
var umt = self.Info.Traits.Get<MobileInfo>().MovementType;
|
||||
|
||||
// Todo: Cache cost for each terraintype
|
||||
return (float)Rules.TerrainTypes[type].GetCost(umt)*
|
||||
self.World.WorldActor.traits.WithInterface<ICustomTerrain>().Aggregate(1f, (a, x) => a * x.GetCost(cell,self));
|
||||
return (float)TerrainCost[type]*
|
||||
self.World.WorldActor.traits.WithInterface<ICustomTerrain>()
|
||||
.Select(t => t.GetCost(cell, self))
|
||||
.Product();
|
||||
}
|
||||
|
||||
public virtual float MovementSpeedForCell(Actor self, int2 cell)
|
||||
{
|
||||
var unitInfo = self.Info.Traits.GetOrDefault<UnitInfo>();
|
||||
if( unitInfo == null || !self.World.Map.IsInMap(cell.X,cell.Y))
|
||||
return 0f;
|
||||
|
||||
var type = self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[cell.X, cell.Y]);
|
||||
var terrain = TerrainSpeed[type]*self.World.WorldActor.traits
|
||||
.WithInterface<ICustomTerrain>()
|
||||
.Select(t => t.GetSpeedModifier(self.Location, self))
|
||||
.Product();
|
||||
|
||||
var modifier = self.traits
|
||||
.WithInterface<ISpeedModifier>()
|
||||
.Select(t => t.GetSpeedModifier())
|
||||
.Product();
|
||||
return unitInfo.Speed * terrain * modifier;
|
||||
}
|
||||
|
||||
public IEnumerable<float2> GetCurrentPath(Actor self)
|
||||
{
|
||||
var move = self.GetCurrentActivity() as Activities.Move;
|
||||
|
||||
@@ -107,9 +107,9 @@ namespace OpenRA.Traits
|
||||
public interface ITags { IEnumerable<TagType> GetTags(); }
|
||||
public interface IMove
|
||||
{
|
||||
UnitMovementType GetMovementType();
|
||||
bool CanEnterCell(int2 location);
|
||||
float MovementCostForCell(Actor self, int2 cell);
|
||||
float MovementSpeedForCell(Actor self, int2 cell);
|
||||
IEnumerable<float2> GetCurrentPath(Actor self);
|
||||
void SetPosition(Actor self, int2 cell);
|
||||
}
|
||||
|
||||
@@ -138,27 +138,6 @@ namespace OpenRA.Traits
|
||||
return new Renderable(s, loc.Round(), pal);
|
||||
}
|
||||
|
||||
public static float GetEffectiveSpeed(Actor self, UnitMovementType umt)
|
||||
{
|
||||
var unitInfo = self.Info.Traits.GetOrDefault<UnitInfo>();
|
||||
if( unitInfo == null ) return 0f;
|
||||
|
||||
var terrain = 1f;
|
||||
if (umt != UnitMovementType.Fly)
|
||||
{
|
||||
var tt = self.World.GetTerrainType(self.Location);
|
||||
terrain = Rules.TerrainTypes[tt].GetSpeedModifier(umt)*self.World.WorldActor.traits
|
||||
.WithInterface<ICustomTerrain>()
|
||||
.Select(t => t.GetSpeedModifier(self.Location, self))
|
||||
.Product();
|
||||
}
|
||||
var modifier = self.traits
|
||||
.WithInterface<ISpeedModifier>()
|
||||
.Select(t => t.GetSpeedModifier())
|
||||
.Product();
|
||||
return unitInfo.Speed * terrain * modifier;
|
||||
}
|
||||
|
||||
public static IActivity SequenceActivities(params IActivity[] acts)
|
||||
{
|
||||
return acts.Reverse().Aggregate(
|
||||
|
||||
@@ -98,8 +98,10 @@ namespace OpenRA.Traits
|
||||
if (content[p.X,p.Y].type == null)
|
||||
return 1.0f;
|
||||
|
||||
var umt = forActor.traits.Get<Mobile>().GetMovementType();
|
||||
return content[p.X,p.Y].type.GetSpeedModifier(umt);
|
||||
// Todo: Reenable based off something that isn't umt
|
||||
return 1f;
|
||||
//var umt = forActor.traits.Get<Mobile>().GetMovementType();
|
||||
//return content[p.X,p.Y].type.GetSpeedModifier(umt);
|
||||
}
|
||||
|
||||
public float GetCost(int2 p, Actor forActor)
|
||||
@@ -107,8 +109,10 @@ namespace OpenRA.Traits
|
||||
if (content[p.X,p.Y].type == null)
|
||||
return 1.0f;
|
||||
|
||||
var umt = forActor.traits.Get<Mobile>().GetMovementType();
|
||||
return content[p.X,p.Y].type.GetCost(umt);
|
||||
// Todo: Reenable based off something that isn't umt
|
||||
return 1f;
|
||||
//var umt = forActor.traits.Get<Mobile>().GetMovementType();
|
||||
//return content[p.X,p.Y].type.GetCost(umt);
|
||||
}
|
||||
|
||||
Sprite[] ChooseContent(ResourceType t)
|
||||
|
||||
@@ -48,13 +48,16 @@ namespace OpenRA
|
||||
Rules.TerrainTypes[world.TileSet.GetTerrainType(world.Map.MapTiles[a.X, a.Y])]
|
||||
.Buildable;
|
||||
}
|
||||
|
||||
|
||||
public static bool IsActorCrushableByActor(this World world, Actor a, Actor b)
|
||||
{
|
||||
var movement = b.traits.GetOrDefault<IMove>();
|
||||
return movement != null && world.IsActorCrushableByMovementType(a, movement.GetMovementType());
|
||||
return false;
|
||||
//var movement = b.traits.GetOrDefault<IMove>();
|
||||
//return movement != null && world.IsActorCrushableByMovementType(a, movement.GetMovementType());
|
||||
}
|
||||
|
||||
// Todo: Reenable crushing based on actor, not umt
|
||||
/*
|
||||
public static bool IsActorPathableToCrush(this World world, Actor a, UnitMovementType umt)
|
||||
{
|
||||
return a != null &&
|
||||
@@ -68,7 +71,7 @@ namespace OpenRA
|
||||
a.traits.WithInterface<ICrushable>()
|
||||
.Any(c => c.IsCrushableBy(umt, a.Owner));
|
||||
}
|
||||
|
||||
*/
|
||||
public static IEnumerable<Actor> FindUnitsAtMouse(this World world, int2 mouseLocation)
|
||||
{
|
||||
var loc = mouseLocation + Game.viewport.Location;
|
||||
|
||||
Reference in New Issue
Block a user