Smarter unit spawn logic. Fixes #3496.

This commit is contained in:
Paul Chote
2013-07-12 20:21:55 +12:00
parent 2091a386fb
commit 7376cccf4d
8 changed files with 106 additions and 35 deletions

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
@@ -8,45 +8,74 @@
*/
#endregion
using System;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Mods.RA.Move;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class SpawnMPUnitsInfo : ITraitInfo, Requires<MPStartLocationsInfo>
public class SpawnMPUnitsInfo : TraitInfo<SpawnMPUnits>, Requires<MPStartLocationsInfo>, Requires<MPStartUnitsInfo> { }
public class SpawnMPUnits : IWorldLoaded
{
public readonly string InitialUnit = "mcv";
public readonly string Faction = null;
public object Create (ActorInitializer init) { return new SpawnMPUnits(this); }
}
class SpawnMPUnits : IWorldLoaded
{
SpawnMPUnitsInfo info;
public SpawnMPUnits(SpawnMPUnitsInfo info) { this.info = info; }
public void WorldLoaded(World world)
{
foreach (var s in world.WorldActor.Trait<MPStartLocations>().Start)
SpawnUnitsForPlayer(s.Key, s.Value);
SpawnUnitsForPlayer(world, s.Key, s.Value);
}
void SpawnUnitsForPlayer(Player p, CPos sp)
void SpawnUnitsForPlayer(World w, Player p, CPos sp)
{
// They don't want a MCV - the map provides something else for them
if (!p.PlayerReference.DefaultStartingUnits)
return; /* they don't want an mcv, the map provides something else for them */
/* support different starting units for each faction */
if (info.Faction != null && p.Country.Race != info.Faction)
return;
p.World.CreateActor(info.InitialUnit, new TypeDictionary
var spawnClass = p.PlayerReference.StartingUnitsClass ?? w.LobbyInfo.GlobalSettings.StartingUnitsClass;
var unitGroup = Rules.Info["world"].Traits.WithInterface<MPStartUnitsInfo>()
.Where(g => g.Class == spawnClass && g.Races != null && g.Races.Contains(p.Country.Race))
.RandomOrDefault(w.SharedRandom);
if (unitGroup == null)
throw new InvalidOperationException("No starting units defined for country {0} with class {1}".F(p.Country.Race, spawnClass));
// Spawn base actor at the spawnpoint
if (unitGroup.BaseActor != null)
{
new LocationInit( sp ),
new OwnerInit( p ),
});
w.CreateActor(unitGroup.BaseActor.ToLowerInvariant(), new TypeDictionary
{
new LocationInit(sp),
new OwnerInit(p),
});
}
if (!unitGroup.SupportActors.Any())
return;
// Spawn support units in an annulus around the base actor
var supportSpawnCells = w.FindTilesInCircle(sp, unitGroup.OuterSupportRadius)
.Except(w.FindTilesInCircle(sp, unitGroup.InnerSupportRadius));
foreach (var s in unitGroup.SupportActors)
{
var mi = Rules.Info[s.ToLowerInvariant()].Traits.Get<MobileInfo>();
var validCells = supportSpawnCells.Where(c => mi.CanEnterCell(w, c));
if (!validCells.Any())
throw new InvalidOperationException("No cells available to spawn starting unit {0}".F(s));
var cell = validCells.Random(w.SharedRandom);
var subCell = w.ActorMap.FreeSubCell(cell).Value;
w.CreateActor(s.ToLowerInvariant(), new TypeDictionary
{
new OwnerInit(p),
new LocationInit(cell),
new SubCellInit(subCell),
new FacingInit(w.SharedRandom.Next(256))
});
}
}
}
}