This commit is contained in:
Chris Forbes
2010-01-19 15:17:02 +13:00
28 changed files with 564 additions and 77 deletions

View File

@@ -120,6 +120,7 @@ namespace OpenRa
public bool IsDead { get { return Health <= 0; } }
public bool IsInWorld { get; set; }
public bool RemoveOnDeath = true;
public DamageState GetDamageState()
{
@@ -145,7 +146,8 @@ namespace OpenRa
if (attacker.Owner != null)
attacker.Owner.Kills++;
Game.world.AddFrameEndTask(w => w.Remove(this));
if (RemoveOnDeath)
Game.world.AddFrameEndTask(w => w.Remove(this));
}
var maxHP = this.GetMaxHP();

67
OpenRa.Game/Bridges.cs Normal file
View File

@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.Traits;
namespace OpenRa
{
static class Bridges
{
public static void MakeBridges(World w)
{
var mini = w.Map.XOffset; var maxi = w.Map.XOffset + w.Map.Width;
var minj = w.Map.YOffset; var maxj = w.Map.YOffset + w.Map.Height;
for (var j = minj; j < maxj; j++)
for (var i = mini; i < maxi; i++)
if (IsBridge(w, w.Map.MapTiles[i, j].tile))
ConvertBridgeToActor(w, i, j);
foreach (var br in w.Actors.SelectMany(a => a.traits.WithInterface<Bridge>()))
br.FinalizeBridges(w);
}
static void ConvertBridgeToActor(World w, int i, int j)
{
var tile = w.Map.MapTiles[i, j].tile;
var image = w.Map.MapTiles[i, j].image;
var template = w.TileSet.walk[tile];
// base position of the tile
var ni = i - image % template.Size.X;
var nj = j - image / template.Size.X;
var replacedTiles = new Dictionary<int2, int>();
for (var y = nj; y < nj + template.Size.Y; y++)
for (var x = ni; x < ni + template.Size.X; x++)
{
var n = (x - ni) + template.Size.X * (y - nj);
if (!template.TerrainType.ContainsKey(n)) continue;
if (w.Map.IsInMap(x, y))
if (w.Map.MapTiles[x, y].tile == tile
&& w.Map.MapTiles[x,y].image == n)
{
// stash it
replacedTiles[new int2(x, y)] = w.Map.MapTiles[x, y].image;
// remove the tile from the actual map
w.Map.MapTiles[x, y].tile = 0xfffe;
w.Map.MapTiles[x, y].image = 0;
}
}
if (replacedTiles.Any())
{
var a = w.CreateActor(template.Bridge, new int2(ni, nj), null);
var br = a.traits.Get<Bridge>();
br.SetTiles(w, template, replacedTiles);
}
}
static bool IsBridge(World w, ushort t)
{
return w.TileSet.walk[t].Bridge != null;
}
}
}

View File

@@ -58,6 +58,7 @@ namespace OpenRa
FileSystem.UnmountTemporaryPackages();
Rules.LoadRules(mapName, usingAftermath);
world = null; // trying to access the old world will NRE, rather than silently doing it wrong.
world = new World();
Game.world.ActorAdded += a =>
{

View File

@@ -51,7 +51,9 @@ namespace OpenRa.Graphics
if (terrainTypeColors == null)
{
var pal = new Palette(FileSystem.Open(world.Map.Theater + ".pal"));
terrainTypeColors = new[] {theater.ToLowerInvariant() == "snow" ? 0xe3 :0x1a, 0x63, 0x2f, 0x1f, 0x14, 0x64, 0x1f, 0x68, 0x6b, 0x6d }
terrainTypeColors = new[] {
theater.ToLowerInvariant() == "snow" ? 0xe3 :0x1a,
0x63, 0x2f, 0x1f, 0x14, 0x64, 0x1f, 0x68, 0x6b, 0x6d, 0x88 }
.Select( a => Color.FromArgb(alpha, pal.GetColor(a) )).ToArray();
playerColors = Util.MakeArray<Color>( 8, b => Color.FromArgb(alpha, Chat.paletteColors[b]) );

View File

@@ -107,8 +107,6 @@ namespace OpenRa.Graphics
public static Sprite GetImageFromCollection(Renderer renderer,string collection, string image)
{
// Cached sprite
if (cachedSprites.ContainsKey(collection) && cachedSprites[collection].ContainsKey(image))
return cachedSprites[collection][image];

View File

@@ -77,6 +77,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bridges.cs" />
<Compile Include="Chat.cs" />
<Compile Include="Chrome.cs" />
<Compile Include="Combat.cs" />
@@ -210,6 +211,7 @@
<Compile Include="Traits\AutoHeal.cs" />
<Compile Include="Traits\AutoTarget.cs" />
<Compile Include="Traits\BelowUnits.cs" />
<Compile Include="Traits\Bridge.cs" />
<Compile Include="Traits\Buildable.cs" />
<Compile Include="Traits\Building.cs" />
<Compile Include="Traits\Cargo.cs" />

View File

@@ -65,7 +65,7 @@ namespace OpenRa.Orders
}
}
}
catch (IOException e)
catch (IOException)
{
State = ConnectionState.NotConnected;
}

View File

@@ -4,6 +4,7 @@ using System.Linq;
using OpenRa.FileFormats;
using OpenRa.Support;
using OpenRa.Traits;
using System.Diagnostics;
namespace OpenRa
{
@@ -143,9 +144,7 @@ namespace OpenRa
return ret;
}
[System.Diagnostics.Conditional( "SANITY_CHECKS" )]
[Conditional( "SANITY_CHECKS" )]
static void CheckSanePath( List<int2> path )
{
if( path.Count == 0 )
@@ -160,7 +159,7 @@ namespace OpenRa
}
}
[System.Diagnostics.Conditional("SANITY_CHECKS")]
[Conditional("SANITY_CHECKS")]
static void CheckSanePath2(List<int2> path, int2 src, int2 dest)
{
if (path.Count == 0)

View File

@@ -14,7 +14,6 @@ namespace OpenRa
public UnitMovementType umt;
Func<int2, bool> customBlock;
public bool checkForBlocked;
public bool ignoreTerrain;
public Actor ignoreBuilding;
public PathSearch()
@@ -40,9 +39,13 @@ namespace OpenRa
var p = queue.Pop();
cellInfo[ p.Location.X, p.Location.Y ].Seen = true;
if (!ignoreTerrain)
if (passableCost[(int)umt][p.Location.X, p.Location.Y] == float.PositiveInfinity)
return p.Location;
var custom2 = Game.world.customTerrain[p.Location.X, p.Location.Y];
var thisCost = (custom2 != null)
? custom2.GetCost(p.Location, umt)
: passableCost[(int)umt][p.Location.X, p.Location.Y];
if (thisCost == float.PositiveInfinity)
return p.Location;
foreach( int2 d in Util.directions )
{
@@ -51,17 +54,18 @@ namespace OpenRa
if (!Game.world.Map.IsInMap(newHere.X, newHere.Y)) continue;
if( cellInfo[ newHere.X, newHere.Y ].Seen )
continue;
if (!ignoreTerrain)
{
if (passableCost[(int)umt][newHere.X, newHere.Y] == float.PositiveInfinity)
continue;
if (!Game.world.BuildingInfluence.CanMoveHere(newHere) &&
Game.world.BuildingInfluence.GetBuildingAt(newHere) != ignoreBuilding)
continue;
if (Game.world.Map.IsOverlaySolid(newHere))
continue;
}
var custom = Game.world.customTerrain[newHere.X, newHere.Y];
var costHere = (custom != null) ? custom.GetCost(newHere, umt) : passableCost[(int)umt][newHere.X, newHere.Y];
if (costHere == float.PositiveInfinity)
continue;
if (!Game.world.BuildingInfluence.CanMoveHere(newHere) &&
Game.world.BuildingInfluence.GetBuildingAt(newHere) != ignoreBuilding)
continue;
if (Game.world.Map.IsOverlaySolid(newHere))
continue;
// Replicate real-ra behavior of not being able to enter a cell if there is a mixture of crushable and uncrushable units
if (checkForBlocked && (Game.world.UnitInfluence.GetUnitsAt(newHere).Any(a => !Game.world.IsActorPathableToCrush(a, umt))))
@@ -69,14 +73,12 @@ namespace OpenRa
if (customBlock != null && customBlock(newHere))
continue;
var est = heuristic( newHere );
if( est == float.PositiveInfinity )
continue;
float cellCost = ( ( d.X * d.Y != 0 ) ? 1.414213563f : 1.0f ) *
(ignoreTerrain ? 1 : passableCost[ (int)umt ][ newHere.X, newHere.Y ]);
float cellCost = ((d.X * d.Y != 0) ? 1.414213563f : 1.0f) * costHere;
float newCost = cellInfo[ p.Location.X, p.Location.Y ].MinCost + cellCost;
if( newCost >= cellInfo[ newHere.X, newHere.Y ].MinCost )

View File

@@ -23,28 +23,29 @@ namespace OpenRa
Wall = 7,
Beach = 8,
Ore = 9,
Special = 10,
}
static class TerrainCosts
{
static double[][] costs = Util.MakeArray<double[]>( 4,
a => Util.MakeArray<double>( 10, b => double.PositiveInfinity ));
static float[][] costs = Util.MakeArray<float[]>(4,
a => Util.MakeArray<float>(11, b => float.PositiveInfinity));
static TerrainCosts()
{
for( int i = 0 ; i < 10 ; i++ )
for( int i = 0 ; i < 11 ; i++ )
{
if( i == 4 ) continue;
var section = Rules.AllRules.GetSection( ( (TerrainMovementType)i ).ToString() );
for( int j = 0 ; j < 4 ; j++ )
{
string val = section.GetValue( ( (UnitMovementType)j ).ToString(), "0%" );
costs[ j ][ i ] = 100.0 / double.Parse( val.Substring( 0, val.Length - 1 ) );
costs[j][i] = 100f / float.Parse(val.Substring(0, val.Length - 1));
}
}
}
public static double Cost( UnitMovementType unitMovementType, int r )
public static float Cost( UnitMovementType unitMovementType, int r )
{
return costs[ (byte)unitMovementType ][ r ];
}

View File

@@ -180,9 +180,19 @@ namespace OpenRa.Traits
if (self == underCursor) return null;
var isHeal = self.GetPrimaryWeapon().Damage < 0;
if (((underCursor.Owner == self.Owner) ^ isHeal)
&& !mi.Modifiers.HasModifier( Modifiers.Ctrl )) return null;
var forceFire = mi.Modifiers.HasModifier(Modifiers.Ctrl);
if (isHeal)
{
if (underCursor.Owner == null)
return null;
if (underCursor.Owner != self.Owner && !forceFire)
return null;
}
else
if ((underCursor.Owner == self.Owner || underCursor.Owner == null) && !forceFire)
return null;
if (!Combat.HasAnyValidWeapons(self, underCursor)) return null;
return new Order(isHeal ? "Heal" : "Attack", self, underCursor, int2.Zero, null);

View File

@@ -0,0 +1,158 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.Graphics;
using OpenRa.FileFormats;
using IjwFramework.Collections;
using System.Drawing;
namespace OpenRa.Traits
{
class BridgeInfo : ITraitInfo
{
public readonly bool Long = false;
public readonly bool UseAlternateNames = false;
public readonly int[] NorthOffset = null;
public readonly int[] SouthOffset = null;
public object Create(Actor self) { return new Bridge(self); }
}
class Bridge : IRender, ICustomTerrain, INotifyDamage
{
Dictionary<int2, int> Tiles;
List<Dictionary<int2, Sprite>> TileSprites = new List<Dictionary<int2,Sprite>>();
List<TileTemplate> Templates = new List<TileTemplate>();
Actor self;
int state;
Bridge northNeighbour, southNeighbour;
public Bridge(Actor self) { this.self = self; self.RemoveOnDeath = false; }
static string cachedTheater;
static Cache<TileReference, Sprite> sprites;
public IEnumerable<Renderable> Render(Actor self)
{
foreach (var t in TileSprites[state])
yield return new Renderable(t.Value, Game.CellSize * t.Key, PaletteType.Gold);
}
public int StateFromTemplate(TileTemplate t)
{
var info = self.Info.Traits.Get<BridgeInfo>();
if (info.UseAlternateNames)
{
if (t.Name.EndsWith("d")) return 2;
if (t.Name.EndsWith("h")) return 1;
return 0;
}
else
return t.Name[t.Name.Length - 1] - 'a';
}
public string NameFromState(TileTemplate t, int state)
{
var info = self.Info.Traits.Get<BridgeInfo>();
if (info.UseAlternateNames)
return t.Bridge + new[] { "", "h", "d" }[state];
else
return t.Bridge + (char)(state + 'a');
}
public void SetTiles(World world, TileTemplate template, Dictionary<int2, int> replacedTiles)
{
Tiles = replacedTiles;
state = StateFromTemplate(template);
foreach (var t in replacedTiles.Keys)
world.customTerrain[t.X, t.Y] = this;
if (cachedTheater != world.Map.Theater)
{
cachedTheater = world.Map.Theater;
sprites = new Cache<TileReference, Sprite>(
x => SheetBuilder.Add(world.TileSet.GetBytes(x),
new Size(Game.CellSize, Game.CellSize)));
}
var numStates = self.Info.Traits.Get<BridgeInfo>().Long ? 6 : 3;
for (var n = 0; n < numStates; n++)
{
var stateTemplate = world.TileSet.Walkability.GetWalkability(NameFromState(template, n));
Templates.Add( stateTemplate );
TileSprites.Add(replacedTiles.ToDictionary(
a => a.Key,
a => sprites[new TileReference { tile = (ushort)stateTemplate.Index, image = (byte)a.Value }]));
}
self.Health = (int)(self.GetMaxHP() * template.HP);
}
Bridge GetNeighbor(World world, int[] offset)
{
if (offset == null) return null;
var pos = self.Location + new int2(offset[0], offset[1]);
if (!world.Map.IsInMap(pos.X, pos.Y)) return null;
return world.customTerrain[pos.X, pos.Y] as Bridge;
}
public void FinalizeBridges(World world)
{
// go looking for our neighbors, if this is a long bridge.
var info = self.Info.Traits.Get<BridgeInfo>();
if (info.NorthOffset != null)
northNeighbour = GetNeighbor(world, info.NorthOffset);
if (info.SouthOffset != null)
southNeighbour = GetNeighbor(world, info.SouthOffset);
}
public float GetCost(int2 p, UnitMovementType umt)
{
// just use the standard walkability from templates.ini. no hackery.
return TerrainCosts.Cost(umt,
Templates[state].TerrainType[Tiles[p]]);
}
bool IsIntact(Bridge b)
{
return b != null && b.self.IsInWorld && b.self.Health > 0;
}
bool IsLong(Bridge b)
{
return b != null && b.self.IsInWorld && b.self.Info.Traits.Get<BridgeInfo>().Long;
}
void UpdateState()
{
var ds = self.GetDamageState();
if (!self.Info.Traits.Get<BridgeInfo>().Long)
{
state = (int)ds;
return;
}
bool waterToSouth = !IsIntact(southNeighbour) && (!IsLong(southNeighbour) || !IsIntact(this));
bool waterToNorth = !IsIntact(northNeighbour) && (!IsLong(northNeighbour) || !IsIntact(this));
if (waterToSouth && waterToNorth) { state = 5; return; }
if (waterToNorth) { state = 4; return; }
if (waterToSouth) { state = 3; return; }
state = (int)ds;
}
public void Damaged(Actor self, AttackInfo e)
{
if (e.DamageStateChanged)
{
UpdateState();
if (northNeighbour != null) northNeighbour.UpdateState();
if (southNeighbour != null) southNeighbour.UpdateState();
}
}
}
}

View File

@@ -9,7 +9,7 @@ namespace OpenRa.Traits
public readonly int[] SpawnOffset = null;
public readonly string[] Produces = { };
public object Create(Actor self) { return new Production(self); }
public virtual object Create(Actor self) { return new Production(self); }
}
class Production : IIssueOrder, IResolveOrder, IProducer, ITags

View File

@@ -3,9 +3,9 @@ using OpenRa.GameRules;
namespace OpenRa.Traits
{
class ProductionSurroundInfo : ITraitInfo
class ProductionSurroundInfo : ProductionInfo
{
public object Create(Actor self) { return new ProductionSurround(self); }
public override object Create(Actor self) { return new ProductionSurround(self); }
}
class ProductionSurround : Production

View File

@@ -25,6 +25,8 @@ namespace OpenRa.Traits
public interface IAcceptThief { void OnSteal(Actor self, Actor thief); }
public interface IAcceptSpy { void OnInfiltrate(Actor self, Actor spy); }
public interface ICustomTerrain { float GetCost(int2 p, UnitMovementType umt); }
interface IProducer
{
bool Produce( Actor self, ActorInfo producee );

View File

@@ -4,6 +4,7 @@ using OpenRa.Effects;
using OpenRa.Support;
using OpenRa.FileFormats;
using OpenRa.Graphics;
using OpenRa.Traits;
namespace OpenRa
{
@@ -21,6 +22,9 @@ namespace OpenRa
public readonly Map Map;
public readonly TileSet TileSet;
// for tricky things like bridges.
public readonly ICustomTerrain[,] customTerrain = new ICustomTerrain[128, 128];
public readonly WorldRenderer WorldRenderer;
internal readonly Minimap Minimap;
@@ -40,10 +44,11 @@ namespace OpenRa
oreTicks = oreFrequency;
Map.InitOreDensity();
PathFinder = new PathFinder(this);
CreateActor("World", new int2(int.MaxValue, int.MaxValue), null);
Bridges.MakeBridges(this);
PathFinder = new PathFinder(this);
WorldRenderer = new WorldRenderer(this, Game.renderer);
Minimap = new Minimap(this, Game.renderer);
}