Updated orb to use new FillerSets

This commit is contained in:
Francesco Macagno 2015-08-23 02:32:12 -07:00
parent 44c6aa4dca
commit 39759655f7

View file

@ -1,11 +1,8 @@
package cr0s.warpdrive.conf.structures;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.world.World;
import org.w3c.dom.Document;
@ -13,9 +10,10 @@ import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.conf.filler.FillerManager.FillerCategory;
import cr0s.warpdrive.conf.InvalidXmlException;
import cr0s.warpdrive.conf.MetaBlock;
import cr0s.warpdrive.conf.XmlRepresentable;
import cr0s.warpdrive.conf.filler.FillerSet;
import cr0s.warpdrive.world.EntitySphereGen;
public abstract class Orb extends DeployableStructure implements XmlRepresentable {
@ -42,15 +40,27 @@ public abstract class Orb extends DeployableStructure implements XmlRepresentabl
this.name = e.getAttribute("name");
ArrayList<OrbShell> newShells = new ArrayList<OrbShell>();
int totalThickness = 0;
NodeList shells = e.getElementsByTagName("shell");
for (int i = 0; i < shells.getLength(); i++) {
Element tmp = (Element) shells.item(i);
OrbShell shell = new OrbShell();
shell.loadFromXmlElement(tmp);
totalThickness += shell.thickness;
newShells.add(shell);
setShell(shell.getRadiusInner(), shell.getRadiusOuter(), shell);
}
int index = 0;
shellRelative = new OrbShell[totalThickness];
for (OrbShell shell : newShells) {
for (int i = 0; i < shell.thickness; i++)
shellRelative[index++] = shell;
}
@ -79,67 +89,37 @@ public abstract class Orb extends DeployableStructure implements XmlRepresentabl
return false;
}
public void setShell(int inner, int outer, OrbShell shell) {
for (; inner <= outer; inner++)
shellRelative[inner] = shell;
}
public OrbShell getShellForRadius(int r) {
return shellRelative[r];
}
public Block getBlockForRadius(Random rand, int r) {
return shellRelative[r].getNextBlock(rand);
public MetaBlock getBlockForRadius(Random rand, int r) {
return shellRelative[r].getRandomBlock(rand);
}
public class OrbShell implements XmlRepresentable {
public class OrbShell extends FillerSet {
private int radiusInner, radiusOuter;
private HashMap<String, Integer> compositionBlocks;
private Block[] blockWeights;
private String name;
private int totalWeight;
private double genericOreChance;
public OrbShell() {
compositionBlocks = new HashMap<String, Integer>();
totalWeight = 1;
blockWeights = new Block[0];
genericOreChance = 0;
}
private int thickness;
/**
* Gets a block randomly chosen according to the setup of this OrbShell
*
* @param r
* The Random to use
* @return A Non-null block instance
* @return the thickness
*/
public Block getNextBlock(Random r) {
return blockWeights[r.nextInt(totalWeight)];
public int getThickness() {
return thickness;
}
public int getRadiusInner() {
return radiusInner;
/**
* @param thickness
* the thickness to set
*/
public void setThickness(int thickness) {
this.thickness = thickness;
}
public void setRadiusInner(int radiusInner) {
this.radiusInner = radiusInner;
}
public int getRadiusOuter() {
return radiusOuter;
}
public void setRadiusOuter(int radiusOuter) {
this.radiusOuter = radiusOuter;
public OrbShell() {
super("");
}
@Override
@ -147,120 +127,18 @@ public abstract class Orb extends DeployableStructure implements XmlRepresentabl
WarpDrive.logger.info("Loading shell " + e.getAttribute("name"));
name = e.getAttribute("name");
super.loadFromXmlElement(e);
totalWeight = 0;
thickness = Integer.parseInt(e.getAttribute("thicknessMin"));
try {
radiusInner = Integer.parseInt(e.getAttribute("radiusInner"));
radiusOuter = Integer.parseInt(e.getAttribute("radiusOuter"));
if (radiusInner < 1 || radiusInner > radiusOuter)
throw new InvalidXmlException("Orb creation arguments are incorrect!");
NodeList compBlocks = e.getElementsByTagName("block");
for (int i = 0; i < compBlocks.getLength(); i++) {
Element tmp = (Element) compBlocks.item(i);
if (!tmp.hasAttribute("weight"))
throw new InvalidXmlException("Shell is missing weight at place " + i + "!");
String blockName = tmp.getTextContent();
if (blockName.isEmpty())
throw new InvalidXmlException("Shell is missing block name at place " + i + "!");
int tmpWeight = Integer.parseInt(tmp.getAttribute("weight"));
if (tmpWeight < 1)
throw new InvalidXmlException("Weight is less than 1 at place " + i + "!");
if (Block.getBlockFromName(blockName) == null)
throw new InvalidXmlException("Shell has unknown block at place " + i + "!");
totalWeight += tmpWeight;
compositionBlocks.put(blockName, tmpWeight);
}
HashMap<String, Integer> categoryTemp = new HashMap<String, Integer>();
NodeList oreCategories = e.getElementsByTagName("OreCategory");
for (int i = 0; i < oreCategories.getLength(); i++) {
Element cat = (Element) oreCategories.item(i);
if (!cat.hasAttribute("weight"))
throw new InvalidXmlException("OreCategory " + i + " must have a weight");
if (!cat.hasAttribute("category"))
throw new InvalidXmlException("OreCategory " + i + " must have a category");
int weight = Integer.parseInt(cat.getAttribute("weight"));
String oreSpawnCategory = cat.getAttribute("category");
if (FillerCategory.getOreSpawnValue(oreSpawnCategory) == null)
throw new InvalidXmlException("Shell has an invalid ores category!");
if (weight < 1)
throw new InvalidXmlException("Shell has an invalid ores probability!");
totalWeight += weight;
if(categoryTemp.containsKey(oreSpawnCategory))
throw new InvalidXmlException("OreCategory used twice in same shell!");
categoryTemp.put(oreSpawnCategory, weight);
}
int index = 0;
blockWeights = new Block[totalWeight];
for (Entry<String, Integer> compBlock : compositionBlocks.entrySet()) {
Block bl = Block.getBlockFromName(compBlock.getKey());
for (int i = 0; i < compBlock.getValue(); i++)
blockWeights[index++] = bl;
}
for(Entry<String, Integer> category : categoryTemp.entrySet()) {
FillerCategory value = FillerCategory.getOreSpawnValue(category.getKey());
for (Entry<Block, Integer> compBlock : value.getBlocks()) {
int reduced = category.getValue() * compBlock.getValue() / value.getTotalWeight();
for (int i = 0; i < reduced; i++) {
blockWeights[index++] = compBlock.getKey();
}
}
}
} catch (NumberFormatException ex) {
throw new InvalidXmlException("Invalid integer in shell " + name + "!");
}
if (totalWeight < 1)
throw new InvalidXmlException("At least one kind of block must be defined!");
//TODO: Implement random thickness
}
@Override
public void saveToXmlElement(Element e, Document d) {
WarpDrive.logger.info("Saving shell " + e.getAttribute("name"));
e.setAttribute("name", name);
e.setAttribute("radiusInner", "" + radiusInner);
e.setAttribute("radiusOuter", "" + radiusOuter);
for (Entry<String, Integer> compBlock : compositionBlocks.entrySet()) {
Element child = d.createElement("CompositionBlock");
child.setAttribute("weight", compBlock.getValue() + "");
child.setAttribute("block", compBlock.getKey());
e.appendChild(child);
}
//Not needed
}
}