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

  1. package bb;
  2.  
  3. import java.util.*;
  4. import java.awt.*;
  5. import java.awt.geom.*;
  6.  
  7. /*
  8. This class maintains the physical world model, handles world rendering and 
  9. also manages the updates of the agents in both teams and manages the teams 
  10. themselves.
  11.  
  12. @author naimad
  13. */
  14.  
  15. public class World {
  16.  
  17.     int numTeams;
  18.     Vector[] teams;
  19.     Color[] colors;
  20.     
  21.     Vector blackboards = new Vector();
  22.     Vector agents = new Vector();
  23.     Vector resources = new Vector();
  24.  
  25.     //Events:
  26.     Vector[] notifiers;
  27.  
  28.     public World(int numTeams) {
  29.         this.numTeams = numTeams;
  30.         teams = new Vector[numTeams];
  31.         colors = new Color[numTeams];
  32.         notifiers = new Vector[numTeams];
  33.         for (int c=0; c < numTeams; c++) {
  34.             teams[c] = new Vector();
  35.             notifiers[c] = new Vector();
  36.         }
  37.         
  38.         if (numTeams>0) colors[0] = Color.red;
  39.         if (numTeams>1) colors[1] = Color.blue;
  40.         if (numTeams>2) colors[2] = Color.green;
  41.         //etc...
  42.     }
  43.  
  44.     public void add(Agent a) {
  45.         agents.addElement(a);
  46.         int t = a.getTeam();
  47.         if (t < numTeams) {
  48.             teams[t].addElement(a);
  49.         }
  50.         else System.out.println("ERROR: TOO MANY TEAMS!");
  51.     }
  52.     
  53.     public void add(Blackboard bb) {
  54.         blackboards.addElement(bb);
  55.     }
  56.     
  57.     public void add(Resource r) {
  58.         resources.addElement(r);
  59.     }
  60.  
  61.     public void remove(Agent a) {
  62.         agents.remove(a);
  63.         int t = a.getTeam();
  64.         if (t < numTeams) {
  65.             teams[t].remove(a);
  66.         }
  67.         else System.out.println("ERROR: TOO MANY TEAMS!");
  68.     }
  69.     
  70.     public Vector getAgents() {
  71.         return agents;
  72.     }
  73.     
  74.     //----------------------------UPDATES-------------------------------
  75.  
  76.     public void update(double time) {
  77.  
  78.         //Update the resource pools
  79.         for (int c=0; c < resources.size(); c++) {
  80.             Resource r = (Resource)resources.elementAt(c);
  81.             if (r.resources <= 0.0) {
  82.                 c--;
  83.                 resources.removeElement(r);
  84.             }
  85.         }
  86.  
  87.         //Update the agents (note that we are NOT updating one team at a time -
  88.         //this avoids giving one team an overall execution order advantage.
  89.         //Instead all agents are dumped into one big list in order of creation
  90.         //time, which, after the initial few, is more or less random.
  91.         for (int c=0; c < agents.size(); c++) {
  92.             Agent a = (Agent)agents.elementAt(c);
  93.             a.actionSelect(time);
  94.         }
  95.  
  96.         //Have the blackboards perform their arbitration
  97.         for (int c=0; c < blackboards.size(); c++) {
  98.             Blackboard bb = (Blackboard)blackboards.elementAt(c);
  99.             bb.updateMissionAssignments(time);
  100.         }
  101.         
  102.         //Have the agents execute their current skills.
  103.         for (int c=0; c < agents.size(); c++) {
  104.             Agent a = (Agent)agents.elementAt(c);
  105.             a.actionExecute(time);
  106.         }
  107.  
  108.         //Finally update their physics, or, if they are dead, 
  109.         //remove them from the list.
  110.         for (int c=0; c < agents.size(); c++) {
  111.             Agent a = (Agent)agents.elementAt(c);
  112.             if (a.isAlive()) a.updatePhysics(time);
  113.             else {
  114.                 for (int d=0; d < notifiers[a.getTeam()].size(); d++) {
  115.                     EventNotifier en = (EventNotifier)(notifiers[a.getTeam()].elementAt(d));
  116.                     en.deathNotification(a, a.pos);
  117.                 }
  118.                 remove(a);
  119.             }
  120.         }
  121.     }
  122.     
  123.     void updateGraphics(double time, Graphics2D g2) {
  124.         
  125.         g2.setPaint(java.awt.Color.gray);
  126.         
  127.         for (int c=0; c < resources.size(); c++) {
  128.             Resource r = (Resource)resources.elementAt(c);
  129.             r.updateGraphics(time, g2);
  130.         }
  131.         
  132.         for (int d=0; d < numTeams; d++) {
  133.         
  134.             //change the color!
  135.             g2.setPaint(colors[d]);
  136.         
  137.             for (int c=0; c < teams[d].size(); c++) {
  138.                 Agent a = (Agent)teams[d].elementAt(c);
  139.                 a.updateGraphics(time, g2);
  140.             }
  141.         }
  142.     }
  143.     
  144.     //---------------------------------SERVICES---------------------------------
  145.  
  146.     /*
  147.     These are a series of self-explanatory services that the world provides to help
  148.     the agents make their decisions about what to do.
  149.     */
  150.  
  151.     Vec2 vtemp = new Vec2();
  152.         
  153.     public void getEnemiesWithinRegion(Agent sensingAgent, Vec2 center, double range, Vector addToList) {
  154.         double rangeSquared = range*range;
  155.         for (int c=0; c < agents.size(); c++) {
  156.             Agent atemp = (Agent)agents.elementAt(c);
  157.             if (atemp.getTeam()!=sensingAgent.getTeam()) {
  158.                 atemp.getPosition(sensingAgent, vtemp);
  159.                 double dtemp = center.distanceSquared(vtemp);
  160.                 if (dtemp <= rangeSquared) addToList.addElement(atemp);
  161.             }
  162.         }
  163.     }
  164.  
  165.     public void getEnemyCitiesWithinRegion(Agent sensingAgent, Vec2 center, double range, Vector addToList) {
  166.         double rangeSquared = range*range;
  167.         for (int c=0; c < agents.size(); c++) {
  168.             Agent atemp = (Agent)agents.elementAt(c);
  169.             if (atemp.getTeam()!=sensingAgent.getTeam() && (atemp instanceof City)) {
  170.                 atemp.getPosition(sensingAgent, vtemp);
  171.                 double dtemp = center.distanceSquared(vtemp);
  172.                 if (dtemp <= rangeSquared) addToList.addElement(atemp);
  173.             }
  174.         }
  175.     }
  176.     
  177.     
  178.     public Agent getNearestWithinRegion(Agent sensingAgent, Vec2 center, double range) {
  179.         Agent nearest = null;
  180.         
  181.         double nearVal = Double.MAX_VALUE;
  182.         for (int c=0; c < agents.size(); c++) {
  183.             Agent atemp = (Agent)agents.elementAt(c);
  184.             if (atemp.getTeam()!=sensingAgent.getTeam()) {
  185.                 atemp.getPosition(sensingAgent, vtemp);
  186.                 double dtemp = center.distanceSquared(vtemp);
  187.                 if (dtemp < nearVal) {
  188.                     nearVal = dtemp;
  189.                     nearest = atemp;
  190.                 }
  191.             }
  192.         }
  193.         if (nearVal <= range*range) return nearest;
  194.         else return null;
  195.     }
  196.  
  197.     public City getNearestEnemyCityWithinRange(Agent sensingAgent, Vec2 center, double range) {
  198.         Agent nearest = null;
  199.         
  200.         double nearVal = Double.MAX_VALUE;
  201.         for (int c=0; c < agents.size(); c++) {
  202.             Agent atemp = (Agent)agents.elementAt(c);
  203.             if (atemp.getTeam()!=sensingAgent.getTeam() && (atemp instanceof City)) {
  204.                 atemp.getPosition(sensingAgent, vtemp);
  205.                 double dtemp = center.distanceSquared(vtemp);
  206.                 if (dtemp < nearVal) {
  207.                     nearVal = dtemp;
  208.                     nearest = atemp;
  209.                 }
  210.             }
  211.         }
  212.         if (nearVal <= range*range) return (City)nearest;
  213.         else return null;
  214.     }
  215.     
  216.     
  217.     public void getAlliesWithinRegion(Agent sensingAgent, Vec2 center, double range, Vector addToList) {
  218.         double rangeSquared = range*range;
  219.         for (int c=0; c < agents.size(); c++) {
  220.             Agent atemp = (Agent)agents.elementAt(c);
  221.             if (atemp.getTeam()==sensingAgent.getTeam()) {
  222.                 atemp.getPosition(sensingAgent, vtemp);
  223.                 double dtemp = center.distanceSquared(vtemp);
  224.                 if (dtemp <= rangeSquared) addToList.addElement(atemp);
  225.             }
  226.         }
  227.     }
  228.     
  229.     public void getAlliedCitiesWithinRegion(Agent sensingAgent, Vec2 center, double range, Vector addToList) {
  230.         double rangeSquared = range*range;
  231.         for (int c=0; c < agents.size(); c++) {
  232.             Agent atemp = (Agent)agents.elementAt(c);
  233.             if (atemp.getTeam()==sensingAgent.getTeam() && (atemp instanceof City)) {
  234.                 atemp.getPosition(sensingAgent, vtemp);
  235.                 double dtemp = center.distanceSquared(vtemp);
  236.                 if (dtemp <= rangeSquared) addToList.addElement(atemp);
  237.             }
  238.         }
  239.     }
  240.     
  241.     public City getNearestAlliedCityWithinRange(Agent sensingAgent, Vec2 center, double range) {
  242.         Agent nearest = null;
  243.         
  244.         double nearVal = Double.MAX_VALUE;
  245.         for (int c=0; c < agents.size(); c++) {
  246.             Agent atemp = (Agent)agents.elementAt(c);
  247.             if (atemp.getTeam()==sensingAgent.getTeam() && (atemp instanceof City)) {
  248.                 atemp.getPosition(sensingAgent, vtemp);
  249.                 double dtemp = center.distanceSquared(vtemp);
  250.                 if (dtemp < nearVal) {
  251.                     nearVal = dtemp;
  252.                     nearest = atemp;
  253.                 }
  254.             }
  255.         }
  256.         if (nearVal <= range*range) return (City)nearest;
  257.         else return null;
  258.     }
  259.     
  260.     
  261.     //-----------------------------RESOURCES-----------------------------------
  262.     
  263.     public Resource getNearestResourceInRange(Vec2 center, double range) {
  264.         Resource nearest = null;
  265.         double nearVal = Double.MAX_VALUE;
  266.  
  267.         for (int c=0; c < resources.size(); c++) {
  268.             Resource atemp = (Resource)resources.elementAt(c);
  269.             double dtemp = center.distanceSquared(atemp.pos);
  270.             if (dtemp < nearVal) {
  271.                 nearVal = dtemp;
  272.                 nearest = atemp;
  273.             }
  274.         }
  275.         if (nearVal <= range*range) return nearest;
  276.         else return null;
  277.     }
  278.  
  279.     //----------------------------EVENTS-----------------------------
  280.     
  281.     public void addEventNotifier(int team, EventNotifier en) {
  282.         notifiers[team].addElement(en);
  283.     }
  284. }
  285.