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 package hughai.basictypes; 22 23 import com.springrts.ai.*; 24 import com.springrts.ai.oo.*; 25 import com.springrts.ai.oo.clb.*; 26 27 import hughai.unitdata.UnitController; 28 29 // wraps the Spring Command with an OOP version 30 31 // allows adding toString etc 32 33 public class OOPCommands { 34 // something that can be attacked, moved to etc 35 public static class Target 36 { Target()37 public Target() { } ToDoubleArray()38 public double[] ToDoubleArray() 39 { 40 return null; 41 } 42 } 43 44 public static class PositionTarget extends Target 45 { 46 public Float3 targetpos; PositionTarget(Float3 targetpos)47 public PositionTarget(Float3 targetpos) 48 { 49 this.targetpos = targetpos; 50 } 51 //@Override 52 //public double[] ToDoubleArray() 53 //{ 54 //return targetpos.ToDoubleArray(); 55 //} 56 @Override toString()57 public String toString() 58 { 59 return "PositionTarget: " + targetpos.toString(); 60 } 61 } 62 63 // targeting a specific unit 64 public static class UnitTarget extends Target 65 { 66 public Unit target; UnitTarget(Unit target)67 public UnitTarget(Unit target) 68 { 69 this.target = target; 70 } 71 // @Override 72 // public double[] ToDoubleArray() 73 //{ 74 //return new double[] { target }; 75 //} 76 } 77 78 public static class OOPCommand 79 { 80 public Unit UnitToReceiveOrder; OOPCommand()81 public OOPCommand() 82 { 83 } OOPCommand(Unit unittoreceiveorder)84 public OOPCommand(Unit unittoreceiveorder) 85 { 86 this.UnitToReceiveOrder = unittoreceiveorder; 87 } execute()88 public void execute() throws CallbackAIException 89 { 90 } 91 @Override toString()92 public String toString() 93 { 94 return this.getClass().getSimpleName() + " unit " + UnitToReceiveOrder.getUnitId() + " " + UnitToReceiveOrder.getDef().getHumanName(); 95 } 96 } 97 98 public static class BuildCommand extends OOPCommand 99 { 100 public UnitDef unitdeftobuild; 101 public Float3 pos = null; 102 public String unitdefhumanname = ""; 103 BuildCommand(Unit builder, UnitDef unitdeftobuild, String unitdefhumanname)104 public BuildCommand(Unit builder, UnitDef unitdeftobuild, String unitdefhumanname) 105 { 106 this.UnitToReceiveOrder = builder; 107 this.unitdeftobuild = unitdeftobuild; 108 this.unitdefhumanname = unitdefhumanname; 109 } 110 BuildCommand(Unit builder, UnitDef unitdeftobuild, Float3 pos, String unitdefhumanname )111 public BuildCommand(Unit builder, UnitDef unitdeftobuild, Float3 pos, String unitdefhumanname ) 112 { 113 this.UnitToReceiveOrder = builder; 114 this.unitdeftobuild = unitdeftobuild; 115 this.pos = pos; 116 this.unitdefhumanname = unitdefhumanname; 117 } 118 @Override execute()119 public void execute() throws CallbackAIException 120 { 121 if (pos == null) 122 { 123 UnitToReceiveOrder.build(unitdeftobuild, new AIFloat3(), 0, (short)0, Integer.MAX_VALUE ); 124 // - idtobuild, new double[] { }); 125 } 126 else 127 { 128 UnitToReceiveOrder.build(unitdeftobuild, pos.toAIFloat3(), 0, (short)0, Integer.MAX_VALUE ); 129 // return new Command( -idtobuild, pos.ToDoubleArray()); 130 } 131 } 132 @Override toString()133 public String toString() 134 { 135 if (pos != null) 136 { 137 return "BuildCommand " + UnitToReceiveOrder.getDef().getHumanName() + " " 138 + UnitToReceiveOrder.getUnitId() + " building " + unitdefhumanname + " at " + pos.toString(); 139 } 140 else 141 { 142 return "BuildCommand " + UnitToReceiveOrder.getDef().getHumanName() + " " 143 + UnitToReceiveOrder.getUnitId() + " building " + unitdefhumanname; 144 } 145 } 146 } 147 148 public static class AttackCommand extends OOPCommand 149 { 150 public Target target; AttackCommand(Unit attacker, Target target)151 public AttackCommand(Unit attacker, Target target) 152 { 153 this.UnitToReceiveOrder = attacker; 154 this.target = target; 155 } 156 @Override execute()157 public void execute() throws CallbackAIException 158 { 159 if( target.getClass() == UnitTarget.class){ 160 UnitToReceiveOrder.attack(((UnitTarget)target).target, (short)0, Integer.MAX_VALUE ); 161 } else if ( target.getClass() == PositionTarget .class){ 162 UnitToReceiveOrder.attackArea(((PositionTarget)target).targetpos.toAIFloat3(), 100, (short)0, Integer.MAX_VALUE ); 163 } 164 throw new RuntimeException("Invalid target"); 165 } 166 @Override toString()167 public String toString() 168 { 169 return "AttackCommand " + UnitToReceiveOrder + " attacking " + target.toString(); 170 } 171 } 172 173 public static class MoveToCommand extends OOPCommand 174 { 175 public Float3 targetpos; MoveToCommand(Unit unit, Float3 pos)176 public MoveToCommand(Unit unit, Float3 pos) 177 { 178 this.UnitToReceiveOrder = unit; 179 this.targetpos = pos; 180 } 181 @Override execute()182 public void execute() throws CallbackAIException 183 { 184 UnitToReceiveOrder.moveTo(targetpos.toAIFloat3(), (short)0, Integer.MAX_VALUE ); 185 } 186 @Override toString()187 public String toString() 188 { 189 UnitDef unitdef = UnitToReceiveOrder.getDef(); 190 if( unitdef != null ) { 191 return "MoveToCommand " + UnitToReceiveOrder.getUnitId() + " " + UnitToReceiveOrder.getDef().getHumanName() + " moving to " + targetpos.toString(); 192 } 193 else { 194 return "MoveToCommand. Unittoreceiveorder doesn't exist. id " + UnitToReceiveOrder.getUnitId(); 195 } 196 } 197 } 198 199 public static class GuardCommand extends OOPCommand 200 { 201 public Unit unittobeguarded; GuardCommand(Unit unit, Unit unittobeguarded)202 public GuardCommand(Unit unit, Unit unittobeguarded) 203 { 204 this.UnitToReceiveOrder = unit; 205 this.unittobeguarded = unittobeguarded; 206 } 207 @Override execute()208 public void execute() throws CallbackAIException 209 { 210 UnitToReceiveOrder.guard(unittobeguarded, (short)0, Integer.MAX_VALUE ); 211 //return new Command(Command.CMD_GUARD, new double[] { targetid } ); 212 } 213 } 214 215 public static class ReclaimCommand extends OOPCommand 216 { 217 public Float3 pos; 218 public double radius; ReclaimCommand(Unit unit, Float3 pos, double radius)219 public ReclaimCommand(Unit unit, Float3 pos, double radius) 220 { 221 this.UnitToReceiveOrder = unit; 222 this.pos = pos; 223 this.radius = radius; 224 } 225 @Override execute()226 public void execute() throws CallbackAIException 227 { 228 UnitToReceiveOrder.reclaimInArea(pos.toAIFloat3(), (float)radius, (short)0, Integer.MAX_VALUE ); 229 //Command(Command.CMD_RECLAIM, new double[] { pos.x, pos.y, pos.z, radius }); 230 } 231 } 232 233 public static class SelfDestructCommand extends OOPCommand 234 { SelfDestructCommand(Unit unit )235 public SelfDestructCommand(Unit unit ) 236 { 237 this.UnitToReceiveOrder = unit; 238 } 239 @Override execute()240 public void execute() throws CallbackAIException 241 { 242 UnitToReceiveOrder.selfDestruct((short)0, Integer.MAX_VALUE ); 243 // return new Command(Command.CMD_SELFD, new double[] {}); 244 } 245 } 246 247 public static class StopCommand extends OOPCommand 248 { StopCommand(Unit unit)249 public StopCommand(Unit unit) 250 { 251 this.UnitToReceiveOrder = unit; 252 } 253 @Override execute()254 public void execute() throws CallbackAIException 255 { 256 UnitToReceiveOrder.stop((short)0, Integer.MAX_VALUE ); 257 // return new Command(Command.CMD_STOP, new double[] { }); 258 } 259 } 260 } 261