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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
// 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.*;
import hughai.EnemyTracker.EnemyAdapter;
import hughai.basictypes.*;
import hughai.mapping.*;
import hughai.packcoordinators.*;
import hughai.unitdata.*;
import hughai.unitdata.UnitController.UnitAdapter;
import hughai.utils.*;
// this will ultimately be migrated to tactics directory as groundmelee or such
// for now it's a quick way of getting a battle going on!
public class TankController
{
public int MinTanksForAttack = 5;
public int MinTanksForSpreadSearch = 50;
// public HashMap< Integer,UnitDef> DefsById = new HashMap< Integer,UnitDef>();
HashSet<Unit> unitsToControl = new HashSet<Unit>();
UnitDef typicalunitdef;
//String[]UnitsWeLike = new String[]{ "armsam", "armstump", "armrock", "armjeth", "armkam", "armanac", "armsfig", "armmh", "armah",
// "armbull", "armmart", "armmav", "armyork" };
CSAI csai;
OOAICallback aicallback;
LogFile logfile;
PlayerObjects playerObjects;
UnitController unitcontroller;
EnemyTracker enemyTracker;
EnemySelector2 enemyselector;
BuildTable buildtable;
AttackPackCoordinator attackpackcoordinator;
GuardPackCoordinator guardpackcoordinator;
SpreadSearchPackCoordinatorWithSearchGrid spreadsearchpackcoordinator;
MoveToPackCoordinator movetopackcoordinator;
PackCoordinatorSelector packcoordinatorselector;
Config config;
// boolean forceattacking = false; // for debugging
TerrainPos LastAttackPos = null;
Random random = new Random();
public TankController( PlayerObjects playerObjects,
UnitDef typicalunitdef)
{
this.playerObjects = playerObjects;
config = playerObjects.getConfig();
csai = playerObjects.getCSAI();
aicallback = csai.aicallback;
logfile = playerObjects.getLogFile();
enemyTracker = playerObjects.getEnemyTracker();
unitcontroller = playerObjects.getUnitController();
// this.unitsToControl = unitsToControl;
this.typicalunitdef = typicalunitdef;
enemyselector = new EnemySelector2( playerObjects, typicalunitdef.getSpeed() * 2, typicalunitdef );
// speed here is experimental
attackpackcoordinator = new AttackPackCoordinator( playerObjects );
spreadsearchpackcoordinator = new SpreadSearchPackCoordinatorWithSearchGrid( playerObjects );
movetopackcoordinator = new MoveToPackCoordinator( playerObjects);
guardpackcoordinator = new GuardPackCoordinator( playerObjects);
packcoordinatorselector = new PackCoordinatorSelector( playerObjects );
packcoordinatorselector.LoadCoordinator( attackpackcoordinator );
packcoordinatorselector.LoadCoordinator( spreadsearchpackcoordinator );
packcoordinatorselector.LoadCoordinator( movetopackcoordinator );
packcoordinatorselector.LoadCoordinator( guardpackcoordinator );
logfile.WriteLine( "*TankController Initialized*" );
}
boolean Active = false;
public void Activate()
{
if( !Active )
{
enemyTracker.registerListener( new EnemyTrackerHandler() );
csai.registerGameListener( new GameListenerHandler() );
if (csai.DebugOn)
{
csai.RegisterVoiceCommand("tankscount", new VoiceCommandCountTanks() );
csai.RegisterVoiceCommand("tanksmoveto", new VoiceCommandMoveTo());
// csai.RegisterVoiceCommand("tanksattackpos", new VoiceCommandAttackPos());
// csai.RegisterVoiceCommand( "tankscancelattack", new VoiceCommandCancelAttack() );
csai.RegisterVoiceCommand("dumptanks", new VoiceCommandDumpTanksHandler());
}
//PackCoordinatorSelector.Activate();
Active = true;
}
}
class VoiceCommandDumpTanksHandler implements VoiceCommandHandler {
@Override
public void commandReceived(String cmd, String[] split, int player) {
logfile.WriteLine("Tankcontroller dump:");
for( Unit unit : unitsToControl ) {
logfile.WriteLine( unit.getUnitId() + " " + unit.getDef().getHumanName() );
}
}
}
public void Disactivate()
{
if( Active )
{
enemyTracker.unregisterGameListener( new EnemyTrackerHandler() );
csai.unregisterGameListener(new GameListenerHandler());
csai.UnregisterVoiceCommand( "tankscount" );
csai.UnregisterVoiceCommand( "tanksmoveto" );
csai.UnregisterVoiceCommand( "tanksattackpos" );
//unitcontroller.UnitAddedEvent -= new UnitController.UnitAddedHandler( UnitAdded );
//unitcontroller.UnitRemovedEvent -= new UnitController.UnitRemovedHandler( UnitRemoved );
packcoordinatorselector.DisactivateAll();
Active = false;
}
}
// planned, controller can control any units at its discretion, for now:
public void AssignUnits( Collection<Unit> units ){
for( Unit unit : units ) {
unitsToControl.add( unit );
//searchcoordinator.
logfile.WriteLine( "New tankcontroller unit: " +
unit.getUnitId() + " " + unit.getDef().getHumanName() );
attackpackcoordinator.AssignUnits( units );
spreadsearchpackcoordinator.AssignUnits( units );
movetopackcoordinator.AssignUnits( units );
guardpackcoordinator.AssignUnits( units );
//searchcoordinator.ExploreWith( unit );
}
} // give units to this controller
public void RevokeUnits( Collection<Unit> units ){
for( Unit unit : units ) {
unitsToControl.remove( unit );
attackpackcoordinator.RevokeUnits( units );
spreadsearchpackcoordinator.RevokeUnits( units );
movetopackcoordinator.RevokeUnits( units );
guardpackcoordinator.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 VoiceCommandCountTanks implements VoiceCommandHandler
{
@Override
public void commandReceived( String voiceString, String[] splitchatString, int player ) {
csai.SendTextMsg( "Number tanks: " + unitsToControl.size() );
logfile.WriteLine( "Number tanks: " + unitsToControl.size() );
}
}
class VoiceCommandMoveTo implements VoiceCommandHandler {
@Override
public void commandReceived( String voiceString, String[] splitchatString, int player )
{
// int targetteam = Convert.ToInt32( splitchatString[2] );
//if( targetteam == csai.Team )
// {
TerrainPos targetpos = new TerrainPos();
targetpos.x = Float.parseFloat( splitchatString[2] );
targetpos.z = Float.parseFloat( splitchatString[3] );
targetpos.y = playerObjects.getMaps().getHeightMap().getElevationAt( targetpos ); // aicallback.getMap().getElevationAt( targetpos.x, targetpos.z );
// GotoPos( targetpos );
movetopackcoordinator.SetTarget( targetpos );
packcoordinatorselector.ActivatePackCoordinator( movetopackcoordinator );
//}
}
}
// class VoiceCommandAttackPos implements VoiceCommandHandler {
// @Override
// public void commandReceived( String voiceString, String[] splitchatString, int player )
// {
// //int targetteam = Convert.ToInt32( splitchatString[2] );
// //if( targetteam == csai.Team )
// //{
// Float3 targetpos = new Float3();
// targetpos.x = Float.parseFloat( splitchatString[2] );
// targetpos.z = Float.parseFloat( splitchatString[3] );
// targetpos.y = aicallback.getMap().getElevationAt( (float)targetpos.x, (float)targetpos.z );
// LastAttackPos = targetpos;
//// forceattacking = true;
//
// attackpackcoordinator.SetTarget( targetpos );
// packcoordinatorselector.ActivatePackCoordinator( attackpackcoordinator );
// //}
// }
// }
// class VoiceCommandCancelAttack implements VoiceCommandHandler {
// @Override
// public void commandReceived( String voiceString, String[] splitchatString, int player )
// {
//// forceattacking = false;
// }
// }
//boolean WeLikeUnit( String name )
//{
// for( String unitwelike : UnitsWeLike )
//{
// if( unitwelike == name )
//{
// return true;
//}
//}
//return false;
//}
class GameListenerHandler extends GameAdapter {
// int tickcount = 0;
// int itick = 0;
@Override
public void Tick( int frame )
{
// tickcount++;
// itick++;
// if( itick >= 30 )
// {
DoSomething();
// itick = 0;
// }
}
@Override
// shifted this to unitidle to give unit time to leave factory
public void UnitIdle( Unit unit )
{
//if( !DefsById.Contains( id ) )
//{
// UnitDef unitdef = aicallback.getUnitDef( id );
//if( WeLikeUnit( unitdef.getName().toLowerCase() ) )
//{
// logfile.WriteLine( "TankController new tank " + " id " + id + " " + unitdef.getHumanName() + " speed " + unitdef.speed );
//TankDefsById.add( id, unitdef );
// DoSomething();
//}
//}
}
}
class UnitControllerHandler extends UnitController.UnitAdapter {
@Override
public void UnitAdded( Unit unit )
{
/*
// make this more elegant later, with concept of ownership etc
logfile.WriteLine("tankcontroller.UnitAdded " + unitdef.getHumanName());
if( WeLikeUnit( unitdef.getName().toLowerCase() ) )
{
logfile.WriteLine("unit in tanktype list");
if (!TankDefsById.Contains(id))
{
logfile.WriteLine("TankController new tank " + " id " + id + " " + unitdef.getHumanName() + " speed " + unitdef.speed);
TankDefsById.add( id, unitdef );
if (LastAttackPos == null)
{
LastAttackPos = aicallback.GetUnitPos(id);
}
enemyselector.InitStartPos(aicallback.GetUnitPos(id));
// aicallback.GiveOrder( id, new Command( Command.CMD_TRAJECTORY, new double[]{1}) );
// DoSomething();
}
}
*/
}
@Override
public void UnitRemoved( Unit unit )
{
/*
if( TankDefsById.Contains( id ) )
{
logfile.WriteLine( "TankController tank removed " + id );
TankDefsById.remove( id );
// DoSomething();
}
*/
}
}
class EnemyTrackerHandler extends EnemyTracker.EnemyAdapter {
@Override
public void AcquiredEnemy( Unit unit )
{
//DoSomething();
}
@Override
public void EnemyDestroyed( Unit unit )
{
//DoSomething();
}
}
// note to self: this function is a mess... spaghetti... unreadable...
void DoSomething()
{
String commanderunitname = playerObjects.getBuildTree().listToOurTeamsUnitName( config.getCommanderunitnames() );
if( unitsToControl.size() >= MinTanksForAttack ) // make sure at least have a few units before attacking
{
// ok, so first, we're going to figure out roughly where
// our units are, or where we were attacking before
// then we'll ask for some enemies near that
TerrainPos approximateattackpos = LastAttackPos;
if( approximateattackpos == null ) {
for( Unit unit : unitsToControl )
{
TerrainPos thispos = unitcontroller.getPos( unit );
if (thispos != null)
{
approximateattackpos = thispos;
}
}
//approximateattackpos = aicallback.GetUnitPos(DefsById.Keys.GetEnumerator().Current);
}
logfile.WriteLine("tankcontroller approximateattackpos: " + approximateattackpos);
// now we ask for an enemy:
TerrainPos nearestenemypos = enemyselector.ChooseAttackPoint( approximateattackpos );
if( nearestenemypos != null )
{
// we got an enemy, so attack...
logfile.WriteLine("tankcontroller found enemy, attacking: " + nearestenemypos);
attackpackcoordinator.SetTarget( nearestenemypos );
packcoordinatorselector.ActivatePackCoordinator( attackpackcoordinator );
LastAttackPos = nearestenemypos;
}
else
{
// otherwise search
if( unitsToControl.size() > MinTanksForSpreadSearch )
{
spreadsearchpackcoordinator.SetTarget( nearestenemypos );
packcoordinatorselector.ActivatePackCoordinator( spreadsearchpackcoordinator );
}
else // or if we're too weak, jsut guard the commander
{
if (unitcontroller.UnitsByName.containsKey(commanderunitname ))
{
guardpackcoordinator.SetTarget(unitcontroller.UnitsByName.get( commanderunitname ).get( 0 ) );
packcoordinatorselector.ActivatePackCoordinator(guardpackcoordinator);
}
}
}
}
else // else, not enough tanks to attack, just guard commander, or something
{
if (unitcontroller.UnitsByName.containsKey(commanderunitname))
{
List<Unit> commanders = unitcontroller.UnitsByName.get( commanderunitname );
if( commanders.size() > 0 ) {
guardpackcoordinator.SetTarget(commanders.get( 0 ) );
packcoordinatorselector.ActivatePackCoordinator(guardpackcoordinator);
}
}
}
}
}
|