Skip to content

Commit

Permalink
Add item attribute to affect hit chance
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Apr 20, 2024
1 parent 5a3bc31 commit 4df1aa3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions data/conf/equipment.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<element name="damagetype" type="Q1:attribute" minOccurs="0" maxOccurs="unbounded"/>
<element name="statusattack" type="Q1:attribute" minOccurs="0" maxOccurs="unbounded"/>
<element name="statusresist" type="Q1:statusresist" minOccurs="0" maxOccurs="unbounded"/>
<element name="accuracy_bonus" type="Q1:attribute" minOccurs="0" maxOccurs="1"/>
</sequence>
</extension>
</complexContent>
Expand Down
9 changes: 8 additions & 1 deletion src/games/stendhal/server/entity/item/Item.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* $Id$ */
/***************************************************************************
* (C) Copyright 2003-2023 - Marauroa *
* (C) Copyright 2003-2024 - Marauroa *
***************************************************************************
***************************************************************************
* *
Expand Down Expand Up @@ -170,6 +170,9 @@ public static void generateRPClass() {
// Some items have ranged attack values
entity.addAttribute("ratk", Type.SHORT, Definition.HIDDEN);

// alters weapon accuracy by a percentage
entity.addAttribute("accuracy_bonus", Type.FLOAT, Definition.HIDDEN);

// Some items indicate how often you can attack.
entity.addAttribute("rate", Type.SHORT, Definition.HIDDEN);

Expand Down Expand Up @@ -871,6 +874,10 @@ public String describe() {
stats.append("%");
}
}
if (has("accuracy_bonus")) {
stats.append(" ACCURACY-BONUS: ");
stats.append(((getDouble("accuracy_bonus") * 100) + "%").replaceFirst("\\.[0]+%$", "%"));
}

if (has("min_level")) {
stats.append(" MIN-LEVEL: ");
Expand Down
20 changes: 18 additions & 2 deletions src/games/stendhal/server/entity/player/Player.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* $Id$ */
/***************************************************************************
* (C) Copyright 2003-2023 - Arianne *
* (C) Copyright 2003-2024 - Arianne *
***************************************************************************
***************************************************************************
* *
Expand Down Expand Up @@ -3212,6 +3212,21 @@ public int getMaxSlotSize(String slot) {
return Integer.parseInt(values[0]) * Integer.parseInt(values[1]);
}

/**
* Retrieves amount to apply to player ATK to adjust hit chance.
*
* @return
* Value of player ATK multiplied by percentage defined in item "accuracy_bonus" attribute.
*/
private int getAccuracyBonus() {
double accBonus = 0;
for (final Item equip: getAllEquipment()) {
accBonus += equip.has("accuracy_bonus") ? equip.getDouble("accuracy_bonus") : 0;
}
// base on raw uncapped value
return (int) Math.round(this.atk * accBonus);
}

/**
* This handicap increases chance that a player can hit an enemy to
* make the game feel more fair. Hit chance is based on raw atk stat,
Expand All @@ -3224,7 +3239,8 @@ public int getMaxSlotSize(String slot) {
protected int calculateRiskForCanHit(final int roll, final int defenderDEF,
final int attackerATK) {
// use 30 as multiple for players instead of 20
return ((int) Math.round(HIT_CHANCE_MULTIPLIER * 1.5)) * attackerATK - roll * defenderDEF;
return ((int) Math.round(HIT_CHANCE_MULTIPLIER * 1.5)) * (attackerATK + this.getAccuracyBonus())
- roll * defenderDEF;
}

/**
Expand Down

0 comments on commit 4df1aa3

Please sign in to comment.