• 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!

GetPlayerGuild

LucasFerraz

Systems Analyst
Joined
Jun 10, 2010
Messages
2,858
Reaction score
96
Location
Brazil
Hello,
I need a scripts that check if all players in area(x,y) are from the same guild then broadcast 1 else broadcast 2

broadcast 1 if same guild
broadcast 2 if not same guild
 
Last edited:
Lua:
local x,v = {}, true
function k()
	for _, pid in ipairs(getPlayersOnline()) do
		table.insert(x,getPlayerGuildId(pid))
	end
	for i = 1, #x-1 do
		if x[i] == x[i+1] then
			v = true
		else
			v = false
	break;
		end
	end
return v
end
Not sure though..
 
I remember I made a function that returned the guildIds of the players in certain area. Here:
Lua:
function getSpectatorsGuildId(centerPos, rangex, rangey, multifloor)
	local guildId = {}
	for _, pid in ipairs(getSpectators(centerPos, rangex, rangey, multifloor)) do
		if(isPlayer(pid)) then
			table.insert(guildId, getPlayerGuildId(pid))
		end
	end

	return guildId
end
or this one (I find the first one better and easier though):
Lua:
function getSpectatorsGuildId(fromPos, toPos)
	local guildId = {}
	for posx = fromPos.x, toPos.x do
		for posy = fromPos.y, toPos.y do
			for posz = fromPos.z, toPos.z do
				local pos = {x = posx, y = posy, z = posz, stackpos = STACKPOS_TOP_CREATURE}
				local pid = getThingfromPos(pos).uid
				if(isPlayer(pid)) then
					table.insert(guildId, getPlayerGuildId(pid))
				end
			end
		end
	end

	return guildId
end
Now to make it check if the guildIds of the players are the same use this function:
Lua:
function table.hassamevalues(table)
	local a = table[1]
	for i = 2, table.maxn(table) do
		if(table[i] ~= a) then
			return false
		end
	end

	return true
end
Just an example on how it works:
Lua:
function onSay(cid, words, param, channel)
	local specGuilds = getSpectatorsGuildId(getThingPosition(cid), 4, 4, false) -- this is an example for the FIRST getSpectatorsGuildId function
	--local specGuilds = getSpectatorsGuildId(getThingPosition(cid), getThingPosition(cid)) -- this is an example for the SECOND getSpectatorsGuildId function
	if(table.hassamevalues(specGuilds)) then
		doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, "The Ids are the same")
	end

	return true
end
 
Last edited:
@Raster (scripts)
Both scripts KILL CPU!
'for _, pid in ipairs(getSpectators(centerPos, rangex, rangey, multifloor)) do' is good (fastest) if you want pick monsters and NPCs from area.
'getThingFromPosition' is slowest possible method and with area 50x50 can laag ots.
@thread
Use Bogart method like [ get players list ]:
Lua:
local fromPos = {x=1,y=2,z=3}
local toPos = {x=4,y=7,z=4}
local peopleInArea = {}
for _, pid in pairs(getPlayersOnline()) do
	if(isInRange(getThingPosition(pid), fromPos, toPos)) then
		table.insert(peopleInArea,pid)
	end
end
local allSame = true
local lastGuild = 0
for _, pid in pairs(peopleInArea) do
	if(lastGuild == 0) then
		lastGuild = getPlayerGuildId(pid)
	end
	if(getPlayerGuildId(pid) == 0 or getPlayerGuildId(pid) ~= lastGuild) then
		allSame = false
		break
	end
	lastGuild = getPlayerGuildId(pid)
end
if(allSame) then
	-- no one is in area or all [can be just one player!] in area are from one guild, number of them: #peopleInArea
else
	-- there are some people without guild or there are two or more guys with different guilds
end
 
And looping through all the players online is like a breeze to the CPU? <_<
Besides, basically both of our scripts would lag. In my case, if there are a lot of creatures (general [monsters, npc, players...]) in the "spectating" area, it will lag. In your case, if there are a lot of players online it will lag.
 
thanks
local fromPos = {x=1,y=2,z=3}
local toPos = {x=4,y=7,z=4}
local peopleInArea = {}
for _, pid in pairs(getPlayersOnline()) do
if(isInRange(getThingPosition(pid), fromPos, toPos)) then
table.insert(peopleInArea,pid)
end
end
local allSame = true
local lastGuild = 0
for _, pid in pairs(peopleInArea) do
if(lastGuild == 0) then
lastGuild = getPlayerGuildId(pid)
end
if(getPlayerGuildId(pid) == 0 or getPlayerGuildId(pid) ~= lastGuild) then
allSame = false
break
end
lastGuild = getPlayerGuildId(pid)
end
if(allSame) then
-- no one is in area or all [can be just one player!] in area are from one guild, number of them: #peopleInArea
else
-- there are some people without guild or there are two or more guys with different guilds
end
 
And looping through all the players online is like a breeze to the CPU? <_<
Besides, basically both of our scripts would lag. In my case, if there are a lot of creatures (general [monsters, npc, players...]) in the "spectating" area, it will lag. In your case, if there are a lot of players online it will lag.
Loop over all players online take ~nothing. Normal PC can loop over around 500.000 table elements per second [in LUA].
There is a list of players online in C++, so engine just parse it to LUA and send to you when you getPlayersOnline() [should take less then 0.001 sec, no matter how muuch you got online].
getSpectators() calls something like: check if tile exists on position x,y,z, if exist get creatures from it, add to list, go to next tile, ..., it loops over all tiles!
getSpectators(pos, 11,11) will check 22 * 22 = 484 tiles, if you try to get bigger room/place server can freez for 0.1-0.2 or even more
About getThingFromPosition(pos, type_creature) - it's muuch slower then getSpectators() [I did many tests]. Use it if you want check few tiles [annihilator quest teleport etc.], not area!
When I made zombie event I had to check all these methods to get fastest to remove zombies after area end.
So if you want to get only some players from area, some players from area with some storage/guild etc. USE getPlayersOnline() and then loop over them and check what you need [position, storage, vocation, guild etc.]
 
Back
Top