Alpha 15: Adding an enemy to microworld
(I am using the title as a note to myself to track compatibility with versions.) When needing to test enemies that attack (and are attacked) by your citizens, we’ll need to follow a set of steps. I prefer using Radiant’s Microworld due to its similarity to the main game and simplicity of use. We’ll spawn our enemy and equip him with a weapon, but we’ll also add him to another population hostile to our player.
Stonehearth uses populations as a way to manage, well, populations, players, factions, and the like. They share a relation map (called amenity map in the code) with each other which determines how each population regards the others. I have not yet found a way to make an entity that attacks another entity while belonging to the same faction, though I’m sure some hack is possible.
- Let’s create a population. Open
microworld.lua
. Under where it sayslocal LOCAL_PLAYER = 'player_1'
, let’s add:
local LOCAL_ENEMY = 'enemy1'
- That was the alias of our testing enemy population. Now to register it as an in-game population, find where it says
stonehearth.player:add_player(LOCAL_PLAYER, 'stonehearth:kingdoms:ascendancy')
, and add below:
stonehearth.population:add_population(LOCAL_ENEMY, 'stonehearth:kingdoms:undead')
- We added undead to the game. Now below the last line, add the next one to make the undead and the player at war with one another:
stonehearth.player:set_amenity(LOCAL_ENEMY, LOCAL_PLAYER, 'hostile')
We’re set to add some undead to the game now. While adding a custom enemy faction and a custom enemy are beyond the scope of this note, in general, keep in mind that they should be created with the faction in mind first.
I have used the function MicroWorld:place_citizen()
as a template for writing the following function. It was placed immediately following it in the code.
function MicroWorld:place_enemy(x, z)
local pop = stonehearth.population:get_population(LOCAL_ENEMY)
local foe = pop:create_new_citizen('skeleton')
foe:add_component 'render_info':set_scale(0.5)
local weapon = radiant.entities.create_entity('stonehearth:weapons:jagged_cleaver')
weapon:add_component 'render_info':set_scale(0.5);
radiant.entities.equip_item(foe, weapon)
local town = stonehearth.town:get_town('player_1')
radiant.terrain.place_entity(foe, Point3(x, 1, z))
return foe
end
Using this function we are given the ability to spawn a giant enemy skeleton. To make him a garden-variety enemy skeleton, remove the render_info
parts.
To use this ability, we navigate to mini_game_world.lua
, and add microworld:place_enemy(-16, 10)
to the bottom of the function MiniGame:start
.
TODO:
- Engine error after killing skeleton
- Skeleton’s attacks really slow, why?