# Revscriptsys

### Script Structure

> **Constructor**:
>
> * *CreatureEvent(eventName)*
> * > **eventName**: equivalent to the `name` tag in XML.
>
> **Events**:
>
> * onLogin(player)
> * onLogout(player)
> * onThink(creature, interval)
> * onPrepareDeath(creature, killer)
> * onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
> * onKill(creature, target)
> * onAdvance(player, skill, oldLevel, newLevel)
> * onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
> * onManaChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
> * onTextEdit(player, item, text)
> * onModalWindow(player, modalWindowId, buttonId, choiceId)
> * onExtendedOpcode(player, opcode, buffer)
>
> **Methods**:
>
> * creatureEvent:register(): Finalizes the creatureEvent's definition and registers it.
> * creatureEvent:type(eventType):
> * > **eventType**: Equivalent to the `type` tag in XML.

### Examples

```lua
local loginEvent = CreatureEvent("newLoginEvent")
loginEvent:type("login")

function loginEvent.onLogin(player)
    print(player:getName() .. " has logged in!")
    return true
end

loginEvent:register()
```

```lua
local function damageMitigator(primaryDamage, secondaryDamage, mul)
    primaryDamage = primaryDamage - (primaryDamage * mul)
    secondaryDamage = secondaryDamage - (secondaryDamage * mul)
    return primaryDamage, secondaryDamage
end

local healthChange = CreatureEvent("healthChangeMitigate")
healthChange:type("healthchange")

function healthChange.onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType)
    -- Reduce health damage by 50%
    primaryDamage, secondaryDamage = damageMitigator(primaryDamage, secondaryDamage, 0.5)
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

healthChange:register()

local manaChange = CreatureEvent("manaChangeMitigate")
manaChange:type("manachange")

function manaChange.onManaChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType)
    -- Reduce mana damage by 75%
    primaryDamage, secondaryDamage = damageMitigator(primaryDamage, secondaryDamage, 0.75)
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

manaChange:register()

local loginEvent = CreatureEvent("mitigatorRegistration")
loginEvent:type("login")

function loginEvent.onLogin(player)
    -- Register both health/mana change mitigators to every single player who logs in
    player:registerEvent("healthChangeMitigate")
    player:registerEvent("manaChangeMitigate")
    return true
end

loginEvent:register()
```


---

# 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://stigmax.gitbook.io/tfs-guide/interface-creaturescripts/cscripts-revscripts.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.
