001    package org.agent;
002    
003    import org.util.xml.io.*;
004    import org.util.xml.element.*;
005    import org.*;
006    import org.util.*;
007    
008    import javax.swing.*;
009    
010    public class SampleAgent {
011    
012            protected FieldPanel view;
013            protected WorldData w;
014            private TagElement message = null;
015            private RealPlayer player;
016    
017            public SampleAgent(RealPlayer p) throws Exception {
018                    player = p;
019                    view = new FieldPanel();
020                    JFrame frame = new JFrame(player.getNickName());
021                    frame.setContentPane(view);
022                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
023                    frame.pack();
024                    frame.setVisible(true);
025                    
026                    w = new WorldData();
027    
028    //////////////////////////////////////////////////
029    ////This will be removed!!!////////////////
030    //      player.receiveTag();////////////////////////
031    //////////////////////////////////////////////////
032    //////////////////////////////////////////////////
033            }
034    
035            /**
036             * Accessible Object
037             *    w : WorldData
038             *    ex. w.ball(), w. 
039             * view : FieldPanel
040             * 
041             * 
042             */
043            public void plan(WorldData w, FieldPanel view) throws Exception {
044                    Vector me = w.me();
045                    Vector ball = w.ball();
046                    Vector goal = w.bottomRightGoal();
047    
048                    if(ball.dis() < 100) {
049                            view.drawString("kick!", ball);
050                            kick(goal, 1);
051                    } else {
052                            view.drawLine(me, ball);
053                            view.drawString("go to ball", ball);
054                            int right = 0;
055                            int left = 0;
056                            if(ball.rad() > deg2rad(30)) {
057                                    right = 0;
058                                    left = 50;
059                            } else if(ball.deg() < deg2rad(-30)) {
060                                    right = 50;
061                                    left = 0;
062                            } else {
063                                    right = 50;     
064                                    left = 50;
065                            }
066                            velocities(right, left);
067                    }
068            }
069            
070    /*
071    long start = System.currentTimeMillis();
072    long end = System.currentTimeMillis();
073    System.out.println("plan: "+(end-start));
074    */
075    
076            public void start() throws Exception {
077                    for(int counter=0; true; counter++) {
078                            
079                            TagElement tag = null;
080                            try{
081    alert("receiving: "+counter);
082                                    long start = System.currentTimeMillis();
083                                    tag = player.receiveTag();
084                                    long end = System.currentTimeMillis();
085    alert("received: "+(end-start)+"[ms]");
086                                    w.update(tag);
087                                    view.update(w);
088                                    plan(w, view);
089                            } catch(Exception e) {
090                                    e.printStackTrace();
091                                    if(tag!=null)
092                                            System.err.println(tag.toString());
093                            }
094                            try{
095                                    if(message==null) velocities(0, 0);
096    alert("sending");
097                                    player.send(message);
098    alert("send: "+message);
099                            }catch(Exception e){
100                                    e.printStackTrace();
101                            }
102                    }
103            }
104            public void kick(Vector angle, int force) throws Exception {
105                    message=player.createKickMessage(angle.deg(), force);
106            }
107            public void kick(Vector angle_force) throws Exception {
108                    message=player.createKickMessage(angle_force.deg(), (int)angle_force.dis());
109            }
110            public void velocities(double right, double left) throws Exception {
111                    message=player.createVerocitiesMessage(right, left);
112            }
113            
114            public static double rad2deg(double rad) {
115                    return rad*180/Math.PI;
116            }
117            public static double deg2rad(double deg) {
118                    return deg*Math.PI/180;
119            }
120            public static void main(String[] args) throws Exception {
121    
122                    if(args.length==1 && args[0].endsWith("help")) {
123                            System.out.println("java SampleAgent [IP]");
124                            System.out.println("java SampleAgent [IP PORT]");
125                            System.out.println("java SampleAgent [IP PORT NICKNAME RCID VTID]");
126                            System.exit(1);
127                    }
128    
129                    String ip = "localhost";
130                    int port = 3310;
131                    int timeout = 0; 
132                    String nickname = "sample player";
133                    int rcid = 0;
134                    int vtid = 0;
135    
136                    if(args.length>0) ip = args[0];
137                    if(args.length>1) port = Integer.parseInt(args[1]);
138                    if(args.length==5) {
139                            nickname = args[2];
140                            rcid = Integer.parseInt(args[3]);
141                            vtid = Integer.parseInt(args[4]);
142                    }
143                    
144                    System.out.println("ip: "+ip);
145                    System.out.println("port: "+port);
146                    System.out.println("timeout: "+timeout);
147                    System.out.println("nickname: "+nickname);
148                    System.out.println("rcid: "+rcid);
149                    System.out.println("vtid: "+vtid);
150                    
151                    
152                    RealPlayer player = new RealPlayer(ip, port, timeout, nickname, rcid, vtid);
153                    
154                    SampleAgent agent = new SampleAgent(player);
155                    agent.start();
156            }
157    
158            public static void alert(Object message) {
159                    System.out.println(message);
160            }
161    }
162    
163    /*
164    <WorldData>
165       <time>5.25131</time>
166       <agent_id>0</agent_id>
167       <nickname>player1</nickname>
168       <status>found</status>
169       <max_agent>2</max_agent>
170       <playMode>play on</playMode>
171       <score>
172          <yellow>0</yellow>
173          <blue>0</blue>
174       </score>
175       <wheel_velocities>
176          <right>0</right>
177          <left>0</left>
178       </wheel_velocities>
179       <ball>
180          <dist>117</dist>
181          <angle>39</angle>
182       </ball>
183       <teamMate>
184          <id>1</id>
185          <nickname>player2</nickname>
186          <status>found</status>
187          <dist>89</dist>
188          <angle>48</angle>
189          <orientation>107</orientation>
190       </teamMate>
191       <opponent>
192          <id>5</id>
193          <nickname>player7</nickname>
194          <status>found</status>
195          <dist>167</dist>
196          <angle>46</angle>
197          <orientation>161</orientation>
198       </opponent>
199       <flag>
200          <id>top_left_pole</id>
201          <dist>152</dist>
202          <angle>-59</angle>
203       </flag>
204       <flag>
205          <id>bottom_left_pole</id>
206          <dist>116</dist>
207          <angle>-83</angle>
208       </flag>
209       <flag>
210          <id>top_right_pole</id>
211          <dist>310</dist>
212          <angle>57</angle>
213       </flag>
214       <flag>
215          <id>bottom_right_pole</id>
216          <dist>294</dist>
217          <angle>69</angle>
218       </flag>
219       <flag>
220          <id>top_left_corner</id>
221          <dist>236</dist>
222          <angle>-40</angle>
223       </flag>
224       <flag>
225          <id>bottom_left_corner</id>
226          <dist>126</dist>
227          <angle>-133</angle>
228       </flag>
229       <flag>
230          <id>top_right_corner</id>
231          <dist>359</dist>
232          <angle>41</angle>
233       </flag>
234       <flag>
235          <id>bottom_right_corner</id>
236          <dist>298</dist>
237          <angle>89</angle>
238       </flag>
239       <flag>
240          <id>top_left_goal</id>
241          <dist>148</dist>
242          <angle>-8</angle>
243       </flag>
244       <flag>
245          <id>bottom_left_goal</id>
246          <dist>14</dist>
247          <angle>85</angle>
248       </flag>
249       <flag>
250          <id>top_right_goal</id>
251          <dist>224</dist>
252          <angle>36</angle>
253       </flag>
254       <flag>
255          <id>bottom_right_goal</id>
256          <dist>169</dist>
257          <angle>78</angle>
258       </flag>
259       <flag>
260          <id>top_left_small_area</id>
261          <dist>83</dist>
262          <angle>-74</angle>
263       </flag>
264       <flag>
265          <id>bottom_left_small_area</id>
266          <dist>128</dist>
267          <angle>-48</angle>
268       </flag>
269       <flag>
270          <id>top_right_small_area</id>
271          <dist>259</dist>
272          <angle>68</angle>
273       </flag>
274       <flag>
275          <id>bottom_right_small_area</id>
276          <dist>277</dist>
277          <angle>55</angle>
278       </flag>
279       <flag>
280          <id>middle_center</id>
281          <dist>117</dist>
282          <angle>39</angle>
283       </flag>
284       <flag>
285          <id>top_center</id>
286          <dist>229</dist>
287          <angle>11</angle>
288       </flag>
289       <flag>
290          <id>bottom_center</id>
291          <dist>112</dist>
292          <angle>112</angle>
293       </flag>
294    </WorldData>
295    */