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
|
// Copyright 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 com.springrts.ai.*;
import com.springrts.ai.oo.clb.*;
import hughai.*;
import hughai.mapping.MovementMaps.MovementMapPos;
import hughai.unitdata.*;
import hughai.utils.*;
import hughai.basictypes.*;
// arguably reservations should go through this class, since this
// is essentially the controller and BuildMap is the model
public class BuildPlanner
{
public final int BuildMargin = 8;
CSAI csai;
OOAICallback aicallback;
LogFile logfile;
UnitDefHelp unitdefhelp;
Maps maps;
// BuildMap buildMap;
// MovementMaps movementMaps;
int mapwidth;
int mapheight;
public BuildPlanner(PlayerObjects playerObjects)
{
csai = playerObjects.getCSAI();
aicallback = csai.aicallback;
logfile = playerObjects.getLogFile();
unitdefhelp = playerObjects.getUnitDefHelp();
// buildMap = playerObjects.getBuildMap();
// movementMaps = playerObjects.getMovementMaps();
maps = playerObjects.getMaps();
mapwidth = aicallback.getMap().getWidth();
mapheight = aicallback.getMap().getHeight();
}
public TerrainPos ClosestBuildSite( UnitDef unitdef, TerrainPos approximatepos, int maxposradius, int mindistancemapunits )
{
// ok, so plan is we work our way out in a kindof spiral
// we start in centre, and increase radius, and work around in a square at each radius, until we find something
int centrex = (int)( approximatepos.x / 8 );
int centrey = (int)( approximatepos.z / 8 );
//int radius = 0;
int radius = mindistancemapunits;
int unitsizexwithmargin = unitdef.getXSize() + 2 * BuildMargin;
int unitsizeywithmargin = unitdef.getZSize() + 2 * BuildMargin;
if( unitdefhelp.IsFactory( unitdef ) )
{
unitsizeywithmargin += 8;
centrey += 4;
}
// while( radius < mapwidth || radius < mapheight ) // hopefully we never get quite this far...
while( radius < ( maxposradius / 8 ) )
{
// logfile.WriteLine( "ClosestBuildSite radius " + radius );
for( int deltax = -radius; deltax <= radius; deltax++ )
{
for( int deltay = -radius; deltay <= radius; deltay++ )
{
if( deltax == radius || deltax == -radius || deltay == radius || deltay == -radius ) // ignore the hollow centre of square
{
// logfile.WriteLine( "delta " + deltax + " " + deltay );
boolean positionok = true;
// go through each square in proposed site, check not build on
for( int buildmapy = centrey - unitsizeywithmargin / 2; positionok && buildmapy < centrey + unitsizeywithmargin / 2; buildmapy++ )
{
//String logline = "";
for( int buildmapx = centrex - unitsizexwithmargin / 2; positionok && buildmapx < centrex + unitsizexwithmargin / 2; buildmapx++ )
{
// int thisx = buildmapx + deltax;
// int thisy = buildmapy + deltay;
BuildMap.BuildMapPos buildMapPos = new BuildMap.BuildMapPos( buildmapx + deltax, buildmapy + deltay );
HeightMap.HeightMapPos heightMapPos = buildMapPos.toHeightMapPos();
MovementMapPos movementMapPos = MovementMapPos.fromHeightMapPos( heightMapPos );
if( !buildMapPos.validate()
|| !maps.getBuildMap().isSquareAvailable( buildMapPos )
|| !maps.getMovementMaps().vehicleCanMoveOk( movementMapPos ) )
// thisx < 0 || thisy < 0 || thisx >= mapwidth || thisy >= mapwidth ||
// !maps.getBuildMap().isSquareAvailable[ thisx][thisy ] ||
// !maps.getMovementMaps().vehiclemap[ thisx / 2][ thisy / 2 ] )
{
//logfile.WriteLine( "check square " + buildmapx + " " + buildmapy + " NOK" );
positionok = false;
// logline += "*";
}
else
{
// logline += "-";
}
//logfile.WriteLine( "check square " + buildmapx + " " + buildmapy + "Ok" );
}
// logfile.WriteLine( logline );
}
// logfile.WriteLine("");
if( positionok )
{
return new TerrainPos( ( centrex + deltax ) * 8, 0, ( centrey + deltay ) * 8 );
}
}
}
}
radius++;
}
return null;
}
}
|