/**
   * sendAll: send all of the information to the print writer
   *
   * @return: the response
   */
  protected String sendAll() throws Exception {

    assert _out != null;
    String output = _server.getID() + " " + _server.getPrio() + " " + _server.getMaxClientNum();
    Map<String, ClientNode> mp = _server.getClients();
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry pair = (Map.Entry) it.next();
      String name = (String) pair.getKey();
      ClientNode node = (ClientNode) pair.getValue();
      output += " " + name + " " + node.getPassword();
    }
    output = ProtocolConstants.PACK_STR_ID_HEAD + " " + output;
    String reply = NetComm.sendAndRecv(output, _out, _in);
    return reply;
  }
Exemple #2
0
  public static void runStaticInitializers() {
    assert !initializationComplete;
    for (TypeDef typeDef : allTypes.values()) {
      typeDef.init();

      if (typeDef.desc.equals(RawType.coreUnit)) {
        // TODO: Fragile...
        objUnit = typeDef.staticFields[0];
      }
      if (typeDef.desc.equals(RawType.coreRelation)) {
        // TODO: Fragile...
        objLT = typeDef.staticFields[0];
        objGT = typeDef.staticFields[1];
        objEQ = typeDef.staticFields[2];
      }
    }
    initializationComplete = true;
  }
Exemple #3
0
  static {
    allTypes = new HashMap<RawType, TypeDef>();
    allMethods = new HashMap<RawMethod, MethodDef>();
    nativeTypes = new HashSet<NativeTypeDef>();

    // We need to ensure that NativeInt, NativeBool etc. are loaded, so that their TYPE
    // objects will be created and eventually added to nativeTypes. We might as well just
    // add them manually, as it guarantees initialization and redundant add's won't hurt.
    NativeTypeDef[] nativeTypesArr = {
      NativeObject.TOP_TYPE,
      NativeBool.TYPE,
      NativeInt.TYPE,
      NativeDouble.TYPE,
      NativeChar.TYPE,
      NativeArray.TYPE,
      NativeConsole.TYPE
    };
    for (NativeTypeDef nativeType : nativeTypesArr) {
      nativeTypes.add(nativeType);
      allTypes.put(nativeType.desc, nativeType);
    }
  }
Exemple #4
0
 public static void linkAll() {
   for (TypeDef typeDef : allTypes.values()) typeDef.link();
 }
Exemple #5
0
 public static void newMethod(MethodDef method) {
   allMethods.put(method.desc, method);
 }
Exemple #6
0
 public static MethodDef resolveMethod(RawMethod desc) {
   MethodDef result = allMethods.get(desc);
   if (result == null) throw new RuntimeException("failed to resolve " + desc);
   return result;
 }
Exemple #7
0
 public static void newType(TypeDef type) {
   // Register the new type.
   allTypes.put(type.desc, type);
   if (type instanceof NativeTypeDef) nativeTypes.add((NativeTypeDef) type);
 }
Exemple #8
0
 public static TypeDef resolveType(RawType desc) {
   TypeDef result = allTypes.get(desc);
   if (result == null) throw new NoSuchElementException("Failed to resolve " + desc);
   return result;
 }
Exemple #9
0
 public static boolean hasType(RawType desc) {
   return allTypes.containsKey(desc);
 }
  @Override
  public void DoTurn(PlanetWars pw) {

    write("==TURN " + ++turnNum);

    if (mirrorMap == null) {
      write("==Generating mirrorMap");
      mirrorMap = new HashMap<Integer, Planet>();

      double mapCenterX = 0;
      double mapCenterY = 0;

      for (Planet p : pw.Planets()) {
        mapCenterX += p.X();
        mapCenterY += p.Y();
      }

      mapCenterX = mapCenterX / pw.Planets().size();
      mapCenterY = mapCenterY / pw.Planets().size();

      // write("Map Center: " + "(" + mapCenterX + ", " + mapCenterY + ")");

      HashMap<Point, Planet> mirroredLocation = new HashMap<Point, Planet>();

      for (Planet p : pw.Planets()) {
        if (p.X() == mapCenterX && p.Y() == mapCenterY) {
          write("-- Detected Center Planet --");
          mirrorMap.put(p.PlanetID(), p);
          continue;
        }

        Point mirroredPoint = new Point(p.X(), p.Y());

        // write("Looking for point of: " + mirroredPoint + " for " + p.PlanetID());
        if (mirroredLocation.containsKey(mirroredPoint)) {
          Planet p1 = mirroredLocation.get(mirroredPoint);

          write("-- Detected Mirror Planet: " + p.PlanetID() + " = " + p1.PlanetID() + " --");

          mirrorMap.put(p1.PlanetID(), p);
          mirrorMap.put(p.PlanetID(), p1);
        } else {
          mirroredPoint.x = mapCenterX + mapCenterX - p.X();
          mirroredPoint.y = mapCenterY + mapCenterY - p.Y();

          mirroredLocation.put(mirroredPoint, p);

          // write("Set mirror point of: " + mirroredPoint + " for " + p.PlanetID());
        }
      }
      write("--Generated mirrorMap");
    }

    if (pw.EnemyFleets().size() > 0) {
      write("Checking Enemy Fleets");

      List<Fleet> fleets = pw.EnemyFleets();
      for (Fleet o : mirroredFleets) {
        fleets.remove(o);
      }

      write("Fleet count: " + fleets.size() + " -- mirrored: " + mirroredFleets.size());

      for (Fleet f : fleets) {
        boolean handled = false;

        Planet src = mirrorMap.get(f.SourcePlanet());
        Planet dest = mirrorMap.get(f.DestinationPlanet());
        // write("lamo: " + f.SourcePlanet() + " " + f.DestinationPlanet());
        // write("lame: " + src.PlanetID() + " " + dest.PlanetID());
        // write("owner: " + src.Owner() + " " + dest.Owner());
        // Don't trade fleets between mirrored planets:
        if (src.PlanetID() != f.DestinationPlanet()) {

          write("Ships: " + src.PlanetID() + " -- " + src.NumShips() + " - " + f.NumShips());
          if (src.NumShips() > f.NumShips() && src.Owner() == 1) {
            pw.IssueOrder(src, dest, f.NumShips());
            // src.NumShips(src.NumShips() - f.NumShips());
            handled = true;
          }

        } else {
          handled = true;
        }
        if (handled) mirroredFleets.add(f);
        //                }
        //                else {
        //                    write("Already handled fleet");
        //                }
      }
    } else {
      write("Waiting");
      //            write("Attacking Something");
      //            if (distances == null) {
      //                distances = new PlanetDistances(pw);
      //            }
      //
      //            //Attack something
      //            for (Planet p : pw.MyPlanets()) {
      //                Planet attack = Find.NearestPlanetNotOwnedCanTake(pw.Planets(), distances,
      // p.PlanetID(), p.NumShips() - 10);
      //                if (attack != null && p.NumShips() > 20) {
      //                    pw.IssueOrder(p.PlanetID(), attack.PlanetID(), p.NumShips() - 15);
      //                }
      //            }
    }
  }