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