Page 1 of 1

"can_place_entity" question

Posted: Tue Jul 18, 2017 6:34 pm
by TheSAguy
Hi,

I'm using "can_place_entity", but still I'm getting units spawned on water tiles. (I thought it would check for water tiles)

This is the code:

Code: Select all

	while Dens_Spawned < 10 do
		
		local surface = game.surfaces['nauvis']
		local pos_x = math.random(-200,200)
		local pos_x = math.random(-200,200)
	
		if surface.can_place_entity({ name="alien-army-26", position={pos_x, pos_y}, force = game.forces.alien}) then					
			
			local lords = surface.create_entity({name="alien-army-26", position={pos_x, pos_y},force = game.forces.alien})						
			Dens_Spawned = Dens_Spawned + 1
			Scatter = Scatter + 100	
			
		end
		
	end
Image

Next I tried looking at the tile, to see if it's a water tile, but I can't seem to get "get_tile" working during "script.on_init"

Code: Select all

script.on_init(on_initialize)


local waterTiles =
{
  ["deepwater"] = true,
  ["deepwater-green"] = true,
  ["water"] = true,
  ["water-green"] = true
}



function Initial_Spawn(surface)

	local spawn_zone = 100
	local Dens_Spawned = 0
	local Scatter = 0
	local chart_radius = 10
	local radius_from_player = settings.startup["Alien_Distance"].value
	
	while Dens_Spawned < 20 do
		
		local surface = surface
		local pos_x = math.random(-200,200)
		local pos_y = math.random(-200,200)
		
		local currentTilename = game.surfaces[1].get_tile(pos_x,pos_y).name
		
		if surface.can_place_entity({ name="alien-army-26", position={pos_x, pos_y}, force = game.forces.alien}) and not waterTiles[currentTilename] then
					
			local lords = surface.create_entity({name="alien-army-26", position={pos_x, pos_y},force = game.forces.alien})	
			
nd			
			
			Dens_Spawned = Dens_Spawned + 1
			Scatter = Scatter + 100
		else 
		
			Dens_Spawned = Dens_Spawned
			
		end
		
	end

	
    return lords
end



Image

Last question for now.
Besides using "surface = game.surfaces['nauvis']" is there a better surface to use? I've had some issues in the past with hard coding the surface in my other mods, but can't use event or created_entity or any of those here.

I've attached the file in question and full mod.


Thanks.

Re: "can_place_entity" question

Posted: Tue Jul 18, 2017 7:33 pm
by Bilka
Does the thing that you try to spawn have the "water_tile" collision mask?

Re: "can_place_entity" question

Posted: Tue Jul 18, 2017 8:12 pm
by TheSAguy
Well, it's a small-biter that get's cloned. Units don't have a Collision Mask.
I've added "collision_mask = {"water-tile"}," to the unit, but it still has the same result.

Re: "can_place_entity" question

Posted: Tue Jul 18, 2017 9:46 pm
by Rseding91
You want to use the surface from what ever event you're running your code.

If you just want to do a generic "on the game" then you iterate all surfaces and work on all of them.

If you're doing something say in reaction to a given player you iterate the players and use the surface off the given player.

If you do something in the on-built event you use the surface from built entity.

Re: "can_place_entity" question

Posted: Wed Jul 19, 2017 1:48 am
by TheSAguy
Rseding91 wrote:You want to use the surface from what ever event you're running your code.

If you just want to do a generic "on the game" then you iterate all surfaces and work on all of them.

If you're doing something say in reaction to a given player you iterate the players and use the surface off the given player.

If you do something in the on-built event you use the surface from built entity.
Thanks for this summary Rseding, this was very helpful. Should probably be in the Wiki if not already.

I'm still unsure what to use on "on_init", when I create a spawner:
on_inot
All the examples I've seen in other mods use "surface = game.surfaces['nauvis']" or "surface = game.surfaces['1]"
IS that correct?

Any input on "can_place_entity" or "get_tile"
Thanks.