Jump to content

Recommended Posts

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

 

 

Link to comment
Share on other sites

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?

Edited by Metin2 Dev
Core X - External 2 Internal
Link to comment
Share on other sites

  • Premium
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)

Link to comment
Share on other sites

  • Honorable Member

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*/);
	}
}

 

  • Love 2
Link to comment
Share on other sites

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

  • Love 1
Link to comment
Share on other sites

  • 3 weeks later...

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.