Re-write craft method
* Re-write the `computeMissingResources` method to be `craft` and refactor how it performs calculations to be easier to follow, and to avoid several errors in the prior calculation related to recipes which produce more 1 quantity of the output items.
This commit is contained in:
+49
-43
@@ -26,8 +26,14 @@ function onCraftingSelectorChanged() {
|
|||||||
$("#crafting_error").fadeIn(FADE_DURATION).delay(ERROR_DISPLAY_DURATION).fadeOut(FADE_DURATION);
|
$("#crafting_error").fadeIn(FADE_DURATION).delay(ERROR_DISPLAY_DURATION).fadeOut(FADE_DURATION);
|
||||||
} else {
|
} else {
|
||||||
var count = parseInt($("#crafting_count option:selected").val());
|
var count = parseInt($("#crafting_count option:selected").val());
|
||||||
|
|
||||||
var inventory = createManifest();
|
var inventory = createManifest();
|
||||||
var missingMaterials = recipe.computeMissingMaterials(inventory, count);
|
var missingMaterials = createManifest();
|
||||||
|
for (var i = 0; i < count; i++) {
|
||||||
|
recipe.craft(inventory, missingMaterials);
|
||||||
|
if (inventory.contains(count, recipeName)) break;
|
||||||
|
}
|
||||||
|
|
||||||
$("#missing_materials").html(missingMaterials.toHtml());
|
$("#missing_materials").html(missingMaterials.toHtml());
|
||||||
$("#leftover_materials").html(inventory.toHtml());
|
$("#leftover_materials").html(inventory.toHtml());
|
||||||
$("#crafting_output").fadeIn(FADE_DURATION);
|
$("#crafting_output").fadeIn(FADE_DURATION);
|
||||||
@@ -119,6 +125,10 @@ function createManifest() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
object.removeAll = function() {
|
||||||
|
object.materials = {};
|
||||||
|
}
|
||||||
|
|
||||||
object.toHtml = function() {
|
object.toHtml = function() {
|
||||||
var result = "";
|
var result = "";
|
||||||
var count = 0;
|
var count = 0;
|
||||||
@@ -149,56 +159,52 @@ function createRecipe(data) {
|
|||||||
|
|
||||||
// Methods ////////////////////////////////////////////
|
// Methods ////////////////////////////////////////////
|
||||||
|
|
||||||
object.computeMissingMaterials = function(inventory, count) {
|
object.craft = function(inventory, missingMaterials) {
|
||||||
if (inventory === undefined) inventory = createManifest();
|
if (inventory === undefined) inventory = createManifest();
|
||||||
if (count === undefined) count = 1;
|
if (missingMaterials === undefined) missingMaterials = createManifest();
|
||||||
var missingMaterials = createManifest();
|
var toCraftManifest = createManifest();
|
||||||
var queue = [];
|
|
||||||
|
|
||||||
function craftItem(ingredient) {
|
function drainCraftingManifest(consumeItems) {
|
||||||
var recipe = findRecipe(ingredient.name);
|
for (var name in toCraftManifest.materials) {
|
||||||
if (recipe === undefined) {
|
var count = toCraftManifest.materials[name];
|
||||||
missingMaterials.add(1, ingredient.name);
|
var itemRecipe = findRecipe(name);
|
||||||
ingredient.count -= 1
|
|
||||||
} else {
|
|
||||||
$.each(recipe.input, function(j, childIngredient) {
|
|
||||||
queue.push({count: childIngredient.count, name: childIngredient.name});
|
|
||||||
});
|
|
||||||
$.each(recipe.output, function(j, childProduct) {
|
|
||||||
inventory.add(childProduct.count, childProduct.name);
|
|
||||||
});
|
|
||||||
$.each(recipe.tools, function(j, tool) {
|
|
||||||
if (! inventory.contains(1, tool)) {
|
|
||||||
if (findRecipe(tool) === undefined) {
|
|
||||||
inventory.add(1, tool);
|
|
||||||
missingMaterials.add(1, tool);
|
|
||||||
} else {
|
|
||||||
craftItem({count: 1, name: tool});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return recipe;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < count; i++) {
|
for (var i = 0; i < count; i++) {
|
||||||
craftItem({count: 1, name: object.output[0].name});
|
if (inventory.contains(1, name)) {
|
||||||
|
if (consumeItems) {
|
||||||
|
inventory.remove(1, name);
|
||||||
|
}
|
||||||
|
} else if (itemRecipe === undefined) {
|
||||||
|
missingMaterials.add(1, name);
|
||||||
|
} else {
|
||||||
|
itemRecipe.craft(inventory, missingMaterials);
|
||||||
|
if (consumeItems) {
|
||||||
|
inventory.remove(1, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
toCraftManifest.removeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
while (queue.length > 0) {
|
$(object.input).each(function(i, input) {
|
||||||
var ingredient = queue.shift();
|
for (var j = 0; j < input.count; j++) {
|
||||||
while (ingredient.count > 0) {
|
toCraftManifest.add(1, input.name);
|
||||||
if (inventory.contains(1, ingredient.name)) {
|
|
||||||
inventory.remove(1, ingredient.name);
|
|
||||||
ingredient.count -= 1;
|
|
||||||
} else {
|
|
||||||
console.log("Crafting ingredient: " + ingredient.name);
|
|
||||||
craftItem(ingredient);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
drainCraftingManifest(true);
|
||||||
|
|
||||||
|
$(object.tools).each(function(i, tool) {
|
||||||
|
if (! inventory.contains(1, tool)) {
|
||||||
|
toCraftManifest.add(1, tool);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
return missingMaterials;
|
drainCraftingManifest(false);
|
||||||
}
|
|
||||||
|
$(object.output).each(function(i, output) {
|
||||||
|
inventory.add(output.count, output.name);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user