generic info loader, projectiles

This commit is contained in:
Chris Forbes
2009-10-14 18:55:34 +13:00
parent 6432ba8779
commit 60a24a5415
9 changed files with 119 additions and 123 deletions

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.FileFormats;
using OpenRa.Game.Graphics;
using IjwFramework.Types;
namespace OpenRa.Game.GameRules
{
class InfoLoader<T>
{
readonly Dictionary<string, T> infos = new Dictionary<string, T>();
public InfoLoader(IniFile rules, params Pair<string, Func<string,T>>[] srcs)
{
foreach (var src in srcs)
foreach (var s in Util.ReadAllLines(FileSystem.Open(src.First)))
{
var name = s.Split(',')[0];
var t = src.Second(name.ToLowerInvariant());
FieldLoader.Load(t, rules.GetSection(name));
infos[name.ToLowerInvariant()] = t;
}
}
public T this[string name]
{
get { return infos[name.ToLowerInvariant()]; }
}
}
}

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.GameRules
{
class ProjectileInfo
{
public readonly bool AA = false;
public readonly bool AG = false;
public readonly bool ASW = false;
public readonly bool Animates = false;
public readonly bool Arcing = false;
public readonly int Arm = 0;
public readonly bool Degenerates = false;
public readonly bool Dropping = false;
public readonly int Frames = 1;
public readonly bool Gigundo = false;
public readonly bool High = false;
public readonly string Image = null;
public readonly bool Inaccurate = false;
public readonly bool Inviso = false;
public readonly bool Parachuted = false;
public readonly bool Proximity = false;
public readonly int ROT = 0;
public readonly bool Ranged = false;
public readonly bool Rotates = false;
public readonly bool Shadow = true;
public readonly bool Translucent = false;
public readonly bool UnderWater = false;
public readonly int RangeLimit = 0;
}
}

View File

@@ -3,23 +3,36 @@ using System.Collections.Generic;
using System.Linq;
using OpenRa.FileFormats;
using OpenRa.Game.GameRules;
using IjwFramework.Types;
namespace OpenRa.Game
{
static class Rules
{
public static UnitInfoLoader UnitInfo;
public static WeaponInfoLoader WeaponInfo;
public static WarheadInfoLoader WarheadInfo;
public static InfoLoader<UnitInfo> UnitInfo;
public static InfoLoader<WeaponInfo> WeaponInfo;
public static InfoLoader<WarheadInfo> WarheadInfo;
public static InfoLoader<ProjectileInfo> ProjectileInfo;
public static Footprint Footprint;
// TODO: load rules from the map, where appropriate.
public static void LoadRules()
{
var rulesIni = new IniFile(FileSystem.Open("rules.ini"));
UnitInfo = new UnitInfoLoader( rulesIni );
WeaponInfo = new WeaponInfoLoader( rulesIni );
WarheadInfo = new WarheadInfoLoader(rulesIni);
UnitInfo = new InfoLoader<UnitInfo>(rulesIni,
Pair.New<string,Func<string,UnitInfo>>( "buildings.txt", s => new UnitInfo.BuildingInfo(s)),
Pair.New<string, Func<string,UnitInfo>>("infantry.txt", s => new UnitInfo.InfantryInfo(s)),
Pair.New<string,Func<string,UnitInfo>>( "vehicles.txt", s => new UnitInfo.VehicleInfo(s)));
WeaponInfo = new InfoLoader<WeaponInfo>(rulesIni,
Pair.New<string,Func<string,WeaponInfo>>("weapons.txt", _ => new WeaponInfo()));
WarheadInfo = new InfoLoader<WarheadInfo>(rulesIni,
Pair.New<string,Func<string,WarheadInfo>>("warheads.txt", _ => new WarheadInfo()));
ProjectileInfo = new InfoLoader<ProjectileInfo>(rulesIni,
Pair.New<string, Func<string, ProjectileInfo>>("projectiles.txt", _ => new ProjectileInfo()));
Footprint = new Footprint(FileSystem.Open("footprint.txt"));
}
}

View File

@@ -7,44 +7,6 @@ using IjwFramework.Types;
namespace OpenRa.Game.GameRules
{
public class UnitInfoLoader
{
static Func<string, IniSection, UnitInfo> BindInfoCtor<T>()
where T : UnitInfo
{
var ctor = typeof( T ).GetConstructor( new[] { typeof( string ), typeof( IniSection ) } );
return ( s, i ) => (UnitInfo)ctor.Invoke( new object[] { s, i } );
}
readonly Dictionary<string, UnitInfo> unitInfos = new Dictionary<string, UnitInfo>();
public UnitInfoLoader( IniFile rules )
{
var srcs = new[]
{
Pair.New( "buildings.txt", BindInfoCtor<UnitInfo.BuildingInfo>() ),
Pair.New( "infantry.txt", BindInfoCtor<UnitInfo.InfantryInfo>() ),
Pair.New( "vehicles.txt", BindInfoCtor<UnitInfo.VehicleInfo>() ),
};
foreach( var src in srcs )
foreach( var s in Util.ReadAllLines( FileSystem.Open( src.First ) ) )
{
var unitName = s.Split( ',' )[ 0 ];
unitInfos.Add( unitName.ToLowerInvariant(),
src.Second( unitName, rules.GetSection( unitName ) ) );
}
}
public UnitInfo this[ string unitName ]
{
get
{
return unitInfos[ unitName.ToLowerInvariant() ];
}
}
}
public class UnitInfo
{
public enum ArmorType
@@ -81,22 +43,14 @@ namespace OpenRa.Game.GameRules
public readonly int Strength = 1;
public readonly int TechLevel = -1;
public UnitInfo( string name, IniSection ini )
{
Name = name.ToLowerInvariant();
FieldLoader.Load( this, ini );
}
public UnitInfo(string name) { Name = name; }
public class MobileInfo : UnitInfo
{
public readonly int Passengers = 0;
public readonly int Speed = 0;
public MobileInfo( string name, IniSection ini )
: base( name, ini )
{
}
public MobileInfo(string name) : base(name) { }
}
public class InfantryInfo : MobileInfo
@@ -107,10 +61,7 @@ namespace OpenRa.Game.GameRules
public readonly bool Infiltrate = false;
public readonly bool IsCanine = false;
public InfantryInfo( string name, IniSection ini )
: base( name, ini )
{
}
public InfantryInfo(string name) : base(name) { }
}
public class VehicleInfo : MobileInfo
@@ -119,10 +70,7 @@ namespace OpenRa.Game.GameRules
public readonly bool Tracked = false;
public readonly bool NoMovingFire = false;
public VehicleInfo( string name, IniSection ini )
: base( name, ini )
{
}
public VehicleInfo(string name) : base(name) { }
}
public class BuildingInfo : UnitInfo
@@ -138,10 +86,7 @@ namespace OpenRa.Game.GameRules
public readonly bool Unsellable = false;
public readonly bool WaterBound = false;
public BuildingInfo( string name, IniSection ini )
: base( name, ini )
{
}
public BuildingInfo(string name) : base(name) { }
}
/*

View File

@@ -7,29 +7,6 @@ using OpenRa.Game.Graphics;
namespace OpenRa.Game.GameRules
{
class WarheadInfoLoader
{
readonly Dictionary<string, WarheadInfo> warheadInfos = new Dictionary<string, WarheadInfo>();
public WarheadInfoLoader(IniFile rules)
{
foreach (var s in Util.ReadAllLines(FileSystem.Open("warheads.txt")))
{
var unitName = s.Split(',')[0];
warheadInfos.Add(unitName.ToLowerInvariant(),
new WarheadInfo(rules.GetSection(unitName)));
}
}
public WarheadInfo this[string unitName]
{
get
{
return warheadInfos[unitName.ToLowerInvariant()];
}
}
}
class WarheadInfo
{
public readonly int Spread = 1;
@@ -39,10 +16,5 @@ namespace OpenRa.Game.GameRules
public readonly bool Ore = false;
public readonly int Explosion = 0;
public readonly int InfDeath = 0;
public WarheadInfo(IniSection ini)
{
FieldLoader.Load(this, ini);
}
}
}

View File

@@ -6,29 +6,6 @@ using OpenRa.Game.Graphics;
namespace OpenRa.Game.GameRules
{
class WeaponInfoLoader
{
readonly Dictionary<string, WeaponInfo> weaponInfos = new Dictionary<string, WeaponInfo>();
public WeaponInfoLoader( IniFile rules )
{
foreach( var s in Util.ReadAllLines( FileSystem.Open( "weapons.txt" ) ) )
{
var unitName = s.Split( ',' )[ 0 ];
weaponInfos.Add( unitName.ToLowerInvariant(),
new WeaponInfo( rules.GetSection( unitName ) ) );
}
}
public WeaponInfo this[ string unitName ]
{
get
{
return weaponInfos[ unitName.ToLowerInvariant() ];
}
}
}
class WeaponInfo
{
public readonly string Anim = null;
@@ -44,10 +21,5 @@ namespace OpenRa.Game.GameRules
public readonly bool Supress = false;
public readonly bool TurboBoost = false;
public readonly string Warhead = null;
public WeaponInfo( IniSection ini )
{
FieldLoader.Load( this, ini );
}
}
}

View File

@@ -78,6 +78,8 @@
<Compile Include="Controller.cs" />
<Compile Include="GameRules\FieldLoader.cs" />
<Compile Include="GameRules\Footprint.cs" />
<Compile Include="GameRules\InfoLoader.cs" />
<Compile Include="GameRules\ProjectileInfo.cs" />
<Compile Include="GameRules\Rules.cs" />
<Compile Include="GameRules\UnitInfo.cs" />
<Compile Include="GameRules\WarheadInfo.cs" />

18
projectiles.txt Normal file
View File

@@ -0,0 +1,18 @@
Invisible
LeapDog
Cannon
Ack
Torpedo
FROG
HeatSeeker
LaserGuided
AAMissile
Lobbed
Catapult
Bomblet
Ballistic
Parachute
GPSSatellite
NukeUp
NukeDown
Fireball

8
warheads.txt Normal file
View File

@@ -0,0 +1,8 @@
SA
HE
AP
Fire
HollowPoint
Super
Organic
Nuke