Jump to content

Count players function


Recommended Posts

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

  • Contributor

Don't know if I'm too late, but try this:

//////////////////////
// questlua_pc.cpp
//////////////////////
add
	int pc_get_players_count_in_current_map_index(lua_State* L)
	{
		uint32_t mIdx = CQuestManager::instance().GetCurrentCharacterPtr()->GetMapIndex();
		uint32_t pCount = SECTREE_MANAGER::Instance().GetPlayerCountInMap(mIdx);

		lua_pushnumber(L, pCount);
		return 1;
	}

find
luaL_reg pc_functions[] =
{

add
	{ "get_players_count_in_map",		pc_get_players_count_in_current_map_index},




//////////////////////
// sectree_manager.h
//////////////////////
find
size_t		GetMonsterCountInMap(long lMapIndex);

add
size_t		GetPlayerCountInMap(long lMapIndex);



//////////////////////
// sectree_manager.cpp
//////////////////////

struct FCountPlayers
{
	std::unordered_map<VID, VID> m_map_Players;

	void operator() (LPENTITY ent)
	{
		if (ent->IsType(ENTITY_CHARACTER) == true)
		{
			LPCHARACTER lpChar = (LPCHARACTER)ent;

			if (lpChar->IsPC())
			{
				m_map_Players[lpChar->GetVID()] = lpChar->GetVID();
			}
		}
	}
};

size_t SECTREE_MANAGER::GetPlayerCountInMap(long lMapIndex)
{
	LPSECTREE_MAP sectree = SECTREE_MANAGER::instance().GetMap(lMapIndex);

	if (sectree != NULL)
	{
		struct FCountPlayers f;

		sectree->for_each(f);

		return f.m_map_Players.size();
	}

	return 0;
}

////////////
// USAGE:
////////////
/*
	local pCount = get_players_count_in_map() -- it might be pc.get_players_count_in_map, dunno
	
	or
	
	when something with get_players_count_in_map() > whatever begin
*/

 

Before you do, though: Counting them all the time isn't the most optimal way of doing this. In reality, you should keep track of the number of players from the beginning and just increment, decrement when they get in/get out of the map.

However, I gave you some code, what you do with it is your choice.

 

Also, I made this in the past 10 minutes, so I didn't test it(that's your job).

Good luck!

Edited by Amun
made it more clear
Link to comment
Share on other sites

  • Active Member

Hi, look my solution from lua.

 

--questlib.lua
table_players_by_map = {}

function getTableSize(t)
	local count = 0
	for _, __ in pairs(t) do
		count = count + 1
	end
	return count
end

players_by_map = function(map_index)
	if map_index == nil then
		map_index = pc.get_map_index()
	end
	local count = 0
	if table_players_by_map[map_index] == nil or
		getTableSize(table_players_by_map[map_index]) == 0 then
		return count
	end
	return getTableSize(table_players_by_map[map_index])
end

--quest
quest register_player_map begin
	state start begin
		when login begin
			local my_idx = pc.get_map_index()
			if table_players_by_map[my_idx] == nil then
				table_players_by_map[my_idx] = {}
			end
			table_players_by_map[my_idx][pc.get_vid()] = 0
		end
		
		when logout begin
			local my_idx = pc.get_map_index()
			if table_players_by_map[my_idx] != nil then
				table_players_by_map[my_idx][pc.get_vid()] = nil
			end
		end
	end
end

--usage

quest test begin
	when 20095.chat."Test" begin
		say(string.format("On your map there are %s players.", players_by_map()))
		say(string.format("On map index 62 there are %s players.", players_by_map(62)))
	end
	when 20095.chat."Full table (experimental)" with pc.is_gm() begin
		setskin(0)
		chat("table_players_by_map = {")
		for map_index, t_vids in pairs(table_players_by_map) do
			chat(string.format("\t[%s] = {", map_index))
			for vid, value in pairs(t_vids) do
				chat(string.format("\t\t[%s] = %s", vid, value))
			end
			chat("\t},")
		end
		chat("}")
	end
end

 

Check "Full table (experimental)" option in order to understand it.

/rel q is same than set table_player_by_map nil. Be ware.

Sure that your player stay on same core.

Edited by caanmasu
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.