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
|
// 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.controllers.level2;
import java.util.*;
import java.util.Map;
import com.springrts.ai.*;
import com.springrts.ai.oo.clb.*;
import hughai.CSAI;
import hughai.EnemyTracker;
import hughai.GameAdapter;
import hughai.GiveOrderWrapper;
import hughai.PlayerObjects;
import hughai.VoiceCommandHandler;
import hughai.EnemyTracker.EnemyAdapter;
import hughai.basictypes.*;
import hughai.mapping.*;
import hughai.packcoordinators.*;
import hughai.unitdata.*;
import hughai.utils.*;
public class ScoutControllerRaider
{
public int nearbyforenemiesmeans = 250;
public int enemysightedsearchradius = 1000;
CSAI csai;
OOAICallback aicallback;
LogFile logfile;
EnemyTracker enemyTracker;
GiveOrderWrapper giveOrderWrapper;
UnitController unitController;
Maps maps;
Config config;
// SpreadSearchPackCoordinatorWithSearchGrid searchcoordinator;
SpreadSearchPackCoordinatorWithSearchGrid searchcoordinator;
List< Unit > ScoutUnits = new ArrayList< Unit>();
HashSet< Unit > movefailed = new HashSet<Unit>();
Random random;
public ScoutControllerRaider( PlayerObjects playerObjects )
{
csai = playerObjects.getCSAI();
aicallback = csai.aicallback;
logfile = playerObjects.getLogFile();
//unitcontroller = playerObjects.getUnitController();
enemyTracker = playerObjects.getEnemyTracker();
giveOrderWrapper = playerObjects.getGiveOrderWrapper();
maps = playerObjects.getMaps();
unitController = playerObjects.getUnitController();
config = playerObjects.getConfig();
random = new Random();
// searchcoordinator = new SpreadSearchPackCoordinatorWithSearchGrid(
// playerObjects);
searchcoordinator = new SpreadSearchPackCoordinatorWithSearchGrid(
playerObjects);
logfile.WriteLine( "*ScoutControllerRaider initialized*" );
}
boolean Active = false;
public void Activate()
{
if( !Active )
{
logfile.WriteLine( "activating " + this.getClass().getSimpleName() );
csai.registerGameListener(new GameListenerHandler());
csai.RegisterVoiceCommand( "countscouts", new VoiceCommandCountScouts() );
// enemyTracker.registerListener( new EnemyTrackerHandler() );
searchcoordinator.Activate();
Active = true;
}
}
public void Disactivate()
{
if( Active )
{
csai.unregisterGameListener(new GameListenerHandler());
csai.UnregisterVoiceCommand( "countscouts" );
// enemyTracker.unregisterGameListener( new EnemyTrackerHandler() );
searchcoordinator.Disactivate();
Active = false;
}
}
// planned, controller can control any units at its discretion, for now:
public void AssignUnits( Collection<Unit> units ){
for( Unit unit : units ) {
ScoutUnits.add( unit );
logfile.WriteLine( "New scoutcontrollerraider unit: " +
unit.getUnitId() + " " + unit.getDef().getHumanName() );
searchcoordinator.AssignUnits( units );
}
} // give units to this controller
public void RevokeUnits( Collection<Unit> units ){
for( Unit unit : units ) {
ScoutUnits.remove( unit );
searchcoordinator.RevokeUnits( units );
}
} // remove these units from this controller
// planned, not used yet, controller can use energy and metal at its discretion for now:
public void AssignEnergy( int energy ){} // give energy to controller; negative to revoke
public void AssignMetal( int metal ){} // give metal to this controller; negative to revoke
public void AssignPower( double power ){} // assign continuous power flow to this controller; negative for reverse flow
public void AssignMetalStream( double metalstream ){} // assign continuous metal flow to this controller; negative for reverse flow
class VoiceCommandCountScouts implements VoiceCommandHandler {
@Override
public void commandReceived( String cmdline, String[]splitString, int player )
{
csai.SendTextMsg( "scouts: " + ScoutUnits.size() );
}
}
void ExploreWith( Unit unit )
{
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 = maps.getHeightMap().getElevationAt( destination ); // aicallback.getMap().getElevationAt( destination.x, destination.y );
logfile.WriteLine( "mapwidth: " + aicallback.getMap().getWidth() + " squaresize: " + maps.getMovementMaps().SQUARE_SIZE );
logfile.WriteLine( "ScoutController sending scout " + unit.getUnitId() + " to " + destination.toString() );
giveOrderWrapper.MoveTo(unit, destination );
}
// class EnemyTrackerHandler extends EnemyTracker.EnemyAdapter {
// @Override
// public void AcquiredEnemy( Unit unit )
// {
// }
// }
void Reappraise()
{
// logfile.WriteLine("reappraise>>>");
for( Unit scout : ScoutUnits ) {
TerrainPos scoutpos = unitController.getPos( scout );
TerrainPos nearestpos = null;
float bestsquareddistance = 100000000;
Unit targetenemy = null;
boolean existsPriorityTargets = false;
for( Unit enemy : enemyTracker.getEnemyUnits() ) {
UnitDef enemyunitdef = enemyTracker
.getEnemyUnitDefByUnit()
.get( enemy );
if( enemyunitdef != null )
{
if( IsPriorityTarget( enemyunitdef ) )
{
existsPriorityTargets = true;
}
}
}
// need to add index by position for this, to speed things up
for( Unit enemy : enemyTracker.getEnemyUnits() ) {
UnitDef enemyunitdef = enemyTracker
.getEnemyUnitDefByUnit()
.get( enemy );
if( enemyunitdef != null )
{
// if priority targets exist grab those, otherwise target anything
// if no threat exists
if( !existsPriorityTargets || IsPriorityTarget( enemyunitdef ) )
{
logfile.WriteLine("considering unit " + enemy.getUnitId() + " " + enemyunitdef.getName());
TerrainPos enemypos = TerrainPos.fromAIFloat3( enemy.getPos() );
float thissquareddistance = scoutpos.GetSquaredDistance( enemypos);
if( thissquareddistance < bestsquareddistance )
{
if( maps.getThreatMap().getThreatAt( enemypos ) < 1 ) {
nearestpos = enemypos;
bestsquareddistance = thissquareddistance;
targetenemy = enemy;
}
}
}
}
}
if( nearestpos != null )
{
searchcoordinator.Disactivate();
giveOrderWrapper.Attack(scout, targetenemy);
movefailed.remove( scout );
if( !attackingscouts.contains( scout ) || movefailed.contains( scout ) )
{
attackingscouts.add( scout );
}
}
else
{
if( attackingscouts.contains( scout ) || movefailed.contains( scout ) )
{
searchcoordinator.Activate();
// ExploreWith( scout );
attackingscouts.remove( scout );
movefailed.remove( scout );
}
}
}
// logfile.WriteLine("reappraise<<<");
}
// List<String> prioritytargets = Arrays.asList(new String[] {
// "armmex", "cormex", "armrad", "corrad" });
boolean IsPriorityTarget(UnitDef unitdef )
{
String name = unitdef.getName().toLowerCase();
return config.getScoutraiderprioritytargets().contains( name );
}
// boolean IsLaserTower(UnitDef unitdef )
// {
// if( unitdef.getName().toLowerCase() == "armllt"
// || unitdef.getName().toLowerCase() == "corllt"
// || unitdef.getName().toLowerCase() == "armfrt"
// || unitdef.getName().toLowerCase() == "corfrt" )
// {
// return true;
// }
// return false;
// }
ArrayList<Unit> attackingscouts = new ArrayList<Unit>(); // scoutid of scouts currently attacking
class GameListenerHandler extends GameAdapter {
// int ticks = 0;
@Override
public void Tick( int frame )
{
Reappraise();
}
@Override
public void UnitMoveFailed( Unit unit ) {
movefailed.add( unit );
}
}
}
|