Jump to content

[Help] Calculations


Recommended Posts

hello guys,
i'm trying to make a teleport, By using a map just like freeroam
but i'm having this weird problem that the coordinates changes whenever i change
the position of the main gui (window)

here is my code:

addEventHandler ("onClientGUIClick", resourceRoot, 
    function (button, _, x, y)
        if (button == "left") then
            if (source == map) then
                local imgX, imgY = guiGetPosition (map, false)
                local width, height = guiGetSize (win, false)
                local x, y = (x - imgX) / width, (y - imgY) / height
                XP = x * 6000 - 3000
                YP = 3000 - y * 6000
                _, _, _, ZP = processLineOfSight (XP, YP, 3000, XP, YP, -3000)
                outputChatBox (XP .. ", " .. YP .. ", " .. (ZP or 0))
            end
        end
    end
)

 

Link to comment

the problem is not there

the problem is in function (button_, x, y)
the coordinations of x, y

when the gui get clicked will be changed if the gui was changed

not gonna be the same result as if it was not moved.

 

the question is, Can i make the coordinations of x, y the same even if it was moved ?

Edited by #Black_Hawk
Link to comment

so i got this new problem

when clicking on the top of the map it get lower

like y - 20 something like that so it's not the same place i selected

and also when clicking on the bottom of the map it get higher like y + 20

so what is that problem ? can someone please help me

Link to comment

i got this problem, that when i select a place it warps me perfectly without any mistakes

but the problem begins when the resolutions of the screen get changed

for example: my resolutions is (1366 / 768)

if someone have a different resolutions from mine when he select a place to warp

it warps him near the place that he selected not the same as he select but different place

 

My code:

local BLIPS = { }
local NAMES = { }
local screenW, screenH = guiGetScreenSize ()

function removeHex (name)
    return type (name) == "string" and string.gsub (name, "#%x%x%x%x%x%x", "") or name
end

addEventHandler ("onClientResourceStart", resourceRoot,
    function ()
        win = guiCreateWindow ((screenW - 674) / 2, (screenH - 716) / 2, 674, 716, "Teleporter", false)
        guiWindowSetSizable (win, false)
        guiSetAlpha (win, 1.00)
        guiSetVisible (win, false)

        map = guiCreateStaticImage (9, 23, 655, 654, ":freeroam/map.png", false, win)

        for _,v in ipairs (getElementsByType ("player")) do
            blip = guiCreateStaticImage (347, 337, 10, 10, ":freeroam/localplayerblip.png", false, map)
            name = guiCreateLabel (347, 337, 123, 20, "", false, map)
            guiSetFont (name, "default-bold-small")
            guiLabelSetColor (name, 24, 230, 39)
            BLIPS [v] = blip
            NAMES [v] = name 
        end

        spawn = guiCreateButton (243, 682, 188, 24, "Teleport Me", false, win)
        guiSetFont (spawn, "clear-normal")
        guiSetProperty (spawn, "NormalTextColour", "FF19E528")   
    end
)

addCommandHandler ("map", 
    function ()
        if getElementData (localPlayer, "allowed") then
            guiSetVisible (win, not guiGetVisible (win))
            showCursor (guiGetVisible (win))
        end
    end
)

local XP, YP
addEventHandler ("onClientGUIClick", resourceRoot, 
    function (button, _, x, y)
        if (button == "left") then
            if (source == map) then
                local winx, winy = guiGetPosition (win, false)
                local imgw, imgh = guiGetSize (map, false)
                local x, y = x - winx - 7, y - winy - 20
                local nx, ny = (x / imgw), (y / imgh)
                XP = (nx * 6000) - 3000
                YP = (3000 - ny * 6000)
            elseif source == spawn and XP and YP then
                setElementPosition (localPlayer, XP, YP, 0)
                setElementFrozen (localPlayer, true)
                setTimer (function (localPlayer)
                    _, _, _, ZP = processLineOfSight (XP, YP, 3000, XP, YP, -3000)
                    setElementPosition (localPlayer, XP, YP, (ZP or 0) + 1)
                    setElementFrozen (localPlayer, false)
                end, 300, 1, localPlayer)
            end
        end
    end
)

addEventHandler ("onClientGUIDoubleClick", resourceRoot, 
    function (button, _, x, y)
        if (button == "left") then
            if (source == map) then
                local winx, winy = guiGetPosition (win, false)
                local imgw, imgh = guiGetSize (map, false)
                local x, y = x - winx - 7, y - winy - 20
                local nx, ny = (x / imgw), (y / imgh)
                XP = (nx * 6000) - 3000
                YP = (3000 - ny * 6000)
                setElementPosition (localPlayer, XP, YP, 0)
                setElementFrozen (localPlayer, true)
                setTimer (function (localPlayer)
                    _, _, _, ZP = processLineOfSight (XP, YP, 3000, XP, YP, -3000)
                    setElementPosition (localPlayer, XP, YP, (ZP or 0) + 1)
                    setElementFrozen (localPlayer, false)
                end, 300, 1, localPlayer)
            end
        end
    end
)

addEventHandler ("onClientRender", root, 
    function ()
        for _,v in ipairs (getElementsByType ("player")) do
            if (BLIPS [v] and NAMES [v]) then
                local x, y = getElementPosition (v)
                x = math.floor ((x + 3000) / 6000 * 655) - 4
                y = math.floor ((3000 - y) / 6000 * 654) - 4
                guiSetPosition (BLIPS [v], x, y, false)
                guiSetText (NAMES [v], removeHex (getPlayerName (v)))
                guiSetPosition (NAMES [v], x + 13, y - 5, false)
            end
        end
    end
)

 

so can someone help me please with that ?

Link to comment
local BLIPS = { }
local NAMES = { }
local screenW, screenH = guiGetScreenSize ()
local multX, multY = 1366/screenW, 768/screenH

function removeHex (name)
    return type (name) == "string" and string.gsub (name, "#%x%x%x%x%x%x", "") or name
end

addEventHandler ("onClientResourceStart", resourceRoot,
    function ()
        win = guiCreateWindow ((screenW - 674) / 2, (screenH - 716) / 2, 674, 716, "Teleporter", false)
        guiWindowSetSizable (win, false)
        guiSetAlpha (win, 1.00)
        guiSetVisible (win, false)

        map = guiCreateStaticImage (9, 23, 655, 654, ":freeroam/map.png", false, win)

        for _,v in ipairs (getElementsByType ("player")) do
            blip = guiCreateStaticImage (347, 337, 10, 10, ":freeroam/localplayerblip.png", false, map)
            name = guiCreateLabel (347, 337, 123, 20, "", false, map)
            guiSetFont (name, "default-bold-small")
            guiLabelSetColor (name, 24, 230, 39)
            BLIPS [v] = blip
            NAMES [v] = name 
        end

        spawn = guiCreateButton (243, 682, 188, 24, "Teleport Me", false, win)
        guiSetFont (spawn, "clear-normal")
        guiSetProperty (spawn, "NormalTextColour", "FF19E528")   
    end
)

addCommandHandler ("map", 
    function ()
        if getElementData (localPlayer, "allowed") then
            guiSetVisible (win, not guiGetVisible (win))
            showCursor (guiGetVisible (win))
        end
    end
)

local XP, YP
addEventHandler ("onClientGUIClick", resourceRoot, 
    function (button, _, x, y)
        if (button == "left") then
            if (source == map) then
                local winx, winy = guiGetPosition (win, false)
                local imgw, imgh = guiGetSize (map, false)
                local x, y = x - winx - 7*multX, y - winy - 20*multY
                local nx, ny = (x / imgw), (y / imgh)
                XP = (nx * 6000) - 3000
                YP = (3000 - ny * 6000)
            elseif source == spawn and XP and YP then
                setElementPosition (localPlayer, XP, YP, 0)
                setElementFrozen (localPlayer, true)
                setTimer (function (localPlayer)
                    _, _, _, ZP = processLineOfSight (XP, YP, 3000, XP, YP, -3000)
                    setElementPosition (localPlayer, XP, YP, (ZP or 0) + 1)
                    setElementFrozen (localPlayer, false)
                end, 300, 1, localPlayer)
            end
        end
    end
)

addEventHandler ("onClientGUIDoubleClick", resourceRoot, 
    function (button, _, x, y)
        if (button == "left") then
            if (source == map) then
                local winx, winy = guiGetPosition (win, false)
                local imgw, imgh = guiGetSize (map, false)
                local x, y = x - winx - 7*multX, y - winy - 20*multY
                local nx, ny = (x / imgw), (y / imgh)
                XP = (nx * 6000) - 3000
                YP = (3000 - ny * 6000)
                setElementPosition (localPlayer, XP, YP, 0)
                setElementFrozen (localPlayer, true)
                setTimer (function (localPlayer)
                    _, _, _, ZP = processLineOfSight (XP, YP, 3000, XP, YP, -3000)
                    setElementPosition (localPlayer, XP, YP, (ZP or 0) + 1)
                    setElementFrozen (localPlayer, false)
                end, 300, 1, localPlayer)
            end
        end
    end
)

addEventHandler ("onClientRender", root, 
    function ()
        for _,v in ipairs (getElementsByType ("player")) do
            if (BLIPS [v] and NAMES [v]) then
                local x, y = getElementPosition (v)
                x = math.floor ((x + 3000) / 6000 * 655) - 4
                y = math.floor ((3000 - y) / 6000 * 654) - 4
                guiSetPosition (BLIPS [v], x, y, false)
                guiSetText (NAMES [v], removeHex (getPlayerName (v)))
                guiSetPosition (NAMES [v], x + 13, y - 5, false)
            end
        end
    end
)

Try this one. I'm not sure if it'll work. :D

Link to comment

Really thank you so much .. but

there is a problem with the blips it's not at the same position but when i spawn it warps me correctly to the place is select

but the blip in the same problem when i warp higher the blip be close to my position

when warp lower the blip move far from the position

Link to comment
addEventHandler ("onClientRender", root, 
    function ()
        for _,v in ipairs (getElementsByType ("player")) do
            if (BLIPS [v] and NAMES [v]) then
                local x, y = getElementPosition (v)
                x = math.floor ((x + 3000) / 6000 * 655) - 4*multX
                y = math.floor ((3000 - y) / 6000 * 654) - 4*multY
                guiSetPosition (BLIPS [v], x, y, false)
                guiSetText (NAMES [v], removeHex (getPlayerName (v)))
                guiSetPosition (NAMES [v], x + 13, y - 5, false)
            end
        end
    end
)

 

Link to comment
local playerBlips = {}
local playerNames = {}
local sX, sY = guiGetScreenSize()
local mainW, mainH = 700, 600
local mainX, mainY = (sX-mainW)/2, (sY-mainH)/2
local multX, multY = 1366/sX, 768/sY

function removeHex (name)
    return type (name) == "string" and string.gsub (name, "#%x%x%x%x%x%x", "") or name
end

addEventHandler("onClientResourceStart", root, function()
	mainWindow = guiCreateWindow(mainX, mainY, mainW, mainH, "Teleport", false)
	guiWindowSetSizable(mainWindow, false)
	mapImg = guiCreateStaticImage(5, 20, mainW, mainH-60, ":freeroam/map.png", false, mainWindow)
	teleportButton = guiCreateButton(5, mainH-35, mainW-10, 30, "Teleport me", false, mainWindow)
	playerBlip = guiCreateStaticImage(0, 0, 10, 10, ":freeroam/localplayerblip.png", false, mapImg)
end)
bindKey("m", "down", function()
	showCursor(not isCursorShowing())
end)

addEventHandler("onClientRender", getRootElement(), function()
	for i, k in ipairs(getElementsByType("player")) do
		local pX, pY = getElementPosition(k)
		local xPerc, yPerc = (pX+3000)/6000, (3000-pY)/6000
		--outputChatBox(xPerc .. " " .. yPerc)
		guiSetPosition(playerBlip, xPerc-10/mainW/2, yPerc-10/mainH/2, true)
	end
end)

addEventHandler("onClientGUIDoubleClick", getRootElement(), function()
	if source == mapImg then
		local tW, tH = guiGetSize(mapImg, false)
		local tX, tY = guiGetPosition(mapImg, false)
		outputChatBox("Map size", 255, 0, 0)
		outputChatBox(tW .. " " .. tH)
		local cX, cY = getCursorPosition()
		local cX, cY = cX*sX, cY*sY
		outputChatBox("Click position", 255, 0, 0)
		outputChatBox(cX .. " " .. cY)
		outputChatBox("Image position", 255, 0, 0)
		outputChatBox(mainX+tX .. " " .. mainY+tY)
		local cX, cY = cX-(mainX+tX), cY-(mainY+tY)
		outputChatBox("Click position on image", 255, 0, 0)
		outputChatBox(cX .. " " .. cY)
		local aX, aY = cX/tW, cY/tH
		outputChatBox("Relative values", 255, 0, 0)
		outputChatBox(aX .. " " .. aY)
		local mapX, mapY = math.floor(aX*6000-3000), math.floor(3000-aY*6000)
		outputChatBox("Map position", 255, 0, 0)
		outputChatBox(mapX .. " " .. mapY)
		setElementFrozen(localPlayer, true)
		setElementPosition(localPlayer, mapX, mapY, 0)
		setTimer(function()
			local _, _, _, foundZ = processLineOfSight(mapX, mapY, 3000, mapX, mapY, -3000)
			setElementPosition(localPlayer, mapX, mapY, foundZ)
			setElementFrozen(localPlayer, false)
		end, 100, 1)
	end
end)

addEventHandler("onClientGUIClick", getRootElement(), function()
	if source == mapImg then
		local tW, tH = guiGetSize(mapImg, false)
		local tX, tY = guiGetPosition(mapImg, false)
		outputChatBox("Map size", 255, 0, 0)
		outputChatBox(tW .. " " .. tH)
		local cX, cY = getCursorPosition()
		local cX, cY = cX*sX, cY*sY
		outputChatBox("Click position", 255, 0, 0)
		outputChatBox(cX .. " " .. cY)
		outputChatBox("Image position", 255, 0, 0)
		outputChatBox(mainX+tX .. " " .. mainY+tY)
		local cX, cY = cX-(mainX+tX), cY-(mainY+tY)
		outputChatBox("Click position on image", 255, 0, 0)
		outputChatBox(cX .. " " .. cY)
		local aX, aY = cX/tW, cY/tH
		outputChatBox("Relative values", 255, 0, 0)
		outputChatBox(aX .. " " .. aY)
		mapX, mapY = math.floor(aX*6000-3000), math.floor(3000-aY*6000)
		outputChatBox("Map position", 255, 0, 0)
		outputChatBox(mapX .. " " .. mapY)
	elseif source == teleportButton and mapX then
		setElementFrozen(localPlayer, true)
		setElementPosition(localPlayer, mapX, mapY, 0)
		setTimer(function()
			local _, _, _, foundZ = processLineOfSight(mapX, mapY, 3000, mapX, mapY, -3000)
			setElementPosition(localPlayer, mapX, mapY, foundZ)
			setElementFrozen(localPlayer, false)
			mapX, mapY = nil, nil, nil
		end, 100, 1)
	end
end)

Left every debugging in, in case you want to see how it works. :D

Link to comment
Just now, #Black_Hawk said:

really thanks

but there is this problem when moving the gui to any other position

and try to warp to a place you can see it's not working correctly so the gui needs to be in one place only

and not be moved to anywhere .. so the last code that probleem wasn't there

 

On ١٨‏/٤‏/٢٠١٧ at 13:05, 3laa33 said:

use guiWindowSetMovable
:3 so he can't move the gui

 

Link to comment
local playerBlips = {}
local playerNames = {}
local sX, sY = guiGetScreenSize()
local mainW, mainH = 700, 600
local mainX, mainY = (sX-mainW)/2, (sY-mainH)/2
local multX, multY = 1366/sX, 768/sY

function removeHex (name)
    return type (name) == "string" and string.gsub (name, "#%x%x%x%x%x%x", "") or name
end

addEventHandler("onClientResourceStart", root, function()
	mainWindow = guiCreateWindow(mainX, mainY, mainW, mainH, "Teleport", false)
	guiWindowSetSizable(mainWindow, false)
	mapImg = guiCreateStaticImage(5, 20, mainW, mainH-60, ":freeroam/map.png", false, mainWindow)
	teleportButton = guiCreateButton(5, mainH-35, mainW-10, 30, "Teleport me", false, mainWindow)
	playerBlip = guiCreateStaticImage(0, 0, 10, 10, ":freeroam/localplayerblip.png", false, mapImg)
end)
bindKey("m", "down", function()
	showCursor(not isCursorShowing())
end)

addEventHandler("onClientRender", getRootElement(), function()
	for i, k in ipairs(getElementsByType("player")) do
		local pX, pY = getElementPosition(k)
		local xPerc, yPerc = (pX+3000)/6000, (3000-pY)/6000
		--outputChatBox(xPerc .. " " .. yPerc)
		guiSetPosition(playerBlip, xPerc-10/mainW/2, yPerc-10/mainH/2, true)
	end
end)

addEventHandler("onClientGUIDoubleClick", getRootElement(), function()
	if source == mapImg then
		local tW, tH = guiGetSize(mapImg, false)
		local tX, tY = guiGetPosition(mapImg, false)
		local mainX, mainY = guiGetPosition(mainWindow, false)
		outputChatBox("Map size", 255, 0, 0)
		outputChatBox(tW .. " " .. tH)
		local cX, cY = getCursorPosition()
		local cX, cY = cX*sX, cY*sY
		outputChatBox("Click position", 255, 0, 0)
		outputChatBox(cX .. " " .. cY)
		outputChatBox("Image position", 255, 0, 0)
		outputChatBox(mainX+tX .. " " .. mainY+tY)
		local cX, cY = cX-(mainX+tX), cY-(mainY+tY)
		outputChatBox("Click position on image", 255, 0, 0)
		outputChatBox(cX .. " " .. cY)
		local aX, aY = cX/tW, cY/tH
		outputChatBox("Relative values", 255, 0, 0)
		outputChatBox(aX .. " " .. aY)
		local mapX, mapY = math.floor(aX*6000-3000), math.floor(3000-aY*6000)
		outputChatBox("Map position", 255, 0, 0)
		outputChatBox(mapX .. " " .. mapY)
		setElementFrozen(localPlayer, true)
		setElementPosition(localPlayer, mapX, mapY, 0)
		setTimer(function()
			local _, _, _, foundZ = processLineOfSight(mapX, mapY, 3000, mapX, mapY, -3000)
			setElementPosition(localPlayer, mapX, mapY, foundZ)
			setElementFrozen(localPlayer, false)
		end, 100, 1)
	end
end)

addEventHandler("onClientGUIClick", getRootElement(), function()
	if source == mapImg then
		local tW, tH = guiGetSize(mapImg, false)
		local tX, tY = guiGetPosition(mapImg, false)
		local mainX, mainY = guiGetPosition(mainWindow, false)
		outputChatBox("Map size", 255, 0, 0)
		outputChatBox(tW .. " " .. tH)
		local cX, cY = getCursorPosition()
		local cX, cY = cX*sX, cY*sY
		outputChatBox("Click position", 255, 0, 0)
		outputChatBox(cX .. " " .. cY)
		outputChatBox("Image position", 255, 0, 0)
		outputChatBox(mainX+tX .. " " .. mainY+tY)
		local cX, cY = cX-(mainX+tX), cY-(mainY+tY)
		outputChatBox("Click position on image", 255, 0, 0)
		outputChatBox(cX .. " " .. cY)
		local aX, aY = cX/tW, cY/tH
		outputChatBox("Relative values", 255, 0, 0)
		outputChatBox(aX .. " " .. aY)
		mapX, mapY = math.floor(aX*6000-3000), math.floor(3000-aY*6000)
		outputChatBox("Map position", 255, 0, 0)
		outputChatBox(mapX .. " " .. mapY)
	elseif source == teleportButton and mapX then
		setElementFrozen(localPlayer, true)
		setElementPosition(localPlayer, mapX, mapY, 0)
		setTimer(function()
			local _, _, _, foundZ = processLineOfSight(mapX, mapY, 3000, mapX, mapY, -3000)
			setElementPosition(localPlayer, mapX, mapY, foundZ)
			setElementFrozen(localPlayer, false)
			mapX, mapY = nil, nil, nil
		end, 100, 1)
	end
end)

Fixed.

6 minutes ago, 3laa33 said:

 

 

Next time try not to use 9999 font size, if he is not using a function, it's because he doesn't want to use it.

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...