Jump to content

How to change lootchance for an existing entity?


Recommended Posts

Hello, I need help :D

Basically I want to change the chance of an item dropping for an entity, specifically I want to make birds have lower chance of dropping a morsel and instead drop feathers more frequently, but I don't know how to do this cause I am dumb.

Hopefully someone smart can show me the way, thank you for reading :D!

Edited by Warbucks
Link to comment
Share on other sites

There's a various way of setting loot tables. But if you want to tweak birds loots, You can access its loot table by

inst.components.lootdropper.randomloot

So, let's say we already know the substantial drop table of crow already.
It is 50%(feather) and 50%(smallmeat). And we will prove this by iterating crow's randomloot.
Since it is a table, you can iterate it by using pairs as you know. And below are pseudo-command-result lines.

for k, v in pairs(CROW.components.lootdropper.randomloot) do 
  print(k, v) 
end)

So first, we can just look randomloot directly. The result would

1	table: 0CF356C0	
2	table: 0CF35530	

like this. Which means, 
CROW.components.lootdropper.randomloot[1] references table: 0CF356C0
CROW.components.lootdropper.randomloot[2] references table: 0CF35530 
and surely you can iterate each randomloot's elements by iterating tables again. Like

for k, v in pairs(CROW.components.lootdropper.randomloot[1]) do 
  print(k, v) 
end)

this. But well, I'll use this one to see all of those at once.

for k, v in pairs(CROW.components.lootdropper.randomloot) do
	for k2, v2 in pairs(v) do
		print(k2, v2)
	end
  	print()
end

And the result would like this.

prefab	feather_crow	
weight	1	

prefab	smallmeat	
weight	1	

It means,
CROW.components.lootdropper.randomloot[1]["prefab"] is "feater_crow"
CROW.components.lootdropper.randomloot[1]["weight"]
is 1
CROW.components.lootdropper.randomloot[2]["prefab"] is "smallmeat"
CROW.components.lootdropper.randomloot[2]["weight"]
is 1
And this intuitively gives us the information about what loots inside and it seems weight one is the chance thing. Right?

So we can test by changing each weight you want to prove.

AddPrefabPostInit("crow", function(inst)
	if not GLOBAL.TheWorld.ismastersim then
		return
	end
	
	inst.components.lootdropper.randomloot[2]["weight"] = 0
end)

So this would make smallmeat not to be dropped. 
But after murdering some crows, soon will know there's a chance to have given nothing that was smallmeat.
Then we can change the 
weight to 0.25
After genociding some, crows, we'll know that there still could be a chance to give nothing but you can obtain smallmeat quite rarely.

That and some more tests will give you proof that substantial loot table of the above case is 50%(feather) 12.5%(smallmeat) 37.5%(nothing).

Then how to code for birds? Here's the code. But I have to sleep now so no explain.

local BIRDS = {"crow", "robin", "robin_winter", "canary"}
local FEATHER_CHANCE_INCREASE_MODIFIER = 5 -- 1 means 33.33% of meat will drop, 2 -> 25%, ..., n -> 1/(2+n) (n must natural)

for _, v in pairs(BIRDS) do
	AddPrefabPostInit(v, function(inst)
		if not GLOBAL.TheWorld.ismastersim then
			return
		end
		
		
		for k, v in pairs(inst.components.lootdropper.randomloot) do
			if v["prefab"] == "smallmeat" then
				for i = 1, FEATHER_CHANCE_INCREASE_MODIFIER do
					inst.components.lootdropper:AddRandomLoot("feather_"..inst.prefab, 1)
				end
			end
		end
	end)
end

Please read it and re-code it and test it to tune as you want. 

Link to comment
Share on other sites

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
 Share

×
  • Create New...