Changeset 13

User picture

Author: conkerjo

(2009/07/01 23:02) Over 2 years ago


  

Affected files

Updated src/BRAINSFramework/Agent.cs Download diff

1213
1
//****************************************************************//
2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
11
using System;
1
using System;
12
using System.Collections.Generic;
2
using System.Collections.Generic;
13
using System.Linq;
3
using System.Linq;
...
...
26
using System.IO;
16
using System.IO;
27
using System.Reflection;
17
using System.Reflection;
28
using Brains.Framework.Designer;
18
using Brains.Framework.Designer;
19
using Brains.Framework.Map;
29
20
30
namespace Brains.Framework
21
namespace Brains.Framework
31
{
22
{
32
    /// <summary>
23
    /// <summary>
33
    /// Class to represent an AI Agent. Add the Agent to a World
24
    /// Represents an autonomous agent in the AI world.
34
    /// </summary>
25
    /// </summary>
35
    /// <remarks>You can implement your own Agent by inheriting from this class</remarks>
26
    /// <remarks>You can implement your own Agent by inheriting from this class</remarks>
36
    public class Agent
27
    public class Agent
...
...
186
            // - for each component calculate row and col
177
            // - for each component calculate row and col
187
178
188
            Vector2 vDistanceFromMapCenter = this.Position;
179
            Vector2 vDistanceFromMapCenter = this.Position;
189
            Grid map = ParentWorld.Map.Cluster[0];
180
            Grid map = ParentWorld.Map.ClusterGrid.Grids[0];
190
            float _hstep = (map.Width/ ((float)map.Cols));
181
            float _hstep = (map.Width/ ((float)map.Cols));
191
            float _vstep = (map.Height/ ((float)map.Rows));
182
            float _vstep = (map.Height/ ((float)map.Rows));
192
            float xComponent = (vDistanceFromMapCenter.X / _hstep) ;
183
            float xComponent = (vDistanceFromMapCenter.X / _hstep) ;
...
...
224
        }
215
        }
225
        public Grid GetGridAtPosition(Vector2 position)
216
        public Grid GetGridAtPosition(Vector2 position)
226
        {
217
        {
227
            foreach (var item in ParentWorld.Map.Cluster)
218
            foreach (var item in ParentWorld.Map.ClusterGrid.Grids)
228
            {
219
            {
229
                if (
220
                if (
230
                    (position.X > item.Position.X && position.X < (item.Position.X + item.Width)) &&
221
                    (position.X > item.Position.X && position.X < (item.Position.X + item.Width)) &&

Updated src/BRAINSFramework/AIEngine.cs Download diff

1213
1
//****************************************************************//
1
using System;
2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
11
using System;
12
using System.Collections.Generic;
2
using System.Collections.Generic;
13
using System.Linq;
3
using System.Linq;
14
using System.Text;
4
using System.Text;
...
...
19
namespace Brains.Framework
9
namespace Brains.Framework
20
{
10
{
21
    /// <summary>
11
    /// <summary>
22
    /// The main AI Engine. 
12
    /// Represents the main AI Engine. 
23
    /// Create an instance of this class and call update every frame to stimulate the world
13
    /// Create an instance of this class and call update every frame to stimulate the world
24
    /// </summary>
14
    /// </summary>
25
    public class AIEngine
15
    public class AIEngine

Updated src/BRAINSFramework/Behaviors/BehaviorBase.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Behaviors/BehaviorList.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Behaviors/BehaviorTask.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Behaviors/GoToBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;
13
using System.Text;
5
using System.Text;
14
using Brains.Framework.Behaviors.PathFinding;
6
using Brains.Framework.Behaviors.PathFinding;
15
using Microsoft.Xna.Framework;
7
using Microsoft.Xna.Framework;
8
using Brains.Framework.Map;
16
9
17
namespace Brains.Framework.Behaviors
10
namespace Brains.Framework.Behaviors
18
{
11
{

Updated src/BRAINSFramework/Behaviors/IActionBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Behaviors/IBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Behaviors/ICompositeBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Behaviors/IConditionBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Behaviors/PathFinding/CyclicPathBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;
13
using System.Text;
5
using System.Text;
14
using Microsoft.Xna.Framework;
6
using Microsoft.Xna.Framework;
7
using Brains.Framework.Map;
15
8
16
namespace Brains.Framework.Behaviors.PathFinding
9
namespace Brains.Framework.Behaviors.PathFinding
17
{
10
{

Updated src/BRAINSFramework/Behaviors/PathFinding/FindPathBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;
13
using System.Text;
5
using System.Text;
14
using Brains.Framework.PathFinding;
6
using Brains.Framework.PathFinding;
15
using Microsoft.Xna.Framework;
7
using Microsoft.Xna.Framework;
8
using Brains.Framework.Map;
16
9
17
namespace Brains.Framework.Behaviors.PathFinding
10
namespace Brains.Framework.Behaviors.PathFinding
18
{
11
{

Updated src/BRAINSFramework/Behaviors/PathFinding/FollowPathBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;
13
using System.Text;
5
using System.Text;
14
using Microsoft.Xna.Framework;
6
using Microsoft.Xna.Framework;
15
using Brains.Framework.PathFinding;
7
using Brains.Framework.PathFinding;
8
using Brains.Framework.Map;
16
9
17
namespace Brains.Framework.Behaviors.PathFinding
10
namespace Brains.Framework.Behaviors.PathFinding
18
{
11
{

Updated src/BRAINSFramework/Behaviors/RandomBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Behaviors/SelectorBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Behaviors/SequenceBehavior.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/BrAInS.csproj Download diff

1213
40
    <UseVSHostingProcess>false</UseVSHostingProcess>
40
    <UseVSHostingProcess>false</UseVSHostingProcess>
41
    <PlatformTarget>x86</PlatformTarget>
41
    <PlatformTarget>x86</PlatformTarget>
42
    <XnaCompressContent>false</XnaCompressContent>
42
    <XnaCompressContent>false</XnaCompressContent>
43
    <DocumentationFile>bin\x86\Debug\Brains.Framework.xml</DocumentationFile>
43
  </PropertyGroup>
44
  </PropertyGroup>
44
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
45
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
45
    <DebugType>pdbonly</DebugType>
46
    <DebugType>pdbonly</DebugType>
...
...
87
    <Compile Include="Behaviors\PathFinding\CyclicPathBehavior.cs" />
88
    <Compile Include="Behaviors\PathFinding\CyclicPathBehavior.cs" />
88
    <Compile Include="Behaviors\PathFinding\FindPathBehavior.cs" />
89
    <Compile Include="Behaviors\PathFinding\FindPathBehavior.cs" />
89
    <Compile Include="Behaviors\PathFinding\FollowPathBehavior.cs" />
90
    <Compile Include="Behaviors\PathFinding\FollowPathBehavior.cs" />
90
    <Compile Include="Consts.cs" />
91
    <Compile Include="Utility\AIConsts.cs" />
92
    <Compile Include="Map\Cluster.cs" />
91
    <Compile Include="Designer\BehaviorNode.cs" />
93
    <Compile Include="Designer\BehaviorNode.cs" />
92
    <Compile Include="Behaviors\BehaviorBase.cs" />
94
    <Compile Include="Behaviors\BehaviorBase.cs" />
93
    <Compile Include="Behaviors\BehaviorList.cs" />
95
    <Compile Include="Behaviors\BehaviorList.cs" />
...
...
98
    <Compile Include="Behaviors\IActionBehavior.cs" />
100
    <Compile Include="Behaviors\IActionBehavior.cs" />
99
    <Compile Include="Behaviors\ICompositeBehavior.cs" />
101
    <Compile Include="Behaviors\ICompositeBehavior.cs" />
100
    <Compile Include="Behaviors\SequenceBehavior.cs" />
102
    <Compile Include="Behaviors\SequenceBehavior.cs" />
101
    <Compile Include="Behaviors\BehaviorState.cs" />
102
    <Compile Include="Behaviors\IBehavior.cs" />
103
    <Compile Include="Behaviors\IBehavior.cs" />
103
    <Compile Include="Designer\BehaviorAttribute.cs" />
104
    <Compile Include="Designer\BehaviorAttribute.cs" />
104
    <Compile Include="ExtensionMethods.cs" />
105
    <Compile Include="Utility\Enums.cs" />
106
    <Compile Include="Utility\ExtensionMethods.cs" />
105
    <Compile Include="Feeler.cs" />
107
    <Compile Include="Feeler.cs" />
106
    <Compile Include="Grid.cs" />
108
    <Compile Include="Map\Grid.cs" />
107
    <Compile Include="GridCell.cs" />
109
    <Compile Include="Map\GridCell.cs" />
108
    <Compile Include="Agent.cs" />
110
    <Compile Include="Agent.cs" />
109
    <Compile Include="Label.cs" />
111
    <Compile Include="Label.cs" />
112
    <Compile Include="Utility\License.cs" />
110
    <Compile Include="Locomotion\LocomotionController.cs" />
113
    <Compile Include="Locomotion\LocomotionController.cs" />
111
    <Compile Include="Locomotion\LocomtionSteering.cs" />
114
    <Compile Include="Locomotion\LocomtionSteering.cs" />
112
    <Compile Include="Map.cs" />
115
    <Compile Include="Map\WorldMap.cs" />
113
    <Compile Include="PathFinding\PathFinder.cs" />
116
    <Compile Include="PathFinding\PathFinder.cs" />
114
    <Compile Include="PathFinding\PathFinderNode.cs" />
117
    <Compile Include="PathFinding\PathFinderNode.cs" />
115
    <Compile Include="PathFinding\NodeComparer.cs" />
118
    <Compile Include="PathFinding\NodeComparer.cs" />
116
    <Compile Include="PathFinding\PathFinderState.cs" />
117
    <Compile Include="PriorityQueue.cs" />
119
    <Compile Include="PriorityQueue.cs" />
118
    <Compile Include="Properties\AssemblyInfo.cs" />
120
    <Compile Include="Properties\AssemblyInfo.cs" />
119
    <Compile Include="QuadTree\FRect.cs" />
121
    <Compile Include="QuadTree\RectangleF.cs" />
120
    <Compile Include="QuadTree\QuadTree.cs" />
122
    <Compile Include="QuadTree\QuadTree.cs" />
121
    <Compile Include="QuadTree\QuadTreeNode.cs" />
123
    <Compile Include="QuadTree\QuadTreeNode.cs" />
122
    <Compile Include="QuadTree\QuadTreePositionItem.cs" />
124
    <Compile Include="QuadTree\QuadTreePositionItem.cs" />
123
    <Compile Include="Util.cs" />
125
    <Compile Include="Utility\Util.cs" />
124
    <Compile Include="World.cs" />
126
    <Compile Include="World.cs" />
125
  </ItemGroup>
127
  </ItemGroup>
126
  <ItemGroup>
128
  <ItemGroup>
...
...
175
  <Target Name="AfterBuild">
177
  <Target Name="AfterBuild">
176
  </Target>
178
  </Target>
177
  -->
179
  -->
180
  <PropertyGroup>
181
    <PostBuildEvent>xcopy /Y $(TargetDir)$(TargetName).* $(ProjectDir)..\..\build\Release\</PostBuildEvent>
182
  </PropertyGroup>
178
</Project>
183
</Project>

Updated src/BRAINSFramework/Designer/BehaviorAttribute.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Designer/BehaviorNode.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Linq;
4
using System.Linq;

Updated src/BRAINSFramework/Feeler.cs Download diff

1213
1
//****************************************************************//
1

2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
11
using System;
2
using System;
12
using System.Collections.Generic;
3
using System.Collections.Generic;
13
using System.Linq;
4
using System.Linq;
14
using System.Text;
5
using System.Text;
15
using Microsoft.Xna.Framework;
6
using Microsoft.Xna.Framework;
7
using Brains.Framework.Utility;
16
8
17
namespace Brains.Framework
9
namespace Brains.Framework
18
{
10
{
11
    /// <summary>
12
    /// Stores a direction and length of the feeler.
13
    /// </summary>
19
    public class Feeler
14
    public class Feeler
20
    {
15
    {
21
        public Agent Owner;
16
        private Agent _owner;
17
        /// <summary>
18
        /// Gets the owning agent of the Feeler
19
        /// </summary>
20
        public Agent Owner { get { return _owner; } }
21
        
22
        /// <summary>
23
        /// Gets or Sets the direction of the Feeler in local space coordinates
24
        /// </summary>
22
        public Vector2 Vector { get; set; }
25
        public Vector2 Vector { get; set; }
26
27
        /// <summary>
28
        /// Gets or sets the length of the Feeler
29
        /// </summary>
23
        public float Length { get; set; }
30
        public float Length { get; set; }
31
        
24
        public Vector2 TipWorldPosition
32
        public Vector2 TipWorldPosition
25
        {
33
        {
26
            get
34
            get
27
            {
35
            {
28
                Vector2 dest = Util.VectorToWorldSpace(Vector, Owner.Orientation);
36
                Vector2 dest = VectorUtil.VectorToWorldSpace(Vector, Owner.Orientation);
29
                Vector2 dd =Owner.Position + (dest * Length);
37
                Vector2 dd =Owner.Position + (dest * Length);
30
                return dd;
38
                return dd;
31
            }
39
            }
32
        }
40
        }
41
        
42
        /// <summary>
43
        /// Gets the direction of the Feeler in world coordinates
44
        /// </summary>
33
        public Vector2 WorldDirection
45
        public Vector2 WorldDirection
34
        {
46
        {
35
            get
47
            get
36
            {
48
            {
37
                Vector2 dest = Util.VectorToWorldSpace(Vector, Owner.Orientation);
49
                Vector2 dest = VectorUtil.VectorToWorldSpace(Vector, Owner.Orientation);
38
                return dest;
50
                return dest;
39
            }
51
            }
40
        }
52
        }
41
        public Feeler(Vector2 feeler,float length,Agent owner)
53
54
        internal Feeler(Vector2 feeler,float length,Agent owner)
42
        {
55
        {
43
            Length = length;
56
            Length = length;
44
            Vector = feeler;
57
            Vector = feeler;
45
            Owner = owner;
58
            _owner = owner;
46
        }
59
        }
60
47
    }
61
    }
62
63
48
}
64
}

Updated src/BRAINSFramework/Label.cs Download diff

1213
3
using System.Linq;
3
using System.Linq;
4
using System.Text;
4
using System.Text;
5
5
6
namespace Brains
6
namespace Brains.Framework
7
{
7
{
8
    public struct Label
8
    internal struct Label
9
    {
9
    {
10
        public float Value;
10
        public float Value;
11
        public float Value2;
11
        public float Value2;

Updated src/BRAINSFramework/Locomotion/LocomotionController.cs Download diff

1213
1
//****************************************************************//
1
using System;
2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
11
using System.Collections.Generic;
2
using System.Collections.Generic;
12
using System.Linq;
3
using System.Linq;
13
using System.Text;
4
using System.Text;
...
...
26
        /// <summary>
17
        /// <summary>
27
        /// Gets or sets the maximum speed the agent can travel at
18
        /// Gets or sets the maximum speed the agent can travel at
28
        /// </summary>
19
        /// </summary>
29
        public float MaxSpeed { get; set; }
20
        public float MaxSpeed 
30
        
21
        { 
22
            get; set; 
23
        }
24
       
31
        /// <summary>
25
        /// <summary>
32
        /// Gets or sets the maximum amount an Agent can rotate per second
26
        /// Gets or sets the maximum amount an Agent can rotate per second
33
        /// </summary>
27
        /// </summary>
34
        /// <remarks>Value is in radians</remarks>
28
        /// <remarks>Value is in radians</remarks>
35
        public float MaxRotation { get; set; }
29
        public float MaxRotation 
36
        
30
        { 
31
            get; set; 
32
        }
33
       
37
        /// <summary>
34
        /// <summary>
38
        /// Gets or sets the owning Agent
35
        /// Gets or sets the owning Agent
39
        /// </summary>
36
        /// </summary>
40
        public Agent Owner { get; set; }
37
        public Agent Owner 
41
        
38
        { 
39
            get; set;
40
        }
41
       
42
        /// <summary>
42
        /// <summary>
43
        /// Gets the current speed of the Agent
43
        /// Gets the current speed of the Agent
44
        /// </summary>
44
        /// </summary>
45
        public float Speed { get { return _speed; } }
45
        public float Speed 
46
        
46
        {
47
            get { return _speed; } 
48
        }
49
       
47
        /// <summary>
50
        /// <summary>
48
        /// Gets the velocity as a Vector2 of the Agent
51
        /// Gets the velocity as a Vector2 of the Agent
49
        /// </summary>
52
        /// </summary>
50
        public Vector2 Velocity { get { return _velocity; } }
53
        public Vector2 Velocity 
51
        
54
        { 
55
            get { return _velocity; } 
56
        }
57
       
52
        /// <summary>
58
        /// <summary>
53
        /// Gets or sets wether the Agent can move at all
59
        /// Gets or sets the value of wether the Agent will move
54
        /// </summary>
60
        /// </summary>
55
        public bool Stationary { get; set; }
61
        public bool Stationary 
56
        
62
        { 
63
            get; set;
64
        }
65
66
        /// <summary>
67
        /// Initializes a new instance of LocomotionController
68
        /// </summary>
57
        public LocomotionController()
69
        public LocomotionController()
58
        {
70
        {
59
            
71
            

Updated src/BRAINSFramework/Locomotion/LocomtionSteering.cs Download diff

1213
1
//****************************************************************//
1
using System;
2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
11
using System.Collections.Generic;
2
using System.Collections.Generic;
12
using System.Linq;
3
using System.Linq;
13
using System.Text;
4
using System.Text;
14
using Microsoft.Xna.Framework;
5
using Microsoft.Xna.Framework;
6
using Brains.Framework.Utility;
15
7
16
namespace Brains.Framework.Locomotion
8
namespace Brains.Framework.Locomotion
17
{
9
{
10
    /// <summary>
11
    /// This class is currently W.I.P
12
    /// </summary>
13
    /// <remarks></remarks>
18
    public class LocomtionSteering:LocomotionController
14
    public class LocomtionSteering:LocomotionController
19
    {
15
    {
16
        private static Random rand = new Random();
17
        private Vector2 _wanderTarget;
20
18
21
        Vector2 _steeringForce;
19
        public const float WanderRadius = 1.2f;
22
        private float ObstacleAvoidanceWeight=10;
20
        public const float WanderDistance = 2.0f;
23
        public float SeekWeight=1;
21
        public const float WanderJitter = 80;
22
         
23
        public enum Deceleration
24
        {
25
            Fast,
26
            Normal,
27
            Slow
28
        }
24
29
25
        public Vector2 SeekPos { get; set; }
26
        [Flags()]
30
        [Flags()]
27
        public enum BehaviorType
31
        public enum BehaviorType
28
        {
32
        {
29
            none = 0x00000,
33
            None = 0x00000,
30
            seek = 0x00002,
34
            Seek = 0x00002,
31
            flee = 0x00004,
35
            Flee= 0x00004,
32
            arrive = 0x00008,
36
            Arrive= 0x00008,
33
            wander = 0x00010,
37
            Wander = 0x00010,
34
            cohesion = 0x00020,
38
            //Cohesion= 0x00020,
35
            separation = 0x00040,
39
            //Seperation= 0x00040,
36
            allignment = 0x00080,
40
            //Alignment = 0x00080,
37
            obstacle_avoidance = 0x00100,
41
            //ObstacleAvoidance= 0x00100,
38
            wall_avoidance = 0x00200,
42
            //WallAvoidance = 0x00200,
39
            follow_path = 0x00400,
43
            //FollowPath = 0x00400,
40
            pursuit = 0x00800,
44
            Pursuit= 0x00800,
41
            evade = 0x01000,
45
            Evade = 0x01000,
42
            interpose = 0x02000,
46
            //Interpose= 0x02000,
43
            hide = 0x04000,
47
            //Hide= 0x04000,
44
            flock = 0x08000,
48
            //Flock = 0x08000,
45
            offset_pursuit = 0x10000,
49
            OffsetPursuit= 0x10000,
46
        }
50
        }
47
        Vector2 SteeringForce = Vector2.Zero;
48
        Vector2 Heading;
49
51
50
        float MaxForce=4;
52
        public void TurnAllOff()
51
        Vector2 Velocity;
52
        private BehaviorType _behaviorFlags;
53
        public LocomtionSteering()
54
        {
53
        {
55
            
54
            _behaviorFlags = BehaviorType.None;
56
        }
55
        }
57
        public void SeekOn(Vector2 target)
56
        
57
        public void TurnOnArrive(Vector2 targetPosition)
58
        {
58
        {
59
            TurnOn(BehaviorType.Arrive);
60
            SeekPos = targetPosition;
61
        }
62
        
63
        public void TurnOffArrive()
64
        {
65
            TurnOff(BehaviorType.Arrive);
66
        }
59
67
60
            if (!On(BehaviorType.seek))
68
        public void TurnOnSeek(Vector2 targetPosition)
61
                _behaviorFlags |= BehaviorType.seek;
69
        {
70
            SeekPos = targetPosition;
71
            TurnOn(BehaviorType.Seek);
72
        }
73
        
74
        public void TurnOffSeek()
75
        {
76
            TurnOff(BehaviorType.Seek);
77
        }
78
        
79
        public void TurnOnWander()
80
        {
81
            TurnOn(BehaviorType.Wander);
82
        }
83
        
84
        public void TurnOffWander()
85
        {
86
            TurnOff(BehaviorType.Wander);
87
        }
88
        
89
        public void TurnOnOffsetPursuit(Vector2 seek)
90
        {
62
91
63
            SeekPos= target;
64
        }
92
        }
93
        
94
        public void TurnOffOffsetPursuit()
95
        {
96
97
        }
98
        
99
        public void TurnOnPursuit(Vector2 seek)
100
        {
101
102
        }
103
        
104
        public void TurnOffPursuit()
105
        {
106
107
        }
108
        
109
        public void TurnOnFlee(Vector2 seek)
110
        {
111
112
        }
113
        
114
        public void TurnOffFlee()
115
        {
116
117
        }
118
      
119
        private Vector2 _steeringForce;
120
        private float ObstacleAvoidanceWeight=10;
121
        private Vector2 SteeringForce = Vector2.Zero;
122
        private Vector2 Heading;
123
        private float MaxForce = 4;
124
        private Vector2 Velocity;
125
        private BehaviorType _behaviorFlags;
126
127
128
        public float SeekWeight = 1;
129
        public float ArriveWeight= 1;
130
        public float WanderWeight= 1;
131
132
        public Vector2 SeekPos { get; set; }
133
        public Deceleration DecelerationSpeed { get; set; }
134
        public LocomtionSteering()
135
        {
136
            DecelerationSpeed = Deceleration.Normal;
137
        }
138
        float timeElapsed;
65
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
139
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
66
        {
140
        {
141
            timeElapsed = gameTime.GetElapsed();
67
            SteeringUpdate(gameTime.GetElapsed());
142
            SteeringUpdate(gameTime.GetElapsed());
68
        }
143
        }
144
69
        
145
        
70
        public void ObstacleAvoidanceOn()
146
71
        {
72
            _behaviorFlags |= BehaviorType.obstacle_avoidance;
73
        }
74
        private void SteeringUpdate(float elapsed)
147
        private void SteeringUpdate(float elapsed)
75
        {
148
        {
76
            if(!On(BehaviorType.seek))
77
                SeekOn(Owner.DesiredPosition);
78
            if (!On(BehaviorType.obstacle_avoidance))
79
                ObstacleAvoidanceOn();
80
81
            Vector2 OldPos = Owner.Position;
149
            Vector2 OldPos = Owner.Position;
82
            SeekPos = Owner.DesiredPosition;
83
            SteeringForce = Vector2.Zero;
150
            SteeringForce = Vector2.Zero;
84
151
85
            SteeringForce = Calculate();
152
            SteeringForce = Calculate();
...
...
94
            Owner.Position += Velocity * elapsed;
161
            Owner.Position += Velocity * elapsed;
95
            Owner.Orientation = Heading;// (float)Math.Atan2(Heading.Y, Heading.X);
162
            Owner.Orientation = Heading;// (float)Math.Atan2(Heading.Y, Heading.X);
96
        }
163
        }
97
        public void SetHeading(Vector2 new_heading)
164
165
        private void SetHeading(Vector2 new_heading)
98
        {
166
        {
99
            Heading= new_heading;
167
            Heading= new_heading;
100
            //Right = Perp(_heading);
168
            //Right = Perp(_heading);
101
        }
169
        }
102
        
170
        
103
        public Vector2 Truncate(Vector2 vec, float max)
171
        private Vector2 Truncate(Vector2 vec, float max)
104
        {
172
        {
105
            Vector2 __v = vec;
173
            Vector2 __v = vec;
106
            if (vec.Length() > max)
174
            if (vec.Length() > max)
...
...
114
        private Vector2 Calculate()
182
        private Vector2 Calculate()
115
        {
183
        {
116
            _steeringForce = Vector2.Zero;
184
            _steeringForce = Vector2.Zero;
117
            //if (isObstacleAvoidanceOn())
185
            
118
            //    CalculateNearbyObjects();
119
120
           
121
            _steeringForce = CalculatePrioritized();
186
            _steeringForce = CalculatePrioritized();
122
             
187
             
123
            return _steeringForce;
188
            return _steeringForce;
124
        }
189
        }
125
        private bool On(BehaviorType bt)
190
191
        private bool On(BehaviorType type)
126
        {
192
        {
127
            return (_behaviorFlags & bt) == bt;
193
            return (_behaviorFlags & type) == type;
128
        }
194
        }
195
        
196
        private void TurnOn(BehaviorType bt)
197
        {
198
            if(!On(bt))
199
                _behaviorFlags |= bt;
200
        }
201
202
        private void TurnOff(BehaviorType bt)
203
        {
204
            if(On(bt))
205
                _behaviorFlags ^= bt;
206
        }
207
        
129
        private Vector2 CalculatePrioritized()
208
        private Vector2 CalculatePrioritized()
130
        {
209
        {
131
210
132
            Vector2 force = Vector2.Zero;
211
            Vector2 force = Vector2.Zero;
133
212
134
            if (On(BehaviorType.obstacle_avoidance))
213
            if (On(BehaviorType.Seek))
135
            {
214
            {
136
                Vector2 __oa = ObstacleAvoidance();
215
                force = Seek(SeekPos);
137
                force = Vector2.Multiply(__oa, ObstacleAvoidanceWeight);
216
                force = Vector2.Multiply(force, SeekWeight);
217
138
                if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
218
                if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
139
            }
219
            }
140
220
141
            if (On(BehaviorType.seek))
221
222
223
            if (On(BehaviorType.Arrive))
142
            {
224
            {
143
                force = Seek(SeekPos);
225
                force = Arrive(SeekPos, DecelerationSpeed) * ArriveWeight;
144
                force = Vector2.Multiply(force, SeekWeight);
145
226
146
                if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
227
                if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
147
            }
228
            }
229
            
230
            if (On(BehaviorType.Wander))
231
            {
232
                force = Wander() * WanderWeight;
148
233
149
234
                if (!AccumulateForce(ref _steeringForce, force)) return _steeringForce;
235
            }
150
         
236
         
151
            return _steeringForce;
237
            return _steeringForce;
152
        }
238
        }
153
239
154
        private Vector2 Seek(Vector2 seek)
240
        
155
        {
241
        
156
            Vector2 tpos = seek - Owner.Position;
157
            Vector2 DesiredVelocity = Vector2.Zero;
158
            if (tpos != Vector2.Zero)
159
                DesiredVelocity = Vector2.Normalize(seek- Owner.Position) * MaxSpeed;
160
161
            return (DesiredVelocity - Velocity);
162
        }
163
        private bool AccumulateForce(ref Vector2 sf, Vector2 ForceToAdd)
242
        private bool AccumulateForce(ref Vector2 sf, Vector2 ForceToAdd)
164
        {
243
        {
165
            //first calculate how much steering force we have left to use
244
            //first calculate how much steering force we have left to use
...
...
186
            return true;
265
            return true;
187
        }
266
        }
188
      
267
      
189
190
        private Vector2 ObstacleAvoidance()
268
        private Vector2 ObstacleAvoidance()
191
        {
269
        {
192
            Vector2 _steer=Vector2.Zero;
270
            Vector2 _steer=Vector2.Zero;
193
            foreach (var item in Owner.ParentWorld.Map.Cluster[0].Cells)
271
            foreach (var item in Owner.ParentWorld.Map.ClusterGrid.Grids[0].Cells)
194
            {
272
            {
195
                if (item.Type == 0)
273
                if (item.Type == 0)
196
                {
274
                {
197
                    //Check feeler 1
275
                    //Check feeler 1
198
                    Ray _ra = new Ray(Owner.Position.ToVector3(), Owner.Feelers[1].WorldDirection.ToVector3());
276
                    Ray _ra = new Ray(Owner.Position.ToVector3(), Owner.Feelers[1].WorldDirection.ToVector3());
199
                    Vector3 tl = (item.Position - new Vector2(Owner.ParentWorld.Map.Cluster[0].CellSize / 2)).ToVector3();
277
                    Vector3 tl = (item.Position - new Vector2(Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize / 2)).ToVector3();
200
                    BoundingBox _box = new BoundingBox(
278
                    BoundingBox _box = new BoundingBox(
201
                        tl,
279
                        tl,
202
                        tl + new Vector3(Owner.ParentWorld.Map.Cluster[0].CellSize,
280
                        tl + new Vector3(Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize,
203
                            Owner.ParentWorld.Map.Cluster[0].CellSize, 0));
281
                            Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize, 0));
204
                    float? inte = _ra.Intersects(_box);
282
                    float? inte = _ra.Intersects(_box);
205
                    if (inte.HasValue)
283
                    if (inte.HasValue)
206
                    {
284
                    {
...
...
214
                    //Check feeler2
292
                    //Check feeler2
215
293
216
                     _ra = new Ray(Owner.Position.ToVector3(), Owner.Feelers[2].WorldDirection.ToVector3());
294
                     _ra = new Ray(Owner.Position.ToVector3(), Owner.Feelers[2].WorldDirection.ToVector3());
217
                     tl = (item.Position - new Vector2(Owner.ParentWorld.Map.Cluster[0].CellSize / 2)).ToVector3();
295
                     tl = (item.Position - new Vector2(Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize / 2)).ToVector3();
218
                     _box = new BoundingBox(
296
                     _box = new BoundingBox(
219
                        tl,
297
                        tl,
220
                        tl + new Vector3(Owner.ParentWorld.Map.Cluster[0].CellSize,
298
                        tl + new Vector3(Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize,
221
                            Owner.ParentWorld.Map.Cluster[0].CellSize, 0));
299
                            Owner.ParentWorld.Map.ClusterGrid.Grids[0].CellSize, 0));
222
                    inte = _ra.Intersects(_box);
300
                    inte = _ra.Intersects(_box);
223
                    if (inte.HasValue)
301
                    if (inte.HasValue)
224
                    {
302
                    {
...
...
232
310
233
            return _steer;
311
            return _steer;
234
        }
312
        }
313
       
314
        public Vector2 Wander()
315
        {
316
            double JitterThisTimeSlice = WanderJitter * timeElapsed;
317
318
            //first, add a small random vector to the target's position (RandomClamped
319
            //returns a value between -1 and 1)
320
            _wanderTarget+= new Vector2(RandomClamped() * WanderJitter,
321
                                  RandomClamped() * WanderJitter);
322
323
            if (_wanderTarget != Vector2.Zero) //reproject this new vector back on to a unit circle
324
                _wanderTarget.Normalize();
325
326
            //increase the length of the vector to the same as the radius
327
            //of the wander circle
328
            _wanderTarget *= WanderRadius;
329
330
            //move the target into a position WanderDist in front of the agent
331
            Vector2 _newtarget = _wanderTarget + new Vector2(WanderDistance, 0);
332
333
            //project the target into world space
334
            Vector2 _target = VectorUtil.PointToWorldSpace(_newtarget,
335
                                                 Owner.Orientation,
336
                                                 Owner.Position);
337
338
            return _target - Owner.Position;
339
            
340
        }
341
342
        public Vector2 Arrive(Vector2 targetPosition,Deceleration decelspeed)
343
        {
344
            Vector2 toTarget = targetPosition - Owner.Position;
345
346
            //calculate the distance to the target
347
            float dist = toTarget.Length();
348
349
            if (dist > 0)
350
            {
351
                //because Deceleration is enumerated as an int, this value is required
352
                //to provide fine tweaking of the deceleration..
353
                const float DecelerationTweaker = 0.3f;
354
355
                //calculate the speed required to reach the target given the desired
356
                //deceleration
357
                float speed = dist / ((float)decelspeed * DecelerationTweaker);
358
359
                //make sure the velocity does not exceed the max
360
                speed = Math.Min(speed, MaxSpeed);
361
362
                //from here proceed just like Seek except we don't need to normalize 
363
                //the ToTarget vector because we have already gone to the trouble
364
                //of calculating its length: dist. 
365
                Vector2 DesiredVelocity = toTarget * speed / dist;
366
367
                return (DesiredVelocity - Velocity);
368
            }
369
370
            return Vector2.Zero;
371
        }
372
        private Vector2 Seek(Vector2 seek)
373
        {
374
            Vector2 tpos = seek - Owner.Position;
375
            Vector2 DesiredVelocity = Vector2.Zero;
376
            if (tpos != Vector2.Zero)
377
                DesiredVelocity = Vector2.Normalize(seek - Owner.Position) * MaxSpeed;
378
379
            return (DesiredVelocity - Velocity);
380
        }
381
382
        float RandomClamped()
383
        {
384
            return rand.Next() - rand.Next();
385
        } 
235
    }
386
    }
236
}
387
}

Added src/BRAINSFramework/Map/Cluster.cs

Show contents

Added src/BRAINSFramework/Map/Grid.cs

Show contents

Added src/BRAINSFramework/Map/GridCell.cs

Show contents

Added src/BRAINSFramework/Map/WorldMap.cs

Show contents

Updated src/BRAINSFramework/PathFinding/NodeComparer.cs Download diff

1213
1
//****************************************************************//
1
2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Text;
4
using System.Text;

Updated src/BRAINSFramework/PathFinding/PathFinder.cs Download diff

1213
1
//****************************************************************//
1
2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Text;
4
using System.Text;
13
using AIFamework;
5
using Brains.Framework.Map;
14
6
7
15
namespace Brains.Framework.PathFinding
8
namespace Brains.Framework.PathFinding
16
{
9
{
17
    public class PathFinder
10
    public class PathFinder
...
...
323
            return _foundInOpenIndex ;
316
            return _foundInOpenIndex ;
324
        }
317
        }
325
        
318
        
326
        public enum HeuristicType
319
       
327
        {
328
            Euclidean,
329
            Manhattan,
330
            Diagonal
331
        }
332
        
320
        
333
321
334
    }
322
    }

Updated src/BRAINSFramework/PathFinding/PathFinderNode.cs Download diff

1213
1
//****************************************************************//
1
2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Text;
4
using System.Text;

Updated src/BRAINSFramework/PriorityQueue.cs Download diff

1213
1
//****************************************************************//
1
2
//********************copyright Rik Dodsworth 2009****************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
using System;
2
using System;
11
using System.Collections.Generic;
3
using System.Collections.Generic;
12
using System.Text;
4
using System.Text;
13
using System.Collections;
5
using System.Collections;
14
using System.Runtime.InteropServices;
6
using System.Runtime.InteropServices;
15
7
16
namespace AIFamework
8
namespace Brains.Framework
17
{
9
{
18
    /// <summary>
10
    /// <summary>
19
    /// Represents an item stored in a priority queue.
11
    /// Represents an item stored in a priority queue.
...
...
21
    /// <typeparam name="TValue">The type of object in the queue.</typeparam>
13
    /// <typeparam name="TValue">The type of object in the queue.</typeparam>
22
    /// <typeparam name="TPriority">The type of the priority field.</typeparam>
14
    /// <typeparam name="TPriority">The type of the priority field.</typeparam>
23
    [Serializable]
15
    [Serializable]
24
    [ComVisible(false)]
25
    public struct PriorityQueueItem<TValue, TPriority>
16
    public struct PriorityQueueItem<TValue, TPriority>
26
    {
17
    {
27
        private TValue value;
18
        private TValue value;

Updated src/BRAINSFramework/QuadTree/QuadTree.cs Download diff

1213
1
// *******************************************//
2
// *********Credits to Kyle Schouviller*******//
3
// *******for the original implementation*****//
4
// *******************************************//
1
using System;
5
using System;
2
using System.Collections.Generic;
6
using System.Collections.Generic;
3
using System.Text;
7
using System.Text;
...
...
5
using Microsoft.Xna.Framework.Graphics;
9
using Microsoft.Xna.Framework.Graphics;
6
10
7
11
8
namespace Brains.Framework
12
namespace Brains.Framework.QuadTree
9
{
13
{
10
    public class QuadTree<T>
14
    public class QuadTree<T>
11
    {
15
    {
12
       
16
13
        public static Rectangle GetRectangle(FRect rect)
17
        public static Rectangle GetRectangle(RectangleF rect)
14
        {
18
        {
15
            return new Rectangle((int)rect.Left, (int)rect.Top, (int)rect.Width, (int)rect.Height);
19
            return new Rectangle((int)rect.Left, (int)rect.Top, (int)rect.Width, (int)rect.Height);
16
        }
20
        }
17
        public void Count()
21
        
18
        {
19
            
20
        }
21
22
        #region Properties
23
24
        /// <summary>
22
        /// <summary>
25
        /// The head node of the QuadTree
23
        /// The head node of the QuadTree
26
        /// </summary>
24
        /// </summary>
...
...
29
        /// <summary>
27
        /// <summary>
30
        /// Gets the world rectangle
28
        /// Gets the world rectangle
31
        /// </summary>
29
        /// </summary>
32
        public FRect WorldRect
30
        public RectangleF WorldRect
33
        {
31
        {
34
            get { return headNode.Rect; }
32
            get { return headNode.Rect; }
35
        }
33
        }
...
...
39
        /// </summary>
37
        /// </summary>
40
        protected int maxItems;
38
        protected int maxItems;
41
39
42
        #endregion
43
44
        #region Initialization
45
46
        /// <summary>
40
        /// <summary>
47
        /// QuadTree constructor
41
        /// QuadTree constructor
48
        /// </summary>
42
        /// </summary>
49
        /// <param name="worldRect">The world rectangle for this QuadTree (a rectangle containing all items at all times)</param>
43
        /// <param name="worldRect">The world rectangle for this QuadTree (a rectangle containing all items at all times)</param>
50
        /// <param name="maxItems">Maximum number of items in any cell of the QuadTree before partitioning</param>
44
        /// <param name="maxItems">Maximum number of items in any cell of the QuadTree before partitioning</param>
51
        public QuadTree(FRect worldRect, int maxItems)
45
        public QuadTree(RectangleF worldRect, int maxItems)
52
        {
46
        {
53
            this.headNode = new QuadTreeNode<T>(worldRect, maxItems, Resize);
47
            this.headNode = new QuadTreeNode<T>(worldRect, maxItems, Resize);
54
            this.maxItems = maxItems;
48
            this.maxItems = maxItems;
...
...
61
        /// <param name="maxItems">Maximum number of items in any cell of the QuadTree before partitioning</param>
55
        /// <param name="maxItems">Maximum number of items in any cell of the QuadTree before partitioning</param>
62
        /// <remarks>This constructor is for ease of use</remarks>
56
        /// <remarks>This constructor is for ease of use</remarks>
63
        public QuadTree(Vector2 size, int maxItems)
57
        public QuadTree(Vector2 size, int maxItems)
64
            : this(new FRect(Vector2.Zero, size), maxItems)
58
            : this(new RectangleF(Vector2.Zero, size), maxItems)
65
        {
59
        {
66
            // Nothing extra to initialize
60
            // Nothing extra to initialize
67
        }
61
        }
68
62
      
69
        #endregion
70
71
        #region Methods
72
73
        /// <summary>
63
        /// <summary>
74
        /// Inserts an item into the QuadTree
64
        /// Inserts an item into the QuadTree
75
        /// </summary>
65
        /// </summary>
...
...
80
            // check if the world needs resizing
70
            // check if the world needs resizing
81
            if (!headNode.ContainsRect(item.Rect))
71
            if (!headNode.ContainsRect(item.Rect))
82
            {
72
            {
83
                Resize(new FRect(
73
                Resize(new RectangleF(
84
                    Vector2.Min(headNode.Rect.TopLeft, item.Rect.TopLeft) * 2,
74
                    Vector2.Min(headNode.Rect.TopLeft, item.Rect.TopLeft) * 2,
85
                    Vector2.Max(headNode.Rect.BottomRight, item.Rect.BottomRight) * 2));
75
                    Vector2.Max(headNode.Rect.BottomRight, item.Rect.BottomRight) * 2));
86
            }
76
            }
...
...
104
            // check if the world needs resizing
94
            // check if the world needs resizing
105
            if (!headNode.ContainsRect(item.Rect))
95
            if (!headNode.ContainsRect(item.Rect))
106
            {
96
            {
107
                Resize(new FRect(
97
                Resize(new RectangleF(
108
                    Vector2.Min(headNode.Rect.TopLeft, item.Rect.TopLeft) * 2,
98
                    Vector2.Min(headNode.Rect.TopLeft, item.Rect.TopLeft) * 2,
109
                    Vector2.Max(headNode.Rect.BottomRight, item.Rect.BottomRight) * 2));
99
                    Vector2.Max(headNode.Rect.BottomRight, item.Rect.BottomRight) * 2));
110
            }
100
            }
...
...
120
        /// </summary>
110
        /// </summary>
121
        /// <param name="newWorld">The new field</param>
111
        /// <param name="newWorld">The new field</param>
122
        /// <remarks>This is an expensive operation, so try to initialize the world to a big enough size</remarks>
112
        /// <remarks>This is an expensive operation, so try to initialize the world to a big enough size</remarks>
123
        public void Resize(FRect newWorld)
113
        public void Resize(RectangleF newWorld)
124
        {
114
        {
125
            // Get all of the items in the tree
115
            // Get all of the items in the tree
126
            List<QuadTreePositionItem<T>> Components = new List<QuadTreePositionItem<T>>();
116
            List<QuadTreePositionItem<T>> Components = new List<QuadTreePositionItem<T>>();
...
...
140
            }
130
            }
141
        }
131
        }
142
132
143
        #endregion
144
145
        #region Query methods
146
147
        /// <summary>
133
        /// <summary>
148
        /// Gets a list of items containing a specified point
134
        /// Gets a list of items containing a specified point
149
        /// </summary>
135
        /// </summary>
...
...
162
        /// </summary>
148
        /// </summary>
163
        /// <param name="Rect">The rectangle</param>
149
        /// <param name="Rect">The rectangle</param>
164
        /// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
150
        /// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
165
        public void GetItems(FRect Rect, ref List<QuadTreePositionItem<T>> ItemsList)
151
        public void GetItems(RectangleF Rect, ref List<QuadTreePositionItem<T>> ItemsList)
166
        {
152
        {
167
            if (ItemsList != null)
153
            if (ItemsList != null)
168
            {
154
            {
...
...
181
                headNode.GetAllItems(ref ItemsList);
167
                headNode.GetAllItems(ref ItemsList);
182
            }
168
            }
183
        }
169
        }
184
185
        #endregion
186
187
      
188
    }
170
    }
189
171
190
172

Updated src/BRAINSFramework/QuadTree/QuadTreeNode.cs Download diff

1213
1
// *******************************************************************************//
1
// *******************************************//
2
// *******************************************************************************//
2
// *********Credits to Kyle Schouviller*******//
3
// *************************Credits to Kyle Schouviller***************************//
3
// *******for the original implementation*****//
4
// *************************for the original implementation***********************//
4
// *******************************************//
5
// *******************************************************************************//
6
// *******************************************************************************//
7
// *******************************************************************************//
8
// *******************************************************************************//
9
// *******************************************************************************//
10
5
11
6
7
12
using System;
8
using System;
13
using System.Collections.Generic;
9
using System.Collections.Generic;
14
using Microsoft.Xna.Framework;
10
using Microsoft.Xna.Framework;
...
...
16
12
17
13
18
14
19
namespace Brains.Framework
15
namespace Brains.Framework.QuadTree
20
{
16
{
21
    public class QuadTreeNode<T>
17
    public class QuadTreeNode<T>
22
    {
18
    {
23
        public static Rectangle GetRectangle(FRect rect)
19
        public static Rectangle GetRectangle(RectangleF rect)
24
        {
20
        {
25
            return new Rectangle((int)rect.Left, (int)rect.Top, (int)rect.Width, (int)rect.Height);
21
            return new Rectangle((int)rect.Left, (int)rect.Top, (int)rect.Width, (int)rect.Height);
26
        }
22
        }
27
23
28
        #region Delegates
24
        
29
25
30
        /// <summary>
26
        /// <summary>
31
        /// World resize delegate
27
        /// World resize delegate
32
        /// </summary>
28
        /// </summary>
33
        /// <param name="newSize">The new world size</param>
29
        /// <param name="newSize">The new world size</param>
34
        public delegate void ResizeDelegate(FRect newSize);
30
        public delegate void ResizeDelegate(RectangleF newSize);
35
31
36
        #endregion
32
        
37
33
38
        #region Properties
34
        
39
35
40
        /// <summary>
36
        /// <summary>
41
        /// The rectangle of this node
37
        /// The rectangle of this node
42
        /// </summary>
38
        /// </summary>
43
        protected FRect rect;
39
        protected RectangleF rect;
44
40
45
        /// <summary>
41
        /// <summary>
46
        /// Gets the rectangle of this node
42
        /// Gets the rectangle of this node
47
        /// </summary>
43
        /// </summary>
48
        public FRect Rect
44
        public RectangleF Rect
49
        {
45
        {
50
            get { return rect; }
46
            get { return rect; }
51
            protected set { rect = value; }
47
            protected set { rect = value; }
...
...
97
        /// <param name="newSize">The new world size</param>
93
        /// <param name="newSize">The new world size</param>
98
        protected ResizeDelegate WorldResize;
94
        protected ResizeDelegate WorldResize;
99
95
100
        #endregion
96
        
101
97
102
        #region Initialization
98
        
103
99
104
        /// <summary>
100
        /// <summary>
105
        /// QuadTreeNode constructor
101
        /// QuadTreeNode constructor
...
...
107
        /// <param name="parentNode">The parent node of this QuadTreeNode</param>
103
        /// <param name="parentNode">The parent node of this QuadTreeNode</param>
108
        /// <param name="rect">The rectangle of the QuadTreeNode</param>
104
        /// <param name="rect">The rectangle of the QuadTreeNode</param>
109
        /// <param name="maxItems">Maximum number of items in the QuadTreeNode before partitioning</param>
105
        /// <param name="maxItems">Maximum number of items in the QuadTreeNode before partitioning</param>
110
        public QuadTreeNode(QuadTreeNode<T> parentNode, FRect rect, int maxItems)
106
        public QuadTreeNode(QuadTreeNode<T> parentNode, RectangleF rect, int maxItems)
111
        {
107
        {
112
            ParentNode = parentNode;
108
            ParentNode = parentNode;
113
            Rect = rect;
109
            Rect = rect;
...
...
122
        /// <param name="rect">The rectangle of the QuadTreeNode</param>
118
        /// <param name="rect">The rectangle of the QuadTreeNode</param>
123
        /// <param name="maxItems">Maximum number of items in the QuadTreeNode before partitioning</param>
119
        /// <param name="maxItems">Maximum number of items in the QuadTreeNode before partitioning</param>
124
        /// <param name="worldResize">The function to return the size</param>
120
        /// <param name="worldResize">The function to return the size</param>
125
        public QuadTreeNode(FRect rect, int maxItems, ResizeDelegate worldResize)
121
        public QuadTreeNode(RectangleF rect, int maxItems, ResizeDelegate worldResize)
126
        {
122
        {
127
            ParentNode = null;
123
            ParentNode = null;
128
            Rect = rect;
124
            Rect = rect;
...
...
132
            Items = new List<QuadTreePositionItem<T>>();
128
            Items = new List<QuadTreePositionItem<T>>();
133
        }
129
        }
134
130
135
        #endregion
131
        
136
132
137
        #region Insertion methods
133
        
138
134
139
        /// <summary>
135
        /// <summary>
140
        /// Insert an item in this node
136
        /// Insert an item in this node
...
...
216
            // Create the nodes
212
            // Create the nodes
217
            Vector2 MidPoint = Vector2.Divide(Vector2.Add(Rect.TopLeft, Rect.BottomRight), 2.0f);
213
            Vector2 MidPoint = Vector2.Divide(Vector2.Add(Rect.TopLeft, Rect.BottomRight), 2.0f);
218
214
219
            TopLeftNode = new QuadTreeNode<T>(this, new FRect(Rect.TopLeft, MidPoint), MaxItems);
215
            TopLeftNode = new QuadTreeNode<T>(this, new RectangleF(Rect.TopLeft, MidPoint), MaxItems);
220
            TopRightNode = new QuadTreeNode<T>(this, new FRect(new Vector2(MidPoint.X, Rect.Top), new Vector2(Rect.Right, MidPoint.Y)), MaxItems);
216
            TopRightNode = new QuadTreeNode<T>(this, new RectangleF(new Vector2(MidPoint.X, Rect.Top), new Vector2(Rect.Right, MidPoint.Y)), MaxItems);
221
            BottomLeftNode = new QuadTreeNode<T>(this, new FRect(new Vector2(Rect.Left, MidPoint.Y), new Vector2(MidPoint.X, Rect.Bottom)), MaxItems);
217
            BottomLeftNode = new QuadTreeNode<T>(this, new RectangleF(new Vector2(Rect.Left, MidPoint.Y), new Vector2(MidPoint.X, Rect.Bottom)), MaxItems);
222
            BottomRightNode = new QuadTreeNode<T>(this, new FRect(MidPoint, Rect.BottomRight), MaxItems);
218
            BottomRightNode = new QuadTreeNode<T>(this, new RectangleF(MidPoint, Rect.BottomRight), MaxItems);
223
219
224
            IsPartitioned = true;
220
            IsPartitioned = true;
225
221
...
...
234
            }
230
            }
235
        }
231
        }
236
232
237
        #endregion
233
        
238
234
239
        #region Query methods
235
        
240
236
241
        /// <summary>
237
        /// <summary>
242
        /// Gets a list of items containing a specified point
238
        /// Gets a list of items containing a specified point
...
...
272
        /// <param name="Rect">The rectangle</param>
268
        /// <param name="Rect">The rectangle</param>
273
        /// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
269
        /// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
274
        /// <remarks>ItemsFound is assumed to be initialized, and will not be cleared</remarks>
270
        /// <remarks>ItemsFound is assumed to be initialized, and will not be cleared</remarks>
275
        public void GetItems(FRect Rect, ref List<QuadTreePositionItem<T>> ItemsFound)
271
        public void GetItems(RectangleF Rect, ref List<QuadTreePositionItem<T>> ItemsFound)
276
        {
272
        {
277
            
273
            
278
            // test the point against this node
274
            // test the point against this node
...
...
359
            else return null;
355
            else return null;
360
        }
356
        }
361
357
362
        #endregion
358
        
363
359
364
        #region Destruction
360
        
365
361
366
        /// <summary>
362
        /// <summary>
367
        /// Destroys this node
363
        /// Destroys this node
...
...
418
            }
414
            }
419
        }
415
        }
420
416
421
        #endregion
417
        
422
418
423
        #region Observer methods
419
        
424
420
425
        /// <summary>
421
        /// <summary>
426
        /// Handles item movement
422
        /// Handles item movement
...
...
444
                    }
440
                    }
445
                    else if (!ContainsRect(item.Rect))
441
                    else if (!ContainsRect(item.Rect))
446
                    {
442
                    {
447
                        WorldResize(new FRect(
443
                        WorldResize(new RectangleF(
448
                             Vector2.Min(Rect.TopLeft, item.Rect.TopLeft) * 2,
444
                             Vector2.Min(Rect.TopLeft, item.Rect.TopLeft) * 2,
449
                             Vector2.Max(Rect.BottomRight, item.Rect.BottomRight) * 2));
445
                             Vector2.Max(Rect.BottomRight, item.Rect.BottomRight) * 2));
450
                    }
446
                    }
...
...
468
            RemoveItem(item);
464
            RemoveItem(item);
469
        }
465
        }
470
466
471
        #endregion
467
        
472
468
473
        #region Helper methods
469
        
474
470
475
        /// <summary>
471
        /// <summary>
476
        /// Tests whether this node contains a rectangle
472
        /// Tests whether this node contains a rectangle
477
        /// </summary>
473
        /// </summary>
478
        /// <param name="rect">The rectangle to test</param>
474
        /// <param name="rect">The rectangle to test</param>
479
        /// <returns>Whether or not this node contains the specified rectangle</returns>
475
        /// <returns>Whether or not this node contains the specified rectangle</returns>
480
        public bool ContainsRect(FRect rect)
476
        public bool ContainsRect(RectangleF rect)
481
        {
477
        {
482
            return (rect.TopLeft.X >= Rect.TopLeft.X &&
478
            return (rect.TopLeft.X >= Rect.TopLeft.X &&
483
                    rect.TopLeft.Y >= Rect.TopLeft.Y &&
479
                    rect.TopLeft.Y >= Rect.TopLeft.Y &&
...
...
485
                    rect.BottomRight.Y <= Rect.BottomRight.Y);
481
                    rect.BottomRight.Y <= Rect.BottomRight.Y);
486
        }
482
        }
487
483
488
        #endregion
484
        
489
485
490
    }
486
    }
491
}
487
}

Updated src/BRAINSFramework/QuadTree/QuadTreePositionItem.cs Download diff

1213
1
// *******************************************//
2
// *********Credits to Kyle Schouviller*******//
3
// *******for the original implementation*****//
4
// *******************************************//
1
using System;
5
using System;
2
using Microsoft.Xna.Framework;
6
using Microsoft.Xna.Framework;
3
7
4
namespace Brains.Framework
8
namespace Brains.Framework.QuadTree
5
{
9
{
6
    /// <summary>
10
    /// <summary>
7
    /// A position item in a quadtree
11
    /// A position item in a quadtree
...
...
11
    {
15
    {
12
        public QuadTreeNode<T> node;
16
        public QuadTreeNode<T> node;
13
        internal QuadTree<T> quadTree;
17
        internal QuadTree<T> quadTree;
14
        #region Events and Event Handlers
18
        
15
19
16
        /// <summary>
20
        /// <summary>
17
        /// Handles the move event
21
        /// Handles the move event
...
...
37
            node.RemoveItem(this);
41
            node.RemoveItem(this);
38
        }
42
        }
39
43
40
        #endregion
44
        
41
45
42
        #region Properties
46
        
43
47
44
        /// <summary>
48
        /// <summary>
45
        /// The center position of this item
49
        /// The center position of this item
...
...
91
        /// <summary>
95
        /// <summary>
92
        /// The rectangle containing this item
96
        /// The rectangle containing this item
93
        /// </summary>
97
        /// </summary>
94
        private FRect rect;
98
        private RectangleF rect;
95
99
96
        /// <summary>
100
        /// <summary>
97
        /// Gets a rectangle containing this item
101
        /// Gets a rectangle containing this item
98
        /// </summary>
102
        /// </summary>
99
        public FRect Rect
103
        public RectangleF Rect
100
        {
104
        {
101
            get { return rect; }
105
            get { return rect; }
102
        }
106
        }
...
...
115
            get { return parent; }
119
            get { return parent; }
116
        }
120
        }
117
121
118
        #endregion
122
        
119
123
120
        #region Initialization
124
        
121
125
122
        /// <summary>
126
        /// <summary>
123
        /// Creates a position item in a QuadTree
127
        /// Creates a position item in a QuadTree
...
...
127
        /// <param name="size">The size of this item</param>
131
        /// <param name="size">The size of this item</param>
128
        public QuadTreePositionItem(T parent, Vector2 position, Vector2 size)
132
        public QuadTreePositionItem(T parent, Vector2 position, Vector2 size)
129
        {
133
        {
130
            this.rect = new FRect(0f, 0f, 1f, 1f);
134
            this.rect = new RectangleF(0f, 0f, 1f, 1f);
131
135
132
            this.parent = parent;
136
            this.parent = parent;
133
            this.position = position ;
137
            this.position = position ;
...
...
135
            OnMove();
139
            OnMove();
136
        }
140
        }
137
141
138
        #endregion
142
        
139
143
140
        #region Methods
144
        
141
145
142
        /// <summary>
146
        /// <summary>
143
        /// Destroys this item and removes it from the QuadTree
147
        /// Destroys this item and removes it from the QuadTree
...
...
147
            OnDestroy();
151
            OnDestroy();
148
        }
152
        }
149
153
150
        #endregion
154
        
151
    }
155
    }
152
156
153
}
157
}

Added src/BRAINSFramework/QuadTree/RectangleF.cs

Show contents

Added src/BRAINSFramework/Utility/AIConsts.cs

Show contents

Added src/BRAINSFramework/Utility/Enums.cs

Show contents

Added src/BRAINSFramework/Utility/ExtensionMethods.cs

Show contents

Added src/BRAINSFramework/Utility/License.cs

Show contents

Added src/BRAINSFramework/Utility/Util.cs

Show contents

Updated src/BRAINSFramework/World.cs Download diff

1213
1
//****************************************************************//
1

2
//***********************(c) Rik Dodsworth 2009*******************//
3
//****************************************************************//
4
//***************************free to use**************************//
5
//****************************************************************//
6
//*******************please just leave this header****************//
7
//*************************for recognition************************//
8
//****************************************************************//
9
//****************************************************************//
10
11
using System;
2
using System;
12
using System.Collections.Generic;
3
using System.Collections.Generic;
13
using System.Linq;
4
using System.Linq;
...
...
15
using Microsoft.Xna.Framework;
6
using Microsoft.Xna.Framework;
16
using Microsoft.Xna.Framework.Graphics;
7
using Microsoft.Xna.Framework.Graphics;
17
using System.Globalization;
8
using System.Globalization;
18
9
using Brains.Framework.QuadTree;
10
using Brains.Framework.Map;
11
using Brains.Framework.Utility;
19
namespace Brains.Framework
12
namespace Brains.Framework
20
{
13
{
21
    /// <summary>
14
    /// <summary>
...
...
28
        //privates
21
        //privates
29
        //
22
        //
30
        private Texture2D _mapTexture;
23
        private Texture2D _mapTexture;
31
        private Map _map;
24
        private WorldMap _map;
32
        //
25
        //
33
        //Public properties
26
        //Public properties
34
        //
27
        //
...
...
42
        /// </summary>
35
        /// </summary>
43
        public List<Agent> Actors = new List<Agent>();
36
        public List<Agent> Actors = new List<Agent>();
44
37
45
        public Map Map { get{return _map; } }
38
        public WorldMap Map { get { return _map; } }
46
39
47
        //
40
        //
48
        //Public Methods
41
        //Public Methods
...
...
82
        /// <param name="maxclusterheight"></param>
75
        /// <param name="maxclusterheight"></param>
83
        internal void SetupMap(int width, int height, int cellsize, int maxclusterwidth,int maxclusterheight,Type gridType)
76
        internal void SetupMap(int width, int height, int cellsize, int maxclusterwidth,int maxclusterheight,Type gridType)
84
        {
77
        {
85
            _map = new Map(width,height);
78
            _map = new WorldMap(width,height);
86
            int totalCols = width / cellsize;
79
            int totalCols = width / cellsize;
87
            int totalRows= height / cellsize;
80
            int totalRows= height / cellsize;
88
            int clusterRows = totalCols / maxclusterwidth;
81
            int clusterRows = totalCols / maxclusterwidth;
89
            int clusterCols = totalRows/ maxclusterheight;
82
            int clusterCols = totalRows/ maxclusterheight;
90
            _map.Rows = clusterRows;
83
            _map.ClusterGrid.Setup(clusterRows, clusterCols);
91
            _map.Cols = clusterCols;
92
            Vector2 position = new Vector2();
84
            Vector2 position = new Vector2();
93
            for (int y = 0; y < clusterRows; y++)
85
            for (int y = 0; y < clusterRows; y++)
94
            {
86
            {
...
...
124
            Map.GridCellTree.GetAllItems(ref list);
116
            Map.GridCellTree.GetAllItems(ref list);
125
            //Grid grid = Map.Cluster[index];
117
            //Grid grid = Map.Cluster[index];
126
            //Annotate clearance.
118
            //Annotate clearance.
127
            for (int xx =Map.Cols * Map.Cluster[0].Cols - 1; xx >= 0; xx--)
119
            int rows = Map.ClusterGrid.Rows;
120
            int cols= Map.ClusterGrid.Cols;
121
            Cluster cluster = Map.ClusterGrid;
122
            for (int xx = cluster.TotalCols - 1; xx >= 0; xx--)
128
            {
123
            {
129
                for (int yy = Map.Rows * Map.Cluster[0].Rows - 1; yy >= 0; yy--)
124
                for (int yy = cluster.TotalRows - 1; yy >= 0; yy--)
130
                {
125
                {
131
                    //int x = xx / Map.Cluster[0].Cols;
126
                    //int x = xx / Map.Cluster[0].Cols;
132
                    //int y = yy/ Map.Cluster[0].Rows;
127
                    //int y = yy/ Map.Cluster[0].Rows;
133
                    //int index = y * Map.Cols + x;
128
                    //int index = y * Map.Cols + x;
134
                    //Grid grid = Map.Cluster[index];
129
                    //Grid grid = Map.Cluster[index];
135
                    int totalCols = Map.Cols * Map.Cluster[0].Cols;
136
                    int index = yy * totalCols + xx;
137
130
131
                    int index = yy * cluster.TotalCols + xx;
138
                    GridCell cell = Map.AllCells[index];// grid.GetCell(xx - (x * grid.Cols), yy - (y * grid.Rows));
132
                    GridCell cell = Map.AllCells[index];// grid.GetCell(xx - (x * grid.Cols), yy - (y * grid.Rows));
139
140
                    if (cell.Type == 0) //If the cell is not traversable, bail out.
133
                    if (cell.Type == 0) //If the cell is not traversable, bail out.
141
                        continue;
134
                        continue;
142
143
                    
135
                    
144
                    //Get adjacent tiles
136
                    //Get adjacent tiles
145
                    GridCell c1, c2, c3;
137
                    GridCell c1, c2, c3;
146
                    int y = (index - (index % totalCols)) / totalCols;
138
                    int y = (index - (index % cluster.TotalCols)) / cluster.TotalCols;
147
                    int x = index % totalCols;
139
                    int x = index % cluster.TotalCols;
148
                    int newindex = (y + 1) * totalCols + (x+1);
140
                    int newindex = (y + 1) * cluster.TotalCols + (x + 1);
149
                    c1 = null;
141
                    c1 = null;
150
                    c2 = null;
142
                    c2 = null;
151
                    c3 = null;
143
                    c3 = null;
152
                    if(Map.AllCells.Count > newindex)
144
                    if(Map.AllCells.Count > newindex)
153
                        c1 = Map.AllCells[newindex];// grid.GetCell(cell.X + 1, cell.Y + 1);
145
                        c1 = Map.AllCells[newindex];// grid.GetCell(cell.X + 1, cell.Y + 1);
154
                    
146
155
                    newindex = (y) * totalCols + (x + 1);
147
                    newindex = (y) * cluster.TotalCols + (x + 1);
156
                    if (Map.AllCells.Count > newindex)
148
                    if (Map.AllCells.Count > newindex)
157
                        c2 = Map.AllCells[newindex]; //grid.GetCell(cell.X + 1, cell.Y);
149
                        c2 = Map.AllCells[newindex]; //grid.GetCell(cell.X + 1, cell.Y);
158
                    newindex = (y + 1) * totalCols + (x);
150
                    newindex = (y + 1) * cluster.TotalCols + (x);
159
                    if (Map.AllCells.Count > newindex)
151
                    if (Map.AllCells.Count > newindex)
160
                        c3 = Map.AllCells[newindex]; //grid.GetCell(cell.X, cell.Y + 1);
152
                        c3 = Map.AllCells[newindex]; //grid.GetCell(cell.X, cell.Y + 1);
161
                    
153
                    
...
...
188
                }
180
                }
189
            }
181
            }
190
                        
182
                        
191
            if (Map.Cluster.Count > 1)
183
            if (cluster.Grids.Count > 1)
192
            {
184
            {
193
                int index = 0;
185
                int index = 0;
194
                for (int y = 0; y < Map.Rows; y++)
186
                for (int y = 0; y < rows; y++)
195
                {
187
                {
196
                    for (int x = 0; x < Map.Cols; x++)
188
                    for (int x = 0; x < cols; x++)
197
                    {
189
                    {
198
                        
190
                        
199
                        //Check right edge
191
                        //Check right edge
200
                        if (Map.Cluster.Count > index + 1)
192
                        if (cluster.Grids.Count > index + 1)
201
                        {
193
                        {
202
                            for (int row = 0; row < Map.Cluster[index].Rows; row++)
194
                            for (int row = 0; row < cluster.Grids[index].Rows; row++)
203
                            {
195
                            {
204
                                GridCell leftcell = Map.Cluster[index].GetCell(
196
                                GridCell leftcell = cluster.Grids[index].GetCell(
205
                                                Map.Cluster[index].Cols - 1,
197
                                                cluster.Grids[index].Cols - 1,
206
                                                row);
198
                                                row);
207
                                GridCell rightcell = Map.Cluster[index + 1].GetCell(
199
                                GridCell rightcell = cluster.Grids[index + 1].GetCell(
208
                                                0,
200
                                                0,
209
                                                row);
201
                                                row);
210
202
...
...
221
213
222
                        if (index > 1)
214
                        if (index > 1)
223
                        {
215
                        {
224
                            for (int row = 0; row < Map.Cluster[index].Rows; row++)
216
                            for (int row = 0; row < cluster.Grids[index].Rows; row++)
225
                            {
217
                            {
226
                                GridCell leftcell = Map.Cluster[index - 1].GetCell(
218
                                GridCell leftcell = cluster.Grids[index - 1].GetCell(
227
                                                    Map.Cluster[index - 1].Cols - 1,
219
                                                    cluster.Grids[index - 1].Cols - 1,
228
                                                    row);
220
                                                    row);
229
                                GridCell rightcell = Map.Cluster[index].GetCell(
221
                                GridCell rightcell = cluster.Grids[index].GetCell(
230
                                                0,
222
                                                0,
231
                                                row);
223
                                                row);
232
224
...
...
241
233
242
                        //check bottom edge
234
                        //check bottom edge
243
235
244
                        if (index + Map.Cols < Map.Cluster.Count)
236
                        if (index + cols < cluster.Grids.Count)
245
                        {
237
                        {
246
                            for (int col = 0; col < Map.Cluster[index].Cols; col++)
238
                            for (int col = 0; col < cluster.Grids[index].Cols; col++)
247
                            {
239
                            {
248
                                GridCell topcell = Map.Cluster[index].GetCell(
240
                                GridCell topcell = cluster.Grids[index].GetCell(
249
                                                col,
241
                                                col,
250
                                                Map.Cluster[index].Rows - 1);
242
                                                cluster.Grids[index].Rows - 1);
251
                                GridCell bottomcell = Map.Cluster[index + Map.Cols].GetCell(
243
                                GridCell bottomcell = cluster.Grids[index + cols].GetCell(
252
                                                col,
244
                                                col,
253
                                                0);
245
                                                0);
254
246
...
...
261
                            }
253
                            }
262
                        }
254
                        }
263
                        //Check Top
255
                        //Check Top
264
                        if (index - Map.Cols > 0)
256
                        if (index - cols > 0)
265
                        {
257
                        {
266
                            for (int col = 0; col < Map.Cluster[index].Cols; col++)
258
                            for (int col = 0; col < cluster.Grids[index].Cols; col++)
267
                            {
259
                            {
268
                                GridCell topcell = Map.Cluster[index - Map.Cols].GetCell(
260
                                GridCell topcell = cluster.Grids[index - cols].GetCell(
269
                                                col,
261
                                                col,
270
                                                Map.Cluster[index - Map.Cols].Rows - 1);
262
                                                cluster.Grids[index - cols].Rows - 1);
271
                                GridCell bottomcell = Map.Cluster[index].GetCell(
263
                                GridCell bottomcell = cluster.Grids[index].GetCell(
272
                                                col,
264
                                                col,
273
                                                0);
265
                                                0);
274
266
...
...
324
            {
316
            {
325
                for (int x = 0; x < cols; x++)
317
                for (int x = 0; x < cols; x++)
326
                {
318
                {
327
                    Grid item = Map.Cluster[index];
319
                    Grid item = Map.ClusterGrid.Grids[index];
328
                    index++;
320
                    index++;
329
321
330
                    int count = item.Rows * item.Cols;
322
                    int count = item.Rows * item.Cols;