much simpler rules loader

This commit is contained in:
Chris Forbes
2010-04-01 21:31:15 +13:00
parent c5fdda9141
commit 38605faecf
16 changed files with 110 additions and 120 deletions

View File

@@ -27,7 +27,7 @@ using OpenRA.FileFormats;
namespace OpenRA
{
static class Combat /* some utility bits that are shared between various things */
public static class Combat /* some utility bits that are shared between various things */
{
static string GetImpactSound(WarheadInfo warhead, bool isWater)
{
@@ -82,6 +82,23 @@ namespace OpenRA
}
}
public static void DoExplosion(Actor attacker, string weapontype, int2 location, int altitude)
{
var args = new ProjectileArgs
{
src = location,
dest = location,
srcAltitude = altitude,
destAltitude = altitude,
firedBy = attacker,
target = null,
weapon = Rules.Weapons[ weapontype.ToLowerInvariant() ],
facing = 0
};
DoImpacts(args, location);
}
static float GetDamageToInflict(Actor target, ProjectileArgs args, WarheadInfo warhead, float modifier)
{
var distance = (target.CenterLocation - args.dest).Length;

View File

@@ -1,51 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using OpenRA.FileFormats;
namespace OpenRA.GameRules
{
public class InfoLoader<T> : IEnumerable<KeyValuePair<string, T>>
{
readonly Dictionary<string, T> infos = new Dictionary<string, T>();
public InfoLoader(params Pair<string, Func<string,T>>[] srcs)
{
foreach (var src in srcs)
foreach (var name in Rules.Categories[src.First])
{
var t = src.Second(name);
FieldLoader.Load(t, Rules.AllRules.GetSection(name));
infos[name] = t;
}
}
public T this[string name]
{
get { return infos[name.ToLowerInvariant()]; }
}
public IEnumerator<KeyValuePair<string, T>> GetEnumerator() { return infos.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return infos.GetEnumerator(); }
}
}

View File

@@ -29,13 +29,11 @@ namespace OpenRA
public static class Rules
{
public static IniFile AllRules;
public static Dictionary<string, List<string>> Categories = new Dictionary<string, List<string>>();
public static InfoLoader<WarheadInfo> WarheadInfo;
public static InfoLoader<VoiceInfo> VoiceInfo;
public static TechTree TechTree;
public static Dictionary<string, ActorInfo> Info;
public static Dictionary<string, WeaponInfo> Weapons;
public static Dictionary<string, VoiceInfo> Voices;
public static void LoadRules(string map, Manifest m)
{
@@ -43,38 +41,21 @@ namespace OpenRA
legacyRules.Insert(0, map);
AllRules = new IniFile(legacyRules.Select(a => FileSystem.Open(a)).ToArray());
LoadCategories(
"Weapon",
"Warhead",
"Projectile",
"Voice");
WarheadInfo = new InfoLoader<WarheadInfo>(
Pair.New<string, Func<string, WarheadInfo>>("Warhead", _ => new WarheadInfo()));
VoiceInfo = new InfoLoader<VoiceInfo>(
Pair.New<string, Func<string, VoiceInfo>>("Voice", _ => new VoiceInfo()));
Log.Write("Using rules files: ");
foreach (var y in m.Rules)
Log.Write(" -- {0}", y);
var yamlRules = m.Rules.Select(a => MiniYaml.FromFile(a)).Aggregate(MiniYaml.Merge);
Info = new Dictionary<string, ActorInfo>();
foreach( var kv in yamlRules )
Info.Add(kv.Key.ToLowerInvariant(), new ActorInfo(kv.Key.ToLowerInvariant(), kv.Value, yamlRules));
var weaponsYaml = m.Weapons.Select(a => MiniYaml.FromFile(a)).Aggregate(MiniYaml.Merge);
Weapons = new Dictionary<string, WeaponInfo>();
foreach (var kv in weaponsYaml)
Weapons.Add(kv.Key.ToLowerInvariant(), new WeaponInfo(kv.Key.ToLowerInvariant(), kv.Value));
Info = LoadYamlRules(m.Rules, (k, y) => new ActorInfo(k.Key.ToLowerInvariant(), k.Value, y));
Weapons = LoadYamlRules(m.Weapons, (k, _) => new WeaponInfo(k.Key.ToLowerInvariant(), k.Value));
Voices = LoadYamlRules(m.Voices, (k, _) => new VoiceInfo(k.Value));
TechTree = new TechTree();
}
static void LoadCategories(params string[] types)
static Dictionary<string, T> LoadYamlRules<T>(string[] files, Func<KeyValuePair<string, MiniYaml>, Dictionary<string, MiniYaml>, T> f)
{
foreach (var t in types)
Categories[t] = AllRules.GetSection(t + "Types").Select(x => x.Key.ToLowerInvariant()).ToList();
var y = files.Select(a => MiniYaml.FromFile(a)).Aggregate(MiniYaml.Merge);
return y.ToDictionary(kv => kv.Key.ToLowerInvariant(), kv => f(kv, y));
}
}
}

View File

@@ -34,8 +34,10 @@ namespace OpenRA.GameRules
public readonly Lazy<Dictionary<string, VoicePool>> Pools;
public VoiceInfo()
public VoiceInfo( MiniYaml y )
{
FieldLoader.Load(this, y);
Pools = Lazy.New(() =>
new Dictionary<string, VoicePool>
{

View File

@@ -153,7 +153,6 @@
<Compile Include="Cursor.cs" />
<Compile Include="Effects\Explosion.cs" />
<Compile Include="GameRules\Footprint.cs" />
<Compile Include="GameRules\InfoLoader.cs" />
<Compile Include="GameRules\Rules.cs" />
<Compile Include="Graphics\Animation.cs" />
<Compile Include="Game.cs" />

View File

@@ -102,16 +102,6 @@ namespace OpenRA
music.Volume = value;
}
}
//public static void SeekMusic(uint delta)
//{
// if (music != null)
// {
// music.PlayPosition += delta;
// if (music.PlayPosition < 0 || music.PlayPosition > music.PlayLength)
// music.PlayPosition = 0;
// }
//}
public static void PlayVoice(string phrase, Actor voicedUnit)
{
@@ -120,7 +110,7 @@ namespace OpenRA
var mi = voicedUnit.Info.Traits.GetOrDefault<SelectableInfo>();
if (mi == null) return;
var vi = Rules.VoiceInfo[mi.Voice];
var vi = Rules.Voices[mi.Voice.ToLowerInvariant()];
var clip = vi.Pools.Value[phrase].GetNext();
if (clip == null)

View File

@@ -44,7 +44,7 @@ namespace OpenRA.Traits
if (--ticks <= 0)
{
ticks = info.Ticks;
self.InflictDamage(self, -info.Step, Rules.WarheadInfo["Super"]);
self.InflictDamage(self, -info.Step, null);
}
}
}

View File

@@ -51,7 +51,7 @@ namespace OpenRA.Traits.Activities
return this;
}
self.InflictDamage(self, -hpToRepair, Rules.WarheadInfo["Super"]);
self.InflictDamage(self, -hpToRepair, null);
if (self.Health == hp)
return NextActivity;

View File

@@ -135,7 +135,7 @@ namespace OpenRA.Traits
}
self.World.AddFrameEndTask(w => w.Add(new RepairIndicator(self)));
self.InflictDamage(self, -hpToRepair, Rules.WarheadInfo["Super"]);
self.InflictDamage(self, -hpToRepair, null);
if (self.Health == maxHP)
{
isRepairing = false;

View File

@@ -35,7 +35,8 @@ namespace OpenRA.Traits
public void OnCrush(Actor crusher)
{
self.InflictDamage(crusher, self.Health, Rules.WarheadInfo["Crush"]);
// ... this wasnt working ANYWAY ...
// self.InflictDamage(crusher, self.Health, Rules.WarheadInfo["Crush"]);
}
public bool IsPathableCrush(UnitMovementType umt, Player player)