1 // Copyright Spring project, Hugh Perkins 2006, 2009
2 // hughperkins@gmail.com http://manageddreams.com
3 //
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 //  more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program in the file licence.txt; if not, write to the
16 // Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
17 // 1307 USA
18 // You can find the licence also on the web at:
19 // http://www.opensource.org/licenses/gpl-license.php
20 //
21 // ======================================================================================
22 //
23 
24 package hughai.mapping;
25 
26 import java.io.*;
27 
28 import com.springrts.ai.*;
29 import com.springrts.ai.oo.*;
30 import com.springrts.ai.oo.clb.*;
31 
32 import hughai.basictypes.*;
33 import hughai.*;
34 import hughai.mapping.HeightMap.HeightMapPos;
35 import hughai.utils.*;
36 import hughai.ui.*;
37 
38 public class SlopeMap
39 {
40    public static final int granularity = 2;
41 
42    public static class SlopeMapPos extends Int2 {
SlopeMapPos()43       public SlopeMapPos() {
44 
45       }
SlopeMapPos( Int2 int2 )46       public SlopeMapPos( Int2 int2 ) {
47          x = int2.x;
48          y = int2.y;
49       }
SlopeMapPos( int x, int y )50       public SlopeMapPos( int x, int y ) {
51          super( x, y );
52       }
toTerrainPos()53       public TerrainPos toTerrainPos() {
54          return new TerrainPos( x * 8 * granularity, 0, y * 8 * granularity );
55       }
fromTerrainPos( TerrainPos terrainPos )56       public static SlopeMapPos fromTerrainPos( TerrainPos terrainPos ) {
57          return new SlopeMapPos( (int)terrainPos.x / 8 / granularity,
58                (int)terrainPos.z / 8 / granularity );
59       }
fromHeightMapPos( HeightMapPos heightMapPos )60       public static SlopeMapPos fromHeightMapPos( HeightMapPos heightMapPos ) {
61          return new SlopeMapPos( (int)heightMapPos.x / granularity,
62                (int)heightMapPos.y / granularity );
63       }
64    }
65 
66    CSAI csai;
67    OOAICallback aicallback;
68    LogFile logfile;
69    //	MovementMaps movementMaps;
70    Map gameMap;
71    //	HeightMap heightMap;
72    //	Maps playerObjects.getMaps();
73    PlayerObjects playerObjects;
74 
75    private float[][] SlopeMap;
76 
SlopeMap( PlayerObjects playerObjects )77    public SlopeMap( PlayerObjects playerObjects )
78    {
79       csai = playerObjects.getCSAI();
80       aicallback = csai.aicallback;
81       logfile = playerObjects.getLogFile();
82       gameMap = aicallback.getMap();
83       this.playerObjects = playerObjects;
84       //		movementMaps = playerObjects.getMovementMaps();
85       //		heightMap = playerObjects.getHeightMap();
86       //		playerObjects.getMaps() = playerObjects.getMaps();
87 
88       playerObjects.getMainUI().registerButton( "Dump slopemap distribution",
89             new ButtonSlopeDistribution() );
90    }
91 
92    class ButtonSlopeDistribution implements MainUI.ButtonHandler {
93       @Override
go()94       public void go() {
95          float[][]slopemap = GetSlopeMap();
96          logfile.WriteLine( "Slopemap distribution:" );
97          new MapHelper( playerObjects ).DumpMapDistribution( slopemap );
98       }
99    }
100 
getSlopeAt( SlopeMapPos slopeMapPos )101    public float getSlopeAt( SlopeMapPos slopeMapPos ) {
102       GetSlopeMap();
103       return SlopeMap[slopeMapPos.x][slopeMapPos.y];
104    }
105 
106    // ported from Spring's ReadMap.cpp by Hugh Perkins
GetSlopeMap()107    private float[][]GetSlopeMap()
108    {
109       if( SlopeMap != null ) {
110          return SlopeMap;
111       }
112       int mapwidth = gameMap.getWidth();
113       int mapheight = gameMap.getHeight();
114 
115       int slopemapwidth = mapwidth / granularity;
116       int slopemapheight = mapheight / granularity;
117 
118       int squaresize = playerObjects.getMaps().getMovementMaps().SQUARE_SIZE; // jsut to save typing...
119 
120 //      float[][] heightmap = playerObjects.getMaps().getHeightMap().GetHeightMap();
121       HeightMap heightMap = playerObjects.getMaps().getHeightMap();
122       logfile.WriteLine( "Getting heightmap, this could take a while... " );
123 
124       // ArrayIndexer heightmapindexer = new ArrayIndexer( mapwidth + 1, mapheight + 1 );
125       logfile.WriteLine("calculating slopes..." );
126       logfile.WriteLine("mapwidth: " + slopemapwidth + " " + slopemapheight);
127 
128       SlopeMap = new float[ slopemapwidth][ slopemapheight ];
129       for(int y = 2; y < mapheight - 2; y+= 2)
130       {
131          for(int x = 2; x < mapwidth - 2; x+= 2)
132          {
133 //            HeightMapPos heightMapPos = new HeightMapPos( x, y );
134             Float3 e1 = new Float3(
135                   -squaresize * 4,
136                   heightMap.getElevationAt( new HeightMapPos( x - 1, y - 1 ) )
137                       - heightMap.getElevationAt( new HeightMapPos( x + 3, y - 1) ),
138 //                  heightmap[x - 1][ y - 1] - heightmap[x + 3][ y - 1]
139                   0);
140             Float3 e2 = new Float3(
141                   0,
142                   heightMap.getElevationAt( new HeightMapPos( x - 1, y - 1 ) )
143                   - heightMap.getElevationAt( new HeightMapPos( x - 1, y + 3) ),
144 //                  heightmap[x - 1][ y - 1] - heightmap[x - 1][ y + 3],
145                   -squaresize * 4);
146 
147             Float3 n=e2.Cross( e1 );
148 
149             n.Normalize();
150 
151             e1 = new Float3(
152                   squaresize * 4,
153                   heightMap.getElevationAt( new HeightMapPos( x + 3, y + 3 ) )
154                   - heightMap.getElevationAt( new HeightMapPos( x - 1, y + 3) ),
155 //                  heightmap[x + 3][ y + 3] - heightmap[x - 1][ y + 3],
156                   0);
157             e2 = new Float3(
158                   0,
159                   heightMap.getElevationAt( new HeightMapPos( x + 3, y + 3 ) )
160                   - heightMap.getElevationAt( new HeightMapPos( x + 3, y - 1 ) ),
161 //                  heightmap[x + 3][ y + 3] - heightmap[x + 3][ y - 1],
162                   squaresize * 4);
163 
164             Float3 n2 = e2.Cross(e1);
165             n2.Normalize();
166 
167             SlopeMap[ x / granularity][ y / granularity ]= 1 - ( n.y + n2.y ) * 0.5f;
168          }
169       }
170       logfile.WriteLine("... slopes calculated" );
171       return SlopeMap;
172    }
173 }
174