Jump to content

Recommended Posts

I'm trying to do something similar to the sunken chests, where different preset loot configurations are based on 'weight'. But I'm doing it from inside the prefab file rather than something like 'messagebottletreasures'. And I'm not exactly sure how to actually go ahead with this?

I'd really appreciate the help!

Here's my table of possible treasures that I want to spawn in my chest. These shouldn't be pre-generated on the chest's creation, since I'm planning on having the treasure scale with a 'loot value' when unlocked later on.

Spoiler

local possible_treasures = { -- Placeholder loot, add more later when working. How to spawn these into our chest...?

    preset_configurations = {
        bananabonanza = {chance = 3, --30%
            guaranteed_loot = { --Guaranteed loot always spawns.
                cave_banana = {4, 6},
            },
            randomly_selected_loot = { --Randomly selected loot is, well, random.
                {bananajuice = 1, frozenbananadaiquiri = 1},
            },
        },

        pollyroger={chance = 3, --30%

            guaranteed_loot = {
                blackflag = {4, 8},

            },
            randomly_selected_loot = {
                {monkey_mediumhat = 1, feather_canary = 1},
            },
        },

        palmcone={chance = 2, --20%

            guaranteed_loot = {
                palmcone_scale = {4, 8},

            },
            randomly_selected_loot = {
                {boat_cannon_kit = 1, rope = 1},
            },
        },

        monkeyequipment={chance = 1, --10%

            guaranteed_loot = {
                monkey_smallhat = {1, 2},
            },
            randomly_selected_loot = {
                {cutless = 1, oar_monkey = 1},
            },
        },

        crustashine = {chance = 1, --10%

            guaranteed_loot = {
                lightcrab = {1, 2},
            },
            randomly_selected_loot = {
                { lightbulb = 1, slurtle_shellpieces = 1 },
            },
        },
    }
}

 

Whoops. forgot I made this topic!

I've figured it out, I think I was just over complicating things in my head. Here's my Table and function for anyone else that wants to do something similar.

Treasure Table:

Spoiler
--guaranteed_loot is treasure that we'll ALWAYS give. Only ONE treasure from randomly_selected_loot is given, split between chance.

local preset_treasure_configurations = {
	bananabonanza = {chance = 3, --30%
		guaranteed_loot = {
			cave_banana = {10, 20},
			cave_banana_cooked = {10, 20},
			dug_bananabush = {1, 3},
		},
		randomly_selected_loot = {
			{bananajuice = 1, frozenbananadaiquiri = 1}, 
			--Example: Gives one, 50% chance to be either Banana Shake or Frozen Banana Daiquiri.
		},
	},

	pollyroger={chance = 3, --30%
		guaranteed_loot = {
			blackflag = {4, 8},
		},
		randomly_selected_loot = {
			{monkey_mediumhat = 1, feather_canary = 1},
		},
	},

	notcannon={chance = 2, --20%
		guaranteed_loot = {
			palmcone_scale = {4, 8},
		},
		randomly_selected_loot = {
			{boat_cannon_kit = 1, rope = 1},
		},
	},

	monkeyequipment={chance = 1, --10%
		guaranteed_loot = {
			monkey_smallhat = {1, 2},
		},
		randomly_selected_loot = {
			{cutless = 1, oar_monkey = 1},
		},
	},

	crustashine = {chance = 1, --10%
			guaranteed_loot = {
			lightcrab = {1, 2},
		},
		randomly_selected_loot = {
			{ lightbulb = 1, slurtle_shellpieces = 1 },
		},
	},
}

local weighted_treasure_contents = {}
for preset, data in pairs(preset_treasure_configurations) do
	print()
	print("----------")
    print("Preset name: "..preset)
    print("Chance of generating: "..data.chance)
	print("----------")
	weighted_treasure_contents[data] = data.chance
end

 


Treasure Generation:

Spoiler
local function GenerateLoot(inst)
	print("Generating contents...")
	if inst.components.container ~= nil then
		
		local lootpreset = weighted_random_choice(weighted_treasure_contents)
		local prefabstospawn = {}

		--==Grabbing loots from chosen table==--
		if lootpreset.guaranteed_loot ~= nil then
			for itemprefab, count in pairs(lootpreset.guaranteed_loot) do
				local total = type(count) ~= "table" and count or math.random(count[1], count[2])
				for i = 1, total do
					table.insert(prefabstospawn, itemprefab)
				end
			end
		end

		if lootpreset.randomly_selected_loot ~= nil then
			for i, one_of in ipairs(lootpreset.randomly_selected_loot) do
				table.insert(prefabstospawn, weighted_random_choice(one_of))
			end
		end

		--==Spawn our items in chest==--
		local item = nil
		for i, itemprefab in ipairs(prefabstospawn) do
			item = SpawnPrefab(itemprefab)
			if inst.components.container ~= nil then
				inst.components.container:GiveItem(item)
			end
		end
	end
end

 



 

Edited by DingoLingo

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