What is surface.find_entities centered around?
Posted: Sun Jan 03, 2016 8:39 pm
What does find_entities get centered around? I want to make an entity that can detect when the player walks into it.
www.factorio.com
https://forums.factorio.com/
Code: Select all
function MinedEntity(event)
if event.entity.name == "concrete-lamppost" then
local b = event.entity
local X = b.position.x
local Y = b.position.y
lamp = b.surface.find_entities_filtered{area = {{X -0.5, Y - 0.5 }, {X + 0.5 , Y +0.5 }}, name= "concrete-lamp"}
if lamp[1] ~= nil then
lamp[1].destroy() end
end
end
in this example, when the entity is mined, it finds the other entity nearby and destroys it. adapt this to suit your need
Code: Select all
local X = game.player.position.x
local Y = game.player.position.y
local entrance = game.surfaces["nauvis"].find_entities_filtered{area = {{X,Y},{X+1,Y+1}}, name= "Cave-Entrance"}
if(entrance == nil) then return else
game.player.print("Found Cave Entrance!")
entrance = nil
end
Code: Select all
local position_string = entrance[1].position.x .. "," .. entrance[1].position.y
if (not global.caves) then
-- https://forums.factorio.com/wiki/index.php?title=Lua/Data_Lifecycle#global
global.caves = {}
end
if (not global.caves[position_string]) then
game.player.print("Found Cave Entrance!")
global.caves[position_string] = true
end
Code: Select all
script.on_event(defines.events.on_tick, function(event)
local X = game.player.position.x
local Y = game.player.position.y
local entrance = game.surfaces["nauvis"].find_entities_filtered{area = {{X,Y},{X,Y}}, name= "Cave-Entrance"}
if (global.caves == nil) then
global.caves = {}
end
if(table.empty(entrance))then
return
end
local position_string = entrance[1].position.x .. "," .. entrance[1].position.y
local diff = 0
if(global.caves[position_string] ~= nil) then
diff = tonumber(event.tick)-tonumber(global.caves[position_string])
else
global.caves[position_string] = event.tick
return
end
--game.player.print(tostring(diff))
if(diff) then
if (diff>50) then
game.player.print("Found Cave Entrance!")
global.caves[position_string] = event.tick
game.player.teleport(game.player.position, game.surfaces.Caves)
end
end
end)