Gen renames

This commit is contained in:
Daniel Mills
2020-01-08 18:53:01 -05:00
parent 0d533a71b6
commit 2a7b2343b2
6 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,344 @@
package ninja.bytecode.iris.schematic;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.material.Directional;
import org.bukkit.material.MaterialData;
import org.bukkit.util.BlockVector;
import org.bukkit.util.Vector;
import ninja.bytecode.iris.util.Direction;
import ninja.bytecode.iris.util.MB;
import ninja.bytecode.iris.util.VectorMath;
import ninja.bytecode.shuriken.collections.GList;
import ninja.bytecode.shuriken.collections.GMap;
import ninja.bytecode.shuriken.io.CustomOutputStream;
import ninja.bytecode.shuriken.logging.L;
public class Schematic
{
private boolean centeredHeight;
private int w;
private int h;
private int d;
private String name = "?";
private final GMap<BlockVector, MB> s;
private BlockVector mount;
private int mountHeight;
private BlockVector shift;
public Schematic(int w, int h, int d)
{
this.w = w;
this.h = h;
this.d = d;
shift = new BlockVector();
s = new GMap<>();
centeredHeight = false;
}
public void computeMountShift()
{
int ly = Integer.MAX_VALUE;
for(BlockVector i : s.k())
{
if(i.getBlockY() < ly)
{
ly = i.getBlockY();
}
}
GList<BlockVector> fmount = new GList<>();
for(BlockVector i : s.k())
{
if(i.getBlockY() == ly)
{
fmount.add(i);
}
}
double avx[] = new double[fmount.size()];
double avy[] = new double[fmount.size()];
double avz[] = new double[fmount.size()];
int c = 0;
for(BlockVector i : fmount)
{
avx[c] = i.getBlockX();
avy[c] = i.getBlockY();
avz[c] = i.getBlockZ();
c++;
}
mountHeight = avg(avy);
mount = new BlockVector(avg(avx), 0, avg(avz));
}
private int avg(double[] v)
{
double g = 0;
for(int i = 0; i < v.length; i++)
{
g += v[i];
}
return (int) Math.round(g / (double) v.length);
}
public void setCenteredHeight()
{
this.centeredHeight = true;
}
public int getW()
{
return w;
}
public int getH()
{
return h;
}
public int getD()
{
return d;
}
public GMap<BlockVector, MB> getSchematic()
{
return s;
}
@SuppressWarnings("deprecation")
public void read(InputStream in) throws IOException
{
GZIPInputStream gzi = new GZIPInputStream(in);
DataInputStream din = new DataInputStream(gzi);
w = din.readInt();
h = din.readInt();
d = din.readInt();
int l = din.readInt();
clear();
for(int i = 0; i < l; i++)
{
s.put(new BlockVector(din.readInt(), din.readInt(), din.readInt()), new MB(Material.getMaterial((int) din.readInt()), din.readInt()));
}
din.close();
}
@SuppressWarnings("deprecation")
public void write(OutputStream out) throws IOException
{
CustomOutputStream cos = new CustomOutputStream(out, 9);
DataOutputStream dos = new DataOutputStream(cos);
dos.writeInt(w);
dos.writeInt(h);
dos.writeInt(d);
dos.writeInt(s.size());
for(BlockVector i : s.keySet())
{
dos.writeInt(i.getBlockX());
dos.writeInt(i.getBlockY());
dos.writeInt(i.getBlockZ());
dos.writeInt(s.get(i).material.getId());
dos.writeInt(s.get(i).data);
}
dos.close();
}
public MB get(int x, int y, int z)
{
return s.get(new BlockVector(x, y, z));
}
public boolean has(int x, int y, int z)
{
return s.contains(new BlockVector(x, y, z));
}
public void put(int x, int y, int z, MB mb)
{
s.put(new BlockVector(x, y, z), mb);
}
public Schematic copy()
{
Schematic s = new Schematic(w, h, d);
s.fill(this.s);
s.centeredHeight = centeredHeight;
s.name = name;
return s;
}
public void clear()
{
s.clear();
}
public void fill(GMap<BlockVector, MB> b)
{
clear();
s.put(b);
}
public int sh(int g)
{
int m = (g / 2);
return g % 2 == 0 ? m : m + 1;
}
@SuppressWarnings("deprecation")
public void place(World source, int wx, int wy, int wz)
{
Location start = new Location(source, wx, wy, wz).clone().add(sh(w), sh(h) + 1, sh(d));
start.subtract(mount);
int highestY = source.getHighestBlockYAt(start);
if(start.getBlockY() + mountHeight > highestY)
{
start.subtract(0, start.getBlockY() + mountHeight - highestY, 0);
}
start.add(shift);
GMap<Location, MB> undo = new GMap<>();
for(BlockVector i : getSchematic().k())
{
MB b = getSchematic().get(i);
Location f = start.clone().add(i);
if(i.getBlockY() == mountHeight && f.clone().subtract(0, 1, 0).getBlock().isLiquid())
{
for(Location j : undo.k())
{
source.getBlockAt(j.getBlockX(), j.getBlockY(), j.getBlockZ()).setTypeIdAndData(undo.get(j).material.getId(), undo.get(j).data, false);
}
return;
}
if(b.material.equals(Material.SKULL))
{
continue;
}
try
{
undo.put(f, MB.of(f.getBlock().getType(), f.getBlock().getData()));
source.getBlockAt(f.getBlockX(), f.getBlockY(), f.getBlockZ()).setTypeIdAndData(b.material.getId(), b.data, false);
}
catch(Throwable e)
{
e.printStackTrace();
}
}
}
public static Schematic load(InputStream in) throws IOException
{
Schematic s = new Schematic(1, 1, 1);
s.read(in);
return s;
}
public static Schematic load(File f) throws IOException
{
Schematic s = new Schematic(1, 1, 1);
s.name = f.getName().replaceAll("\\Q.ish\\E", "");
FileInputStream fin = new FileInputStream(f);
s.read(fin);
return s;
}
public String getName()
{
return name;
}
public void rotate(Direction from, Direction to)
{
GMap<BlockVector, MB> g = s.copy();
s.clear();
for(BlockVector i : g.k())
{
MB mb = rotate(from, to, g.get(i));
s.put(VectorMath.rotate(from, to, i).toBlockVector(), mb);
}
name = name + "-rt" + to.name();
}
@SuppressWarnings("deprecation")
private MB rotate(Direction from, Direction to, MB mb)
{
Class<? extends MaterialData> cl = mb.material.getData();
if(cl.isAssignableFrom(Directional.class))
{
try
{
Directional d = (Directional) cl.getConstructor(int.class, byte.class).newInstance(mb.material.getId(), mb.data);
BlockFace f = d.getFacing();
Vector mod = new Vector(f.getModX(), f.getModY(), f.getModZ());
Vector modded = VectorMath.rotate(from, to, mod);
for(BlockFace i : BlockFace.values())
{
if(i.getModX() == modded.getBlockX() && i.getModY() == modded.getBlockY() && i.getModZ() == modded.getBlockZ())
{
d.setFacingDirection(i);
return new MB(mb.material, ((MaterialData) d).getData());
}
}
}
catch(Throwable e)
{
e.printStackTrace();
}
}
return mb;
}
public void computeFlag(String j)
{
try
{
if(j.startsWith("sink="))
{
int downshift = Integer.valueOf(j.split("\\Q=\\E")[1]);
shift.subtract(new Vector(0, downshift, 0));
}
}
catch(Throwable e)
{
L.f("Failed to compute flag '" + j + "'");
L.ex(e);
}
}
}

View File

@@ -0,0 +1,206 @@
package ninja.bytecode.iris.schematic;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;
import net.md_5.bungee.api.ChatColor;
import ninja.bytecode.iris.Iris;
import ninja.bytecode.iris.util.Direction;
import ninja.bytecode.shuriken.collections.GList;
import ninja.bytecode.shuriken.execution.TaskExecutor;
import ninja.bytecode.shuriken.execution.TaskExecutor.TaskGroup;
import ninja.bytecode.shuriken.format.F;
import ninja.bytecode.shuriken.io.IO;
import ninja.bytecode.shuriken.logging.L;
public class SchematicGroup
{
private GList<Schematic> schematics;
private GList<String> flags;
private String name;
private int priority;
public SchematicGroup(String name)
{
this.schematics = new GList<>();
this.flags = new GList<>();
priority = 0;
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public GList<Schematic> getSchematics()
{
return schematics;
}
public void setSchematics(GList<Schematic> schematics)
{
this.schematics = schematics;
}
public GList<String> getFlags()
{
return flags;
}
public void setFlags(GList<String> flags)
{
this.flags = flags;
}
public int getPriority()
{
return priority;
}
public void setPriority(int priority)
{
this.priority = priority;
}
public int size()
{
return getSchematics().size();
}
public static SchematicGroup load(String string)
{
File folder = Iris.loadFolder(string);
if(folder != null)
{
SchematicGroup g = new SchematicGroup(string);
for(File i : folder.listFiles())
{
if(i.getName().endsWith(".ifl"))
{
try
{
g.flags.add(IO.readAll(i).split("\\Q\n\\E"));
}
catch(IOException e)
{
L.ex(e);
}
}
if(i.getName().endsWith(".ish"))
{
try
{
Schematic s = Schematic.load(i);
g.getSchematics().add(s);
}
catch(IOException e)
{
L.f("Cannot load Schematic: " + string + "/" + i.getName());
L.ex(e);
}
}
}
return g;
}
return null;
}
public void processVariants()
{
GList<Schematic> inject = new GList<>();
String x = Thread.currentThread().getName();
ReentrantLock rr = new ReentrantLock();
TaskExecutor ex = new TaskExecutor(Iris.settings.performance.compilerThreads, Iris.settings.performance.compilerPriority, x + "/Subroutine ");
TaskGroup gg = ex.startWork();
for(Schematic i : getSchematics())
{
for(Direction j : new Direction[] {Direction.S, Direction.E, Direction.W})
{
Schematic cp = i.copy();
gg.queue(() -> {
Schematic f = cp;
f.rotate(Direction.N, j);
rr.lock();
inject.add(f);
rr.unlock();
});
}
}
gg.execute();
gg = ex.startWork();
getSchematics().add(inject);
for(Schematic i : getSchematics())
{
gg.queue(() -> {
i.computeMountShift();
for(String j : flags)
{
i.computeFlag(j);
}
});
}
gg.execute();
ex.close();
L.i(ChatColor.LIGHT_PURPLE + "Processed " + ChatColor.WHITE + F.f(schematics.size()) + ChatColor.LIGHT_PURPLE + " Schematics in " + ChatColor.WHITE + name);
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((flags == null) ? 0 : flags.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + priority;
return result;
}
@Override
public boolean equals(Object obj)
{
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
SchematicGroup other = (SchematicGroup) obj;
if(flags == null)
{
if(other.flags != null)
return false;
}
else if(!flags.equals(other.flags))
return false;
if(name == null)
{
if(other.name != null)
return false;
}
else if(!name.equals(other.name))
return false;
if(priority != other.priority)
return false;
return true;
}
}