SetElementPosition: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
__NOTOC__  
__NOTOC__
This function sets the position of an element to the co-ordinates specified.
{{Server client function}}
This function sets the position of an element to the specified coordinates.


==Syntax==  
==Syntax==  
Line 9: Line 10:
===Required Arguments===  
===Required Arguments===  
*'''theElement:''' A valid [[element]] to be moved.
*'''theElement:''' A valid [[element]] to be moved.
*'''x:''' The x co-ordinate of the destination.
*'''x:''' The x coordinate of the destination.
*'''y:''' The y co-ordinate of the destination.
*'''y:''' The y coordinate of the destination.
*'''z:''' The z co-ordinate of the destination.
*'''z:''' The z coordinate of the destination.


===Returns===
===Returns===
Line 17: Line 18:


==Example==  
==Example==  
This example enables a player to type /warpto <playername> to warp to them, if the player being warped to is in a vehicle with a free passenger seat, it will warp into the vehicle.
This example enables a player to type /warpto <playername> to warp to them. If the player being warped to is in a vehicle with a free passenger seat, it will warp into the vehicle.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function consoleWarpTo ( player, commandName, player2nick )
function consoleWarpTo ( sourcePlayer, commandName, player2nick )
if ( player ) then
-- Make sure required parameters are set
--Setup the variables we will be using for teleportation
if ( not sourcePlayer or not player2nick ) then return end
    local x, y, z, r, d = 0, 0, 0, 0, 2.5
-- Setup the variables we will be using for teleportation
    --Grab the players element identifer we are trying to warp too
local x, y, z, r, d = 0, 0, 0, 0, 2.5
    local player2 = getPlayerFromNick ( player2nick )
-- Grab the element identifier of the player we are trying to warp to
    --Make sure it exists!
local player2 = getPlayerFromNick ( player2nick )
    if ( player2 ) then
-- Make sure it exists!
    --Is the player were warping too in a vehicle?
if ( player2 ) then
        if ( isPlayerInVehicle ( player2 ) ) then
-- Is the player we're warping to in a vehicle?
        --Indeed they are, lets get the vehicle information such as the vehicle element itself, and the seats its got.
if ( isPlayerInVehicle ( player2 ) ) then
        local player2vehicle = getPlayerOccupiedVehicle ( player2 )
-- Indeed they are, let's get the vehicle information such as the vehicle element itself, and the seats it's got.
local maxseats = getVehicleMaxPassengers ( player2vehicle ) + 1
local player2vehicle = getPlayerOccupiedVehicle ( player2 )
local i = 0
local numseats = getVehicleMaxPassengers ( player2vehicle )
--Loop around the max seats seeing if there is a free seat
local i = 0
while ( i < maxseats ) do
-- Loop over the seats to see if there's a free one
if ( getVehicleOccupant ( player2vehicle, i ) ) then
while ( i < numseats ) do
--There isnt a free seat, so lets goto the next seat and check that one
if ( getVehicleOccupant ( player2vehicle, i ) ) then
i = i + 1
-- This seat isn't free, go ahead and check the next one
else
i = i + 1
--
break
end
end
--Check if the value in i, is under the max seats, if it is it means its a free seat
if ( i < maxseats ) then
--Teleport the player into the seat
local status = warpPlayerIntoVehicle ( player, player2vehicle, i )
else
else
--There are no free seats, tell the player that.
-- This seat is free, get out of the loop
outputChatBox ( "Sorry, the player's vehicle is full (" .. getVehicleName ( player2vehicle ) .. " " .. i .. "/" .. maxseats .. ")", player )
break
end
end
end
-- Check if 'i' is lower than the number of seats. If it is, it means it's the number of a free seat
if ( i < numseats ) then
-- Teleport the player into the seat
warpPlayerIntoVehicle ( sourcePlayer, player2vehicle, i )
else
else
--The player isnt in a vehicle, lets get the players position and warp us to them.
-- There are no free seats, tell the player that.
x, y, z = getElementPosition ( player2 )
outputChatBox ( "Sorry, the player's vehicle is full (" .. getVehicleName ( player2vehicle ) .. " " .. i .. "/" .. numseats .. ")", sourcePlayer )
r = getPlayerRotation ( player2 )
--Make sure we get interior data, they might be inside one!
interior = getElementInterior ( player2 )
dimension = getElementDimension ( player2 )
--Do some funky math to make sure that we dont teleport inside of them (get us both stuck inside eachother)
  x = x - ( ( math.cos ( math.rad ( r + 90 ) ) ) * d )
  y = y - ( ( math.sin ( math.rad ( r + 90 ) ) ) * d )
  --Set a few timers for setting interiors, dimensions and positions
setTimer ( setElementInterior, 800, 1, player, interior )
setTimer ( setElementDimension, 900, 1, player, dimension )
  setTimer ( setElementPosition, 1000, 1, player, x, y, z )
  setTimer ( setPlayerRotation, 1000, 1, player, r )
  --Fade the camera to make it look nicer
fadeCamera ( player, false, 1, 0, 0, 0 )
--Fade it back once its all complete!
setTimer ( fadeCamera, 1000, 1, player, true, 1 )
end
end
else
else
--No player by the name was found, tell the warper this.
-- The player isn't in a vehicle, let's get the player's position and warp to them.
outputChatBox ( "No such player.", player )
x, y, z = getElementPosition ( player2 )
r = getPlayerRotation ( player2 )
-- Make sure we get interior data, they might be inside one!
interior = getElementInterior ( player2 )
dimension = getElementDimension ( player2 )
-- Do some funky math to make sure that we dont teleport inside of them (get us both stuck inside each other)
x = x - ( ( math.cos ( math.rad ( r + 90 ) ) ) * d )
y = y - ( ( math.sin ( math.rad ( r + 90 ) ) ) * d )
-- Set a few timers for setting interiors, dimensions and positions
setTimer ( setElementInterior, 800, 1, sourcePlayer, interior )
setTimer ( setElementDimension, 900, 1, sourcePlayer, dimension )
setTimer ( setElementPosition, 1000, 1, sourcePlayer, x, y, z )
setTimer ( setPlayerRotation, 1000, 1, sourcePlayer, r )
-- Fade the camera to make it look nicer
fadeCamera ( sourcePlayer, false, 1, 0, 0, 0 )
-- Fade it back once it's all complete!
setTimer ( fadeCamera, 1000, 1, sourcePlayer, true, 1 )
end
end
else
-- No player by the specified name was found, tell the warper this.
outputChatBox ( "No such player.", sourcePlayer )
end
end
end
end

Revision as of 11:36, 16 August 2007

This function sets the position of an element to the specified coordinates.

Syntax

bool setElementPosition ( element theElement, float x, float y, float z )  

Required Arguments

  • theElement: A valid element to be moved.
  • x: The x coordinate of the destination.
  • y: The y coordinate of the destination.
  • z: The z coordinate of the destination.

Returns

Returns true if the function was successful, false otherwise.

Example

This example enables a player to type /warpto <playername> to warp to them. If the player being warped to is in a vehicle with a free passenger seat, it will warp into the vehicle.

function consoleWarpTo ( sourcePlayer, commandName, player2nick )
	-- Make sure required parameters are set
	if ( not sourcePlayer or not player2nick ) then return end
	-- Setup the variables we will be using for teleportation
	local x, y, z, r, d = 0, 0, 0, 0, 2.5
	-- Grab the element identifier of the player we are trying to warp to
	local player2 = getPlayerFromNick ( player2nick )
	-- Make sure it exists!
	if ( player2 ) then
		-- Is the player we're warping to in a vehicle?
		if ( isPlayerInVehicle ( player2 ) ) then
			-- Indeed they are, let's get the vehicle information such as the vehicle element itself, and the seats it's got.
			local player2vehicle = getPlayerOccupiedVehicle ( player2 )
			local numseats = getVehicleMaxPassengers ( player2vehicle )
			local i = 0
			-- Loop over the seats to see if there's a free one
			while ( i < numseats ) do
				if ( getVehicleOccupant ( player2vehicle, i ) ) then
					-- This seat isn't free, go ahead and check the next one
					i = i + 1
				else
					-- This seat is free, get out of the loop
					break
				end
			end
			-- Check if 'i' is lower than the number of seats. If it is, it means it's the number of a free seat
			if ( i < numseats ) then
				-- Teleport the player into the seat
				warpPlayerIntoVehicle ( sourcePlayer, player2vehicle, i )
			else
				-- There are no free seats, tell the player that.
				outputChatBox ( "Sorry, the player's vehicle is full (" .. getVehicleName ( player2vehicle ) .. " " .. i .. "/" .. numseats .. ")", sourcePlayer )
			end
		else
			-- The player isn't in a vehicle, let's get the player's position and warp to them.
			x, y, z = getElementPosition ( player2 )
			r = getPlayerRotation ( player2 )
			-- Make sure we get interior data, they might be inside one!
			interior = getElementInterior ( player2 )
			dimension = getElementDimension ( player2 )
			-- Do some funky math to make sure that we dont teleport inside of them (get us both stuck inside each other)
			x = x - ( ( math.cos ( math.rad ( r + 90 ) ) ) * d )
			y = y - ( ( math.sin ( math.rad ( r + 90 ) ) ) * d )
			-- Set a few timers for setting interiors, dimensions and positions
			setTimer ( setElementInterior, 800, 1, sourcePlayer, interior )
			setTimer ( setElementDimension, 900, 1, sourcePlayer, dimension )
			setTimer ( setElementPosition, 1000, 1, sourcePlayer, x, y, z )
			setTimer ( setPlayerRotation, 1000, 1, sourcePlayer, r )
			-- Fade the camera to make it look nicer
			fadeCamera ( sourcePlayer, false, 1, 0, 0, 0 )
			-- Fade it back once it's all complete!
			setTimer ( fadeCamera, 1000, 1, sourcePlayer, true, 1 )
		end
	else
		-- No player by the specified name was found, tell the warper this.
		outputChatBox ( "No such player.", sourcePlayer )
	end
end
addCommandHandler ( "warpto", consoleWarpTo )

See Also