Here's the include we use for this. For items like this, we typically do two charges per use, with an odd number of charges (3, for one use with the possibility of recharge).
const int IP_CONST_CASTSPELL_TALK = 536;
void RestoreItemSpellUses (object oItem) {
int nSpell, nUses;
string sSpell, sRestored = "";
itemproperty ip = GetFirstItemProperty(oItem);
while (GetIsItemPropertyValid(ip)) {
if (GetItemPropertyDurationType(ip) == DURATION_TYPE_PERMANENT &&
GetItemPropertyType(ip) == ITEM_PROPERTY_CAST_SPELL) {
nSpell = GetItemPropertySubType(ip);
sSpell = " " + IntToString(nSpell) + " ";
if (FindSubString(sRestored, sSpell) < 0) {
sRestored += sSpell;
nUses = GetItemPropertyCostTableValue(ip);
RemoveItemProperty(oItem, ip);
AddItemProperty(DURATION_TYPE_PERMANENT, ItemPropertyCastSpell(nSpell, nUses), oItem);
}
}
ip = GetNextItemProperty(oItem);
}
}
int GetItemSpellUses (object oItem) {
int nUses = GetItemCharges(oItem);
if (nUses != 1)
return nUses;
itemproperty ip = GetFirstItemProperty(oItem);
while (GetIsItemPropertyValid(ip)) {
if (GetItemPropertyDurationType(ip) == DURATION_TYPE_PERMANENT &&
GetItemPropertyType(ip) == ITEM_PROPERTY_CAST_SPELL) {
if (GetItemPropertyCostTableValue(ip) == IP_CONST_CASTSPELL_NUMUSES_2_CHARGES_PER_USE)
return 0;
}
ip = GetNextItemProperty(oItem);
}
return 1;
}
void SetItemSpellUses (object oItem, int nUses, int nItemSpellId=-1) {
int nSpell, nSpellUses, nCostTable, nLimit, nRadial = 0;
string sSpell, sSet = "";
if (nUses > 0 && nItemSpellId < 0) {
if (nUses > 50)
SetItemCharges(oItem, 50);
else
SetItemCharges(oItem, nUses);
}
itemproperty ip = GetFirstItemProperty(oItem);
while (GetIsItemPropertyValid(ip)) {
if (GetItemPropertyDurationType(ip) == DURATION_TYPE_PERMANENT &&
GetItemPropertyType(ip) == ITEM_PROPERTY_CAST_SPELL) {
nSpell = GetItemPropertySubType(ip);
sSpell = " " + IntToString(nSpell) + " ";
if (FindSubString(sSet, sSpell) < 0) {
sSet += sSpell;
if ((nItemSpellId >= 0 && nSpell != nItemSpellId) ||
(nItemSpellId < 0 && nSpell == IP_CONST_CASTSPELL_TALK)) {
nCostTable = GetItemPropertyCostTableValue(ip);
} else if (nUses == 0) {
nCostTable = IP_CONST_CASTSPELL_NUMUSES_2_CHARGES_PER_USE;
} else if (nItemSpellId >= 0 || nUses < 0) {
if (nUses < 0) {
nLimit = GetLocalInt(oItem, "Radial_" + IntToString(nRadial) + "_Limit");
nSpellUses = nLimit - GetLocalInt(oItem, "Radial_" + IntToString(nRadial) + "_Used");
if (nLimit < 1 || nSpellUses < 0)
nSpellUses = -1;
} else
nSpellUses = nUses;
switch (nSpellUses) {
case -1: nCostTable = IP_CONST_CASTSPELL_NUMUSES_4_CHARGES_PER_USE; break;
case 0: nCostTable = IP_CONST_CASTSPELL_NUMUSES_2_CHARGES_PER_USE; break;
case 1: nCostTable = IP_CONST_CASTSPELL_NUMUSES_1_USE_PER_DAY; break;
case 2: nCostTable = IP_CONST_CASTSPELL_NUMUSES_2_USES_PER_DAY; break;
case 3: nCostTable = IP_CONST_CASTSPELL_NUMUSES_3_USES_PER_DAY; break;
case 4: nCostTable = IP_CONST_CASTSPELL_NUMUSES_4_USES_PER_DAY; break;
case 5: nCostTable = IP_CONST_CASTSPELL_NUMUSES_5_USES_PER_DAY; break;
default: nCostTable = IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE; break;
}
} else
nCostTable = IP_CONST_CASTSPELL_NUMUSES_1_CHARGE_PER_USE;
nRadial++;
RemoveItemProperty(oItem, ip);
AddItemProperty(DURATION_TYPE_PERMANENT, ItemPropertyCastSpell(nSpell, nCostTable), oItem);
}
}
ip = GetNextItemProperty(oItem);
}
if (nUses <= 0)
SetItemCharges(oItem, 1);
}
Funky