File: ReclaimHelper.java

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (136 lines) | stat: -rwxr-xr-x 5,192 bytes parent folder | download | duplicates (7)
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
// 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 java.util.*;

import com.springrts.ai.*;
import com.springrts.ai.oo.clb.*;

import hughai.CSAI;
import hughai.PlayerObjects;
import hughai.ResourceManager;
import hughai.basictypes.*;
import hughai.*;
import hughai.unitdata.UnitController;
import hughai.unitdata.UnitDefHelp;
import hughai.utils.*;


public class ReclaimHelper
{
   final int maxreclaimradius = 500;
   final int reclaimradiusperonehundredmetal = 150;

   PlayerObjects playerObjects;
   LogFile logfile;
   OOAICallback aicallback;
   UnitController unitController;
   UnitDefHelp unitDefHelp;
   ResourceManager resourceManager;
   DrawingUtils drawingUtils;
   //	MovementMaps movementMaps;
   CSAI csai;
   Maps maps;

   public ReclaimHelper( PlayerObjects playerObjects )
   {
      this.playerObjects = playerObjects;
      logfile = playerObjects.getLogFile();
      aicallback = playerObjects.getAicallback();
      unitController = playerObjects.getUnitController();
      unitDefHelp = playerObjects.getUnitDefHelp();
      resourceManager = playerObjects.getResourceManager();
      drawingUtils = playerObjects.getDrawingUtils();
      //		movementMaps = playerObjects.getMovementMaps();
      csai = playerObjects.getCSAI();
      maps = playerObjects.getMaps();
   }

   public TerrainPos GetNearestReclaim(Unit constructor)
   {
      TerrainPos mypos = unitController.getPos( constructor );
      if( playerObjects.getFrameController().getFrame() == 0 )// check ticks first, beacuse metal shows as zero at start
      {
         return null;
      }
      UnitDef unitdef = constructor.getDef();
      if (!unitDefHelp.IsMobile(unitdef))
      {
         return null;
      }
      //Float3 mypos = aicallback.GetUnitPos( constructorid );
      int currentarea = maps.getMovementMaps().GetArea( unitdef, mypos );
      //double nearestreclaimdistancesquared = 1000000;
      //Float3 nearestreclaimpos = null;
      float bestmetaldistanceratio = 0;
      Feature bestreclaim = null;
      int metalspace = (int)( resourceManager.getMetalStorage() 
            - resourceManager.getCurrentMetal() );
      logfile.WriteLine( "available space in metal storage: " + metalspace );
      List<Feature> nearbyfeatures = aicallback.getFeaturesIn(
            mypos.toAIFloat3(), maxreclaimradius );
      boolean reclaimfound = false;
      for( Feature feature : nearbyfeatures )
      {
         FeatureDef featuredef = feature.getDef();
         float containedMetal = featuredef.getContainedResource(resourceManager.getMetalResource()); 
         if( containedMetal > 0
               && containedMetal <= metalspace  )
         {
            TerrainPos thisfeaturepos = TerrainPos.fromAIFloat3( feature.getPosition() );
            float thisdistance = (float) Math.sqrt( 
                  thisfeaturepos.GetSquaredDistance( mypos ) );
            float thismetaldistanceratio = containedMetal / thisdistance;
            if( thismetaldistanceratio > bestmetaldistanceratio 
                  && maps.getMovementMaps().GetArea( unitdef, thisfeaturepos ) == currentarea )
            {
               logfile.WriteLine( "Potential reclaim, distance = " + thisdistance + " metal = " + containedMetal + " ratio = " + thismetaldistanceratio );
               bestmetaldistanceratio = thismetaldistanceratio;
               bestreclaim = feature;
               //         nearestreclaimpo
               reclaimfound = true;
            }
         }
      }
      if( reclaimfound 
            && ( bestmetaldistanceratio > ( 1.0 / ( 100 * reclaimradiusperonehundredmetal ) ) ) )
      {
         TerrainPos reclaimpos = TerrainPos.fromAIFloat3( bestreclaim.getPosition() );
         logfile.WriteLine( "Reclaim found, pos " + reclaimpos.toString() );
         if( csai.DebugOn )
         {
            drawingUtils.DrawUnit( "ARMMEX", reclaimpos, 0.0f, 200, aicallback.getSkirmishAI().getTeamId(), true, true);
         }
         return reclaimpos;
         //aicallback.GiveOrder( constructorid, new Command( Command.CMD_RECLAIM, 
         //    new double[]{ reclaimpos.x, reclaimpos.y, reclaimpos.z, 10 } ) );
      }
      else
      {
         //logfile.WriteLine( "No reclaim within parameters" );
         return null;
      }
   }
}