Table Building Function

Place to get help with not working mods / modding interface.
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2179
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Table Building Function

Post by Ranakastrasz »

I would like to make a function I can call in config, or in an addon mod, which will let me build this kind of table. Is there a pre-existing mod that has something like this I could look at, or a good explaination on how to do such?

I suspect that Factorio itself does something similar, with how it uses Extend for prototype definitions.

Code: Select all

RanaMods.ModularArmor.equipmentData =
{
    {
        name = "conduit",
        type = "conduit",
        equipment =
        {
            {name = "power-conduit-equipment"    , power = 60 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
            {name = "power-conduit-mk2-equipment", power = 90 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
            {name = "power-conduit-mk3-equipment", power =150 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
            {name = "power-conduit-mk4-equipment", power =240 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
        }
    },
    {
        name = "burner",
        type = "fuelled",
        equipment = 
        {
            {name = "engine-equipment"    ,  power = 100 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
            {name = "engine-mk2-equipment",  power = 150 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
            {name = "engine-mk3-equipment",  power = 250 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
            {name = "engine-mk4-equipment",  power = 400 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
        },
        fuel = 
        {
            {name = "solid-fuel", power =  25.*1000*1000*RanaMods.ModularArmor.config.fuelCoef}, -- Value of each fuel type.
            {name = "coal"      , power =   8.*1000*1000*RanaMods.ModularArmor.config.fuelCoef}, 
            {name = "raw-wood"  , power =   4.*1000*1000*RanaMods.ModularArmor.config.fuelCoef},
        },
        pollution = 0.01, -- Per kilowatt produced.
    
    },
    {
        name = "fusion",
        type = "fuelled",
        equipment = 
        {
            {name = "fusion-reactor-equipment"    , power =  960 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
            {name = "fusion-reactor-mk2-equipment", power = 1500 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
            {name = "fusion-reactor-mk3-equipment", power = 2500 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
            {name = "fusion-reactor-mk4-equipment", power = 4000 * 1000 * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick},
        },
        fuel = 
        {
            {name = "alien-fuel", power =  200.*1000*1000*RanaMods.ModularArmor.config.fuelCoef},
        }
    
    }
}
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Table Building Function

Post by prg »

You're looking for a function that can insert a new element into an existing table?
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2179
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Table Building Function

Post by Ranakastrasz »

Yes.
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Table Building Function

Post by prg »

Code: Select all

table.insert(mytable, element)
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2179
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Table Building Function

Post by Ranakastrasz »

How can I use that to alter the inner table?
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Table Building Function

Post by prg »

What inner table do you want to modify how? You want to do more than just append a new { type="foo", name="foo", ... } to equipmentData?
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2179
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Table Building Function

Post by Ranakastrasz »

Need to add to the "equipment ="
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Table Building Function

Post by prg »

Could you give a proper specification of that function you're looking for and an example usage? I'm just guessing here. Do you want to add a new entry to the equipment table of a certain name/type in the equipmentData table?

Code: Select all

for _, v in pairs(RanaMods.ModularArmor.equipmentData) do
    if v.type == "fuelled" and v.name == "burner" then
        table.insert(v.equipment, {name="foo", power=123})
        break
    end
end
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2179
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Table Building Function

Post by Ranakastrasz »

Basically, yes. That looks like it should work..
Work, even tho it is ugly.
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Table Building Function

Post by bobingabout »

I know it's probably solved now, but, if you ever need to look at example functions, take a look at this viewtopic.php?f=51&t=16642
I do this sort of table manipulation all over the place in my mods.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2179
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Table Building Function

Post by Ranakastrasz »

Appears to works now, But I would honestly like more error/duplicate checking. Still, should make the next part possible at least.

Code: Select all

function registerEquipmentGroup(iName,iType)
    if not RanaMods.ModularArmor.equipmentData then
        RanaMods.ModularArmor.equipmentData = {}
    end
    for _, v in pairs(RanaMods.ModularArmor.equipmentData) do
        if v.name == iName then
            return
        end
    end
    table.insert(RanaMods.ModularArmor.equipmentData, {name=iName, type = iType})
end

function registerEquipmentPrototype (iName,iType,iPrototype,iPower)

    if not RanaMods.ModularArmor.equipmentData then
        RanaMods.ModularArmor.equipmentData = {}
    end
    for _, v in pairs(RanaMods.ModularArmor.equipmentData) do
        if v.name == iName then
            if not v.equipment then
                v.equipment = {}
            end
            
            table.insert(v.equipment, {name=iPrototype, power=iPower * RanaMods.ModularArmor.config.powerCoef *RanaMods.ModularArmor.config.secondsPerTick})
            
            break
        end
    end
end

function registerFuelPrototype (iName,iType,iPrototype,iPower)

    if not RanaMods.ModularArmor.equipmentData then
        RanaMods.ModularArmor.equipmentData = {}
    end
    for _, v in pairs(RanaMods.ModularArmor.equipmentData) do
        if v.name == iName then
            if not v.fuel then
                v.fuel = {}
            end
            
            table.insert(v.fuel, {name=iPrototype, power=iPower*RanaMods.ModularArmor.config.fuelCoef})
            
            break
        end
    end
end

registerEquipmentGroup("conduit","conduit")
registerEquipmentGroup("burner","fuelled")
registerEquipmentGroup("fusion","fuelled")

registerEquipmentPrototype("conduit","conduit","power-conduit-equipment"    , 60 * 1000)
registerEquipmentPrototype("burner" ,"fuelled","engine-equipment"           ,100 * 1000)
registerEquipmentPrototype("fusion" ,"fuelled","fusion-reactor-equipment"   ,960 * 1000)

registerFuelPrototype("burner" ,"fuelled","solid-fuel",25   * 1000 * 1000)
registerFuelPrototype("burner" ,"fuelled","coal"      ,8.   * 1000 * 1000)
registerFuelPrototype("burner" ,"fuelled","raw-wood"  ,4.   * 1000 * 1000)

registerFuelPrototype("fusion" ,"fuelled","alien-fuel",200. * 1000 * 1000)
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
Post Reply

Return to “Modding help”