Creating array with vector

11/30/2016 17:37 raventh1984#1
hi elitepvpers,

I am having an question about adding stuff to an vector.

What i have done is this

Code:
BOOL CRiftMng::LoadScript()
{
	if (m_Lua.RunScript("Rift.lua") != 0)
	{
		Error("Rift.lua Load Failed!!!");
		exit(0);
	}

	m_Lua.GetGloabal("tWorld");
	m_Lua.PushNil();

	while (m_Lua.TableLoop(-2))
	{
		__RANDOM_WORLD srRndWorld;
		srRndWorld.dwWorldID = static_cast<int>(CScript::GetDefineNum(m_Lua.GetFieldToString(-1, "strWorldID")));
		srRndWorld.vPosX = static_cast<int>(m_Lua.GetFieldToNumber(-1, "vPosX"));
		srRndWorld.vPosY = static_cast<int>(m_Lua.GetFieldToNumber(-1, "vPosY"));
		srRndWorld.vPosZ = static_cast<int>(m_Lua.GetFieldToNumber(-1, "vPosZ"));

		m_vecWorld.push_back(srRndWorld);
		m_Lua.Pop(1);
	}
	m_Lua.Pop(0);

	return TRUE;
}
Inside my Lua
[code]
dofile( ".\\LuaFunc\\RiftFunc.lua" )

-----------------------------------------------------
------------Define WORLD Information----------------
-----------------------------------------------------
AddWorld("WI_INSTANCE_CONTAMINTRAILS", 1413.187f, 100.487f, 1261.361f)
AddWorld("WI_INSTANCE_KALGAS", 626.433f, 300.086F, 1119.150F)
[code]

LuaFunc
Code:
tWorld[nIndex] = {}
function AddMonster(strWorldID, vPosX, vPosY, vPosZ)
	
	nIndex = table.getn( tWorld ) + 1
	tWorld[nIndex].strWorldID = strWorldID
	tWorld[nIndex].vPosX = vPosX
	tWorld[nIndex].VposY = VposY
	tWorld[nIndex].VposZ = VposZ
end
Now the next part is this
Code:
void CRiftMatch::SetRandomWorld()
{
	vector<__RANDOM_WORLD> m_vecRndInfo = CRiftMng::GetInstance()->m_vecWorld;
	for (int i = 0; i < (int)m_vecRndInfo.size(); i++)
	{
		nWorldId = m_vecRndInfo.at(i).dwWorldID;
	}
}
For testing i added Error("%s", nWorldId); to see if its returning an id.

However its giving an NULL as return.

Now is the code i produce correct? espacialy the part to set an Variable nWorldId?

@[Only registered and activated users can see links. Click Here To Register...] you have some experience according to your post can you take an look at it?

With kind regards
11/30/2016 18:59 alfredico#2
Is it loading the lua?

[Only registered and activated users can see links. Click Here To Register...]

Also this part is wrong, should be.

Code:
void CRiftMatch::SetRandomWorld()
{
	vector<__RANDOM_WORLD> m_vecRndInfo = CRiftMng::GetInstance()->m_vecWorld;
	int id = rand() % m_vecRndInfo.size();
	nWorldId = m_vecRndInfo[id];
}
11/30/2016 19:32 Nortix#3
You should initialize the array before you initialize an element of it
11/30/2016 19:46 raventh1984#4
Quote:
Originally Posted by alfredico View Post
Is it loading the lua?

[Only registered and activated users can see links. Click Here To Register...]

Also this part is wrong, should be.

Code:
void CRiftMatch::SetRandomWorld()
{
	vector<__RANDOM_WORLD> m_vecRndInfo = CRiftMng::GetInstance()->m_vecWorld;
	int id = rand() % m_vecRndInfo.size();
	nWorldId = m_vecRndInfo[id];
}
Oops missed that one big time. haha.
Thanks i will test it.

[Test]
Ok implanted the way you have done it @[Only registered and activated users can see links. Click Here To Register...].

its saying that m_vecRndInfo has no suitable conversion from __RANDOM_WORLD to int Exist.
Its about this line

nWorldId = m_vecRndInfo[id];

so if i do this nWorldId = m_vecRndInfo.at(id).dwWorldID;

Then my worldserver is crashing by giving the error (Division by zero)
@ dpsrv.cpp at Last semicolomn (})
From the code

Code:
void CDPSrvr::OnPlayerRift(CAr & ar, DPID dpidCache, DPID dpidUser, LPBYTE, u_long)
{
	try
	{
		CUser* pUser = g_UserMng.GetUser(dpidCache, dpidUser);
		if (!IsValidObj(pUser))
			return;

		DWORD dwPlayerID;
		ar >> dwPlayerID;

		CRiftMng::GetInstance()->CreateNewMatch(pUser, dwPlayerID);
	}
	catch (...)
	{
		Error("Error on Line %d in %s", __LINE__, __FILE__);
	}
} <--Error Line
i have not much experience in using vectors.
11/30/2016 20:28 alfredico#5
Division by zero error must be inside CreateNewMatch function.
12/01/2016 02:41 raventh1984#6
Code:
BOOL CRiftMng::CreateNewMatch(CUser* pUser, DWORD dwPlayerID)
{
	if(IsValidObj(pUser))
	{
		CRiftMatch* pMatch = new CRiftMatch;
		pMatch->dwPlayerID = pUser->m_idPlayer;
		pMatch->SetRandomWorld();

//		pMatch->CreateRoomLayer(pUser->m_idPlayer);
//		pMatch->JoinRift(pUser, dwPlayerID);

		return TRUE;
	}
	return FALSE;
}
There is nothing there that can create an division by zero.

The problem lies with the vector and struct. Cause when using the switch statement its not crashing at all.
12/01/2016 07:52 alfredico#7
If that's the case, the loading is not working and there is no data in the vector.


Code:
void CRiftMatch::SetRandomWorld()
{
	vector<__RANDOM_WORLD> m_vecRndInfo = CRiftMng::GetInstance()->m_vecWorld;
	int id = rand() % m_vecRndInfo.size(); //m_vecRndInfo.size() returns 0
	nWorldId = m_vecRndInfo[id].dwWorldID;
}
12/01/2016 09:35 ディオニュソス#8
Your initialization of tWorld is incorrect, afaik it should be

Code:
tWorld = {}
function AddMonster(strWorldID, vPosX, vPosY, vPosZ)
	
	local nIndex = table.getn( tWorld ) + 1
	tWorld[nIndex].strWorldID = strWorldID
	tWorld[nIndex].vPosX = vPosX
	tWorld[nIndex].VposY = VposY
	tWorld[nIndex].VposZ = VposZ
end
Though I don't know whether that's the root cause of your problem
12/01/2016 14:43 raventh1984#9
Hoi. Its almost solved.

It wasnt loading the Lua file.
So i changed some part of the code so that it was loading.
Then i recieved error codes from the lua yeah :D.
Solved them.

Edited the code again.
Now i have this

Code:
void CRiftMatch::SetRandomWorld()
{
	vector<__RANDOM_WORLD> m_vecRndInfo = CRiftMng::GetInstance()->m_vecWorld;
	for (int i = 0; i < (int)m_vecRndInfo.size(); i++)
	{
		nWorldId = rand() % m_vecRndInfo.at(i).dwWorldID;
		nWorldId = m_vecRndInfo.at(i).dwWorldID;
	}

}
And
Code:
void CRiftMatch::GetRandomWorld()
{
	if (nWorldId != NULL)
		OUTPUTDEBUGSTRING("\nGetWorld: WorldID = %d", nWorldId);
}
So in debug modes its returning the first WorldId what i have in Lua.
So that is now working.

However when i do nWorldId = rand() % m_vecRndInfo.at(i).dwWorldID;
Then its giving me random numbers even numbers that are not stored inside the .lua file.

Debug Window
Code:
GetWorld: WorldID = 41
GetWorld: WorldID = 172
GetWorld: WorldID = 82
GetWorld: WorldID = 53
Those are not WorldId Numbers
12/01/2016 15:05 alfredico#10
The code I posted above should work.

In your code:
for (int i = 0; i < (int)m_vecRndInfo.size(); i++) There is no need of that, you don't need to loop.
nWorldId = rand() % m_vecRndInfo.at(i).dwWorldID; For example, Kalgas cave (world id 130) would be rand() % 130; Still no sense.


Code:
void CRiftMatch::SetRandomWorld()
{
	vector<__RANDOM_WORLD> m_vecRndInfo = CRiftMng::GetInstance()->m_vecWorld;
	int id = rand() % m_vecRndInfo.size();
	nWorldId = m_vecRndInfo[id].dwWorldID;
}
int id = rand() % m_vecRndInfo.size(); Returns a random number between 0 and the size of vector
nWorldId = m_vecRndInfo[id].dwWorldID; From the previous obtained operation (vector position) use the giving number to get the value from structure.

This only fails if there is no data stored in the vector, consider using assert() if needed.

[Only registered and activated users can see links. Click Here To Register...]
12/01/2016 15:40 raventh1984#11
Quote:
Originally Posted by alfredico View Post
The code I posted above should work.

In your code:
for (int i = 0; i < (int)m_vecRndInfo.size(); i++) There is no need of that, you don't need to loop.
nWorldId = rand() % m_vecRndInfo.at(i).dwWorldID; For example, Kalgas cave (world id 130) would be rand() % 130; Still no sense.


Code:
void CRiftMatch::SetRandomWorld()
{
	vector<__RANDOM_WORLD> m_vecRndInfo = CRiftMng::GetInstance()->m_vecWorld;
	int id = rand() % m_vecRndInfo.size();
	nWorldId = m_vecRndInfo[id].dwWorldID;
}
int id = rand() % m_vecRndInfo.size(); Returns a random number between 0 and the size of vector
nWorldId = m_vecRndInfo[id].dwWorldID; From the previous obtained operation (vector position) use the giving number to get the value from structure.

This only fails if there is no data stored in the vector, consider using assert() if needed.

[Only registered and activated users can see links. Click Here To Register...]

Your code is working the only thing i am facing is that i will always just show 1 WorldID.

This is my Lua
Code:
AddWorld("WI_INSTANCE_CONTAMINTRAILS", 1413, 1261, 187, 361, 100 )
AddWorld("WI_INSTANCE_KALGAS", 626, 1119, 433, 150, 300)
As you can see i have 2 instances.
But when i run your code it always returns the first one.
So ID 237== WI_INSTANCE_CONTAMINTRAILS

If i switch them so that kalgas is first then its only loading the first world id.

So i think i am loading it wrong. And that is way it isnt working.

My load function
Code:
BOOL CRiftMng::LoadWorld()
{
	if (m_Lua.RunScript("Rift.lua") != 0)
	{
		Error("Rift.lua Load Failed!!!");
		exit(0);
	}

	m_Lua.GetGloabal("tWorld");
	m_Lua.PushNil();

	while (m_Lua.TableLoop(-2))
	{
		__RANDOM_WORLD srRndWorld;
		srRndWorld.dwWorldID = CScript::GetDefineNum(m_Lua.GetFieldToString(-1, "strWorldID"));
		srRndWorld.x1 = static_cast<int>(m_Lua.GetFieldToNumber(-1, "x1"));
		srRndWorld.z1 = static_cast<int>(m_Lua.GetFieldToNumber(-1, "z1"));
		srRndWorld.x2 = static_cast<int>(m_Lua.GetFieldToNumber(-1, "x2"));
		srRndWorld.z2 = static_cast<int>(m_Lua.GetFieldToNumber(-1, "z2"));
		srRndWorld.y = static_cast<int>(m_Lua.GetFieldToNumber(-1, "y"));

		m_vecWorld.push_back(srRndWorld);
		m_Lua.Pop(1);

		return TRUE;
	}
	m_Lua.Pop(0);

	return FALSE;
}
I have taken the code from Colloseum.cpp and altered it.
Guess its an fault in there.

EDIT!!!
SOLVED.

Pfff. i have taken an closer look on how its loading the Worlds
And sure enought it was the return FALSE; this leads to execute one time. same as break;
Changed it to true problem solved.

Thank you verry much for your help and your link to the vector i have learned an lot from it.

Code that is now changed
Code:
BOOL CRiftMng::LoadWorld()
{
	if (m_Lua.RunScript("Rift.lua") != 0)
	{
		Error("Rift.lua Load Failed!!!");
		exit(0);
	}

	m_Lua.GetGloabal("tWorld");
	m_Lua.PushNil();

	while (m_Lua.TableLoop(-2))
	{
		__RANDOM_WORLD srRndWorld;
		srRndWorld.dwWorldID = CScript::GetDefineNum(m_Lua.GetFieldToString(-1, "strWorldID"));
		srRndWorld.x1 = static_cast<int>(m_Lua.GetFieldToNumber(-1, "x1"));
		srRndWorld.z1 = static_cast<int>(m_Lua.GetFieldToNumber(-1, "z1"));
		srRndWorld.x2 = static_cast<int>(m_Lua.GetFieldToNumber(-1, "x2"));
		srRndWorld.z2 = static_cast<int>(m_Lua.GetFieldToNumber(-1, "z2"));
		srRndWorld.y = static_cast<int>(m_Lua.GetFieldToNumber(-1, "y"));

		m_vecWorld.push_back(srRndWorld);
		m_Lua.Pop(1);

	}
	m_Lua.Pop(0);

	return TRUE;
}