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 
24 package hughai.packcoordinators;
25 
26 import java.util.*;
27 import java.util.Map;
28 
29 import com.springrts.ai.*;
30 import com.springrts.ai.oo.clb.*;
31 
32 import hughai.GameAdapter;
33 import hughai.PlayerObjects;
34 import hughai.basictypes.*;
35 import hughai.*;
36 import hughai.mapping.*;
37 import hughai.packcoordinators.*;
38 import hughai.unitdata.*;
39 import hughai.utils.*;
40 
41 
42 
43 // this moves the units to a particular point
44 // the point is assumed to be friendly; no particular attack pattern is assumed
45 public class MoveToPackCoordinator extends PackCoordinator
46 {
47 	// can pass in pointer to a hashtable in another class if we want
48 	// ie other class can directly modify our hashtable
MoveToPackCoordinator( PlayerObjects playerObjects )49 	public MoveToPackCoordinator(
50 			PlayerObjects playerObjects )
51 	{
52 		super(playerObjects );
53 
54 		csai.registerGameListener( new GameListenerHandler() );
55 	}
56 
57 	TerrainPos targetpos;
58 
59 	// does NOT imply Activate()
SetTarget( TerrainPos newtarget )60 	public void SetTarget( TerrainPos newtarget )
61 	{
62 		this.targetpos = newtarget;
63 		//Activate();
64 	}
65 
66 	TerrainPos lasttargetpos = null;
67 
68 	@Override
Recoordinate()69 	void Recoordinate()
70 	{
71 		if( !activated )
72 		{
73 			return;
74 		}
75 
76 		if( restartedfrompause
77 				|| targetpos.GetSquaredDistance( lasttargetpos )
78 				> ( 20 * 20 ) )
79 		{
80 			for( Unit unit : unitsControlled )
81 			{
82 				//int deployedid = (int)de.Key;
83 				//UnitDef unitdef = de.Value as UnitDef;
84 				Move( unit );
85 			}
86 			lasttargetpos = targetpos;
87 		}
88 	}
89 
Move( Unit unit )90 	void Move( Unit unit )
91 	{
92 		giveOrderWrapper.MoveTo(unit, targetpos );
93 		//aicallback.GiveOrder( unitid, new Command( Command.CMD_MOVE, targetpos.ToDoubleArray() ) );
94 	}
95 
96 	class GameListenerHandler extends GameAdapter {
97 		@Override
UnitIdle( Unit unit )98 		public void UnitIdle( Unit unit )
99 		{
100 			if( activated )
101 			{
102 				if( unitsControlled.contains( unit ) )
103 				{
104 					Move( unit );
105 				}
106 			}
107 		}
108 
109 //		int ticks = 0;
110 		@Override
Tick( int frame )111 		public void Tick( int frame )
112 		{
113 //			ticks++;
114 //			if( ticks >= 30 )
115 //			{
116 				//Recoordinate();
117 
118 //				ticks = 0;
119 //			}
120 		}
121 	}
122 }
123 
124