Files
terrain-utilities/src/main/java/pl/wat/ms4ds/terenfunkcje/Square.java

283 lines
7.7 KiB
Java

package pl.wat.ms4ds.terenfunkcje;
import pl.wat.ms4ds.common.EGeoDirection;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Square {
/**
* The height above the level of the sea. Unit of measure decimeter [m].
*/
float elevation;
/**
* Terrain type. <p></p>Possible values:
* 0 - BARE_GROUND
* 1 - GRASS
* 2 - SWAMP
* 3 - WATER
* 4 - SCRUB, BUSHES
* 5 - BUILDINGS
* 6 - FOREST
*/
short terrainType;
/**
* Type of watercourse (water obstacle) in a given direction. Each index corresponds to a given direction.
* <p></p>Possible values: 0 - no watercourse, 1 - drain, ditch, 2 - canal, stream, 3 - river
*/
byte[] watercourses;
/**
* Type of road in a given direction. Each index corresponds to a given direction.
* <p></p>Possible values: 0 - no road, 1 - small roads, 2 - minor roads, 3 - major roads
*/
byte[] roads;
////////////////////////////////////////
/// tymczasowo
public float stopienZabudowy;
public float stopienZalesienia;
public float stopienZawodnienia;
public float stopienZabagnienia;
public boolean[] jestDroga;
public boolean[] jestRow;
public boolean[] jestPrzeszkodaWodna;
public int roznicaWzniesien;
public int wysokoscSrednia;
////////////////////////////////////////
public static class RawData {
/**
* The height above the level of the sea. Unit of measure decimeter [dm].
*/
short elevation;
/**
* Terrain type. <p></p>Possible values:
* 0 - BARE_GROUND
* 1 - GRASS
* 2 - SWAMP
* 3 - WATER
* 4 - SCRUB, BUSHES
* 5 - BUILDINGS
* 6 - FOREST
*/
byte terrainType;
/**
* Small road in a given direction. Each bit corresponds to a given direction.
*/
byte smallRoads;
/**
* Minor road in a given direction. Each bit corresponds to a given direction.
*/
byte minorRoads;
/**
* Major road in a given direction. Each bit corresponds to a given direction.
*/
byte majorRoads;
/**
* The existence of a drain in a given direction. Each bit corresponds to a given direction.
*/
byte drains;
/**
* The existence of a stream in a given direction. Each bit corresponds to a given direction.
*/
byte streams;
/**
* The existence of a river in a given direction. Each bit corresponds to a given direction.
*/
byte rivers;
public void reset() {
elevation = 0;
terrainType = 0;
smallRoads = 0;
minorRoads = 0;
majorRoads = 0;
drains = 0;
streams = 0;
rivers = 0;
}
public void save(ObjectOutputStream out) throws IOException {
out.writeShort(elevation);
out.writeByte(terrainType);
out.writeByte(smallRoads);
out.writeByte(minorRoads);
out.writeByte(majorRoads);
out.writeByte(drains);
out.writeByte(streams);
out.writeByte(rivers);
}
public void load(ObjectInputStream in) throws IOException {
elevation = in.readShort();
terrainType = in.readByte();
smallRoads = in.readByte();
minorRoads = in.readByte();
majorRoads = in.readByte();
drains = in.readByte();
streams = in.readByte();
rivers = in.readByte();
}
public RawData(short elevation, byte terrainType) {
this.elevation = elevation;
this.terrainType = terrainType;
}
public RawData() {
}
public RawData(Square kw) {
terrainType = (byte) kw.terrainType;
// Konwersja na decymetry.
elevation = (short) (kw.elevation * 10);
byte bit = 1;
for (int i = 0; i < kw.watercourses.length; i++) {
switch (kw.watercourses[i]) {
case 1:
drains |= bit;
break;
case 2:
streams |= bit;
break;
case 3:
rivers |= bit;
break;
default:
break;
}
bit <<= 1;
}
bit = 1;
for (int i = 0; i < kw.roads.length; i++) {
switch (kw.roads[i]) {
case 1:
smallRoads |= bit;
break;
case 2:
minorRoads |= bit;
break;
case 3:
majorRoads |= bit;
break;
default:
break;
}
bit <<= 1;
}
}
}
public static final Square EMPTY = new Square(-1, -1);
public Square() {
this(-2, -2);
}
public Square(int x, int y) {
this.x = x;
this.y = y;
roads = new byte[8];
watercourses = new byte[8];
// Brak danych o wysokości.
elevation = -1000;
}
public Square(int x, int y, RawData rawData) {
this(x, y);
elevation = rawData.elevation / 10.f;
terrainType = rawData.terrainType;
int bit = 1;
for (int i = 0; i < 8; i++) {
int b1 = ((rawData.majorRoads & bit) > 0) ? 3 : 0;
int b2 = ((rawData.minorRoads & bit) > 0) ? 2 : 0;
int b3 = ((rawData.smallRoads & bit) > 0) ? 1 : 0;
roads[i] = (byte) (b1 + b2 + b3);
b1 = ((rawData.rivers & bit) > 0) ? 3 : 0;
b2 = ((rawData.streams & bit) > 0) ? 2 : 0;
b3 = ((rawData.drains & bit) > 0) ? 1 : 0;
watercourses[i] = (byte) (b1 + b2 + b3);
bit <<= 1;
}
}
final int x;
final int y;
public double sumaWysokosci;
public int count = 1;
public double ell;
public double nll;
public double eur;
public double nur;
@Override
public final boolean equals(Object o) {
if (!(o instanceof Square SQUARE)) return false;
return x == SQUARE.x && y == SQUARE.y;
}
@Override
public int hashCode() {
int result = 7;
result = 31 * result + x;
result = 31 * result + y;
return result;
}
public String toString() {
StringBuilder linia = new StringBuilder(100);
linia.append("[");
char c = switch (terrainType) {
case 1 -> 'G';
case 2 -> 'S';
case 3 -> 'W';
case 4 -> 'R';
case 5 -> 'B';
case 6 -> 'F';
default -> ' ';
};
linia.append(c);
linia.append(' ');
String s = String.format("%5.1f", elevation);
linia.append(s);
linia.append(' ');
for (byte road : roads) {
c = switch (road) {
case 1 -> '1';
case 2 -> '2';
case 3 -> '3';
default -> '0';
};
linia.append(c);
}
linia.append(' ');
for (byte watercours : watercourses) {
c = switch (watercours) {
case 1 -> '1';
case 2 -> '2';
case 3 -> '3';
default -> '0';
};
linia.append(c);
}
linia.append(']');
return linia.toString();
}
}