Killed that fail TechTree, and added a new one to Rules.
This commit is contained in:
@@ -29,7 +29,7 @@ namespace OpenRa.Game.GameRules
|
||||
return x;
|
||||
|
||||
else if (fieldType.IsEnum)
|
||||
return Enum.Parse(fieldType, x);
|
||||
return Enum.Parse(fieldType, x, true);
|
||||
|
||||
else if (fieldType == typeof(bool))
|
||||
return ParseYesNo(x);
|
||||
|
||||
@@ -16,6 +16,7 @@ namespace OpenRa.Game
|
||||
public static InfoLoader<WeaponInfo> WeaponInfo;
|
||||
public static InfoLoader<WarheadInfo> WarheadInfo;
|
||||
public static InfoLoader<ProjectileInfo> ProjectileInfo;
|
||||
public static TechTree TechTree;
|
||||
|
||||
public static void LoadRules( string mapFileName )
|
||||
{
|
||||
@@ -52,6 +53,8 @@ namespace OpenRa.Game
|
||||
|
||||
ProjectileInfo = new InfoLoader<ProjectileInfo>(
|
||||
Pair.New<string, Func<string, ProjectileInfo>>("Projectile", _ => new ProjectileInfo()));
|
||||
|
||||
TechTree = new TechTree();
|
||||
}
|
||||
|
||||
static void LoadCategories( params string[] types )
|
||||
|
||||
65
OpenRa.Game/GameRules/TechTree.cs
Executable file
65
OpenRa.Game/GameRules/TechTree.cs
Executable file
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using IjwFramework.Collections;
|
||||
|
||||
namespace OpenRa.Game.GameRules
|
||||
{
|
||||
class TechTree
|
||||
{
|
||||
readonly Cache<string, List<UnitInfo.BuildingInfo>> producesIndex = new Cache<string, List<UnitInfo.BuildingInfo>>( x => new List<UnitInfo.BuildingInfo>() );
|
||||
|
||||
public TechTree()
|
||||
{
|
||||
foreach( var b in Rules.Categories[ "Building" ] )
|
||||
{
|
||||
var info = (UnitInfo.BuildingInfo)Rules.UnitInfo[ b ];
|
||||
foreach( var p in info.Produces )
|
||||
producesIndex[ p ].Add( info );
|
||||
}
|
||||
}
|
||||
|
||||
public Cache<string, List<Actor>> GatherBuildings( Player player )
|
||||
{
|
||||
var ret = new Cache<string, List<Actor>>( x => new List<Actor>() );
|
||||
foreach( var b in Game.world.Actors.Where( x => x.Owner == player && Rules.UnitCategory[ x.unitInfo.Name ] == "Building" ) )
|
||||
ret[ b.unitInfo.Name ].Add( b );
|
||||
return ret;
|
||||
}
|
||||
|
||||
public bool CanBuild( UnitInfo unit, Player player, Cache<string, List<Actor>> playerBuildings )
|
||||
{
|
||||
if( unit.TechLevel == -1 )
|
||||
return false;
|
||||
|
||||
if( !unit.Owner.Any( x => x == player.Race ) )
|
||||
return false;
|
||||
|
||||
foreach( var p in unit.Prerequisite )
|
||||
if( playerBuildings[ p ].Count == 0 )
|
||||
return false;
|
||||
|
||||
if( producesIndex[ Rules.UnitCategory[ unit.Name ] ].All( x => playerBuildings[ x.Name ].Count == 0 ) )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public IEnumerable<string> BuildableItems( Player player, params string[] categories )
|
||||
{
|
||||
var playerBuildings = GatherBuildings( player );
|
||||
foreach( var unit in categories.SelectMany( x => Rules.Categories[ x ] ).Select( x => Rules.UnitInfo[ x ] ) )
|
||||
if( CanBuild( unit, player, playerBuildings ) )
|
||||
yield return unit.Name;
|
||||
}
|
||||
|
||||
public IEnumerable<string> UnitBuiltAt( UnitInfo info )
|
||||
{
|
||||
if( info.BuiltAt.Length != 0 )
|
||||
return info.BuiltAt;
|
||||
else
|
||||
return producesIndex[ Rules.UnitCategory[ info.Name ] ].Select( x => x.Name );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ namespace OpenRa.Game.GameRules
|
||||
public readonly int GuardRange = -1; // -1 = use weapon's range
|
||||
public readonly string Image = null; // sprite-set to use when rendering
|
||||
public readonly bool Invisible = false;
|
||||
public readonly string[] Owner = { "allies", "soviet" };
|
||||
public readonly Race[] Owner = { Race.Allies, Race.Soviet };
|
||||
public readonly int Points = 0;
|
||||
public readonly string[] Prerequisite = { };
|
||||
public readonly string Primary = null;
|
||||
@@ -81,6 +81,7 @@ namespace OpenRa.Game.GameRules
|
||||
{
|
||||
public readonly int2 Dimensions = new int2( 1, 1 );
|
||||
public readonly string Footprint = "x";
|
||||
public readonly string[] Produces = { };
|
||||
|
||||
public readonly bool BaseNormal = true;
|
||||
public readonly int Adjacent = 1;
|
||||
|
||||
Reference in New Issue
Block a user