/* ScannerSwarm.java (C) Copyright 2001 by Scott Sherman. All rights reserved. Scott Sherman 11-01... a controller which controls one pawn with a weapon & a bunch of other pawns with scanners */ import java.util.Enumeration; import com.loki3.cyber.pawn.*; import com.loki3.util.*; /** A controller of multiple pawns. * Assumes one primary pawn with a weapon & * a bunch of other pawns that just have scanners. * Assumes every pawn has a GPS */ public class ScannerSwarm implements GameListener, DamageListener { // GameListener public boolean use(Pawn pawn) { // ask to be notified of damage to the pawn pawn.addDamageListener(this); // if pawn has a weapon, remember it boolean isprimary = false; for (Enumeration e=pawn.getWeapons(); e!=null && e.hasMoreElements();) { Object weapon = e.nextElement(); if (weapon instanceof WeaponTurret) { isprimary = true; mWeaponTurret = (WeaponTurret)weapon; } } // find a GPS Object gps = null; for (Enumeration e=pawn.getTools(); e!=null && e.hasMoreElements();) { Object element = e.nextElement(); if (element instanceof ToolGPS) gps = element; } // remember primary and secondary pawns separately if (isprimary) { mPrimary = pawn; if (gps != null) mGPS = (ToolGPS)gps; mName = pawn.getIdentity().getFirstName(); } else { // if pawn has perfect scanner & GPS, add them to our lists Object scanner = null; for (Enumeration e=pawn.getScanners(); e!=null && e.hasMoreElements();) { Object element = e.nextElement(); if (element instanceof ScannerPerfect) scanner = element; } if ( (scanner != null) && (gps != null) ) { // pawn has both so add to both lists if (mScanners == null) { mScanners = new LinkedList(scanner); mGPSes = new LinkedList(gps); } else { mScanners = mScanners.addToHead(scanner); mGPSes = mGPSes.addToHead(gps); } } } return true; } public void gameTick(double tick) { if ( (mPrimary == null) || (mGPS == null) ) return; // don't have a main pawn - don't bother // find where the closest target is Vector closest = findClosest(); // if we found a close unit, attack it if (closest != null) { mTemp.set(closest); mTemp.subtract(mGPS.getPosition()); attack(mTemp.getLength(), mTemp.getAngle()); } } public void gameStarted() {} public void gameEnded() { mPrimary = null; } // DamageListener public void unitDamaged(Unit unit, double amount) {} public void unitDestroyed(Unit unit){ if (unit == mPrimary) mPrimary = null; } // find the location of the closest unit to our primary pawn protected Vector findClosest() { Vector primary = mGPS.getPosition(); if (mScannerEnum == null) { mScannerEnum = mScanners.elements(); mGPSEnum = mGPSes.elements(); } else { mScannerEnum.reset(mScanners); mGPSEnum.reset(mGPSes); } double closestdistance = -1; ScannedUnit closestunit = null; // run through all scanners to find closest target to primary pawn while (mScannerEnum.hasMoreElements()) { ScannerPerfect scanner = (ScannerPerfect)mScannerEnum.nextElement(); ToolGPS gps = (ToolGPS)mGPSEnum.nextElement(); Vector where = gps.getPosition(); Vector orientation = gps.getOrientation(); scanner.scan(); ScannedList list = scanner.getPawns(); // run through all units this scanner found for (int i=0; i Angle.degreesToRadians(350)) ) if ( (mWeaponTurret != null) && mWeaponTurret.canFire() ) mWeaponTurret.fire(); } // info about our primary pawn private Pawn mPrimary = null; private ToolGPS mGPS = null; private WeaponTurret mWeaponTurret = null; private String mName = null; // our primary identity // lists of scanner/gps pairs private LinkedList mScanners = null; private LinkedListEnumeration mScannerEnum = null; private LinkedList mGPSes = null; private LinkedListEnumeration mGPSEnum = null; // scratch private Vector mLocation = new Vector(), mTemp = new Vector(); }