File: Metal.java

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; 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 (671 lines) | stat: -rwxr-xr-x 27,829 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
// 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.w3c.dom.*;
import java.io.*;

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

import hughai.basictypes.*;
import hughai.*;
import hughai.mapping.BuildMap.BuildMapPos;
import hughai.ui.MainUI;
import hughai.unitdata.*;
import hughai.utils.*;


// handles figuring out where the best metal spots are,
// and handing them out on demand
// listens for new extractors being built, and ticks those areas off
// from the list
public class Metal
{
   public final String MetalClassVersion = "0.1"; // used for detecting cache validity; if cache was built with older version we rebuild it

   public int MinMetalForSpot = 30; // from 0-255, the minimum percentage of metal a spot needs to have
   //from the maximum to be saved. Prevents crappier spots in between taken spaces.
   //They are still perfectly valid and will generate metal mind you!
   public int MaxSpots = 5000; //If more spots than that are found the map is considered a metalmap, tweak this as needed        
   //int MaxSpots = 3;
   
   public static final int granularity = 2;
   
   public static class MetalPos extends Int2 {
      public MetalPos() {
         
      }
      public MetalPos( Int2 int2 ) {
         x = int2.x;
         y = int2.y;
      }
      public MetalPos( int x, int y ) {
         super( x, y );
      }
      public TerrainPos toTerrainPos() {
         return new TerrainPos( x * 8 * granularity, 0, y * 8 * granularity );
      }
      public static MetalPos fromTerrainPos( TerrainPos terrainPos ) {
         return new MetalPos( (int)terrainPos.x / 8 / granularity,
               (int)terrainPos.z / 8 / granularity );
      }
   }

   public MetalSpot[] MetalSpots;
   public ArrayList<MetalSpot> MetalSpotsUsed = new ArrayList<MetalSpot>(); // arraylist of MetalSpots
   public boolean isMetalMap = false;

   public HashMap<Unit,Float3> Extractors = new HashMap<Unit,Float3>();

   double ExtractorRadius;

   CSAI csai;
   OOAICallback aicallback;
   LogFile logfile;
   UnitController unitcontroller;
   UnitDefHelp unitdefhelp;
   ResourceManager resourceManager;
   //	BuildMap buildMap;
   TimeHelper timeHelper;
   //	MovementMaps movementMaps;
   Maps maps;
   DrawingUtils drawingUtils;
   PlayerObjects playerObjects;

   public Metal( PlayerObjects playerObjects ) // protected constructor to force Singleton instantiation
   {
      this.playerObjects = playerObjects;
      csai = playerObjects.getCSAI();
      aicallback = csai.aicallback;
      logfile = playerObjects.getLogFile();
      unitcontroller = playerObjects.getUnitController();
      unitdefhelp = playerObjects.getUnitDefHelp();
      resourceManager = playerObjects.getResourceManager();
      //		buildMap = playerObjects.getBuildMap();
      timeHelper = playerObjects.getTimeHelper();
      //		movementMaps = playerObjects.getMovementMaps();
      maps = playerObjects.getMaps();
      drawingUtils = playerObjects.getDrawingUtils();

      ExtractorRadius = aicallback.getMap().getExtractorRadius(resourceManager.getMetalResource());

      unitcontroller.registerListener( new UnitListener());

//      csai.RegisterVoiceCommand( "showmetalspots", new DrawMetalSpotsHandler() );
      csai.RegisterVoiceCommand( "searchmetalspots", new VoiceSearchMetal() );
      
      playerObjects.getMainUI().registerButton( "Show metal spots", new ShowMetalSpotsButton() );
   }
   
   class ShowMetalSpotsButton implements MainUI.ButtonHandler {
      @Override
      public void go() {
         DrawMetalSpots();
      }
   }

   @Override
   protected void finalize() {
      System.out.println( this.getClass().getSimpleName() + ".finalize()");
   }
//   static Object lock = new Object();

   public void Init()
   {
//      synchronized( lock ) {
         if( !LoadCache() )
         {
            csai.SendTextMsg( "Metal analyzer rebuilding cachefile" );
            SearchMetalSpots();
         }
         if( !isMetalMap )
         {
            ReserveMetalExtractorSpaces();
         }
//      }
   }
   
   class VoiceSearchMetal implements VoiceCommandHandler {
      @Override
      public void commandReceived(  String chatString, String[] splitchatString, int player ) {
         csai.sendTextMessage( "searching metal spots..." );
         SearchMetalSpots();
      }
   }

   void ReserveMetalExtractorSpaces()
   {
      for( MetalSpot metalspot : MetalSpots )
      {
         //			logfile.WriteLine("reserving space for " + metalspot.Pos.toString() );
         BuildMapPos buildMapPos = BuildMapPos.fromTerrainPos( metalspot.Pos );
         maps.getBuildMap().ReserveSpace( this, buildMapPos, 6, 6 );
      }
   }

   class UnitListener extends UnitController.UnitAdapter {
      @Override
      public void UnitAdded( Unit newunit )
      {
        // logfile.WriteLine( "Metal.UnitAdded " + newunit.getDef().getHumanName() + " " + newunit.getUnitId() );
         if( !isMetalMap )
         {
            boolean ismex = unitdefhelp.IsMex( newunit.getDef() ); 
            //logfile.WriteLine( "Is in extractors? " + Extractors.containsKey( newunit ) 
             //  + " ismex? " + ismex );
            if( !Extractors.containsKey( newunit ) 
                  && ismex )
            {
               TerrainPos mexpos = unitcontroller.getPos( newunit );
               logfile.WriteLine( "Metal.UnitAdded new extractor finished, pos " + mexpos.toString() );
               Extractors.put( newunit, mexpos );
               double squareextractorradius = ExtractorRadius * ExtractorRadius;
               for( MetalSpot metalspot : MetalSpots )
               {
                  float thisdistancesquared = metalspot.Pos.GetSquaredDistance( mexpos );
                  //   logfile.WriteLine( "squareextractorradius: " + squareextractorradius + " thisdistancesquared: " + thisdistancesquared );
                  if( thisdistancesquared <= squareextractorradius )
                  {
                     MetalSpotsUsed.add( metalspot );
                     logfile.WriteLine( "Marking metal spot used: " + metalspot.Pos.toString() );
                  }
               }
            }
         }
      }

      @Override
      public void UnitRemoved( Unit removedunit )
      {
         if( !isMetalMap )
         {
            if( Extractors.containsKey( removedunit ) )
            {
               TerrainPos mexpos = unitcontroller.getPos( removedunit );
               logfile.WriteLine( "Metal.UnitRemoved, pos " + mexpos.toString() );
               double squareextractorradius = ExtractorRadius * ExtractorRadius;
               for( MetalSpot metalspot : MetalSpots )
               {
                  if( metalspot.Pos.GetSquaredDistance( mexpos ) < squareextractorradius )
                  {
                     if( MetalSpotsUsed.contains( metalspot ) )
                     {
                        logfile.WriteLine( "Marking metal spot free: " + metalspot.Pos.toString() );
                        MetalSpotsUsed.remove( metalspot );
                     }
                  }
               }
               Extractors.remove( removedunit );
            }
         }
      }
   }

   public TerrainPos GetNearestMetalSpot( TerrainPos mypos )
   {
      if( !isMetalMap )
      {
         double closestdistancesquared = 1000000000000d;
         TerrainPos bestpos = null;
         MetalSpot bestspot = null;
         for( MetalSpot metalspot : MetalSpots )
         {
            if( !MetalSpotsUsed.contains( metalspot ) )
            {
               if( bestpos == null )
               {
                  bestpos = metalspot.Pos;
               }
               float thisdistancesquared = mypos.GetSquaredDistance( metalspot.Pos );
               //logfile.WriteLine( "thisdistancesquared = " + thisdistancesquared + " closestdistancesquared= " + closestdistancesquared );
               if( thisdistancesquared < closestdistancesquared )
               {
                  closestdistancesquared = thisdistancesquared;
                  bestpos = metalspot.Pos;
                  bestspot = metalspot;
               }
            }
         }
         return bestspot.Pos;
      }
      else
      {
         return mypos; // if metal map just return passed-in pos
      }
   }

   public class DrawMetalSpotsHandler implements VoiceCommandHandler {
      @Override
      public void commandReceived( String chatString, String[] splitchatString, int player )
      {
         DrawMetalSpots();
      }
   }

   public class MetalSpot
   {
      public int Amount;
      public TerrainPos Pos = new TerrainPos();
      public boolean IsOccupied = false;

      public MetalSpot(){}
      public MetalSpot( int amount, TerrainPos pos )
      {
         Amount = amount;
         Pos = pos;
      }
      public MetalSpot( int amount, TerrainPos pos, boolean isoccupied )
      {
         Amount = amount;
         IsOccupied = isoccupied;
         Pos = pos;
      }
      @Override
      public String toString()
      {
         return "MetalSpot( Pos=" + Pos.toString() + ", Amount=" + Amount + ", IsOccupied=" + IsOccupied;
      }
   }

   // for debugging / convincing oneself spots are in right place
   void DrawMetalSpots()
   {
      if( !isMetalMap )
      {
         String  metalspotunitname = playerObjects.getConfig().getMetalspotmarkerunitname();
         String metalspotusedunit = playerObjects.getConfig().getUsedmetalspotmarkerunitname();
         for( MetalSpot metalspot : MetalSpots )
         {
            logfile.WriteLine("drawing spot at " + metalspot.Pos );
            drawingUtils.DrawUnit(metalspotunitname, metalspot.Pos, 0.0f, 1, aicallback.getSkirmishAI().getTeamId(), true, true);
            drawingUtils.drawText(metalspot.Pos, "" + metalspot.Pos );
         }
         for( MetalSpot metalspot : MetalSpotsUsed )
         {
            logfile.WriteLine("drawing usedspot at " + metalspot.Pos );
            drawingUtils.DrawUnit(metalspotusedunit, metalspot.Pos, 0.0f, 1, aicallback.getSkirmishAI().getTeamId(), true, true);
            drawingUtils.drawText(metalspot.Pos, "" + metalspot.Pos );
         }
      }
      else
      {
         csai.SendTextMsg( "Metal analyzer reports this is a metal map" );
      }
   }

   // loads cache file
   // returns true if cache loaded ok, otherwise false if not found, out-of-date, etc
   // we check the version and return false if out-of-date
   boolean LoadCache()
   {
      String MapName = aicallback.getMap().getName();
      String cachefilepath = csai.getCacheDirectoryPath() + File.separator + MapName + "_metal.xml";

      if( !new File( cachefilepath ).exists() )
      {
         logfile.WriteLine( "cache file doesnt exist -> building" );
         return false;
      }

      Document cachedom = XmlHelper.OpenDom( cachefilepath );
      Element metadata = XmlHelper.SelectSingleElement( cachedom.getDocumentElement(), "/root/metadata" );
      String cachemetalclassversion = metadata.getAttribute( "version" );
      if( !cachemetalclassversion.equals( MetalClassVersion ) )
      {
         logfile.WriteLine( "cache file out of date ( " + cachemetalclassversion + " vs " + MetalClassVersion + " ) -> rebuilding" );
         return false;
      }

      logfile.WriteLine( cachedom.getTextContent() );

      isMetalMap = Boolean.parseBoolean( metadata.getAttribute( "ismetalmap" ) );

      if( isMetalMap )
      {
         logfile.WriteLine( "metal map" );
         return true;
      }


      Element metalspots = XmlHelper.SelectSingleElement( cachedom.getDocumentElement(), "/root/metalspots" );
      ArrayList<MetalSpot> metalspotsal = new ArrayList<MetalSpot>();
      for( Element metalspot : XmlHelper.SelectElements( metalspots, "metalspot" ) ) {
         int amount = Integer.parseInt( metalspot.getAttribute("amount") );
         TerrainPos pos = new TerrainPos();
         pos.readFromXmlElement( metalspot );
         //pos.LoadCsv( metalspot.GetAttribute("pos") );
         MetalSpot newmetalspot = new MetalSpot( amount, pos );
         metalspotsal.add( newmetalspot );
         // logfile.WriteLine( "metalspot xml: " + metalspot.InnerXml );
         //         logfile.WriteLine( "metalspot: " + newmetalspot.toString() );
      }
      MetalSpots = (MetalSpot[])metalspotsal.toArray( new MetalSpot[0] );

      logfile.WriteLine( "metal spot cache file loaded" );
      return true;
   }

   // save cache to speed up load times
   // we store the MetalClassVersion to enable cache rebuild if algo changed
   void SaveCache()
   {
      Map map = aicallback.getMap();
      String MapName = map.getName();
      String cachefilepath = csai.getCacheDirectoryPath() + File.separator +  MapName + "_metal.xml";

      Document cachedom = XmlHelper.CreateDom();
      Element metadata = XmlHelper.AddChild( cachedom.getDocumentElement(), "metadata" );
      metadata.setAttribute( "type", "MetalCache" );
      metadata.setAttribute( "map", MapName );
      metadata.setAttribute( "version", MetalClassVersion );
      metadata.setAttribute( "datecreated", timeHelper.GetCurrentRealTimeString() );
      metadata.setAttribute( "ismetalmap", "" + isMetalMap );
      metadata.setAttribute( "extractorradius", "" + map.getExtractorRadius(resourceManager.getMetalResource()) );
      metadata.setAttribute( "mapheight", "" + map.getHeight() );
      metadata.setAttribute( "mapwidth", "" + map.getWidth() );

      Element metalspots = XmlHelper.AddChild( cachedom.getDocumentElement(), "metalspots" );
      for( MetalSpot metalspot : MetalSpots )
      {
         Element metalspotnode = XmlHelper.AddChild( metalspots, "metalspot" );
         //metalspotnode.setAttribute( "pos", metalspot.Pos.ToCsv() );
         metalspot.Pos.writeToXmlElement( metalspotnode );
         metalspotnode.setAttribute( "amount", "" + metalspot.Amount );
      }

      XmlHelper.SaveDom( cachedom, cachefilepath );
   }

   public static class AvailableMetalResult {
      public int [][] availableMetal;
      public int maxMetalAmount;
      public AvailableMetalResult( int[][] availableMetal, int maxMetalAmount ) {
         this.availableMetal = availableMetal;
         this.maxMetalAmount = maxMetalAmount;
      }
   }

   // algorithm more or less by krogothe
   // ported from Submarine's original C++ version
   AvailableMetalResult CalculateAvailableMetalForEachSpot( int[][] metalremaining, int ExtractorRadius )
   {
      int mapwidth = metalremaining.length;
      int mapheight = metalremaining[0].length;
      int SquareExtractorRadius = (int)( ExtractorRadius * ExtractorRadius );
      int [][]SpotAvailableMetal = new int[ mapwidth][ mapheight ];

      logfile.WriteLine( "mapwidth: " + mapwidth + " mapheight: " + mapheight + " ExtractorRadius: " + ExtractorRadius + " SquareExtractorRadius: " + SquareExtractorRadius );

      // Now work out how much metal each spot can make by adding up the metal from nearby spots
      // we scan each point on map, and for each point, we scan from + to - ExtractorRadius, eliminating anything where
      // the actual straight line distance is more than the ExtractorRadius
      int maxmetalspotamount = 0;
      for (int spoty = 0; spoty < mapheight; spoty++)
      {
         for (int spotx = 0; spotx < mapwidth; spotx++)
         {
            int metalthisspot = 0;
            //get the metal from all pixels around the extractor radius 
            for (int deltax = - ExtractorRadius; deltax <= ExtractorRadius; deltax++)
            {
               int thisx = spotx + deltax;
               if ( thisx >= 0 && thisx < mapwidth )
               {
                  for (int deltay = - ExtractorRadius; deltay <= ExtractorRadius; deltay++)
                  {
                     int thisy = spoty + deltay;
                     if ( thisy >= 0 && thisy < mapheight )
                     {
                        if( ( deltax * deltax + deltay * deltay ) <= SquareExtractorRadius )
                        {
                           metalthisspot += metalremaining[ thisx][ thisy ]; 
                        }
                     }
                  }
               }
            }
            SpotAvailableMetal[ spotx][ spoty ] = metalthisspot; //set that spots metal making ability (divide by cells to values are small)
            maxmetalspotamount = Math.max( metalthisspot, maxmetalspotamount ); //find the spot with the highest metal to set as the map's max
         }
      }

      //  logfile.WriteLine ("*******************************************");
      return new AvailableMetalResult( SpotAvailableMetal, maxmetalspotamount );
   }

   // algorithm more or less by krogothe
   // ported from Submarine's original C++ version
   public void SearchMetalSpots()
   {	
      logfile.WriteLine( "SearchMetalSpots() >>>");

      isMetalMap = false;

      ArrayList<MetalSpot> metalspotsal = new ArrayList<MetalSpot>();

      Map map = aicallback.getMap();
      int mapheight = map.getHeight() / granularity; //metal map has 1/2 resolution of normal map
      int mapwidth = map.getWidth() / granularity;
      double mapmaxmetal = map.getMaxResource(resourceManager.getMetalResource());
      int totalcells = mapheight * mapwidth;

      logfile.WriteLine( "mapwidth: " + mapwidth + " mapheight " + mapheight + " maxmetal:" + mapmaxmetal );

      List<Short> metalmap = map.getResourceMapRaw(resourceManager.getMetalResource()); // original metal map
      int[][] metalremaining = new int[ mapwidth][ mapheight ];  // actual metal available at that point. we remove metal from this as we add spots to MetalSpots
      int[][] SpotAvailableMetal = new int [ mapwidth][ mapheight ]; // amount of metal an extractor on this spot could make
      int[][] NormalizedSpotAvailableMetal = new int [ mapwidth][ mapheight ]; // SpotAvailableMetal, normalized to 0-255 range

      int totalmetal = 0;
      ArrayIndexer arrayindexer = new ArrayIndexer( mapwidth, mapheight );
      //Load up the metal Values in each pixel
      logfile.WriteLine( "width: " + mapwidth + " height: " + mapheight );
      for (int y = 0; y < mapheight; y++)
      {
         //String logline = "";
         for( int x = 0; x < mapwidth; x++ )
         {
            metalremaining[ x][ y ] = (int)metalmap.get( arrayindexer.GetIndex( x, y ) );
            totalmetal += metalremaining[ x][ y ];		// Count the total metal so you can work out an average of the whole map
            //logline += metalremaining[ x, y ].toString() + " ";
            //  logline += metalremaining[ x, y ] + " ";
         }
         // logfile.WriteLine( logline );
      }
      logfile.WriteLine ("*******************************************");

      double averagemetal = ((double)totalmetal) / ((double)totalcells);  //do the average
      // int maxmetal = 0;

      int ExtractorRadius = (int)( map.getExtractorRadius(resourceManager.getMetalResource())
            / 16.0 );
      int DoubleExtractorRadius = ExtractorRadius * 2;
      int SquareExtractorRadius = ExtractorRadius * ExtractorRadius; //used to speed up loops so no recalculation needed
      int FourSquareExtractorRadius = 4 * SquareExtractorRadius; // same as above 
      double CellsInRadius = Math.PI * ExtractorRadius * ExtractorRadius;

      int maxmetalspotamount = 0;
      logfile.WriteLine( "Calculating available metal for each spot..." );
      AvailableMetalResult availableMetalResult = CalculateAvailableMetalForEachSpot( metalremaining, ExtractorRadius );
      SpotAvailableMetal = availableMetalResult.availableMetal;
      maxmetalspotamount = availableMetalResult.maxMetalAmount;

      //		logfile.WriteLine( "Normalizing..." );
      // normalize the metal so any map will have values 0-255, no matter how much metal it has
      int[][] NormalizedMetalRemaining = new int[ mapwidth][ mapheight ];
      for (int y = 0; y < mapheight; y++)
      {
         for (int x = 0; x < mapwidth; x++)
         {
            NormalizedSpotAvailableMetal[ x][ y ] = ( SpotAvailableMetal[ x][ y ] * 255 ) / maxmetalspotamount;
         }
      }

      //		logfile.WriteLine( "maxmetalspotamount: " + maxmetalspotamount );

      boolean Stopme = false;
      int SpotsFound = 0;
      //logfile.WriteLine( BuildTable.GetInstance().GetBiggestMexUnit().toString() );
      // UnitDef biggestmex = BuildTable.GetInstance().GetBiggestMexUnit();
      // logfile.WriteLine( "biggestmex is " + biggestmex.name + " " + biggestmex.humanName );
      for (int spotindex = 0; spotindex < MaxSpots && !Stopme; spotindex++)
      {	                
         //			logfile.WriteLine( "spotindex: " + spotindex );
         int bestspotx = 0, bestspoty = 0;
         int actualmetalatbestspot = 0; // use to try to put extractors over spot itself
         //finds the best spot on the map and gets its coords
         int BestNormalizedAvailableSpotAmount = 0;
         for (int y = 0; y < mapheight; y++)
         {
            for (int x = 0; x < mapwidth; x++)
            {
               if( NormalizedSpotAvailableMetal[ x ][ y ] > BestNormalizedAvailableSpotAmount ||
                     ( NormalizedSpotAvailableMetal[ x ][ y ] == BestNormalizedAvailableSpotAmount && 
                           metalremaining[ x ][ y ] > actualmetalatbestspot ) )
               {
                  BestNormalizedAvailableSpotAmount = NormalizedSpotAvailableMetal[ x ][ y ];
                  bestspotx = x;
                  bestspoty = y;
                  actualmetalatbestspot = metalremaining[ x ][ y ];
               }
            }
         }		
         //			logfile.WriteLine( "BestNormalizesdAvailableSpotAmount: " + BestNormalizedAvailableSpotAmount );                
         if( BestNormalizedAvailableSpotAmount < MinMetalForSpot )
         {
            Stopme = true; // if the spots get too crappy it will stop running the loops to speed it all up
            //				logfile.WriteLine( "Remaining spots too small; stopping search" );
         }

         if( !Stopme )
         {
            MetalPos metalPos = new MetalPos( bestspotx, bestspoty );
            TerrainPos terrainpos = metalPos.toTerrainPos();
            terrainpos.y = maps.getHeightMap().getElevationAt( terrainpos );

            //pos = Map.PosToFinalBuildPos( pos, biggestmex );

            //				logfile.WriteLine( "Metal spot: " + pos + " " + BestNormalizedAvailableSpotAmount );
            MetalSpot thismetalspot = new MetalSpot( (int)( ( BestNormalizedAvailableSpotAmount  * mapmaxmetal * maxmetalspotamount ) / 255 ), terrainpos );

            //    if (aicallback.CanBuildAt(biggestmex, pos) )
            //  {
            // pos = Map.PosToBuildMapPos( pos, biggestmex );
            // logfile.WriteLine( "Metal spot: " + pos + " " + BestNormalizedAvailableSpotAmount );

            //     if(pos.z >= 2 && pos.x >= 2 && pos.x < mapwidth -2 && pos.z < mapheight -2)
            //      {
            //  if(CanBuildAt(pos.x, pos.z, biggestmex.xsize, biggestmex.ysize))
            // {
            metalspotsal.add( thismetalspot );			
            SpotsFound++;

            //if(pos.y >= 0)
            //{
            // SetBuildMap(pos.x-2, pos.z-2, biggestmex.xsize+4, biggestmex.ysize+4, 1);
            //}
            //else
            //{
            //SetBuildMap(pos.x-2, pos.z-2, biggestmex.xsize+4, biggestmex.ysize+4, 5);
            //}
            //  }
            //   }
            //   }

            for (int myx = bestspotx - (int)ExtractorRadius; myx < bestspotx + (int)ExtractorRadius; myx++)
            {
               if (myx >= 0 && myx < mapwidth )
               {
                  for (int myy = bestspoty - (int)ExtractorRadius; myy < bestspoty + (int)ExtractorRadius; myy++)
                  {
                     if ( myy >= 0 && myy < mapheight &&
                           ( ( bestspotx - myx ) * ( bestspotx - myx ) + ( bestspoty - myy ) * ( bestspoty - myy ) ) <= (int)SquareExtractorRadius )
                     {
                        metalremaining[ myx ][ myy ] = 0; //wipes the metal around the spot so its not counted twice
                        NormalizedSpotAvailableMetal[ myx ][ myy ] = 0;
                     }
                  }
               }
            }

            // Redo the whole averaging process around the picked spot so other spots can be found around it
            for (int y = bestspoty - (int)DoubleExtractorRadius; y < bestspoty + (int)DoubleExtractorRadius; y++)
            {
               if(y >=0 && y < mapheight)
               {
                  for (int x = bestspotx - (int)DoubleExtractorRadius; x < bestspotx + (int)DoubleExtractorRadius; x++)
                  {
                     //funcion below is optimized so it will only update spots between r and 2r, greatly speeding it up
                     if((bestspotx - x)*(bestspotx - x) + (bestspoty - y)*(bestspoty - y) <= (int)FourSquareExtractorRadius && 
                           x >=0 && x < mapwidth && 
                           NormalizedSpotAvailableMetal[ x ][ y ] > 0 )
                     {
                        totalmetal = 0;
                        for (int myx = x - (int)ExtractorRadius; myx < x + (int)ExtractorRadius; myx++)
                        {
                           if (myx >= 0 && myx < mapwidth )
                           {
                              for (int myy = y - (int)ExtractorRadius; myy < y + (int)ExtractorRadius; myy++)
                              { 
                                 if (myy >= 0 && myy < mapheight && 
                                       ((x - myx)*(x - myx) + (y - myy)*(y - myy)) <= (int)SquareExtractorRadius )
                                 {
                                    totalmetal += metalremaining[ myx ][ myy ]; //recalculate nearby spots to account for deleted metal from chosen spot
                                 }
                              }
                           }
                        }
                        NormalizedSpotAvailableMetal[ x ][ y ] = totalmetal * 255 / maxmetalspotamount; //set that spots metal amount 
                     }
                  }
               }
            }
         }
      }

      if(SpotsFound > 500)
      {
         isMetalMap = true;
         metalspotsal.clear();
         logfile.WriteLine( "Map is considered to be a metal map" );
      }
      else
      {
         isMetalMap = false;

         // debug
         //for(list<AAIMetalSpot>::iterator spot = metal_spots.begin(); spot != metal_spots.end(); spot++)
      }

      MetalSpots = ( MetalSpot[] )metalspotsal.toArray( new MetalSpot[0] );

      SaveCache();
      logfile.WriteLine( "SearchMetalSpots() <<<");
   }
}