Skip to content

feat: attr element and damage calculing#3518

Draft
LeoTKBR wants to merge 35 commits into
opentibiabr:mainfrom
LeoTKBR:attr-element-and-damage-calculing
Draft

feat: attr element and damage calculing#3518
LeoTKBR wants to merge 35 commits into
opentibiabr:mainfrom
LeoTKBR:attr-element-and-damage-calculing

Conversation

@LeoTKBR

@LeoTKBR LeoTKBR commented May 8, 2025

Copy link
Copy Markdown
Contributor

This modification aims to fix the elemental damage, being able to modify the elemental value, working with magic, the damage calculation was also redone, before the elemental or physical damage was too high, leaving the other at zero, so a weapon with atk: 25 and elemental: 100, 1000 simply made the physical not do any damage, this was changed now even if it is 10000 it will do physical damage even if it is little.

Fire sword, elemental damage 100:
image

Fire sword, elemental damage 1000:
image

Fire sword, elemental damage 10000:
image

Of course the physical damage was very low however and compared to the exorbitant value of the elemental damage there still needs to be a balance between the values ​​but one will not cancel the other leaving the value 0

Summary by CodeRabbit

  • New Features

    • Items now support a dedicated element attribute system for improved element damage mechanics.
    • Enhanced element damage calculations and handling across melee, ranged, and wand weapons.
    • Improved accuracy of element damage display in item descriptions and combat information.
  • Bug Fixes

    • Fixed element damage not being properly recognized when parsing item attributes.

✏️ Tip: You can customize this high-level summary in your review settings.

@majestyotbr majestyotbr changed the title new: attr element and damage calculing feat: attr element and damage calculing May 13, 2025
@MadsonPaulo

Copy link
Copy Markdown

Testado e funcionando 100%.

Falta apenas incluir em data\scripts\talkactions\god\attributes.lua
["element"] = { isActive = true, targetFunction = function(item, target) return item:setAttribute(ITEM_ATTRIBUTE_ELEMENT, target) end, },

@LeoTKBR

LeoTKBR commented Jun 1, 2025

Copy link
Copy Markdown
Contributor Author

Testado e funcionando 100%.

Falta apenas incluir em data\scripts\talkactions\god\attributes.lua ["element"] = { isActive = true, targetFunction = function(item, target) return item:setAttribute(ITEM_ATTRIBUTE_ELEMENT, target) end, },

Na verdade não já tem attr por padrão no código por isso não adicionei já vem no comando attr o element a menos que você tenha um datapack antigo o que eu usei de base para fazer esse PR já tinha incluido

@LeoTKBR LeoTKBR closed this Jun 1, 2025
@LeoTKBR LeoTKBR reopened this Jun 1, 2025
@sonarqubecloud

sonarqubecloud Bot commented Jun 1, 2025

Copy link
Copy Markdown

@andersonfaaria andersonfaaria requested a review from dudantas June 5, 2025 19:52
@github-actions

github-actions Bot commented Jul 6, 2025

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 45 days with no activity.

@github-actions github-actions Bot added the Stale No activity label Jul 6, 2025
@coderabbitai

coderabbitai Bot commented Jan 30, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

These changes introduce an ELEMENT item attribute and widen element damage representation from int16_t to int32_t across items, weapons, and combat calculations. The ELEMENT attribute is added as an enumerated value, integrated into item serialization/deserialization, and used in damage calculations and descriptions as a stored alternative to the default element type value.

Changes

Cohort / File(s) Summary
Item Attribute System
src/enums/item_attribute.hpp, src/items/functions/item/attribute.hpp, src/items/items_definitions.hpp, src/utils/tools.cpp
Added ELEMENT attribute enum value (37) to ItemAttribute_t, marked as an integer attribute in the switch statement, defined as ATTR_ELEMENT (45) in AttrTypes_t, and registered in stringToItemAttribute conversion.
Item Type and Data
src/items/items.hpp, src/items/item.hpp, src/items/item.cpp
Added element field to ItemType, introduced getElementDamage() method to Item, and implemented ELEMENT attribute serialization/deserialization with fallback to ItemType's element field. Updated item description logic to use the new element value in damage displays.
Weapon Type System
src/items/weapons/weapons.hpp, src/items/weapons/weapons.cpp
Widened getElementDamageValue() return type from int16_t to int32_t across base Weapon and all subclasses (Melee, Distance, Wand). Changed elementDamage member from uint16_t to uint32_t. Updated calculateSkillFormula() signature to use int32_t for elementAttack parameter. Refactored getCombatDamage to handle wand damage specially and use element values consistently.
Combat Calculations
src/creatures/combat/combat.cpp
Changed elementAttack local variable from int16_t to int32_t in ValueCallback::getMinMaxValues for wider range in elemental damage calculations.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 Hops with glee and whiskers bright,
Elements now stored just right!
Wider numbers, int32 flame,
Damage calculations — same old game!
But stronger now from root to tip,

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'feat: attr element and damage calculing' partially addresses the changeset but contains a spelling error ('calculing' instead of 'calculating') and is somewhat vague. While it references the main features (element attribute and damage calculation), the phrasing could be clearer and more professional. Consider revising to 'feat: add element attribute and improve damage calculation' to be more specific, grammatically correct, and better reflect the damage calculation refactoring that prevents elemental values from nullifying physical damage.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/items/weapons/weapons.cpp`:
- Around line 288-295: The current damage split computes ratios using
(physicalAttack + elementalAttack) which can be zero; calculate int32_t denom =
physicalAttack + elementalAttack and only perform the division when denom > 0,
otherwise set physicalDamage and elementalDamage to 0 (or another safe default);
update the blocks around physicalDamage/elementalDamage calculations (variables
physicalDamage, elementalDamage, totalDamage, physicalAttack, elementalAttack)
to use denom to avoid division-by-zero.
🧹 Nitpick comments (5)
src/items/weapons/weapons.hpp (2)

282-282: Redundant virtual keyword on override methods.

The virtual keyword is redundant when override is already specified. While this is a minor style issue, modern C++ guidelines recommend using only override for clarity.

♻️ Optional cleanup
-	virtual int32_t getElementDamageValue() const override;
+	int32_t getElementDamageValue() const override;

Apply similar changes to Lines 306 and 329.

Also applies to: 306-306, 329-329


286-286: Type consistency: member is uint32_t but getter returns int32_t.

The elementDamage member is declared as uint32_t, but getElementDamageValue() returns int32_t. This implicit conversion is generally safe for values within int32_t range, but for consistency and to avoid potential issues with very large values, consider aligning the types.

Also applies to: 312-312

src/items/weapons/weapons.cpp (3)

267-267: Non-English comment should be translated.

The comment "Aplica o dano para varinhas" (Portuguese for "Apply damage for wands") should be in English for codebase consistency.

♻️ Suggested translation
-			// Aplica o dano para varinhas
+			// Apply damage for wands

260-273: Code duplication: wand damage handling duplicated between getCombatDamage and internalUseWeapon.

The wand-specific damage logic (setting primary damage from weapon damage, clearing secondary damage) is now duplicated in both getCombatDamage (Lines 181-187) and internalUseWeapon (Lines 260-273). Consider extracting this to a helper or consolidating the paths to reduce maintenance burden.


475-481: Variable shadowing: item reassigned within nested scope.

At Line 475, item is reassigned via player->getWeapon(true), which shadows the item variable declared at Line 460. This can cause confusion since the two item variables serve different purposes (weapon slot vs ammo). Consider using a distinct variable name for clarity.

♻️ Suggested rename for clarity
 	if (elementType != COMBAT_NONE) {
 		shouldCalculateSecondaryDamage = true;
-		item = player->getWeapon(true);
-		if (item) {
-			int32_t element = item->getElementDamage();
+		const auto &elementItem = player->getWeapon(true);
+		if (elementItem) {
+			int32_t element = elementItem->getElementDamage();
 			elementAttack = (element > 0) ? element : getElementDamageValue();
 		} else {
 			elementAttack = getElementDamageValue();
 		}

Comment on lines +288 to +295
int32_t physicalDamage = 0;
if (physicalAttack > 0) {
physicalDamage = static_cast<int32_t>(totalDamage * (static_cast<float>(physicalAttack) / (physicalAttack + elementalAttack)));
}
int32_t elementalDamage = 0;
if (elementalAttack > 0) {
elementalDamage = static_cast<int32_t>(totalDamage * (static_cast<float>(elementalAttack) / (physicalAttack + elementalAttack)));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Potential division by zero when both physicalAttack and elementalAttack are zero.

The division (physicalAttack + elementalAttack) at Lines 290 and 294 can be zero if both values are zero, leading to undefined behavior. Although this scenario may be rare for valid weapons, defensive coding is recommended.

🐛 Proposed fix to add division guard
 			const int32_t physicalAttack = item->getAttack();
 			const int32_t element = item->getElementDamage();
 			const int32_t elementalAttack = (element > 0) ? element : getElementDamageValue();
+			const int32_t totalAttackValue = physicalAttack + elementalAttack;
 			int32_t physicalDamage = 0;
-			if (physicalAttack > 0) {
-				physicalDamage = static_cast<int32_t>(totalDamage * (static_cast<float>(physicalAttack) / (physicalAttack + elementalAttack)));
+			if (physicalAttack > 0 && totalAttackValue > 0) {
+				physicalDamage = static_cast<int32_t>(totalDamage * (static_cast<float>(physicalAttack) / totalAttackValue));
 			}
 			int32_t elementalDamage = 0;
-			if (elementalAttack > 0) {
-				elementalDamage = static_cast<int32_t>(totalDamage * (static_cast<float>(elementalAttack) / (physicalAttack + elementalAttack)));
+			if (elementalAttack > 0 && totalAttackValue > 0) {
+				elementalDamage = static_cast<int32_t>(totalDamage * (static_cast<float>(elementalAttack) / totalAttackValue));
 			}
🤖 Prompt for AI Agents
In `@src/items/weapons/weapons.cpp` around lines 288 - 295, The current damage
split computes ratios using (physicalAttack + elementalAttack) which can be
zero; calculate int32_t denom = physicalAttack + elementalAttack and only
perform the division when denom > 0, otherwise set physicalDamage and
elementalDamage to 0 (or another safe default); update the blocks around
physicalDamage/elementalDamage calculations (variables physicalDamage,
elementalDamage, totalDamage, physicalAttack, elementalAttack) to use denom to
avoid division-by-zero.

Ellectroma added a commit to Ellectroma/canary_fork that referenced this pull request Apr 4, 2026
Resolved conflicts:
- item_attribute.hpp / items_definitions.hpp: ELEMENT renumbered to 38/46 to avoid collision with MANTRA (37/45) added in HEAD
- item.cpp: kept both ATTR_MANTRA and ATTR_ELEMENT serialize/deserialize cases
- weapons.cpp: took PR's wand handling (direct damage, secondary=COMBAT_NONE); non-wand else block with item->getElementDamage() was already cleanly merged

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Ellectroma added a commit to Ellectroma/canary_fork that referenced this pull request Apr 4, 2026
…nd new imbuement scroll)

Resolved conflict in WeaponMelee::getWeaponDamage: kept HEAD's item->getElementDamage()
check (from PR opentibiabr#3518) over PR opentibiabr#3845's plain getElementDamageValue(); dropped unused
combinedAttack variable introduced by PR opentibiabr#3845.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@dudantas dudantas marked this pull request as draft May 7, 2026 18:04
@github-actions github-actions Bot removed the Stale No activity label Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants