[C++]Kind Of New lua Function

03/07/2015 14:15 MrLibya#1
hallo all

Will it's not really new lua function

some time ago i was want delete a item from all players when login by quest

but the problem was if the player is equip the item thos two function will not count it
PHP Code:
pc.count_item
pc
.remove_item 
so i just make new function for this
PHP Code:
pc.count_item0
pc
.removeitem0 
it's the same of the old function but thos can count the equip item

questlua_pc.cpp :

add thos :
PHP Code:
    int pc_count_item0(lua_StateL)
    {
        if (
lua_isnumber(L, -1))
            
lua_pushnumber(L,CQuestManager::instance().GetCurrentCharacterPtr()->CountSpecifyItem0((DWORD)lua_tonumber(L, -1)));
        else if (
lua_isstring(L, -1))
        {
            
DWORD item_vnum;

            if (!
ITEM_MANAGER::instance().GetVnum(lua_tostring(L,1), item_vnum))
            {
                
sys_err("QUEST count_item call error : wrong item name : %s"lua_tostring(L,1));
                
lua_pushnumber(L0);
            }
            else
            {
                
lua_pushnumber(LCQuestManager::instance().GetCurrentCharacterPtr()->CountSpecifyItem0(item_vnum));
            }
        }
        else
            
lua_pushnumber(L0);

        return 
1;
    }
 
    
int pc_remove_item0(lua_StateL)
    {
        if (
lua_gettop(L) == 1)
        {
            
DWORD item_vnum;

            if (
lua_isnumber(L,1))
            {
                
item_vnum = (DWORD)lua_tonumber(L1);
            }
            else if (
lua_isstring(L,1))
            {
                if (!
ITEM_MANAGER::instance().GetVnum(lua_tostring(L,1), item_vnum))
                {
                    
sys_err("QUEST remove_item call error : wrong item name : %s"lua_tostring(L,1));
                    return 
0;
                }
            }
            else
            {
                
sys_err("QUEST remove_item wrong argument");
                return 
0;
            }

            
sys_log(0,"QUEST remove a item vnum %d of %s[%d]"item_vnumCQuestManager::instance().GetCurrentCharacterPtr()->GetName(), CQuestManager::instance().GetCurrentCharacterPtr()->GetPlayerID());
            
CQuestManager::instance().GetCurrentCharacterPtr()->RemoveSpecifyItem0(item_vnum);
        }
        else if (
lua_gettop(L) == 2)
        {
            
DWORD item_vnum;

            if (
lua_isnumber(L1))
            {
                
item_vnum = (DWORD)lua_tonumber(L1);
            }
            else if (
lua_isstring(L1))
            {
                if (!
ITEM_MANAGER::instance().GetVnum(lua_tostring(L,1), item_vnum))
                {
                    
sys_err("QUEST remove_item call error : wrong item name : %s"lua_tostring(L,1));
                    return 
0;
                }
            }
            else
            {
                
sys_err("QUEST remove_item wrong argument");
                return 
0;
            }

            
DWORD item_count = (DWORDlua_tonumber(L2);
            
sys_log(0"QUEST remove items(vnum %d) count %d of %s[%d]",
                    
item_vnum,
                    
item_count,
                    
CQuestManager::instance().GetCurrentCharacterPtr()->GetName(),
                    
CQuestManager::instance().GetCurrentCharacterPtr()->GetPlayerID());

            
CQuestManager::instance().GetCurrentCharacterPtr()->RemoveSpecifyItem0(item_vnumitem_count);
        }
        return 
0;
    } 
PHP Code:
"count_item0",        pc_count_item0        },
"removeitem0",        pc_remove_item0        }, 
now open char_item.cpp
PHP Code:
find

int CHARACTER
::CountSpecifyItem(DWORD vnum) const

after this function add :

int CHARACTER::CountSpecifyItem0(DWORD vnum) const
{
    
int    count 0;
    
LPITEM item;

    for (
int i 0INVENTORY_AND_EQUIP_SLOT_MAX; ++i)
    {
        
item GetFullInventoryItem(i);
        if (
NULL != item && item->GetVnum() == vnum)
        {
            
// °³ہخ »َء،؟، µî·دµب ¹°°اہج¸é ³ر¾î°£´ظ.
            
if (m_pkMyShop && m_pkMyShop->IsSellingItem(item->GetID()))
            {
                continue;
            }
            else
            {
                
count += item->GetCount();
            }
        }
    }

    return 
count;
}

void CHARACTER::RemoveSpecifyItem0(DWORD vnumDWORD count)
{
    if (
== count)
        return;

    for (
UINT i 0INVENTORY_AND_EQUIP_SLOT_MAX; ++i)
    {
        if (
NULL == GetFullInventoryItem(i))
            continue;

        if (
GetFullInventoryItem(i)->GetVnum() != vnum)
            continue;

        if (
count >= GetFullInventoryItem(i)->GetCount())
        {
            
count -= GetFullInventoryItem(i)->GetCount();
            
GetFullInventoryItem(i)->SetCount(0);

            if (
== count)
                return;
        }
        else
        {
            
GetFullInventoryItem(i)->SetCount(GetFullInventoryItem(i)->GetCount() - count);
            return;
        }
    }

    
// ؟¹؟ـأ³¸®°، ¾àاد´ظ.
    
if (count)
        
sys_log(0"CHARACTER::RemoveSpecifyItem cannot remove enough item vnum %u, still remain %d"vnumcount);

find
PHP Code:
LPITEM CHARACTER::GetInventoryItem(WORD wCell) const

after it add :

LPITEM CHARACTER::GetFullInventoryItem(WORD wCell) const
{
    return 
GetItem(TItemPos(EQUIPMENTwCell));

open char.h
PHP Code:

find 
:
LPITEM            GetInventoryItem(WORD wCell) const;
add after it
LPITEM            GetFullInventoryItem
(WORD wCell) const;
 
find
void            RemoveSpecifyItem
(DWORD vnumDWORD count 1);
add after it
void            RemoveSpecifyItem0
(DWORD vnumDWORD count 1);
 
find
int                CountSpecifyItem
(DWORD vnum) const;
add after it
int                CountSpecifyItem0
(DWORD vnum) const; 
someone will say this is unusles

there too many ideas for it

e.g :
PHP Code:
quest gm_item_check begin
    state start beign
        when login with not pc
.is_gm() begin
        
if pc.count_item0(xx) > 0 then --- will count the item in INVENTORY AND EQUIP //
            
say("You Are Not Allowed To Have This Item")
            
pc.removeitem0(xx ,1) --- if He Have 2 From This Item He Will Delete the own in INVENTORY And If He Only Have 1 And He IS Equip It Item will Be Delete
            pc
.removeitem0(xx) -- same of The First
            pc
.removeitem0(xxpc.count_item0(xx)) -- He Will Delete IT all
            end
        end
    end
end 
Also Can Be Used For the Monarch :
PHP Code:
                if pc.count_item(11971) > or pc.count_item(11972) > or pc.count_item(11973) > or pc.count_item(11974) > 0 then
                    
if pc.get_part(PART_MAIN) > 11970 and pc.get_part(PART_MAIN) < 11975 then
                        syschat
(" الرجاء عدم ارتداء عتاد الملك .")
                        
syschat(" أنت غير مؤهل لارتدائها. ")
                    else
                        
local armor 11971 pc.get_job()
                        
pc.removeitem(armor)
                        
syschat(" تم ازالة درع الهوانغ الخاص بك. ")
                    
end
                end 
Here He Will Not Count Or Delete The Item If He IS Equip it

so We Have To Use The New Function For It

Also can Be Used For a lot of new things

Regards MrLibya
03/07/2015 14:20 rollback#2
same in LUA but only if the item is a weapon or a armor

PHP Code:
function pc.count_item0(vnum)
    
local count pc.count_item(vnum)
    if 
pc.get_weapon() == vnum or pc.get_armor() == vnum then
        count 
count+1
    end
    
return count
end 
or

PHP Code:
function pc.count_item0(vnum)
    
local count pc.count_item(vnum)
    if 
in_list(vnum, {pc.get_armor(), pc.get_weapon()}) then
        count 
count+1
    end
    
return count
end 
03/07/2015 14:28 Lefloyd#3
Actually you don't have to create new functions of the CHARACTER-Class to count the equipment items. There are many ways to get the inventory & equipment items without new functions. Event the "CHARACTER::GetInventoryItem"-function allows you to get the equipment items. It uses "INVENTORY" as window_type but if you look into the "CHARACTER::GetItem"-function (which is called by the GetInventoryItem-function) you see:
Code:
	case INVENTORY:
	case EQUIPMENT:
		if (wCell >= INVENTORY_AND_EQUIP_SLOT_MAX)
		{
			sys_err("CHARACTER::GetInventoryItem: invalid item cell %d", wCell);
			return NULL;
		}
		return m_pointsInstant.pItems[wCell];
There is no difference between the window_type INVENTORY and EQUIPMENT.
Just use something like:
Code:
for (int i = 0; i < INVENTORY_AND_EQUIP_SLOT_MAX; ++i)
	{
		if (LPITEM pkItem = GetInventoryItem(i))
		{
			if (pkItem->GetVnum() == lua_tonumber(L, 1))
			{
				lua_pushboolean(L, 1);
				return 1;
			}
		}
	}
Anyways thanks for the release, I'm happy to see there are some C++-releases there now and I'm looking forward to see some more things. :)

Kind Regards
03/07/2015 14:41 MrLibya#4
Quote:
Originally Posted by [Sensenmann] View Post
same in LUA but only if the item is a weapon or a armor

PHP Code:
function pc.count_item0(vnum)
    
local count pc.count_item(vnum)
    if 
pc.get_weapon() == vnum or pc.get_armor() == vnum then
        count 
count+1
    end
    
return count
end 
Yes But This is Beeter Becous Its for all type :)

Quote:
Originally Posted by Lefloyd View Post
Actually you don't have to create new functions of the CHARACTER-Class to count the equipment items. There are many ways to get the inventory & equipment items without new functions. Event the "CHARACTER::GetInventoryItem"-function allows you to get the equipment items. It uses "INVENTORY" as window_type but if you look into the "CHARACTER::GetItem"-function (which is called by the GetInventoryItem-function) you see:
Code:
    case INVENTORY:
    case EQUIPMENT:
        if (wCell >= INVENTORY_AND_EQUIP_SLOT_MAX)
        {
            sys_err("CHARACTER::GetInventoryItem: invalid item cell %d", wCell);
            return NULL;
        }
        return m_pointsInstant.pItems[wCell];
There is no difference between the window_type INVENTORY and EQUIPMENT.
Just use something like:
Code:
for (int i = 0; i < INVENTORY_AND_EQUIP_SLOT_MAX; ++i)
    {
        if (LPITEM pkItem = GetInventoryItem(i))
        {
            if (pkItem->GetVnum() == lua_tonumber(L, 1))
            {
                lua_pushboolean(L, 1);
                return 1;
            }
        }
    }
Anyways thanks for the release, I'm happy to see there are some C++-releases there now and I'm looking forward to see some more things. :)

Kind Regards
Yes I didnt code it From 0 , i just edit Litile Things On The Old One
+ Yes U Can Sample Edit The Old Function To Make it Count eqiup

But I Think its beeter To Make New Function For It :)
03/07/2015 15:51 Lefloyd#5
I think you misunderstood me ^^ I meant your function "CHARACTER::GetFullInventoryItem" is actually equal to "CHARACTER::GetInventoryItem" - you don't have to create a new function there.^^

I also think you should write a new function and not edit an old one :)

Kind Regards
03/07/2015 16:02 terron#6
Quote:
Originally Posted by MrLibya View Post
Yes But This is Beeter Becous Its for all type :)
You can use item.select_cell() function. For example:

Code:
item.select_cell(91)
local a = item.get_cell() == 91 and item.get_vnum() or 0
chat(a)
It returns the ID of the item contained in the 91th slot, for 2-windowed inventory it's a helmet.
03/07/2015 16:17 rollback#7
Quote:
Originally Posted by terron View Post
You can use item.select_cell() function. An example:

Code:
item.select_cell(91)
local a = item.get_cell() == 91 and item.get_vnum() or 0
chat(a)
It returns an ID of the item contained in the 91th slot, for 2-windowed inventory it's a helmet.
so this would work?:

PHP Code:
function pc.count_item0(vnum)
    
local count pc.get_item(vnum)
    for 
9198 do
        
item.select_cell(i)
        if 
item.get_vnum() == vnum then
            count 
count+1
        end
    end
    
return count
end 
03/07/2015 16:28 MrLibya#8
Quote:
Originally Posted by Lefloyd View Post
I think you misunderstood me ^^ I meant your function "CHARACTER::GetFullInventoryItem" is actually equal to "CHARACTER::GetInventoryItem" - you don't have to create a new function there.^^

I also think you should write a new function and not edit an old one :)

Kind Regards
oh yeah , sry :p
Thx For The tip but i in this moemnt don't know anything about c++
but in few days i will start leurning it
and thx for u replay
03/07/2015 16:28 terron#9
Quote:
Originally Posted by [Sensenmann] View Post
so this would work?:

Yes, but I recommend you to create own functions for every slot. It may be also useful in future. I use selfmade functions like pc.get_helmet():

Code:
pc.get_helmet = function() item.select_cell(91) return item.get_cell() == 91 and item.get_vnum() or 0 end
pc.get_boots = function() item.select_cell(92) return item.get_cell() == 92 and item.get_vnum() or 0 end
...
etc
So you may simply use it in your pc.count_item0() function:

Code:
pc.count_item0 = function(item_id)
	local count = pc.count_item(item_id)
	if pc.get_helmet() == item_id then count = count + 1 end
	if pc.get_boots() == item_id then count = count + 1 end
	...
	etc
	...
	return count
end
03/07/2015 18:23 Lefloyd#10
You can just write
Code:
if item.select_cell(91) then return item.get_vnum() else return 0 end
(or return (item.select_cell(91) and ...) or (...) but I don't like this writing style (: ) (if you want to do it in lua)

Kind Regards
03/08/2015 13:27 Vordeaux#11
Quote:
Originally Posted by terron View Post
Yes, but I recommend you to create own functions for every slot. It may be also useful in future. I use selfmade functions like pc.get_helmet():

Code:
pc.get_helmet = function() item.select_cell(91) return item.get_cell() == 91 and item.get_vnum() or 0 end
pc.get_boots = function() item.select_cell(92) return item.get_cell() == 92 and item.get_vnum() or 0 end
...
etc
selfmade aha.

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