First half of bridge overhaul

This commit is contained in:
Paul Chote
2010-06-27 01:09:06 +12:00
parent 9a34b049a8
commit c5718456fd
4 changed files with 182 additions and 173 deletions

View File

@@ -33,121 +33,119 @@ namespace OpenRA.Mods.RA
{ {
public readonly bool Long = false; public readonly bool Long = false;
public readonly string Template = null; public readonly ushort Template;
public readonly string DamagedTemplate = null; public readonly float DamagedThreshold = 0.5f;
public readonly string DestroyedTemplate = null; public readonly ushort DamagedTemplate;
public readonly ushort DestroyedTemplate;
// For long bridges // For long bridges
public readonly string DestroyedPlusNorthTemplate = null; public readonly ushort DestroyedPlusNorthTemplate;
public readonly string DestroyedPlusSouthTemplate = null; public readonly ushort DestroyedPlusSouthTemplate;
public readonly string DestroyedPlusBothTemplate = null; public readonly ushort DestroyedPlusBothTemplate;
public readonly bool UseAlternateNames = false; public readonly bool UseAlternateNames = false;
public readonly int[] NorthOffset = null; public readonly int[] NorthOffset = null;
public readonly int[] SouthOffset = null; public readonly int[] SouthOffset = null;
public object Create(ActorInitializer init) { return new Bridge(init.self); } public object Create(ActorInitializer init) { return new Bridge(init.self, this); }
public IEnumerable<ushort> Templates
{ get {
if (Template != 0)
yield return Template;
if (DamagedTemplate != 0)
yield return DamagedTemplate;
if (DestroyedTemplate != 0)
yield return DestroyedTemplate;
if (DestroyedPlusNorthTemplate != 0)
yield return DestroyedPlusNorthTemplate;
if (DestroyedPlusSouthTemplate != 0)
yield return DestroyedPlusSouthTemplate;
if (DestroyedPlusBothTemplate != 0)
yield return DestroyedPlusBothTemplate;
} }
} }
class Bridge //: IRender, INotifyDamage class Bridge: IRender, 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 cachedTileset; static string cachedTileset;
static Cache<TileReference<ushort,byte>, Sprite> sprites; static Cache<TileReference<ushort,byte>, Sprite> sprites;
public IEnumerable<Renderable> Render(Actor self) Dictionary<ushort, Dictionary<int2, Sprite>> TileSprites = new Dictionary<ushort, Dictionary<int2, Sprite>>();
Dictionary<ushort, TileTemplate> Templates = new Dictionary<ushort, TileTemplate>();
ushort currentTemplate;
Actor self;
BridgeInfo info;
Bridge northNeighbour, southNeighbour;
public Bridge(Actor self, BridgeInfo info)
{ {
foreach (var t in TileSprites[state]) this.self = self;
yield return new Renderable(t.Value, Game.CellSize * t.Key, "terrain"); self.RemoveOnDeath = false;
this.info = info;
} }
public void FinalizeBridges(World world, Bridge[,] bridges) public void Create(ushort template, Dictionary<int2, byte> subtiles)
{ {
// go looking for our neighbors, if this is a long bridge. currentTemplate = template;
var info = self.Info.Traits.Get<BridgeInfo>(); if (template == info.DamagedTemplate)
if (info.NorthOffset != null) self.Health = (int)(info.DamagedThreshold*self.GetMaxHP());
northNeighbour = GetNeighbor(world, info.NorthOffset, bridges); else if (template != info.Template)
if (info.SouthOffset != null) self.Health = 0;
southNeighbour = GetNeighbor(world, info.SouthOffset, bridges);
} // Create a new cache to store the tile data
if (cachedTileset != self.World.Map.Tileset)
public Bridge GetNeighbor(World world, int[] offset, Bridge[,] bridges)
{
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 bridges[pos.X, pos.Y];
}
public int StateFromTemplate(TileTemplate t)
{
/*
var info = self.Info.Traits.Get<BridgeInfo>();
if (info.UseAlternateNames)
{ {
if (t.Name.EndsWith("d")) return 2; cachedTileset = self.World.Map.Tileset;
if (t.Name.EndsWith("h")) return 1;
return 0;
}
else
return t.Name[t.Name.Length - 1] - 'a';
*/
return 0;
}
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');*/
return "";
}
public void SetTiles(World world, TileTemplate template, Dictionary<int2, int> replacedTiles)
{
/*
Tiles = replacedTiles;
state = StateFromTemplate(template);
if (cachedTileset != world.Map.Tileset)
{
cachedTileset = world.Map.Tileset;
sprites = new Cache<TileReference<ushort,byte>, Sprite>( sprites = new Cache<TileReference<ushort,byte>, Sprite>(
x => SheetBuilder.SharedInstance.Add(world.TileSet.GetBytes(x), x => SheetBuilder.SharedInstance.Add(self.World.TileSet.GetBytes(x),
new Size(Game.CellSize, Game.CellSize))); new Size(Game.CellSize, Game.CellSize)));
} }
var numStates = self.Info.Traits.Get<BridgeInfo>().Long ? 6 : 3; // Cache templates and tiles for the different states
for (var n = 0; n < numStates; n++) foreach (var t in info.Templates)
{ {
var stateTemplate = world.TileSet.Walkability.GetTileTemplate(NameFromState(template, n)); Templates.Add(t,self.World.TileSet.Templates[t]);
Templates.Add( stateTemplate ); TileSprites.Add(t,subtiles.ToDictionary(
TileSprites.Add(replacedTiles.ToDictionary(
a => a.Key, a => a.Key,
a => sprites[new TileReference<ushort,byte>((ushort)stateTemplate.Index, (byte)a.Value)])); a => sprites[new TileReference<ushort,byte>(t, (byte)a.Value)]));
} }
self.Health = (int)(self.GetMaxHP() * template.HP);
*/
} }
public string GetTerrainType(int2 cell) public string GetTerrainType(int2 cell)
{ {
return ""; var dx = cell - self.Location;
// Ugly hack until terraintypes are converted from enums var index = dx.X + Templates[currentTemplate].Size.X*dx.Y;
//return Enum.GetName( typeof(TerrainType), Templates[state].TerrainType[Tiles[cell]]); return self.World.TileSet.GetTerrainType(new TileReference<ushort, byte>(currentTemplate,(byte)index));
}
public void LinkNeighbouringBridges(World world, BridgeLayer bridges)
{
// go looking for our neighbors if this is a long bridge.
var info = self.Info.Traits.Get<BridgeInfo>();
if (info.NorthOffset != null)
northNeighbour = GetNeighbor(info.NorthOffset, bridges);
if (info.SouthOffset != null)
southNeighbour = GetNeighbor(info.SouthOffset, bridges);
}
public Bridge GetNeighbor(int[] offset, BridgeLayer bridges)
{
if (offset == null) return null;
return bridges.GetBridge(self.Location + new int2(offset[0], offset[1]));
}
public IEnumerable<Renderable> Render(Actor self)
{
foreach (var t in TileSprites[currentTemplate])
yield return new Renderable(t.Value, Game.CellSize * t.Key, "terrain");
} }
static bool IsIntact(Bridge b) static bool IsIntact(Bridge b)
@@ -162,7 +160,7 @@ namespace OpenRA.Mods.RA
void UpdateState() void UpdateState()
{ {
var ds = self.GetDamageState(); /*var ds = self.GetDamageState();
if (!self.Info.Traits.Get<BridgeInfo>().Long) if (!self.Info.Traits.Get<BridgeInfo>().Long)
{ {
state = (int)ds; state = (int)ds;
@@ -176,16 +174,17 @@ namespace OpenRA.Mods.RA
if (waterToNorth) { state = 4; return; } if (waterToNorth) { state = 4; return; }
if (waterToSouth) { state = 3; return; } if (waterToSouth) { state = 3; return; }
state = (int)ds; state = (int)ds;
*/
} }
public void Damaged(Actor self, AttackInfo e) public void Damaged(Actor self, AttackInfo e)
{ {
if (e.DamageStateChanged) /*if (e.DamageStateChanged)
{ {
UpdateState(); UpdateState();
if (northNeighbour != null) northNeighbour.UpdateState(); if (northNeighbour != null) northNeighbour.UpdateState();
if (southNeighbour != null) southNeighbour.UpdateState(); if (southNeighbour != null) southNeighbour.UpdateState();
} }*/
} }
} }
} }

View File

@@ -25,89 +25,108 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
class BridgeLayerInfo : TraitInfo<BridgeLayer> { } class BridgeLayerInfo : ITraitInfo
{
public readonly string[] Bridges = {"br1", "br2", "br3", "bridge1", "bridge2"};
public object Create(ActorInitializer init) { return new BridgeLayer(init.self, this); }
}
class BridgeLayer : ILoadWorldHook, ITerrainTypeModifier class BridgeLayer : ILoadWorldHook, ITerrainTypeModifier
{ {
// for tricky things like bridges. // for tricky things like bridges.
Bridge[,] bridges; Bridge[,] Bridges;
void MakeBridges(World w) readonly BridgeLayerInfo Info;
readonly World world;
public BridgeLayer(Actor self, BridgeLayerInfo Info)
{ {
this.Info = Info;
this.world = self.World;
}
static Dictionary<ushort, string> BridgeTypes;
public void WorldLoaded(World w)
{
Bridges = new Bridge[w.Map.MapSize.X, w.Map.MapSize.Y];
BridgeTypes = new Dictionary<ushort, string>();
// Build a list of templates that should be overlayed with bridges
foreach(var bridge in Info.Bridges)
{
var bi = Rules.Info[bridge].Traits.Get<BridgeInfo>();
foreach (var template in bi.Templates)
{
BridgeTypes.Add(template, bridge);
Log.Write("debug", "Adding template {0} for bridge {1}", template, bridge);
}
}
// Loop through the map looking for templates to overlay
var tl = w.Map.TopLeft; var tl = w.Map.TopLeft;
var br = w.Map.BottomRight; var br = w.Map.BottomRight;
for (int i = tl.X; i < br.X; i++) for (int i = tl.X; i < br.X; i++)
for (int j = tl.Y; j < br.Y; j++) for (int j = tl.Y; j < br.Y; j++)
if (IsBridge(w, w.Map.MapTiles[i, j].type)) if (BridgeTypes.Keys.Contains(w.Map.MapTiles[i, j].type))
ConvertBridgeToActor(w, i, j); ConvertBridgeToActor(w, i, j);
// Link adjacent (long)-bridges so that artwork is updated correctly
foreach (var b in w.Actors.SelectMany(a => a.traits.WithInterface<Bridge>())) foreach (var b in w.Actors.SelectMany(a => a.traits.WithInterface<Bridge>()))
b.FinalizeBridges(w, bridges); b.LinkNeighbouringBridges(w,this);
} }
void ConvertBridgeToActor(World w, int i, int j) void ConvertBridgeToActor(World w, int i, int j)
{ {
Log.Write("debug", "Converting bridge at {0} {1}", i, j); Log.Write("debug", "Converting bridge at {0} {1}", i, j);
/*
// This cell already has a bridge overlaying it from a previous iteration
if (Bridges[i,j] != null)
return;
// Correlate the tile "image" aka subtile with its position to find the template origin
var tile = w.Map.MapTiles[i, j].type; var tile = w.Map.MapTiles[i, j].type;
var image = w.Map.MapTiles[i, j].image; var image = w.Map.MapTiles[i, j].image;
var template = w.TileSet.walk[tile]; var template = w.TileSet.Templates[tile];
// base position of the tile
var ni = i - image % template.Size.X; var ni = i - image % template.Size.X;
var nj = j - image / template.Size.X; var nj = j - image / template.Size.X;
var replacedTiles = new Dictionary<int2, int>(); // Create a new actor for this bridge and keep track of which subtiles this bridge includes
var bridge = w.CreateActor(BridgeTypes[tile], new int2(ni, nj), w.WorldActor.Owner).traits.Get<Bridge>();
Dictionary<int2, byte> subTiles = new Dictionary<int2, byte>();
// Loop through the cells on the bridge template; mark each cell that is part
// of the bridge and add to the bridges array
for (var x = ni; x < ni + template.Size.X; x++) for (var x = ni; x < ni + template.Size.X; x++)
for (var y = nj; y < nj + template.Size.Y; y++) for (var y = nj; y < nj + template.Size.Y; y++)
{ {
var n = (x - ni) + template.Size.X * (y - nj); // This isn't the bridge we're looking for
if (!template.TerrainType.ContainsKey(n)) continue; if (!w.Map.IsInMap(x, y) || w.Map.MapTiles[x, y].type != tile)
continue;
if (w.Map.IsInMap(x, y))
if (w.Map.MapTiles[x, y].type == tile Log.Write("debug", "Adding tile {0} {1} for type {2}", x,y,tile);
&& w.Map.MapTiles[x, y].index == n)
{ subTiles.Add(new int2(x,y),w.Map.MapTiles[x, y].image);
// stash it Bridges[x,y] = bridge;
replacedTiles[new int2(x, y)] = w.Map.MapTiles[x, y].index;
// remove the tile from the actual map
w.Map.MapTiles[x, y].type = 0xfffe;
w.Map.MapTiles[x, y].index = 0;
w.Map.MapTiles[x, y].image = 0;
}
} }
if (replacedTiles.Any()) bridge.Create(tile, subTiles);
{
var a = w.CreateActor(template.Bridge, new int2(ni, nj), w.WorldActor.Owner);
var br = a.traits.Get<Bridge>();
foreach (var t in replacedTiles.Keys)
bridges[t.X, t.Y] = br;
br.SetTiles(w, template, replacedTiles);
}
*/
} }
public string GetTerrainType(int2 cell) public string GetTerrainType(int2 cell)
{ {
/*if (bridges[ cell.X, cell.Y ] != null) if (Bridges[ cell.X, cell.Y ] != null)
return bridges[ cell.X, cell.Y ].GetTerrainType(cell);*/ return Bridges[ cell.X, cell.Y ].GetTerrainType(cell);
return null; return null;
} }
static bool IsBridge(World w, ushort t) // Used to check for neighbouring bridges
public Bridge GetBridge(int2 cell)
{ {
return false; if (!world.Map.IsInMap(cell.X, cell.Y))
//return w.TileSet.walk[t].Bridge != null; return null;
}
return Bridges[ cell.X, cell.Y ];
public void WorldLoaded(World w)
{
bridges = new Bridge[w.Map.MapSize.X, w.Map.MapSize.Y];
MakeBridges(w);
} }
} }
} }

View File

@@ -392,34 +392,23 @@ MISS:
Bib: Bib:
BR1: BR1:
Inherits: ^Bridge
Bridge: Bridge:
Template: 235 Template: 235
DamagedTemplate: 236 DamagedTemplate: 236
DestroyedTemplate: 237 DestroyedTemplate: 237
SouthOffset: 0,2 SouthOffset: 0,2
Category: Building
Selectable:
BelowUnits:
Building:
Footprint: ____ ____
Dimensions: 4,2
HP: 1000
BR2: BR2:
Inherits: ^Bridge
Bridge: Bridge:
Template: 238 Template: 238
DamagedTemplate: 239 DamagedTemplate: 239
DestroyedTemplate: 240 DestroyedTemplate: 240
NorthOffset: 3,0 NorthOffset: 3,0
Category: Building
Selectable:
BelowUnits:
Building:
Footprint: ____ ____
Dimensions: 4,2
HP: 1000
BR3: BR3:
Inherits: ^Bridge
Bridge: Bridge:
Long: yes Long: yes
Template: 241 Template: 241
@@ -430,38 +419,28 @@ BR3:
DestroyedPlusBothTemplate: 246 DestroyedPlusBothTemplate: 246
NorthOffset: 2,0 NorthOffset: 2,0
SouthOffset: 0,1 SouthOffset: 0,1
Category: Building
Selectable:
BelowUnits:
Building:
Footprint: ____ ____
Dimensions: 4,2
HP: 1000
BRIDGE1: BRIDGE1:
Inherits: ^Bridge
Bridge: Bridge:
Template: 131 Template: 131
DamagedTemplate: 378 DamagedTemplate: 378
DestroyedTemplate: 132 DestroyedTemplate: 132
UseAlternateNames: yes UseAlternateNames: yes
Category: Building
Selectable:
BelowUnits:
Building: Building:
Footprint: _____ _____ _____ Footprint: _____ _____ _____
Dimensions: 5,3 Dimensions: 5,3
HP: 1000 Selectable:
Bounds: 120,48
BRIDGE2: BRIDGE2:
Inherits: ^Bridge
Bridge: Bridge:
Template: 133 Template: 133
DamagedTemplate: 379 DamagedTemplate: 379
DestroyedTemplate: 134 DestroyedTemplate: 134
UseAlternateNames: yes UseAlternateNames: yes
Category: Building
Selectable:
BelowUnits:
Building: Building:
Footprint: _____ _____ Footprint: _____ _____
Dimensions: 5,2 Dimensions: 5,2
HP: 1000 Selectable:
Bounds: 120,48

View File

@@ -137,3 +137,15 @@
HiddenUnderFog: HiddenUnderFog:
RevealsShroud: RevealsShroud:
Burns: Burns:
^Bridge:
Category: Building
Valued:
Description: Bridge
Selectable:
Bounds: 96,48
BelowUnits:
Building:
Footprint: ____ ____
Dimensions: 4,2
HP: 1000