|
You last visited: Today at 21:43
Advertisement
[C++]Kind Of New lua Function
Discussion on [C++]Kind Of New lua Function within the Metin2 PServer Guides & Strategies forum part of the Metin2 Private Server category.
03/07/2015, 14:15
|
#1
|
elite*gold: 30
Join Date: Mar 2012
Posts: 517
Received Thanks: 339
|
[C++]Kind Of New lua Function
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_State* L) { 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(L, 0); } else { lua_pushnumber(L, CQuestManager::instance().GetCurrentCharacterPtr()->CountSpecifyItem0(item_vnum)); } } else lua_pushnumber(L, 0);
return 1; } int pc_remove_item0(lua_State* L) { if (lua_gettop(L) == 1) { DWORD item_vnum;
if (lua_isnumber(L,1)) { item_vnum = (DWORD)lua_tonumber(L, 1); } 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_vnum, CQuestManager::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(L, 1)) { item_vnum = (DWORD)lua_tonumber(L, 1); } 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; }
DWORD item_count = (DWORD) lua_tonumber(L, 2); 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_vnum, item_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 = 0; i < INVENTORY_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 vnum, DWORD count) { if (0 == count) return;
for (UINT i = 0; i < INVENTORY_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 (0 == 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", vnum, count); }
find
PHP Code:
LPITEM CHARACTER::GetInventoryItem(WORD wCell) const
after it add :
LPITEM CHARACTER::GetFullInventoryItem(WORD wCell) const { return GetItem(TItemPos(EQUIPMENT, wCell)); }
open char.h
PHP Code:
find : LPITEM GetInventoryItem(WORD wCell) const; add after it LPITEM GetFullInventoryItem(WORD wCell) const; find void RemoveSpecifyItem(DWORD vnum, DWORD count = 1); add after it void RemoveSpecifyItem0(DWORD vnum, DWORD 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(xx, pc.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) > 0 or pc.count_item(11972) > 0 or pc.count_item(11973) > 0 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
|
#2
|
elite*gold: 83
Join Date: Nov 2013
Posts: 2,891
Received Thanks: 2,764
|
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
|
#3
|
elite*gold: 0
Join Date: Jan 2014
Posts: 268
Received Thanks: 373
|
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
|
#4
|
elite*gold: 30
Join Date: Mar 2012
Posts: 517
Received Thanks: 339
|
Quote:
Originally Posted by [Sensenmann]
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
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
|
#5
|
elite*gold: 0
Join Date: Jan 2014
Posts: 268
Received Thanks: 373
|
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
|
#6
|
elite*gold: 0
Join Date: Jun 2011
Posts: 69
Received Thanks: 25
|
Quote:
Originally Posted by MrLibya
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
|
#7
|
elite*gold: 83
Join Date: Nov 2013
Posts: 2,891
Received Thanks: 2,764
|
Quote:
Originally Posted by terron
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 i = 91, 98 do item.select_cell(i) if item.get_vnum() == vnum then count = count+1 end end return count end
|
|
|
03/07/2015, 16:28
|
#8
|
elite*gold: 30
Join Date: Mar 2012
Posts: 517
Received Thanks: 339
|
Quote:
Originally Posted by Lefloyd
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 
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
|
#9
|
elite*gold: 0
Join Date: Jun 2011
Posts: 69
Received Thanks: 25
|
Quote:
Originally Posted by [Sensenmann]
so this would work?:
PHP Code:
function pc.count_item0(vnum) local count = pc.count_item(vnum) item.select_cell(91) if item.get_vnum() == vnum then count = count+1 end item.select_cell(92) if item.get_vnum() == vnum then count = count+1 end item.select_cell(93) if item.get_vnum() == vnum then count = count+1 end item.select_cell(94) if item.get_vnum() == vnum then count = count+1 end item.select_cell(95) if item.get_vnum() == vnum then count = count+1 end item.select_cell(96) if item.get_vnum() == vnum then count = count+1 end item.select_cell(97) if item.get_vnum() == vnum then count = count+1 end item.select_cell(98) if item.get_vnum() == vnum then count = count+1 end return count end
|
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
|
#10
|
elite*gold: 0
Join Date: Jan 2014
Posts: 268
Received Thanks: 373
|
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
|
#11
|
elite*gold: 0
Join Date: Feb 2015
Posts: 17
Received Thanks: 9
|
Quote:
Originally Posted by terron
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.
|
|
|
 |
Similar Threads
|
std::function of a function returning an std::function
11/11/2013 - C/C++ - 19 Replies
Nun muss ich nach langer Zeit auch mal wieder einen Thread erstellen, weil mir Google nicht mehr weiterhelfen kann.
Ich verzweifle an Folgendem Vorhaben:
#include <Windows.h>
#include <string>
#include <iostream>
using namespace std;
|
Running Function 2 after Function 1 finished
09/15/2013 - AutoIt - 3 Replies
Hey, its me again.
Im stuck on a problem since yesterday and as much as i hate to ask for help, i really dont know what else to try. I want Function 2 to run after Function 1 has finished. I tried GuiCtrlSetOnEvent and MsgLoop, but i dont really understand it. I tried to read tutorials but they didnt help at all.
The line that are underline is what im talking about. I want gamestart() to run first and when its finished, i want iniviteteam() to run.
#AutoIt3Wrapper_UseX64=n...
|
Würdet ihr ein Kind zeugen um das bereits kranke Kind zu retten...? (Leukämie)
04/28/2013 - Off Topic - 97 Replies
Ich gucke gerade mit meiner Freundin "Beim Leben meiner Schwester" und wir diskutieren darüber, ob wir das selber machen würden...
Thema des Films: Familie, 3 Kinder. Das zweite Kind hat Leukämie und das dritte Kind wurde mit dem Gedanken gezeugt, dass ihre Kranke Schwester per Blutspende, Rückenmarkspende etc. "retten" kann. Das erste Kind kam als Spender nicht infrage.
Ich meine, dass man das nicht mit diesen Gedanken tun sollte. Man "opfert" das neue Kind, nur weil man als Elternteil...
|
All times are GMT +1. The time now is 21:44.
|
|