root/src/BRAINSFramework/World.cs

113
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;