diff --git a/src/projectzombie/Main.java b/src/projectzombie/Main.java index efcf400..e8e47ce 100755 --- a/src/projectzombie/Main.java +++ b/src/projectzombie/Main.java @@ -33,7 +33,7 @@ import projectzombie.settings.Environment; import projectzombie.settings.Settings; import projectzombie.time.GameTimer; import projectzombie.time.NoSleep; -import projectzombie.worker.Worker; +import projectzombie.worker.WorkerLighting; import projectzombie.world.World; import projectzombie.world.chunk.ChunkEventHandler; @@ -47,7 +47,7 @@ public class Main public static AudioEngine audio; public static Random rand = new Random(); public static Menu menu; - public static Worker worker; + public static WorkerLighting workerLighting; public static boolean game_paused = false; public static int tickrate = 10; @@ -57,8 +57,8 @@ public class Main { clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); - worker = new Worker(); - worker.start(); + workerLighting = new WorkerLighting(); + workerLighting.start(); MathHelpers.init(); Settings.init(); @@ -109,14 +109,10 @@ public class Main mainloop.start(); } - catch(IOException e) { - e.printStackTrace(); - } - finally { // Kill the worker thread - worker.kill(); + workerLighting.kill(); } } } diff --git a/src/projectzombie/display/DisplayLighting.java b/src/projectzombie/display/DisplayLighting.java index d890b4d..ec86524 100755 --- a/src/projectzombie/display/DisplayLighting.java +++ b/src/projectzombie/display/DisplayLighting.java @@ -125,7 +125,7 @@ public class DisplayLighting } } - Main.worker.processLighting(lights, size, size, + Main.workerLighting.processLighting(lights, size, size, MathHelpers.floor(ppos.x / 16.0) - Chunk.RENDER_DISTANCE, MathHelpers.floor(ppos.y / 16.0) - Chunk.RENDER_DISTANCE); } @@ -318,15 +318,8 @@ public class DisplayLighting setLighting(lighting); } - public static void updateLighting(BdfObject bdf) + public static void updateLighting(float[] light, int x, int y, int width, int height) { - BdfNamedList nl = bdf.getNamedList(); - float[] light = nl.get("light").getFloatArray(); - int width = nl.get("w").getInteger(); - int height = nl.get("h").getInteger(); - int x = nl.get("x").getInteger(); - int y = nl.get("y").getInteger(); - float[] pixels = new float[width*height*3]; for(int i=0;i 0) + { + ItemStack stack = inventory.getItem(cook_slot); + + // Check for cookable items if things aren't being cooked + if(stack.isEmpty() || !(stack.item instanceof ItemSmeltable)) + { + cook_time = 1600; + + for(int i=0;i 0.8) { - Vec2i cpos = pos.add(new Vec2i((int)Math.floor(Math.random() * 3) - 1, (int)Math.floor(Math.random() * 3) - 1)); + Vec2i cpos = pos.add(new Vec2i( + (int)Math.floor(Math.random() * 3) - 1, + (int)Math.floor(Math.random() * 3) - 1)); if(layer.getBackTile(cpos).tile == Tiles.DIRT) { layer.setBackTile(Tiles.GRASS.getDefaultState(), cpos); } } + + if(Math.random() > 0.998) + { + if(layer.getFrontTile(pos).tile == Tiles.VOID) + { + int grass_count = 0; + int d = 3; + + for(int x=-d;x<=d;x++) { + for(int y=-d;y<=d;y++) + { + if(layer.getFrontTile(pos.add(new Vec2i(x, y))).tile == Tiles.TALL_GRASS) { + grass_count += 1; + } + } + } + + if(grass_count < 2) + { + layer.setFrontTile(Tiles.TALL_GRASS.getDefaultState(), pos); + } + } + } } } diff --git a/src/projectzombie/worker/Worker.java b/src/projectzombie/worker/Worker.java deleted file mode 100644 index 28d65c6..0000000 --- a/src/projectzombie/worker/Worker.java +++ /dev/null @@ -1,132 +0,0 @@ -package projectzombie.worker; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.lang.ProcessBuilder.Redirect; -import java.nio.ByteBuffer; - -import bdf.data.IBdfDatabase; -import bdf.types.BdfNamedList; -import bdf.types.BdfObject; -import bdf.types.BdfReader; -import projectzombie.display.DisplayLighting; - -public class Worker extends Thread -{ - ProcessBuilder pb; - Process process; - - InputStream in; - OutputStream out; - - boolean running = true; - - public Worker() throws IOException - { - pb = new ProcessBuilder("java", "-jar", "worker.jar"); - pb.redirectOutput(Redirect.PIPE); - pb.redirectInput(Redirect.PIPE); - pb.redirectError(Redirect.INHERIT); - - process = pb.start(); - - in = process.getInputStream(); - out = process.getOutputStream(); - } - - - - public void processLighting(float[] lights, int width, int height, int x, int y) - { - BdfReader reader = new BdfReader(); - BdfObject bdf = reader.getObject(); - BdfNamedList nl = bdf.getNamedList(); - nl.set("task", bdf.newObject().setString("light")); - nl.set("light", bdf.newObject().setFloatArray(lights)); - nl.set("w", bdf.newObject().setInteger(width)); - nl.set("h", bdf.newObject().setInteger(height)); - nl.set("x", bdf.newObject().setInteger(x)); - nl.set("y", bdf.newObject().setInteger(y)); - - sendData(reader); - } - - public void updateTime() - { - BdfReader reader = new BdfReader(); - BdfObject bdf = reader.getObject(); - BdfNamedList nl = bdf.getNamedList(); - nl.set("task", bdf.newObject().setString("time")); - nl.set("millis", bdf.newObject().setLong(System.currentTimeMillis())); - - sendData(reader); - } - - private void sendData(BdfReader reader) - { - IBdfDatabase data = reader.serialize(); - - ByteBuffer size_buff = ByteBuffer.allocate(4); - size_buff.putInt(0, data.size()); - - try - { - out.write(size_buff.array()); - data.writeToStream(out); - out.write('\n'); - out.flush(); - } - - catch(IOException e) - { - - } - } - - @Override - public void run() { - super.run(); - - try - { - while(true) - { - ByteBuffer size_buff = ByteBuffer.allocate(4); - byte[] data; - - in.read(size_buff.array()); - data = new byte[size_buff.getInt(0)]; - in.read(data); - in.read(new byte[1]); - - BdfReader reader = new BdfReader(data); - BdfObject bdf = reader.getObject(); - BdfNamedList nl = bdf.getNamedList(); - - switch(nl.get("task").getString()) - { - case "light": - DisplayLighting.updateLighting(bdf); - break; - default: - Thread.sleep(10); - } - - if(!running) { - return; - } - } - } - - catch(IOException | InterruptedException e) - { - - } - } - - public void kill() { - process.destroy(); - running = false; - } -} diff --git a/src/projectzombie/worker/WorkerLighting.java b/src/projectzombie/worker/WorkerLighting.java new file mode 100644 index 0000000..60d1760 --- /dev/null +++ b/src/projectzombie/worker/WorkerLighting.java @@ -0,0 +1,115 @@ +package projectzombie.worker; + +import projectzombie.display.DisplayLighting; +import projectzombie.task.Task; + +public class WorkerLighting extends Thread +{ + private class LightingTask { + float[] lights; + int width; + int height; + int x; + int y; + } + + private boolean running; + private LightingTask task; + + public void processLighting(float[] lights, int width, int height, int x, int y) + { + LightingTask task = new LightingTask(); + task.lights = lights; + task.width = width; + task.height = height; + task.x = x; + task.y = y; + + this.task = task; + } + + private float getLight(LightingTask task, int x, int y) { + return task.lights[(x + y * task.width) * 2]; + } + + private void setLight(LightingTask task, int x, int y, float v) { + task.lights[(x + y * task.width) * 2] = v; + } + + private float getTransparency(LightingTask task, int x, int y) { + return task.lights[(x + y * task.width) * 2 + 1]; + } + + public void calculateLight(LightingTask task, int x, int y, float level, boolean ignore) + { + if(x < 0 || y < 0 || x >= task.width || y >= task.height) { + return; + } + + float level_current = getLight(task, x, y); + + if(level_current >= level && !ignore) { + return; + } + + setLight(task, x, y, level); + + float level_next = level - getTransparency(task, x, y); + int[] adjacent = new int[] { + x+1, y+0, + x+0, y+1, + x-1, y-0, + x-0, y-1, + }; + + for(int i=0;i<8;i+=2) { + calculateLight(task, adjacent[i], adjacent[i+1], level_next, false); + } + } + + + @Override + public void run() { + super.run(); + + running = true; + + try + { + while(running) + { + LightingTask task = this.task; + + if(task != null) + { + for(int x=0;x