PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src-ai/hughai/controllers/level2/ScoutControllerRaider.java

https://github.com/hoijui/HughAI
Java | 279 lines | 186 code | 37 blank | 56 comment | 22 complexity | d1091c0bab73e5cc23890b5aa12bd780 MD5 | raw file
  1. // Copyright Hugh Perkins 2006, 2009
  2. // hughperkins@gmail.com http://manageddreams.com
  3. //
  4. // This program is free software; you can redistribute it and/or modify it
  5. // under the terms of the GNU General Public License as published by the
  6. // Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. // more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program in the file licence.txt; if not, write to the
  16. // Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  17. // 1307 USA
  18. // You can find the licence also on the web at:
  19. // http://www.opensource.org/licenses/gpl-license.php
  20. //
  21. // ======================================================================================
  22. //
  23. package hughai.controllers.level2;
  24. import java.util.*;
  25. import java.util.Map;
  26. import com.springrts.ai.*;
  27. import com.springrts.ai.oo.clb.*;
  28. import hughai.CSAI;
  29. import hughai.EnemyTracker;
  30. import hughai.GameAdapter;
  31. import hughai.GiveOrderWrapper;
  32. import hughai.PlayerObjects;
  33. import hughai.VoiceCommandHandler;
  34. import hughai.EnemyTracker.EnemyAdapter;
  35. import hughai.basictypes.*;
  36. import hughai.mapping.*;
  37. import hughai.packcoordinators.*;
  38. import hughai.unitdata.*;
  39. import hughai.utils.*;
  40. public class ScoutControllerRaider
  41. {
  42. public int nearbyforenemiesmeans = 250;
  43. public int enemysightedsearchradius = 1000;
  44. CSAI csai;
  45. OOAICallback aicallback;
  46. LogFile logfile;
  47. EnemyTracker enemyTracker;
  48. GiveOrderWrapper giveOrderWrapper;
  49. UnitController unitController;
  50. Maps maps;
  51. Config config;
  52. // SpreadSearchPackCoordinatorWithSearchGrid searchcoordinator;
  53. SpreadSearchPackCoordinatorWithSearchGrid searchcoordinator;
  54. List< Unit > ScoutUnits = new ArrayList< Unit>();
  55. HashSet< Unit > movefailed = new HashSet<Unit>();
  56. Random random;
  57. public ScoutControllerRaider( PlayerObjects playerObjects )
  58. {
  59. csai = playerObjects.getCSAI();
  60. aicallback = csai.aicallback;
  61. logfile = playerObjects.getLogFile();
  62. //unitcontroller = playerObjects.getUnitController();
  63. enemyTracker = playerObjects.getEnemyTracker();
  64. giveOrderWrapper = playerObjects.getGiveOrderWrapper();
  65. maps = playerObjects.getMaps();
  66. unitController = playerObjects.getUnitController();
  67. config = playerObjects.getConfig();
  68. random = new Random();
  69. // searchcoordinator = new SpreadSearchPackCoordinatorWithSearchGrid(
  70. // playerObjects);
  71. searchcoordinator = new SpreadSearchPackCoordinatorWithSearchGrid(
  72. playerObjects);
  73. logfile.WriteLine( "*ScoutControllerRaider initialized*" );
  74. }
  75. boolean Active = false;
  76. public void Activate()
  77. {
  78. if( !Active )
  79. {
  80. logfile.WriteLine( "activating " + this.getClass().getSimpleName() );
  81. csai.registerGameListener(new GameListenerHandler());
  82. csai.RegisterVoiceCommand( "countscouts", new VoiceCommandCountScouts() );
  83. // enemyTracker.registerListener( new EnemyTrackerHandler() );
  84. searchcoordinator.Activate();
  85. Active = true;
  86. }
  87. }
  88. public void Disactivate()
  89. {
  90. if( Active )
  91. {
  92. csai.unregisterGameListener(new GameListenerHandler());
  93. csai.UnregisterVoiceCommand( "countscouts" );
  94. // enemyTracker.unregisterGameListener( new EnemyTrackerHandler() );
  95. searchcoordinator.Disactivate();
  96. Active = false;
  97. }
  98. }
  99. // planned, controller can control any units at its discretion, for now:
  100. public void AssignUnits( Collection<Unit> units ){
  101. for( Unit unit : units ) {
  102. ScoutUnits.add( unit );
  103. logfile.WriteLine( "New scoutcontrollerraider unit: " +
  104. unit.getUnitId() + " " + unit.getDef().getHumanName() );
  105. searchcoordinator.AssignUnits( units );
  106. }
  107. } // give units to this controller
  108. public void RevokeUnits( Collection<Unit> units ){
  109. for( Unit unit : units ) {
  110. ScoutUnits.remove( unit );
  111. searchcoordinator.RevokeUnits( units );
  112. }
  113. } // remove these units from this controller
  114. // planned, not used yet, controller can use energy and metal at its discretion for now:
  115. public void AssignEnergy( int energy ){} // give energy to controller; negative to revoke
  116. public void AssignMetal( int metal ){} // give metal to this controller; negative to revoke
  117. public void AssignPower( double power ){} // assign continuous power flow to this controller; negative for reverse flow
  118. public void AssignMetalStream( double metalstream ){} // assign continuous metal flow to this controller; negative for reverse flow
  119. class VoiceCommandCountScouts implements VoiceCommandHandler {
  120. @Override
  121. public void commandReceived( String cmdline, String[]splitString, int player )
  122. {
  123. csai.SendTextMsg( "scouts: " + ScoutUnits.size() );
  124. }
  125. }
  126. void ExploreWith( Unit unit )
  127. {
  128. TerrainPos destination = new TerrainPos();
  129. destination.x = random.nextFloat() * aicallback.getMap().getWidth() * maps.getMovementMaps().SQUARE_SIZE;
  130. destination.z = random.nextFloat() * aicallback.getMap().getHeight() * maps.getMovementMaps().SQUARE_SIZE;
  131. destination.y = maps.getHeightMap().getElevationAt( destination ); // aicallback.getMap().getElevationAt( destination.x, destination.y );
  132. logfile.WriteLine( "mapwidth: " + aicallback.getMap().getWidth() + " squaresize: " + maps.getMovementMaps().SQUARE_SIZE );
  133. logfile.WriteLine( "ScoutController sending scout " + unit.getUnitId() + " to " + destination.toString() );
  134. giveOrderWrapper.MoveTo(unit, destination );
  135. }
  136. // class EnemyTrackerHandler extends EnemyTracker.EnemyAdapter {
  137. // @Override
  138. // public void AcquiredEnemy( Unit unit )
  139. // {
  140. // }
  141. // }
  142. void Reappraise()
  143. {
  144. // logfile.WriteLine("reappraise>>>");
  145. for( Unit scout : ScoutUnits ) {
  146. TerrainPos scoutpos = unitController.getPos( scout );
  147. TerrainPos nearestpos = null;
  148. float bestsquareddistance = 100000000;
  149. Unit targetenemy = null;
  150. boolean existsPriorityTargets = false;
  151. for( Unit enemy : enemyTracker.getEnemyUnits() ) {
  152. UnitDef enemyunitdef = enemyTracker
  153. .getEnemyUnitDefByUnit()
  154. .get( enemy );
  155. if( enemyunitdef != null )
  156. {
  157. if( IsPriorityTarget( enemyunitdef ) )
  158. {
  159. existsPriorityTargets = true;
  160. }
  161. }
  162. }
  163. // need to add index by position for this, to speed things up
  164. for( Unit enemy : enemyTracker.getEnemyUnits() ) {
  165. UnitDef enemyunitdef = enemyTracker
  166. .getEnemyUnitDefByUnit()
  167. .get( enemy );
  168. if( enemyunitdef != null )
  169. {
  170. // if priority targets exist grab those, otherwise target anything
  171. // if no threat exists
  172. if( !existsPriorityTargets || IsPriorityTarget( enemyunitdef ) )
  173. {
  174. logfile.WriteLine("considering unit " + enemy.getUnitId() + " " + enemyunitdef.getName());
  175. TerrainPos enemypos = TerrainPos.fromAIFloat3( enemy.getPos() );
  176. float thissquareddistance = scoutpos.GetSquaredDistance( enemypos);
  177. if( thissquareddistance < bestsquareddistance )
  178. {
  179. if( maps.getThreatMap().getThreatAt( enemypos ) < 1 ) {
  180. nearestpos = enemypos;
  181. bestsquareddistance = thissquareddistance;
  182. targetenemy = enemy;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. if( nearestpos != null )
  189. {
  190. searchcoordinator.Disactivate();
  191. giveOrderWrapper.Attack(scout, targetenemy);
  192. movefailed.remove( scout );
  193. if( !attackingscouts.contains( scout ) || movefailed.contains( scout ) )
  194. {
  195. attackingscouts.add( scout );
  196. }
  197. }
  198. else
  199. {
  200. if( attackingscouts.contains( scout ) || movefailed.contains( scout ) )
  201. {
  202. searchcoordinator.Activate();
  203. // ExploreWith( scout );
  204. attackingscouts.remove( scout );
  205. movefailed.remove( scout );
  206. }
  207. }
  208. }
  209. // logfile.WriteLine("reappraise<<<");
  210. }
  211. // List<String> prioritytargets = Arrays.asList(new String[] {
  212. // "armmex", "cormex", "armrad", "corrad" });
  213. boolean IsPriorityTarget(UnitDef unitdef )
  214. {
  215. String name = unitdef.getName().toLowerCase();
  216. return config.getScoutraiderprioritytargets().contains( name );
  217. }
  218. // boolean IsLaserTower(UnitDef unitdef )
  219. // {
  220. // if( unitdef.getName().toLowerCase() == "armllt"
  221. // || unitdef.getName().toLowerCase() == "corllt"
  222. // || unitdef.getName().toLowerCase() == "armfrt"
  223. // || unitdef.getName().toLowerCase() == "corfrt" )
  224. // {
  225. // return true;
  226. // }
  227. // return false;
  228. // }
  229. ArrayList<Unit> attackingscouts = new ArrayList<Unit>(); // scoutid of scouts currently attacking
  230. class GameListenerHandler extends GameAdapter {
  231. // int ticks = 0;
  232. @Override
  233. public void Tick( int frame )
  234. {
  235. Reappraise();
  236. }
  237. @Override
  238. public void UnitMoveFailed( Unit unit ) {
  239. movefailed.add( unit );
  240. }
  241. }
  242. }