# Damage Formula

When selecting a potion item type or skill, you can input a **damage formula**. However, you can also use this field to execute other actions (e.g., awarding items, skills, or gold). Here's an explanation of how this works:

***

1. **`a` (attacker)**: Represents the entity initiating the action (e.g., player, NPC).
2. **`b` (defender)**: Represents the entity receiving the action (e.g., player, enemy).
3. **Both `a` and `b` are `ScriptUnit` objects.**
4. For **items**, `b` does not exist, and you can use `level` to get the item's level.
5. For **states**, only `a` and `b` exist.
6. For **skills**, `level` refers to the skill's level.

***

## **Variable**

<table><thead><tr><th width="280">Variable</th><th>Description</th></tr></thead><tbody><tr><td><strong>a.id</strong></td><td>a playerID</td></tr><tr><td><strong>a.type</strong></td><td>a Type (0=player, 1=event, 2=monster)</td></tr><tr><td><strong>a.name</strong></td><td>a Name</td></tr><tr><td><strong>a.x</strong></td><td>a position X</td></tr><tr><td><strong>a.y</strong></td><td>a position Y</td></tr><tr><td><strong>a.atk</strong></td><td>a Attack</td></tr><tr><td><strong>a.def</strong></td><td>a Defense</td></tr><tr><td><strong>a.magicAtk</strong></td><td>a MagicAttack</td></tr><tr><td><strong>a.magicDef</strong></td><td>a MagicDefense</td></tr><tr><td><strong>a.agi</strong></td><td> A gility</td></tr><tr><td><strong>a.luk</strong></td><td>a Lucky </td></tr><tr><td><strong>a.maxHP</strong></td><td>a maxHP</td></tr><tr><td><strong>a.maxMP</strong></td><td>a maxMP</td></tr><tr><td><strong>a.hp</strong></td><td>a currentHP</td></tr><tr><td><strong>a.mp</strong></td><td>a currentMP</td></tr></tbody></table>

## **Function**

<table><thead><tr><th width="333">함수</th><th>설명</th></tr></thead><tbody><tr><td><strong>a.SpawnAt(x, y)</strong></td><td>Move a to the coordinates x, y.</td></tr><tr><td><strong>a.SpawnAtField(mapID, x, y)</strong></td><td>Move a to the coordinates x, y on the map with ID mapID.</td></tr><tr><td><strong>a.RespawnAt(x, y)</strong></td><td>Teleport a to the coordinates x, y.</td></tr><tr><td><strong>a.SetVar(id, value)</strong></td><td>Change the variable with ID id of a to value.</td></tr><tr><td><strong>a.GetVar(id)</strong></td><td>Retrieve the variable with ID id from a.</td></tr><tr><td><strong>a.AddSkill(id, level)</strong></td><td>Add a skill with ID id and level level to a.</td></tr><tr><td><strong>a.AddItem(id, count)</strong></td><td>Add count items with ID id to a.</td></tr><tr><td><strong>a.AddGameMoney(amount)</strong></td><td>Add gold to a.</td></tr><tr><td><strong>b.RemoveSkill(id)</strong></td><td>Remove a skill with ID id from b.</td></tr><tr><td><strong>b.RemoveItem(id, count)</strong></td><td>Consume count items with ID id from b.</td></tr><tr><td><strong>b.UseGameMoney(amount)</strong></td><td>Deduct amount gold from b.</td></tr><tr><td><strong>b.StartGlobalEvent(id)</strong></td><td>Call the global event with ID id.</td></tr><tr><td><strong>b.AddBuff(id)</strong></td><td>Applies the state with the specified ID to b.</td></tr><tr><td><strong>b.RemoveBuff(id)</strong></td><td>Removes the state with the specified ID from b.</td></tr></tbody></table>

#### **Practical Examples**

**Damage Calculation Examples:**

1. **Basic Damage Formula**: Damage = Attacker's attack - Defender's defense

   ```css
   a.atk - b.def
   ```
2. **Random Damage**: Generates random damage between 10 and 50

   ```scss
   rand(10, 50)
   ```

**Custom Actions:**

3. **Steal Gold from Defender**:

   ```css
   b.UseGameMoney(10)
   ```
4. **Remove Items or Gold from Attacker**:

   ```css
   a.RemoveItem(1, 10) or a.UseGameMoney(10)
   ```

**Conditional Formulas:**

5. **Critical Damage Calculation**: Apply `(a.atk * 2)` for critical hits; otherwise, `(a.atk)`

   ```css
   critical and (a.atk * 2) or (a.atk)
   ```

**Skill-Specific Damage:**

6. **Damage Based on Skill Level**: Damage = `a.magicAtk - b.magicDef + level + 1`

   ```css
   a.magicAtk - b.magicDef + level + 1
   ```

**Item-Specific Actions:**

7. **Grant a Skill with an Item**: When using an item, grant `Skill ID 0` at `Level 1` to `a`

   ```css
   a.AddSkill(0, 1)
   ```

**State Effects:**

8. **Continuous Damage from States**: Apply 10 damage over time (minimum of 2 seconds)

   ```
   10
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.punkland.io/punkland-studio/start/damageformula.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
