Page 1 of 1

How create biters is not closer to a certain area?

Posted: Mon Jan 15, 2018 9:23 pm
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 1123 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)

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

Posted: Mon Jan 15, 2018 10:37 pm
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.