2-APL UPC project.
1package blockworld;
2
3/*
4 3APL Blockworld program by Jelle Herold, copyright 2003.
5 Written for the Intelligent Systems group at Utrecht University.
6 This is LGPL software.
7
8 $Id: Agent.java,v 1.6 2004/09/14 10:58:06 cvs-3apl Exp $
9 */
10
11import java.awt.Point;
12
13import blockworld.lib.Signal;
14
15// / Agent representation in Env. This doubles as a plugin instance.
16public class Agent
17{
18 protected String _name;
19
20 /**
21 * _position is null means agent did not enter the world. This method is
22 * only for the blockworld package to prevent setting the position by other
23 * means that through the north/south/west/east methods of the environment.
24 * \todo is protection "package" correct?
25 */
26 protected Point _position = null;
27
28 protected TypeObject _bomb = null;
29
30 int _colorID = 0;
31
32 public Agent( String name ) {
33 _name = name;
34 }
35
36 public String getName() {
37 return _name;
38 }
39
40 /**
41 * Get current agent position. \returns This will return null if the agent
42 * is not entered in the world.
43 */
44 public Point getPosition() {
45 return _position;
46 }
47
48 // / Sense if agent is carrying a bomb.
49 // / \return True if agent is carrying a bomb, false otherwise.
50 public TypeObject senseBomb() {
51 return _bomb;
52 }
53 public TypeObject senseBomb(String type) {
54 return (_bomb != null && _bomb.getType().equals(type))?_bomb:null;
55 }
56
57 public boolean atCapacity() {
58 return _bomb != null;
59 }
60
61 public void pickBomb(TypeObject bomb) {
62 _bomb = bomb;
63 }
64
65 public void dropBomb() {
66 _bomb = null;
67 }
68
69 /**
70 * Check if agent is "entered" in the environment. That is, it has a
71 * position in the world. \returns true if agent is entered in the
72 * environment
73 */
74 public boolean isEntered() {
75 return (_position != null);
76 }
77
78 /**
79 * Called by the interpreter when the agent this instance refers to is
80 * reset. \todo signalMove show become special signal enter/exit
81 */
82 public void reset() {
83 _position = null;
84 _bomb = null; // Fixed bug pointed out by Bas of UU - Sohan
85 signalMove.emit();
86 }
87
88 /**
89 * returns the unique name of the agent this instance refers to.
90 */
91 public String toString() {
92 return getName();
93 }
94
95 // / removes all listeners
96 public void deleteObservers() {
97 signalMove.deleteObservers();
98 signalPickupBomb.deleteObservers();
99 signalDropBomb.deleteObservers();
100
101 signalMoveSucces.deleteObservers();
102 signalPickupBombSucces.deleteObservers();
103 signalDropBombSucces.deleteObservers();
104 }
105
106 // / emitted if agent attemps movement (succesful or not)
107 public transient Signal signalMove = new Signal( "agent attempts move" );
108
109 // / emitted if agent attemps to pickup a bomb (succesful or not)
110 public transient Signal signalPickupBomb = new Signal(
111 "agent attempts pickup" );
112
113 // / emitted if agent attemps to drop a bomb (succesful or not)
114 public transient Signal signalDropBomb = new Signal( "agent attempts drop" );
115
116 // / emitted if agent succesfully moves
117 public transient Signal signalMoveSucces = new Signal(
118 "agent succesful move" );
119
120 // / emitted if agent (succesfully) picks up a bomb
121 public transient Signal signalPickupBombSucces = new Signal(
122 "agent succesful pickup" );
123
124 // / emitted if agent (succesfully) drops a bomb
125 public transient Signal signalDropBombSucces = new Signal(
126 "agent sucessful drop" );
127}