2.2 - Critters all around!
So now we’re going to look at how to make new critters to be captured by the trappers. First of all though; I’d like to give thanks to @RepeatPan with his solution, without that this section would not exist!
We’ll be using the mandragora once again for this. Though now we’re stripping away a lot of components and other unnecessary stuff, and only focus on making it into a pure critter. So let’s look at its new description file.
{
"type" : "entity",
"mixins" : "stonehearth:mixins:critter",
"components":
{
"render_info":
{
"animation_table" : "critter_ex:skeletons:mandragora",
"scale" : 0.1
},
"model_variants":
{
"default":
{
"models" : [ "file(mandragora.qb)" ]
}
},
"unit_info":
{
"name" : "A Mandragora",
"description" : "A rare, dangerous, and mind-numbingly loud plant",
"player_id" : "critters"
}
},
"entity_data":
{
"stonehearth:harvest_beast_loot_table":
{
"num_rolls":
{
"min" : 1,
"max" : 1
},
"items":
[
{ "uri" : "critter_ex:mandrake:leaves", "weight" : 4 },
{ "uri" : "critter_ex:mandrake:root", "weight" : 2 }
]
}
}
}
Notice that we’re using critter as a mixin here; it holds a lot of data specific for all critters, and because of that we don’t have to add that much more to this except for data specific for this critter (of course).
Looking at "entity_data,"
we can see that it’s got its loot table just as decribed in 2.1.2. The difference here is that we’re using "stonehearth:harvest_beast_loot_table"
instead of "stonehearth:destroyed_loot_table."
There are a few differences between them: "destroyed_loot_table"
is what the entity will drop when it’s destroyed at any time now matter the circumstance, and the loot will always belong to the faction that the entity belonged to (so the player will have to use the loot command to take the items), while "stonehearth:harvest_beast_loot_table"
is specific for when the entity is ‘harvested’ through the trapper, which means that the loot will only drop by such an event and it will always belong to the faction which ‘harvested’ the poor creature (removing the need of the loot command).
Right, now the critter is ready to go; we just need to have it become a magnet for traps. For that we have to look at some Lua code, and add something new to the manifest. Let’s look at the manifest before getting into the nitty-gritty.
{
"info":
{
"name" : "Critter Example",
"version" : 1
},
"server_init_script" : "file(scenario_server)",
"aliases":
{
"mandragora" : "file(entities/mandragora)",
"skeletons:mandragora" : "file(data/rigs/mandragora)",
"mandrake:leaves" : "file(entities/mandrake_leaves)",
"mandrake:root" : "file(entities/mandrake_root)"
}
}
I bet you’ve noticed the new entry here: "server_init_script."
Basically, this points to a script file which is run, on the server side, at the game’s start-up. There is also another one ("client_init_script"
) for the client side, but it’s not needed here so we’re ignoring that. One thing I’d like to point out is that the file extension must be excluded, or else it won’t run (for some reason).
Now we can take a look at the Lua file.
local rng = _radiant.csg.get_default_rng()
local critter_ex = class()
function critter_ex:_init()
local component = radiant.mods.require('stonehearth.components.trapping.trapping_grounds_component')
local old_initialize = component.initialize
function component:initialize(...)
local ret = { old_initialize(self, ...) }
self._spawnable_critters = self._spawnable_critters or { 'stonehearth:rabbit', 'stonehearth:racoon', 'stonehearth:red_fox', 'stonehearth:squirrel' }
-- This is where we add our own critter. If you have multiple critters you'd like to add; copy the below line for each critter.
table.insert(self._spawnable_critters, 'critter_ex:mandragora')
return unpack(ret)
end
function component:_choose_spawned_critter_type()
return self._spawnable_critters[rng:get_int(1, #self._spawnable_critters)]
end
end
radiant.events.listen_once(radiant, 'radiant:required_loaded', critter_ex, critter_ex._init)
return critter_ex()
Again, this is from @RepeatPan’s solution, so all credit goes to him.
This tutorial won’t go into detail exactly how this works. Let’s just say that we’re getting the trapping grounds component, and changing a couple of functions in it into our own implementation which allows us to add new critters.
With this kind of implementation, even if several mods were to add their own critter that can be captured, it will work for all of them, which makes this solution very powerful in that regard. But that’s assuming that all of the mods are using exactly this solution.
If you’re using this implementation for yourself; be sure to change 'critter_ex:mandragora'
to your own critter, and also change the 'critter_ex'
variable to the name of your mod.
To close this up; let’s take a look at some poor critters in cages (because why not).
The mandragora is not pleased.
A wild, and tiny, golem. It looks confused as to how it got there.