Merge pull request #4360 from cjshmyr/crates

CrateSpawner changes
This commit is contained in:
Chris Forbes
2013-12-27 19:11:12 -08:00
22 changed files with 128 additions and 163 deletions

View File

@@ -131,6 +131,10 @@ namespace OpenRA.Mods.RA
self.World.ActorMap.AddInfluence(self, this);
self.World.ActorMap.AddPosition(self, this);
self.World.ScreenMap.Add(self);
var cs = self.World.WorldActor.TraitOrDefault<CrateSpawner>();
if (cs != null)
cs.IncrementCrates();
}
public void RemovedFromWorld(Actor self)
@@ -138,6 +142,10 @@ namespace OpenRA.Mods.RA
self.World.ActorMap.RemoveInfluence(self, this);
self.World.ActorMap.RemovePosition(self, this);
self.World.ScreenMap.Remove(self);
var cs = self.World.WorldActor.TraitOrDefault<CrateSpawner>();
if (cs != null)
cs.DecrementCrates();
}
}
}

View File

@@ -1,117 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 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,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Mods.RA.Air;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public class CrateDropInfo : ITraitInfo
{
[Desc("Minimum number of crates")]
public readonly int Minimum = 1;
[Desc("Maximum number of crates")]
public readonly int Maximum = 255;
[Desc("Which terrain types can we drop on?")]
public readonly string[] ValidGround = {"Clear", "Rough", "Road", "Ore", "Beach"};
[Desc("Which terrain types count as water?")]
public readonly string[] ValidWater = {"Water"};
[Desc("Average time (seconds) between crate spawn")]
public readonly int SpawnInterval = 180;
[Desc("Chance of generating a water crate instead of a land crate")]
public readonly float WaterChance = .2f;
[ActorReference]
public readonly string CrateActor = "crate";
[ActorReference]
public readonly string DeliveryAircraft = "badr";
public object Create (ActorInitializer init) { return new CrateDrop(this); }
}
public class CrateDrop : ITick
{
List<Actor> crates = new List<Actor>();
int ticks = 0;
CrateDropInfo Info;
public CrateDrop(CrateDropInfo info) { Info = info; }
public void Tick(Actor self)
{
if (!self.World.LobbyInfo.GlobalSettings.Crates) return;
if (--ticks <= 0)
{
ticks = Info.SpawnInterval * 25; // TODO: randomize
crates.RemoveAll(x => !x.IsInWorld); // BUG: this removes crates that are cargo of a BADR!
var toSpawn = Math.Max(0, Info.Minimum - crates.Count)
+ (crates.Count < Info.Maximum ? 1 : 0);
for (var n = 0; n < toSpawn; n++)
SpawnCrate(self);
}
}
CPos? ChooseDropCell(Actor self, bool inWater, int maxTries)
{
for( var n = 0; n < maxTries; n++ )
{
var p = self.World.ChooseRandomCell(self.World.SharedRandom);
// Is this valid terrain?
var terrainType = self.World.GetTerrainType(p);
if (!(inWater ? Info.ValidWater : Info.ValidGround).Contains(terrainType)) continue;
// Don't drop on any actors
if (self.World.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(p) != null) continue;
if (self.World.ActorMap.GetUnitsAt(p).Any()) continue;
return p;
}
return null;
}
void SpawnCrate(Actor self)
{
var inWater = self.World.SharedRandom.NextFloat() < Info.WaterChance;
var pp = ChooseDropCell(self, inWater, 100);
if (pp == null) return;
var p = pp.Value;
self.World.AddFrameEndTask(w =>
{
var crate = w.CreateActor(false, Info.CrateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner) });
crates.Add(crate);
var startPos = w.ChooseRandomEdgeCell();
var altitude = Rules.Info[Info.DeliveryAircraft].Traits.Get<PlaneInfo>().CruiseAltitude;
var plane = w.CreateActor(Info.DeliveryAircraft, new TypeDictionary
{
new CenterPositionInit(startPos.CenterPosition + new WVec(WRange.Zero, WRange.Zero, altitude)),
new OwnerInit(w.WorldActor.Owner),
new FacingInit(Util.GetFacing(p - startPos, 0))
});
plane.CancelActivity();
plane.QueueActivity(new FlyAttack(Target.FromCell(p)));
plane.Trait<ParaDrop>().SetLZ(p);
plane.Trait<Cargo>().Load(plane, crate);
});
}
}
}

View File

@@ -12,76 +12,150 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Mods.RA.Air;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public class CrateSpawnerInfo : TraitInfo<CrateSpawner>
public class CrateSpawnerInfo : ITraitInfo
{
[Desc("Minimum number of crates")]
public readonly int Minimum = 1;
[Desc("Maximum number of crates")]
public readonly int Maximum = 255;
[Desc("Which terrain types can we drop on?")]
public readonly string[] ValidGround = {"Clear", "Rough", "Road", "Ore", "Beach"};
[Desc("Which terrain types count as water?")]
public readonly string[] ValidWater = {"Water"};
[Desc("Average time (seconds) between crate spawn")]
public readonly int SpawnInterval = 180;
[Desc("Which terrain types can we drop on?")]
public readonly string[] ValidGround = { "Clear", "Rough", "Road", "Ore", "Beach" };
[Desc("Which terrain types count as water?")]
public readonly string[] ValidWater = { "Water" };
[Desc("Chance of generating a water crate instead of a land crate")]
public readonly float WaterChance = .2f;
[Desc("If a DeliveryAircraft: is specified, then this actor will deliver crates"), ActorReference]
public readonly string DeliveryAircraft = null;
[Desc("Crate actors to drop"), ActorReference]
public readonly string[] CrateActors = { "crate" };
[Desc("Chance of each crate actor spawning")]
public readonly int[] CrateActorShares = { 10 };
public object Create(ActorInitializer init) { return new CrateSpawner(this, init.self); }
}
public class CrateSpawner : ITick
{
List<Actor> crates = new List<Actor>();
int crates = 0;
int ticks = 0;
CrateSpawnerInfo info;
Actor self;
public CrateSpawner(CrateSpawnerInfo info, Actor self)
{
this.info = info;
this.self = self;
}
public void Tick(Actor self)
{
if (!self.World.LobbyInfo.GlobalSettings.Crates) return;
if (!self.World.LobbyInfo.GlobalSettings.Crates)
return;
if (--ticks <= 0)
{
var info = self.Info.Traits.Get<CrateSpawnerInfo>();
ticks = info.SpawnInterval * 25; // TODO: randomize
ticks = info.SpawnInterval * 25;
crates.RemoveAll(x => !x.IsInWorld);
var toSpawn = Math.Max(0, info.Minimum - crates.Count)
+ (crates.Count < info.Maximum ? 1 : 0);
var toSpawn = Math.Max(0, info.Minimum - crates)
+ (crates < info.Maximum ? 1 : 0);
for (var n = 0; n < toSpawn; n++)
SpawnCrate(self, info);
SpawnCrate(self);
}
}
void SpawnCrate(Actor self, CrateSpawnerInfo info)
void SpawnCrate(Actor self)
{
var threshold = 100;
var inWater = self.World.SharedRandom.NextFloat() < info.WaterChance;
var pp = ChooseDropCell(self, inWater, threshold);
for (var n = 0; n < threshold; n++ )
if (pp == null)
return;
var p = pp.Value;
var crateActor = ChooseCrateActor();
self.World.AddFrameEndTask(w =>
{
if (info.DeliveryAircraft != null)
{
var crate = w.CreateActor(false, crateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner) });
var startPos = w.ChooseRandomEdgeCell();
var altitude = Rules.Info[info.DeliveryAircraft].Traits.Get<PlaneInfo>().CruiseAltitude;
var plane = w.CreateActor(info.DeliveryAircraft, new TypeDictionary
{
new CenterPositionInit(startPos.CenterPosition + new WVec(WRange.Zero, WRange.Zero, altitude)),
new OwnerInit(w.WorldActor.Owner),
new FacingInit(Util.GetFacing(p - startPos, 0))
});
plane.CancelActivity();
plane.QueueActivity(new FlyAttack(Target.FromCell(p)));
plane.Trait<ParaDrop>().SetLZ(p);
plane.Trait<Cargo>().Load(plane, crate);
}
else
{
w.CreateActor(crateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner), new LocationInit(p) });
}
});
}
CPos? ChooseDropCell(Actor self, bool inWater, int maxTries)
{
for (var n = 0; n < maxTries; n++)
{
var p = self.World.ChooseRandomCell(self.World.SharedRandom);
// Is this valid terrain?
var terrainType = self.World.GetTerrainType(p);
if (!(inWater ? info.ValidWater : info.ValidGround).Contains(terrainType)) continue;
if (!(inWater ? info.ValidWater : info.ValidGround).Contains(terrainType))
continue;
// Don't spawn on any actors
if (self.World.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(p) != null) continue;
if (self.World.ActorMap.GetUnitsAt(p).Any()) continue;
// Don't drop on any actors
if (self.World.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(p) != null
|| self.World.ActorMap.GetUnitsAt(p).Any())
continue;
self.World.AddFrameEndTask(
w => crates.Add(w.CreateActor("crate", new TypeDictionary
{
new LocationInit( p ),
new OwnerInit( self.World.WorldActor.Owner ),
})));
return;
return p;
}
return null;
}
string ChooseCrateActor()
{
var crateShares = info.CrateActorShares;
var n = self.World.SharedRandom.Next(crateShares.Sum());
var cumulativeShares = 0;
for (var i = 0; i < crateShares.Length; i++)
{
cumulativeShares += crateShares[i];
if (n <= cumulativeShares)
return info.CrateActors[i];
}
return null;
}
public void IncrementCrates()
{
crates++;
}
public void DecrementCrates()
{
crates--;
}
}
}

View File

@@ -195,7 +195,6 @@
<Compile Include="ContainsCrate.cs" />
<Compile Include="Crate.cs" />
<Compile Include="CrateAction.cs" />
<Compile Include="CrateDrop.cs" />
<Compile Include="CrateSpawner.cs" />
<Compile Include="Crates\CloakCrateAction.cs" />
<Compile Include="Crates\ExplodeCrateAction.cs" />

View File

@@ -482,7 +482,7 @@ Smudges:
Rules:
World:
CrateDrop:
CrateSpawner:
Maximum: 1
SpawnInterval: 100
-SpawnMPUnits:

View File

@@ -1192,7 +1192,7 @@ Smudges:
Rules:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
Survival01Script:

View File

@@ -991,7 +991,7 @@ Smudges:
Rules:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
Survival02Script:

View File

@@ -575,7 +575,7 @@ Rules:
Player:
-ConquestVictoryConditions:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
LuaScriptInterface:

View File

@@ -359,7 +359,7 @@ Rules:
Player:
-ConquestVictoryConditions:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
Allies01Script:

View File

@@ -870,7 +870,7 @@ Rules:
Player:
-ConquestVictoryConditions:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
LuaScriptInterface:

View File

@@ -1745,7 +1745,7 @@ Rules:
Player:
-ConquestVictoryConditions:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
Allies02Script:

View File

@@ -1440,7 +1440,7 @@ Rules:
Player:
-ConquestVictoryConditions:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
Allies03Script:

View File

@@ -1811,7 +1811,7 @@ Rules:
Player:
-ConquestVictoryConditions:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
Allies04Script:

View File

@@ -769,7 +769,7 @@ Smudges:
Rules:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
APWR:
Buildable:

View File

@@ -1319,7 +1319,7 @@ Rules:
Player:
-ConquestVictoryConditions:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
DesertShellmapScript:

View File

@@ -291,7 +291,7 @@ Smudges:
Rules:
World:
CrateDrop:
CrateSpawner:
Maximum: 3
SpawnInterval: 5
-SpawnMPUnits:

View File

@@ -204,7 +204,7 @@ Smudges:
Rules:
World:
CrateDrop:
CrateSpawner:
Maximum: 3
SpawnInterval: 5
WaterChance: 1

View File

@@ -186,7 +186,7 @@ Smudges:
Rules:
World:
CrateDrop:
CrateSpawner:
Maximum: 3
SpawnInterval: 5
-SpawnMPUnits:

View File

@@ -2545,7 +2545,7 @@ Rules:
Player:
-ConquestVictoryConditions:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
MonsterTankMadnessScript:

View File

@@ -851,7 +851,7 @@ Rules:
Player:
-ConquestVictoryConditions:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
Soviet01ClassicScript:

View File

@@ -807,7 +807,7 @@ Smudges:
Rules:
World:
-CrateDrop:
-CrateSpawner:
-SpawnMPUnits:
-MPStartLocations:
Player:

View File

@@ -551,7 +551,8 @@ World:
ChooseBuildTabOnSelect:
BridgeLayer:
Bridges: bridge1, bridge2, br1, br2, br3, sbridge1, sbridge2, sbridge3, sbridge4
CrateDrop:
CrateSpawner:
DeliveryAircraft: badr
Minimum: 1
Maximum: 3
SpawnInterval: 120