Jump to content
metin2dev
  • 0
notbugme

solved Sort Inventory

Question

Hello, I had problem with adding this function:

Spoiler

ACMD(do_sort_inventory)
{
	std::vector<CItem*> collectItems;
	std::vector<WORD> oldCells;
	int totalSize = 0;
	for (auto i = 0; i < INVENTORY_MAX_COUNT; ++i) {
		auto item = ch->GetInventoryItem(i);
		if (item) {
			totalSize += item->GetSize();
			oldCells.push_back(item->GetCell());
			collectItems.push_back(item);
		}
	}
	if (totalSize - 3 >= INVENTORY_MAX_COUNT) {
		ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("INVENTORY_FULL_CANNOT_SORT"));
		return;
	}

	for (auto& item : collectItems) {
		item->RemoveFromCharacter();
	}

	std::sort(collectItems.begin(), collectItems.end(),
	          []( CItem* a,  CItem* b) {
		          return a->GetVnum() < b->GetVnum();
	          });

	for (auto& sortedItem : collectItems) {
		auto cell = ch->GetEmptyInventory(sortedItem->GetSize());
		sortedItem->AddToCharacter(ch, TItemPos(INVENTORY, cell), false);
	}
}

 

When I add this code I get this errors:

Spoiler

cmd_general.cpp: In function 'void do_sort_inventory(CHARACTER*, const char*, int, int)':
cmd_general.cpp:3641: error: ISO C++ forbids declaration of 'i' with no type
cmd_general.cpp:3641: error: 'INVENTORY_MAX_COUNT' was not declared in this scope
cmd_general.cpp:3642: error: ISO C++ forbids declaration of 'item' with no type
cmd_general.cpp:3642: error: invalid conversion from 'CItem*' to 'int'
cmd_general.cpp:3644: error: request for member 'GetSize' in 'item', which is of non-class type 'int'
cmd_general.cpp:3645: error: request for member 'GetCell' in 'item', which is of non-class type 'int'
cmd_general.cpp:3646: error: invalid conversion from 'int' to 'CItem*'
cmd_general.cpp:3646: error:   initializing argument 1 of 'void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = CItem*, _Alloc = std::allocator<CItem*>]'
cmd_general.cpp:3649: error: 'INVENTORY_MAX_COUNT' was not declared in this scope
cmd_general.cpp:3654: error: a function-definition is not allowed here before ':' token
cmd_general.cpp:3659: error: expected primary-expression before '[' token
cmd_general.cpp:3659: error: expected primary-expression before ']' token
cmd_general.cpp:3659: error: expected primary-expression before '*' token
cmd_general.cpp:3659: error: 'a' was not declared in this scope
cmd_general.cpp:3659: error: expected primary-expression before '*' token
cmd_general.cpp:3659: error: 'b' was not declared in this scope
cmd_general.cpp:3663: error: expected primary-expression before 'for'
cmd_general.cpp:3663: error: expected `)' before 'for'
cmd_general.cpp:3663: error: a function-definition is not allowed here before ':' token
cmd_general.cpp:3667: error: expected primary-expression before '}' token
cmd_general.cpp:3667: error: expected `;' before '}' token
cmd_general.cpp:3667: error: expected primary-expression before '}' token
cmd_general.cpp:3667: error: expected `)' before '}' token
cmd_general.cpp:3667: error: expected primary-expression before '}' token
cmd_general.cpp:3667: error: expected `;' before '}' token

 

 

Share this post


Link to post
Share on other sites

14 answers to this question

Recommended Posts

  • 0
9 minut temu, kasko30 napisał:

you have problem with putting ; and ]


error: expected `;' before '}' token

I know it but i can't found where 

Share this post


Link to post
Share on other sites
  • 0

AddToCharacter take two agrguments not three.

INVENTORY_MAX_COUNT dosen't exist in source, unless you changed his name.

C++11 is required for these codes to work. You are using lambda expression which is a c++11 feature, same for auto.

 

Here it's the fixed version.

Spoiler

{
	std::vector<CItem*> collectItems;
	std::vector<WORD> oldCells;
	int totalSize = 0;
	for (auto i = 0; i < INVENTORY_MAX_NUM; ++i) {
		auto item = ch->GetInventoryItem(i);
		if (item) {
			totalSize += item->GetSize();
			oldCells.push_back(item->GetCell());
			collectItems.push_back(item);
		}
	}
	if (totalSize - 3 >= INVENTORY_MAX_NUM) {
		ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("INVENTORY_FULL_CANNOT_SORT"));
		return;
	}

	for (auto& item : collectItems) {
		item->RemoveFromCharacter();
	}

	std::sort(collectItems.begin(), collectItems.end(),
	          []( CItem* a,  CItem* b) {
		          return a->GetVnum() < b->GetVnum();
	          });

	for (auto& sortedItem : collectItems) {
		auto cell = ch->GetEmptyInventory(sortedItem->GetSize());
		sortedItem->AddToCharacter(ch, TItemPos(INVENTORY, cell));
	}
}

 

 

eYEBWK4qQbC7YzVZR52P9A.png

 

You get the errors because your compiler dosen't support C++11.

 

From what source did you steal the codes?

Share this post


Link to post
Share on other sites
  • 0
il y a 9 minutes, notbugme a dit :

I don't steal this i found on another forum bro, thanks for checks.

Someone can transform this code to older compiler?  

What is your compiler at the moment ? You can simply add -std=c++11 into your makefile, but I clearly suggest you to update your compiler (either clang or gcc)

Share this post


Link to post
Share on other sites
  • 0

Here is the code for old compilers:

struct s_sort {
	bool operator() (LPITEM src, LPITEM dst) { return (src->GetVnum()<dst->GetVnum()); }
} _sort;

ACMD(do_sort_inventory)
{
	std::vector<LPITEM> collectItems;
	std::vector<WORD> oldCells;
	int totalSize = 0;
	for (WORD i = 0; i < INVENTORY_MAX_NUM; ++i)
	{
		LPITEM item = ch->GetInventoryItem(i);
		if (item)
		{
			totalSize += item->GetSize();
			oldCells.push_back(item->GetCell());
			collectItems.push_back(item);
		}
	}
	if (totalSize - 3 >= INVENTORY_MAX_NUM)
	{
		ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("INVENTORY_FULL_CANNOT_SORT"));
		return;
	}

	std::vector<LPITEM>::iterator it = collectItems.begin(), end = collectItems.end();
	for (; it != end; ++it)
		((LPITEM)*it)->RemoveFromCharacter();

	std::sort(collectItems.begin(), collectItems.end(), _sort);

	std::vector<LPITEM>::iterator it1 = collectItems.begin(), end1 = collectItems.end();
	for (; it1 != end1; ++it1)
	{
		WORD cell = ch->GetEmptyInventory(((LPITEM)*it1)->GetSize());
		((LPITEM)*it1)->AddToCharacter(ch, TItemPos(INVENTORY, cell)/*, isNew=false*/);
	}
}

 

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
vor 19 Minuten schrieb xP3NG3Rx:

Here is the code for old compilers:


struct s_sort {
	bool operator() (LPITEM src, LPITEM dst) { return (src->GetVnum()<dst->GetVnum()); }
} _sort;

ACMD(do_sort_inventory)
{
	std::vector<LPITEM> collectItems;
	std::vector<WORD> oldCells;
	int totalSize = 0;
	for (WORD i = 0; i < INVENTORY_MAX_NUM; ++i)
	{
		LPITEM item = ch->GetInventoryItem(i);
		if (item)
		{
			totalSize += item->GetSize();
			oldCells.push_back(item->GetCell());
			collectItems.push_back(item);
		}
	}
	if (totalSize - 3 >= INVENTORY_MAX_NUM)
	{
		ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("INVENTORY_FULL_CANNOT_SORT"));
		return;
	}

	std::vector<LPITEM>::iterator it = collectItems.begin(), end = collectItems.end();
	for (; it != end; ++it)
		((LPITEM)*it)->RemoveFromCharacter();

	std::sort(collectItems.begin(), collectItems.end(), _sort);

	std::vector<LPITEM>::iterator it1 = collectItems.begin(), end1 = collectItems.end();
	for (; it1 != end1; ++it1)
	{
		WORD cell = ch->GetEmptyInventory(((LPITEM)*it1)->GetSize());
		((LPITEM)*it1)->AddToCharacter(ch, TItemPos(INVENTORY, cell)/*, isNew=false*/);
	}
}

 

with auto stack or not? <3

Share this post


Link to post
Share on other sites
  • 0
if (totalSize - 3 >= INVENTORY_MAX_NUM)
	{
		ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("INVENTORY_FULL_CANNOT_SORT"));
		return;
	}

This function don't work for me.

I have 5 inventory.Can help me? Thank!

Share this post


Link to post
Share on other sites
  • 0
Am 13.1.2018 um 01:35 schrieb thespeedy:

how to cmd.cpp?

and can anyone add stack?

i have 4x 30pots and make 1x 120 pots?

push no stack items :(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×