[질문]
Lua 질문좀 받아줘
고속도로(highwaystarjojo)
2020-09-11 13:59
추천 0
https://gall.dcinside.com/mgallery/board/view/?id=factorio&no=24739
생산모듈 적용되는 레시피 볼 때
이렇게 나오는 커맨드는 이해했는데
아이템에 들어가는 그 원재료 그거는 못 뽑나?
예를 들어서
철 판 = 1 철 광석
구리 판 = 1 구리 광석
전자 회로 = 1 철 광석 1.5 구리 광석
...
이렇게
인게임 제작탭에서 마우스 올려놓을때 나오는 그거만 쫙 뽑을수 있는 방법 없음?
https://forums.factorio.com/viewtopic.php?p=34789#p34789
여기서 비슷한거 봤음. 게임 버전도 오래전꺼라 지금 적용 되는지 잘 모르겠네
package.path = package.path .. ';data/core/lualib/?.lua;data/base/?.lua'
require "dataloader"
require "data.base.data"
function getIngredients(recipe)
local ingredients = {}
for i,ingredient in pairs(recipe.ingredients) do
if (ingredient.name and ingredient.amount) then
ingredients[ingredient.name] = ingredient.amount
elseif (ingredient[1] and ingredient[2]) then
ingredients[ingredient[1]] = ingredient[2]
end
end
return ingredients
end
function getProducts(recipe)
local products = {}
if (recipe.results) then
for i,product in pairs(recipe.results) do
if (product.name and product.amount) then
products[product.name] = product.amount
end
end
elseif (recipe.result) then
local amount = 1
if (recipe.result_count) then
amount = recipe.result_count
end
products[recipe.result] = amount
end
return products
end
function getRecipes(item)
local recipes = {}
for i,recipe in pairs(data.raw.recipe) do
local products = getProducts(recipe)
for product,amount in pairs(products) do
if (product == item) then
table.insert(recipes, recipe)
end
end
end
return recipes
end
function getRawIngredients(recipe, exclude)
local raw_ingredients = {}
for name,amount in pairs(getIngredients(recipe)) do
-- Do not use an item as its own ingredient
if (exclude[name]) then
return {ERROR_INFINITE_LOOP = name}
end
local excluded_ingredients = {[name] = true}
for k,v in pairs(exclude) do
excluded_ingredients[k] = true
end
-- Recursively find the sub-ingredients for each ingredient
-- There might be more than one recipe to choose from
local subrecipes = {}
local loop_error = nil
for i,subrecipe in pairs(getRecipes(name)) do
local subingredients = getRawIngredients(subrecipe, excluded_ingredients)
if (subingredients.ERROR_INFINITE_LOOP) then
loop_error = subingredients.ERROR_INFINITE_LOOP
else
local value = 0
for subproduct,subamount in pairs(getProducts(subrecipe)) do
value = value + subamount
end
local divisor = 0
for subingredient,subamount in pairs(subingredients) do
divisor = divisor + subamount
end
if (divisor == 0) then divisor = 1 end
table.insert(subrecipes, {recipe = subrecipe, ingredients = subingredients, value = value / divisor})
end
end
if (#subrecipes == 0) then
if (loop_error and loop_error ~= name) then
-- This branch of the recipe tree is invalid
return {ERROR_INFINITE_LOOP = loop_error}
else
-- This is a raw resource
if (raw_ingredients[name]) then
raw_ingredients[name] = raw_ingredients[name] + amount
else
raw_ingredients[name] = amount
end
end
else
-- Pick the cheapest recipe
local best_recipe = nil
local best_value = 0
for i,subrecipe in pairs(subrecipes) do
if (best_value < subrecipe.value) then
best_value = subrecipe.value
best_recipe = subrecipe
end
end
local multiple = 0
for subname,subamount in pairs(getProducts(best_recipe.recipe)) do
multiple = multiple + subamount
end
for subname,subamount in pairs(best_recipe.ingredients) do
if (raw_ingredients[subname]) then
raw_ingredients[subname] = raw_ingredients[subname] + amount * subamount / multiple
else
raw_ingredients[subname] = amount * subamount / multiple
end
end
end
end
return raw_ingredients
end
function round(num, idp)
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
for recipename,recipe in pairs(data.raw.recipe) do
local exclude = {}
for product,amount in pairs(getProducts(recipe)) do
exclude[product] = true
end
local ingredients = getRawIngredients(recipe, exclude)
if (ingredients.ERROR_INFINITE_LOOP) then
ingredients = getIngredients(recipe)
end
print("[u]" .. recipename .. "[/u]")
for name,amount in pairs(ingredients) do
print(round(amount, 1) .. "x " .. name)
end
print ""
end이거는 어떻게 적용하는거임
콘솔창에 써넣는거는 아닌거같어
저 코드는 게임플레이중에 쓸수있는 코드가 아니라 모드 로딩때 쓰는 코드인것같음
https://gall.dcinside.com/mgallery/board/view/?id=factorio&no=25486
댓글로 쓰기 애매해서 글로 씀
전자 회로 = 1 철 판 3 구리 전선 - 이렇게 나오는 거는
https://gall.dcinside.com/mgallery/board/view/?id=factorio&no=17681
여기 있고, 나는 그거 말고
전자 회로 = 1 철 광석 1.5 구리 광석 이렇게 광석까지 쪼개주는거 찾는거임
아 완전 원재료 까지였누
근데 그거는 가능하긴 한데 생산법 두개이상 있는거나오는 순간 더 이상 못 쪼갬
ㅇㅇ 그래도 상관없음
아니 인게임에서 제작법에 마우스 올리면 원재료 나오자너... 나는 그거를 직접 뽑을수 있을까 싶어서 그런거지
이거는 밤돼야 쓸수 있을듯
나도 코드 만들고싶어
테이블이나 폴더를 다차원으로 파고들려면 재귀함수 (recursive function)를 만들어서 자기가 자기 호출하게 돌려야함. 호출하면서 던지는 인수는 한층 파고든 위치 같은걸로 쓰고, 원점은 함수 바깥에 local 변수같은걸 만들어두고 씀(루아에서 upvalue라고 함) 그리고 재귀함수 쓸때 또 중요한건 탈출조건을 만들어줘야한다는거임. 안그럼 프로그램 프리징걸림.
긁어온 함수도 잘 보면 getRawIngredients 안에 getRawIngredients 자기자신을 호출하는게 보임.
lua 는 그냥 팩토리오 말고 lua 프로그램 깔고 돌려봐도 됨.
팩토리오는 n차 생산을 하니까, 원재료 탐색하려면 윗 댓글처럼 재귀로 도는게 편함