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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
// 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.packcoordinators;
import java.util.*;
import java.util.Map;
import com.springrts.ai.*;
import com.springrts.ai.oo.clb.*;
import hughai.*;
import hughai.mapping.*;
import hughai.packcoordinators.*;
import hughai.unitdata.*;
import hughai.utils.*;
import hughai.basictypes.*;
// this carries out a spread search: sends units to nearest
// unseen los, otherwise sends all units around randomly
// can be used by scoutcontroller, or by tankcontroller, for example
public class SpreadSearchPackCoordinatorWithSearchGrid extends PackCoordinator
{
// HashMap< Integer, UnitDef> UnitDefListByDeployedId;
TerrainPos targetpos;
HashSet<Unit> moveFailed = new HashSet<Unit>();
HashMap<Unit, TerrainPos> lastPosByUnit = new HashMap<Unit, TerrainPos>(); // to check the thing is not stuck...
HashMap<Unit, Integer> stuckCountByUnit = new HashMap<Unit, Integer>();
Cache<Unit> unitsinselfdestruct = new Cache<Unit>( 180 );
PlayerObjects playerObjects;
UnitController unitController;
Config config;
int recentmeansnumframes = 5000;
int mapwidth;
int mapheight;
// can pass in pointer to a hashtable in another class if we want
// ie other class can directly modify our hashtable
public SpreadSearchPackCoordinatorWithSearchGrid(
PlayerObjects playerObjects )
{
super( playerObjects );
this.playerObjects = playerObjects;
this.unitController = playerObjects.getUnitController();
config = playerObjects.getConfig();
mapwidth = aicallback.getMap().getWidth();
mapheight = aicallback.getMap().getHeight();
csai.registerGameListener( new GameListenerHandler() );
}
// does NOT imply Activate()
@Override
public void SetTarget( TerrainPos newtarget )
{
this.targetpos = newtarget;
//Activate();
}
@Override
public void Activate()
{
if( !activated )
{
logfile.WriteLine( "SpreadSearchPackCoordinatorWithSearchGrid initiating spreadsearch" );
activated = true;
restartedfrompause = true;
Recoordinate();
}
}
@Override
public void Disactivate()
{
if( activated )
{
activated = false;
logfile.WriteLine( "SpreadSearchPackCoordinatorWithSearchGrid shutting down" );
csai.UnregisterVoiceCommand("dumpsearchgrid" );
}
}
boolean restartedfrompause = true;
Float3 lasttargetpos = null;
void Recoordinate()
{
if( !activated )
{
return;
}
// logfile.WriteLine("SpreadSearchPackCoordinatorWithSearchGrid.Recoordinate() restartedfrompause " + restartedfrompause );
// int frame = aicallback.getGame().getCurrentFrame();
int frame = playerObjects.getFrameController().getFrame();
// just send each unit to random destination
// in unit onidle, we send each unit to a new place
for( Unit unit : unitsControlled )
{
boolean stuck = false;
// logfile.WriteLine( "unitsControlled unit " + unit.getUnitId() + " " + unit.hashCode() + " in movefailed? " + moveFailed.contains( unit ) );
// logfile.WriteLine( "Search Unit " + unit.getUnitId()
// + " lastpos: " + lastPosByUnit.get( unit )
// + " thispos: " + unitController.getPos( unit )
// + " same? " + unitController.getPos( unit ).equals( lastPosByUnit.get( unit ) ) );
if( restartedfrompause ) {
logfile.WriteLine("SpreadSearchPackCoordinatorWithSearchGrid.Explorewith() reason: restartedfrompause " + unit.getUnitId() );
ExploreWith( unit, false );
// } else if( moveFailed.contains( unit ) ) {
// logfile.WriteLine("SpreadSearchPackCoordinatorWithSearchGrid.Explorewith() reason: movefailed " + unit.getUnitId() );
// ExploreWith( unit, false );
} else if( unitController.getPos( unit ).equals( lastPosByUnit.get( unit ) ) ) {
logfile.WriteLine("SpreadSearchPackCoordinatorWithSearchGrid.Explorewith() reason: hasn't moved " + unit.getUnitId() );
stuck = true;
if( !stuckCountByUnit.containsKey( unit ) || stuckCountByUnit.get( unit ) <= 10 ) {
ExploreWith( unit, true );
}
}
if( stuck ) {
if( stuckCountByUnit.containsKey( unit ) ) {
stuckCountByUnit.put( unit, stuckCountByUnit.get( unit ) + 1 );
if( stuckCountByUnit.get( unit ) > 10 ){
logfile.WriteLine( "stuck count for " + unit.getUnitId()
+ " " +
unitController.getUnitDef( unit ).getHumanName() +
" too high: self-destructing." );
if( !unitsinselfdestruct.isInCache( frame, unit ) ) {
giveOrderWrapper.SelfDestruct( unit );
unitsinselfdestruct.cache( frame, unit );
}
}
} else {
stuckCountByUnit.put( unit, 1 );
}
} else {
stuckCountByUnit.remove( unit );
}
lastPosByUnit.put( unit, unitController.getPos( unit ) );
}
restartedfrompause = false;
moveFailed.clear();
}
void HandleMoveFailed() {
for( Unit unit : moveFailed ) {
logfile.WriteLine( "Triggering new explore for move failed unit " + unit.getUnitId() + " " +
unitController.getUnitDef( unit ).getHumanName() );
ExploreWith( unit, true );
}
moveFailed.clear();
}
HashMap< Unit, Integer > lastexploretime = new HashMap< Unit, Integer >();
public void ExploreWith( Unit unit, boolean forcerandomdestination )
{
// forcerandomdestination = true;
if (lastexploretime.containsKey(unit) &&
playerObjects.getFrameController().getFrame() - lastexploretime.get( unit ) < 30)
{
return;
}
boolean destinationfound = false;
TerrainPos currentpos = unitController.getPos( unit );
MovementMaps movementmaps = maps.getMovementMaps();
UnitDef unitdef = unitController.getUnitDef( unit );
int currentarea = movementmaps.GetArea( unitdef, currentpos );
LosMap losmap = maps.getLosMap();
if( csai.DebugOn )
{
logfile.WriteLine("SpreadSearchWithSearchGrid explorewith unit "
+ unit.getUnitId() + " "
+ unitdef.getHumanName()
+ " area: " + currentarea );
}
/*
int numtriesleft = 30; // just try a few times then give up
// maybe there is a better way to do this?
while( !destinationfound )
{
Float3 destination = GetRandomDestination();
// logfile.WriteLine( "SpreadSearchWithSearchGrid attempt " + destination.toString() );
int mapx = (int)( destination.x / 16 );
int mapy = (int)( destination.z / 16 );
if( ( movementmaps.GetArea( unitdef, destination ) == currentarea &&
losmap.LastSeenFrameCount[ mapx, mapy ] < recentmeansnumframes || numtriesleft <= 0 ) )
{
logfile.WriteLine( "Looks good. Go. " + numtriesleft + " retriesleft" );
if( csai.DebugOn )
{
aicallback.CreateLineFigure( currentpos, destination,10,true,400,0);
aicallback.DrawUnit( "ARMFAV", destination, 0.0f, 400, aicallback.GetMyAllyTeam(), true, true);
}
aicallback.GiveOrder( unitid, new Command( Command.CMD_MOVE, destination.ToDoubleArray() ) );
return;
}
numtriesleft--;
}
*/
// find nearest, area that hasnt had los recently
//int maxradius = Math.Max( aicallback.getMap().getWidth()(), aicallback.getMap().getHeight()() ) / 2;
//for( int radius =
TerrainPos nextpoint = null;
if( !forcerandomdestination ) {
nextpoint = losHelper.GetNearestUnseen(currentpos, unitdef, 12000 );
}
if (nextpoint == null)
{
nextpoint = GetRandomDestination(currentarea);
}
if (nextpoint == null ) {
return;
}
lastexploretime.put(unit, playerObjects.getFrameController().getFrame());
giveOrderWrapper.MoveTo(unit, nextpoint );
if( config.isDebug() ) {
drawingUtils.AddLine( currentpos, nextpoint );
drawingUtils.DrawUnit( config.getSpreadsearchnextmovemarkerunitname(),
nextpoint );
}
}
// note: this needs to know whether we are a vehicle, or whatever
TerrainPos GetRandomDestination( int currentarea )
{
int attempts = 20;
while( attempts > 0 ) {
TerrainPos destination = new TerrainPos();
destination.x = random.nextFloat() * aicallback.getMap().getWidth() * maps.getMovementMaps().SQUARE_SIZE;
destination.z = random.nextFloat() * aicallback.getMap().getHeight() * maps.getMovementMaps().SQUARE_SIZE;
destination.y = aicallback.getMap().getElevationAt( destination.x, destination.y );
int newarea = maps.getMovementMaps().GetVehicleArea( destination );
if( newarea == currentarea ) {
return destination;
}
attempts--;
}
return null;
}
class GameListenerHandler extends GameAdapter {
int totalticks = 0;
// int ticks = 0;
@Override
public void Tick( int frame )
{
// ticks++;
totalticks++;
//Reappraise();
//HandleMoveFailed();
Recoordinate();
// if( ticks >= 30 )
// {
//Recoordinate();
// ticks = 0;
// }
}
@Override
public void UnitIdle( Unit unit )
{
if( activated )
{
if( unitsControlled.contains( unit ) )
{
ExploreWith( unit, false );
}
}
}
@Override
public void UnitMoveFailed( Unit unit ) {
if( activated )
{
if( unitsControlled.contains( unit ) )
{
if( !moveFailed.contains( unit )) {
moveFailed.add( unit );
csai.sendTextMessage( "Move failed for " + unit.getUnitId() + " " +
unitController.getUnitDef( unit ).getHumanName() );
}
}
}
}
}
}
|