How create biters is not closer to a certain area?

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

How create biters is not closer to a certain area?

Post by WIZ4 »

I want to make a script that in time creates in random place spitter spawners
But they should not appear be created to a certain area(red zone)
How can I do it? All my attempts were failed
I hope this picture explains:
Безымянный-1.jpg
Безымянный-1.jpg (113.71 KiB) Viewed 830 times

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  local interval = 10 * 60 * 1 
    if game.tick >= interval then
    if (game.tick % interval) == 0 then
	game.print{"msg.intro"}
	for y=-1,1 do   for x=-1,1 do   
	thisX = math.random(-222,-100) thisY = math.random(-222,-100)
	thisXa = math.random(100,222) thisYa = math.random(100,222)      
	thistiletype = game.surfaces.nauvis.get_tile(thisX, thisY).name 
	if thistiletype ~= "water" and thistiletype ~= "deepwater" then
	game.surfaces.nauvis.create_entity({ name="spitter-spawner", amount=1, position={math.random(thisX,thisXa),math.random(thisY,thisYa)}})
	end    
	end  
	end
	end
	end

end)
My native language is russian. Sorry if my messages are difficult to read.

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: How create biters is not closer to a certain area?

Post by orzelek »

You have to many random calls.
Don't call random again when creating an entity.
First set of calls gets you two good positions on both sides of rectangle.
You need to roll two 0/1 numbers to chose one of those two and then you should have position inside rectangle you want.

Then don't call the create entity - first use can_place_entity to check if spawner will fit in there(instead that tile check).
If it's ok then spawn entity otherwise reroll.

Post Reply

Return to “Gameplay Help”