# Battle System

#### Battle System

The battle function leverages character attributes (HP, attack, defense, evasion, and magic defense) to determine the winner in a combat scenario. Below is a detailed explanation of each step in the battle process and how each attribute influences the outcome.

**1. Initializing Health Points (HP)**

Each character's initial HP is set based on their stats before the battle begins, representing their endurance in combat.

**2. Attack Rounds**

The battle is structured in rounds where both characters engage in attacks simultaneously until the HP of one character reaches zero.

**3. Calculating Attack Success**

Each attack has a chance of success influenced by the opponent's evasion attribute, now scaled from 0 to 1000 for enhanced balance:

* **Player’s Attack**: The attack succeeds if a randomly generated value (between 0 and 1000) is higher than the opponent's evasion value.
* **Opponent’s Attack**: Similarly, the opponent's attack succeeds if the random value is higher than the player’s evasion.

**4. Calculating Damage**

When an attack is successful, the damage inflicted on the opponent is calculated based on the attacker’s strength and the defender’s resilience:

* **Physical Damage**: Determined by subtracting the defender's defense (def) from the attacker's physical attack (atk). If the defense is equal to or greater than the attack, the damage will be zero.
* **Magic Damage**: Calculated as the difference between the attacker’s magic attack (matk) and the defender’s magic defense (mdef).
* **Total Damage**: The sum of both physical and magic damage inflicted upon the opponent.

**5. HP Reduction**

After calculating the damage, each character’s HP is reduced accordingly, based on the total damage received in that round.

**6. Battle Event Logs**

During each round, detailed logs capture the results of each attack, including whether it hit or missed, and the damage inflicted.

***

#### Battle Example: Warrior vs. Wizard

Let's consider an example where the player selects a Warrior to face a Wizard. Here are their attributes:

**Warrior**

* **HP**: 100
* **Attack (atk)**: 10
* **Defense (def)**: 25
* **Evasion**: 100 (low evasion)

**Wizard**

* **HP**: 80
* **Magic Attack (matk)**: 30
* **Magic Defense (mdef)**: 10
* **Evasion**: 200 (moderate evasion)

**Round 1**

**Warrior’s Attack**

* **Chance to Hit**: With the Wizard's evasion set at 200, the Warrior needs a random value between 0 and 1000 greater than 200 to hit.
* **Damage Dealt**: If the Warrior's attack is successful, the damage would be `atk (10) - mdef (10)`, resulting in zero physical and magic damage due to the Wizard's magic defense.

**Wizard’s Attack**

* **Chance to Hit**: With the Warrior's evasion at 100, the Wizard needs a random value above 100 to succeed.
* **Damage Dealt**: If the Wizard’s attack hits, the damage would be calculated as `matk (30) - def (25)`, resulting in 5 damage inflicted on the Warrior.

**Subsequent Rounds**

The battle continues similarly until one character’s HP is fully depleted.

**Example Outcome**

In this scenario, the Wizard's strong magic attack might give them an advantage, while the Warrior's high defense allows them to absorb more damage. The winner is the first to reduce the opponent's HP to zero.
