Skip to content

Commit

Permalink
shuffle exits
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisforbes committed Dec 27, 2010
1 parent 888fe35 commit d8eaa7c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
14 changes: 14 additions & 0 deletions OpenRA.Game/Traits/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Support;
using System.Collections.Generic;

namespace OpenRA.Traits
{
Expand Down Expand Up @@ -132,5 +133,18 @@ public static IActivity RunActivity( Actor self, IActivity act )
public static Color ArrayToColor(int[] x) { return Color.FromArgb(x[0], x[1], x[2]); }

public static int2 CellContaining(float2 pos) { return (1f / Game.CellSize * pos).ToInt2(); }

/* pretty crap */
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> ts, Thirdparty.Random random)
{
var items = ts.ToList();
while (items.Count > 0)
{
var t = items.Random(random);
yield return t;
items.Remove(t);
}
}

}
}
33 changes: 14 additions & 19 deletions OpenRA.Mods.RA/Production.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,21 @@ public void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo)

//Log.Write("debug", "{0} #{1} produced by {2} #{3}", newUnit.Info.Name, newUnit.ActorID, self.Info.Name, self.ActorID);
}

public virtual bool Produce( Actor self, ActorInfo producee )
{
// Todo: remove assumption on Mobile;
// required for 3-arg CanEnterCell
//var mobile = newUnit.Trait<Mobile>();
var mobileInfo = producee.Traits.Get<MobileInfo>();
var uim = self.World.WorldActor.Trait<UnitInfluence>();

// Pick a spawn/exit point pair
// Todo: Reorder in a synced random way
foreach (var s in self.Info.Traits.WithInterface<ExitInfo>())
if( mobileInfo.CanEnterCell( self.World, uim, self.Location + s.ExitCell,self,true ) )
{
DoProduction(self, producee, s);
return true;
}
return false;
}

public virtual bool Produce(Actor self, ActorInfo producee)
{
// todo: remove Mobile requirement.
var mobileInfo = producee.Traits.Get<MobileInfo>();
var uim = self.World.WorldActor.Trait<UnitInfluence>();

// pick a spawn/exit point pair
foreach (var s in self.Info.Traits.WithInterface<ExitInfo>().Shuffle(self.World.SharedRandom))
if (mobileInfo.CanEnterCell(self.World, uim, self.Location + s.ExitCell, self, true))
{
DoProduction(self, producee, s);
return true;
}
return false;
}
}
}

0 comments on commit d8eaa7c

Please sign in to comment.