Here's where most of the work happens. It also contains lots of untidy code which is a consequence of making it up as I go along: stub functions which are unimplemented, implemented but unused functions which don't do anything useful, and references to libraries that don't actually get used. No matter; the bits which do get used do what I need them to.
String item_name: The name of the base item (broadsword, Dwarven theodolite, arrow, etc.)
Double base_cost: The cost of the unembellished item, used as the basis for CF-related price calculations.
Double base_weight: The unembellished weight of the item.
Integer quantity: Usually 1, but ammunition and bulk goods often have a larger quantity
String curse_name: Defaults to an empty string. An item may have no more than one curse.
Double curse_cf: The CF of the curse, which applies to the cost of enchantments.
String unit. The unit of measurement. Defaults to "each," but may be a unit of weight, length, etc.
String bulk_type: The type of bulk storage container the item requires. "W" for liquids, "D" for dry goods like spices, or "N" for items which don't require containers, like bales of hides.
ArrayList<String> attributes: Attributes indicate what kinds of embellishments and other treatments an item may have. For example, a soft item ("S" is an attribute) might have resist dying or embroidery as an embellishment but not relief carving or silver plating, while a hard item ("H" is an attribute) would be the opposite.
ArrayList<Embellishment> embellishments: A list of embellishments applied to the item.
ArrayList<Enchantment> enchantments: A list of enchantments applied to the item.
ArrayList<Gem> gems: a list of gemstones embedded in the object. Gems are a bit more complicated than other embellishments, so they get their own object.
Item container: A container, if necessary. Containers are themselves items which can have embellishments of their own.
String motif_name: A brief description of the item's decorative motif. Defaults to an empty string, since not all items will have a motif.
This is really the key class, representing a single valuable item, which may have multiple embellishments, enchantments, and other frippery about it. Most of what goes on happens in the Item() constructor. First, the getRandomItem() method opens up the list of items (Items.xml), checks the length, gets a random entry within that range, and takes it apart to populate some of the key variables with data.
Certain attributes are treated specially: ammo, jewelry, and bulk goods get a variable quantity. For jewelry, the quantity is invisible; it's effectively a multiplier for heavier rings, larger bracelets, and so on. Bulk goods get additional treatment. There's a stripped-down constructor: Item(String c_type, Double wt_cap). Given a containment type and a weight capacity, it'll get a container out of a special items list (Containers.xml, which contains additional properties not relevant to most items) and make that the container property for the item.
If the item isn't bulk goods, it'll get embellishments and is a candidate for enchantments. After a number of embellishments is rolled up, it opens up the list of embellishments (embellishments.xml) and starts picking some at random. The embellishment's appropriateness is compared to the list of item attributes and, if it's suitable, is added to the list. The loop continues for a while to try to get the right number of embellishments, but it'll bail out if it takes too long. The "Jeweled" embellishment code briefly diverts the process into the machinery to add a randomly selected and sized new gem to the list. After the embellishments are selected, their properties are checked to see if the item needs a decorative motif, which is also selected randomly.
The enchantment section follows. There's a chance of an item having enchantments. If it does, the machinery for adding enchantments is essentially identical to that adding embellishments, with a few specific adjustments. For one thing, an enchanted item has a chance of getting a supernatural embellishment (picked at random from SupernaturalEmbellishments.xml). While the source of the embellishment may be mystical, it acts just like any other embellishment, so it gets added to the embellishment list rather than the enchantment list. There's also a chance of a curse, picked from Curses.xml.
Once created, the key function is report(), which sends back a full description of the item, summarizing the complex of embellishments, enchantments, and so on. The function starts by constructing an expression for the quantity and unit of measurement if necessary and the name of the item itself. Then it constructs a summary list of embellishments and, if necessary, motif, followed by enchantments, curses, container (the container's description is a stripped down version of the full report; it excludes cost and weight, since those will be included in the grand total), and finally the total cost (computed from item base cost, embellishment CFs, enchantment costs, quantity, and container cost) and weight.
package hoardomatic;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Random;
import java.util.Set;
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Item {
private String item_name;
private Double base_cost;
private Double base_weight;
private Integer quantity = 1;
private String curse_name = "";
private Double curse_cf = 0.0;
private String unit = "";
private String bulk_type;
private ArrayList<String> attributes = new ArrayList<String>(); // item attributes
private ArrayList<Embellishment> embellishments = new ArrayList<Embellishment>(); // decorative and functional embellishments
private ArrayList<Enchantment> enchantments = new ArrayList<Enchantment>(); // enchantments
private ArrayList<Gem> gems = new ArrayList<Gem>();
private Item container = null;
private String motif_name = "";
// shiny, iron, organic, plate, mail, scale, edged, impaling
public Item() {
// TODO Auto-generated constructor stub
getRandomItem();
curse_cf = 0.0;
curse_name = "";
if (isAttribute("AMMO")){
// if it's ammo, make it a bunch of them
quantity = dieRoll(1, 20);
}
if (isAttribute("Jewelry")){
// if it's jewelry, maybe make it big
quantity = dieRoll(1, 6);
}
if (isAttribute("BULK")){
// if it's bulk goods, make it a bunch of them
quantity = dieRoll(1, 6);
// add a container
//get type from Bulk Goods table.
getBulkAttributes(getName());
String the_type = bulk_type;
if (!(unit.contentEquals("Bale")) & !(the_type.contentEquals("N"))){
container = new Item(the_type, getFinalWeight());
}
// end container
} else {
// apply random number of embellishments
Integer rounds = 0; // this prevents excessive looping
Integer numEmbs = dieRoll(1, 3);
while (embellishments.size() < numEmbs & rounds < 40 ){
Node anEmbellishment = null;
rounds = rounds + 1;
while ( anEmbellishment == null){
anEmbellishment = getRandomNodeFromTable("Embellishments.xml");
}
Embellishment newEmb = new Embellishment(anEmbellishment);
if (newEmb.getEmbellishmentName().contentEquals("Jeweled")){
Gem newgem = new Gem();
gems.add(newgem);
} else {
String e_code = newEmb.getEmbCode();
boolean already_got = hasEmbellishment(newEmb.getEmbellishmentName());
String[] e_codes = e_code.split(";");
boolean acceptable_attribute = false;
for (int e_code_count = 0; e_code_count < e_codes.length; e_code_count++){
if(isAttribute(e_codes[e_code_count])){
acceptable_attribute = true;
}
}
if (isAttribute(e_code) | (item_name.toLowerCase().contains(e_code.toLowerCase()) & e_code.length() > 3 ) ){
if (! already_got){
embellishments.add(newEmb);
}
}
}
}
// add a motif, if needed
// go through the embellishments and see if they require or at least support a motif
if(embellishments.size() > 0){
String need_motif = "N";
for (Embellishment eLine : embellishments) {
String emb_motif = eLine.getMotifOption();
if (emb_motif.contentEquals("Y")){
need_motif = "Y";
}else if (emb_motif.contentEquals("P") & !(need_motif.contentEquals("Y")) ){
need_motif = "P";
}
}
// if they support but don't require a motif, flip a coin
if (need_motif.contentEquals("P") ){
int flip = dieRoll(1,2);
if (flip == 1){
need_motif = "Y";
}
}
// so if we need a motif, add one
if (need_motif.contentEquals("Y")){
Node the_motif_node = getRandomNodeFromTable("Motifs.xml");
NodeList tempNodes = the_motif_node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("Motif")) {
motif_name = element.getTextContent();
}
}
}
}
} // end embellishments
// apply random number of enchantments
rounds = 0;
Integer hasEnch = dieRoll(1, 10);
if (hasEnch > 5){
Integer numEnch = dieRoll(1, 2);
while (enchantments.size() < numEnch & rounds < 20 ){
Node anEnchantment = null;
rounds = rounds + 1;
while ( anEnchantment == null){
anEnchantment = getRandomNodeFromTable("Enchantments.xml");
}
Enchantment newEnch = new Enchantment(anEnchantment);
String e_code = newEnch.getenchCode();
boolean already_got = hasEmbellishment(newEnch.getEnchantmentName());
if (isAttribute(e_code) | e_code.contentEquals("ALL") ){
if (! already_got){
enchantments.add(newEnch);
}
}
}
// check for supernatural embellishments
Integer hasSE = dieRoll(1,10);
if (hasSE > 5) {
Node anEmbellishment = getRandomNodeFromTable("SupernaturalEmbellishments.xml");
Embellishment newEmb = new Embellishment(anEmbellishment);
String se_name = newEmb.getEmbellishmentName();
newEmb.setEmbellishmentName(se_name + " (supernatural effect)");
embellishments.add(newEmb);
}
// check for curses
Integer hasC = dieRoll(1,10);
if (hasC > 9) {
Node aCurse = getRandomNodeFromTable("Curses.xml");
NodeList tempNodes = aCurse.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("Curse")) {
curse_name = element.getTextContent();
}
if (element.getNodeName().contentEquals("CF")) {
curse_cf = Double.parseDouble(element.getTextContent());
}
}
}
}
// end curses
}
// end enchantments
}
}
public String report(){
String full_report = "";
// name and, if needed, quantity
if(quantity > 1 & !(isAttribute("Jewelry")) ){
full_report = full_report + quantity.toString() + " ";
if (!(unit.contentEquals("Each"))){
full_report = full_report + unit + " ";
}
}
full_report = full_report + item_name + ". ";
String motif_exp = "";
if (!(motif_name.contentEquals("") )){
motif_exp = "; decorated with " + motif_name + " motif";
}
// embellishment list
if(embellishments.size() > 0){
int rounds = 1;
for (Embellishment eLine : embellishments) {
full_report = full_report + eLine.getEmbellishmentName();
rounds = rounds + 1;
if (rounds <= embellishments.size()){
full_report = full_report + ", ";
}else{
full_report = full_report + motif_exp + ". ";
}
}
} // end embellishments
// gem list
if(gems.size() > 0){
int rounds = 1;
full_report = full_report + "Set with ";
for (Gem eLine : gems) {
full_report = full_report + eLine.getGemWt().toString() + " ct. " + eLine.getGemName();
rounds = rounds + 1;
if (rounds <= gems.size()){
full_report = full_report + ", ";
}else{
full_report = full_report + ". ";
}
}
} // end gems
// enchantment list
if(enchantments.size() > 0){
int rounds = 1;
full_report = full_report + "Enchanted with ";
for (Enchantment eLine : enchantments) {
full_report = full_report + eLine.getEnchantmentName() ;
rounds = rounds + 1;
if (rounds <= enchantments.size()){
full_report = full_report + ", ";
}else{
full_report = full_report + ". ";
}
}
if(!curse_name.contentEquals("")){
full_report = full_report + "Cursed with " + curse_name + ". ";
}
} // end enchantments
if(!(container == null)){
full_report = full_report + "Contained in " + container.report_short();
}
Double f_cst = getFinalCost();
Double f_wt = getFinalWeight();
DecimalFormat df = new DecimalFormat("#.00");
NumberFormat fmt = NumberFormat.getCurrencyInstance();
full_report = full_report + fmt.format(f_cst) + ", ";
full_report = full_report + df.format(f_wt);
if (f_wt > 1){
full_report = full_report + " lbs.";
}else{
full_report = full_report + " lb.";
}
return full_report;
}
public String report_short(){
String full_report = "";
// name and, if needed, quantity
if(quantity > 1){
full_report = full_report + quantity.toString() + " x ";
}
full_report = full_report + item_name + ". ";
String motif_exp = "";
if (!(motif_name.contentEquals("") )){
motif_exp = "; decorated with " + motif_name + " motif";
}
// embellishment list
if(embellishments.size() > 0){
int rounds = 1;
for (Embellishment eLine : embellishments) {
full_report = full_report + eLine.getEmbellishmentName();
rounds = rounds + 1;
if (rounds <= embellishments.size()){
full_report = full_report + ", ";
}else{
full_report = full_report + motif_exp + ". ";
}
}
} // end embellishments
// enchantment list
if(enchantments.size() > 0){
int rounds = 1;
full_report = full_report + "Enchanted with ";
for (Enchantment eLine : enchantments) {
full_report = full_report + eLine.getEnchantmentName() ;
rounds = rounds + 1;
if (rounds <= enchantments.size()){
full_report = full_report + ", ";
}else{
full_report = full_report + ". ";
}
}
if(!curse_name.contentEquals("")){
full_report = full_report + "Cursed with " + curse_name + ". ";
}
} // end enchantments
return full_report;
}
public Item(String specific_item) {
// TODO Auto-generated constructor stub
getNamedItem(specific_item);
//
}
public Item(double low_val, double high_val) {
// get random item
getRandomItem();
}
public Double getFinalCost(){
Double final_cost = getBaseCost();
final_cost = final_cost * (1 + getTotalCF());
final_cost = final_cost + getEnchantmentCosts();
// gem list
if(gems.size() > 0){
for (Gem eLine : gems) {
final_cost = final_cost + eLine.getGemCost();
}
} // end gems
if (!(container == null)){
final_cost = final_cost + container.getFinalCost();
}
return final_cost;
}
public boolean hasEmbellishment(String embName){
boolean return_it = false;
if (embellishments.size() > 0){
for (Embellishment eLine : embellishments) {
String this_nom = eLine.getEmbellishmentName();
String embName_short = "";
if (embName.indexOf("(") > 0){
embName_short = embName.substring(0, embName.indexOf("("));
}else{
embName_short = embName;
}
if (this_nom.contains(embName_short)){
return_it = true;
}
}
}
return return_it;
}
public Double getTotalCF(){
Double total_cf = 0.0;
if (embellishments.size() > 0){
for (Embellishment eLine : embellishments) {
total_cf = total_cf + eLine.getCf();
}
}
return total_cf;
}
public Double getEnchantmentCosts(){
Double total_costs = 0.0;
if (enchantments.size() > 0){
for (Enchantment eLine : enchantments) {
total_costs = total_costs + eLine.getCost() ;
}
}
total_costs = total_costs * (1+ curse_cf);
return total_costs;
}
public void getRandomItem() {
try {
InputStream items = new FileInputStream(new File("Items.xml"));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(items);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
Integer itemlistlen = nodes.getLength();
Integer pickItm = dieRoll(0, itemlistlen-1);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
NamedNodeMap attrs = node.getAttributes();
Node sn = attrs.getNamedItem("name");
if (i == pickItm) {
NodeList tempNodes = node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("name")) {
item_name = element.getTextContent();
}
if (element.getNodeName().contentEquals("Wt")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_weight = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("Cost")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_cost = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("attributes")) {
attributes.add(element.getAttribute("value").toString()) ;
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getNamedItem(String the_item) {
try {
InputStream items = new FileInputStream(new File("Items.xml"));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(items);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
NamedNodeMap attrs = node.getAttributes();
Node sn = attrs.getNamedItem("name");
if (sn.getTextContent().contentEquals(the_item)) {
NodeList tempNodes = node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("name")) {
item_name = element.getTextContent();
}
if (element.getNodeName().contentEquals("Wt")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_weight = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("Cost")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_cost = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("attributes")) {
attributes.add(element.getAttribute("value").toString()) ;
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Double getFinalWeight(){
Double running_weight = base_weight;
if (embellishments.size() > 0){
for (Embellishment eLine : embellishments) {
running_weight = running_weight * eLine.getWtMod();
}
}
running_weight = running_weight * quantity;
if (!(container == null)){
running_weight = running_weight + container.getFinalWeight();
}
return running_weight;
}
public String getName(){
return item_name;
}
public Integer getQuantity(){
return quantity;
}
public Double getWeight(){
// do a bunch of math
return base_weight;
}
public Double getCost(){
return base_cost;
}
public Double getBaseWeight(){
return base_weight;
}
public Double getBaseCost(){
return base_cost;
}
public boolean isAttribute(String test_val){
boolean p = false;
for (String aLine : attributes) {
if (aLine.contentEquals(test_val)) {
p = true;
}
}
return p;
}
public String getEmbellishments(){
return "";
}
public boolean isEmbellishment(String test_val){
return true;
}
public String getEnchantments(){
return "";
}
public boolean isEnchantment(String test_val){
return true;
}
public String getContainer(){
return "";
}
public void addEmbellishment(){}
public void addEnchantment(){}
public void addContainer(){}
public ArrayList<Gem> getGems(){
return gems;
}
public Node getRandomNodeFromTable(String fname) {
Node return_it;
return_it = null;
try {
InputStream items = new FileInputStream(new File(fname));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(items);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
Integer itemlistlen = nodes.getLength();
Integer pickItm = dieRoll(0, itemlistlen - 1);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (i == pickItm) {
return_it = node;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return return_it;
}
public String getBulkType(String bulk_name){
String bulk_type = "";
Node bulk_node = getNamedNodeFromTable("BulkGoods.xml", bulk_name);
NodeList tempNodes = bulk_node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("ContainmentType")) {
bulk_type = element.getTextContent();
}
}
}
return bulk_type;
}
public void getBulkAttributes(String bulk_name){
Node bulk_node = getNamedNodeFromTable("BulkGoods.xml", bulk_name);
NodeList tempNodes = bulk_node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("ContainmentType")) {
bulk_type = element.getTextContent();
}
if (element.getNodeName().contentEquals("Unit")) {
unit = element.getTextContent();
}
}
}
}
public Node getNamedNodeFromTable(String fname, String get_item) {
Node return_it;
return_it = null;
try {
InputStream items = new FileInputStream(new File(fname));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(items);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
NamedNodeMap attrs = node.getAttributes();
Node sn = attrs.getNamedItem("name");
if (sn.getTextContent().contentEquals(get_item)) {
return_it = node;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return return_it;
}
private int dieRoll(Integer min, Integer max) {
Random r = new Random();
int inroll = r.nextInt(max - min + 1) + min;
return inroll;
}
// container stuff
public Item(String c_type, Double wt_cap) {
// This creates a container item of a given type and capacity
getSizedContainer(c_type, wt_cap);
// apply random number of embellishments
Integer rounds = 0;
Integer numEmbs = dieRoll(1, 2);
while (embellishments.size() < numEmbs & rounds < 20 ){
Node anEmbellishment = null;
rounds = rounds + 1;
while ( anEmbellishment == null){
anEmbellishment = getRandomNodeFromTable("Embellishments.xml");
}
Embellishment newEmb = new Embellishment(anEmbellishment);
if (newEmb.getEmbellishmentName().contentEquals("Jeweled")){
Gem newgem = new Gem();
gems.add(newgem);
} else {
String e_code = newEmb.getEmbCode();
boolean already_got = hasEmbellishment(newEmb.getEmbellishmentName());
if (isAttribute(e_code) | (item_name.toLowerCase().contains(e_code.toLowerCase()) & e_code.length() > 3 ) ){
if (! already_got){
embellishments.add(newEmb);
}
}
}
}
// add a motif, if needed
// go through the embellishments and see if they require or at least support a motif
if(embellishments.size() > 0){
String need_motif = "N";
for (Embellishment eLine : embellishments) {
String emb_motif = eLine.getMotifOption();
if (emb_motif.contentEquals("Y")){
need_motif = "Y";
}else if (emb_motif.contentEquals("P") & !(need_motif.contentEquals("Y")) ){
need_motif = "P";
}
}
// if they support but don't require a motif, flip a coin
if (need_motif.contentEquals("P") ){
int flip = dieRoll(1,2);
if (flip == 1){
need_motif = "Y";
}
}
// so if we need a motif, add one
if (need_motif.contentEquals("Y")){
Node the_motif_node = getRandomNodeFromTable("Motifs.xml");
NodeList tempNodes = the_motif_node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("Motif")) {
motif_name = element.getTextContent();
}
}
}
}
} // end embellishments
}
public void getSizedContainer(String substance_type, Double wt_capacity) {
Double ct_cap = 0.0;
String ct_type = "";
try {
InputStream items = new FileInputStream(new File("Containers.xml"));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(items);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
NamedNodeMap attrs = node.getAttributes();
Node sn = attrs.getNamedItem("name");
NodeList tempNodes = node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("ContainmentType")) {
ct_type = element.getTextContent();
}
if (element.getNodeName().contentEquals("Capacity")) {
String base_read = element.getTextContent();
ct_cap = Double.parseDouble(element.getTextContent());
}
if (element.getNodeName().contentEquals("name")) {
item_name = element.getTextContent();
}
if (element.getNodeName().contentEquals("Wt")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_weight = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("Cost")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_cost = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("attributes")) {
attributes.add(element.getAttribute("value").toString()) ;
}
}
}
// if the containment type is good and the container will hold enough, break the loop
if (ct_type.contentEquals(substance_type) & ct_cap >= wt_capacity){
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// end container stuff
}
Item
String item_name: The name of the base item (broadsword, Dwarven theodolite, arrow, etc.)
Double base_cost: The cost of the unembellished item, used as the basis for CF-related price calculations.
Double base_weight: The unembellished weight of the item.
Integer quantity: Usually 1, but ammunition and bulk goods often have a larger quantity
String curse_name: Defaults to an empty string. An item may have no more than one curse.
Double curse_cf: The CF of the curse, which applies to the cost of enchantments.
String unit. The unit of measurement. Defaults to "each," but may be a unit of weight, length, etc.
String bulk_type: The type of bulk storage container the item requires. "W" for liquids, "D" for dry goods like spices, or "N" for items which don't require containers, like bales of hides.
ArrayList<String> attributes: Attributes indicate what kinds of embellishments and other treatments an item may have. For example, a soft item ("S" is an attribute) might have resist dying or embroidery as an embellishment but not relief carving or silver plating, while a hard item ("H" is an attribute) would be the opposite.
ArrayList<Embellishment> embellishments: A list of embellishments applied to the item.
ArrayList<Enchantment> enchantments: A list of enchantments applied to the item.
ArrayList<Gem> gems: a list of gemstones embedded in the object. Gems are a bit more complicated than other embellishments, so they get their own object.
Item container: A container, if necessary. Containers are themselves items which can have embellishments of their own.
String motif_name: A brief description of the item's decorative motif. Defaults to an empty string, since not all items will have a motif.
This is really the key class, representing a single valuable item, which may have multiple embellishments, enchantments, and other frippery about it. Most of what goes on happens in the Item() constructor. First, the getRandomItem() method opens up the list of items (Items.xml), checks the length, gets a random entry within that range, and takes it apart to populate some of the key variables with data.
Certain attributes are treated specially: ammo, jewelry, and bulk goods get a variable quantity. For jewelry, the quantity is invisible; it's effectively a multiplier for heavier rings, larger bracelets, and so on. Bulk goods get additional treatment. There's a stripped-down constructor: Item(String c_type, Double wt_cap). Given a containment type and a weight capacity, it'll get a container out of a special items list (Containers.xml, which contains additional properties not relevant to most items) and make that the container property for the item.
If the item isn't bulk goods, it'll get embellishments and is a candidate for enchantments. After a number of embellishments is rolled up, it opens up the list of embellishments (embellishments.xml) and starts picking some at random. The embellishment's appropriateness is compared to the list of item attributes and, if it's suitable, is added to the list. The loop continues for a while to try to get the right number of embellishments, but it'll bail out if it takes too long. The "Jeweled" embellishment code briefly diverts the process into the machinery to add a randomly selected and sized new gem to the list. After the embellishments are selected, their properties are checked to see if the item needs a decorative motif, which is also selected randomly.
The enchantment section follows. There's a chance of an item having enchantments. If it does, the machinery for adding enchantments is essentially identical to that adding embellishments, with a few specific adjustments. For one thing, an enchanted item has a chance of getting a supernatural embellishment (picked at random from SupernaturalEmbellishments.xml). While the source of the embellishment may be mystical, it acts just like any other embellishment, so it gets added to the embellishment list rather than the enchantment list. There's also a chance of a curse, picked from Curses.xml.
Once created, the key function is report(), which sends back a full description of the item, summarizing the complex of embellishments, enchantments, and so on. The function starts by constructing an expression for the quantity and unit of measurement if necessary and the name of the item itself. Then it constructs a summary list of embellishments and, if necessary, motif, followed by enchantments, curses, container (the container's description is a stripped down version of the full report; it excludes cost and weight, since those will be included in the grand total), and finally the total cost (computed from item base cost, embellishment CFs, enchantment costs, quantity, and container cost) and weight.
package hoardomatic;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Random;
import java.util.Set;
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Item {
private String item_name;
private Double base_cost;
private Double base_weight;
private Integer quantity = 1;
private String curse_name = "";
private Double curse_cf = 0.0;
private String unit = "";
private String bulk_type;
private ArrayList<String> attributes = new ArrayList<String>(); // item attributes
private ArrayList<Embellishment> embellishments = new ArrayList<Embellishment>(); // decorative and functional embellishments
private ArrayList<Enchantment> enchantments = new ArrayList<Enchantment>(); // enchantments
private ArrayList<Gem> gems = new ArrayList<Gem>();
private Item container = null;
private String motif_name = "";
// shiny, iron, organic, plate, mail, scale, edged, impaling
public Item() {
// TODO Auto-generated constructor stub
getRandomItem();
curse_cf = 0.0;
curse_name = "";
if (isAttribute("AMMO")){
// if it's ammo, make it a bunch of them
quantity = dieRoll(1, 20);
}
if (isAttribute("Jewelry")){
// if it's jewelry, maybe make it big
quantity = dieRoll(1, 6);
}
if (isAttribute("BULK")){
// if it's bulk goods, make it a bunch of them
quantity = dieRoll(1, 6);
// add a container
//get type from Bulk Goods table.
getBulkAttributes(getName());
String the_type = bulk_type;
if (!(unit.contentEquals("Bale")) & !(the_type.contentEquals("N"))){
container = new Item(the_type, getFinalWeight());
}
// end container
} else {
// apply random number of embellishments
Integer rounds = 0; // this prevents excessive looping
Integer numEmbs = dieRoll(1, 3);
while (embellishments.size() < numEmbs & rounds < 40 ){
Node anEmbellishment = null;
rounds = rounds + 1;
while ( anEmbellishment == null){
anEmbellishment = getRandomNodeFromTable("Embellishments.xml");
}
Embellishment newEmb = new Embellishment(anEmbellishment);
if (newEmb.getEmbellishmentName().contentEquals("Jeweled")){
Gem newgem = new Gem();
gems.add(newgem);
} else {
String e_code = newEmb.getEmbCode();
boolean already_got = hasEmbellishment(newEmb.getEmbellishmentName());
String[] e_codes = e_code.split(";");
boolean acceptable_attribute = false;
for (int e_code_count = 0; e_code_count < e_codes.length; e_code_count++){
if(isAttribute(e_codes[e_code_count])){
acceptable_attribute = true;
}
}
if (isAttribute(e_code) | (item_name.toLowerCase().contains(e_code.toLowerCase()) & e_code.length() > 3 ) ){
if (! already_got){
embellishments.add(newEmb);
}
}
}
}
// add a motif, if needed
// go through the embellishments and see if they require or at least support a motif
if(embellishments.size() > 0){
String need_motif = "N";
for (Embellishment eLine : embellishments) {
String emb_motif = eLine.getMotifOption();
if (emb_motif.contentEquals("Y")){
need_motif = "Y";
}else if (emb_motif.contentEquals("P") & !(need_motif.contentEquals("Y")) ){
need_motif = "P";
}
}
// if they support but don't require a motif, flip a coin
if (need_motif.contentEquals("P") ){
int flip = dieRoll(1,2);
if (flip == 1){
need_motif = "Y";
}
}
// so if we need a motif, add one
if (need_motif.contentEquals("Y")){
Node the_motif_node = getRandomNodeFromTable("Motifs.xml");
NodeList tempNodes = the_motif_node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("Motif")) {
motif_name = element.getTextContent();
}
}
}
}
} // end embellishments
// apply random number of enchantments
rounds = 0;
Integer hasEnch = dieRoll(1, 10);
if (hasEnch > 5){
Integer numEnch = dieRoll(1, 2);
while (enchantments.size() < numEnch & rounds < 20 ){
Node anEnchantment = null;
rounds = rounds + 1;
while ( anEnchantment == null){
anEnchantment = getRandomNodeFromTable("Enchantments.xml");
}
Enchantment newEnch = new Enchantment(anEnchantment);
String e_code = newEnch.getenchCode();
boolean already_got = hasEmbellishment(newEnch.getEnchantmentName());
if (isAttribute(e_code) | e_code.contentEquals("ALL") ){
if (! already_got){
enchantments.add(newEnch);
}
}
}
// check for supernatural embellishments
Integer hasSE = dieRoll(1,10);
if (hasSE > 5) {
Node anEmbellishment = getRandomNodeFromTable("SupernaturalEmbellishments.xml");
Embellishment newEmb = new Embellishment(anEmbellishment);
String se_name = newEmb.getEmbellishmentName();
newEmb.setEmbellishmentName(se_name + " (supernatural effect)");
embellishments.add(newEmb);
}
// check for curses
Integer hasC = dieRoll(1,10);
if (hasC > 9) {
Node aCurse = getRandomNodeFromTable("Curses.xml");
NodeList tempNodes = aCurse.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("Curse")) {
curse_name = element.getTextContent();
}
if (element.getNodeName().contentEquals("CF")) {
curse_cf = Double.parseDouble(element.getTextContent());
}
}
}
}
// end curses
}
// end enchantments
}
}
public String report(){
String full_report = "";
// name and, if needed, quantity
if(quantity > 1 & !(isAttribute("Jewelry")) ){
full_report = full_report + quantity.toString() + " ";
if (!(unit.contentEquals("Each"))){
full_report = full_report + unit + " ";
}
}
full_report = full_report + item_name + ". ";
String motif_exp = "";
if (!(motif_name.contentEquals("") )){
motif_exp = "; decorated with " + motif_name + " motif";
}
// embellishment list
if(embellishments.size() > 0){
int rounds = 1;
for (Embellishment eLine : embellishments) {
full_report = full_report + eLine.getEmbellishmentName();
rounds = rounds + 1;
if (rounds <= embellishments.size()){
full_report = full_report + ", ";
}else{
full_report = full_report + motif_exp + ". ";
}
}
} // end embellishments
// gem list
if(gems.size() > 0){
int rounds = 1;
full_report = full_report + "Set with ";
for (Gem eLine : gems) {
full_report = full_report + eLine.getGemWt().toString() + " ct. " + eLine.getGemName();
rounds = rounds + 1;
if (rounds <= gems.size()){
full_report = full_report + ", ";
}else{
full_report = full_report + ". ";
}
}
} // end gems
// enchantment list
if(enchantments.size() > 0){
int rounds = 1;
full_report = full_report + "Enchanted with ";
for (Enchantment eLine : enchantments) {
full_report = full_report + eLine.getEnchantmentName() ;
rounds = rounds + 1;
if (rounds <= enchantments.size()){
full_report = full_report + ", ";
}else{
full_report = full_report + ". ";
}
}
if(!curse_name.contentEquals("")){
full_report = full_report + "Cursed with " + curse_name + ". ";
}
} // end enchantments
if(!(container == null)){
full_report = full_report + "Contained in " + container.report_short();
}
Double f_cst = getFinalCost();
Double f_wt = getFinalWeight();
DecimalFormat df = new DecimalFormat("#.00");
NumberFormat fmt = NumberFormat.getCurrencyInstance();
full_report = full_report + fmt.format(f_cst) + ", ";
full_report = full_report + df.format(f_wt);
if (f_wt > 1){
full_report = full_report + " lbs.";
}else{
full_report = full_report + " lb.";
}
return full_report;
}
public String report_short(){
String full_report = "";
// name and, if needed, quantity
if(quantity > 1){
full_report = full_report + quantity.toString() + " x ";
}
full_report = full_report + item_name + ". ";
String motif_exp = "";
if (!(motif_name.contentEquals("") )){
motif_exp = "; decorated with " + motif_name + " motif";
}
// embellishment list
if(embellishments.size() > 0){
int rounds = 1;
for (Embellishment eLine : embellishments) {
full_report = full_report + eLine.getEmbellishmentName();
rounds = rounds + 1;
if (rounds <= embellishments.size()){
full_report = full_report + ", ";
}else{
full_report = full_report + motif_exp + ". ";
}
}
} // end embellishments
// enchantment list
if(enchantments.size() > 0){
int rounds = 1;
full_report = full_report + "Enchanted with ";
for (Enchantment eLine : enchantments) {
full_report = full_report + eLine.getEnchantmentName() ;
rounds = rounds + 1;
if (rounds <= enchantments.size()){
full_report = full_report + ", ";
}else{
full_report = full_report + ". ";
}
}
if(!curse_name.contentEquals("")){
full_report = full_report + "Cursed with " + curse_name + ". ";
}
} // end enchantments
return full_report;
}
public Item(String specific_item) {
// TODO Auto-generated constructor stub
getNamedItem(specific_item);
//
}
public Item(double low_val, double high_val) {
// get random item
getRandomItem();
}
public Double getFinalCost(){
Double final_cost = getBaseCost();
final_cost = final_cost * (1 + getTotalCF());
final_cost = final_cost + getEnchantmentCosts();
// gem list
if(gems.size() > 0){
for (Gem eLine : gems) {
final_cost = final_cost + eLine.getGemCost();
}
} // end gems
if (!(container == null)){
final_cost = final_cost + container.getFinalCost();
}
return final_cost;
}
public boolean hasEmbellishment(String embName){
boolean return_it = false;
if (embellishments.size() > 0){
for (Embellishment eLine : embellishments) {
String this_nom = eLine.getEmbellishmentName();
String embName_short = "";
if (embName.indexOf("(") > 0){
embName_short = embName.substring(0, embName.indexOf("("));
}else{
embName_short = embName;
}
if (this_nom.contains(embName_short)){
return_it = true;
}
}
}
return return_it;
}
public Double getTotalCF(){
Double total_cf = 0.0;
if (embellishments.size() > 0){
for (Embellishment eLine : embellishments) {
total_cf = total_cf + eLine.getCf();
}
}
return total_cf;
}
public Double getEnchantmentCosts(){
Double total_costs = 0.0;
if (enchantments.size() > 0){
for (Enchantment eLine : enchantments) {
total_costs = total_costs + eLine.getCost() ;
}
}
total_costs = total_costs * (1+ curse_cf);
return total_costs;
}
public void getRandomItem() {
try {
InputStream items = new FileInputStream(new File("Items.xml"));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(items);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
Integer itemlistlen = nodes.getLength();
Integer pickItm = dieRoll(0, itemlistlen-1);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
NamedNodeMap attrs = node.getAttributes();
Node sn = attrs.getNamedItem("name");
if (i == pickItm) {
NodeList tempNodes = node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("name")) {
item_name = element.getTextContent();
}
if (element.getNodeName().contentEquals("Wt")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_weight = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("Cost")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_cost = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("attributes")) {
attributes.add(element.getAttribute("value").toString()) ;
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getNamedItem(String the_item) {
try {
InputStream items = new FileInputStream(new File("Items.xml"));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(items);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
NamedNodeMap attrs = node.getAttributes();
Node sn = attrs.getNamedItem("name");
if (sn.getTextContent().contentEquals(the_item)) {
NodeList tempNodes = node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("name")) {
item_name = element.getTextContent();
}
if (element.getNodeName().contentEquals("Wt")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_weight = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("Cost")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_cost = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("attributes")) {
attributes.add(element.getAttribute("value").toString()) ;
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Double getFinalWeight(){
Double running_weight = base_weight;
if (embellishments.size() > 0){
for (Embellishment eLine : embellishments) {
running_weight = running_weight * eLine.getWtMod();
}
}
running_weight = running_weight * quantity;
if (!(container == null)){
running_weight = running_weight + container.getFinalWeight();
}
return running_weight;
}
public String getName(){
return item_name;
}
public Integer getQuantity(){
return quantity;
}
public Double getWeight(){
// do a bunch of math
return base_weight;
}
public Double getCost(){
return base_cost;
}
public Double getBaseWeight(){
return base_weight;
}
public Double getBaseCost(){
return base_cost;
}
public boolean isAttribute(String test_val){
boolean p = false;
for (String aLine : attributes) {
if (aLine.contentEquals(test_val)) {
p = true;
}
}
return p;
}
public String getEmbellishments(){
return "";
}
public boolean isEmbellishment(String test_val){
return true;
}
public String getEnchantments(){
return "";
}
public boolean isEnchantment(String test_val){
return true;
}
public String getContainer(){
return "";
}
public void addEmbellishment(){}
public void addEnchantment(){}
public void addContainer(){}
public ArrayList<Gem> getGems(){
return gems;
}
public Node getRandomNodeFromTable(String fname) {
Node return_it;
return_it = null;
try {
InputStream items = new FileInputStream(new File(fname));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(items);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
Integer itemlistlen = nodes.getLength();
Integer pickItm = dieRoll(0, itemlistlen - 1);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (i == pickItm) {
return_it = node;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return return_it;
}
public String getBulkType(String bulk_name){
String bulk_type = "";
Node bulk_node = getNamedNodeFromTable("BulkGoods.xml", bulk_name);
NodeList tempNodes = bulk_node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("ContainmentType")) {
bulk_type = element.getTextContent();
}
}
}
return bulk_type;
}
public void getBulkAttributes(String bulk_name){
Node bulk_node = getNamedNodeFromTable("BulkGoods.xml", bulk_name);
NodeList tempNodes = bulk_node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("ContainmentType")) {
bulk_type = element.getTextContent();
}
if (element.getNodeName().contentEquals("Unit")) {
unit = element.getTextContent();
}
}
}
}
public Node getNamedNodeFromTable(String fname, String get_item) {
Node return_it;
return_it = null;
try {
InputStream items = new FileInputStream(new File(fname));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(items);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
NamedNodeMap attrs = node.getAttributes();
Node sn = attrs.getNamedItem("name");
if (sn.getTextContent().contentEquals(get_item)) {
return_it = node;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return return_it;
}
private int dieRoll(Integer min, Integer max) {
Random r = new Random();
int inroll = r.nextInt(max - min + 1) + min;
return inroll;
}
// container stuff
public Item(String c_type, Double wt_cap) {
// This creates a container item of a given type and capacity
getSizedContainer(c_type, wt_cap);
// apply random number of embellishments
Integer rounds = 0;
Integer numEmbs = dieRoll(1, 2);
while (embellishments.size() < numEmbs & rounds < 20 ){
Node anEmbellishment = null;
rounds = rounds + 1;
while ( anEmbellishment == null){
anEmbellishment = getRandomNodeFromTable("Embellishments.xml");
}
Embellishment newEmb = new Embellishment(anEmbellishment);
if (newEmb.getEmbellishmentName().contentEquals("Jeweled")){
Gem newgem = new Gem();
gems.add(newgem);
} else {
String e_code = newEmb.getEmbCode();
boolean already_got = hasEmbellishment(newEmb.getEmbellishmentName());
if (isAttribute(e_code) | (item_name.toLowerCase().contains(e_code.toLowerCase()) & e_code.length() > 3 ) ){
if (! already_got){
embellishments.add(newEmb);
}
}
}
}
// add a motif, if needed
// go through the embellishments and see if they require or at least support a motif
if(embellishments.size() > 0){
String need_motif = "N";
for (Embellishment eLine : embellishments) {
String emb_motif = eLine.getMotifOption();
if (emb_motif.contentEquals("Y")){
need_motif = "Y";
}else if (emb_motif.contentEquals("P") & !(need_motif.contentEquals("Y")) ){
need_motif = "P";
}
}
// if they support but don't require a motif, flip a coin
if (need_motif.contentEquals("P") ){
int flip = dieRoll(1,2);
if (flip == 1){
need_motif = "Y";
}
}
// so if we need a motif, add one
if (need_motif.contentEquals("Y")){
Node the_motif_node = getRandomNodeFromTable("Motifs.xml");
NodeList tempNodes = the_motif_node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("Motif")) {
motif_name = element.getTextContent();
}
}
}
}
} // end embellishments
}
public void getSizedContainer(String substance_type, Double wt_capacity) {
Double ct_cap = 0.0;
String ct_type = "";
try {
InputStream items = new FileInputStream(new File("Containers.xml"));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(items);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
NamedNodeMap attrs = node.getAttributes();
Node sn = attrs.getNamedItem("name");
NodeList tempNodes = node.getChildNodes();
for (int j = 0; j < tempNodes.getLength(); j++) {
Node subnode = tempNodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) subnode;
if (element.getNodeName().contentEquals("ContainmentType")) {
ct_type = element.getTextContent();
}
if (element.getNodeName().contentEquals("Capacity")) {
String base_read = element.getTextContent();
ct_cap = Double.parseDouble(element.getTextContent());
}
if (element.getNodeName().contentEquals("name")) {
item_name = element.getTextContent();
}
if (element.getNodeName().contentEquals("Wt")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_weight = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("Cost")) {
String base_read = element.getTextContent();
if (base_read == "" | base_read.length() == 0)
{
base_cost = 0.0;
} else {
base_cost = Double.parseDouble(element.getTextContent());
}
}
if (element.getNodeName().contentEquals("attributes")) {
attributes.add(element.getAttribute("value").toString()) ;
}
}
}
// if the containment type is good and the container will hold enough, break the loop
if (ct_type.contentEquals(substance_type) & ct_cap >= wt_capacity){
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// end container stuff
}
Comments