• 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] searchThingText(fromPos, toPos) [generate text like exiva]

Gesior.pl

Mega Noob&LOL 2012
Senator
Joined
Sep 18, 2007
Messages
2,965
Solutions
99
Reaction score
3,375
Location
Poland
GitHub
gesior
Code from engine changed from C++ to LUA.
Good to make quests/events where players must find on map some items/npc/boss/spawn etc. ^_^
You can make talkaction which will show where is something (not only player) :D
Lua:
function searchThingText(fromPos, toPos)
	local DISTANCE_BESIDE = 0
	local DISTANCE_CLOSE = 1
	local DISTANCE_FAR = 2
	local DISTANCE_VERYFAR = 3

	local DIR_N = 0
	local DIR_S = 1
	local DIR_E = 2
	local DIR_W = 3
	local DIR_NE = 4
	local DIR_NW = 5
	local DIR_SE = 6
	local DIR_SW = 7

	local LEVEL_HIGHER = 0
	local LEVEL_LOWER = 1
	local LEVEL_SAME = 2

	local distance = 0
	local direction = 0
	local level = 0

	local dx = fromPos.x - toPos.x
	local dy = fromPos.y - toPos.y
	local dz = fromPos.z - toPos.z
	if(dz > 0) then
		level = LEVEL_HIGHER
	elseif(dz < 0) then
		level = LEVEL_LOWER
	else
		level = LEVEL_SAME
	end

	if(math.abs(dx) < 5 and math.abs(dy) < 5) then
		distance = DISTANCE_BESIDE
	else
		local tmp = dx * dx + dy * dy
		if(tmp < 10000) then
			distance = DISTANCE_CLOSE
		elseif(tmp < 75625) then
			distance = DISTANCE_FAR
		else
			distance = DISTANCE_VERYFAR
		end
	end

	local tang = 0
	if(dx ~= 0) then
		tang = dy / dx
	else
		tang = 10
	end

	if(math.abs(tang) < 0.4142) then
		if(dx > 0) then
			direction = DIR_W
		else
			direction = DIR_E
		end
	elseif(math.abs(tang) < 2.4142) then
		if(tang > 0) then
			if(dy > 0) then
				direction = DIR_NW
			else
				direction = DIR_SE
			end
		else
			if(dx > 0) then
				direction = DIR_SW
			else
				direction = DIR_NE
			end
		end
	else
		if(dy > 0) then
			direction = DIR_N
		else
			direction = DIR_S
		end
	end

	local ss = ''
	if(distance == DISTANCE_BESIDE) then
		if(level == LEVEL_SAME) then
			ss = ss .. "is stangding next to you"
		elseif(level == LEVEL_HIGHER) then
			ss = ss .. "is above you"
		elseif(level == LEVEL_LOWER) then
			ss = ss .. "is below you"
		end
	elseif(distance == DISTANCE_CLOSE) then
		if(level == LEVEL_SAME) then
			ss = ss .. "is to the"
		elseif(level == LEVEL_HIGHER) then
			ss = ss .. "is on a higher level to the"
		elseif(level == LEVEL_LOWER) then
			ss = ss .. "is on a lower level to the"
		end
	elseif(distance == DISTANCE_FAR) then
		ss = ss .. "is far to the";
	elseif(distance == DISTANCE_VERYFAR) then
		ss = ss .. "is very far to the"
	end

	if(distance ~= DISTANCE_BESIDE) then
		if(direction == DIR_N) then
			ss = ss .. "north"
		elseif(direction == DIR_S) then
			ss = ss .. "south"
		elseif(direction == DIR_E) then
			ss = ss .. "east"
		elseif(direction == DIR_W) then
			ss = ss .. "west"
		elseif(direction == DIR_NE) then
			ss = ss .. "north-east"
		elseif(direction == DIR_NW) then
			ss = ss .. "north-west"
		elseif(direction == DIR_SE) then
			ss = ss .. "south-east"
		elseif(direction == DIR_SW) then
			ss = ss .. "south-west"
		end
	end

	return ss
end
 
Last edited by a moderator:
Not bad. :)

Use inline conditions. It will short the function.
I'm not sure what you meant with 'short' as you can put all LUA code in 1 line and it will work.
Functions/scripts are 'long' to make them readable for other programmers.
Code without constants:
Lua:
function searchThingText(fromPos, toPos) 
	local distance = 0
	local direction = 0
	local level = 0
 
	local dx = fromPos.x - toPos.x
	local dy = fromPos.y - toPos.y
	local dz = fromPos.z - toPos.z
	if(dz > 0) then
		level = 0
	else if(dz < 0) then
		level = 1
	else
		level = 2
	end
 
	if(math.abs(dx) < 5 and math.abs(dy) < 5) then
		distance = 0
	else
		local tmp = dx * dx + dy * dy
		if(tmp < 10000) then
			distance = 1
		else if(tmp < 75625) then
			distance = 2
		else
			distance = 3
		end
	end
 
	local tang = 0
	if(dx ~= 0) then
		tang = dy / dx
	else
		tang = 10
	end
 
	if(math.abs(tang) < 0.4142) then
		if(dx > 0) then
			direction = 3
		else
			direction = 2
		end
	else if(math.abs(tang) < 2.4142) then
		if(tang > 0) then
			if(dy > 0) then
				direction = 5
			else
				direction = 6
			end
		else
			if(dx > 0) then
				direction = 7
			else
				direction = 4
			end
		end
	else
		if(dy > 0) then
			direction = 0
		else
			direction = 1
		end
	end
 
	local ss = ''
	if(distance == 0) then
		if(level == 2) then
			ss = ss .. "is stangding next to you"
		else if(level == 0) then
			ss = ss .. "is above you"
		else if(level == 1) then
			ss = ss .. "is below you"
		end
	else if(distance == 1) then
		if(level == 2) then
			ss = ss .. "is to the"
		else if(level == 0) then
			ss = ss .. "is on a higher level to the"
		else if(level == 1) then
			ss = ss .. "is on a lower level to the"
		end
	else if(distance == 2) then
		ss = ss .. "is far to the";
	else if(distance == 3) then
		ss = ss .. "is very far to the"
	end
 
	if(distance ~= 0) then
		if(direction == 0) then
			ss = ss .. "north"
		else if(direction == 1) then
			ss = ss .. "south"
		else if(direction == 2) then
			ss = ss .. "east"
		else if(direction == 3) then
			ss = ss .. "west"
		else if(direction == 4) then
			ss = ss .. "north-east"
		else if(direction == 5) then
			ss = ss .. "north-west"
		else if(direction == 6) then
			ss = ss .. "south-east"
		else if(direction == 7) then
			ss = ss .. "south-west"
		end
	end
 
	return ss
end
 
I think gesior that VirrageS has in mid something like that
Code:
function searchThingText(fromPos, toPos) 
	local distance = 0
	local direction = 0
	local level = 0
 
	local dx = fromPos.x - toPos.x
	local dy = fromPos.y - toPos.y
	local dz = fromPos.z - toPos.z
	
	dv > 0 and (level = 0) or (dz < 0 and level = 1 or level = 2)
 
	if(math.abs(dx) < 5 and math.abs(dy) < 5) then
		distance = 0
	else
		local tmp = dx * dx + dy * dy
		if(tmp < 10000) then
			distance = 1
		else if(tmp < 75625) then
			distance = 2
		else
			distance = 3
		end
	end
 
	local tang = 0
	(dx ~= 0) and tang = dy/dx or tang = 10

	if(math.abs(tang) < 0.4142) then
		dx > 0 and direction = 3 or direction = 2
		
	else if(math.abs(tang) < 2.4142) then
		tang > 0 and ( dy > 0 and direction =5 or direction =7) or (dx >0 and direction = 7 or direction = 4

	else
		(dy > 0) and (direction = 1) or (direction = 0)

	end
 
	local ss = ''
	local texts = {
		--distance
		[0] = { 
		   --level[0] = "is above you",
				  [1] = "is below you",
				  [2] = "is stangding next to you",
			},
		 [1] = {
			 [0] = "is on a higher level to the",
			 [1] = "is on a lower level to the",
			 [2] = "is to the",
		 }
		 [2] = "is far to the",
		 [3] = "is very far to the"
	}
	local dirs = {
		[0] = "north"
		[1] = "south"
		[2] = "east"
		[3] = "west"
		[4] = "north-east"
		[5] = "north-west"
		[6] = "south-east"
		[7] = "south-west"
	}
	type(text[distance]) == "table" and ss = ss .. text[distance][level] or ss = ss .. text[distance]
	(distance ~= 0) and ss = ss.. dirs[direction]
		
 
	return ss
end
It's nice to use it instead of if statements beacosue in some cases it makes code more readable. Matter of taste. Cheers:)
 
Lua:
function searchThingText(fromPos, toPos) 
	local distance = 0
	local direction = 0
	local level = 0
 
	local dx = fromPos.x - toPos.x
	local dy = fromPos.y - toPos.y
	local dz = fromPos.z - toPos.z
	
	level = (dz > 0) and 0 or (dz < 0) and 1 or 2
 
	if math.abs(dx) < 5 and math.abs(dy) < 5 then
		distance = 0
	else
		local tmp = dx * dx + dy * dy
		distance = (tmp < 10000) and 1 or (tmp < 75625) and 2 or 3
	end
 
	local tang = (dx ~= 0) and dy / dx or 10
	if math.abs(tang) < 0.4142 then
		direction = (dx > 0) and 3 or 2		
	else if math.abs(tang) < 2.4142 then
		direction = (tang > 0) and ((dy > 0) and 5 or 6) or ((dx > 0) and 7 or 4)
	else
		direction = (dy > 0) and 0 or 1
	end

	local texts = {
		--distance
		[0] = { 
			--level
			[0] = "is above you",
			[1] = "is below you",
			[2] = "is stangding next to you"
		},
		[1] = {
			[0] = "is on a higher level to the",
			[1] = "is on a lower level to the",
			[2] = "is to the"
		},
		[2] = "is far to the",
		[3] = "is very far to the"
	}
	local dirs = {
		[0] = "north",
		[1] = "south",
		[2] = "east",
		[3] = "west",
		[4] = "north-east",
		[5] = "north-west",
		[6] = "south-east",
		[7] = "south-west"
	}

	return ((type(text[distance]) == "table") and text[distance][level] or text[distance])..((distance ~= 0) and dirs[direction] or '')
end

It's nice to use it instead of if statements beacosue in some cases it makes code more readable. Matter of taste. Cheers:)

I agree with you that if/else statements make code more readable but in my opinion is nice to have code shortened (without any type of difficult tricks which make code really unreadable)
 
u can show
examples?
Bit late to the party, but here's an example, showing 'exiva' to get back to players home town.
Lua:
local from_pos = getThingPosition(cid)
local to_pos = getTownTemplePosition(getPlayerTown(cid))
local text = searchThingText(from_pos, to_pos)

doCreatureSay(cid, text, TALKTYPE_ORANGE_1)
 
Back
Top