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 WorldData { 011 012 private double time_; 013 private int agent_id_; 014 private String nickname_; 015 private String status_; 016 private int max_agent_; 017 private String play_mode_; 018 private int yellow_score_; 019 private int blue_score_; 020 private Vector ball_; 021 private AgentState[] team_mate_list_; 022 private AgentState[] opponent_list_; 023 private Vector top_left_pole_; 024 private Vector bottom_left_pole_; 025 private Vector top_right_pole_; 026 private Vector bottom_right_pole_; 027 private Vector top_left_corner_; 028 private Vector bottom_left_corner_; 029 private Vector top_right_corner_; 030 private Vector bottom_right_corner_; 031 private Vector top_left_goal_; 032 private Vector bottom_left_goal_; 033 private Vector top_right_goal_; 034 private Vector bottom_right_goal_; 035 private Vector top_left_small_area_; 036 private Vector bottom_left_small_area_; 037 private Vector top_right_small_area_; 038 private Vector bottom_right_small_area_; 039 private Vector middle_center_; 040 private Vector top_center_; 041 private Vector bottom_center_; 042 043 public WorldData() { 044 045 } 046 047 048 public double time() { 049 return time_; 050 } 051 public int agentID() { 052 return agent_id_; 053 } 054 public String nickname() { 055 return nickname_; 056 } 057 public String status() { 058 return status_; 059 } 060 public int maxAgent() { 061 return max_agent_; 062 } 063 public String playMode() { 064 return play_mode_; 065 } 066 public int yellowScore() { 067 return yellow_score_; 068 } 069 public int blueScore() { 070 return blue_score_; 071 } 072 public Vector ball() { 073 return ball_; 074 } 075 public AgentState[] teamMates() { 076 return team_mate_list_; 077 } 078 public AgentState[] opponents() { 079 return opponent_list_; 080 } 081 /** 082 * 1,2,3...max 083 * except me. 084 */ 085 public AgentState teamMate(int id) { 086 return team_mate_list_[id-1]; 087 } 088 /** 089 * 1,2,3...max 090 */ 091 public AgentState opponent(int id) { 092 return opponent_list_[id-1]; 093 } 094 095 096 097 public Vector topLeftPole() { 098 return top_left_pole_; 099 } 100 public Vector bottomLeftPole() { 101 return bottom_left_pole_; 102 } 103 public Vector topRightPole() { 104 return top_right_pole_; 105 } 106 public Vector bottomRightPole() { 107 return bottom_right_pole_; 108 } 109 110 111 public Vector topLeftCorner() { 112 return top_left_corner_; 113 } 114 public Vector bottomLeftCorner() { 115 return bottom_left_corner_; 116 } 117 public Vector topRightCorner() { 118 return top_right_corner_; 119 } 120 public Vector bottomRightCorner() { 121 return bottom_right_corner_; 122 } 123 124 125 126 public Vector topLeftGoal() { 127 return top_left_goal_; 128 } 129 public Vector bottomLeftGoal() { 130 return bottom_left_goal_; 131 } 132 public Vector topRightGoal() { 133 return top_right_goal_; 134 } 135 public Vector bottomRightGoal() { 136 return bottom_right_goal_; 137 } 138 139 140 141 public Vector topLeftSmallArea() { 142 return top_left_small_area_; 143 } 144 public Vector bottomLeftSmallArea() { 145 return bottom_left_small_area_; 146 } 147 public Vector topRightSmallArea() { 148 return top_right_small_area_; 149 } 150 public Vector bottomRightSmallArea() { 151 return bottom_right_small_area_; 152 } 153 154 155 public Vector middleCenter() { 156 return middle_center_; 157 } 158 public Vector topCenter() { 159 return top_center_; 160 } 161 public Vector bottomCenter() { 162 return bottom_center_; 163 } 164 165 166 public Vector me() { 167 return new Vector(0, 0); 168 } 169 170 171 public void update(TagElement world_data) throws Exception { 172 time_ = getDouble(world_data, "time"); 173 agent_id_ = getInt(world_data, "agent_id"); 174 nickname_ = world_data.getChildValue("nickname"); 175 status_ = world_data.getChildValue("status"); 176 max_agent_ = getInt(world_data, "max_agent"); 177 if(team_mate_list_==null) { 178 team_mate_list_ = new AgentState[Math.max(max_agent_/2-1, 0)]; 179 opponent_list_ = new AgentState[max_agent_/2]; 180 } 181 // System.out.println(); 182 play_mode_ = world_data.getChildValue("play"); 183 TagElement score = world_data.getTagChild("score"); 184 yellow_score_ = getInt(score, "yellow"); 185 blue_score_ = getInt(score, "blue"); 186 // TagElement wheel_velocities = world_data.getTagChild("wheel_velocities"); 187 // left_wheel_velocity_ = getDouble(wheel_velocities, "left"); 188 // right_wheel_velocity_ = getDouble(wheel_velocities, "right"); 189 ball_ = getVector(world_data.getTagChild("ball")); 190 TagElement[] team_mate_tag_list_ = world_data.getTagChildren("teamMate"); 191 for(int i=0; i<Math.min(team_mate_list_.length, team_mate_tag_list_.length); i++) 192 try{ 193 team_mate_list_[i] = getAgentState(team_mate_tag_list_[i]); 194 }catch(Exception e){e.printStackTrace();} 195 196 TagElement[] opponent_tag_list_ = world_data.getTagChildren("opponent"); 197 for(int i=0; i<Math.min(opponent_list_.length, opponent_tag_list_.length); i++) 198 try{ 199 opponent_list_[i] = getAgentState(opponent_tag_list_[i]); 200 }catch(Exception e){e.printStackTrace();} 201 202 TagElement[] flag_tag_list_ = world_data.getTagChildren("flag"); 203 top_left_pole_ = getVectorByID(flag_tag_list_, "top_left_pole"); 204 bottom_left_pole_ = getVectorByID(flag_tag_list_, "bottom_left_pole"); 205 top_right_pole_ = getVectorByID(flag_tag_list_, "top_right_pole"); 206 bottom_right_pole_ = getVectorByID(flag_tag_list_, "bottom_right_pole"); 207 208 top_left_corner_ = getVectorByID(flag_tag_list_, "top_left_corner"); 209 bottom_left_corner_ = getVectorByID(flag_tag_list_, "bottom_left_corner"); 210 top_right_corner_ = getVectorByID(flag_tag_list_, "top_right_corner"); 211 bottom_right_corner_ = getVectorByID(flag_tag_list_, "bottom_right_corner"); 212 213 top_left_goal_ = getVectorByID(flag_tag_list_, "top_left_goal"); 214 bottom_left_goal_ = getVectorByID(flag_tag_list_, "bottom_left_goal"); 215 top_right_goal_ = getVectorByID(flag_tag_list_, "top_right_goal"); 216 bottom_right_goal_ = getVectorByID(flag_tag_list_, "bottom_right_goal"); 217 218 top_left_small_area_ = getVectorByID(flag_tag_list_, "top_left_small_area"); 219 bottom_left_small_area_ = getVectorByID(flag_tag_list_, "bottom_left_small_area"); 220 top_right_small_area_ = getVectorByID(flag_tag_list_, "top_right_small_area"); 221 bottom_right_small_area_ = getVectorByID(flag_tag_list_, "bottom_right_small_area"); 222 223 middle_center_ = getVectorByID(flag_tag_list_, "middle_center"); 224 top_center_ = getVectorByID(flag_tag_list_, "top_center"); 225 bottom_center_ = getVectorByID(flag_tag_list_, "bottom_center"); 226 } 227 228 public static int getInt(TagElement tag, String key) throws Exception { 229 return Integer.parseInt(tag.getChildValue(key)); 230 } 231 public static double getDouble(TagElement tag, String key) throws Exception { 232 return Double.parseDouble(tag.getChildValue(key)); 233 } 234 public static Vector getVectorByID(TagElement[] tags, String id) throws Exception { 235 for(int i=0; i<tags.length; i++) { 236 if(tags[i].getChildValue("id").equals(id)) 237 return getVector(tags[i]); 238 } 239 return null; 240 } 241 public static Vector getVector(TagElement tag) throws Exception { 242 int angle = getInt(tag, "angle"); 243 double distance = getDouble(tag, "dist"); 244 return new Vector(angle, distance); 245 } 246 public static AgentState getAgentStateByID(TagElement[] tags, String id) throws Exception { 247 for(int i=0; i<tags.length; i++) { 248 if(tags[i].getChildValue("id").equals(id)) 249 return getAgentState(tags[i]); 250 } 251 return null; 252 } 253 254 public static AgentState getAgentState(TagElement tag) throws Exception { 255 int id = getInt(tag, "id"); 256 String nickname = tag.getChildValue("nickname"); 257 int distance = (int)(getDouble(tag, "dist")); 258 int angle = (int)roundDeg(getDouble(tag, "angle")); 259 double orientation = getDouble(tag, "orientation"); 260 return new AgentState(id, nickname, new Vector(angle, distance), new Vector(orientation, 1.0)); 261 } 262 263 public static double roundDeg(double deg) { 264 while(deg>180) deg -= 360; 265 while(deg<-180) deg += 360; 266 return deg; 267 } 268 } 269 270 271 /* 272 <WorldData> 273 <time>5.25131</time> 274 <agent_id>0</agent_id> 275 <nickname>player1</nickname> 276 <status>found</status> 277 <max_agent>2</max_agent> 278 <playMode>play on</playMode> 279 <score> 280 <yellow>0</yellow> 281 <blue>0</blue> 282 </score> 283 <wheel_velocities> 284 <right>0</right> 285 <left>0</left> 286 </wheel_velocities> 287 <ball> 288 <dist>117</dist> 289 <angle>39</angle> 290 </ball> 291 <teamMate> 292 <id>1</id> 293 <nickname>player2</nickname> 294 <status>found</status> 295 <dist>89</dist> 296 <angle>48</angle> 297 <orientation>107</orientation> 298 </teamMate> 299 <opponent> 300 <id>5</id> 301 <nickname>player7</nickname> 302 <status>found</status> 303 <dist>167</dist> 304 <angle>46</angle> 305 <orientation>161</orientation> 306 </opponent> 307 <flag> 308 <id>top_left_pole</id> 309 <dist>152</dist> 310 <angle>-59</angle> 311 </flag> 312 <flag> 313 <id>bottom_left_pole</id> 314 <dist>116</dist> 315 <angle>-83</angle> 316 </flag> 317 <flag> 318 <id>top_right_pole</id> 319 <dist>310</dist> 320 <angle>57</angle> 321 </flag> 322 <flag> 323 <id>bottom_right_pole</id> 324 <dist>294</dist> 325 <angle>69</angle> 326 </flag> 327 <flag> 328 <id>top_left_corner</id> 329 <dist>236</dist> 330 <angle>-40</angle> 331 </flag> 332 <flag> 333 <id>bottom_left_corner</id> 334 <dist>126</dist> 335 <angle>-133</angle> 336 </flag> 337 <flag> 338 <id>top_right_corner</id> 339 <dist>359</dist> 340 <angle>41</angle> 341 </flag> 342 <flag> 343 <id>bottom_right_corner</id> 344 <dist>298</dist> 345 <angle>89</angle> 346 </flag> 347 <flag> 348 <id>top_left_goal</id> 349 <dist>148</dist> 350 <angle>-8</angle> 351 </flag> 352 <flag> 353 <id>bottom_left_goal</id> 354 <dist>14</dist> 355 <angle>85</angle> 356 </flag> 357 <flag> 358 <id>top_right_goal</id> 359 <dist>224</dist> 360 <angle>36</angle> 361 </flag> 362 <flag> 363 <id>bottom_right_goal</id> 364 <dist>169</dist> 365 <angle>78</angle> 366 </flag> 367 <flag> 368 <id>top_left_small_area</id> 369 <dist>83</dist> 370 <angle>-74</angle> 371 </flag> 372 <flag> 373 <id>bottom_left_small_area</id> 374 <dist>128</dist> 375 <angle>-48</angle> 376 </flag> 377 <flag> 378 <id>top_right_small_area</id> 379 <dist>259</dist> 380 <angle>68</angle> 381 </flag> 382 <flag> 383 <id>bottom_right_small_area</id> 384 <dist>277</dist> 385 <angle>55</angle> 386 </flag> 387 <flag> 388 <id>middle_center</id> 389 <dist>117</dist> 390 <angle>39</angle> 391 </flag> 392 <flag> 393 <id>top_center</id> 394 <dist>229</dist> 395 <angle>11</angle> 396 </flag> 397 <flag> 398 <id>bottom_center</id> 399 <dist>112</dist> 400 <angle>112</angle> 401 </flag> 402 </WorldData> 403 */