Made bdf use the .gz file ending to say its gzip

This commit is contained in:
josua 2020-09-10 12:30:23 +10:00
parent 76fce435b1
commit ce06e1bb53
38 changed files with 410 additions and 309 deletions

BIN
run/saves.bdf.gz Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
run/settings.bdf.gz Normal file

Binary file not shown.

View File

@ -5,6 +5,7 @@ import java.awt.datatransfer.Clipboard;
import java.io.IOException; import java.io.IOException;
import java.util.Random; import java.util.Random;
import bdf.types.BdfReader;
import gl_engine.MathHelpers; import gl_engine.MathHelpers;
import mainloop.manager.MainloopManager; import mainloop.manager.MainloopManager;
import projectzombie.audio.AudioEngine; import projectzombie.audio.AudioEngine;
@ -33,14 +34,16 @@ import projectzombie.settings.Environment;
import projectzombie.settings.Settings; import projectzombie.settings.Settings;
import projectzombie.time.GameTimer; import projectzombie.time.GameTimer;
import projectzombie.time.NoSleep; import projectzombie.time.NoSleep;
import projectzombie.worker.WorkerChunks; import projectzombie.util.FileHelpers;
import projectzombie.worker.WorkerFile; import projectzombie.worker.WorkerTasks;
import projectzombie.worker.WorkerLighting; import projectzombie.worker.WorkerLighting;
import projectzombie.world.World; import projectzombie.world.World;
import projectzombie.world.chunk.ChunkEventHandler; import projectzombie.world.chunk.ChunkEventHandler;
public class Main public class Main
{ {
public static BdfReader bdf_saves;
public static Clipboard clipboard; public static Clipboard clipboard;
public static MainloopManager mainloop; public static MainloopManager mainloop;
public static DisplayWindow window; public static DisplayWindow window;
@ -49,9 +52,8 @@ public class Main
public static AudioEngine audio; public static AudioEngine audio;
public static Random rand = new Random(); public static Random rand = new Random();
public static Menu menu; public static Menu menu;
public static WorkerFile workerFile;
public static WorkerLighting workerLighting; public static WorkerLighting workerLighting;
public static WorkerChunks[] workerChunks; public static WorkerTasks[] workerChunks;
public static boolean game_paused = false; public static boolean game_paused = false;
public static boolean running = true; public static boolean running = true;
public static int tickrate = 10; public static int tickrate = 10;
@ -60,18 +62,23 @@ public class Main
{ {
try try
{ {
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Runtime runtime = Runtime.getRuntime();
int cores = runtime.availableProcessors();
workerFile = new WorkerFile(); clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
workerFile.start();
workerLighting = new WorkerLighting(); workerLighting = new WorkerLighting();
workerLighting.start(); workerLighting.start();
workerChunks = new WorkerChunks[4]; int workers = cores - 2;
if(workers < 1) {
workers = 1;
}
workerChunks = new WorkerTasks[workers];
for(int i=0;i<workerChunks.length;i++) { for(int i=0;i<workerChunks.length;i++) {
workerChunks[i] = new WorkerChunks(); workerChunks[i] = new WorkerTasks();
workerChunks[i].start(); workerChunks[i].start();
} }
@ -88,6 +95,8 @@ public class Main
LayerGenerators.init(); LayerGenerators.init();
Recipes.init(); Recipes.init();
bdf_saves = FileHelpers.readBDF("./saves.bdf.gz");
// Create the display // Create the display
window = new DisplayWindow(); window = new DisplayWindow();
window.init(); window.init();

View File

@ -4,7 +4,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Random; import java.util.Random;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import gl_engine.MathHelpers; import gl_engine.MathHelpers;
@ -19,7 +19,7 @@ import projectzombie.util.math.TileState;
import projectzombie.world.chunk.Chunk; import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer; import projectzombie.world.layer.Layer;
public abstract class Entity implements IBdfClassManager public abstract class Entity implements ClassBdf
{ {
public double hitbox = 1; public double hitbox = 1;
public boolean isSolid = false; public boolean isSolid = false;

View File

@ -1,6 +1,6 @@
package projectzombie.inventory; package projectzombie.inventory;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.types.BdfArray; import bdf.types.BdfArray;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
@ -13,7 +13,7 @@ import projectzombie.util.math.ItemStack;
import projectzombie.world.chunk.Chunk; import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer; import projectzombie.world.layer.Layer;
public class Inventory implements IInventory, IBdfClassManager public class Inventory implements IInventory, ClassBdf
{ {
protected ItemStack[] items; protected ItemStack[] items;

View File

@ -1,11 +1,11 @@
package projectzombie.inventory; package projectzombie.inventory;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import projectzombie.util.math.ItemStack; import projectzombie.util.math.ItemStack;
public class InventoryArmor implements IBdfClassManager, IInventoryArmor public class InventoryArmor implements ClassBdf, IInventoryArmor
{ {
private ItemStack helmet, chest, leggings; private ItemStack helmet, chest, leggings;

View File

@ -1,11 +1,11 @@
package projectzombie.inventory; package projectzombie.inventory;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import projectzombie.util.math.ItemStack; import projectzombie.util.math.ItemStack;
public class InventoryClothing implements IBdfClassManager, IInventoryClothing public class InventoryClothing implements ClassBdf, IInventoryClothing
{ {
private ItemStack shirt, pants, boots; private ItemStack shirt, pants, boots;

View File

@ -3,7 +3,7 @@ package projectzombie.items.modifier;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import projectzombie.entity.Entity; import projectzombie.entity.Entity;
@ -11,7 +11,7 @@ import projectzombie.init.ItemModifiers;
import projectzombie.init.Tasks; import projectzombie.init.Tasks;
import projectzombie.task.Task; import projectzombie.task.Task;
public abstract class ItemModifier implements IBdfClassManager public abstract class ItemModifier implements ClassBdf
{ {
public abstract boolean isEqual(ItemModifier other); public abstract boolean isEqual(ItemModifier other);
public abstract ItemModifier copy(); public abstract ItemModifier copy();

View File

@ -4,10 +4,10 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import bdf.file.BdfFileManager;
import bdf.types.BdfArray; import bdf.types.BdfArray;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import bdf.types.BdfReader;
import gl_engine.matrix.Matrix4; import gl_engine.matrix.Matrix4;
import gl_engine.vec.Vec2d; import gl_engine.vec.Vec2d;
import projectzombie.Main; import projectzombie.Main;
@ -21,13 +21,14 @@ import projectzombie.menu.gui.GUIContainerSlider;
import projectzombie.menu.gui.GUILabel; import projectzombie.menu.gui.GUILabel;
import projectzombie.menu.gui.GUISavesCard; import projectzombie.menu.gui.GUISavesCard;
import projectzombie.model.ModelGui; import projectzombie.model.ModelGui;
import projectzombie.util.FileHelpers;
import projectzombie.worker.WorkerTasks;
import projectzombie.world.World; import projectzombie.world.World;
public class MenuSaves extends Menu public class MenuSaves extends Menu
{ {
private static final Random rand = new Random(); private static final Random rand = new Random();
private BdfFileManager reader;
private ArrayList<SaveCard> saves; private ArrayList<SaveCard> saves;
private int saves_remove = -1; private int saves_remove = -1;
@ -74,7 +75,7 @@ public class MenuSaves extends Menu
Models.UI_SELECTION_BOX_WIDE.getWidth() * 254 / 256.0, Models.UI_SELECTION_BOX_WIDE.getWidth() * 254 / 256.0,
Models.UI_SELECTION_BOX_WIDE.getHeight() * 126 / 128.0), 10); Models.UI_SELECTION_BOX_WIDE.getHeight() * 126 / 128.0), 10);
reloadDatabase(); reloadDatabase(Main.bdf_saves);
buttonBack.setAlign(GUIAlignment.RIGHT); buttonBack.setAlign(GUIAlignment.RIGHT);
buttonCreate.setAlign(GUIAlignment.LEFT); buttonCreate.setAlign(GUIAlignment.LEFT);
@ -99,14 +100,14 @@ public class MenuSaves extends Menu
private void loadSave(int index) private void loadSave(int index)
{ {
BdfObject bdf = reader.getObject(); BdfObject bdf = Main.bdf_saves.getObject();
BdfNamedList nl = bdf.getNamedList(); BdfNamedList nl = bdf.getNamedList();
BdfArray array = nl.get("saves").getArray(); BdfArray array = nl.get("saves").getArray();
BdfObject save_bdf = array.remove(index); BdfObject save_bdf = array.remove(index);
array.add(save_bdf); array.add(save_bdf);
reader.saveDatabase(); WorkerTasks.saveToFile("./saves.bdf.gz", Main.bdf_saves);
Main.world = new World(saves.get(index).filename); Main.world = new World(saves.get(index).filename);
Main.menu = new MenuLoadingWorld(); Main.menu = new MenuLoadingWorld();
@ -118,10 +119,9 @@ public class MenuSaves extends Menu
Main.menu = new MenuWorldDelete(this, save.text, save.filename); Main.menu = new MenuWorldDelete(this, save.text, save.filename);
} }
public void reloadDatabase() public void reloadDatabase(BdfReader reader)
{ {
saves.clear(); saves.clear();
reader = new BdfFileManager("./saves.bdf", true);
BdfNamedList nl = reader.getObject().getNamedList(); BdfNamedList nl = reader.getObject().getNamedList();
BdfArray array = nl.get("saves").getArray(); BdfArray array = nl.get("saves").getArray();

View File

@ -9,7 +9,7 @@ import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.GUIAlignment; import projectzombie.menu.gui.GUIAlignment;
import projectzombie.menu.gui.GUILabel; import projectzombie.menu.gui.GUILabel;
import projectzombie.util.gl.GlHelpers; import projectzombie.util.gl.GlHelpers;
import projectzombie.worker.WorkerChunks; import projectzombie.worker.WorkerTasks;
import projectzombie.world.chunk.ChunkEventHandler; import projectzombie.world.chunk.ChunkEventHandler;
public class MenuSavingWorld extends Menu public class MenuSavingWorld extends Menu
@ -56,7 +56,7 @@ public class MenuSavingWorld extends Menu
{ {
super.update(); super.update();
if(WorkerChunks.isDone()) { if(WorkerTasks.isDone()) {
Main.menu = new MenuMain(); Main.menu = new MenuMain();
} }
} }

View File

@ -9,7 +9,7 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import bdf.file.BdfFileManager;
import bdf.types.BdfArray; import bdf.types.BdfArray;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
@ -24,6 +24,8 @@ import projectzombie.menu.gui.GUIBackToMenu;
import projectzombie.menu.gui.GUIButtonBasic; import projectzombie.menu.gui.GUIButtonBasic;
import projectzombie.menu.gui.GUILabel; import projectzombie.menu.gui.GUILabel;
import projectzombie.menu.gui.GUITextBox; import projectzombie.menu.gui.GUITextBox;
import projectzombie.util.FileHelpers;
import projectzombie.worker.WorkerTasks;
public class MenuWorldDelete extends Menu public class MenuWorldDelete extends Menu
{ {
@ -85,8 +87,7 @@ public class MenuWorldDelete extends Menu
private void deleteSave() private void deleteSave()
{ {
BdfFileManager reader = new BdfFileManager("./saves.bdf", true); BdfObject bdf = Main.bdf_saves.getObject();
BdfObject bdf = reader.getObject();
BdfNamedList nl = bdf.getNamedList(); BdfNamedList nl = bdf.getNamedList();
BdfArray array = nl.get("saves").getArray(); BdfArray array = nl.get("saves").getArray();
@ -132,8 +133,8 @@ public class MenuWorldDelete extends Menu
Main.menu = parent; Main.menu = parent;
reader.saveDatabase(); WorkerTasks.saveToFile("./saves.bdf.gz", Main.bdf_saves);
parent.reloadDatabase(); parent.reloadDatabase(Main.bdf_saves);
} }
@Override @Override

View File

@ -2,10 +2,11 @@ package projectzombie.menu;
import java.util.Random; import java.util.Random;
import bdf.file.BdfFileManager;
import bdf.types.BdfArray; import bdf.types.BdfArray;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import bdf.types.BdfReader;
import gl_engine.vec.Vec2d; import gl_engine.vec.Vec2d;
import projectzombie.Main; import projectzombie.Main;
import projectzombie.init.Models; import projectzombie.init.Models;
@ -16,6 +17,8 @@ import projectzombie.menu.gui.GUIBackToMenu;
import projectzombie.menu.gui.GUIButtonBasic; import projectzombie.menu.gui.GUIButtonBasic;
import projectzombie.menu.gui.GUILabel; import projectzombie.menu.gui.GUILabel;
import projectzombie.menu.gui.GUITextBox; import projectzombie.menu.gui.GUITextBox;
import projectzombie.util.FileHelpers;
import projectzombie.worker.WorkerTasks;
import projectzombie.world.World; import projectzombie.world.World;
public class MenuWorldNew extends Menu public class MenuWorldNew extends Menu
@ -114,8 +117,7 @@ public class MenuWorldNew extends Menu
return; return;
} }
BdfFileManager reader = new BdfFileManager("./saves.bdf", true); BdfObject bdf = Main.bdf_saves.getObject();
BdfObject bdf = reader.getObject();
BdfNamedList nl = bdf.getNamedList(); BdfNamedList nl = bdf.getNamedList();
BdfArray array = nl.get("saves").getArray(); BdfArray array = nl.get("saves").getArray();
@ -155,7 +157,7 @@ public class MenuWorldNew extends Menu
save_nl.set("path", bdf.newObject().setString(path_new)); save_nl.set("path", bdf.newObject().setString(path_new));
array.add(save_bdf); array.add(save_bdf);
reader.saveDatabase(); WorkerTasks.saveToFile("./saves.bdf.gz", Main.bdf_saves);
World.createWorld(path_new, seed); World.createWorld(path_new, seed);
Main.menu = new MenuLoadingWorld(); Main.menu = new MenuLoadingWorld();

View File

@ -4,7 +4,7 @@ import projectzombie.world.chunk.Chunk;
public class Environment public class Environment
{ {
public static String gdir = "."; public static String gdir = "./";
public static void init(String args[]) public static void init(String args[])
{ {

View File

@ -1,19 +1,21 @@
package projectzombie.settings; package projectzombie.settings;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.file.BdfFileManager; import projectzombie.util.FileHelpers;
import projectzombie.worker.WorkerTasks;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import bdf.types.BdfReader;
import bdf.types.BdfTypes; import bdf.types.BdfTypes;
import projectzombie.display.DisplayRender; import projectzombie.display.DisplayRender;
import projectzombie.display.DisplayRenderUI; import projectzombie.display.DisplayRenderUI;
import projectzombie.display.DisplayWindow; import projectzombie.display.DisplayWindow;
import projectzombie.world.chunk.Chunk; import projectzombie.world.chunk.Chunk;
public class Settings implements IBdfClassManager public class Settings implements ClassBdf
{ {
public static final Settings SETTINGS = new Settings(); public static final Settings SETTINGS = new Settings();
private static BdfFileManager FILE_MANAGER; private static BdfReader READER;
@Override @Override
public void BdfClassLoad(BdfObject bdf) { public void BdfClassLoad(BdfObject bdf) {
@ -120,13 +122,13 @@ public class Settings implements IBdfClassManager
} }
public static void init() { public static void init() {
FILE_MANAGER = new BdfFileManager(Environment.gdir + "/settings.bdf", true); READER = FileHelpers.readBDF("./settings.bdf.gz");
SETTINGS.BdfClassLoad(FILE_MANAGER.getObject()); SETTINGS.BdfClassLoad(READER.getObject());
} }
public static void update() { public static void update() {
SETTINGS.BdfClassSave(FILE_MANAGER.getObject()); SETTINGS.BdfClassSave(READER.getObject());
FILE_MANAGER.saveDatabase(); WorkerTasks.saveToFile("./settings.bdf.gz", READER);
} }
} }

View File

@ -3,14 +3,14 @@ package projectzombie.task;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.types.BdfArray; import bdf.types.BdfArray;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import projectzombie.entity.Entity; import projectzombie.entity.Entity;
import projectzombie.init.Tasks; import projectzombie.init.Tasks;
public abstract class Task implements IBdfClassManager public abstract class Task implements ClassBdf
{ {
protected Entity parent; protected Entity parent;

View File

@ -0,0 +1,8 @@
package projectzombie.util;
import bdf.types.BdfObject;
public interface ClassBdf {
public void BdfClassLoad(BdfObject bdf);
public void BdfClassSave(BdfObject bdf);
}

View File

@ -0,0 +1,37 @@
package projectzombie.util;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import bdf.types.BdfReader;
import projectzombie.settings.Environment;
public class FileHelpers
{
public static BdfReader readBDF(String path)
{
try
{
InputStream in = new GZIPInputStream(new FileInputStream(Environment.gdir + path));
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte buffer[] = new byte[1024];
int size;
while((size = in.read(buffer)) != -1) {
out.write(buffer, 0, size);
}
in.close();
return new BdfReader(out.toByteArray());
}
catch(IOException e) {
return new BdfReader();
}
}
}

View File

@ -1,6 +1,6 @@
package projectzombie.util.math; package projectzombie.util.math;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.types.BdfArray; import bdf.types.BdfArray;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
@ -8,7 +8,7 @@ import projectzombie.init.Items;
import projectzombie.items.Item; import projectzombie.items.Item;
import projectzombie.items.modifier.ItemModifier; import projectzombie.items.modifier.ItemModifier;
public class ItemStack implements IBdfClassManager public class ItemStack implements ClassBdf
{ {
public ItemModifier[] modifiers; public ItemModifier[] modifiers;
public Item item; public Item item;

View File

@ -1,13 +0,0 @@
package projectzombie.worker;
import java.util.Random;
import gl_engine.vec.Vec2i;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
import projectzombie.world.layer.layergen.LayerGen;
abstract class WorkerChunkTask
{
Vec2i pos;
}

View File

@ -1,16 +0,0 @@
package projectzombie.worker;
import gl_engine.vec.Vec2i;
import projectzombie.world.layer.Layer;
public class WorkerChunkTaskLoad extends WorkerChunkTask
{
String path;
Layer layer;
public WorkerChunkTaskLoad(String path, Layer layer, Vec2i pos) {
this.path = path;
this.layer = layer;
this.pos = pos;
}
}

View File

@ -1,18 +0,0 @@
package projectzombie.worker;
import gl_engine.vec.Vec2i;
import projectzombie.world.chunk.Chunk;
public class WorkerChunkTaskSave extends WorkerChunkTask
{
Chunk chunk;
String path;
int id;
public WorkerChunkTaskSave(String path, Vec2i pos, Chunk chunk, int id) {
this.path = path;
this.pos = pos;
this.chunk = chunk;
this.id = id;
}
}

View File

@ -1,143 +0,0 @@
package projectzombie.worker;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import bdf.file.BdfFileManager;
import gl_engine.vec.Vec2i;
import projectzombie.Main;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
import projectzombie.world.layer.layergen.LayerGen;
public class WorkerChunks extends Thread
{
private static Vector<WorkerChunkTask> chunks_in = new Vector<WorkerChunkTask>();
private static AtomicReference<Vector<Chunk>> chunks_out = new AtomicReference<Vector<Chunk>>();
public static void generateChunk(LayerGen layergen, Layer layer, Chunk chunk, Random rand, Vec2i pos) {
chunks_in.add(new WorkerChunkTaskGenerate(layergen, layer, chunk, rand, pos.copy()));
}
public static void loadChunk(String path, Layer layer, Vec2i pos) {
chunks_in.add(new WorkerChunkTaskLoad(path, layer, pos.copy()));
}
public static void saveChunk(String path, Vec2i pos, Chunk chunk, int id) {
chunks_in.add(new WorkerChunkTaskSave(path, pos.copy(), chunk, id));
}
public static Vector<Chunk> getChunks() {
return chunks_out.getAndSet(new Vector<Chunk>());
}
public static boolean hasChunk(Vec2i cpos)
{
boolean has[] = {false};
chunks_in.forEach(c ->
{
if(
(c instanceof WorkerChunkTaskGenerate ||
c instanceof WorkerChunkTaskLoad) &&
c.pos.equal(cpos))
{
has[0] = true;
}
});
return has[0];
}
public WorkerChunks() {
chunks_out.set(new Vector<Chunk>());
}
private synchronized WorkerChunkTask getNextIfAvailable()
{
if(chunks_in.size() == 0) {
return null;
} else {
return chunks_in.remove(0);
}
}
@Override
public void run()
{
try
{
while(Main.running)
{
WorkerChunkTask ct = getNextIfAvailable();
if(ct == null) {
Thread.sleep(10);
continue;
}
// Generate a new chunk
if(ct instanceof WorkerChunkTaskGenerate)
{
WorkerChunkTaskGenerate ct_g = (WorkerChunkTaskGenerate)ct;
ct_g.layergen.generateChunk(ct_g.chunk, ct_g.layer, ct_g.rand, ct_g.pos);
chunks_out.get().add(ct_g.chunk);
}
// Load an existing chunk
if(ct instanceof WorkerChunkTaskLoad)
{
WorkerChunkTaskLoad ct_l = (WorkerChunkTaskLoad)ct;
if(ct_l.path == null) {
System.out.println("ChunkLoad: Path is NULL");
continue;
}
String path = (
"./saves/" + ct_l.path + "/c_" + ct_l.layer.id +
"_" + ct_l.pos.x + "_" + ct_l.pos.y + ".bdf");
BdfFileManager reader = new BdfFileManager(path, true);
Chunk chunk = new Chunk(ct_l.layer, ct_l.pos, reader.getObject());
chunks_out.get().add(chunk);
}
// Save a chunk
if(ct instanceof WorkerChunkTaskSave)
{
WorkerChunkTaskSave ct_s = (WorkerChunkTaskSave)ct;
if(ct_s.path == null) {
System.out.println("ChunkSave: Path is NULL");
continue;
}
String path = (
"./saves/" + ct_s.path + "/c_" + ct_s.id +
"_" + ct_s.pos.x + "_" + ct_s.pos.y + ".bdf");
BdfFileManager reader = new BdfFileManager(path, true);
ct_s.chunk.BdfClassSave(reader.getObject());
reader.saveDatabase();
}
}
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
public static boolean isDone() {
return chunks_in.size() == 0;
}
}

View File

@ -1,23 +1,19 @@
package projectzombie.worker; package projectzombie.worker;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Vector; import java.util.Vector;
import java.util.zip.GZIPOutputStream;
import bdf.types.BdfReader; import bdf.types.BdfReader;
import projectzombie.Main; import projectzombie.Main;
public class WorkerFile extends Thread public class WorkerFile extends Thread
{ {
private static Vector<WorkerFileTask> tasks = new Vector<WorkerFileTask>(); private static Vector<WorkerTaskFile> tasks = new Vector<WorkerTaskFile>();
public static void addTask(String path, BdfReader reader) { public static void addTask(String path, BdfReader reader) {
tasks.add(new WorkerFileTask(path, reader)); tasks.add(new WorkerTaskFile(path, reader));
} }
private synchronized WorkerFileTask getNextIfAvailable() private synchronized WorkerTaskFile getNextIfAvailable()
{ {
if(tasks.size() == 0) { if(tasks.size() == 0) {
return null; return null;
@ -33,26 +29,14 @@ public class WorkerFile extends Thread
{ {
while(Main.running) while(Main.running)
{ {
WorkerFileTask task = getNextIfAvailable(); WorkerTaskFile task = getNextIfAvailable();
if(task == null) { if(task == null) {
Thread.sleep(10); Thread.sleep(10);
continue; continue;
} }
try
{
OutputStream out = new FileOutputStream(task.path);
out = new GZIPOutputStream(out);
task.reader.serialize().writeToStream(out);
out.close();
}
catch(IOException e) {
e.printStackTrace();
}
} }
} }

View File

@ -1,14 +0,0 @@
package projectzombie.worker;
import bdf.types.BdfReader;
class WorkerFileTask
{
String path;
BdfReader reader;
public WorkerFileTask(String path, BdfReader reader) {
this.reader = reader;
this.path = path;
}
}

View File

@ -0,0 +1,10 @@
package projectzombie.worker;
import java.util.Vector;
import projectzombie.world.chunk.Chunk;
abstract class WorkerTask
{
public abstract void run();
}

View File

@ -0,0 +1,8 @@
package projectzombie.worker;
import gl_engine.vec.Vec2i;
public abstract class WorkerTaskChunk extends WorkerTask
{
Vec2i pos;
}

View File

@ -1,24 +1,33 @@
package projectzombie.worker; package projectzombie.worker;
import java.util.Random; import java.util.Random;
import java.util.Vector;
import gl_engine.vec.Vec2i; import gl_engine.vec.Vec2i;
import projectzombie.world.chunk.Chunk; import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer; import projectzombie.world.layer.Layer;
import projectzombie.world.layer.layergen.LayerGen; import projectzombie.world.layer.layergen.LayerGen;
class WorkerChunkTaskGenerate extends WorkerChunkTask class WorkerTaskChunkGenerate extends WorkerTaskChunk
{ {
Chunk chunk; Chunk chunk;
LayerGen layergen; LayerGen layergen;
Layer layer; Layer layer;
Random rand; Random rand;
public WorkerChunkTaskGenerate(LayerGen layergen, Layer layer, Chunk chunk, Random rand, Vec2i pos) { public WorkerTaskChunkGenerate(LayerGen layergen, Layer layer, Chunk chunk, Random rand, Vec2i pos) {
this.chunk = chunk; this.chunk = chunk;
this.layergen = layergen; this.layergen = layergen;
this.layer = layer; this.layer = layer;
this.rand = rand; this.rand = rand;
this.pos = pos; this.pos = pos;
} }
@Override
public void run()
{
layergen.generateChunk(chunk, layer, rand, pos);
WorkerTasks.addChunkOut(chunk);
}
} }

View File

@ -0,0 +1,38 @@
package projectzombie.worker;
import java.util.Vector;
import bdf.types.BdfReader;
import gl_engine.vec.Vec2i;
import projectzombie.util.FileHelpers;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
public class WorkerTaskChunkLoad extends WorkerTaskChunk
{
String path;
Layer layer;
public WorkerTaskChunkLoad(String path, Layer layer, Vec2i pos) {
this.path = path;
this.layer = layer;
this.pos = pos;
}
@Override
public void run()
{
if(path == null) {
System.out.println("ChunkLoad: Path is NULL");
return;
}
String path = "./saves/" + this.path + "/c_" + layer.id + "_" + pos.x + "_" + pos.y + ".bdf.gz";
BdfReader reader = FileHelpers.readBDF(path);
Chunk chunk = new Chunk(layer, pos, reader.getObject());
WorkerTasks.addChunkOut(chunk);
}
}

View File

@ -0,0 +1,54 @@
package projectzombie.worker;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Vector;
import java.util.zip.GZIPOutputStream;
import bdf.types.BdfReader;
import gl_engine.vec.Vec2i;
import projectzombie.settings.Environment;
import projectzombie.util.FileHelpers;
import projectzombie.world.chunk.Chunk;
public class WorkerTaskChunkSave extends WorkerTaskChunk
{
Chunk chunk;
String path;
int id;
public WorkerTaskChunkSave(String path, Vec2i pos, Chunk chunk, int id) {
this.path = path;
this.pos = pos;
this.chunk = chunk;
this.id = id;
}
@Override
public void run()
{
if(path == null) {
return;
}
String path = "./saves/" + this.path + "/c_" + id + "_" + pos.x + "_" + pos.y + ".bdf.gz";
BdfReader reader = new BdfReader();
chunk.BdfClassSave(reader.getObject());
try
{
OutputStream out = new GZIPOutputStream(new FileOutputStream(Environment.gdir + path));
reader.serialize().writeToStream(out);
out.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,39 @@
package projectzombie.worker;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Vector;
import java.util.zip.GZIPOutputStream;
import bdf.types.BdfReader;
import projectzombie.settings.Environment;
import projectzombie.world.chunk.Chunk;
class WorkerTaskFile extends WorkerTask
{
String path;
BdfReader reader;
public WorkerTaskFile(String path, BdfReader reader) {
this.reader = reader;
this.path = path;
}
@Override
public void run()
{
try
{
OutputStream out = new GZIPOutputStream(new FileOutputStream(Environment.gdir + path));
reader.serialize().writeToStream(out);
out.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,102 @@
package projectzombie.worker;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import bdf.types.BdfReader;
import gl_engine.vec.Vec2i;
import projectzombie.Main;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
import projectzombie.world.layer.layergen.LayerGen;
public class WorkerTasks extends Thread
{
private static Vector<WorkerTask> tasks_in = new Vector<WorkerTask>();
private static AtomicReference<Vector<Chunk>> chunks_out = new AtomicReference<Vector<Chunk>>();
public static void generateChunk(LayerGen layergen, Layer layer, Chunk chunk, Random rand, Vec2i pos) {
tasks_in.add(new WorkerTaskChunkGenerate(layergen, layer, chunk, rand, pos.copy()));
}
public static void loadChunk(String path, Layer layer, Vec2i pos) {
tasks_in.add(new WorkerTaskChunkLoad(path, layer, pos.copy()));
}
public static void saveChunk(String path, Vec2i pos, Chunk chunk, int id) {
tasks_in.add(new WorkerTaskChunkSave(path, pos.copy(), chunk, id));
}
public static void saveToFile(String path, BdfReader reader) {
tasks_in.add(new WorkerTaskFile(path, reader));
}
public synchronized static Vector<Chunk> getChunks() {
return chunks_out.getAndSet(new Vector<Chunk>());
}
synchronized static void addChunkOut(Chunk chunk) {
chunks_out.get().add(chunk);
}
public static boolean hasChunk(Vec2i cpos)
{
boolean has[] = {false};
tasks_in.forEach(c ->
{
if(
(c instanceof WorkerTaskChunkGenerate ||
c instanceof WorkerTaskChunkLoad) &&
((WorkerTaskChunk)c).pos.equal(cpos))
{
has[0] = true;
}
});
return has[0];
}
public WorkerTasks() {
chunks_out.set(new Vector<Chunk>());
}
private static synchronized WorkerTask getNextIfAvailable()
{
if(tasks_in.size() == 0) {
return null;
} else {
return tasks_in.remove(0);
}
}
@Override
public void run()
{
try
{
while(Main.running)
{
WorkerTask ct = getNextIfAvailable();
if(ct == null) {
Thread.sleep(10);
continue;
}
ct.run();
}
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
public static boolean isDone() {
return tasks_in.size() == 0;
}
}

View File

@ -8,8 +8,8 @@ import java.util.Random;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL33; import org.lwjgl.opengl.GL33;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.file.BdfFileManager; import projectzombie.util.FileHelpers;
import bdf.types.BdfArray; import bdf.types.BdfArray;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
@ -25,12 +25,13 @@ import projectzombie.init.LayerGenerators;
import projectzombie.init.Layers; import projectzombie.init.Layers;
import projectzombie.menu.MenuLoadingWorld; import projectzombie.menu.MenuLoadingWorld;
import projectzombie.model.Model; import projectzombie.model.Model;
import projectzombie.settings.Environment;
import projectzombie.time.GameTimer; import projectzombie.time.GameTimer;
import projectzombie.worker.WorkerFile; import projectzombie.worker.WorkerTasks;
import projectzombie.world.chunk.ChunkEventHandler; import projectzombie.world.chunk.ChunkEventHandler;
import projectzombie.world.layer.Layer; import projectzombie.world.layer.Layer;
public class World implements IBdfClassManager public class World implements ClassBdf
{ {
public static final double BIOME_SIZE = 1024; public static final double BIOME_SIZE = 1024;
@ -53,16 +54,16 @@ public class World implements IBdfClassManager
if(path != null) if(path != null)
{ {
path = "./saves/" + path + "/"; path = "./saves/" + path + "/";
File save_dir = new File(path); File save_dir = new File(Environment.gdir + path);
File file_dir = new File(path + "world.bdf"); File file_dir = new File(Environment.gdir + path + "world.bdf.gz");
if(!save_dir.exists()) { if(!save_dir.exists()) {
save_dir.mkdirs(); save_dir.mkdirs();
} }
if(file_dir.exists()) { if(file_dir.exists()) {
BdfFileManager file_manager = new BdfFileManager(path + "world.bdf", true); BdfReader reader = FileHelpers.readBDF(path + "world.bdf.gz");
BdfClassLoad(file_manager.getObject()); BdfClassLoad(reader.getObject());
} }
} }
} }
@ -272,7 +273,7 @@ public class World implements IBdfClassManager
BdfObject bdf = reader.getObject(); BdfObject bdf = reader.getObject();
BdfClassSave(bdf); BdfClassSave(bdf);
WorkerFile.addTask("./saves/" + path + "/world.bdf", reader); WorkerTasks.saveToFile("./saves/" + path + "/world.bdf.gz", reader);
} }
} }

View File

@ -4,7 +4,7 @@ import java.nio.FloatBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.types.BdfArray; import bdf.types.BdfArray;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
@ -34,7 +34,7 @@ import projectzombie.util.math.TileState;
import projectzombie.util.math.random.RandomHelpers; import projectzombie.util.math.random.RandomHelpers;
import projectzombie.world.layer.Layer; import projectzombie.world.layer.Layer;
public class Chunk implements IBdfClassManager public class Chunk implements ClassBdf
{ {
public static final Range2i CHUNK_SIZE = new Range2i(16, 16); public static final Range2i CHUNK_SIZE = new Range2i(16, 16);
public static final Chunk CHUNK_EMPTY = new ChunkEmpty(); public static final Chunk CHUNK_EMPTY = new ChunkEmpty();

View File

@ -7,7 +7,7 @@ import mainloop.task.IMainloopTask;
import projectzombie.Main; import projectzombie.Main;
import projectzombie.display.DisplayLighting; import projectzombie.display.DisplayLighting;
import projectzombie.util.math.map.Map2DElement; import projectzombie.util.math.map.Map2DElement;
import projectzombie.worker.WorkerChunks; import projectzombie.worker.WorkerTasks;
import projectzombie.world.layer.Layer; import projectzombie.world.layer.Layer;
public class ChunkEventHandler implements IMainloopTask public class ChunkEventHandler implements IMainloopTask
@ -74,7 +74,7 @@ public class ChunkEventHandler implements IMainloopTask
Vec2i c_pos = new Vec2i(cx, cy); Vec2i c_pos = new Vec2i(cx, cy);
// Is this chunk not loaded // Is this chunk not loaded
if(!layer.chunkLoaded(c_pos) && !WorkerChunks.hasChunk(c_pos)) if(!layer.chunkLoaded(c_pos) && !WorkerTasks.hasChunk(c_pos))
{ {
// Load the chunk // Load the chunk
layer.loadChunk(c_pos); layer.loadChunk(c_pos);

View File

@ -4,7 +4,7 @@ import java.nio.FloatBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import bdf.classes.IBdfClassManager; import projectzombie.util.ClassBdf;
import bdf.types.BdfArray; import bdf.types.BdfArray;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
@ -21,11 +21,11 @@ import projectzombie.util.math.TileState;
import projectzombie.util.math.map.Map2D; import projectzombie.util.math.map.Map2D;
import projectzombie.util.math.map.Map2DElement; import projectzombie.util.math.map.Map2DElement;
import projectzombie.util.math.random.NoiseGenerator; import projectzombie.util.math.random.NoiseGenerator;
import projectzombie.worker.WorkerChunks; import projectzombie.worker.WorkerTasks;
import projectzombie.world.chunk.Chunk; import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.layergen.LayerGen; import projectzombie.world.layer.layergen.LayerGen;
public class Layer implements IBdfClassManager public class Layer implements ClassBdf
{ {
public Map2D<Chunk> chunks; public Map2D<Chunk> chunks;
public NoiseGenerator[] noise_gens; public NoiseGenerator[] noise_gens;
@ -269,7 +269,7 @@ public class Layer implements IBdfClassManager
public void loadChunks() public void loadChunks()
{ {
WorkerChunks.getChunks().forEach(chunk -> WorkerTasks.getChunks().forEach(chunk ->
{ {
if(chunk.layer != this) { if(chunk.layer != this) {
return; return;
@ -296,7 +296,7 @@ public class Layer implements IBdfClassManager
{ {
// Has the chunk been saved for later // Has the chunk been saved for later
if(hasSavedChunk(pos)) { if(hasSavedChunk(pos)) {
WorkerChunks.loadChunk(Main.world.getSavePath(), this, pos); WorkerTasks.loadChunk(Main.world.getSavePath(), this, pos);
} }
else else
@ -306,7 +306,7 @@ public class Layer implements IBdfClassManager
// Create and generate the chunk // Create and generate the chunk
Chunk chunk = new Chunk(this, pos, rand); Chunk chunk = new Chunk(this, pos, rand);
WorkerChunks.generateChunk(layergen, this, chunk, new Random(cseed), pos); WorkerTasks.generateChunk(layergen, this, chunk, new Random(cseed), pos);
} }
} }
@ -316,7 +316,7 @@ public class Layer implements IBdfClassManager
Chunk chunk = chunks.get(pos); Chunk chunk = chunks.get(pos);
if(chunk.isDirty()) if(chunk.isDirty())
{ {
WorkerChunks.saveChunk(Main.world.getSavePath(), pos, chunk, id); WorkerTasks.saveChunk(Main.world.getSavePath(), pos, chunk, id);
if(!hasSavedChunk(pos)) { if(!hasSavedChunk(pos)) {
chunks_saved.add(pos); chunks_saved.add(pos);
@ -362,19 +362,18 @@ public class Layer implements IBdfClassManager
{ {
BdfNamedList nl = bdf.getNamedList(); BdfNamedList nl = bdf.getNamedList();
BdfArray bdf_chunks = nl.get("chunks").getArray(); int[] bdf_chunks = nl.get("chunks").getIntegerArray();
this.layergen = LayerGenerators.loadFromID((int)nl.get("lgen").getAutoInt()); this.layergen = LayerGenerators.loadFromID((int)nl.get("lgen").getAutoInt());
this.chunks_saved = new ArrayList<Vec2i>(bdf_chunks.size()); this.chunks_saved = new ArrayList<Vec2i>(bdf_chunks.length / 2);
this.chunks = new Map2D<Chunk>(layergen); this.chunks = new Map2D<Chunk>(layergen);
this.seed = nl.get("seed").getLong(); this.seed = nl.get("seed").getLong();
this.lseed = nl.get("lseed").getLong(); this.lseed = nl.get("lseed").getLong();
this.id = (int)nl.get("id").getAutoInt(); this.id = (int)nl.get("id").getAutoInt();
this.rand = new Random(); this.rand = new Random();
for(BdfObject bdf_chunk : bdf_chunks) { for(int i=0;i<bdf_chunks.length;i+=2) {
Vec2i c = new Vec2i(bdf_chunk); this.chunks_saved.add(new Vec2i(bdf_chunks[i], bdf_chunks[i + 1]));
chunks_saved.add(c);
} }
layergen.init(this); layergen.init(this);
@ -385,17 +384,19 @@ public class Layer implements IBdfClassManager
{ {
BdfNamedList nl = bdf.getNamedList(); BdfNamedList nl = bdf.getNamedList();
int[] bdf_chunks = new int[chunks_saved.size() * 2];
for(int i=0;i<chunks_saved.size();i++) {
Vec2i pos = chunks_saved.get(i);
bdf_chunks[i*2] = pos.x;
bdf_chunks[i*2+1] = pos.y;
}
nl.set("lseed", bdf.newObject().setLong(lseed)); nl.set("lseed", bdf.newObject().setLong(lseed));
nl.set("seed", bdf.newObject().setLong(seed)); nl.set("seed", bdf.newObject().setLong(seed));
nl.set("id", bdf.newObject().setAutoInt(id)); nl.set("id", bdf.newObject().setAutoInt(id));
nl.set("lgen", bdf.newObject().setAutoInt(LayerGenerators.getLGID(this.layergen))); nl.set("lgen", bdf.newObject().setAutoInt(LayerGenerators.getLGID(this.layergen)));
nl.set("chunks", bdf.newObject().setIntegerArray(bdf_chunks));
BdfArray bdf_chunks = bdf.newArray(chunks_saved.size());
nl.set("chunks", bdf.newObject().setArray(bdf_chunks));
for(int i=0;i<chunks_saved.size();i++) {
chunks_saved.get(i).BdfClassSave(bdf_chunks.get(i));
}
} }
public void save() public void save()
@ -404,7 +405,7 @@ public class Layer implements IBdfClassManager
{ {
if(chunk.o.isDirty()) if(chunk.o.isDirty())
{ {
WorkerChunks.saveChunk(Main.world.getSavePath(), chunk.pos, chunk.o, id); WorkerTasks.saveChunk(Main.world.getSavePath(), chunk.pos, chunk.o, id);
if(!hasSavedChunk(chunk.pos)) { if(!hasSavedChunk(chunk.pos)) {
chunks_saved.add(chunk.pos); chunks_saved.add(chunk.pos);