Beispiel #1
0
  private void startExecution(int numberOfDragons, int numberOfPlayers) {

    System.out.println("Units will now start to connect!");

    this.generateDragons(numberOfDragons);
    BattleField.generatePlayeres(numberOfPlayers, "localhost", port + 100, url, port);

    // Updates to game state
    final BattleField bf = this;
    new Thread(
            new Runnable() {
              public void run() {
                SynchronizedClientSocket clientSocket;
                while (true) {

                  for (Map.Entry<InetSocketAddress, Unit> entry : units.entrySet()) {
                    if (!entry
                        .getValue()
                        .getBattlefieldAddress()
                        .equals(new InetSocketAddress(url, port))) continue;
                    Message message = new Message();
                    message.put("request", MessageRequest.gameState);
                    message.put("gamestate", map);
                    // Puts position of the unit we are sending to in the map we are sending
                    Unit u = entry.getValue();
                    message.put("unit", u);

                    clientSocket = new SynchronizedClientSocket(message, entry.getKey(), bf);
                    clientSocket.sendMessage();
                  }

                  try {
                    Thread.sleep(100L); // Time between gameState update is sent to units
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }
                }
              }
            })
        .start();

    // Checks  game state
    new Thread(
            new Runnable() {
              public void run() {
                try {
                  Thread.sleep(2000L);
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }

                while (true) {
                  int dragon = 0;
                  int player = 0;

                  // System.out.println(units);
                  for (Unit entry : units.values()) {
                    if (entry instanceof Dragon) dragon++;
                    if (entry instanceof Player) player++;
                  }
                  System.out.println("Units: " + dragon + " Dragons and " + player + " Players");

                  if (dragon == 0 || player == 0) {
                    System.out.println("GAME ENDED");
                    logger.readOrderedLog();
                    logger.writeOrderedLogToTextfile("_ordered");
                    logger.cleanupStructures();
                    System.exit(1);
                  }

                  try {
                    Thread.sleep(1000L);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }
                }
              }
            })
        .start();
  }
Beispiel #2
0
  public static void main(String[] args) {
    battlefield1 = new BattleField(0, "localhost", 50000, false);
    battlefield2 = new BattleField(1, "localhost", 51000, "localhost", 50000, false);
    battlefield3 = new BattleField(2, "localhost", 52000, "localhost", 50000, false);

    /* Spawn a new battlefield viewer */
    new Thread(
            new Runnable() {
              public void run() {
                new BattleFieldViewer(battlefield1);
              }
            })
        .start();
    /* Spawn a new battlefield viewer */
    new Thread(
            new Runnable() {
              public void run() {
                new BattleFieldViewer(battlefield2);
              }
            })
        .start();
    /* Spawn a new battlefield viewer */
    new Thread(
            new Runnable() {
              public void run() {
                new BattleFieldViewer(battlefield3);
              }
            })
        .start();

    try {
      Thread.sleep(2000);
    } catch (InterruptedException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    /* All the dragons connect */
    for (int i = 0; i < DRAGON_COUNT; i++) {
      /* Try picking a random spot */
      int x, y, attempt = 0;
      do {
        x = (int) (Math.random() * BattleField.MAP_WIDTH);
        y = (int) (Math.random() * BattleField.MAP_HEIGHT);
        attempt++;
      } while (battlefield1.getUnit(x, y) != null && attempt < 10);

      // If we didn't find an empty spot, we won't add a new dragon
      if (battlefield1.getUnit(x, y) != null) break;

      final int finalX = x;
      final int finalY = y;

      /* Create the new dragon in a separate
       * thread, making sure it does not
       * block the system.
       */
      final int temp = i;
      new Thread(
              new Runnable() {
                public void run() {
                  new Dragon(
                      finalX,
                      finalY,
                      "localhost",
                      50050 + temp,
                      "localhost",
                      50000 + (temp % 3 * 1000));
                }
              })
          .start();
    }

    /* Initialize a random number of players (between [MIN_PLAYER_COUNT..MAX_PLAYER_COUNT] */
    playerCount = (int) ((MAX_PLAYER_COUNT - MIN_PLAYER_COUNT) * Math.random() + MIN_PLAYER_COUNT);
    for (int i = 0; i < playerCount; i++) {
      /*
      try {
      	Thread.sleep(10);
      } catch (InterruptedException e) {
      	// TODO Auto-generated catch block
      	e.printStackTrace();
      }*/
      /* Once again, pick a random spot */
      int x, y, attempt = 0;
      do {
        x = (int) (Math.random() * BattleField.MAP_WIDTH);
        y = (int) (Math.random() * BattleField.MAP_HEIGHT);
        attempt++;
      } while (battlefield1.getUnit(x, y) != null && attempt < 10);

      // If we didn't find an empty spot, we won't add a new player
      if (battlefield1.getUnit(x, y) != null) break;

      final int finalX = x;
      final int finalY = y;
      // System.out.println("CORE:" + finalX + " " +  finalY);

      /* Create the new player in a separate
       * thread, making sure it does not
       * block the system.
       */
      final int temp = i;
      new Thread(
              new Runnable() {
                public void run() {
                  // TODO Ports have to be different for each player even when only connecting to
                  // different battlefields
                  // Now I'm just worried about all of them having different ports
                  new Player(
                      finalX,
                      finalY,
                      "localhost",
                      50100 + temp,
                      "localhost",
                      50000 + (temp % 3 * 1000));
                }
              })
          .start();
    }

    /* Add a random player every (5 seconds x GAME_SPEED) so long as the
     * maximum number of players to enter the battlefield has not been exceeded.
     */
    while (GameState.getRunningState()) {
      try {
        Thread.sleep((int) (5000 * GameState.GAME_SPEED));
        // Connect a player to the game if the game still has room for a new player
        if (playerCount >= MAX_PLAYER_COUNT) continue;

        // Once again, pick a random spot
        int x, y, attempts = 0;
        do {
          // If finding an empty spot just keeps failing then we stop adding the new player
          x = (int) (Math.random() * BattleField.MAP_WIDTH);
          y = (int) (Math.random() * BattleField.MAP_HEIGHT);
          attempts++;
        } while (battlefield1.getUnit(x, y) != null && attempts < 10);

        // If we didn't find an empty spot, we won't add the new player
        if (battlefield1.getUnit(x, y) != null) continue;

        final int finalX = x;
        final int finalY = y;

        if (battlefield1.getUnit(x, y) == null) {
          // new Player(finalX, finalY, battlefield1.getNewUnitID(),"localhost", 50000 +playerCount,
          // "localhost", 50000);

          new Thread(
                  new Runnable() {
                    public void run() {
                      // new Player(finalX, finalY, battlefield1.getNewUnitID(),"localhost", 50000
                      // +playerCount, "localhost", 50000 + playerCount%3);
                    }
                  })
              .start();

          playerCount++;
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    /* Make sure both the battlefield and
     * the socketmonitor close down.
     */

    battlefield1.shutdown();
    battlefield2.shutdown();
    battlefield3.shutdown();

    System.exit(0); // Stop all running processes
  }
Beispiel #3
0
  public static void main(String[] args) {

    String usage =
        "Usage: BattleField <id> <nDragons> <nPlayers> <hostname> <port> [<otherBFHostname> <otherBFPort> [-r]]";

    if (args.length != 5 && args.length != 7 && args.length != 8) {
      System.out.println(usage);
      System.exit(1);
    }

    BattleField bf = null;

    if (args.length == 8) {
      if (args[7].equals("-r")) {
        try {
          System.out.println("Launching BattleField in RESTART mode.");
          bf =
              new BattleField(
                  Integer.parseInt(args[0]),
                  args[3],
                  Integer.parseInt(args[4]),
                  args[5],
                  Integer.parseInt(args[6]),
                  true);
        } catch (Exception e) {
          e.printStackTrace();
          System.out.println(usage);
          System.exit(1);
        }
      } else {
        System.out.println(usage);
        System.exit(1);
      }
    } else if (args.length == 7) {
      try {
        System.out.println("Launching BattleField in NORMAL mode.");
        bf =
            new BattleField(
                Integer.parseInt(args[0]),
                args[3],
                Integer.parseInt(args[4]),
                args[5],
                Integer.parseInt(args[6]),
                false);
      } catch (Exception e) {
        e.printStackTrace();
        System.out.println(usage);
        System.exit(1);
      }
    } else if (args.length == 5) {
      try {
        System.out.println("Launching BattleField in NORMAL mode.");
        bf = new BattleField(Integer.parseInt(args[0]), args[3], Integer.parseInt(args[4]), false);
      } catch (Exception e) {
        e.printStackTrace();
        System.out.println(usage);
        System.exit(1);
      }
    } else {

      bf = null;
    }

    /*final BattleField otherBf = bf;
    new Thread(new Runnable() {
    	public void run() {
    		new BattleFieldViewer(otherBf);
    	}
    }).start();
    */

    System.out.println("Press ENTER to start generating units");
    try {
      System.in.read();
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    // Number Dragons, Number Players
    bf.startExecution(Integer.parseInt(args[1]), Integer.parseInt(args[2]));
  }