Changeset 3071 for trunk/twcore/src/twcore/bots/multibot/dodgeball
- Timestamp:
- 12/03/08 16:04:23 (21 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/twcore/src/twcore/bots/multibot/dodgeball/dodgeball.java
r2678 r3071 19 19 20 20 public class dodgeball extends MultiModule { 21 21 22 22 // Dodgeball game parameters 23 23 private int dodgetime = 3; //secs … … 25 25 private int orgBallCount = -1; 26 26 protected boolean running = false; 27 27 28 28 private HashMap<Integer,Ball> balls = new HashMap<Integer,Ball>(); 29 29 // <ball id,Ball> 30 30 31 31 private int ballMode = 0; 32 32 // 0 => ball not carried 33 33 // 1 => ball carried 34 34 35 35 private HashMap<Short,Integer> players = new HashMap<Short,Integer>(); 36 36 // List with PlayerIDs of players in the game 37 37 // <PlayerID,number of eliminations> 38 38 39 39 private TimerTask checkWinner; 40 40 41 41 private List<String> publicHelp = Arrays.asList(new String[]{ 42 42 "+-----------------------------------------------------------------+", … … 51 51 "+-----------------------------------------------------------------+" 52 52 }); 53 53 54 54 private List<String> staffHelp = Arrays.asList(new String[]{ 55 55 "| !start - Starts a new game |", … … 70 70 "+-----------------------------------------------------------------+" 71 71 }); 72 72 73 73 @Override 74 74 public void requestEvents(ModuleEventRequester eventRequester) { … … 80 80 @Override 81 81 public void cancel() { 82 82 83 83 // When unloading module, set ball count back to what it was when loading module 84 84 if(orgBallCount != -1 && orgBallCount != ballCount) { … … 91 91 List<String> help = new ArrayList<String>(publicHelp); 92 92 help.addAll(staffHelp); 93 93 94 94 // Add status line at bottom of staff's !help menu 95 95 help.addAll( Arrays.asList( new String[] { … … 97 97 "+-----------------------------------------------------------------+" 98 98 })); 99 99 100 100 return help.toArray(new String[]{}); 101 101 } … … 111 111 return !running; 112 112 } 113 113 114 114 /* (non-Javadoc) 115 115 * @see twcore.bots.MultiModule#handleEvent(twcore.core.events.Message) … … 117 117 @Override 118 118 public void handleEvent(Message event) { 119 120 if(ballCount == 0) 119 120 if(ballCount == 0) 121 121 return; 122 122 123 123 if(event.getMessageType() == Message.PRIVATE_MESSAGE) { 124 124 String message = event.getMessage(); … … 129 129 } 130 130 OperatorList opList = m_botAction.getOperatorList(); 131 131 132 132 // Public commands 133 133 if(!opList.isER(name)) { … … 144 144 running = true; 145 145 m_botAction.shipResetAll(); 146 146 147 147 // Fill list of players 148 148 for(Player p:m_botAction.getPlayingPlayers()) { 149 149 players.put(p.getPlayerID(), 0); 150 150 } 151 151 152 152 } else { 153 153 m_botAction.sendPrivateMessage(playerID, "Dodgeball is already started. PM !stop to stop dodgeball."); … … 163 163 } 164 164 } 165 165 166 166 if(message.startsWith("!usage")) { 167 167 List<String> usage = Arrays.asList(new String[]{ … … 188 188 m_botAction.privateMessageSpam(playerID, usage); 189 189 } 190 190 191 191 if(message.startsWith("!add")) { 192 192 String arg1 = message.substring(4).trim(); 193 193 194 194 if(!running) { 195 195 m_botAction.sendPrivateMessage(playerID, "The command !add can only be used when the game is started. Command aborted."); 196 196 return; 197 197 } 198 198 199 199 if(arg1 == null || arg1.length() == 0) { 200 200 m_botAction.sendPrivateMessage(playerID, "Syntax error. Please specify part of the playername to add to the game. Type ::!help for more information."); 201 201 return; 202 202 } 203 203 204 204 Player selectedPlayer = m_botAction.getFuzzyPlayer(arg1); 205 205 206 206 if(selectedPlayer == null) { 207 207 m_botAction.sendPrivateMessage(playerID, "The specified player can't be found in this arena. Please specify part of the playername to add to the game. Type ::!help for more information."); … … 212 212 return; 213 213 } 214 214 215 215 players.put(selectedPlayer.getPlayerID(), 0); 216 216 m_botAction.sendPrivateMessage(playerID, "Player '"+selectedPlayer.getPlayerName()+"' added to the game."); 217 217 checkWinner(); 218 218 } 219 219 220 220 if(message.startsWith("!remove")) { 221 221 String arg1 = message.substring(7).trim(); 222 222 223 223 if(!running) { 224 224 m_botAction.sendPrivateMessage(playerID, "The command !remove can only be used when the game is started. Command aborted."); 225 225 return; 226 226 } 227 227 228 228 if(arg1 == null || arg1.length() == 0) { 229 229 m_botAction.sendPrivateMessage(playerID, "Syntax error. Please specify part of the playername to remove from the game. Type ::!help for more information."); 230 230 return; 231 231 } 232 232 233 233 Player selectedPlayer = m_botAction.getFuzzyPlayer(arg1); 234 234 235 235 if(selectedPlayer == null) { 236 236 m_botAction.sendPrivateMessage(playerID, "The specified player can't be found in this arena. Use the !remove-id and the !list command to remove a specific player using a player ID."); … … 245 245 checkWinner(); 246 246 } 247 247 248 248 if(message.startsWith("!list")) { 249 249 if(!running) { … … 251 251 return; 252 252 } 253 253 254 254 List<String> list = Arrays.asList(new String[]{ 255 255 "ID Name Ship ", 256 256 "---- -------------------------- ----------" 257 257 }); 258 258 259 259 for(short id:players.keySet()) { 260 260 Player p = m_botAction.getPlayer(id); … … 265 265 ); 266 266 } 267 267 268 268 m_botAction.privateMessageSpam(playerID, list); 269 270 } 271 269 270 } 271 272 272 if(message.startsWith("!dodgetime")) { 273 273 String arg1 = message.substring(7).trim(); 274 274 int argument = 3; 275 275 276 276 if(arg1 == null || arg1.length() == 0) { 277 277 m_botAction.sendPrivateMessage(playerID, "Current dodgetime: "+dodgetime+" seconds."); … … 283 283 return; 284 284 } 285 285 286 286 argument = Integer.parseInt(arg1); 287 287 288 288 if(argument > 60) { 289 289 m_botAction.sendPrivateMessage(playerID, "You can't set a dodge time larger then 60 seconds."); 290 290 return; 291 291 } 292 292 293 293 dodgetime = argument; 294 294 m_botAction.sendPrivateMessage(playerID, "Dodge time set to: "+dodgetime+" seconds."); 295 295 m_botAction.sendPrivateMessage(playerID, "If game is already started, will take effect immediatly."); 296 296 } 297 297 298 298 if(message.startsWith("!fixball")) { 299 299 // !fixball <x>,<y> - Fixes all balls on <x>,<y> pos 300 300 // !fixball <id>,<x>,<y> - Fixes ball <id> on <x>,<y> pos 301 301 String arg1 = message.substring(8).trim(); 302 302 303 303 if(arg1 == null || arg1.length() == 0 || arg1.indexOf(',') == -1) { 304 304 m_botAction.sendPrivateMessage(playerID, "Syntax error. Please specify the <x> and <y> coordinates where to fix all the balls. Type ::!help for more information."); 305 305 return; 306 306 } 307 307 308 308 String[] args = arg1.split(","); 309 309 int id = -1; 310 310 int x = -1; 311 311 int y = -1; 312 312 313 313 if(args.length < 2 || args.length > 3 || Tools.isAllDigits(arg1.replace(",", "")) == false) { 314 314 m_botAction.sendPrivateMessage(playerID, "Syntax error. Please specify the <x> and <y> coordinates where to fix all the balls. Type ::!help for more information."); 315 315 return; 316 316 } 317 317 318 318 if(args.length == 2) { 319 319 x = Integer.parseInt(args[0]); 320 320 y = Integer.parseInt(args[1]); 321 321 322 322 for(int i = 0 ; i < ballCount; i++) { 323 323 m_botAction.sendPrivateMessage(playerID, "Moving ball #"+(i+1)+" to "+x+","+y); 324 324 this.moveBall(i, x, y); 325 325 } 326 326 327 327 } else if(args.length == 3) { 328 328 id = Integer.parseInt(args[0]); 329 329 x = Integer.parseInt(args[1]); 330 330 y = Integer.parseInt(args[2]); 331 331 332 332 if(id > 0) id--; 333 333 m_botAction.sendPrivateMessage(playerID, "Moving ball #"+(id+1)+" to "+x+","+y); … … 335 335 } 336 336 } 337 337 338 338 if(message.startsWith("!setballcount ")) { 339 339 String arg1 = message.substring(13).trim(); 340 340 int argument = 1; 341 341 342 342 if(arg1 == null || arg1.length() == 0) { 343 343 m_botAction.sendPrivateMessage(playerID, "Syntax error. Please specify number of balls to set. Type ::!help for more information"); … … 348 348 return; 349 349 } 350 350 351 351 argument = Integer.parseInt(arg1); 352 352 353 353 if(argument < 1 || argument > 8) { 354 354 m_botAction.sendPrivateMessage(playerID, "You can't set the ball count higher then 8 or lower then 1."); 355 355 return; 356 356 } 357 357 358 358 m_botAction.sendUnfilteredPublicMessage("?set Soccer:BallCount:"+argument); 359 359 m_botAction.sendPrivateMessage(playerID, "Ball count set to "+argument+" balls."); 360 360 m_botAction.sendPrivateMessage(playerID, "The arena settings are already changed and the balls will (dis)appear in a few seconds."); 361 361 362 362 } 363 363 } 364 364 } 365 365 366 366 // Gets the result from ?get Soccer:BallCount or ?set Soccer:BallCount: 367 367 // Soccer:BallCount=1 … … 371 371 if(Tools.isAllDigits(count)) { 372 372 ballCount = Integer.parseInt(count); 373 374 if(orgBallCount == -1) // Store original ball count so it can be set back when unloading 373 374 if(orgBallCount == -1) // Store original ball count so it can be set back when unloading 375 375 orgBallCount = ballCount; 376 376 377 377 if(ballCount == 0) { 378 378 m_botAction.sendPublicMessage("No balls are set in this arena. Module disabled."); … … 393 393 if(ballCount == 0) 394 394 return; 395 395 396 396 Ball ball = null; 397 397 if(balls.get(event.getBallID()) == null) { … … 402 402 ball.setCarrier(event.getCarrier()); 403 403 } 404 404 405 405 if(running) { 406 406 407 407 if(ball.getCarrier() != -1) { // A player has picked up the ball or is carrying it 408 408 ballMode = 1; 409 409 410 410 if((System.currentTimeMillis() - ball.getPreviousCarrierTime()) > dodgetime) { 411 411 // the player is out 412 412 ballMode = 0; 413 413 414 414 Player p = m_botAction.getPlayer(event.getCarrier()); 415 415 Player prev = m_botAction.getPlayer(ball.getPreviousCarrier()); 416 if( p == null || prev == null ) 417 return; 416 418 if(p.getFrequency() != prev.getFrequency() && p.getPlayerID() != m_botAction.getPlayerID(m_botAction.getBotName())) { 417 419 m_botAction.specWithoutLock(ball.getCarrier()); … … 420 422 players.put(ball.getPreviousCarrier(), (players.get(ball.getPreviousCarrier()) + 1)); 421 423 } 422 424 423 425 checkWinner(); 424 426 } else { … … 430 432 ball.setPreviousCarrierTime( System.currentTimeMillis() ); 431 433 } 432 434 433 435 } 434 436 } 435 437 } 436 438 437 439 public void handleEvent(PlayerLeft event) { 438 440 if(running) 439 441 players.remove(event.getPlayerID()); 440 442 } 441 443 442 444 public void handleEvent(FrequencyShipChange event) { 443 445 // if game has started and the new ship type is 0 (Spectator) … … 447 449 } 448 450 } 449 451 450 452 private void checkWinner() { 451 453 m_botAction.scheduleTask(checkWinner, 1000); 452 454 } 453 455 454 456 private void clear() { 455 457 players.clear(); … … 457 459 ballMode = 0; 458 460 } 459 461 460 462 private void moveBall(int ballID, int x, int y) { 461 463 m_botAction.stopReliablePositionUpdating(); // makes the bot stop following people … … 469 471 } 470 472 471 473 472 474 /** 473 475 * Essentially a TimerTask that stores info about each player. … … 485 487 m_botAction.sendArenaMessage(winner.getPlayerName()+" WINS! ("+players.get(winner.getPlayerID())+" kills)", Tools.Sound.HALLELUJAH); 486 488 clear(); 487 489 488 490 } else if(players.size() == 0) { 489 491 running = false; … … 493 495 } 494 496 } 495 497 496 498 private class Ball { 497 499 private short id; … … 499 501 private short previousCarrier; 500 502 private long previousCarrierTime; 501 503 502 504 public Ball(short id, short carrier, short previousCarrier, long previousCarrierTime) { 503 505 this.id = id; … … 511 513 public short getPreviousCarrier() { return previousCarrier; } 512 514 public long getPreviousCarrierTime() { return previousCarrierTime; } 513 515 514 516 public void setCarrier(short carrier) { this.carrier = carrier; } 515 517 public void setPreviousCarrier(short previousCarrier) { this.previousCarrier = previousCarrier; } 516 518 public void setPreviousCarrierTime(long previousCarrierTime) { this.previousCarrierTime = previousCarrierTime; } 517 518 } 519 519 520 } 521 520 522 521 523 }
