Page 1 of 1

Distance between buildings

Posted: Thu Jul 24, 2014 7:54 pm
by Forien
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.

Re: Distance between buildings

Posted: Thu Jul 24, 2014 8:01 pm
by Dark
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

Posted: Thu Jul 24, 2014 8:05 pm
by FreeER
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 using

Code: Select all

if game.findentitiesfiltered(name="thing", area=bb)[1] then event.createdentity.destroy() game.player.insert(name="thing", count=1) end
with 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.

Re: Distance between buildings

Posted: Thu Jul 24, 2014 8:10 pm
by Forien
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?

Re: Distance between buildings

Posted: Thu Jul 24, 2014 8:18 pm
by FreeER
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?
The area parameter takes a 'bounding box' which is a set of two postions representing the top left and bottom right of a square.
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 
so getboundingbox(event.createdentity.position, 2.5) would check 2.5 blocks in each direction from (I think) the center of the built entity. Which would make the onbuiltentity code look like so

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)
edit: ...I seem to faintly recall that there was a bug in destroying an entity within onbuiltentity and the devs 'fixed' it by ignoring the call to destroy...but I can't seem to find a bug report now so, perhaps I'm mistaken. Still, if you try this and find that it's not working it may be because of that (and if so it's still doable, but you'd need to store the entity in a table and then destroy and return the item in ontick instead of directly in onbuiltentity).

Re: Distance between buildings

Posted: Thu Jul 24, 2014 8:48 pm
by Forien
Oh, ok :) I will do that and as soon as I try, I will post if it worked inside onbuildentity.