FOR_LINKMAP with Lambda

02/22/2017 22:25 Mognakor#1
I got no Test Server right now, so this is not tested. It does compile, though

World.h

Put this somewhere near the top.
Code:
#include <functional>
Put this where FOR_LINKMAP is.

Update:
Code:
inline void for_Linkmap(CWorld* p_World, const D3DXVECTOR3&	vPos, const int nRange, DWORD dwLinkType, int nLayer, std::function<void(CObj* p_Obj)> lambda)
{
	int nLinkX = int(vPos.x / p_World->m_iMPU);
	int nLinkZ = int(vPos.z / p_World->m_iMPU);

#ifdef __WORLDSERVER
	for (int i = 0; i < p_World->m_linkMap.GetMaxLinkLevel(dwLinkType, nLayer); i++)
	{
		int nWidthLink = p_World->m_linkMap.GetLinkWidth(dwLinkType, i, nLayer);

		CObj** p_Objs = p_World->m_linkMap.GetObj(dwLinkType, i, nLayer);
		ASSERT(p_Objs);	
#else
	for( int i = 0; i < MAX_LINKLEVEL; i++ )
	{
		int nWidthLink = CLandscape::m_nWidthLinkMap[i];
#endif

		int nMaxWidth = nWidthLink * p_World->m_nLandWidth;
		int nMaxHeight = nWidthLink * p_World->m_nLandHeight;

		int nUnit = (MAP_SIZE * p_World->m_nLandWidth) / nMaxWidth;

		int nX = (nLinkX / nUnit) * nUnit * p_World->m_iMPU;
		int nZ = (nLinkZ / nUnit) * nUnit * p_World->m_iMPU;

		int d = nUnit * p_World->m_iMPU / 2;

		nX = (int(vPos.x) - nX > d) ? 1 : 0;
		nZ = (int(vPos.z) - nZ > d) ? 1 : 0;

		int nLinkXMin = ( (nLinkX - nRange) / nUnit) + (nX - 1);
		int nLinkZMin = ( (nLinkZ - nRange) / nUnit) + (nZ - 1);

		if( nLinkXMin < 0 ) 
			nLinkXMin = 0;

		if( nLinkZMin < 0 ) 
			nLinkZMin = 0;

		int nLinkXMax = ( (nLinkX + nRange) / nUnit) + nX;
		int nLinkZMax = ( (nLinkZ + nRange) / nUnit) + nZ;	

		if( nLinkXMax >= nMaxWidth ) 
			nLinkXMax = nMaxWidth - 1;

		if( nLinkZMax >= nMaxHeight ) 
			nLinkZMax = nMaxHeight - 1;

		for (int j = nLinkZMin; j <= nLinkZMax; j++)
		{
			for (int k = nLinkXMin; k <= nLinkXMax; k++)
			{
#ifdef __WORLDSERVER
				{
					int nPos = j * nMaxWidth + k;
					CObj* pObj = p_Objs[nPos];
					int limit = 1000; 

					while( pObj && limit-- )
					{ 
#else
				CLandscape* pLand = p_World->m_apLand[(j / nWidthLink) * p_World->m_nLandWidth + (k / nWidthLink)];
				if (pLand)
				{
					int nPos = (j % nWidthLink) * nWidthLink + (k % nWidthLink);
					CObj** p_Objs = pLand->GetObjLink(dwLinkType, i);
					ASSERT(p_Objs);

					CObj* pObj = p_Objs[nPos];

					while (pObj)
						if (IsValidObj(pObj))
						{
#endif
							lambda(pObj);

							pObj = pObj->GetNextNode();
					}
				}

			}
		}

	}
}
Original(causes link error)

Now you can do stuff like
Code:
forLinkmap( GetWorld(), vPos, nRange, CObj::linkDynamic, GetLayer(), [] (CObj* p_Obj) {
// Do Stuff
} );
02/23/2017 14:56 elitemember21#2
What does this do?
02/23/2017 15:29 Sedrika#3
Quote:
Originally Posted by elitemember21 View Post
What does this do?
https://en.wikipedia.org/wiki/Anonymous_function
02/23/2017 22:36 Mognakor#4
Quote:
Originally Posted by elitemember21 View Post
What does this do?
You can do the same as with regular FOR_LINKMAP but it's much easier to debug.
02/24/2017 18:44 Wanetrain#5
This!

Best release ever i seen in this section! you made my day!

But over 90% don't know what lambda is..