• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved In game changeable attributes from items.xml [TFS 1.2]

Lakkir

Active Member
Joined
Jan 6, 2009
Messages
29
Reaction score
25
Location
Chile
I don't usually post here (I think this is the first time) but i'm too tired of trying.
Here is the thing, I'm making a Durability System.
I create the attribute key in items.xml and I can see the durability in game (when you look)
The problem it's:
item:setDurability
does not work and
item:setAtrribute(ITEM_ATTRIBUTE_DURABILITY, xx)
does not work.
xm9qZSW.png


The problem is I can't change the durability of the item in game, there is a way to do that?

Here is some pieces of code that i'm using, i'll post all the sources at the end.
Mercy please, I'm new in this

enums.h
Code:
enum itemAttrTypes : uint32_t {
    ITEM_ATTRIBUTE_NONE,

    ITEM_ATTRIBUTE_ACTIONID = 1 << 0,
    ITEM_ATTRIBUTE_UNIQUEID = 1 << 1,
    ITEM_ATTRIBUTE_DESCRIPTION = 1 << 2,
    ITEM_ATTRIBUTE_TEXT = 1 << 3,
    ITEM_ATTRIBUTE_DATE = 1 << 4,
    ITEM_ATTRIBUTE_WRITER = 1 << 5,
    ITEM_ATTRIBUTE_NAME = 1 << 6,
    ITEM_ATTRIBUTE_ARTICLE = 1 << 7,
    ITEM_ATTRIBUTE_PLURALNAME = 1 << 8,
    ITEM_ATTRIBUTE_WEIGHT = 1 << 9,
    ITEM_ATTRIBUTE_ATTACK = 1 << 10,
    ITEM_ATTRIBUTE_DEFENSE = 1 << 11,
    ITEM_ATTRIBUTE_EXTRADEFENSE = 1 << 12,
    ITEM_ATTRIBUTE_ARMOR = 1 << 13,
    ITEM_ATTRIBUTE_HITCHANCE = 1 << 14,
    ITEM_ATTRIBUTE_SHOOTRANGE = 1 << 15,
    ITEM_ATTRIBUTE_OWNER = 1 << 16,
    ITEM_ATTRIBUTE_DURATION = 1 << 17,
    ITEM_ATTRIBUTE_DECAYSTATE = 1 << 18,
    ITEM_ATTRIBUTE_CORPSEOWNER = 1 << 19,
    ITEM_ATTRIBUTE_CHARGES = 1 << 20,
    ITEM_ATTRIBUTE_FLUIDTYPE = 1 << 21,
    ITEM_ATTRIBUTE_DOORID = 1 << 22,
    ITEM_ATTRIBUTE_DURABILITY = 1 << 23
};

item.h

Code:
enum AttrTypes_t {

    //ATTR_DESCRIPTION = 1,
    //ATTR_EXT_FILE = 2,
    ATTR_TILE_FLAGS = 3,
    ATTR_ACTION_ID = 4,
    ATTR_UNIQUE_ID = 5,
    ATTR_TEXT = 6,
    ATTR_DESC = 7,
    ATTR_TELE_DEST = 8,
    ATTR_ITEM = 9,
    ATTR_DEPOT_ID = 10,
    //ATTR_EXT_SPAWN_FILE = 11,
    ATTR_RUNE_CHARGES = 12,
    //ATTR_EXT_HOUSE_FILE = 13,
    ATTR_HOUSEDOORID = 14,
    ATTR_COUNT = 15,
    ATTR_DURATION = 16,
    ATTR_DECAYING_STATE = 17,
    ATTR_WRITTENDATE = 18,
    ATTR_WRITTENBY = 19,
    ATTR_SLEEPERGUID = 20,
    ATTR_SLEEPSTART = 21,
    ATTR_CHARGES = 22,
    ATTR_CONTAINER_ITEMS = 23,
    ATTR_NAME = 24,
    ATTR_ARTICLE = 25,
    ATTR_PLURALNAME = 26,
    ATTR_WEIGHT = 27,
    ATTR_ATTACK = 28,
    ATTR_DEFENSE = 29,
    ATTR_EXTRADEFENSE = 30,
    ATTR_ARMOR = 31,
    ATTR_HITCHANCE = 32,
    ATTR_SHOOTRANGE = 33,
    ATTR_DURABILITY = 34,
};

Code:
void setDurability(uint32_t n) {
setIntAttr(ITEM_ATTRIBUTE_DURABILITY, n);
}

Code:
uint32_t getDurability() const {
return static_cast<uint32_t>(getIntAttr(ITEM_ATTRIBUTE_DURABILITY));
}

Code:
void setDurability(uint32_t n) {
setIntAttr(ITEM_ATTRIBUTE_DURABILITY, n);
}

Code:
int32_t getDurability() const {
if (hasAttribute(ITEM_ATTRIBUTE_DURABILITY)) {
return getIntAttr(ITEM_ATTRIBUTE_DURABILITY);
}
return items[id].durability;
}

items.h
Code:
int32_t durability;

luascript.h
Code:
static int luaItemGetDurability(lua_State* L);

static int luaItemSetDurability(lua_State* L);


item.cpp
Code:
case ATTR_DURABILITY: {
uint32_t durability;
if (!propStream.read<uint32_t>(durability)) {
return ATTR_READ_ERROR;
}
setIntAttr(ITEM_ATTRIBUTE_DURABILITY, durability);
break;
}

Code:
if (hasAttribute(ITEM_ATTRIBUTE_DURABILITY)) {
propWriteStream.write<uint8_t>(ATTR_DURABILITY);
propWriteStream.write<uint32_t>(getIntAttr(ITEM_ATTRIBUTE_DURABILITY));
}

Code:
if (it.durability || (item && item->getDurability()))
{
s << std::endl << "Resistencia:" << int32_t(item ? item->getDurability() : it.durability) << std::noshowpos;
}

Code:
ItemType::ItemType()
{
.......
durability = 0;
.......
}

Code:
}else if (tmpStrValue == "durability") {
it.durability= pugi::cast<uint32_t>(valueAttribute.value());
}

luascript.cpp
Code:
registerEnum(ITEM_ATTRIBUTE_DURABILITY)

Code:
registerMethod("Item", "getDurability", LuaScriptInterface::luaItemGetDurability);
registerMethod("Item", "setDurability", LuaScriptInterface::luaItemSetDurability);

Code:
int LuaScriptInterface::luaItemgetDurability(lua_State* L)
{
    // item:getDurability()
    Item* item = getUserdata<Item>(L, 1);
    if (item) {
        lua_pushnumber(L, item->getDurability());
    }
    else {
        lua_pushnil(L);
    }
    return 1;
}

int LuaScriptInterface::luaItemsetDurability(lua_State* L)
{
    // item:setDurability(Durability)
    uint32_t Durability = getNumber<uint32_t>(L, 2);
    Item* item = getUserdata<Item>(L, 1);
    if (item) {
        item->setDurability(Durability);
        pushBoolean(L, true);
    }
    else {
        lua_pushnil(L);
    }
    return 1;
}

Code:
static int luaItemgetDurability(lua_State* L);
static int luaItemsetDurability(lua_State* L);

Sources:
http://www.mediafire.com/download/ckg35zg2z42cack/src.rar
 
Last edited:
Item.getAttribute/setAttribute is not working with your new attribute because you haven't modified the hexadecimal value in item.h. The attribute is of the type integer, so you should look at the function isIntAttrType.
Code:
inline static bool isIntAttrType(itemAttrTypes type) {
    return (type & 0x7FFE13) != 0; // 0x7FFE13 is the combination of all integer attribute flags
}
Item attributes uses bitshifting (see bitwise operation for more information). The new attribute shifts 23 bits (1 << 23), which converted to decimal format is 8388608. Convert 0x7FFE13 to decimal format, sum up the values, convert the new value back to hexadecimal format, and you're done.

Code:
inline static bool isIntAttrType(itemAttrTypes type) {
    return (type & 0xFEFE26) != 0;
}
 
Damn! Thanks! Was a little bit difficult to understand but it works 100%
I appreciate you took the time to explain it to me instead of just showing me what should I put.
Thanks again.
Oh, and I think you made a mistake it was 0xFFFE13 (I think, at least it works)
 
Back
Top