Jump to content

Recommended Posts

Ok, so since the recent update, the code i was using to add set piece isn't working anymore.

Here is the old code.

Spoiler

-- GLOBAL variables which are needed.
local require = GLOBAL.require

--[[    Copyright © 2015 Ysovuka/Kzisor     ]]

-- The Layouts table is where we will add our set piece layout.
-- The file is provided in "scripts/map" of the original game.
local Layouts = require("map/layouts").Layouts

-- The StaticLayouts variable is used to get the layout of our set piece.
-- The file is provided in "scripts/map" of the original game.
local StaticLayouts = require("map/static_layout")

-- Thie LEVELTYPE table is used to indicate the level identification.
-- The file is provided in "scripts/map" of the original game.
require("map/level")
local LEVELTYPE = GLOBAL.LEVELTYPE
LEVELTYPE.ALL = "ALL"


-- This function acts as a wrapper which we will use to add our custom layout to the game.
-- The name of the layout will be identical to the filename.
local function AddLayout(name)
    -- Puts our layout in the Layouts table.
    Layouts[name] = StaticLayouts.Get("map/static_layout/"..name)

    -- We return the name and the layout for later usage.
    return name, Layouts[name]
end


-- This function acts as a wrapper which we will use to add our custom layout as a random set piece.
-- This function requires a Level Identification (LEVELTYPE.ALL, LEVELTYPE.SURVIVAL, LEVELTYPE.CAVE, LEVELTYPE.ADVENTURE, LEVELTYPE.TEST, LEVELTYPE.UNKNOWN, LEVELTYPE.CUSTOM).
local function AddRandomSetPiece(levelid, layout_name)
    -- Check to see if we want it on all level types.
    if levelid == LEVELTYPE.ALL then
        AddLevelPreInitAny(function(level)
            -- Ensure that we have a variable to store our data.
            if not level.random_set_pieces then
                level.random_set_pieces = {}
            end

            table.insert(level.random_set_pieces, layout_name)
        end)

    else -- We want it on a specific level type.
        AddLevelPreInit(levelid, function(level)
            -- Ensure that we have a variable to store our data.
            if not level.random_set_pieces then
                level.random_set_pieces = {}
            end

            table.insert(level.random_set_pieces, layout_name)
        end)
    end
end

-- This function acts as a wrapper which we will use to add our custom layout as a static set piece per level.
-- This function requires Level Identification (LEVELTYPE.ALL, LEVELTYPE.SURVIVAL, LEVELTYPE.CAVE, LEVELTYPE.ADVENTURE, LEVELTYPE.TEST, LEVELTYPE.UNKNOWN, LEVELTYPE.CUSTOM).
-- This function requires a list of tasks which can be located in "scripts/map/tasks.lua" file of the original game.
function env.AddSetPiece(levelid, layout_name, count, tasks, chance)
     
    -- Set chance to chance or 100 to ensure it spawns.
    chance = chance or 100

    -- Set count to count or 1 to ensure it spawns.
    count = count or 1
    
local function RandomChance()
        local _count = 0
 
        for i = 0, count do
            if (chance * 10) >= math.random(1, 1000) then
                _count = _count + 1
            end
        end
 
        return _count
    end
    -- Check to see if we want it on all level types.
    if levelid == LEVELTYPE.ALL then
        AddLevelPreInitAny(function(level)
            -- Ensure that we have a variable to store our data.
            if not level.set_pieces then
                level.set_pieces = {}
            end
 
            -- Initial set up.
            level.set_pieces[layout_name] = { count = 0, tasks = tasks }
 
            -- Add our layout to the room layouts ensuring at least one spawns.
            level.set_pieces[layout_name].count = RandomChance() or 0
        end)
 
    else -- We want it on a specific level type.
        AddLevelPreInit(levelid, function(level)
            -- Ensure that we have a variable to store our data.
            if not level.set_pieces then
                level.set_pieces = {}
            end
 
            -- Initial set up.
            level.set_pieces[layout_name] = { count = 0, tasks = tasks }

            -- Add our layout to the room layouts ensuring at least one spawns.
            level.set_pieces[layout_name].count = RandomChance() or 0
        end)
    end
end

-- This function acts as a wrapper which we will use to add our custom layout as a static set piece per room.
-- This function requires Room Idenficiation (A list of rooms can be found in rooms.lua).
-- The rooms.lua file is located in the root directory of this mod.
function env.AddRoomSetPiece(room_name, layout_name, count, chance)
     
    -- Set chance to chance or 100 to ensure it spawns.
    chance = chance or 100

    -- Set count to count or 1 to ensure it spawns.
    count = count or 1

local function RandomChance()
        local _count = 0
 
        for i = 0, count do
            if (chance * 10) >= math.random(1, 1000) then
                _count = _count + 1
            end
        end
 
        return _count
    end
 
    AddRoomPreInit(room_name, function(room)
        -- Ensure that we have a variable to store our data.
        if not room.contents.countstaticlayouts then
            room.contents.countstaticlayouts = {}
        end
 
        -- Initial set up.
        room.contents.countstaticlayouts[layout_name] = 0
 
        -- Add our layout to the room layouts ensuring at least one spawns.
        room.contents.countstaticlayouts[layout_name] = RandomChance() or 0
    end)
 
end

-- Initialize the task list.
require("map/tasks")
-- Obtain the global task list for usage.
local tasklist = require("map/tasks").sampletasks
-- This function will return all task names in the game, including those from mods.
local function GetTaskList()
    local task_names = {}
 
    for _, task in pairs(tasklist) do
       table.insert(task_names, task.id)
    end

    return task_names
end

-- Initialize our custom layout.
local mushrooms_little_farm_layout_name, mushrooms_little_farm_layout = AddLayout("mushrooms_little_farm")

AddSetPiece(LEVELTYPE.ALL, mushrooms_little_farm_layout_name, 1, GetTaskList(), 100)

 

As @Ipsquiggle   asked me, here is the topic to try to fix it.

So i used the new function added in the update.

Quote

local function GetAllTaskNames()
    local ret = {}
    for i,task in ipairs(taskdefinitions) do
        table.insert(ret, task.id)
    end
    for mod, tasks in pairs(modtaskdefinitions) do
        for i,task in ipairs(tasks) do
            table.insert(ret, task.id)
        end
    end
    return ret
end

Here is the new code :

Spoiler

-- GLOBAL variables which are needed.
local require = GLOBAL.require

--[[    Copyright © 2015 Ysovuka/Kzisor     ]]

-- The Layouts table is where we will add our set piece layout.
-- The file is provided in "scripts/map" of the original game.
local Layouts = require("map/layouts").Layouts

-- The StaticLayouts variable is used to get the layout of our set piece.
-- The file is provided in "scripts/map" of the original game.
local StaticLayouts = require("map/static_layout")

-- Thie LEVELTYPE table is used to indicate the level identification.
-- The file is provided in "scripts/map" of the original game.
require("map/level")
local LEVELTYPE = GLOBAL.LEVELTYPE
LEVELTYPE.ALL = "ALL"


-- This function acts as a wrapper which we will use to add our custom layout to the game.
-- The name of the layout will be identical to the filename.
local function AddLayout(name)
    -- Puts our layout in the Layouts table.
    Layouts[name] = StaticLayouts.Get("map/static_layout/"..name)

    -- We return the name and the layout for later usage.
    return name, Layouts[name]
end


-- This function acts as a wrapper which we will use to add our custom layout as a random set piece.
-- This function requires a Level Identification (LEVELTYPE.ALL, LEVELTYPE.SURVIVAL, LEVELTYPE.CAVE, LEVELTYPE.ADVENTURE, LEVELTYPE.TEST, LEVELTYPE.UNKNOWN, LEVELTYPE.CUSTOM).
local function AddRandomSetPiece(levelid, layout_name)
    -- Check to see if we want it on all level types.
    if levelid == LEVELTYPE.ALL then
        AddLevelPreInitAny(function(level)
            -- Ensure that we have a variable to store our data.
            if not level.random_set_pieces then
                level.random_set_pieces = {}
            end

            table.insert(level.random_set_pieces, layout_name)
        end)

    else -- We want it on a specific level type.
        AddLevelPreInit(levelid, function(level)
            -- Ensure that we have a variable to store our data.
            if not level.random_set_pieces then
                level.random_set_pieces = {}
            end

            table.insert(level.random_set_pieces, layout_name)
        end)
    end
end

-- This function acts as a wrapper which we will use to add our custom layout as a static set piece per level.
-- This function requires Level Identification (LEVELTYPE.ALL, LEVELTYPE.SURVIVAL, LEVELTYPE.CAVE, LEVELTYPE.ADVENTURE, LEVELTYPE.TEST, LEVELTYPE.UNKNOWN, LEVELTYPE.CUSTOM).
-- This function requires a list of tasks which can be located in "scripts/map/tasks.lua" file of the original game.
function env.AddSetPiece(levelid, layout_name, count, tasks, chance)
     
    -- Set chance to chance or 100 to ensure it spawns.
    chance = chance or 100

    -- Set count to count or 1 to ensure it spawns.
    count = count or 1
    
local function RandomChance()
        local _count = 0
 
        for i = 0, count do
            if (chance * 10) >= math.random(1, 1000) then
                _count = _count + 1
            end
        end
 
        return _count
    end
    -- Check to see if we want it on all level types.
    if levelid == LEVELTYPE.ALL then
        AddLevelPreInitAny(function(level)
            -- Ensure that we have a variable to store our data.
            if not level.set_pieces then
                level.set_pieces = {}
            end
 
            -- Initial set up.
            level.set_pieces[layout_name] = { count = 0, tasks = tasks }
 
            -- Add our layout to the room layouts ensuring at least one spawns.
            level.set_pieces[layout_name].count = RandomChance() or 0
        end)
 
    else -- We want it on a specific level type.
        AddLevelPreInit(levelid, function(level)
            -- Ensure that we have a variable to store our data.
            if not level.set_pieces then
                level.set_pieces = {}
            end
 
            -- Initial set up.
            level.set_pieces[layout_name] = { count = 0, tasks = tasks }

            -- Add our layout to the room layouts ensuring at least one spawns.
            level.set_pieces[layout_name].count = RandomChance() or 0
        end)
    end
end

-- This function acts as a wrapper which we will use to add our custom layout as a static set piece per room.
-- This function requires Room Idenficiation (A list of rooms can be found in rooms.lua).
-- The rooms.lua file is located in the root directory of this mod.
function env.AddRoomSetPiece(room_name, layout_name, count, chance)
     
    -- Set chance to chance or 100 to ensure it spawns.
    chance = chance or 100

    -- Set count to count or 1 to ensure it spawns.
    count = count or 1

local function RandomChance()
        local _count = 0
 
        for i = 0, count do
            if (chance * 10) >= math.random(1, 1000) then
                _count = _count + 1
            end
        end
 
        return _count
    end
 
    AddRoomPreInit(room_name, function(room)
        -- Ensure that we have a variable to store our data.
        if not room.contents.countstaticlayouts then
            room.contents.countstaticlayouts = {}
        end
 
        -- Initial set up.
        room.contents.countstaticlayouts[layout_name] = 0
 
        -- Add our layout to the room layouts ensuring at least one spawns.
        room.contents.countstaticlayouts[layout_name] = RandomChance() or 0
    end)
 
end

-- Initialize the task list.
require("map/tasks")
-- Obtain the global task list for usage.
local taskdefinitions = require("map/tasks").sampletasks
local modtaskdefinitions = require("map/tasks").sampletasks
-- This function will return all task names in the game, including those from mods.
local function GetAllTaskNames()
    local ret = {}
    for i,task in ipairs(taskdefinitions) do
        table.insert(ret, task.id)
    end
    for mod, tasks in pairs(modtaskdefinitions) do
        for i,task in ipairs(tasks) do
            table.insert(ret, task.id)
        end
    end
    return ret
end

-- Initialize our custom layout.
local mushrooms_little_farm_layout_name, mushrooms_little_farm_layout = AddLayout("mushrooms_little_farm")

AddSetPiece(LEVELTYPE.ALL, mushrooms_little_farm_layout_name, 1, GetAllTaskNames(), 100)

 

I obtain this error :

Quote


[00:00:44]: [string "../mods/Mushroom - Test/modworldgenmain.lua"]:157: bad argument #1 to 'ipairs' (table expected, got nil)
LUA ERROR stack traceback:

Spoiler


[00:00:43]: Mod: Mushroom - Test (Mushrooms Test)    Loading modworldgenmain.lua    
[00:00:44]: MOD ERROR: Mushroom - Test (Mushrooms Test): Mod: Mushroom - Test (Mushrooms Test)    
[00:00:44]: Mod: Mushroom - Test (Mushrooms Test)    Loading modmain.lua    
[00:00:44]: [string "../mods/Mushroom - Test/modworldgenmain.lua"]:157: bad argument #1 to 'ipairs' (table expected, got nil)
LUA ERROR stack traceback:
        =[C] in function 'ipairs'
        ../mods/Mushroom - Test/modworldgenmain.lua(157,1) in function 'GetAllTaskNames'
        ../mods/Mushroom - Test/modworldgenmain.lua(171,1) in main chunk
        =[C] in function 'xpcall'
        scripts/util.lua(609,1) in function 'RunInEnvironment'
        scripts/mods.lua(475,1) in function 'InitializeModMain'
        scripts/mods.lua(445,1) in function 'LoadMods'
        scripts/main.lua(250,1) in function 'ModSafeStartup'
        scripts/main.lua(305,1)
        =[C] in function 'SetPersistentString'
        scripts/mainfunctions.lua(22,1) in function 'SavePersistentString'
        scripts/modindex.lua(81,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(68,1) in function 'BeginStartupSequence'
        scripts/main.lua(304,1) in function 'callback'
        scripts/modindex.lua(521,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(495,1) in function 'Load'
        scripts/main.lua(303,1) in main chunk
[00:00:44]: [string "scripts/mainfunctions.lua"]:965: variable 'global_error_widget' is not declared
LUA ERROR stack traceback:
        =[C] in function 'error'
        scripts/strict.lua(23,1)
        scripts/mainfunctions.lua(965,1)
        =[C] in function 'SetPersistentString'
        scripts/mainfunctions.lua(22,1) in function 'SavePersistentString'
        scripts/modindex.lua(81,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(68,1) in function 'BeginStartupSequence'
        scripts/main.lua(304,1) in function 'callback'
        scripts/modindex.lua(521,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(495,1) in function 'Load'
        scripts/main.lua(303,1) in main chunk
[00:00:44]: DoLuaFile Error: (null)
[00:00:44]: LuaError but no error string

And i'm unable to fix it.

 

Here the test mod i'm using for my test.

Mushroom - Test.rar

Edited by Lumina
Link to comment
https://forums.kleientertainment.com/forums/topic/66255-adding-set-piece/
Share on other sites

  • Developer

Pretty close! But all you need to do is this, at the bottom:

-- Initialize the task list.
local Tasks = require("map/tasks")

-- Initialize our custom layout.
local mushrooms_little_farm_layout_name, mushrooms_little_farm_layout = AddLayout("mushrooms_little_farm")

AddSetPiece(LEVELTYPE.ALL, mushrooms_little_farm_layout_name, 1, Tasks.GetAllTaskNames(), 100)

Basically, I wrote the GetAllTaskNames function so that modders don't have to anymore. So first you get a reference to "map/tasks", and then call the GetAllTaskNames() function to get the list you need. :)

Let me know if that works or if there are further problems!

3 hours ago, Ipsquiggle said:

Basically, I wrote the GetAllTaskNames function so that modders don't have to anymore. So first you get a reference to "map/tasks", and then call the GetAllTaskNames() function to get the list you need. :)

Let me know if that works or if there are further problems!

Oohhhhhh ok :D

All seems to work fine at the moment. No crash, no error log and set piece appears in game.

It's wonderfull, thanks for all your help. I'm happy. Set piece ! Set piece everywhere !

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...