categorize traits better

This commit is contained in:
Chris Forbes
2010-02-09 13:42:33 +13:00
parent 8cb01df8cd
commit 5a95048396
49 changed files with 49 additions and 49 deletions

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Traits
{
class BridgeLoadHookInfo : StatelessTraitInfo<BridgeLoadHook> { }
class BridgeLoadHook : ILoadWorldHook
{
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.Queries.WithTraitMultiple<Bridge>())
br.Trait.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;
}
public void WorldLoaded(World w) { MakeBridges(w); }
}
}

View 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];
}
}}

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Traits
{
class ChoosePaletteOnSelectInfo : StatelessTraitInfo<ChoosePaletteOnSelect> { }
class ChoosePaletteOnSelect : INotifySelection
{
public void SelectionChanged()
{
var firstItem = Game.controller.selection.Actors.FirstOrDefault(
a => a.World.LocalPlayer == a.Owner && a.traits.Contains<Production>());
if (firstItem == null)
return;
var produces = firstItem.Info.Traits.Get<ProductionInfo>().Produces.FirstOrDefault();
if (produces == null)
return;
Game.chrome.SetCurrentTab(produces);
}
}
}

View File

@@ -0,0 +1,41 @@
using System.Drawing;
using OpenRa.Graphics;
namespace OpenRa.Traits
{
class ChronoshiftPaletteEffectInfo : StatelessTraitInfo<ChronoshiftPaletteEffect> { }
public class ChronoshiftPaletteEffect : IPaletteModifier, ITick
{
const int chronoEffectLength = 20;
int remainingFrames;
public void DoChronoshift()
{
remainingFrames = chronoEffectLength;
}
public void Tick(Actor self)
{
if (remainingFrames > 0)
remainingFrames--;
}
public void AdjustPalette(Bitmap b)
{
if (remainingFrames == 0)
return;
/* TODO: FIX ME
var frac = (float)remainingFrames / chronoEffectLength;
for( var y = 0; y < (int)PaletteType.Chrome; y++ )
for (var x = 0; x < 256; x++)
{
var orig = b.GetPixel(x, y);
var lum = (int)(255 * orig.GetBrightness());
var desat = Color.FromArgb(orig.A, lum, lum, lum);
b.SetPixel(x, y, Graphics.Util.Lerp(frac, orig, desat));
}
*/
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Traits
{
class CrateSpawnerInfo : ITraitInfo
{
public readonly int CrateMinimum = 1; // Minumum number of crates
public readonly int CrateMaximum = 255; // Maximum number of crates
public readonly int CrateRadius = 3; // Radius of crate effect TODO: This belongs on the crate effect itself
public readonly int CrateRegen = 180; // Average time (seconds) between crate spawn
public readonly float WaterCrateChance = 0.2f; // Change of generating a water crate instead of a land crate
public object Create(Actor self) { return new CrateSpawner(self); }
}
class CrateSpawner
{
Actor self;
public CrateSpawner(Actor self)
{
this.self = self;
}
}
}

View File

@@ -0,0 +1,25 @@
using System.Drawing;
namespace OpenRa.Traits
{
class LightPaletteRotatorInfo : StatelessTraitInfo<LightPaletteRotator> { }
class LightPaletteRotator : ITick, IPaletteModifier
{
float t = 0;
public void Tick(Actor self)
{
t += .5f;
}
public void AdjustPalette(Bitmap b)
{
var rotate = (int)t % 18;
if (rotate > 9)
rotate = 18 - rotate;
using (var bitmapCopy = new Bitmap(b))
for (int j = 0; j < 16; j++)
b.SetPixel(0x67, j, b.GetPixel(230+rotate, j));
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.Graphics;
namespace OpenRa.Traits
{
class OreGrowthInfo : ITraitInfo
{
public readonly float Interval = 1f;
public readonly float Chance = .02f;
public readonly bool Spreads = true;
public readonly bool Grows = true;
public object Create(Actor self) { return new OreGrowth(); }
}
class OreGrowth : ITick, ILoadWorldHook
{
int remainingTicks;
public void Tick(Actor self)
{
if (--remainingTicks <= 0)
{
var info = self.Info.Traits.Get<OreGrowthInfo>();
if (info.Spreads)
Ore.SpreadOre(self.World,
Game.SharedRandom,
info.Chance);
if (info.Grows)
Ore.GrowOre(self.World, Game.SharedRandom);
self.World.Minimap.InvalidateOre();
remainingTicks = (int)(info.Interval * 60 * 25);
}
}
public void WorldLoaded(World w)
{
Ore.InitOreDensity(w.Map);
}
}
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRa.FileFormats;
namespace OpenRa.Traits
{
class PaletteFromFileInfo : ITraitInfo
{
public readonly string Name = null;
public readonly string Theater = null;
public readonly string Filename = null;
public object Create(Actor self) { return new PaletteFromFile(self, this); }
}
class PaletteFromFile
{
public PaletteFromFile(Actor self, PaletteFromFileInfo info)
{
if (info.Theater == null ||
info.Theater.ToLowerInvariant() == self.World.Map.Theater.ToLowerInvariant())
{
//Log.Write("Loading palette {0} from file {1}", info.Name, info.Filename);
self.World.WorldRenderer.AddPalette(info.Name, new Palette(FileSystem.Open(info.Filename)));
}
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using OpenRa.FileFormats;
namespace OpenRa.Traits
{
class PaletteFromRGBAInfo : ITraitInfo
{
public readonly string Name = null;
public readonly string Theatre = null;
public readonly int R = 0;
public readonly int G = 0;
public readonly int B = 0;
public readonly int A = 255;
public object Create(Actor self) { return new PaletteFromRGBA(self, this); }
}
class PaletteFromRGBA
{
public PaletteFromRGBA(Actor self, PaletteFromRGBAInfo info)
{
if (info.Theatre == null ||
info.Theatre.ToLowerInvariant() == self.World.Map.Theater.ToLowerInvariant())
{
Log.Write("Loading palette {0} from RGBA {1} {2} {3} {4}",info.Name,info.R,info.G,info.B,info.A);
// TODO: This shouldn't rely on a base palette
var wr = self.World.WorldRenderer;
var pal = wr.GetPalette("player0");
wr.AddPalette(info.Name, new Palette(pal, new SingleColorRemap(Color.FromArgb(info.A, info.R, info.G, info.B))));
}
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRa.FileFormats;
namespace OpenRa.Traits
{
class PaletteFromRemapInfo : ITraitInfo
{
public readonly string Name = null;
public readonly string Theatre = null;
public readonly string BasePalette = null;
public readonly string Remap = null;
public object Create(Actor self) { return new PaletteFromRemap(self, this); }
}
class PaletteFromRemap
{
public PaletteFromRemap(Actor self, PaletteFromRemapInfo info)
{
if (info.Theatre == null ||
info.Theatre.ToLowerInvariant() == self.World.Map.Theater.ToLowerInvariant())
{
Log.Write("Loading palette {0} from theatre {1} with remap {2}", info.Name, info.BasePalette, info.Remap);
var wr = self.World.WorldRenderer;
var pal = wr.GetPalette(info.BasePalette);
var newpal = (info.Remap == null) ? pal : new Palette(pal, new PlayerColorRemap(FileSystem.Open(info.Remap)));
wr.AddPalette(info.Name, newpal);
}
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using OpenRa.FileFormats;
namespace OpenRa.Traits
{
class PlayerColorPaletteInfo : ITraitInfo
{
public readonly string Name = null;
public readonly string DisplayName = null;
public readonly string BasePalette = null;
public readonly string Remap = null;
public readonly int[] DisplayColor = null;
public object Create(Actor self) { return new PlayerColorPalette(self, this); }
}
class PlayerColorPalette
{
public PlayerColorPalette(Actor self, PlayerColorPaletteInfo info)
{
var wr = self.World.WorldRenderer;
var pal = wr.GetPalette(info.BasePalette);
var newpal = (info.Remap == null) ? pal : new Palette(pal, new PlayerColorRemap(FileSystem.Open(info.Remap)));
wr.AddPalette(info.Name, newpal);
Player.RegisterPlayerColor(info.Name, info.DisplayName, Color.FromArgb(info.DisplayColor[0], info.DisplayColor[1], info.DisplayColor[2]));
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using OpenRa.FileFormats;
namespace OpenRa.Traits
{
class ShroudPaletteInfo : ITraitInfo
{
public object Create(Actor self) { return new ShroudPalette(self); }
}
class ShroudPalette
{
public ShroudPalette(Actor self)
{
// TODO: This shouldn't rely on a base palette
var wr = self.World.WorldRenderer;
var pal = wr.GetPalette("terrain");
wr.AddPalette("shroud", new Palette(pal, new ShroudPaletteRemap()));
}
}
}

View 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 aa in self.World.Queries.WithTrait<ICrushable>())
{
var a = aa.Actor;
// 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( var t in self.World.Queries.WithTraitMultiple<IOccupySpace>() )
foreach( var cell in t.Trait.OccupiedCells() )
if (!influence[cell.X, cell.Y].Contains(t.Actor))
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);
}
}
}

View File

@@ -0,0 +1,24 @@
using System.Drawing;
namespace OpenRa.Traits
{
class WaterPaletteRotationInfo : StatelessTraitInfo<WaterPaletteRotation> { }
class WaterPaletteRotation : ITick, IPaletteModifier
{
float t = 0;
public void Tick(Actor self)
{
t += .25f;
}
public void AdjustPalette(Bitmap b)
{
var rotate = (int)t % 7;
using (var bitmapCopy = new Bitmap(b))
for (int j = 0; j < 16; j++)
for (int i = 0; i < 7; i++)
b.SetPixel(0x60 + (rotate + i) % 7, j, bitmapCopy.GetPixel(0x60 + i, j));
}
}
}