Page 1 of 1

How to check collisions for "teleport" command?

Posted: Mon Jan 09, 2017 9:26 pm
by darkfrei
Do you know some easy method to check collisions of entities by using teleport? teleport(position, surface)

Re: How to check collisions for "teleport" command?

Posted: Mon Jan 09, 2017 10:23 pm
by mophydeen
This is what I did for my /slap command

Code: Select all

-- till parameter is to make sure there is no infinite loop. eg When the player has no other place to reach.
function slapTill(player, till)
	if player and player.valid and player.connected then
		local randomXdirection = randomPosOrNeg()
		local randomX =  randomXdirection * math.random(10)
		local randomYdirection = randomPosOrNeg()
		local randomY = randomYdirection * math.random(10)

		local newPosition = {x = player.position.x + randomX, y = player.position.y + randomY}
		-- if a wall can be placed at the position, a player won't be stack there. Unless it's an island :)
		if player.surface.can_place_entity({name="stone-wall", position=newPosition, direction=nil, force=player.force}) then
			local success = player.teleport(newPosition)
			if success then 
				slapReceived(player)
				return true
			else
				return false
			end
		else
			if till > 1 then
				return slap(player, till-1)
			else
				return false
			end

		end
    end
    return false
end
or see:
viewtopic.php?f=36&t=39577#p235465
in sl_utils.lua

Re: How to check collisions for "teleport" command?

Posted: Tue Jan 10, 2017 5:49 pm
by orzelek
Silly question:
Why are you checking for stone-wall and teleporting player there?
You should cehck if actual player entity will fit - it has different bounding parameters then wall :D
(Spoilers: player is an actual entity named player)

Re: How to check collisions for "teleport" command?

Posted: Tue Jan 10, 2017 6:11 pm
by mophydeen
orzelek wrote:Silly question:
Why are you checking for stone-wall and teleporting player there?
You should cehck if actual player entity will fit - it has different bounding parameters then wall :D
(Spoilers: player is an actual entity named player)
I'll look into it

Re: How to check collisions for "teleport" command?

Posted: Tue Jan 10, 2017 8:37 pm
by Nexela
darkfrei wrote:Do you know some easy method to check collisions of entities by using teleport? teleport(position, surface)
There is also a way to find the closest position to something with find_non_colliding_position.

local non_colliding = player.surface.find_non_colliding_position(name="player", center=target_position, radius=10, precision=1) -- check in a 10x10 tile box around target_position, checking every 1 tile until there is a spot
if non_colliding then player.teleport(non_colliding) end