From a65358f1910e221744ae18f5a0882e367cdec5d2 Mon Sep 17 00:00:00 2001 From: Curtis Shmyr Date: Mon, 23 Dec 2013 19:34:08 -0700 Subject: [PATCH] Add support for multiple crate actors to be spawned from CrateSpawner --- OpenRA.Mods.RA/CrateSpawner.cs | 38 +++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/OpenRA.Mods.RA/CrateSpawner.cs b/OpenRA.Mods.RA/CrateSpawner.cs index 2cc7a81df3..7652b6cf1b 100644 --- a/OpenRA.Mods.RA/CrateSpawner.cs +++ b/OpenRA.Mods.RA/CrateSpawner.cs @@ -32,14 +32,16 @@ namespace OpenRA.Mods.RA public readonly string[] ValidWater = { "Water" }; [Desc("Chance of generating a water crate instead of a land crate")] public readonly float WaterChance = .2f; - [Desc("Drop crates via DeliveryAircraft: or instantly spawn them on the ground")] + [Desc("Airdrop crates via DeliveryAircraft: or instantly spawn them on the ground")] public readonly bool DeliverByAircraft = false; [Desc("If DeliverByAircraft: yes, this actor will deliver crates"), ActorReference] public readonly string DeliveryAircraft = "badr"; - [Desc("Crate actor to drop"), ActorReference] - public readonly string CrateActor = "crate"; + [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); } + public object Create(ActorInitializer init) { return new CrateSpawner(this, init.self); } } public class CrateSpawner : ITick @@ -47,8 +49,13 @@ namespace OpenRA.Mods.RA List crates = new List(); int ticks = 0; CrateSpawnerInfo Info; + Actor self; - public CrateSpawner(CrateSpawnerInfo info) { Info = info; } + public CrateSpawner(CrateSpawnerInfo info, Actor self) + { + Info = info; + this.self = self; + } public void Tick(Actor self) { @@ -79,12 +86,13 @@ namespace OpenRA.Mods.RA return; var p = pp.Value; + var crateActor = ChooseCrateActor(); self.World.AddFrameEndTask(w => { if (Info.DeliverByAircraft) { - var crate = w.CreateActor(false, Info.CrateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner) }); + var crate = w.CreateActor(false, crateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner) }); crates.Add(crate); var startPos = w.ChooseRandomEdgeCell(); @@ -103,7 +111,7 @@ namespace OpenRA.Mods.RA } else { - crates.Add(w.CreateActor(Info.CrateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner), new LocationInit(p) })); + crates.Add(w.CreateActor(crateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner), new LocationInit(p) })); } }); } @@ -129,5 +137,21 @@ namespace OpenRA.Mods.RA 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; + } } }