Page 1 of 1

Coding Help Needed

Posted: Thu Apr 14, 2016 4:11 pm
by TheSAguy
Hi,

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

Thanks!

Re: Coding Help Needed

Posted: Thu Apr 14, 2016 5:20 pm
by Adil
I'm not good at C++ so no pointers :P
The code you've made looks like it could work. At least for default surface and a single type of vampires.
Though I'm not sure if

Code: Select all

game.get_surface(0) 
is correct syntax at all. (I believe a name of surface should be instead of zero there.) But generally, you don't need that function, you can obtain relevant surface from the entity which is given to handler. (See example in my code below.)
Also, if there's more than one type of vampires it'd be indeed better to use the third variant of find_entities:

Code: Select all

local V_Biter = surface.find_entities_filtered{area=area, force="vampire"}
As I said, I'm not good with pointers and reviewing. When I see code, alternatives of how would I do the same keep popping out in my head. So below under spoiler my untested implementation is provided. Feel free to examine or use it.
My code
edit: code bugfix

Re: Coding Help Needed

Posted: Thu Apr 14, 2016 6:04 pm
by TheSAguy
OMG Adil!
This looks great, can't wait to give it a try.

Thanks.