• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Lua Function] determination of the nearest town (to which the town closest to us)

Seminari

Banned User
Joined
Dec 13, 2009
Messages
1,496
Reaction score
34
Location
Poland
Lua:
function getNajblizej(pos)
local miasta = {
["carlin"] = {x=200,y=380,z=7},
["venore"] = {x=1200,y=2380,z=7},
["thais"] = {x=200,y=17000,z=7},
["edron"] = {x=250, y=600, z=7},
}
local city = ''
local distance = 0
for k,v in pairs(miasta) do
local dist = ((pos.x-v.x)^2+(pos.y-v.y)^2)^(1/2)
 if (dist < 0) then
  dist = 0-dist
 end
 if city == '' then
  city = k
  distance = dist
 elseif (distance > dist)then
  city = k
  distance = dist
 end 
end
return {city, distance} 
end


example:
Lua:
local poz = {x=340, y=800, z=7}
lol = getNajblizej(poz)
print(lol[1], lol[2])



in talkaction:

Lua:
function onSay(cid, words, param, channel)
        if(param == '') then
            return doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command requires param.")
        end
 
        local t = string.explode(param, ",")
		if((not t[1]) or (not t[2]) or (not t[3])) then
            return doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command requires param.")
		end
		poz = getPlayerPosition(cid)
		lol = getNajblizej(poz)
		doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "The nearest town is: ".. lol[1] ..", distance is: ".. lol[2] .."")
		
	return true
end






thx for help Azi :)
 
Last edited:
# changed distance formula from:

local dist = (pos.x-v.x)+(pos.y-v.y)

to:

local dist = ((pos.x-v.x)^2+(pos.y-v.y)^2)^(1/2)
 
sorry where do i put this lua file in talkactions? a bit cunfused

function getNajblizej(pos)
local miasta = {
["carlin"] = {x=200,y=380,z=7},
["venore"] = {x=1200,y=2380,z=7},
["thais"] = {x=200,y=17000,z=7},
["edron"] = {x=250, y=600, z=7},
}
local city = ''
local distance = 0
for k,v in pairs(miasta) do
local dist = ((pos.x-v.x)^2+(pos.y-v.y)^2)^(1/2)
if (dist < 0) then
dist = 0-dist
end
if city == '' then
city = k
distance = dist
elseif (distance > dist)then
city = k
distance = dist
end
end
return {city, distance}
end
 
if (dist < 0) then
dist = 0-dist
end

that's not necessary
you are using a distant between 2-point formula, which will always return a positive value (as distances can only be positive)
 
if (dist < 0) then
dist = 0-dist
end

that's not necessary
you are using a distant between 2-point formula, which will always return a positive value (as distances can only be positive)

o_O smartas, ok, remove this line, and test ;s

do you read my script or only 1 line hm? look again.
 
Back
Top