home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 07 DecisionMaking Architectures / 01 Isla, Blumberg / City.java < prev    next >
Encoding:
Java Source  |  2001-09-25  |  9.4 KB  |  274 lines

  1. package bb;
  2.  
  3. import java.awt.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import java.awt.geom.*;
  7.  
  8. /*
  9. The city agent is the factory for the army. It has lots of skills - most of them
  10. when executed don't ACTUALLY perform the requested operation (e.g. a city can't
  11. ATTACK_CITY directly) but rather build a unit that can.
  12.  
  13. The city also issues some other basic missions. For example, its "defendMeMission"
  14. requests a shield to protect the city at all times. Its "defendMeSoldierMission" 
  15. requests direct defense from soldier units whenever the city is under attack. 
  16. Finally its "workerMission" enlists Worker units to gather resources for it so that
  17. it can build things.
  18.  
  19. @author naimad
  20. */
  21.  
  22. public class City extends Agent {
  23.  
  24.     /*
  25.     The city's skills. Most of these are self-explanatory.
  26.     */
  27.  
  28.     class CreateWorkerSkill extends Skill {
  29.         public final static String CREATE_WORKER = "create shield";
  30.  
  31.         public CreateWorkerSkill(String name, Agent agent, double prof) {
  32.             super(name, agent, prof);
  33.         }
  34.         
  35.         public CreateWorkerSkill(Agent agent, double prof) {
  36.             this(CREATE_WORKER, agent, prof);
  37.         }
  38.         
  39.         int count =0;
  40.         public void apply(double time, Mission mission) {
  41.             //System.out.println("Creating a shield for mission " + mission);
  42.             count++;
  43.             if (((City)agent).requestResources(Worker.COST)) {
  44.                 Vec2 position = new Vec2();
  45.                 getPosition(agent, position);
  46.                 System.out.println("City " + agent + " creating worker at location " + position);
  47.                 Worker worker = new Worker(getTeam(), position, bb, world);
  48.                 world.add(worker);
  49.                 if (mission!=null) {
  50.                     bb.removeFromMission(agent, mission);
  51.                     currentMission = null;
  52.                 }
  53.             }
  54.         }
  55.     }
  56.  
  57.  
  58.     class CreateShieldSkill extends Skill {
  59.         public final static String CREATE_SHIELD = "create shield";
  60.  
  61.         public CreateShieldSkill(String name, Agent agent, double prof) {
  62.             super(name, agent, prof);
  63.         }
  64.         
  65.         public CreateShieldSkill(Agent agent, double prof) {
  66.             this(CREATE_SHIELD, agent, prof);
  67.         }
  68.         
  69.         int count =0;
  70.         public void apply(double time, Mission mission) {
  71.             //System.out.println("Creating a shield for mission " + mission);
  72.             count++;
  73.             if (((City)agent).requestResources(Shield.COST)) {
  74.                 Vec2 position = new Vec2();
  75.                 getPosition(agent, position);
  76.                 System.out.println("City " + agent + " creating shield at location " + position);
  77.                 Shield shield = new Shield(getTeam(), position, bb, world);
  78.                 world.add(shield);
  79.                 if (mission!=null) {
  80.                     bb.removeFromMission(agent, mission);
  81.                     currentMission = null;
  82.                 }
  83.             }
  84.         }
  85.     }
  86.  
  87.  
  88.     class CreateSoldierSkill extends Skill {
  89.  
  90.         public final static String CREATE_SOLDIER = "create soldier";
  91.  
  92.         public CreateSoldierSkill(String name, Agent agent, double prof) {
  93.             super(name, agent, prof);
  94.         }
  95.         
  96.         public CreateSoldierSkill(Agent agent, double prof) {
  97.             this(CREATE_SOLDIER, agent, prof);
  98.         }
  99.  
  100.         public void apply(double time, Mission mission) {
  101.             //System.out.println("Creating a soldier for mission " + mission);
  102.             if (((City)agent).requestResources(Soldier.COST)) {
  103.                 Vec2 position = new Vec2();
  104.                 getPosition(agent, position);
  105.                 System.out.println("City " + agent + " creating soldier at location " + position);
  106.                 Soldier soldier = new Soldier(getTeam(), position, bb, world);
  107.                 world.add(soldier);
  108.                 if (mission!=null) {
  109.                     bb.removeFromMission(agent, mission);
  110.                     currentMission = null;
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     class CreateCommanderSkill extends Skill {
  117.  
  118.         public final static String CREATE_COMMANDER = "create commander";
  119.  
  120.         public CreateCommanderSkill(String name, Agent agent, double prof) {
  121.             super(name, agent, prof);
  122.         }
  123.         
  124.         public CreateCommanderSkill(Agent agent, double prof) {
  125.             this(CREATE_COMMANDER, agent, prof);
  126.         }
  127.         
  128.         int count =0;
  129.         public void apply(double time, Mission mission) {
  130.             if (((City)agent).requestResources(Commander.COST)) {
  131.                 Vec2 position = new Vec2();
  132.                 getPosition(agent, position);
  133.                 System.out.println("City " + agent + " creating commander at location " + position);
  134.                 Commander commander = new Commander(getTeam(), position, bb, world);
  135.                 world.add(commander);
  136.                 if (mission!=null) {
  137.                     bb.removeFromMission(agent, mission);
  138.                     currentMission = null;
  139.                 }
  140.             }
  141.         }
  142.     }
  143.     
  144.     //The three missions the city itself issues. Note that sometimes it might
  145.     //respond to its own mission -- i.e. by building a shield to satisfy the 
  146.     //"defendMeMission"
  147.     Mission defendmeMission;
  148.     Mission defendmeSoldierMission;
  149.     Mission workerMission;
  150.     
  151.     public final static double CITY_LIFE = 100.0;
  152.  
  153.     /*
  154.     The CITY constructor.
  155.     */
  156.     public City(int team, Vec2 pos, Blackboard bb, World world) {
  157.         super(team, 0, pos, 0.0, CITY_LIFE, 200.0, bb, world);
  158.  
  159.         //A city's skills:
  160.         
  161.         Skill createSoldier = new CreateSoldierSkill(Soldier.AttackLocationSkill.ATTACKLOCATION, this, 0.5);
  162.         skills.put(createSoldier.getName(), createSoldier);
  163.  
  164.         Skill createShield = new CreateShieldSkill(Shield.ShieldSkill.SHIELD, this, 0.5);
  165.         skills.put(createShield.getName(), createShield);
  166.  
  167.         Skill createCommander = new CreateCommanderSkill(Commander.AttackCitySkill.ATTACKCITY, this, 0.5);
  168.         skills.put(createCommander.getName(), createCommander);
  169.  
  170.         Skill createWorker = new CreateWorkerSkill(Worker.GatherSkill.GATHER, this, 0.001);
  171.         skills.put(createWorker.getName(), createWorker);
  172.  
  173.         defaultSkill = createCommander;
  174.  
  175.  
  176.         
  177.         //Create and post some missions of my own
  178.         
  179.         defendmeMission = new LocatableMission(Shield.ShieldSkill.SHIELD, 1, 1.0, pos);
  180.         bb.addMission(defendmeMission);
  181.         
  182.         workerMission = new LocatableMission(Worker.GatherSkill.GATHER, 1, 1.0, pos);
  183.         bb.addMission(workerMission);
  184.     }
  185.  
  186.     
  187.     Vector enemyList = new Vector();
  188.     Vector allyList = new Vector();
  189.     int highestNum = -1;
  190.  
  191.     /*
  192.     In addition to performing the usual action arbitration that all
  193.     agents perform, the city also has to maintain the missions it has 
  194.     posted.
  195.     */
  196.     public void actionExecute(double time) {
  197.  
  198.         if (hitPoints < CITY_LIFE/2) {
  199.             double d = (CITY_LIFE/2 - hitPoints)/(CITY_LIFE/2);
  200.             defendmeMission.setPriority(1.0+10.0*d);
  201.         }
  202.  
  203.         enemyList.removeAllElements();
  204.         allyList.removeAllElements();
  205.         world.getEnemiesWithinRegion(this, pos, 200.0, enemyList);
  206.         world.getAlliesWithinRegion(this, pos, 200.0, allyList);
  207.            int num = enemyList.size();
  208.            if (num>highestNum) {
  209.                if (defendmeSoldierMission!=null) defendmeSoldierMission.setMissionComplete();
  210.                defendmeSoldierMission = new LocatableMission(Soldier.AttackLocationSkill.ATTACKLOCATION, num, 1.0, pos);
  211.                bb.addMission(defendmeSoldierMission);
  212.                highestNum = num;
  213.            }
  214.            double d = (enemyList.size()-allyList.size());
  215.            if (d < 0) d=0;
  216.            defendmeSoldierMission.setPriority(0.1+d);
  217.         
  218.         super.actionExecute(time);
  219.     }
  220.     
  221.     public Shape createShape() {
  222.         return new Rectangle2D.Double(-10.0, -10.0, 20.0, 20.0);
  223.     }
  224.  
  225.     //The city doesn't move around, so it has no need to update its physics.
  226.     public void updatePhysics(double time) {}
  227.     
  228.     public void die() {
  229.         bb.removeMission(defendmeMission);
  230.         defendmeMission.setMissionComplete();
  231.         bb.removeMission(defendmeSoldierMission);
  232.         defendmeSoldierMission.setMissionComplete();
  233.         workerMission.setMissionComplete();
  234.         super.die();
  235.     }
  236.     
  237.     //---------------------------RESOURCE MANAGEMENT-------------------------------------
  238.     
  239.     double resources = 200.0;        //initial state
  240.     
  241.     public void addResources(double res) {
  242.         resources += res;
  243.     }
  244.     
  245.     double lastP = 1.0;
  246.     double filter = 0.99;
  247.  
  248.     /*
  249.     Requesting resources indirectly effects the priority and capacity of the
  250.     workerMission to gather resources.
  251.     */
  252.     public boolean requestResources(double cost) {
  253.         if (resources >= cost) {
  254.             resources -= cost;
  255.             workerMission.setPriority(1.0);
  256.             return true;
  257.         }
  258.         else {
  259.             int numw = 1+(int)(cost/Worker.CAPACITY);
  260.             if (workerMission.getNumber() != numw) {
  261.                 workerMission.setNumber(numw);
  262.                 if (!workerMission.isFull()) bb.addMission(workerMission);    //if it's already full, it will just be removed...
  263.             }
  264.             
  265.             if (!workerMission.isFull()) {
  266.                 lastP = lastP*filter + (1-filter)*100.0;
  267.                 workerMission.setPriority(lastP);
  268.             }
  269.             else workerMission.setPriority(1.0);
  270.             
  271.             return false;
  272.         }
  273.     }
  274. }