optimized map ionization to avoid extra marks

v5stable
Ondřej Hruška 11 years ago
parent 8872fd3f5f
commit f8f49b81ff
  1. 1
      src/mightypork/gamecore/gui/components/LayoutComponent.java
  2. 10
      src/mightypork/rogue/App.java
  3. 10
      src/mightypork/rogue/screens/ingame/GameGui.java
  4. 2
      src/mightypork/rogue/screens/ingame/ScreenGame.java
  5. 14
      src/mightypork/rogue/screens/ingame/WorldLayer.java
  6. 9
      src/mightypork/rogue/screens/ingame/WorldRenderer.java
  7. 17
      src/mightypork/rogue/world/MapGenerator.java
  8. 12
      src/mightypork/rogue/world/PlayerInfo.java
  9. 55
      src/mightypork/rogue/world/World.java
  10. 44
      src/mightypork/rogue/world/map/Level.java
  11. 5
      src/mightypork/rogue/world/structs/LevelList.java
  12. 36
      src/mightypork/rogue/world/tile/Tile.java
  13. 4
      src/mightypork/rogue/world/tile/models/SimpleTile.java
  14. 5
      src/mightypork/util/constraints/rect/Rect.java
  15. 1
      src/mightypork/util/error/CorruptedDataException.java
  16. 20
      src/mightypork/util/files/ion/Ion.java
  17. 1
      src/mightypork/util/files/ion/IonBundle.java
  18. 2
      src/mightypork/util/logging/Log.java

@ -13,7 +13,6 @@ import mightypork.util.constraints.rect.proxy.RectBound;
import mightypork.util.control.Enableable;
import mightypork.util.control.eventbus.EventBus;
import mightypork.util.control.eventbus.clients.ClientHub;
import mightypork.util.logging.Log;
public abstract class LayoutComponent extends VisualComponent implements Enableable, ClientHub, AppAccess {

@ -22,11 +22,12 @@ import mightypork.rogue.screens.main_menu.ScreenMainMenu;
import mightypork.rogue.screens.test_bouncyboxes.ScreenTestBouncy;
import mightypork.rogue.screens.test_cat_sound.ScreenTestCat;
import mightypork.rogue.screens.test_render.ScreenTestRender;
import mightypork.rogue.world.LocalPlayer;
import mightypork.rogue.world.PlayerInfo;
import mightypork.rogue.world.World;
import mightypork.rogue.world.WorldPos;
import mightypork.rogue.world.item.Item;
import mightypork.rogue.world.map.LevelMap;
import mightypork.rogue.world.map.Level;
import mightypork.rogue.world.structs.LevelList;
import mightypork.rogue.world.tile.Tile;
import mightypork.util.control.eventbus.EventBus;
import mightypork.util.control.eventbus.events.Event;
@ -110,8 +111,9 @@ public final class App extends BaseApp {
protected void preInit()
{
Ion.registerIonizable(Item.ION_MARK, Item.class);
Ion.registerIonizable(LevelMap.ION_MARK, LevelMap.class);
Ion.registerIonizable(LocalPlayer.ION_MARK, LocalPlayer.class);
Ion.registerIonizable(Level.ION_MARK, Level.class);
Ion.registerIonizable(LevelList.ION_MARK, LevelList.class);
Ion.registerIonizable(PlayerInfo.ION_MARK, PlayerInfo.class);
Ion.registerIonizable(Tile.ION_MARK, Tile.class);
Ion.registerIonizable(World.ION_MARK, World.class);
Ion.registerIonizable(WorldPos.ION_MARK, WorldPos.class);

@ -2,23 +2,17 @@ package mightypork.rogue.screens.ingame;
import mightypork.gamecore.gui.AlignX;
import mightypork.gamecore.gui.components.Component;
import mightypork.gamecore.gui.components.layout.HorizontalFixedFlowLayout;
import mightypork.gamecore.gui.components.painters.ImagePainter;
import mightypork.gamecore.gui.components.painters.QuadPainter;
import mightypork.gamecore.gui.screens.Screen;
import mightypork.gamecore.gui.screens.ScreenLayer;
import mightypork.gamecore.render.Render;
import mightypork.rogue.Res;
import mightypork.util.constraints.num.Num;
import mightypork.util.constraints.rect.Rect;
import mightypork.util.math.color.PAL16;
import mightypork.util.math.color.RGB;
public class GameGui extends ScreenLayer {
public GameGui(Screen screen)
{
super(screen);
@ -26,7 +20,7 @@ public class GameGui extends ScreenLayer {
final Num h = root.height();
final Num w = root.width();
final Num minWH = w.min(h).max(700); // avoid too small shrinking
final ImagePainter nav = new ImagePainter(Res.getTxQuad("panel"));
nav.setRect(root.bottomEdge().growUp(minWH.perc(7)));
root.add(nav);
@ -59,11 +53,11 @@ public class GameGui extends ScreenLayer {
return 100;
}
@Override
public void render()
{
super.render();
}

@ -10,7 +10,7 @@ public class ScreenGame extends LayeredScreen {
public ScreenGame(AppAccess app)
{
super(app);
addLayer(new WorldLayer(this)); //TODO with provided world
addLayer(new GameGui(this));
}

@ -1,12 +1,14 @@
package mightypork.rogue.screens.ingame;
import java.io.IOException;
import java.util.Random;
import mightypork.gamecore.gui.screens.Screen;
import mightypork.gamecore.gui.screens.ScreenLayer;
import mightypork.rogue.world.MapGenerator;
import mightypork.rogue.world.World;
import mightypork.util.files.ion.Ion;
public class WorldLayer extends ScreenLayer {
@ -15,14 +17,20 @@ public class WorldLayer extends ScreenLayer {
{
super(screen);
Random rand = new Random();
World w = MapGenerator.createWorld(rand.nextLong());
final Random rand = new Random();
final World w = MapGenerator.createWorld(rand.nextLong());
try {
Ion.toFile("amap.ion", w);
} catch (final IOException e) {
e.printStackTrace();
}
WorldRenderer wr = new WorldRenderer(w);
final WorldRenderer wr = new WorldRenderer(w);
wr.setRect(root);
root.add(wr);
}
@Override
public int getPriority()
{

@ -8,7 +8,6 @@ import mightypork.rogue.world.World;
import mightypork.util.constraints.num.Num;
import mightypork.util.constraints.rect.Rect;
import mightypork.util.control.timing.Updateable;
import mightypork.util.math.color.PAL16;
import mightypork.util.math.color.RGB;
@ -28,9 +27,9 @@ public class WorldRenderer extends InputComponent implements Updateable {
final Num h = height();
final Num w = width();
final Num minWH = w.min(h).max(700);
Num grX = w.perc(30);
Num grY = h.perc(20);
final Num grX = w.perc(30);
final Num grY = h.perc(20);
leftShadow = leftEdge().growRight(grX);
rightShadow = rightEdge().growLeft(grX);
@ -48,7 +47,7 @@ public class WorldRenderer extends InputComponent implements Updateable {
@Override
protected void renderComponent()
{
{
world.render(this, 8, 6, 64);
Render.quadGradH(leftShadow, RGB.BLACK, RGB.NONE);

@ -3,7 +3,7 @@ package mightypork.rogue.world;
import java.util.Random;
import mightypork.rogue.world.map.LevelMap;
import mightypork.rogue.world.map.Level;
import mightypork.rogue.world.tile.Tiles;
@ -17,31 +17,32 @@ public class MapGenerator {
synchronized (rand) {
rand.setSeed(seed);
World w = new World();
final World w = new World();
w.setSeed(seed);
int levels = 4 + rand.nextInt(6);
final int levels = 4 + rand.nextInt(6);
for (int i = 0; i < levels; i++) {
w.addLevel(createLevel(rand.nextLong()));
}
// TODO place on start position
w.setPlayer(new LocalPlayer(10, 10, 0));
return w;
w.setPlayer(new PlayerInfo(10, 10, 0));
return w;
}
}
private static LevelMap createLevel(long seed)
private static Level createLevel(long seed)
{
// TODO
LevelMap lm = new LevelMap(20, 20);
final Level lm = new Level(20, 20);
lm.setSeed(seed);
lm.fill(Tiles.CRYSTAL_FLOOR);
Random rand = new Random();
final Random rand = new Random();
rand.setSeed(seed);
for (int i = 0; i < 150; i++) {

@ -16,7 +16,7 @@ import mightypork.util.files.ion.Ionizable;
*
* @author MightyPork
*/
public class LocalPlayer implements Ionizable, MapObserver {
public class PlayerInfo implements Ionizable, MapObserver {
public static final short ION_MARK = 708;
@ -24,18 +24,18 @@ public class LocalPlayer implements Ionizable, MapObserver {
@IonConstructor
public LocalPlayer()
public PlayerInfo()
{
}
public LocalPlayer(int x, int y, int floor)
public PlayerInfo(int x, int y, int floor)
{
this.position.setTo(x, y, floor);
}
public LocalPlayer(WorldPos pos)
public PlayerInfo(WorldPos pos)
{
this.position = pos;
}
@ -44,7 +44,7 @@ public class LocalPlayer implements Ionizable, MapObserver {
@Override
public void load(InputStream in) throws IOException
{
IonBundle ib = (IonBundle) Ion.readObject(in);
final IonBundle ib = (IonBundle) Ion.readObject(in);
position = ib.get("pos", position);
}
@ -53,7 +53,7 @@ public class LocalPlayer implements Ionizable, MapObserver {
@Override
public void save(OutputStream out) throws IOException
{
IonBundle ib = new IonBundle();
final IonBundle ib = new IonBundle();
ib.put("pos", position);

@ -7,21 +7,18 @@ import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;
import mightypork.gamecore.render.Render;
import mightypork.rogue.world.map.LevelMap;
import mightypork.rogue.world.map.Level;
import mightypork.rogue.world.map.TileRenderContext;
import mightypork.rogue.world.structs.LevelList;
import mightypork.util.constraints.rect.Rect;
import mightypork.util.constraints.rect.RectConst;
import mightypork.util.constraints.rect.proxy.RectBound;
import mightypork.util.constraints.vect.Vect;
import mightypork.util.constraints.vect.VectConst;
import mightypork.util.control.timing.Updateable;
import mightypork.util.error.CorruptedDataException;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.IonBundle;
import mightypork.util.files.ion.Ionizable;
import mightypork.util.math.color.RGB;
public class World implements Ionizable, Updateable {
@ -30,10 +27,11 @@ public class World implements Ionizable, Updateable {
private LevelList levels = new LevelList();
private LocalPlayer player = new LocalPlayer();
private PlayerInfo player = new PlayerInfo();
private transient final Set<MapObserver> observers = new HashSet<>();
/** This seed can be used to re-create identical world. */
private long seed;
@ -41,10 +39,10 @@ public class World implements Ionizable, Updateable {
public void load(InputStream in) throws IOException
{
// world data
IonBundle ib = (IonBundle) Ion.readObject(in);
final IonBundle ib = (IonBundle) Ion.readObject(in);
player = ib.get("player", player);
levels = ib.get("levels", levels);
seed = ib.get("seed", seed);
levels = ib.get("levels", levels);
// levels
Ion.readSequence(in, levels);
@ -56,14 +54,15 @@ public class World implements Ionizable, Updateable {
@Override
public void save(OutputStream out) throws IOException
{
IonBundle ib = new IonBundle();
final IonBundle ib = new IonBundle();
ib.put("player", player);
ib.put("levels", levels);
ib.put("seed", seed);
ib.put("levels", levels);
Ion.writeObject(out, ib);
}
public void setPlayer(LocalPlayer player)
public void setPlayer(PlayerInfo player)
{
removeObserver(this.player);
@ -85,7 +84,7 @@ public class World implements Ionizable, Updateable {
}
public void addLevel(LevelMap level)
public void addLevel(Level level)
{
levels.add(level);
}
@ -102,7 +101,7 @@ public class World implements Ionizable, Updateable {
public void update(double delta)
{
for (int level = 0; level < levels.size(); level++) {
for (MapObserver observer : observers) {
for (final MapObserver observer : observers) {
if (observer.getPosition().floor == level) {
levels.get(level).update(observer, delta);
}
@ -111,7 +110,7 @@ public class World implements Ionizable, Updateable {
}
public LevelMap getLevelForObserver(MapObserver observer)
public Level getLevelForObserver(MapObserver observer)
{
return levels.get(observer.getPosition().floor);
}
@ -127,28 +126,28 @@ public class World implements Ionizable, Updateable {
*/
public void render(final RectBound viewport, final int yTiles, final int xTiles, final int minSize)
{
LevelMap floor = getLevelForObserver(player); // TODO fractional movement
final Level floor = getLevelForObserver(player); // TODO fractional movement
Rect r = viewport.getRect();
double vpH = r.height().value();
double vpW = r.width().value();
final Rect r = viewport.getRect();
final double vpH = r.height().value();
final double vpW = r.width().value();
// adjust tile size to fit desired amount of tiles
double allowedSizeW = vpW / xTiles;
double allowedSizeH = vpH / yTiles;
final double allowedSizeW = vpW / xTiles;
final double allowedSizeH = vpH / yTiles;
int tileSize = (int) Math.round(Math.max(Math.min(allowedSizeH, allowedSizeW), minSize));
tileSize -= tileSize % 16;
VectConst vpCenter = r.center().sub(tileSize * 0.5, tileSize).freeze(); // 0.5 to center, 1 to move up (down is teh navbar)
final VectConst vpCenter = r.center().sub(tileSize * 0.5, tileSize).freeze(); // 0.5 to center, 1 to move up (down is teh navbar)
int playerX = player.getPosition().x;
int playerY = player.getPosition().y;
final int playerX = player.getPosition().x;
final int playerY = player.getPosition().y;
// total map area
//@formatter:off
RectConst mapRect = vpCenter.startRect().grow(
final RectConst mapRect = vpCenter.startRect().grow(
playerX*tileSize,
playerY*tileSize,//
(floor.getWidth() - playerX) * tileSize,
@ -157,12 +156,12 @@ public class World implements Ionizable, Updateable {
//@formatter:on
// tiles to render
int x1 = (int) Math.floor(playerX - (vpW / tileSize));
int y1 = (int) Math.floor(playerY - (vpH / tileSize));
int x2 = (int) Math.ceil(playerX + (vpW / tileSize));
int y2 = (int) Math.ceil(playerY + (vpH / tileSize));
final int x1 = (int) Math.floor(playerX - (vpW / tileSize));
final int y1 = (int) Math.floor(playerY - (vpH / tileSize));
final int x2 = (int) Math.ceil(playerX + (vpW / tileSize));
final int y2 = (int) Math.ceil(playerY + (vpH / tileSize));
TileRenderContext trc = new TileRenderContext(floor, mapRect); //-tileSize*0.5
final TileRenderContext trc = new TileRenderContext(floor, mapRect); //-tileSize*0.5
for (trc.y = y1; trc.y <= y2; trc.y++) {
for (trc.x = x1; trc.x <= x2; trc.x++) {
trc.render();

@ -13,7 +13,6 @@ import mightypork.rogue.world.tile.Tiles;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.IonConstructor;
import mightypork.util.files.ion.Ionizable;
import mightypork.util.logging.Log;
/**
@ -21,7 +20,7 @@ import mightypork.util.logging.Log;
*
* @author MightyPork
*/
public class LevelMap implements MapAccess, Ionizable {
public class Level implements MapAccess, Ionizable {
public static final int ION_MARK = 702;
@ -35,12 +34,12 @@ public class LevelMap implements MapAccess, Ionizable {
@IonConstructor
public LevelMap()
public Level()
{
}
public LevelMap(int width, int height)
public Level(int width, int height)
{
this.width = width;
this.height = height;
@ -56,7 +55,7 @@ public class LevelMap implements MapAccess, Ionizable {
}
public void fill(int id)
public void fill(short id)
{
fill(Tiles.get(id));
}
@ -131,19 +130,17 @@ public class LevelMap implements MapAccess, Ionizable {
@Override
public void load(InputStream in) throws IOException
{
seed = Ion.readLong(in);
width = Ion.readInt(in);
height = Ion.readInt(in);
buildArray();
while (Ion.hasNextEntry(in)) {
final int x = Ion.readInt(in);
final int y = Ion.readInt(in);
final Tile tile = (Tile) Ion.readObject(in);
setTile(tile, x, y);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
tiles[y][x] = new Tile();
tiles[y][x].load(in);
}
}
}
@ -151,24 +148,15 @@ public class LevelMap implements MapAccess, Ionizable {
@Override
public void save(OutputStream out) throws IOException
{
Ion.writeLong(out, seed);
Ion.writeInt(out, width);
Ion.writeInt(out, height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
final Tile t = getTile(x, y);
// skip null tiles
if (!t.getModel().isNullTile()) {
Ion.writeMark(out, Ion.ENTRY);
Ion.writeInt(out, x);
Ion.writeInt(out, y);
Ion.writeObject(out, t);
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
tiles[y][x].save(out);
}
}
// end of sequence
Ion.writeMark(out, Ion.END);
}
@ -181,8 +169,8 @@ public class LevelMap implements MapAccess, Ionizable {
public void update(MapObserver observer, double delta)
{
int viewRange = observer.getViewRange();
WorldPos position = observer.getPosition();
final int viewRange = observer.getViewRange();
final WorldPos position = observer.getPosition();
int x1 = position.x - viewRange;
int y1 = position.y - viewRange;

@ -1,14 +1,15 @@
package mightypork.rogue.world.structs;
import mightypork.rogue.world.map.LevelMap;
import mightypork.rogue.world.map.Level;
import mightypork.util.files.ion.templates.IonizableArrayList;
public class LevelList extends IonizableArrayList<LevelMap> {
public class LevelList extends IonizableArrayList<Level> {
public static final short ION_MARK = 709;
@Override
public short getIonMark()
{

@ -4,14 +4,12 @@ package mightypork.rogue.world.tile;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Stack;
import mightypork.rogue.world.item.Item;
import mightypork.rogue.world.map.TileRenderContext;
import mightypork.rogue.world.structs.ItemStack;
import mightypork.util.control.timing.Animator;
import mightypork.util.control.timing.Updateable;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.IonBundle;
import mightypork.util.files.ion.IonConstructor;
import mightypork.util.files.ion.Ionizable;
@ -26,7 +24,7 @@ public final class Tile implements Ionizable, Updateable {
public int id;
public Stack<Item> items = new Stack<>();
public ItemStack items = new ItemStack();
public boolean[] flags;
public int[] numbers;
@ -66,30 +64,30 @@ public final class Tile implements Ionizable, Updateable {
{
if (model.isNullTile()) throw new RuntimeException("Cannot save null tile.");
final IonBundle ib = new IonBundle();
Ion.writeShort(out, (short) id);
ib.put("id", id);
ib.put("flags", flags);
ib.put("numbers", numbers);
byte written = 0;
if (flags != null) written |= 1;
if (numbers != null) written |= 2;
if (items != null && !items.isEmpty()) written |= 4;
Ion.writeByte(out, written);
Ion.writeSequence(out, items);
Ion.writeObject(out, ib);
if ((written & 1) != 0) Ion.writeBooleanArray(out, flags);
if ((written & 2) != 0) Ion.writeIntArray(out, numbers);
if ((written & 4) != 0) Ion.writeObject(out, items);
}
@Override
public void load(InputStream in) throws IOException
{
// bundle of data
final IonBundle ib = (IonBundle) Ion.readObject(in);
id = ib.get("id", id);
flags = ib.get("flags", flags);
numbers = ib.get("numbers", numbers);
id = Ion.readShort(in);
final byte written = Ion.readByte(in);
// stored items
items.clear();
Ion.readSequence(in, items);
if ((written & 1) != 0) flags = Ion.readBooleanArray(in);
if ((written & 2) != 0) numbers = Ion.readIntArray(in);
if ((written & 4) != 0) items = (ItemStack) Ion.readObject(in);
// renew model
if (model == null || id != model.id) {

@ -1,7 +1,6 @@
package mightypork.rogue.world.tile.models;
import mightypork.gamecore.render.DisplaySystem;
import mightypork.gamecore.render.Render;
import mightypork.gamecore.render.textures.TxSheet;
import mightypork.rogue.Res;
@ -9,8 +8,6 @@ import mightypork.rogue.world.map.TileRenderContext;
import mightypork.rogue.world.tile.Tile;
import mightypork.rogue.world.tile.TileModel;
import mightypork.util.annotations.DefaultImpl;
import mightypork.util.constraints.vect.Vect;
import mightypork.util.math.color.RGB;
public abstract class SimpleTile extends TileModel {
@ -31,6 +28,7 @@ public abstract class SimpleTile extends TileModel {
Render.quadTextured(context.getRect(), sheet.getRandomQuad(context.getTileNoise()));
}
@Override
@DefaultImpl
public void update(Tile tile, double delta)

@ -725,7 +725,6 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
};
}
/**
@ -756,7 +755,6 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
};
}
/**
@ -789,7 +787,6 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
}
public Num x()
{
return p_x != null ? p_x : (p_x = origin().xn());
@ -1048,5 +1045,5 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
// overflow || intersect
return ((rw < rx || rw > tx) && (rh < ry || rh > ty) && (tw < tx || tw > rx) && (th < ty || th > ry));
}
}

@ -1,5 +1,6 @@
package mightypork.util.error;
import java.io.IOException;

@ -3,9 +3,13 @@ package mightypork.util.files.ion;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import mightypork.rogue.world.tile.Tile;
import mightypork.util.error.CorruptedDataException;
import mightypork.util.logging.Log;
@ -403,11 +407,17 @@ public class Ion {
*/
public static void writeObject(OutputStream out, Object obj) throws IOException
{
if (obj instanceof Tile) throw new IllegalAccessError();
if (obj == null) {
writeMark(out, NULL);
return;
}
if (obj instanceof Ionizable) {
short mark = ((Ionizable) obj).getIonMark();
final short mark = ((Ionizable) obj).getIonMark();
Class<? extends Ionizable> clzRegistered = customIonizables.get(mark);
final Class<? extends Ionizable> clzRegistered = customIonizables.get(mark);
if (clzRegistered == null) {
throw new IOException("Ionizable object not registered: " + Log.str(obj.getClass()));
@ -1292,6 +1302,10 @@ public class Ion {
public static <K, V> void writeMap(OutputStream out, Map<K, V> map) throws IOException
{
for (final Entry<K, V> e : map.entrySet()) {
if (e.getValue() == null) {
continue;
}
writeMark(out, ENTRY);
writeObject(out, e.getKey());
writeObject(out, e.getValue());

@ -1,5 +1,6 @@
package mightypork.util.files.ion;
import mightypork.util.files.ion.templates.IonizableHashMap;

@ -337,6 +337,8 @@ public class Log {
public static String str(Object o)
{
if (o == null) return "<null>";
boolean hasToString = false;
try {

Loading…
Cancel
Save