File: WorkflowController.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 (654 lines) | stat: -rwxr-xr-x 25,264 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
// 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;

import java.util.*;
import java.util.Map;


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

import hughai.*;
import hughai.building.Workflows.Workflow;
import hughai.Ownership.IOrder;
import hughai.building.Workflows.Workflow.Order;
import hughai.building.*;
import hughai.basictypes.*;
import hughai.mapping.*;
import hughai.unitdata.*;
import hughai.utils.*;
import hughai.controllers.level1.*;
import hughai.controllers.level2.ScoutControllerRaider;
import hughai.controllers.level2.TankController;

// in theory this is a high level class that just matches
// other classes together
// for now, it's still a bit of a hack
// a lot of this needs to be factored out better
//
// The connection between this and Workflows is a bit hacky for now,
// not least the hashmap of unitsUnderContructionByOrder
// but it gets it working.  This bit is under transition at the moment, since
// just finished writing the Workflows class.
public class WorkflowController
{
   PlayerObjects playerObjects;
   CSAI csai;
   OOAICallback aicallback;
   ResourceManager resourceManager;
   LogFile logfile;
   UnitController unitController;
   //	MovementMaps movementMaps;
   //	BuildMap buildMap;
   BuildPlanner buildPlanner;
   Ownership ownership;
   BuildTree buildTree;
   BuildTable buildTable;
   MetalController metalController;
   EnergyController energyController;
   GiveOrderWrapper giveOrderWrapper;
   UnitDefHelp unitDefHelp;
   Metal metal;
   DrawingUtils drawingUtils;
   ReclaimHelper reclaimHelper;
   UnitLists unitLists;
   //LosMap losmap;
   Maps maps;
   EnemyTracker enemyTracker;
//   TankController tankcontroller;
//   TankController helicoptercontroller;
//   ScoutControllerRaider scoutcontroller;
   Config config;
   Offense offense;
   Reconnaissance reconnaissance;
   Workflows workflows;

   Random random = new Random();

   public double energymetalratio = 10.0;

   HashMap<Order,ArrayList<Ownership.IOrder>> unitsUnderContructionByOrder = new HashMap<Order, ArrayList<IOrder>>();

   List<Unit> ActiveConstructors = new ArrayList<Unit>();
   Map<Unit, Unit> AssistingConstructors = new HashMap<Unit, Unit>(); // id of assisted keyed by id of assistor

   Workflows.Workflow currentWorkflow;
   Collection<Workflows.Workflow.Order> currentOrders;
   
//   int lastcheckidleframe = -1000;

//   List<Order> orders = new ArrayList<Order>();

   public WorkflowController( PlayerObjects playerObjects )
   {
      this.playerObjects = playerObjects;
      csai = playerObjects.getCSAI();
      aicallback = csai.aicallback;
      resourceManager = playerObjects.getResourceManager();
      logfile = playerObjects.getLogFile();
      unitController = playerObjects.getUnitController();
      //		movementMaps = playerObjects.getMovementMaps();
      //		buildMap = playerObjects.getBuildMap();
      buildPlanner = playerObjects.getBuildPlanner();
      ownership = playerObjects.getOwnership();
      buildTree = playerObjects.getBuildTree();
      buildTable = playerObjects.getBuildTable();
      metalController = playerObjects.getMetalController();
      energyController = playerObjects.getEnergyController();
      giveOrderWrapper = playerObjects.getGiveOrderWrapper();
      unitDefHelp = playerObjects.getUnitDefHelp();
      metal = playerObjects.getMetal();
      drawingUtils = playerObjects.getDrawingUtils();
      reclaimHelper = playerObjects.getReclaimHelper();
      unitLists = playerObjects.getUnitLists();
      config = playerObjects.getConfig();
      //		losmap = playerObjects.getLosMap();
      maps = playerObjects.getMaps();
      enemyTracker = playerObjects.getEnemyTracker();
      workflows = playerObjects.getWorkflows();
      
      if (csai.DebugOn)
      {
         csai.RegisterVoiceCommand("dumpworkflow", new DumpWorkFlowHandler());
      }
   }

   public class DumpWorkFlowHandler implements VoiceCommandHandler {
      public void commandReceived(String cmd, String[] split, int playe)
      {
         logfile.WriteLine("Workflow:");
         for (Order order : currentOrders )
         {
            logfile.WriteLine(order.toString());
         }
      }
   }

   public void Activate()
   {
      metal.Init();

      // just pick some workflow for now...
      currentWorkflow = workflows.getWorkflowsByName().get(
            config.getDefaultWorkflowName() );
      if( currentWorkflow == null ) {
         for( Workflow workflow : workflows.getWorkflowsByName().values() ) {
            currentWorkflow = workflow;
         }
      }
      currentOrders = currentWorkflow.orders;
      
//      tankcontroller = new TankController(
//            playerObjects,
//            unitLists.getTankList().getUnits(), 
//            buildTable.getUnitDefByName("armstump" ) );
//      tankcontroller.Activate();
      //	helicoptercontroller = new TankController(HelicopterList.GetInstance().defbyid, BuildTable.GetInstance().UnitDefByName["arm_brawler"]);
      //helicoptercontroller.Activate();
//      scoutcontroller = new ScoutControllerRaider( playerObjects );
//      scoutcontroller.Activate();

      // note to self: should these really be in the workflow?
      reconnaissance = new Reconnaissance( playerObjects );
      reconnaissance.Activate();
      
      offense = new Offense( playerObjects );
      offense.Activate();

      maps.getLosMap();
      
      playerObjects.getBuildTree();

      unitController.LoadExistingUnits();
      enemyTracker.LoadExistingUnits();

      CheckIdleUnits();

      csai.registerGameListener( new GameListener() );
   }

   class GameListener extends GameAdapter {
      @Override
      public void Tick( int frame ) {
//         if ( frame - lastcheckidleframe > 30)
//         {
            CheckIdleUnits();
//            lastcheckidleframe = frame;
//         }
      }

      // note to self: this function is a f**king mess
      @Override
      public void UnitIdle(Unit constructor)
      {
         UnitDef constructorunitdef = unitController.getUnitDef( constructor );
//         logfile.WriteLine( "workflow.unitidle " + idleunit.getUnitId() 
//               + " " + idleunit.getDef().getHumanName() + " " +
//               " isbuilder: " + unitdef.isBuilder() + " "
//               + " unitdefhelp.isconstructor " + unitDefHelp.IsConstructor( unitdef ));
         if (!constructorunitdef.isBuilder() )
         {
            //logfile.WriteLine("cantbuild");
            return;
         }
         logfile.WriteLine("unitidleevent " + constructor.getUnitId()
               + " " + constructorunitdef.getName() 
               + " " + constructorunitdef.getHumanName());
         if (ActiveConstructors.contains(constructor))
         {
            ActiveConstructors.remove(constructor);
         }
         ownership.SignalConstructorIsIdle(constructor);
         double highestpriority = 0;
         List<Order> bestorders = new ArrayList<Order>();
         for (Order order : currentOrders)
         {
            double thispriority = order.priority;
            if (thispriority >= highestpriority)
            {
               if( unitsUnderContructionByOrder.get( order) == null ) {
                  unitsUnderContructionByOrder.put(  order, new ArrayList<IOrder>() );
               }
               List<IOrder> unitsunderconstruction = unitsUnderContructionByOrder.get( order);
               int currentunits = unitsunderconstruction.size();
               if (unitController.UnitsByName.containsKey(order.unitname))
               {
                  currentunits += unitController.UnitsByName.get(order.unitname).size();
               }
               if( currentunits < order.quantity)
               {
                  if (buildTree.CanBuild(constructorunitdef.getName().toLowerCase(), order.unitname))
                  {
                     //if( CanBuild(deployedunitid, 
                     if (thispriority > highestpriority)
                     {
                        highestpriority = thispriority;
                        bestorders = new ArrayList<Order>();
                        bestorders.add(order);
                       // csai.DebugSay("Possible order: " + order.toString());
                     }
                     else if (thispriority == highestpriority)
                     {
                        bestorders.add(order);
                      //  csai.DebugSay("Possible order: " + order.toString());
                     }
                  }
               }
            }
         }
         //if (bestorders.Count == 0)
         //  {
         //      csai.DebugSay("No orders found");
         //      return;
         //  }
         ArrayList<Order>possibleorders = new ArrayList<Order>(); // get orders this unit can build
         boolean metalneeded = false;
         boolean energyneeded = false;
         UnitDef deftobuild = null;
         for (Order order : bestorders)
         {
            csai.DebugSay("bestorder " + order.unitname);
            //if( BuildTree.GetInstance().CanBuild( unitdef.getName().ToLower(), order.unitname ) )
            //{
            deftobuild = buildTable.getUnitDefByName(order.unitname);
            if (metalController.CanBuild( constructorunitdef, deftobuild))
            {
               if (energyController.CanBuild( constructorunitdef, deftobuild))
               {
                  possibleorders.add(order);
                  csai.DebugSay("possible: " + order.unitname);
               }
               else
               {
                  csai.DebugSay("needs energy");
                  energyneeded = true;
               }
            }
            else
            {
               csai.DebugSay("needs metal");
               metalneeded = true;
            }
            //}
         }
         if( possibleorders.size() == 0 )
         {
            String commanderUnitName = buildTree.listToOurTeamsUnitName( config.getCommanderunitnames() );
            if ( unitLists.getLevel1ConstructorList().getUnits().size() < 1 &&
                  !unitController.UnitsByName.containsKey( commanderUnitName ) )
            {
               if (BuildConstructionVehicle(constructor, constructorunitdef))
               {
                  return;
               }
            }

            if (energyneeded || resourceManager.getCurrentEnergy() < resourceManager.getEnergyStorage() / 5 )
            {
               if (BuildSolarCell(constructor))
               {
                  logfile.WriteLine("building solarcell");
                  if (AssistingConstructors.containsKey(constructor))
                  {
                     AssistingConstructors.remove(constructor);
                  }
                  return;
               }
            }
            if (metalneeded || resourceManager.getCurrentMetal() < resourceManager.getMetalStorage() / 5 )
            {
               TerrainPos reclaimpos = reclaimHelper.GetNearestReclaim( constructor );
               if( reclaimpos != null )
               {
                  giveOrderWrapper.Reclaim( constructor, reclaimpos, 100 );
                  return;
               }
               if (BuildMex(constructor))
               {
                  logfile.WriteLine("building mex");
                  if (AssistingConstructors.containsKey(constructor))
                  {
                     AssistingConstructors.remove(constructor);
                  }
                  return;
               }
            }


            logfile.WriteLine("offering assistance");
            OfferAssistance(constructor);
            return;
         }
         Order ordertodo = possibleorders.get(random.nextInt(possibleorders.size()));
         String metalextractorname = buildTree.listToOurTeamsUnitName( config.getBasicmetalextractorunitnames() );
         if (ordertodo.unitname.equals(metalextractorname ) )
         {
            BuildMex(constructor);
            if (AssistingConstructors.containsKey(constructor))
            {
               AssistingConstructors.remove(constructor);
            }
         }
         else
         {
            //ordertodo.unitsunderconstruction += 1;
            deftobuild = buildTable.getUnitDefByName(ordertodo.unitname);
            TerrainPos pos = BuildUnit(constructor, ordertodo.unitname);
            Ownership.IOrder ownershiporder = ownership.RegisterBuildingOrder(
                  new BuildListener(),
                  constructor,
                  deftobuild, pos);
            logfile.WriteLine("building: " + ordertodo.unitname);
            if( unitsUnderContructionByOrder.get( ordertodo ) == null ) {
               unitsUnderContructionByOrder.put(  ordertodo, new ArrayList<IOrder>() );
            }
            unitsUnderContructionByOrder.get( ordertodo ).add(ownershiporder);
            if (AssistingConstructors.containsKey(constructor))
            {
               AssistingConstructors.remove(constructor);
            }
         }
      }
   }

   void CheckIdleUnits()
   {
     // logfile.WriteLine("Workflow.CheckIdleUnits()");
      for( Unit unit : unitController.units ) {
         // foreach (KeyValuePair<int, UnitDef> kvp in unitController.UnitDefByDeployedId)
         // logfile.WriteLine("Checking " + kvp.Key);
         UnitDef unitdef = unit.getDef();
         //logfile.WriteLine( " considering " + unit.getUnitId() + " " + unit.getDef().getHumanName() + " ..." );
         if (unitdef.isBuilder() )
         {
            //logfile.WriteLine( "... is builder ..." );
            if( unit.getCurrentCommands().size() == 0 )
            {
               //logfile.WriteLine( "... command queue is empty ..." );
               logfile.WriteLine("Unit " + unitdef.getHumanName() + " not busy, calling idle");
               if( AssistingConstructors.containsKey( unit ) )
               {
                  logfile.WriteLine("removing from assistingconstructors");
                  AssistingConstructors.remove(unit);
               }
               logfile.WriteLine( "... calling unitidle ..." );
               new GameListener().UnitIdle(unit);
            }
            if (!ActiveConstructors.contains(unit))
            {
               logfile.WriteLine("Unit " + unitdef.getHumanName() + " not in active commanders, calling idle");
               new GameListener().UnitIdle(unit);
            }
         }
      }
   }

//   public class Order
//   {
//      public double priority;
//      public String unitname;
//      public int quantity;
//      public ArrayList<Ownership.IOrder> unitsunderconstruction = new ArrayList<Ownership.IOrder>();
//      public Order(double priority, String unitname, int quantity)
//      {
//         this.priority = priority;
//         this.unitname = unitname;
//         this.quantity = quantity;
//      }
//      @Override
//      public String toString()
//      {
//         int underconstruction = unitsunderconstruction.size();
//         int currentunits = 0;
//         if (unitController.UnitsByName.containsKey(unitname))
//         {
//            currentunits += unitController.UnitsByName.get(unitname).size();
//         }
//         return "Order priority: " + priority + " unitname: " + unitname + " quantity: " + currentunits + "(" + 
//         underconstruction + ") / " + quantity;
//      }
//   }

   // note to self: need to add weighting probably
   // need to add filters/position requireemtns for radars, metal extractors
//   public void BuildUnit(double priority, String name, int quantity)
//   {
//      orders.add( new Order(priority, name, quantity));
//   }

   List<String> energyunits = new ArrayList<String>();

   public void AddEnergyUnit(String energyunitname)
   {
      energyunits.add(energyunitname);
   }

   List<String> metalunits = new ArrayList<String>();

   public void AddMetalUnit(String metalunitname)
   {
      metalunits.add(metalunitname);
   }

   // this causes all units to offer assistance, even static ones
   // which means units coming out of the factories will automatically assist,
   // which isn't always what we want, but anyway...
   void OfferAssistance(Unit constructor)
   {
     // if( constructor.)
      Unit bestconstructor = null;
      double bestsquareddistance = 1000000000;
      TerrainPos constructorpos = unitController.getPos( constructor );
      for (Unit activeconstructor : ActiveConstructors)
      {
         TerrainPos activeconstructorpos = unitController.getPos( activeconstructor );
         if (activeconstructorpos != null)
         {
            float thissquareddistance = constructorpos.GetSquaredDistance( activeconstructorpos);
            if (thissquareddistance < bestsquareddistance)
            {
               bestconstructor = activeconstructor;
               bestsquareddistance = thissquareddistance;
            }
         }
      }
      if (bestconstructor != null)
      {
         logfile.WriteLine("unit to assist should be " + bestconstructor);
         if( !AssistingConstructors.containsKey( constructor ) )
         {
            logfile.WriteLine("assisting " + bestconstructor);
            giveOrderWrapper.Guard(constructor, bestconstructor);
            AssistingConstructors.put(constructor, bestconstructor);
         }
         else if( AssistingConstructors.get( constructor ) != bestconstructor )
         {
            logfile.WriteLine("assisting " + bestconstructor);
            giveOrderWrapper.Guard(constructor, bestconstructor);
            AssistingConstructors.put( constructor, bestconstructor );
         }
      }
   }

   boolean BuildConstructionVehicle( Unit constructor, UnitDef constructordef)
   {
      UnitDef deftobuild = null;
      String basicConstructionVehicleName = buildTree.listToOurTeamsUnitName( config.getBasicconstructionvehicleunitnames() );
      if( basicConstructionVehicleName == null ) {
         return false;
      }
      if( buildTree.CanBuild( constructordef.getName().toLowerCase(), basicConstructionVehicleName ) )
      {
         deftobuild = buildTable.getUnitDefByName(basicConstructionVehicleName);
      }
      else if (buildTree.CanBuild(constructordef.getName().toLowerCase(), basicConstructionVehicleName))
      {
         deftobuild = buildTable.getUnitDefByName(basicConstructionVehicleName);
      }
      else
      {
         return false;
      }
      TerrainPos pos = BuildUnit(constructor, deftobuild.getName().toLowerCase());
      Ownership.IOrder ownershiporder = ownership.RegisterBuildingOrder(
            new BuildListener(),
            constructor,
            deftobuild, pos);
      logfile.WriteLine("building: " + deftobuild.getName().toLowerCase());
      //ordertodo.unitsunderconstruction.Add(ownershiporder);
      if (AssistingConstructors.containsKey(constructor))
      {
         AssistingConstructors.remove(constructor);
      }
      return true;
   }

   TerrainPos BuildUnit(Unit constructor, String targetunitname)
   {
      csai.DebugSay("workflow, building " + targetunitname);
      UnitDef targetunitdef = buildTable.getUnitDefByName(targetunitname);
      UnitDef constructordef = constructor.getDef();
      if (unitDefHelp.IsMobile(constructordef))
      {
         logfile.WriteLine("constructor is mobile");
         TerrainPos constructorpos = unitController.getPos( constructor );
         TerrainPos buildsite = buildPlanner.ClosestBuildSite(targetunitdef,
               constructorpos,
               3000, 2);
         buildsite = TerrainPos.fromAIFloat3( aicallback.getMap().findClosestBuildSite(
               targetunitdef, buildsite.toAIFloat3(), 1400, 0, 0) );
         logfile.WriteLine("constructor location: " + constructorpos + " Buildsite: " + buildsite + " target item: " + targetunitdef.getHumanName());
         if (!ActiveConstructors.contains(constructor))
         {
            ActiveConstructors.add(constructor);
         }
         drawingUtils.DrawUnit(targetunitname.toUpperCase(), buildsite, 0.0f, 1, aicallback.getSkirmishAI().getTeamId(), true, true);
         giveOrderWrapper.BuildUnit(constructor, targetunitname, buildsite);
         return buildsite;
      }
      else
      {
         TerrainPos factorypos = unitController.getPos( constructor );
         logfile.WriteLine("factory location: " + factorypos + " target item: " + targetunitdef.getHumanName());
         if (!ActiveConstructors.contains(constructor))
         {
            ActiveConstructors.add(constructor);
         }
         drawingUtils.DrawUnit(targetunitdef.getName().toUpperCase(), factorypos, 0.0f, 200, aicallback.getSkirmishAI().getTeamId(), true, true);
         giveOrderWrapper.BuildUnit(constructor, targetunitname );
         return factorypos;
      }
   }

   boolean BuildMex(Unit constructor)
   {
//      String metalextractorname = config.getBasicmetalextractorunitname();
      String metalextractorname = buildTree.listToOurTeamsUnitName( config.getBasicmetalextractorunitnames() );
      if( metalextractorname == null ) {
         return false;
      }
      if( !buildTree.CanBuild( constructor.getDef().getName().toLowerCase(),
            metalextractorname ) )
      {
         return false;
      }
      UnitDef unitdef = buildTable.getUnitDefByName(metalextractorname);
      TerrainPos buildsitefrommetal = metal.GetNearestMetalSpot(
            unitController.getPos( constructor ) );
      TerrainPos buildsite = TerrainPos.fromAIFloat3( aicallback.getMap().findClosestBuildSite(
            unitdef, buildsitefrommetal.toAIFloat3(), 100, 0, 0) );
      logfile.WriteLine( "Workflow.buildmex, metal says build at " + buildsitefrommetal + " map changes this to: " + buildsite );
      if (!ActiveConstructors.contains(constructor))
      {
         ActiveConstructors.add(constructor);
      }
      //aicallback.GiveOrder(constructorid, new Command(-unitdef.id, buildsite.ToDoubleArray()));
      giveOrderWrapper.BuildUnit(constructor, unitdef.getName(), buildsite);
      return true;
   }

   boolean BuildSolarCell(Unit constructor)
   {
      String energyextractorunitname = buildTree.listToOurTeamsUnitName( config.getBasicenergyextractorunitnames() );
      if( energyextractorunitname == null ) {
         return false;
      }
      if (!buildTree.CanBuild(constructor.getDef().getName().toLowerCase(),
            energyextractorunitname))
      {
         return false;
      }
      UnitDef unitdef = buildTable.getUnitDefByName(energyextractorunitname);
      TerrainPos buildsite = buildPlanner.ClosestBuildSite(unitdef,
            unitController.getPos( constructor ),
            3000, 2);
      buildsite = TerrainPos.fromAIFloat3( aicallback.getMap().findClosestBuildSite(
            unitdef, buildsite.toAIFloat3(), 100, 0, 0) );
      if (!ActiveConstructors.contains(constructor))
      {
         ActiveConstructors.add(constructor);
      }
      //aicallback.GiveOrder(constructorid, new Command(-unitdef.id, buildsite.ToDoubleArray()));
      giveOrderWrapper.BuildUnit(constructor, unitdef.getName(), buildsite );
      return true;
   }

   class BuildListener implements Ownership.IBuilder {
      @Override
      public void UnitFinished(Ownership.IOrder ownershiporder, Unit unit )
      {
         for (Order order : currentOrders)
         {
            if( unitsUnderContructionByOrder.get( order ) == null ) {
               unitsUnderContructionByOrder.put(  order, new ArrayList<IOrder>() );
            }
            if (unitsUnderContructionByOrder.get( order ).contains(ownershiporder))
            {
               unitsUnderContructionByOrder.get( order ).remove(ownershiporder);
            }
         }
         new GameListener().UnitIdle(unit);
      }
      
      @Override
      public void UnitCreated(Ownership.IOrder order,Unit unit )
      {
      }

      @Override
      public void UnitDestroyed(Ownership.IOrder ownershiporder, Unit unit )
      {
         for (Order order : currentOrders )
         {
            if( unitsUnderContructionByOrder.get( order ) == null ) {
               unitsUnderContructionByOrder.put(  order, new ArrayList<IOrder>() );
            }
            if (unitsUnderContructionByOrder.get( order ).contains(ownershiporder))
            {
               unitsUnderContructionByOrder.get( order ).remove(ownershiporder);
            }
         }
      }
   }
}