GetItemACValue

From NWN Lexicon
Jump to: navigation, search

GetItemACValue(object)

Get the armor class (AC) of an item.

int GetItemACValue(
    object oItem
);

Parameters

oItem

Item whose AC is to be determined.

Description

Returns the armor class (AC) of oItem. Returns 0 if oItem is not a valid item, or if oItem has no armor value.

Remarks

This will return the full AC value of an item - taking into account all modifiers on the item in regards to + AC. The function will however stacks multiple ac bonuses instead of taking highest! This function does not take into account AC bonuses vs certain condition nor AC decrease itemproperty.

It will not take into account ability changes, nor if there is an existing amount of that bonus type (for example, wearing +1 armor, thus using a +1 Armor AC modifier, will not stack with Epic Mage Armor, which gives +5 in Armor AC bonuses).

For a function that will return the base armor value of an item, check the code sample. There are various ways to do this, and it can be easily done using 2da files, item property checking and so forth, but to be honest, the code provided is quite simple enough, and easier to understand!

Version

1.62

Example

// Example of a time to use this function, when a blacksmith needs
// to check if a piece of armor the character is wearing can be
// actually forged.
// It can only be forged if it is has an total AC of 4 to 7. This is
// when it will return TRUE.
 
void main()
{
    // Get the item
    object oItem = GetItemInSlot(INVENTORY_SLOT_CHEST, GetPCSpeaker());
 
    // Get the AC value of it
    int nAC = GetItemACValue(oItem);
 
    // Check the value
    if(nAC >= 4 && nAC <= 7)
    {
        // Passes
        return TRUE;
    }
    return FALSE;
}
 
// Sample code 2. This is a very simple function that will accurately
// use the unidentified (base price of that item type) to get the
// base AC value.
// So, 0 is clothing, up to 8, which is full plate.
 
 
// Returns the base armor type as a number, of oItem
// -1 if invalid, or not armor, or just plain not found.
// 0 to 8 as the value of AC got from the armor - 0 for none, 8 for Full plate.
int GetArmorType(object oItem)
{
    // Make sure the item is valid and is an armor.
    if (!GetIsObjectValid(oItem))
        return -1;
    if (GetBaseItemType(oItem) != BASE_ITEM_ARMOR)
        return -1;
 
    // Get the identified flag for safe keeping.
    int bIdentified = GetIdentified(oItem);
    SetIdentified(oItem,FALSE);
 
    int nType = -1;
    switch (GetGoldPieceValue(oItem))
    {
        case    1: nType = 0; break; // None
        case    5: nType = 1; break; // Padded
        case   10: nType = 2; break; // Leather
        case   15: nType = 3; break; // Studded Leather / Hide
        case  100: nType = 4; break; // Chain Shirt / Scale Mail
        case  150: nType = 5; break; // Chainmail / Breastplate
        case  200: nType = 6; break; // Splint Mail / Banded Mail
        case  600: nType = 7; break; // Half-Plate
        case 1500: nType = 8; break; // Full Plate
    }
    // Restore the identified flag, and return armor type.
    SetIdentified(oItem,bIdentified);
    return nType;
}
 
// In any version of the game (1.69 and up into NWN:EE) that properly caches 2da's this is a very fast and simple lookup.
// It also safely works with 2da changes to item costs
int GetArmorBaseACValue(object oArmor)
{
    // Get the appearance of the torso slot
    int nAppearance = GetItemAppearance(oArmor, ITEM_APPR_TYPE_ARMOR_MODEL, ITEM_APPR_ARMOR_MODEL_TORSO);
    // Look up in parts_chest.2da the relevant line, which links to the actual AC bonus of the armor
    // We cast it to int, even though the column is technically a float.
    int nAC = StringToInt(Get2DAString("parts_chest", "ACBONUS", nAppearance));
 
    // Return the given AC value (0 to 8)
    return nAC;
}

See Also

functions: 

GetBaseItemType


 author: Jason Harris, editor: Jasperre, additional contributor(s): Kristian Markon, Jasperre