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
|
// 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.unitdata;
import java.util.*;
import java.util.Map;
import com.springrts.ai.*;
import com.springrts.ai.oo.clb.*;
import hughai.*;
import hughai.utils.*;
import hughai.basictypes.*;
import hughai.basictypes.Float3.*;
import hughai.ui.*;
// The role of UnitController is to keep track of what units we have
// Really it's just a wrapper for unitfinished and unitdestroyed events from csai.
// Note to self: do we really need this class???
public class UnitController
{
public static interface UnitListener {
public void ExistingUnit(Unit unit);
public void UnitAdded(Unit unit );
public void UnitRemoved( Unit unit );
public void AllUnitsLoaded();
}
public static class UnitAdapter implements UnitListener {
@Override public void ExistingUnit(Unit unit){}
@Override public void UnitAdded(Unit unit ){}
@Override public void UnitRemoved( Unit unit ){}
@Override public void AllUnitsLoaded(){}
}
HashSet<UnitListener> unitListeners = new HashSet<UnitListener>();
public void registerListener( UnitListener listener ){
unitListeners.add(listener);
}
CSAI csai;
OOAICallback aicallback;
LogFile logfile;
UnitDefHelp unitdefhelp;
GiveOrderWrapper giveOrderWrapper;
DrawingUtils drawingUtils;
//public Hashtable UnitDefByDeployedId = new Hashtable();
//public HashMap<Integer,UnitDef> UnitDefByDeployedId = new HashMap<Integer,UnitDef>();
//public HashMap<Integer,Unit> UnitByUnitId = new HashMap<Integer,Unit>();
public HashMap<String, List<Unit>> UnitsByName = new HashMap<String, List<Unit>>();
public List<Unit> units = new ArrayList<Unit>();
HashMap<Unit,UnitDef> unitdefbyunit = new HashMap<Unit, UnitDef>(); // because unit.getDef() is f**king slow.
// wipe these each frame:
HashMap<Unit,TerrainPos> posbyunit = new HashMap<Unit,TerrainPos>();
List<Map<?,?>> mapstowipeeachframe = Arrays.asList( new Map<?,?>[]{
posbyunit } );
public UnitDef getUnitDef( Unit unit ) {
return unitdefbyunit.get( unit );
}
// retrieves cached pos if exists, or gets it, and caches it.
public TerrainPos getPos( Unit unit ) {
// we assume that mostly the pos will already exist, so check this first
if( posbyunit.containsKey( unit ) ) {
return posbyunit.get( unit );
}
// store it in variable, so we don't have to do a fetch from the collection after the put
TerrainPos thispos = TerrainPos.fromAIFloat3( unit.getPos() );
if( thispos.equals( new TerrainPos() ) ) { // if the pos is zero, just make it null, it makes life simpler...
thispos = null;
}
posbyunit.put( unit, thispos );
return thispos;
}
public UnitController( PlayerObjects playerObjects )
{
csai = playerObjects.getCSAI();
aicallback = csai.aicallback;
logfile = playerObjects.getLogFile();
unitdefhelp = new UnitDefHelp( playerObjects );
giveOrderWrapper = playerObjects.getGiveOrderWrapper();
drawingUtils = playerObjects.getDrawingUtils();
csai.registerGameListener(new GameListenerHandler());
// csai.RegisterVoiceCommand( "killallfriendly", new VoiceCommandKillAllFriendlyHandler() );
csai.RegisterVoiceCommand( "countunits", new VoiceCommandCountUnits() );
// csai.RegisterVoiceCommand( "labelunits", new VoiceLabelUnits() );
playerObjects.getMainUI().registerButton( "Label units", new ButtonLabelUnits() );
playerObjects.getMainUI().registerButton( "Kill all friendly", new ButtonKillAllFriendly() );
logfile.WriteLine ("*UnitController initialized*");
}
class ButtonLabelUnits implements MainUI.ButtonHandler {
public void go() {
labelUnits();
}
}
class ButtonKillAllFriendly implements MainUI.ButtonHandler {
public void go() {
killAllFriendly();
}
}
public class VoiceCommandCountUnits implements VoiceCommandHandler {
@Override public void commandReceived( String cmd, String[]splitcmd, int player )
{
csai.SendTextMsg( "friendly unit count: " + units.size() );
}
}
public class VoiceLabelUnits implements VoiceCommandHandler {
@Override public void commandReceived( String cmd, String[]splitcmd, int player )
{
labelUnits();
}
}
public void labelUnits() {
for( Unit unit : units ) {
drawingUtils.drawText( getPos( unit ), "" + unit.getUnitId() );
}
}
class GameListenerHandler extends GameAdapter {
@Override
public void Tick( int frame ) {
// csai.sendTextMessage("unitcontroller.tick");
//units = aicallback.getTeamUnits();
for( Map<?,?> map : mapstowipeeachframe ) {
map.clear();
}
}
@Override
public void UnitFinished( Unit newunit )
{
logfile.WriteLine( "UnitController.NewUnitFinished " + newunit.getDef().getHumanName() + " " + newunit.getUnitId() );
AddUnit( newunit );
}
@Override
public void UnitDestroyed( Unit destroyedunit, Unit enemy )
{
logfile.WriteLine( "UnitController.UnitDestroyed " + destroyedunit.getUnitId() );
RemoveUnit( destroyedunit );
}
}
public void LoadExistingUnits()
{
List<Unit> friendlyunits = aicallback.getFriendlyUnits();
for( Unit friendlyunit : friendlyunits )
{
logfile.WriteLine("friendly unit existing: " + friendlyunit.getUnitId() + " " + friendlyunit.getDef().getHumanName() + " " + friendlyunit.getDef().getName() + " " + friendlyunit.getDef().getHumanName() );
AddUnit( friendlyunit );
for( UnitListener unitListener : unitListeners ) {
unitListener.ExistingUnit( friendlyunit );
}
}
for( UnitListener unitListener : unitListeners ) {
unitListener.AllUnitsLoaded();
}
}
public void RefreshMyMemory( UnitListener listener )
{
units = aicallback.getTeamUnits();
//for( int deployedid : UnitByUnitId.keySet() )
for( Unit unit : units )
{
// Unit unit = UnitByUnitId.get(deployedid);
listener.UnitAdded( unit );
}
}
public void AddUnit( Unit friendlyunit )
{
UnitDef unitdef = friendlyunit.getDef();
if( !units.contains( friendlyunit ) )
{
logfile.WriteLine("UnitController.AddUnit: unit id " + friendlyunit.getDef().getName() + " " + unitdef.getHumanName() );
String name = unitdef.getName().toLowerCase();
//UnitByUnitId.put( friendlyunit.getUnitId(), friendlyunit );
if( !UnitsByName.containsKey( name ) )
{
UnitsByName.put( name, new ArrayList<Unit>() );
}
UnitsByName.get(name).add(friendlyunit);
units.add(friendlyunit);
unitdefbyunit.put( friendlyunit, friendlyunit.getDef() );
for( UnitListener unitListener : unitListeners ) {
unitListener.UnitAdded( friendlyunit );
}
logfile.WriteLine("UnitController.AddUnit finished");
}
else
{
logfile.WriteLine( "UnitController.AddUnit: unit id " + friendlyunit.getUnitId() + " " + unitdef.getHumanName() + " already exists" );
}
}
void RemoveUnit( Unit friendlyunit )
{
if( units.contains( friendlyunit ) )
{
UnitDef unitdef = friendlyunit.getDef();
String name = unitdef.getName().toLowerCase();
//UnitByUnitId.remove(friendlyunit);
UnitsByName.get(name).remove(friendlyunit);
units.remove(friendlyunit);
unitdefbyunit.remove( friendlyunit );
}
for( UnitListener unitListener : unitListeners ) {
unitListener.UnitRemoved( friendlyunit );
}
}
public void killAllFriendly(){
for( Unit unit : units )
{
if( !unit.getDef().isCommander() )
{
giveOrderWrapper.SelfDestruct(unit);
}
}
}
// kills all except commander, allows testing expansion from nothing, without having to relaod spring.exe
public class VoiceCommandKillAllFriendlyHandler implements VoiceCommandHandler {
@Override
public void commandReceived( String cmd, String[]splitcmd, int player )
{
killAllFriendly();
}
}
}
|