home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 07 DecisionMaking Architectures / 01 Isla, Blumberg / Worker.java < prev    next >
Encoding:
Java Source  |  2001-09-25  |  4.1 KB  |  145 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. A worked is just good for gathering resources. It always picks up its load 
  10. from the nearest non-empty resource pool. Then it dumps if off at the city
  11. specified by its mission. If it has no mission, it dumps it off at the nearest
  12. city.
  13.  
  14. @author naimad
  15. */
  16.  
  17. public class Worker extends Agent {
  18.  
  19.     class GatherSkill extends Skill {
  20.     
  21.         public final static String GATHER = "gather";
  22.         public final static double DIST_THRESHOLD = 10.0;
  23.  
  24.         public GatherSkill(Agent agent, double prof) {
  25.             this(GATHER, agent, prof);
  26.         }
  27.         
  28.         public GatherSkill(String name, Agent agent, double prof) {
  29.             super(name, agent, prof);
  30.         }
  31.  
  32.         Vec2 vtemp = new Vec2();
  33.         private City pickCity(Mission mission) {
  34.             City city = null;
  35.             if (mission==null) {
  36.                 city = agent.world.getNearestAlliedCityWithinRange(agent, agent.pos, 2000.0);
  37.             }
  38.             else if (mission instanceof LocatableMission) {
  39.                 LocatableMission lmission = (LocatableMission)mission;
  40.                 lmission.getTargetPosition(vtemp);
  41.                 city = agent.world.getNearestAlliedCityWithinRange(agent, vtemp, 2000.0);
  42.             }
  43.             return city;
  44.         }
  45.         
  46.         private Resource pickResource() {
  47.             return agent.world.getNearestResourceInRange(agent.pos, 2000.0);
  48.         }
  49.         
  50.         double resources = 0.0;
  51.         City targetCity = null;
  52.         Resource targetResource = null;
  53.         
  54.         public void apply(double time, Mission mission) {
  55.             //find the nearest res source and go there and get res!
  56.             if (resources > Worker.CAPACITY) {
  57.                 //go back to city
  58.                 if (targetCity==null) targetCity = pickCity(mission);
  59.                 if (targetCity!=null) {
  60.                     targetCity.getPosition(agent, vtemp);
  61.                     Vec2.sub(vtemp, agent.pos, vtemp);
  62.                     
  63.                     double dist = vtemp.mag();
  64.                     
  65.                     if (dist < DIST_THRESHOLD) {
  66.                         targetCity.addResources(resources);
  67.                         resources = 0.0;
  68.                         bb.removeFromMission(agent, mission);
  69.                         agent.currentMission = null;
  70.                         agent.currentSkill = null;
  71.                         targetResource=null;
  72.                     }
  73.                     else {
  74.                         vtemp.normalize();
  75.                         agent.addForce(vtemp);
  76.                     }
  77.                 }
  78.             }
  79.             else {
  80.                 //go to the appropriate resource
  81.                 if (targetResource==null || targetResource.resources <= 0.0) targetResource = pickResource();
  82.                 if (targetResource!=null) {
  83.                     vtemp.set(targetResource.pos);
  84.                     Vec2.sub(vtemp, agent.pos, vtemp);
  85.                     
  86.                     double dist = vtemp.mag();
  87.                     
  88.                     if (dist < DIST_THRESHOLD) {
  89.                         resources += targetResource.extractResources();
  90.                         targetCity=null;
  91.                     }
  92.                     else {
  93.                         vtemp.normalize();
  94.                         agent.addForce(vtemp);
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.     }
  100.  
  101.     public final static int COST = 30;
  102.     public final static double CAPACITY = 50.0;
  103.  
  104.     public Worker(int team, Vec2 pos, Blackboard bb, World world) {
  105.         super(team, 0, pos, 5.0, 10.0, 250.0, bb, world);
  106.  
  107.         //subclasses should add skills to their skill hashTable...
  108.         Skill gather = new GatherSkill(this, 1.0);
  109.         skills.put(gather.getName(), gather);
  110.  
  111.         defaultSkill = gather;
  112.     }
  113.  
  114.     //Give workers the ability to avoid enemies (since we know they can't defend themselves)
  115.     Vector elist = new Vector();
  116.     Vec2 vtemp = new Vec2();
  117.     public void updatePhysics(double time) {
  118.         
  119.         elist.removeAllElements();
  120.         world.getEnemiesWithinRegion(this, pos, 110.0, elist);
  121.         
  122.         for (int c=0; c < elist.size(); c++) {
  123.             Agent a = (Agent)elist.elementAt(c);
  124.             if (!(a instanceof Worker)) {    //we're not scared of workers!
  125.                 a.getPosition(this, vtemp);
  126.                 Vec2.sub(pos, vtemp, vtemp);
  127.                 double d = vtemp.mag();
  128.                 if (d == 0.0) {
  129.                     vtemp.set(1.0, 0.0);
  130.                     d = 1.0;
  131.                 }
  132.                 else vtemp.normalize();
  133.                 vtemp.scale((110.0-d)/40.0);
  134.                 this.addForce(vtemp);
  135.             }
  136.         }
  137.         
  138.         super.updatePhysics(time);
  139.     }
  140.     
  141.     public Shape createShape() {
  142.         return new Rectangle2D.Double(-4.0, 4.0, 8.0, 8.0) ;
  143.     }    
  144. }
  145.