Skip to content

Commit

Permalink
Merge remote-tracking branch 'rubyruy/gh-pages' into gh-pages
Browse files Browse the repository at this point in the history
Includes port of pull request #37 from Lua to JS
  • Loading branch information
Andrew Brindamour committed May 13, 2017
2 parents d4f987c + 7382c12 commit 0a1a4fc
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 9,866 deletions.
28 changes: 27 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# factorio-calc

The MIT License (MIT)

Copyright (c) 2014 Ruy Asan
Expand All @@ -18,4 +20,28 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.

# lua.vm.js

The MIT License (MIT)

Copyright (c) 2013 Alon Zakai (kripken)
Copyright (c) 2014-2016 Daurnimator

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
79 changes: 79 additions & 0 deletions calc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
local function required_asslvl(ingredient_count)
if ingredient_count > 4 then return 1.25 end
if ingredient_count > 2 then return 0.75 end
return 0.5
end

function get_recipe(name, options)
local rdata = data.raw.recipe[name]
local recipe = {}
if not rdata then
return rdata
end
recipe.name = rdata.name
recipe.time = rdata.energy_required or 0.5
if rdata.category == 'smelting' then
console(options.smeltlvl)
recipe.time = recipe.time / tonumber(options.smeltlvl)
recipe.outputs = rdata.result_count or 1
elseif rdata.category == 'chemistry' then
recipe.time = recipe.time / 1.25
if rdata.results then
for i, res in ipairs(rdata.results) do
if res.name == recipe.name then
recipe.outputs = res.amount
end
end
else
recipe.outputs = 1
end
else
recipe.time = recipe.time / math.max(
tonumber(options.asslvl),
required_asslvl(#rdata.ingredients)
)
recipe.outputs = rdata.result_count or 1
end
recipe.ips = recipe.outputs / recipe.time
recipe.inputs = {}
for i, ingr in ipairs(rdata.ingredients) do
local ingredient = {}
if ingr.name then
ingredient.name = ingr.name
ingredient.amount = ingr.amount
else
ingredient.name = ingr[1]
ingredient.amount = ingr[2]
end
table.insert(recipe.inputs, ingredient)
end
return recipe
end


function request(name, ips, options)
local recipe = get_recipe(name, options)
if not options then
options = {asslvl = 1, smeltlvl = 1, beltlvl = 14.2}
end
if not recipe then
return {name = name, ips=ips}
end

local req = {}
req.name = recipe.name
req.ips = ips
req.ipspa = recipe.ips
req.assemblers = req.ips / req.ipspa
req.assembler_max_line = tonumber(options.beltlvl) / recipe.ips
req.lines_required = req.assemblers / math.floor(req.assembler_max_line)
req.cycle_time = recipe.time
req.inputs = {}
for i, input in ipairs(recipe.inputs) do
local ingr_per_cycle = input.amount * req.assemblers
local ingr_required_ips = ingr_per_cycle / req.cycle_time
table.insert(req.inputs,
request(input.name, ingr_required_ips, options))
end
return req
end
16 changes: 15 additions & 1 deletion calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ App.Calculator = {
return {name: rawIngredient[0], amount: rawIngredient[1]};
}
});

recipe.minAssemblerLevel = this._getMinAssemblerLevel(recipe);

return recipe;
},
Expand Down Expand Up @@ -137,6 +139,18 @@ App.Calculator = {
return finalTotals;
},

_getMinAssemblerLevel: function(recipe) {

if (recipe.ingredients.length > 4) {
return 1.25;
} else if (recipe.ingredients.length > 2) {
return 0.75;
} else {
return 0.5;
}

},

_getAssemblyInfoForRecipe: function(recipe, options) {

var assemblyTime;
Expand All @@ -151,7 +165,7 @@ App.Calculator = {
assemblyTime = recipe.baseTime;
}
else {
assemblyTime = recipe.baseTime / parseFloat(options.asslvl);
assemblyTime = recipe.baseTime / Math.max(recipe.minAssemblerLevel, parseFloat(options.asslvl));
}

var oneAssemblerRate = recipe.outputs / assemblyTime;
Expand Down
File renamed without changes.
15 changes: 8 additions & 7 deletions explain.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ App.Explain = React.createClass({
var recipe = this.props.recipe;
var details = null;
if (this.props.recipe.assemblersRequired) {

var madeBySingular = "assembler";
var madeByPlural = "assemblers";
var madeUnits = "items";
var assemblerPartOne = (<span>Your chosen assembler has a crafting speed of { bmono(this.props.options.asslvl) }. This means it will take {decimalNumber(recipe.assemblyTime)} seconds to create { wholeNumber(recipe.outputs)} { bmono(recipe.name) }.</span>);
var madeBySingular, madeByPlural, madeUnits, assemblerPartOne;
if (recipe.category == "ore") {
madeBySingular = "drill";
madeByPlural = "drills";
Expand All @@ -53,10 +49,15 @@ App.Explain = React.createClass({
madeBySingular = "chemplant";
madeByPlural = "chemplants";
assemblerPartOne = (<span>Chemplants have a crafting speed of { bmono(1.25) }. This means it will take {decimalNumber(recipe.assemblyTime)} seconds to create { wholeNumber(recipe.outputs)} { bmono(recipe.name) }.</span>);
} else {
madeBySingular = "assembler";
madeByPlural = "assemblers";
assemblerPartOne = (<span>Your chosen assembler has a crafting speed of { bmono(Math.max(recipe.minAssemblerLevel, this.props.options.asslvl)) }. This means it will take {decimalNumber(recipe.assemblyTime)} seconds to create { wholeNumber(recipe.outputs)} { bmono(recipe.name) }.</span>);
}
if (recipe.type == "fluid")
{
if (recipe.type == "fluid") {
madeUnits = "units";
} else {
madeUnits = "items";
}

var assemblyLinesExplanation = null;
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<script type="text/javascript">
window.DATALIBS = [
'core-0-15-9',
'core-0-15-02',
'core-0-15-2',
'core-0-14-22',
'core-0-14-20',
'core-0-12-33',
Expand Down
Loading

0 comments on commit 0a1a4fc

Please sign in to comment.