Removed the worker process and replaced it with worker threads.
Updated binary data format.
This commit is contained in:
parent
e08cdf66fc
commit
7c176bbcde
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<width*height;i++) {
|
||||
|
|
|
|||
|
|
@ -25,8 +25,6 @@ public class DisplayStatsEventHandler implements IMainloopTask
|
|||
// Display the fps
|
||||
fps = DisplayWindow.fps;
|
||||
DisplayWindow.fps = 0;
|
||||
|
||||
Main.worker.updateTime();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import projectzombie.init.Tiles;
|
|||
import projectzombie.inventory.Inventory;
|
||||
import projectzombie.items.ItemBurnable;
|
||||
import projectzombie.items.ItemSmeltable;
|
||||
import projectzombie.items.ItemSmeltable;
|
||||
import projectzombie.items.ItemFlint;
|
||||
import projectzombie.items.ItemSmeltable;
|
||||
import projectzombie.items.modifier.ItemModifierDamage;
|
||||
|
|
@ -25,8 +26,8 @@ public class TileEntityBlastFurnace extends TileEntity
|
|||
private Inventory inventory;
|
||||
private int next_level;
|
||||
private int burn_time;
|
||||
private int smelt_time;
|
||||
private int smelt_slot = -1;
|
||||
private int cook_time;
|
||||
private int cook_slot;
|
||||
private int level;
|
||||
|
||||
public TileEntityBlastFurnace(BdfObject bdf) {
|
||||
|
|
@ -48,8 +49,8 @@ public class TileEntityBlastFurnace extends TileEntity
|
|||
burning = new ItemStack(nl.get("burning"));
|
||||
|
||||
burn_time = nl.get("burnTime").getInteger();
|
||||
smelt_time = nl.get("smeltTime").getInteger();
|
||||
smelt_slot = nl.get("smeltSlot").getInteger();
|
||||
cook_time = nl.get("cookTime").getInteger();
|
||||
cook_slot = nl.get("cookSlot").getInteger();
|
||||
next_level = nl.get("nextLevel").getInteger();
|
||||
level = nl.get("level").getInteger();
|
||||
}
|
||||
|
|
@ -64,8 +65,8 @@ public class TileEntityBlastFurnace extends TileEntity
|
|||
burning.BdfClassSave(nl.get("burning"));
|
||||
|
||||
nl.set("burnTime", bdf.newObject().setInteger(burn_time));
|
||||
nl.set("smeltTime", bdf.newObject().setInteger(smelt_time));
|
||||
nl.set("smeltSlot", bdf.newObject().setInteger(smelt_slot));
|
||||
nl.set("cookTime", bdf.newObject().setInteger(cook_time));
|
||||
nl.set("cookSlot", bdf.newObject().setInteger(cook_slot));
|
||||
nl.set("nextLevel", bdf.newObject().setInteger(next_level));
|
||||
nl.set("level", bdf.newObject().setInteger(level));
|
||||
}
|
||||
|
|
@ -115,48 +116,6 @@ public class TileEntityBlastFurnace extends TileEntity
|
|||
{
|
||||
super.tick(chunk, layer);
|
||||
|
||||
// Check for smeltable items if things aren't being smelted
|
||||
if(inventory.getItem(smelt_slot).isEmpty())
|
||||
{
|
||||
for(int i=0;i<inventory.getSlotCount();i++)
|
||||
{
|
||||
ItemStack stack = inventory.getItem(i);
|
||||
|
||||
if(stack.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(stack.item instanceof ItemSmeltable) {
|
||||
smelt_slot = i;
|
||||
smelt_time = 1600;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Smelt the item thats currently being smelted
|
||||
// and process them if they have finished smelting.
|
||||
// Make sure any non-smeltable items don't get smelted.
|
||||
{
|
||||
ItemStack stack = inventory.getItem(smelt_slot);
|
||||
|
||||
if(!stack.isEmpty() && stack.item instanceof ItemSmeltable)
|
||||
{
|
||||
smelt_time -= 1;
|
||||
|
||||
if(smelt_time < 0)
|
||||
{
|
||||
stack.count -= 1;
|
||||
|
||||
ItemStack[] result = ((ItemSmeltable)stack.item).getSmeltProducts(stack);
|
||||
|
||||
for(int i=0;i<result.length;i++) {
|
||||
inventory.addItem(result[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for fuel if the fuel has run out
|
||||
if(burning.isEmpty())
|
||||
{
|
||||
|
|
@ -188,6 +147,55 @@ public class TileEntityBlastFurnace extends TileEntity
|
|||
}
|
||||
}
|
||||
|
||||
if(level > 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<inventory.getSlotCount();i++)
|
||||
{
|
||||
ItemStack check = inventory.getItem(i);
|
||||
|
||||
if(check.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(check.item instanceof ItemSmeltable) {
|
||||
cook_slot = i;
|
||||
cook_time = 1600;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cook the item thats currently being cooked
|
||||
// and process them if they have finished cooking.
|
||||
// Make sure any non-cookable items don't get cooked.
|
||||
|
||||
stack = inventory.getItem(cook_slot);
|
||||
|
||||
if(!stack.isEmpty() && stack.item instanceof ItemSmeltable)
|
||||
{
|
||||
cook_time -= 1;
|
||||
|
||||
if(cook_time < 0)
|
||||
{
|
||||
stack.count -= 1;
|
||||
cook_time = 1600;
|
||||
|
||||
ItemStack[] result = ((ItemSmeltable)stack.item).getSmeltProducts(stack);
|
||||
|
||||
for(int i=0;i<result.length;i++) {
|
||||
inventory.addItem(result[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
next_level -= 1;
|
||||
|
||||
// Make the fire burn brighter/darker based on whether there is fuel or not
|
||||
|
|
|
|||
|
|
@ -35,17 +35,17 @@ public class TileBlastFurnace extends Tile
|
|||
|
||||
@Override
|
||||
public Model getModel(byte meta) {
|
||||
return Models.TILE_CAMPFIRE;
|
||||
return Models.TILE_BLAST_FURNACE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivated(Layer layer, Vec2i tpos, Entity entity, TileState state) {
|
||||
super.onActivated(layer, tpos, entity, state);
|
||||
|
||||
TileEntityCampfire te = (TileEntityCampfire)layer.getTileEntity(tpos);
|
||||
TileEntityBlastFurnace te = (TileEntityBlastFurnace)layer.getTileEntity(tpos);
|
||||
|
||||
if(!(te instanceof TileEntityCampfire)) {
|
||||
te = new TileEntityCampfire(tpos);
|
||||
if(!(te instanceof TileEntityBlastFurnace)) {
|
||||
te = new TileEntityBlastFurnace(tpos);
|
||||
layer.createTileEntity(tpos, te);
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +73,7 @@ public class TileBlastFurnace extends Tile
|
|||
public void onDestroy(Layer layer, Chunk chunk, TileState oldState, TileState newState, Vec2i pos) {
|
||||
super.onDestroy(layer, chunk, oldState, newState, pos);
|
||||
|
||||
if(newState.tile == Tiles.CAMPFIRE) {
|
||||
if(newState.tile == Tiles.BLAST_FURNACE) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,12 +22,37 @@ public class TileGrass extends Tile
|
|||
|
||||
if(Math.random() > 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<task.width;x++) {
|
||||
for(int y=0;y<task.height;y++)
|
||||
{
|
||||
float level = getLight(task, x, y);
|
||||
|
||||
if(level == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
calculateLight(task, x, y, level, true);
|
||||
}
|
||||
}
|
||||
|
||||
DisplayLighting.updateLighting(task.lights, task.x, task.y, task.width, task.height);
|
||||
}
|
||||
|
||||
else {
|
||||
Thread.sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
catch(InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void kill() {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
BIN
worker.jar
BIN
worker.jar
Binary file not shown.
Loading…
Reference in New Issue