WIP pathfinding AI navigator, Level class refactoring

This commit is contained in:
Regalis11
2015-11-15 19:14:08 +02:00
parent 24ed95cd68
commit ef9d7ef0d4
11 changed files with 199 additions and 107 deletions

Binary file not shown.

View File

@@ -63,6 +63,7 @@
<Compile Include="Source\Characters\AI\Objectives\AIObjectiveOperateItem.cs" />
<Compile Include="Source\Characters\AI\Objectives\AIObjective.cs" />
<Compile Include="Source\Characters\AI\PathFinder.cs" />
<Compile Include="Source\Characters\AI\PathSteeringManager.cs" />
<Compile Include="Source\Characters\AnimController.cs" />
<Compile Include="Source\Characters\Attack.cs" />
<Compile Include="Source\Characters\BackgroundSprite\BackgroundSprite.cs" />
@@ -101,6 +102,7 @@
<Compile Include="Source\Items\FixRequirement.cs" />
<Compile Include="Source\Items\ItemSpawner.cs" />
<Compile Include="Source\Map\FireSource.cs" />
<Compile Include="Source\Map\Levels\LevelRenderer.cs" />
<Compile Include="Source\Map\Levels\WrappingWall.cs" />
<Compile Include="Source\Map\Lights\LightSource.cs" />
<Compile Include="Source\Map\LocationType.cs" />

View File

@@ -10,7 +10,7 @@ namespace Barotrauma
public enum AiState { None, Attack, GoTo, Escape }
public enum SteeringState { Wander, Seek, Escape }
public Character Character;
public readonly Character Character;
protected AiState state;
@@ -45,10 +45,7 @@ namespace Barotrauma
steeringManager = new SteeringManager(this);
}
public virtual void DebugDraw(SpriteBatch spriteBatch)
{
}
public virtual void DebugDraw(SpriteBatch spriteBatch) { }
public virtual void OnAttacked(IDamageable attacker, float amount) { }

View File

@@ -11,7 +11,7 @@ namespace Barotrauma
public virtual bool IsCompleted()
{
return true;
return false;
}
public AIObjective()

View File

@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using FarseerPhysics;
namespace Barotrauma
{
class PathSteeringManager : SteeringManager
{
private PathFinder pathFinder;
private SteeringPath currentPath;
private Vector2 currentTarget;
private float findPathTimer;
public PathSteeringManager(ISteerable host)
: base(host)
{}
public override void Update(float speed = 1)
{
base.Update(speed);
findPathTimer -= 1.0f / 60.0f;
}
protected override Vector2 DoSteeringSeek(Vector2 target, float speed = 1)
{
//find a new path if one hasn't been found yet or the target is different from the current target
if (currentPath == null || Vector2.DistanceSquared(target, currentTarget)>10.0f)
{
if (findPathTimer > 0.0f) return Vector2.Zero;
currentTarget = target;
currentPath = pathFinder.FindPath(ConvertUnits.ToDisplayUnits(host.SimPosition), target);
findPathTimer = 1.0f;
return DiffToCurrentNode();
}
Vector2 diff = DiffToCurrentNode();
return (diff == Vector2.Zero) ? Vector2.Zero : Vector2.Normalize(diff)*speed;
}
private Vector2 DiffToCurrentNode()
{
if (currentPath == null) return Vector2.Zero;
currentPath.CheckProgress(host.SimPosition, 0.1f);
if (currentPath.CurrentNode == null) return Vector2.Zero;
return currentPath.CurrentNode.SimPosition - host.SimPosition;
}
}
}

View File

@@ -6,12 +6,12 @@ namespace Barotrauma
{
class SteeringManager
{
private const float CircleDistance = 2.5f;
private const float CircleRadius = 0.3f;
protected const float CircleDistance = 2.5f;
protected const float CircleRadius = 0.3f;
private const float RayCastInterval = 0.5f;
protected const float RayCastInterval = 0.5f;
private ISteerable host;
protected ISteerable host;
private Vector2 steering;
@@ -50,7 +50,7 @@ namespace Barotrauma
steering += DoSteeringAvoid(deltaTime, speed);
}
public void Update(float speed = 1.0f)
public virtual void Update(float speed = 1.0f)
{
float steeringSpeed = steering.Length();
if (steeringSpeed>speed)
@@ -61,7 +61,7 @@ namespace Barotrauma
host.Steering = steering;
}
private Vector2 DoSteeringSeek(Vector2 target, float speed = 1.0f)
protected virtual Vector2 DoSteeringSeek(Vector2 target, float speed = 1.0f)
{
Vector2 targetVel = target - host.SimPosition;
@@ -81,7 +81,7 @@ namespace Barotrauma
return newSteering;
}
private Vector2 DoSteeringWander(float speed = 1.0f)
protected virtual Vector2 DoSteeringWander(float speed = 1.0f)
{
Vector2 circleCenter = (host.Velocity == Vector2.Zero) ? new Vector2(speed, 0.0f) : host.Velocity;
circleCenter = Vector2.Normalize(circleCenter) * CircleDistance;
@@ -105,7 +105,7 @@ namespace Barotrauma
return newSteering;
}
private Vector2 DoSteeringAvoid(float deltaTime, float speed = 1.0f)
protected virtual Vector2 DoSteeringAvoid(float deltaTime, float speed = 1.0f)
{
if (steering == Vector2.Zero || host.Steering == Vector2.Zero) return Vector2.Zero;

View File

@@ -48,10 +48,10 @@ namespace Barotrauma
currentIndex++;
}
public WayPoint CheckProgress(Vector2 pos, float minSimDistance = 0.1f)
public WayPoint CheckProgress(Vector2 simPosition, float minSimDistance = 0.1f)
{
if (nodes.Count == 0 || currentIndex>nodes.Count-1) return null;
if (Vector2.Distance(pos, nodes[currentIndex].SimPosition) < minSimDistance) currentIndex++;
if (Vector2.Distance(simPosition, nodes[currentIndex].SimPosition) < minSimDistance) currentIndex++;
return CurrentNode;
}

View File

@@ -21,7 +21,7 @@ namespace Barotrauma
static Level loaded;
private static Texture2D shaftTexture;
private LevelRenderer renderer;
//how close the sub has to be to start/endposition to exit
const float ExitDistance = 6000.0f;
@@ -40,13 +40,10 @@ namespace Barotrauma
//List<Body> bodies;
private List<VoronoiCell> cells;
private static BasicEffect basicEffect;
private VertexPositionTexture[] vertices;
//private VertexBuffer vertexBuffer;
private Vector2 startPosition;
private Vector2 endPosition;
private Vector2 startPosition, endPosition;
private Rectangle borders;
@@ -94,6 +91,11 @@ namespace Barotrauma
get { return positionsOfInterest; }
}
public WrappingWall[,] WrappingWalls
{
get { return wrappingWalls; }
}
public string Seed
{
get { return seed; }
@@ -107,18 +109,6 @@ namespace Barotrauma
public Level(string seed, float difficulty, int width, int height, int siteInterval)
{
if (shaftTexture == null) shaftTexture = TextureLoader.FromFile("Content/Map/shaft.png");
if (basicEffect==null)
{
basicEffect = new BasicEffect(GameMain.CurrGraphicsDevice);
basicEffect.VertexColorEnabled = false;
basicEffect.TextureEnabled = true;
basicEffect.Texture = TextureLoader.FromFile("Content/Map/iceSurface.png");
}
this.seed = seed;
this.siteInterval = siteInterval;
@@ -150,13 +140,12 @@ namespace Barotrauma
Stopwatch sw = new Stopwatch();
sw.Start();
if (loaded != null)
{
loaded.Unload();
}
if (loaded != null) loaded.Unload();
loaded = this;
renderer = new LevelRenderer(this);
Voronoi voronoi = new Voronoi(1.0);
List<Vector2> sites = new List<Vector2>();
@@ -391,7 +380,10 @@ namespace Barotrauma
endPosition = temp;
}
Debug.WriteLine("**********************************************************************************");
Debug.WriteLine("Generated a map with " + sites.Count + " sites in " + sw.ElapsedMilliseconds + " ms");
Debug.WriteLine("Seed: "+seed);
Debug.WriteLine("**********************************************************************************");
}
private List<VoronoiCell> GeneratePath(List<Vector2> points, List<VoronoiCell> cells, Microsoft.Xna.Framework.Rectangle limits, float minWidth, float wanderAmount = 0.3f, bool mirror=false, bool placeWaypoints=false)
@@ -821,6 +813,15 @@ namespace Barotrauma
}
}
public void Draw(SpriteBatch spriteBatch)
{
renderer.Draw(spriteBatch);
}
public void Render(GraphicsDevice graphicsDevice, Camera cam)
{
renderer.Render(graphicsDevice, cam, vertices);
}
public void DebugCheckPos()
@@ -841,26 +842,6 @@ namespace Barotrauma
System.Diagnostics.Debug.WriteLine("pos: " + Position);
}
public void Draw(SpriteBatch spriteBatch)
{
Vector2 pos = endPosition;
pos.Y = -pos.Y - Position.Y;
if (GameMain.GameScreen.Cam.WorldView.Y < -pos.Y-512) return;
pos.X = GameMain.GameScreen.Cam.WorldView.X-512.0f;
//pos.X += Position.X % 512;
int width = (int)(Math.Ceiling(GameMain.GameScreen.Cam.WorldView.Width/512.0f + 2.0f)*512.0f);
spriteBatch.Draw(shaftTexture,
new Rectangle((int)(MathUtils.Round(pos.X, 512.0f) + Position.X % 512) , (int)pos.Y, width, 512),
new Rectangle(0, 0, width, 256),
Color.White, 0.0f,
Vector2.Zero,
SpriteEffects.None, 0.0f);
}
public List<VoronoiCell> GetCells(Vector2 pos, int searchDepth = 2)
{
int gridPosX = (int)Math.Floor(pos.X / GridCellWidth);
@@ -907,7 +888,6 @@ namespace Barotrauma
public List<Vector2[]> GetCellEdges(Vector2 refPos, int searchDepth = 2, bool onlySolid = true)
{
int gridPosX = (int)Math.Floor(refPos.X / GridCellWidth);
int gridPosY = (int)Math.Floor(refPos.Y / GridCellWidth);
@@ -928,6 +908,7 @@ namespace Barotrauma
for (int i = 0; i < cell.edges.Count; i++)
{
if (onlySolid && !cell.edges[i].isSolid) continue;
Vector2 start = cell.edges[i].point1 + Position;
start.Y = -start.Y;
@@ -949,13 +930,14 @@ namespace Barotrauma
foreach (VoronoiCell cell in wrappingWalls[side, n].Cells)
{
Vector2 offset = wrappingWalls[side, n].Offset + Position;
for (int i = 0; i < cell.edges.Count; i++)
{
if (onlySolid && !cell.edges[i].isSolid) continue;
Vector2 start = cell.edges[i].point1 + Position;
Vector2 start = cell.edges[i].point1 + offset;
start.Y = -start.Y;
Vector2 end = cell.edges[i].point2 + Position;
Vector2 end = cell.edges[i].point2 + offset;
end.Y = -end.Y;
edges.Add(new Vector2[] { start, end });
@@ -967,51 +949,9 @@ namespace Barotrauma
return edges;
}
public void Render(GraphicsDevice graphicsDevice, Camera cam)
{
if (vertices == null) return;
if (vertices.Length <= 0) return;
basicEffect.World = Matrix.CreateTranslation(new Vector3(Position, 0.0f)) * cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
basicEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
graphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, (int)Math.Floor(vertices.Length / 3.0f));
for (int side = 0; side < 2; side++)
{
for (int i = 0; i < 2; i++)
{
basicEffect.World = Matrix.CreateTranslation(new Vector3(Position + wrappingWalls[side, i].Offset, 0.0f)) * cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
basicEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList,
wrappingWalls[side, i].Vertices, 0, (int)Math.Floor(wrappingWalls[side, i].Vertices.Length / 3.0f));
}
}
}
private void Unload()
{
//position = Vector2.Zero;
//foreach (VoronoiCell cell in cells)
//{
// //foreach (Body b in cell.bodies)
// //{
// Game1.world.RemoveBody(cell.body);
// //}
//}
//bodies = null;
renderer = null;
vertices = null;

View File

@@ -0,0 +1,93 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Barotrauma
{
class LevelRenderer
{
private static BasicEffect basicEffect;
private static Texture2D shaftTexture;
private Level level;
public LevelRenderer(Level level)
{
if (shaftTexture == null) shaftTexture = TextureLoader.FromFile("Content/Map/shaft.png");
if (basicEffect == null)
{
basicEffect = new BasicEffect(GameMain.CurrGraphicsDevice);
basicEffect.VertexColorEnabled = false;
basicEffect.TextureEnabled = true;
basicEffect.Texture = TextureLoader.FromFile("Content/Map/iceSurface.png");
}
this.level = level;
}
public void Draw(SpriteBatch spriteBatch)
{
Vector2 pos = level.EndPosition;
pos.Y = -pos.Y - level.Position.Y;
if (GameMain.GameScreen.Cam.WorldView.Y < -pos.Y - 512) return;
pos.X = GameMain.GameScreen.Cam.WorldView.X - 512.0f;
//pos.X += Position.X % 512;
int width = (int)(Math.Ceiling(GameMain.GameScreen.Cam.WorldView.Width / 512.0f + 2.0f) * 512.0f);
spriteBatch.Draw(shaftTexture,
new Rectangle((int)(MathUtils.Round(pos.X, 512.0f) + level.Position.X % 512), (int)pos.Y, width, 512),
new Rectangle(0, 0, width, 256),
Color.White, 0.0f,
Vector2.Zero,
SpriteEffects.None, 0.0f);
}
public void Render(GraphicsDevice graphicsDevice, Camera cam, VertexPositionTexture[] vertices)
{
if (vertices == null) return;
if (vertices.Length <= 0) return;
basicEffect.World = Matrix.CreateTranslation(new Vector3(level.Position, 0.0f)) * cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
basicEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
graphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, (int)Math.Floor(vertices.Length / 3.0f));
for (int side = 0; side < 2; side++)
{
for (int i = 0; i < 2; i++)
{
basicEffect.World = Matrix.CreateTranslation(
new Vector3(level.Position + level.WrappingWalls[side, i].Offset, 0.0f)) *
cam.ShaderTransform *
Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
basicEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives(
PrimitiveType.TriangleList,
level.WrappingWalls[side, i].Vertices, 0,
(int)Math.Floor(level.WrappingWalls[side, i].Vertices.Length / 3.0f));
}
}
}
}
}

View File

@@ -149,8 +149,8 @@ namespace Barotrauma
slot += amount;
Vector2 moveAmount = Vector2.UnitX * WallWidth * amount;
Vector2 simMoveAmount = ConvertUnits.ToSimUnits(moveAmount);
foreach (VoronoiCell cell in cells)
{
cell.body.SetTransform(cell.body.Position + simMoveAmount, 0.0f);
@@ -158,7 +158,6 @@ namespace Barotrauma
midPos += moveAmount;
offset += moveAmount;
}
}
}

View File

@@ -3,7 +3,6 @@ using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Voronoi2;
namespace Barotrauma