Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Flyff > Flyff Private Server
You last visited: Today at 23:23

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Creating array with vector

Discussion on Creating array with vector within the Flyff Private Server forum part of the Flyff category.

Closed Thread
 
Old   #1
 
elite*gold: 0
Join Date: Oct 2012
Posts: 948
Received Thanks: 157
Creating array with vector

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?

@ you have some experience according to your post can you take an look at it?

With kind regards
raventh1984 is offline  
Old 11/30/2016, 18:59   #2
 
elite*gold: 0
Join Date: Mar 2008
Posts: 665
Received Thanks: 230
Is it loading the lua?



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];
}
alfredico is offline  
Thanks
1 User
Old 11/30/2016, 19:32   #3
 
elite*gold: 0
Join Date: Mar 2008
Posts: 333
Received Thanks: 284
You should initialize the array before you initialize an element of it
Nortix is offline  
Old 11/30/2016, 19:46   #4
 
elite*gold: 0
Join Date: Oct 2012
Posts: 948
Received Thanks: 157
Quote:
Originally Posted by alfredico View Post
Is it loading the lua?



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 @.

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.
raventh1984 is offline  
Old 11/30/2016, 20:28   #5
 
elite*gold: 0
Join Date: Mar 2008
Posts: 665
Received Thanks: 230
Division by zero error must be inside CreateNewMatch function.
alfredico is offline  
Old 12/01/2016, 02:41   #6
 
elite*gold: 0
Join Date: Oct 2012
Posts: 948
Received Thanks: 157
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.
raventh1984 is offline  
Old 12/01/2016, 07:52   #7
 
elite*gold: 0
Join Date: Mar 2008
Posts: 665
Received Thanks: 230
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;
}
alfredico is offline  
Old 12/01/2016, 09:35   #8
 
elite*gold: 50
Join Date: Jun 2015
Posts: 100
Received Thanks: 210
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
ディオニュソス is offline  
Old 12/01/2016, 14:43   #9
 
elite*gold: 0
Join Date: Oct 2012
Posts: 948
Received Thanks: 157
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 .
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
raventh1984 is offline  
Old 12/01/2016, 15:05   #10
 
elite*gold: 0
Join Date: Mar 2008
Posts: 665
Received Thanks: 230
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.

alfredico is offline  
Thanks
1 User
Old 12/01/2016, 15:40   #11
 
elite*gold: 0
Join Date: Oct 2012
Posts: 948
Received Thanks: 157
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.


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;
}
raventh1984 is offline  
Closed Thread


Similar Threads Similar Threads
Vector ?
10/17/2014 - C/C++ - 6 Replies
Hey, wollte mal etwas Fragen (2 sachen). Ich verende Vectoren(möchte sie verwenden) in einen TCP Server den ich im Moment schreibe. Ich habe 2 Threads die die Vectors erstmal bearbeiten. Code: DWORD TCP_Server::Client_Accept_Thread() { sockaddr_in Clientinfo; int Clientinfolen = sizeof(Clientinfo);
C# array of tabControls, creating new tabs by template
08/12/2014 - .NET Languages - 0 Replies
EDIT: I found a way to do that, anyone willing to check my code If it can be better or contains any mistakes I need to create new tabControl with tabPages content inside tabPage, but I am still at the beginning this creates pages I want, then I need to create in each page new tabControl http://oi59.tinypic.com/1597gg.jpg for (int i = 0; i < l_Controller.Player.VillageCount; i++) {
[c++] help with sub-vector
05/22/2014 - C/C++ - 8 Replies
Well, i have a vector that stores objects of my class named 'Tools': vercotr<Tools>myvect that has lets say 5 objects saved in it... But now i want to make a method that makes a subvector from lets say 2nd (start) to 4th (end/finish) element in myvect and returns that newly created subvector. returnSubvect(2,4); that is returning: subvect Can anyone explain me how to do this? -Was strugling with this for some time now, but i cant get it to work.
Vector out of bounce
06/05/2013 - C/C++ - 7 Replies
try { return MapContainer.at(index) ; } catch (std::exception& e) { std::cout << "Element " << index << ": index exceeds vector dimensions." << std::endl; }
Mw2 Vector
05/12/2011 - Call of Duty - 9 Replies
Stimmt das wenn man Schaldämpfer raufschrauft dass sich die Vector nicht mehr so verzieht



All times are GMT +1. The time now is 23:23.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.