I've tried to figure it out by myself, but it's hard and I'm pretty quick getting confused and lost in wiki references (where I rarely find anything useful tbh).
I want building to be forced to be created at least X distance from another building of the same type (by type I mean entity name property). So player can't place 2 of them one by another, but can build them next to other buildings.
Distance between buildings
Distance between buildings
My mods:
- Power Mod (WiP)
- Power Mod (WiP)
Re: Distance between buildings
You'll need to hook to defines.events.onbuiltentity and check all conditions manually, i.e. using game.findentitiesfiltered{name=...} in required radius and destroying built entity if anything found (also inserting item back to the player) .
Re: Distance between buildings
edit: aw Dark replied first, lol
edit: wow, I made a slight mistake here, the () for findentitiesfiltered and insert should be {} (since they take tables, not a list of parameters)... sorry
Fairly sure that's not possible via prototypes (though I could be wrong), my initial idea for something like that would be monitoring it via onbuiltentity and usingwith the area being the bounding box you don't want them to be constructed near. It automatically checks the first entity in the table ([1]) since findentities(filtered) will always return a table (even if it's empty) so there's no possibility of indexing a nil value.
That however won't catch robot built ones... but that's just the limitation of the events and hopefully it'll eventually change.
edit: wow, I made a slight mistake here, the () for findentitiesfiltered and insert should be {} (since they take tables, not a list of parameters)... sorry
Fairly sure that's not possible via prototypes (though I could be wrong), my initial idea for something like that would be monitoring it via onbuiltentity and using
Code: Select all
if game.findentitiesfiltered(name="thing", area=bb)[1] then event.createdentity.destroy() game.player.insert(name="thing", count=1) end
That however won't catch robot built ones... but that's just the limitation of the events and hopefully it'll eventually change.
Re: Distance between buildings
So if area parameter for function is set to 5, then it will detect any building 5 squares from my building (starting from center), yes?
My mods:
- Power Mod (WiP)
- Power Mod (WiP)
Re: Distance between buildings
The area parameter takes a 'bounding box' which is a set of two postions representing the top left and bottom right of a square.Forien wrote: So if area parameter for function is set to 5, then it will detect any building 5 squares from my building (starting from center), yes?
I tend to use this function to simplify creating my bounding boxes
Code: Select all
function getboundingbox(position, radius)
if not position then error("Invalid position!", 2) end -- just error checking
if not radius then local radius = 1 end -- default radius to one if not provided
return {{position.x-radius, position.y-radius}, {position.x+radius,position.y+radius}}
end
Code: Select all
game.onevent(defines.events.onbuiltentity, fucntion(event)
if event.createdentity.name == "thing" then
if game.findentitiesfiltered{name="thing", area=getboundingbox(event.createdentity.position, 2.5)}[1] then
event.createdentity.destroy()
game.player.insert{name="thing", count=1} -- note () changed to {} here, that was a mistake in my last post...
end
end
end)
Re: Distance between buildings
Oh, ok
I will do that and as soon as I try, I will post if it worked inside onbuildentity.

My mods:
- Power Mod (WiP)
- Power Mod (WiP)