Move Util to Mods.Common.

This commit is contained in:
Paul Chote
2016-01-16 23:38:53 +00:00
parent 0039a2bdbf
commit b0bd252343
13 changed files with 27 additions and 26 deletions

View File

@@ -115,7 +115,7 @@ namespace OpenRA.Mods.Common.Effects
target = args.PassiveTarget;
if (info.Inaccuracy.Length > 0)
{
var inaccuracy = OpenRA.Traits.Util.ApplyPercentageModifiers(info.Inaccuracy.Length, args.InaccuracyModifiers);
var inaccuracy = Util.ApplyPercentageModifiers(info.Inaccuracy.Length, args.InaccuracyModifiers);
var maxOffset = inaccuracy * (target - headPos).Length / args.Weapon.Range.Length;
target += WVec.FromPDF(world.SharedRandom, 2) * maxOffset / 1024;
}

View File

@@ -116,8 +116,8 @@ namespace OpenRA.Mods.Common.Effects
target = args.PassiveTarget;
if (info.Inaccuracy.Length > 0)
{
var inaccuracy = OpenRA.Traits.Util.ApplyPercentageModifiers(info.Inaccuracy.Length, args.InaccuracyModifiers);
var range = OpenRA.Traits.Util.ApplyPercentageModifiers(args.Weapon.Range.Length, args.RangeModifiers);
var inaccuracy = Util.ApplyPercentageModifiers(info.Inaccuracy.Length, args.InaccuracyModifiers);
var range = Util.ApplyPercentageModifiers(args.Weapon.Range.Length, args.RangeModifiers);
var maxOffset = inaccuracy * (target - pos).Length / range;
target += WVec.FromPDF(world.SharedRandom, 2) * maxOffset / 1024;
}

View File

@@ -14,6 +14,7 @@ using System.Linq;
using OpenRA.Effects;
using OpenRA.GameRules;
using OpenRA.Graphics;
using OpenRA.Mods.Common;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
@@ -200,7 +201,7 @@ namespace OpenRA.Mods.Common.Effects
if (info.Inaccuracy.Length > 0)
{
var inaccuracy = OpenRA.Traits.Util.ApplyPercentageModifiers(info.Inaccuracy.Length, args.InaccuracyModifiers);
var inaccuracy = Util.ApplyPercentageModifiers(info.Inaccuracy.Length, args.InaccuracyModifiers);
offset = WVec.FromPDF(world.SharedRandom, 2) * inaccuracy / 1024;
}
@@ -723,8 +724,8 @@ namespace OpenRA.Mods.Common.Effects
desiredHFacing = hFacing;
// Compute new direction the projectile will be facing
hFacing = OpenRA.Traits.Util.TickFacing(hFacing, desiredHFacing, info.HorizontalRateOfTurn);
vFacing = OpenRA.Traits.Util.TickFacing(vFacing, desiredVFacing, info.VerticalRateOfTurn);
hFacing = Util.TickFacing(hFacing, desiredHFacing, info.HorizontalRateOfTurn);
vFacing = Util.TickFacing(vFacing, desiredVFacing, info.VerticalRateOfTurn);
// Compute the projectile's guided displacement
return new WVec(0, -1024 * speed, 0)

View File

@@ -237,7 +237,7 @@ namespace OpenRA.Mods.Common.Graphics
protected virtual Sprite GetSprite(int start, int frame, int facing)
{
var f = OpenRA.Traits.Util.QuantizeFacing(facing, Facings);
var f = Util.QuantizeFacing(facing, Facings);
if (reverseFacings)
f = (Facings - f) % Facings;

View File

@@ -144,19 +144,19 @@ namespace OpenRA.Mods.Common.Graphics
// Draw voxel bounding box
var draw = voxel.voxels.Where(v => v.DisableFunc == null || !v.DisableFunc());
var scaleTransform = Util.ScaleMatrix(voxel.scale, voxel.scale, voxel.scale);
var cameraTransform = Util.MakeFloatMatrix(voxel.camera.AsMatrix());
var scaleTransform = OpenRA.Graphics.Util.ScaleMatrix(voxel.scale, voxel.scale, voxel.scale);
var cameraTransform = OpenRA.Graphics.Util.MakeFloatMatrix(voxel.camera.AsMatrix());
foreach (var v in draw)
{
var bounds = v.Voxel.Bounds(v.FrameFunc());
var worldTransform = v.RotationFunc().Reverse().Aggregate(scaleTransform,
(x, y) => Util.MatrixMultiply(x, Util.MakeFloatMatrix(y.AsMatrix())));
(x, y) => OpenRA.Graphics.Util.MatrixMultiply(x, OpenRA.Graphics.Util.MakeFloatMatrix(y.AsMatrix())));
float sx, sy, sz;
wr.ScreenVectorComponents(v.OffsetFunc(), out sx, out sy, out sz);
var pxPos = pxOrigin + new float2(sx, sy);
var screenTransform = Util.MatrixMultiply(cameraTransform, worldTransform);
var screenTransform = OpenRA.Graphics.Util.MatrixMultiply(cameraTransform, worldTransform);
DrawBoundsBox(pxPos, screenTransform, bounds, iz, Color.Yellow);
}
}
@@ -171,7 +171,7 @@ namespace OpenRA.Mods.Common.Graphics
for (var i = 0; i < 8; i++)
{
var vec = new float[] { bounds[CornerXIndex[i]], bounds[CornerYIndex[i]], bounds[CornerZIndex[i]], 1 };
var screen = Util.MatrixVectorMultiply(transform, vec);
var screen = OpenRA.Graphics.Util.MatrixVectorMultiply(transform, vec);
corners[i] = pxPos + new float2(screen[0], screen[1]);
}
@@ -192,8 +192,8 @@ namespace OpenRA.Mods.Common.Graphics
{
var pxOrigin = wr.ScreenPosition(voxel.pos);
var draw = voxel.voxels.Where(v => v.DisableFunc == null || !v.DisableFunc());
var scaleTransform = Util.ScaleMatrix(voxel.scale, voxel.scale, voxel.scale);
var cameraTransform = Util.MakeFloatMatrix(voxel.camera.AsMatrix());
var scaleTransform = OpenRA.Graphics.Util.ScaleMatrix(voxel.scale, voxel.scale, voxel.scale);
var cameraTransform = OpenRA.Graphics.Util.MakeFloatMatrix(voxel.camera.AsMatrix());
var minX = float.MaxValue;
var minY = float.MaxValue;
@@ -203,17 +203,17 @@ namespace OpenRA.Mods.Common.Graphics
{
var bounds = v.Voxel.Bounds(v.FrameFunc());
var worldTransform = v.RotationFunc().Reverse().Aggregate(scaleTransform,
(x, y) => Util.MatrixMultiply(x, Util.MakeFloatMatrix(y.AsMatrix())));
(x, y) => OpenRA.Graphics.Util.MatrixMultiply(x, OpenRA.Graphics.Util.MakeFloatMatrix(y.AsMatrix())));
float sx, sy, sz;
wr.ScreenVectorComponents(v.OffsetFunc(), out sx, out sy, out sz);
var pxPos = pxOrigin + new float2(sx, sy);
var screenTransform = Util.MatrixMultiply(cameraTransform, worldTransform);
var screenTransform = OpenRA.Graphics.Util.MatrixMultiply(cameraTransform, worldTransform);
for (var i = 0; i < 8; i++)
{
var vec = new float[] { bounds[CornerXIndex[i]], bounds[CornerYIndex[i]], bounds[CornerZIndex[i]], 1 };
var screen = Util.MatrixVectorMultiply(screenTransform, vec);
var screen = OpenRA.Graphics.Util.MatrixVectorMultiply(screenTransform, vec);
minX = Math.Min(minX, pxPos.X + screen[0]);
minY = Math.Min(minY, pxPos.Y + screen[1]);
maxX = Math.Max(maxX, pxPos.X + screen[0]);

View File

@@ -730,6 +730,7 @@
<Compile Include="Orders\GuardOrderGenerator.cs" />
<Compile Include="UtilityCommands\CheckExplicitInterfacesCommand.cs" />
<Compile Include="FileFormats\LZOCompression.cs" />
<Compile Include="Util.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View File

@@ -15,7 +15,7 @@ using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using Util = OpenRA.Traits.Util;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Orders
{

View File

@@ -159,7 +159,7 @@ namespace OpenRA.Mods.Common.Traits
var sequence = a.Info.MuzzleSequence;
if (a.Info.MuzzleSplitFacings > 0)
sequence += OpenRA.Traits.Util.QuantizeFacing(muzzleFacing, a.Info.MuzzleSplitFacings).ToString();
sequence += Util.QuantizeFacing(muzzleFacing, a.Info.MuzzleSplitFacings).ToString();
var muzzleFlash = new AnimationWithOffset(muzzleAnim,
() => PortOffset(self, port),

View File

@@ -84,7 +84,7 @@ namespace OpenRA.Mods.Common.Traits
return;
if (a.Info.MuzzleSplitFacings > 0)
sequence += OpenRA.Traits.Util.QuantizeFacing(getFacing(), a.Info.MuzzleSplitFacings).ToString();
sequence += Util.QuantizeFacing(getFacing(), a.Info.MuzzleSplitFacings).ToString();
if (barrel == null)
return;

View File

@@ -190,14 +190,14 @@ namespace OpenRA.Mods.Common.Traits
void UpdateNeighbours(IReadOnlyDictionary<CPos, SubCell> footprint)
{
// Include actors inside the footprint too
var cells = OpenRA.Traits.Util.ExpandFootprint(footprint.Keys, true);
var cells = Util.ExpandFootprint(footprint.Keys, true);
foreach (var p in cells.SelectMany(c => PreviewsAt(c)))
p.ReplaceInit(new RuntimeNeighbourInit(NeighbouringPreviews(p.Footprint)));
}
Dictionary<CPos, string[]> NeighbouringPreviews(IReadOnlyDictionary<CPos, SubCell> footprint)
{
var cells = OpenRA.Traits.Util.ExpandFootprint(footprint.Keys, true).Except(footprint.Keys);
var cells = Util.ExpandFootprint(footprint.Keys, true).Except(footprint.Keys);
return cells.ToDictionary(c => c, c => PreviewsAt(c).Select(p => p.Info.Name).ToArray());
}

121
OpenRA.Mods.Common/Util.cs Normal file
View File

@@ -0,0 +1,121 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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.Support;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
public static class Util
{
public static int TickFacing(int facing, int desiredFacing, int rot)
{
var leftTurn = (facing - desiredFacing) & 0xFF;
var rightTurn = (desiredFacing - facing) & 0xFF;
if (Math.Min(leftTurn, rightTurn) < rot)
return desiredFacing & 0xFF;
else if (rightTurn < leftTurn)
return (facing + rot) & 0xFF;
else
return (facing - rot) & 0xFF;
}
public static int GetNearestFacing(int facing, int desiredFacing)
{
var turn = desiredFacing - facing;
if (turn > 128)
turn -= 256;
if (turn < -128)
turn += 256;
return facing + turn;
}
public static int QuantizeFacing(int facing, int numFrames)
{
var step = 256 / numFrames;
var a = (facing + step / 2) & 0xff;
return a / step;
}
public static WPos BetweenCells(World w, CPos from, CPos to)
{
return WPos.Lerp(w.Map.CenterOfCell(from), w.Map.CenterOfCell(to), 1, 2);
}
/* pretty crap */
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> ts, MersenneTwister random)
{
var items = ts.ToList();
while (items.Count > 0)
{
var t = items.Random(random);
yield return t;
items.Remove(t);
}
}
static IEnumerable<CPos> Neighbours(CPos c, bool allowDiagonal)
{
yield return c;
yield return new CPos(c.X - 1, c.Y);
yield return new CPos(c.X + 1, c.Y);
yield return new CPos(c.X, c.Y - 1);
yield return new CPos(c.X, c.Y + 1);
if (allowDiagonal)
{
yield return new CPos(c.X - 1, c.Y - 1);
yield return new CPos(c.X + 1, c.Y - 1);
yield return new CPos(c.X - 1, c.Y + 1);
yield return new CPos(c.X + 1, c.Y + 1);
}
}
public static IEnumerable<CPos> ExpandFootprint(IEnumerable<CPos> cells, bool allowDiagonal)
{
return cells.SelectMany(c => Neighbours(c, allowDiagonal)).Distinct();
}
public static IEnumerable<CPos> AdjacentCells(World w, Target target)
{
var cells = target.Positions.Select(p => w.Map.CellContaining(p)).Distinct();
return ExpandFootprint(cells, true);
}
public static int ApplyPercentageModifiers(int number, IEnumerable<int> percentages)
{
// See the comments of PR#6079 for a faster algorithm if this becomes a performance bottleneck
var a = (decimal)number;
foreach (var p in percentages)
a *= p / 100m;
return (int)a;
}
public static IEnumerable<CPos> RandomWalk(CPos p, MersenneTwister r)
{
for (;;)
{
var dx = r.Next(-1, 2);
var dy = r.Next(-1, 2);
if (dx == 0 && dy == 0)
continue;
p += new CVec(dx, dy);
yield return p;
}
}
}
}