[ATTRIBUTE] Critical Chance Hit
Idea:
weapons-critical-hit-system
Description:
This attribute change that player get critical hit +X%.
Example:
XML:
<attribute key="criticalhitchance" value="15" />
Warning:
Code was written 100% by me!!
If you don't know what you are doing, don't change anything and leave from this theard.
Test:
I tested it on TFS 0.4 trunk 3884 (it should work on better versions)
Edit:
item.cpp
Below:
[cpp]
case ATTR_ARTICLE:
{
std::string article;
if(!propStream.getString(article))
return ATTR_READ_ERROR;
setAttribute("article", article);
break;
}
[/cpp]
Paste:
[cpp]
case ATTR_CRITICALHITCHANCE:
{
int32_t criticalHitChance;
if(!propStream.getLong((uint32_t&)criticalHitChance))
return ATTR_READ_ERROR;
setAttribute("criticalhitchance", criticalHitChance);
break;
}
[/cpp]
Below:
[cpp]
bool begin = true;
if(tmp)
{
s << " (Arm:" << tmp;
begin = false;
}
[/cpp]
Paste:
[cpp]
if(it.criticalHitChance || (item && item->getCriticalHitChance()))
{
if(begin)
{
begin = false;
s << " (";
}
else
s << ", ";
s << "Crit Chance:" << std::showpos << int32_t(item ? item->getCriticalHitChance() : it.criticalHitChance) << "%"<< std::noshowpos;
}
[/cpp]
Below:
[cpp]
if(it.defense || it.extraDefense || (item && (item->getDefense() || item->getExtraDefense())))
{
if(begin)
{
begin = false;
s << " (";
}
else
s << ", ";
s << "Def:" << int32_t(item ? item->getDefense() : it.defense);
if(it.extraDefense || (item && item->getExtraDefense()))
s << " " << std::showpos << int32_t(item ? item->getExtraDefense() : it.extraDefense) << std::noshowpos;
}
[/cpp]
Paste:
[cpp]
if(it.criticalHitChance || (item && item->getCriticalHitChance()))
{
if(begin)
{
begin = false;
s << " (";
}
else
s << ", ";
s << "Crit Chance:" << std::showpos << int32_t(item ? item->getCriticalHitChance() : it.criticalHitChance) << "%"<< std::noshowpos;
}
[/cpp]
item.h
Below:
[cpp]
ATTR_DUALWIELD = 43,
[/cpp]
Paste:
[cpp]
ATTR_CRITICALHITCHANCE = 44,
[/cpp]
Below:
[cpp]
int32_t getAttack() const;
[/cpp]
Paste:
[cpp]
int32_t getCriticalHitChance() const;
[/cpp]
Below:
[cpp]
inline bool Item::isScriptProtected() const
{
const bool* v = getBooleanAttribute("scriptprotected");
if(v)
return *v;
return false;
}
[/cpp]
Paste:
[cpp]
inline int32_t Item::getCriticalHitChance() const
{
const int32_t* v = getIntegerAttribute("criticalhitchance");
if(v)
return *v;
return items[id].criticalHitChance;
}
[/cpp]
items.cpp
Below:
[cpp]
attackSpeed = 0;
[/cpp]
Paste:
[cpp]
criticalHitChance = 0;
[/cpp]
Below:
[cpp]
else if(tmpStrValue == "extradefense" || tmpStrValue == "extradef")
{
if(readXMLInteger(itemAttributesNode, "value", intValue))
it.extraDefense = intValue;
}
[/cpp]
Paste:
[cpp]
else if(tmpStrValue == "criticalhitchance")
{
if(readXMLInteger(itemAttributesNode, "value", intValue))
it.criticalHitChance = intValue;
}
[/cpp]
items.h
Change:
[cpp]
int32_t attack, extraAttack, defense, extraDefense, armor, breakChance, hitChance, maxHitChance,
[/cpp]
To:
[cpp]
int32_t attack, criticalHitChance, extraAttack, defense, extraDefense, armor, breakChance, hitChance, maxHitChance,
[/cpp]
luascript.cpp
Below:
[cpp]
setField(L, "text", item->text);
[/cpp]
Paste:
[cpp]
setField(L, "criticalHitChance", item->criticalHitChance);
[/cpp]
player.cpp
Below:
[cpp]
int32_t Player::getArmor() const
{
int32_t i = SLOT_FIRST, armor = 0;
for(; i < SLOT_LAST; ++i)
{
if(Item* item = getInventoryItem((slots_t)i))
armor += item->getArmor();
}
if(vocation->getMultiplier(MULTIPLIER_ARMOR) != 1.0)
return int32_t(armor * vocation->getMultiplier(MULTIPLIER_ARMOR));
return armor;
}
[/cpp]
Paste:
[cpp]
int32_t Player::getCriticalHitChance() const
{
int32_t i = SLOT_FIRST, crit = 0;
for(; i < SLOT_LAST; ++i)
{
if(Item* item = getInventoryItem((slots_t)i))
crit += item->getCriticalHitChance();
}
return crit;
}
[/cpp]
player.h
Under:
[cpp]
virtual int32_t getArmor() const;
[/cpp]
Paste:
[cpp]
virtual int32_t getCriticalHitChance() const;
[/cpp]
weapons.cpp
FIND ALL:
[cpp]
if(random_range(1, 100) < g_config.getNumber(ConfigManager::CRITICAL_HIT_CHANCE))
[/cpp]
Change to:
[cpp]
if(random_range(1, 100) < player->getCriticalHitChance() + g_config.getNumber(ConfigManager::CRITICAL_HIT_CHANCE))
[/cpp]