Distance between buildings

Place to get help with not working mods / modding interface.
Post Reply
Forien
Inserter
Inserter
Posts: 26
Joined: Sat Jul 19, 2014 9:16 am
Contact:

Distance between buildings

Post 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.
My mods:
- Power Mod (WiP)

Dark
Long Handed Inserter
Long Handed Inserter
Posts: 83
Joined: Wed May 07, 2014 12:45 pm
Contact:

Re: Distance between buildings

Post 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) .

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Distance between buildings

Post 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.

Forien
Inserter
Inserter
Posts: 26
Joined: Sat Jul 19, 2014 9:16 am
Contact:

Re: Distance between buildings

Post 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?
My mods:
- Power Mod (WiP)

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Distance between buildings

Post 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).

Forien
Inserter
Inserter
Posts: 26
Joined: Sat Jul 19, 2014 9:16 am
Contact:

Re: Distance between buildings

Post by Forien »

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)

Post Reply

Return to “Modding help”