private void getVerticalWallSegment(
      Player player, Location loc, Map<String, SimpleBlock> simpleBlocks) throws CivException {
    Location tmp = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ());
    for (int i = 0; i < Wall.HEIGHT; i++) {
      SimpleBlock sb;
      if (i == 0) {
        sb = new SimpleBlock(CivData.STONE_BRICK, 0x1);
      } else {
        sb = new SimpleBlock(CivData.STONE_BRICK, 0);
      }
      sb.worldname = tmp.getWorld().getName();
      sb.x = tmp.getBlockX();
      sb.y = tmp.getBlockY();
      sb.z = tmp.getBlockZ();

      validateBlockLocation(player, tmp);
      simpleBlocks.put(sb.worldname + "," + sb.x + "," + sb.y + "," + sb.z, sb);

      tmp.add(0, 1.0, 0);
    }
  }
  private int buildWallSegment(
      Player player,
      BlockCoord first,
      BlockCoord second,
      int blockCount,
      HashMap<String, SimpleBlock> simpleBlocks,
      int verticalSegments)
      throws CivException {
    Location locFirst = first.getLocation();
    Location locSecond = second.getLocation();

    Vector dir =
        new Vector(
            locFirst.getX() - locSecond.getX(),
            locFirst.getY() - locSecond.getY(),
            locFirst.getZ() - locSecond.getZ());
    dir.normalize();
    dir.multiply(0.5);
    HashMap<String, SimpleBlock> thisWallBlocks = new HashMap<String, SimpleBlock>();

    this.getTown().lastBuildableBuilt = null;

    getVerticalWallSegment(player, locSecond, thisWallBlocks);
    simpleBlocks.putAll(thisWallBlocks);
    verticalSegments++;

    double distance = locSecond.distance(locFirst);
    BlockCoord lastBlockCoord = new BlockCoord(locSecond);
    BlockCoord currentBlockCoord = new BlockCoord(locSecond);
    while (locSecond.distance(locFirst) > 1.0) {
      locSecond.add(dir);
      ChunkCoord coord = new ChunkCoord(locSecond);
      CivGlobal.addWallChunk(this, coord);

      currentBlockCoord.setFromLocation(locSecond);
      if (lastBlockCoord.equals(currentBlockCoord)) {
        continue;
      } else {
        lastBlockCoord.setFromLocation(locSecond);
      }

      blockCount++;
      if (blockCount > Wall.RECURSION_LIMIT) {
        throw new CivException(
            "ERROR: Building wall blocks exceeded recusion limit! Halted to keep server alive.");
      }

      getVerticalWallSegment(player, locSecond, thisWallBlocks);
      simpleBlocks.putAll(thisWallBlocks);
      verticalSegments++;

      // Distance should always be going down, as a failsave
      // check that it is. Abort if our distance goes up.
      double tmpDist = locSecond.distance(locFirst);
      if (tmpDist > distance) {
        break;
      }
    }

    /* build the last wall segment. */
    if (!wallBlocks.containsKey(new BlockCoord(locFirst))) {
      try {
        getVerticalWallSegment(player, locFirst, thisWallBlocks);
        simpleBlocks.putAll(thisWallBlocks);
        verticalSegments++;
      } catch (CivException e) {
        CivLog.warning("Couldn't build the last wall segment, oh well.");
      }
    }

    for (SimpleBlock sb : simpleBlocks.values()) {
      BlockCoord bcoord = new BlockCoord(sb);
      int old_id = ItemManager.getId(bcoord.getBlock());
      int old_data = ItemManager.getData(bcoord.getBlock());
      if (!wallBlocks.containsKey(bcoord)) {
        try {
          WallBlock wb = new WallBlock(bcoord, this, old_id, old_data, sb.getType(), sb.getData());

          wallBlocks.put(bcoord, wb);
          this.addStructureBlock(bcoord, true);
          wb.save();
        } catch (SQLException e) {
          e.printStackTrace();
        }
      }
    }
    return verticalSegments;
  }
  @Override
  public void onMarkerPlacement(Player player, Location next, ArrayList<Location> locs)
      throws CivException {
    BlockCoord first = new BlockCoord(next);
    BlockCoord second = null;

    CultureChunk cc = CivGlobal.getCultureChunk(next);
    if (cc == null || cc.getTown().getCiv() != this.getTown().getCiv()) {
      throw new CivException("Cannot build here, you need to build inside your culture.");
    }

    if (locs.size() <= 1) {
      CivMessage.send(
          player,
          CivColor.LightGray + "First location placed, place another to start building a wall.");
      return;
    }

    // Validate our locations
    if (locs.get(0).distance(locs.get(1)) > Wall.MAX_SEGMENT) {
      throw new CivException(
          "Can only build a wall in "
              + Wall.MAX_SEGMENT
              + " block segments, pick a closer location");
    }

    second = new BlockCoord(locs.get(0));
    locs.clear();
    MarkerPlacementManager.removeFromPlacementMode(player, false);

    Location secondLoc = second.getLocation();
    // Setting to a new block coord so we can increment in buildWallSegment without changing the
    // corner.
    this.setCorner(new BlockCoord(secondLoc));
    this.setComplete(true);
    this.save();

    // We should now be able to draw a line between these two block points.
    HashMap<String, SimpleBlock> simpleBlocks = new HashMap<String, SimpleBlock>();
    int verticalSegments = this.buildWallSegment(player, first, second, 0, simpleBlocks, 0);

    // Pay the piper
    double cost = verticalSegments * COST_PER_SEGMENT;
    if (!this.getTown().getTreasury().hasEnough(cost)) {

      for (WallBlock wb : this.wallBlocks.values()) {
        try {
          wb.delete();
        } catch (SQLException e) {
          e.printStackTrace();
        }
      }
      this.wallBlocks.clear();

      throw new CivException(
          "Cannot build, not enough coins to pay "
              + cost
              + " coins for wall of length "
              + verticalSegments
              + " blocks.");
    }

    this.getTown().getTreasury().withdraw(cost);

    CivMessage.sendTown(
        this.getTown(),
        CivColor.Yellow + "Paid " + cost + " coins for " + verticalSegments + " wall segments.");

    // build the blocks
    for (SimpleBlock sb : simpleBlocks.values()) {
      BlockCoord bcoord = new BlockCoord(sb);
      ItemManager.setTypeId(bcoord.getBlock(), sb.getType());
      ItemManager.setData(bcoord.getBlock(), sb.getData());
    }

    // Add wall to town and global tables
    this.getTown().addStructure(this);
    CivGlobal.addStructure(this);
    this.getTown().lastBuildableBuilt = this;
  }