I have created a new enemy unit, with it's own force. Vampire unit & force.
I'm trying to accomplish the following. When a Vampire unit kills another vanilla enemy, a new vampire unit will spawn.
This was my approach, when a small biter dies, look in the immediate area and see if there is a vampire biter. if there is one, assume that it killed the biter and spawn a new vampire biter.
I'm not 100% sure how to search the area around the dead biter and if I need to use "find_non_colliding_position" when spawning the new unit.
Below is my code attempt, with some alternative possibilities commented out. Could I please have someone review this and give me pointers?
Code: Select all
---------------------------------------------
script.on_event({defines.events.on_entity_died,},function(event) On_Remove(event) end)
---------------------------------------------
function On_Remove(event)
-- When a small biter gets killed.
if event.entity.name == "small-biter" then
local surface = game.get_surface(0)
local radius = 1
local pos = event.entity.position
local area = {{pos.x - radius, pos.y - radius}, {pos.x + radius, pos.y + radius}}
-- find nearby Vampire biters
local V_Biters = surface.find_entities_filtered{area=area, name="small-vampire"}
--local V_Biters = surface.find_enemy_units(pos, radius, vampire) -- Alternative possibility?
--local V_Biter = surface.find_entities_filtered{area=area, force="vampire"} -- Alternative possibility?
if V_Biters then
player.surface.create_entity{name="small-vampire", position={pos}, force=game.forces.vampire}
-- player.surface.create_entity{name="small-vampire", position={PosX,PosY}, force=game.forces.vampire} -- Alternative possibility?
--SpawnVampire(event.entity) -- Alternative possibility using below function
end
end
end
function SpawnVampire(enemy)
local SpawnPosition = enemy.surface.find_non_colliding_position(enemy, enemy.position, 2, 0.5)
if SpawnPosition then
enemy.surface.create_entity({name = "small-vampire", position = SpawnPosition, force = game.forces.enemy})
end
end