How to check collisions for "teleport" command?

Place to get help with not working mods / modding interface.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

How to check collisions for "teleport" command?

Post by darkfrei »

Do you know some easy method to check collisions of entities by using teleport? teleport(position, surface)
mophydeen
Filter Inserter
Filter Inserter
Posts: 529
Joined: Sun Nov 22, 2015 5:02 pm
Contact:

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

Post 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
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

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

Post 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)
mophydeen
Filter Inserter
Filter Inserter
Posts: 529
Joined: Sun Nov 22, 2015 5:02 pm
Contact:

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

Post 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
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

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

Return to “Modding help”