Module:GameData: Difference between revisions

From the Dyson Sphere Program Wiki
(This is why I don't do things live. The debug console won't work for this without a valid frame, and I'm not sure how to give it one. Just don't look at edit history.)
(Count Items not Results, and actually make table rows. Found a way to not do it live. I hope. I think. If this works.)
Line 70: Line 70:
for i, itemid in ipairs(recipe.Items) do
for i, itemid in ipairs(recipe.Items) do
itemdata[string.format('In%d', i)] = itemByID(itemid).Name
itemdata[string.format('In%d', i)] = itemByID(itemid).Name
itemdata[string.format('In%dQty', i)] = tostring(recipe.ResultCounts[i])
itemdata[string.format('In%dQty', i)] = tostring(recipe.ItemCounts[i])
end
end
data = {
data = {
Line 87: Line 87:
data.Technology = ''
data.Technology = ''
end
end
table.insert(result, frame:expandTemplate{title='ProductionChain', args=data})
table.insert(result, '| ' .. frame:expandTemplate{title='ProductionChain', args=data})
end
end
prefix = frame:expandTemplate{title='ProductionChainTable/head', args={}}
prefix = frame:expandTemplate{title='ProductionChainTable/head', args={}}
return string.format('%s\n%s\n|}', prefix, table.concat(result, '|-'))
return string.format('%s\n%s\n|}', prefix, table.concat(result, '\n|-\n'))
end
end



Revision as of 00:01, 27 April 2024

Source data for this module is stored at Module:GameData/protosets.json

Exported Functions

Each function in this module is exported twice: one as-is for use in templates, and once with a Direct suffix for use directly on pages using {{#invoke|GameData|functionDirect|...}}.

recipesMaking

Arguments: Item Name

Print recipes making an item, given by name. This produces a full table with headers.

Example invocation:

{{#invoke:GameData|recipesMakingDirect|Sulfuric Acid}}
Recipe Building Replicator? Technology
Icon Sulfuric Acid.png
4
6 s
Icon Refined Oil.png
6
Icon Stone.png
8
Icon Water.png
4
Icon Chemical Plant.pngIcon Quantum Chemical Plant.png
Tech Basic Chemical Engineering.png

recipesUsing

Arguments: Item Name

Print recipes using an item as an ingredient. Output is separated into two full tables: recipes which create components, and recipes which create buildings. This is selected by the CanBuild property of the produced items.

Example invocation:

{{#invoke:GameData|recipesUsingDirect|Copper Ingot}}

Components

Recipe Building Replicator? Technology
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Semiconductor Material.png
Icon Magnetic Coil.png
2
1 s
Icon Magnet.png
2
Icon Copper Ingot.png
1
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png N/A
Icon Particle Container.png
1
4 s
Icon Electromagnetic Turbine.png
2
Icon Copper Ingot.png
2
Icon Graphene.png
2
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Magnetic Particle Trap.png
Icon Thruster.png
1
4 s
Icon Steel.png
2
Icon Copper Ingot.png
3
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Thruster.png
Icon Circuit Board.png
2
1 s
Icon Iron Ingot.png
2
Icon Copper Ingot.png
1
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png N/A
Icon Particle Container.png
1
4 s
Icon Unipolar Magnet.png
10
Icon Copper Ingot.png
2
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Magnetic Particle Trap.png
Icon Shell Set.png
1
1.5 s
Icon Copper Ingot.png
9
Icon Combustible Unit.png
2
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Implosion Cannon.png
Icon Engine.png
1
3 s
Icon Magnetic Coil.png
1
Icon Copper Ingot.png
2
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Engine.png
Icon Missile Set.png
1
2 s
Icon Copper Ingot.png
6
Icon Circuit Board.png
3
Icon Combustible Unit.png
2
Icon Engine.png
1
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Missile Turret.png
Icon Magnum Ammo Box.png
1
1 s
Icon Copper Ingot.png
3
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Weapon System.png

Buildings

Recipe Building Replicator? Technology
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Solar Collection.png
Icon Jammer Tower.png
1
5 s
Icon Copper Ingot.png
12
Icon Plasma Exciter.png
9
Icon Diamond.png
6
Icon Processor.png
3
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Jammer Tower.png
Icon Geothermal Power Station.png
1
6 s
Icon Steel.png
15
Icon Copper Ingot.png
20
Icon Photon Combiner.png
4
Icon Super-magnetic Ring.png
1
Icon Assembling Machine Mk.I.pngIcon Assembling Machine Mk.II.pngIcon Assembling Machine Mk.III.pngIcon Re-composing Assembler.png
Tech Geothermal Extraction.png

itemRecipes

Arguments: Item Name

Print recipes making an item. This does not produce a full table, and is intended for use in ItemInfo boxes such as at Graphene/ItemInfo.

itemField

Arguments: Item Name, Field Name

Print a field from an item, with the field to print as an argument. You can look at Module:GameData/protosets.json and scroll down or search (Ctrl+F) to the ItemProtoSet to browse fields.

Example invocation:

{{#invoke:GameData|itemFieldDirect|Magnetic Coil|Description}}

Script error: The function "itemFieldDirect" does not exist.

{{#invoke:GameData|itemFieldDirect|Hydrogen|StackSize}}

Script error: The function "itemFieldDirect" does not exist.


local funcs = {}
local protosets = mw.loadJsonData('Module:GameData/protosets.json')
local machines = {
	Smelt='Smelter',
	Assemble='Assembling Machine',
	Refine='Oil Refinery',
	Chemical='Chemical Plant',
	Exchange='Energy Exchanger',
	Particle='Miniature Particle Collider',
	PhotonStore='Ray Receiver',
	Fractionate='Fractionator',
	Resarch='Matrix Lab',
}

function funcs.recipesMaking(frame)
	name = frame:getParent().args[1]
	item = itemByName(name)
	recipes = recipesMakingByID(item.ID)
	return tableRecipes(recipes, frame)
end

function funcs.recipesMakingDirect(frame)
	name = frame.args[1]
	item = itemByName(name)
	recipes = recipesMakingByID(item.ID)
	return tableRecipes(recipes, frame)
end

function funcs.recipesUsing(frame)
	output = {}
	name = frame:getParent().args[1]
	item = itemByName(name)
	components, buildings = recipesUsingByID(item.ID)
	if next(components) then
		table.insert(output, '=== Components ===')
		table.insert(output, tableRecipes(components, frame))
	end
	if next(buildings) then
		table.insert(output, '=== Buildings ===')
		table.insert(output, tableRecipes(buildings, frame))
	end
	return table.concat(output, '\n')
end

function funcs.recipesUsingDirect(frame)
	output = {}
	name = frame.args[1]
	item = itemByName(name)
	components, buildings = recipesUsingByID(item.ID)
	if next(components) then
		table.insert(output, '=== Components ===')
		table.insert(output, tableRecipes(components, frame))
	end
	if next(buildings) then
		table.insert(output, '=== Buildings ===')
		table.insert(output, tableRecipes(buildings, frame))
	end
	return table.concat(output, '\n')
end

function tableRecipes(recipes, frame)
	result = {}
	for _, recipe in pairs(recipes) do
		itemdata = {}
		itemdata.CraftTime = string.format('%s s', tostring(recipe.TimeSpend/60))
		for i, itemid in ipairs(recipe.Results) do
			itemdata[string.format('Out%d', i)] = itemByID(itemid).Name
			itemdata[string.format('Out%dQty', i)] = tostring(recipe.ResultCounts[i])
		end
		for i, itemid in ipairs(recipe.Items) do
			itemdata[string.format('In%d', i)] = itemByID(itemid).Name
			itemdata[string.format('In%dQty', i)] = tostring(recipe.ItemCounts[i])
		end
		data = {
			Building = machines[recipe.Type],
			Recipe = frame:expandTemplate{title='ItemRecipe', args=itemdata},
		}
		if data.Handcraft then
			data.Replicator = 'Yes'
		else
			data.Replicator = 'No'
		end
		tech = technologyForRecipeID(recipe.ID)
		if tech ~= nil then
			data.Technology = tech.Name
		else
			data.Technology = ''
		end
		table.insert(result, '| ' .. frame:expandTemplate{title='ProductionChain', args=data})
	end
	prefix = frame:expandTemplate{title='ProductionChainTable/head', args={}}
	return string.format('%s\n%s\n|}', prefix, table.concat(result, '\n|-\n'))
end

function itemByName(name)
	for _, item in pairs(protosets.ItemProtoSet.dataArray) do
		if item.Name == name then
			return item
		end
	end
	error('No item named ' .. name)
end
function itemByID(id)
	for _, item in pairs(protosets.ItemProtoSet.dataArray) do
		if item.ID == id then
			return item
		end
	end
	error('No item with ID ' .. id)
end

function recipesMakingByID(id)
	result = {}
	for _, recipe in pairs(protosets.RecipeProtoSet.dataArray) do
		for _, itemid in pairs(recipe.Results) do
			if itemid == id then
				table.insert(result, recipe)
				break
			end
		end
	end
	return result
end
function recipesUsingByID(id)
	components = {}
	buildings = {}
	for _, recipe in pairs(protosets.RecipeProtoSet.dataArray) do
		for _, itemid in pairs(recipe.Items) do
			if itemid == id then
				if itemByID(recipe.Results[1]).CanBuild then
					table.insert(buildings, recipe)
				else
					table.insert(components, recipe)
				end
				break
			end
		end
	end
	return components, buildings
end

function technologyForRecipeID(id)
	for _, tech in pairs(protosets.TechProtoSet.dataArray) do
		for _, recipeid in pairs(tech.UnlockRecipes) do
			if recipeid == id then
				return tech
			end
		end
	end
	return nil
end

return funcs
🍪 We use cookies to keep session information to provide you a better experience.