Jump to content

Recommended Posts

Hello,

 

Thanks to the very useful tutorial of @Kzisor,i've manage to make a set piece and make it appears in my world.

 

But this set piece appears a little too much for my taste.

 

Here is the code i'm using.

 

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

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


-- 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(hive_vine)
    -- Puts our layout in the Layouts table.
    Layouts[hive_vine] = StaticLayouts.Get("map/static_layout/hive_vine")

    -- We return the name and the layout for later usage.
    return hive_vine, Layouts[hive_vine]
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()
        if (chance * 10) >= math.random(1, 1000) then
            return count
        end

        return nil
    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()
        if (chance * 10) >= math.random(1, 1000) then
            return count
        end

        return nil
    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 = GLOBAL.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 example_layout_name, example_layout = AddLayout("hive_vine")

AddRandomSetPiece(LEVELTYPE.ALL, example_layout_name)
AddSetPiece(LEVELTYPE.ALL, example_layout_name, 2, GetTaskList(), 50)
AddRoomSetPiece("Forest", example_layout_name, 2, 50)

 

Do i have to add or modify a value ? I want one to three occurence of this set piece but nothing more if possible.

 

Also, if i want to have multiple set piece, what part of the code i must repeat ?

 

And, finally, i've keep (for the moment), the "example_layout_name" and "example_layout" because i'm still not sure of what and how rename here. If i do :

 


local hive_vine_name, hive_vine = AddLayout("hive_vine")

AddRandomSetPiece(LEVELTYPE.ALL, hive_vine_name)
AddSetPiece(LEVELTYPE.ALL, hive_vine_name, 2, GetTaskList(), 50)
AddRoomSetPiece("Forest", hive_vine_name, 2, 50)

 

Would it work ? Or this value must keep the "layout" part ?

 

(And again, a great thanks to all of you, because without all the help i wouldn't be able to do something)

Link to comment
https://forums.kleientertainment.com/forums/topic/55880-custom-set-piece/
Share on other sites

I would recommend to not use Kzisor's code there, simply because it makes it so that there's some percentage chance that the set piece will appear, say, 3 times, and otherwise it won't appear at all. Unless that's what you want to do, obviously.

 

EDIT: Or, you could do something like this:

AddSetPiece(LEVELTYPE.ALL, hive_vine_name, math.random(3), GetTaskList(), 100)

This will generate one to three set pieces on the world.

 

Also, answering your question about "example_layout" and "example_layout_name" - sure, you can rename those.

Edited by przemolsz

@przemolsz, not using the code will just mean you have to reinvent the wheel.

 

@Lumina, the reason it's spawning too much is because you're adding it in 3 different locations. You're adding it to the Random Set Piece list, the Level Set Piece list and the Room Set Piece list. Choose how you want it to spawn and then use that single function. 

 

Also that reminds me I needed to update that tutorial. I've since changed the code to coincide with the changes I've made in Additional Set Pieces. Here is the updated RandomChance function.

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

Copy/paste it over the random chance function you have in your code. There should 2 two instances of that function.

Edited by Kzisor

@przemolsz, Ok, thanks for the rename part. I'm still have a hard job to try to understand all the code part and be able to add set piece, even with restrictions, is far more better than all i was able to hope :-)

 

@Kzisor, Ok, it's done. But i still have too many set piece even when adding it to only one list.

 

Here the code.

 

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

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

-- 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 = GLOBAL.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 hive_vine_layout_name, hive_vine_layout = AddLayout("hive_vine")
oasis_ananas_layout_name, oasis_ananas_layout = AddLayout("oasis_ananas")
vine_and_berries_layout_name, vine_and_berries_layout = AddLayout("vine_and_berries")

AddRoomSetPiece("Forest", hive_vine_layout_name, 1, 50)
AddRoomSetPiece("Forest", vine_and_berries_layout_name, 1, 50)
AddRoomSetPiece("BGBadlands", oasis_ananas_layout_name, 1, 50)

 

I obtain like more than ten of one of this setpiece in a single world, so what i'm doing wrong ?

Do i misunderstand the "room" part and must use another setpiece type ?

I want my setpiece to spawn only in some terrain type (it's why i use RoomSetPiece) but maybe i make a mistake ?

 

Thanks again :)

@Lumina, the mistake you're making is you think "room" means a complete biome. In actuality a biome is made up of a bunch of rooms, including multiples of the same room. It's one of the more common mistakes you make when looking at the code. Current you have a 50% chance to spawn, reduce that amount to below 25% and it won't spawn as often.

Hello again.

It is possible to do something like it ?

 

AddSetPiece(LEVELTYPE.ALL, hive_vine_layout_name, 1, GetTaskList(), 100)
AddSetPiece(LEVELTYPE.ALL, hive_vine_layout_name, 1, GetTaskList(), 25)
 

And this way have one garanted set piece, and another with less chance to spawn ? Or it's not a good thing to proceed this way ? (Duplicate the "addsetpiece", i mean. Could it create any issue or something ?)

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...