Package Hexel.generation.terrainGenerator.originalPlateGenerator.plate

Source Code of Hexel.generation.terrainGenerator.originalPlateGenerator.plate.LocalPlateGrids

package Hexel.generation.terrainGenerator.originalPlateGenerator.plate;

import java.util.concurrent.ConcurrentHashMap;

import Hexel.math.Vector2i;

public class LocalPlateGrids {
  private ConcurrentHashMap<Vector2i, int[][]> inMemoryLPGs = new ConcurrentHashMap<Vector2i, int[][]>();
  private Object[] locks = new Object[100];

  public LocalPlateGrids() {
    for (int i = 0; i < this.locks.length; i++) {
      this.locks[i] = new Object();
    }
  }

  public int[][] get(int x, int y) {
    return this.get(new Vector2i(x, y));
  }

  public int[][] get(Vector2i ppos) {
    if (hasLPGInMemory(ppos)) {
      return this.inMemoryLPGs.get(ppos);
    }
    maybeGenLPG(ppos);
    return this.inMemoryLPGs.get(ppos);
  }

  private void maybeGenLPG(Vector2i ppos) {
    int hash = Math.abs(ppos.hashCode() % this.locks.length);
    synchronized (this.locks[hash]) {
      if (hasLPGInMemory(ppos))
        return;

      int[][] lpg = LocalPlateGridGenerator.gen(ppos.x, ppos.y);
      this.inMemoryLPGs.put(ppos, lpg);
    }
  }

  private boolean hasLPGInMemory(Vector2i ppos) {
    return this.inMemoryLPGs.containsKey(ppos);
  }

  public void unloadLPG(Vector2i ppos) {
    this.inMemoryLPGs.remove(ppos);
  }

  public int getValue(int x, int y) {
    int px = (int) Math.floor(x * 1.0 / PlateChunk.WIDTH);
    int py = (int) Math.floor(y * 1.0 / PlateChunk.HEIGHT);

    x -= px * PlateChunk.WIDTH;
    y -= py * PlateChunk.HEIGHT;

    return this.get(px, py)[x][y];
  }

}
TOP

Related Classes of Hexel.generation.terrainGenerator.originalPlateGenerator.plate.LocalPlateGrids

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.