Wersja działająca.Raczej

This commit is contained in:
2026-01-22 11:28:46 +01:00
parent 31f424e3e9
commit fdf9110c45
12 changed files with 1010 additions and 489 deletions

View File

@@ -1,6 +1,7 @@
package pl.wat.ms4ds.terrain;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
@@ -8,9 +9,9 @@ import java.io.ObjectOutputStream;
public class Square {
/**
* The height above the level of the sea. Unit of measure decimeter [m].
* The height above the level of the sea. Unit of measure meter [m].
*/
float elevation;
public float elevation;
/**
* Terrain type. <p></p>Possible values:
@@ -22,21 +23,21 @@ public class Square {
* 5 - BUILDINGS
* 6 - FOREST
*/
short terrainType;
public 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;
public final 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;
public final byte[] roads;
////////////////////////////////////////
/// /////////////////////////////////////
/// tymczasowo
public float stopienZabudowy;
public float stopienZalesienia;
@@ -47,11 +48,12 @@ public class Square {
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].
* The height above the level of the sea. Unit of measure [0.25m].
*/
short elevation;
/**
@@ -107,7 +109,7 @@ public class Square {
rivers = 0;
}
public void save(ObjectOutputStream out) throws IOException {
public void write(ObjectOutputStream out) throws IOException {
out.writeShort(elevation);
out.writeByte(terrainType);
out.writeByte(smallRoads);
@@ -118,7 +120,7 @@ public class Square {
out.writeByte(rivers);
}
public void load(ObjectInputStream in) throws IOException {
public void read(ObjectInputStream in) throws IOException {
elevation = in.readShort();
terrainType = in.readByte();
smallRoads = in.readByte();
@@ -140,7 +142,7 @@ public class Square {
public RawData(Square kw) {
terrainType = (byte) kw.terrainType;
// Konwersja na decymetry.
elevation = (short) (kw.elevation * 10);
elevation = (short) (kw.elevation * 4);
byte bit = 1;
for (int i = 0; i < kw.watercourses.length; i++) {
switch (kw.watercourses[i]) {
@@ -156,10 +158,6 @@ public class Square {
default:
break;
}
bit <<= 1;
}
bit = 1;
for (int i = 0; i < kw.roads.length; i++) {
switch (kw.roads[i]) {
case 1:
smallRoads |= bit;
@@ -176,6 +174,43 @@ public class Square {
bit <<= 1;
}
}
public void read(Square kw) {
terrainType = (byte) kw.terrainType;
// Konwersja na decymetry.
elevation = (short) (kw.elevation * 4);
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;
}
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);
@@ -189,13 +224,122 @@ public class Square {
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;
this.x = x;
this.y = y;
roads = new byte[8];
watercourses = new byte[8];
// Konwersja na metry a[0.25m] -> b[m]
elevation = (float) (rawData.elevation) / 4;
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;
}
}
public int writeToBuffer(byte[] buffer, int offset) {
// Konwersja [m] -> [0.25m].
int elev = (short) (elevation * 4);
byte bit = 1;
byte drains = 0;
byte streams = 0;
byte rivers = 0;
byte smallRoads = 0;
byte minorRoads = 0;
byte majorRoads = 0;
for (int i = 0; i < watercourses.length; i++) {
switch (watercourses[i]) {
case 1:
drains |= bit;
break;
case 2:
streams |= bit;
break;
case 3:
rivers |= bit;
break;
default:
break;
}
switch (roads[i]) {
case 1:
smallRoads |= bit;
break;
case 2:
minorRoads |= bit;
break;
case 3:
majorRoads |= bit;
break;
default:
break;
}
bit <<= 1;
}
byte b1 = (byte) (elev & 0xFF);
elev >>= 8;
byte b0 = (byte) (elev & 0xFF);
if (b0 == -1 && b1 == -4) {
System.out.println("a");
}
buffer[offset + 1] = b1;
buffer[offset] = b0;
buffer[offset + 2] = (byte) terrainType;
buffer[offset + 3] = smallRoads;
buffer[offset + 4] = minorRoads;
buffer[offset + 5] = majorRoads;
buffer[offset + 6] = drains;
buffer[offset + 7] = streams;
buffer[offset + 8] = rivers;
return offset + 9;
}
public int readFromBuffer(byte[] buffer, int offset) {
int elev = buffer[offset] & 0xFF;
elev = (elev << 8) + (buffer[offset + 1] & 0xFF);
short v = (short) elev;
// Konwersja na metry a[0.25m] -> b[m]
elevation = (float) (v) / 4;
if (elevation > 2660) {
System.out.println("h=" + elevation);
}
terrainType = buffer[offset + 2];
byte smallRoads = buffer[offset + 3];
byte minorRoads = buffer[offset + 4];
byte majorRoads = buffer[offset + 5];
byte drains = buffer[offset + 6];
byte streams = buffer[offset + 7];
byte rivers = buffer[offset + 8];
int bit = 1;
// 8 kierunków geograficznych (0 - NORTH, 1 - NORTH_EAST, ...)
for (int i = 0; i < 8; i++) {
int b1 = ((majorRoads & bit) > 0) ? 3 : 0;
int b2 = ((minorRoads & bit) > 0) ? 2 : 0;
int b3 = ((smallRoads & bit) > 0) ? 1 : 0;
roads[i] = (byte) (b1 + b2 + b3);
b1 = ((rivers & bit) > 0) ? 3 : 0;
b2 = ((streams & bit) > 0) ? 2 : 0;
b3 = ((drains & bit) > 0) ? 1 : 0;
watercourses[i] = (byte) (b1 + b2 + b3);
bit <<= 1;
}
return offset + 9;
}
void read(RawData rawData) {
// Konwersja na metry a[0.25m] -> b[m]
elevation = (float) (rawData.elevation) / 4;
terrainType = rawData.terrainType;
int bit = 1;
for (int i = 0; i < 8; i++) {
@@ -213,12 +357,6 @@ public class Square {
public final int x;
public final int y;
public double sumaWysokosci;
public int count = 1;
public double ell;
public double nll;
public double eur;
public double nur;
@Override
@@ -250,7 +388,7 @@ public class Square {
};
linia.append(c);
linia.append(' ');
String s = String.format("%5.1f", elevation);
String s = String.format("%7.2f", elevation);
linia.append(s);
linia.append(' ');
for (byte road : roads) {