pokeapi icon indicating copy to clipboard operation
pokeapi copied to clipboard

Battle Mechanics

Open ninjaman0722 opened this issue 8 months ago • 17 comments

I want to add programmatic item held effects, use effects, and the conditions for those items effects to make the API better for battle simulators (what i am personally using it for).

i want to take a shot at implementing it myself if someone isn't already working on something similar.

ninjaman0722 avatar Mar 30 '26 14:03 ninjaman0722

if there is no specific preference for how this would be implemented this is how i would do it

there would be 4 new tables, item_effect_triggers, item_effect_types, item_effect_targets, and item_mechanics

1. item_effect_triggers

Defines the specific "event"

id: Primary Key.

identifier: The slug (e.g., on-hp-below-50, on-contact-taken, on-entry, on-move-selection, end-of-turn).

2. item_effect_types

Defines the mechanical change the item applies to the game state.

id: Primary Key.

identifier: The slug (e.g., hp-recovery, stat-multiplier, damage-reflection, status-cure, prevent-fainting).

3. item_effect_targets

Defines who the effect is applied to

id: Primary Key.

identifier: The slug (e.g., holder, attacker, field-ally, field-opponent).

4. item_mechanics

this connects items to their logic

item_id: FK to items.

version_group_id: FK to version_groups

trigger_id: FK to item_effect_triggers.

effect_type_id: FK to item_effect_types.

target_id: FK to item_effect_targets.

value: The raw numerical data (e.g., 25, 1.5, 0.166).

value_type: Defines how to interpret the value (e.g., percent-max, multiplier, flat-hp, probability).

im going off based on how i understand the API to work after looking over it for a bit, so i may be way off on how it would actually be implemented XD

ninjaman0722 avatar Mar 30 '26 15:03 ninjaman0722

Do you mean - endpoints that provide game-style functionality?

PokeAPI is a read-only API. We generated JSON files from a local database and then publish them statically. No part of the API is connected to a live application server. This is mostly so we can keep costs down and avoid computation costs for the billions of API hits we get...just to serve static files 😆

PokeAPI is not intended as a game-engine and is just read-only data. You're more than welcome to propose a separate project and use the expertise here of contributors (we know so so much about the games!) though.

phalt avatar Mar 31 '26 08:03 phalt

Do you mean - endpoints that provide game-style functionality?

PokeAPI is a read-only API. We generated JSON files from a local database and then publish them statically. No part of the API is connected to a live application server. This is mostly so we can keep costs down and avoid computation costs for the billions of API hits we get...just to serve static files 😆

PokeAPI is not intended as a game-engine and is just read-only data. You're more than welcome to propose a separate project and use the expertise here of contributors (we know so so much about the games!) though.

I think the information ninjaman is proposing is adding detailed information on items as endpoints that describe how they're triggered, what those triggers do, and who those triggers effect.

Right now that information is only really contained in item_prose.csv and item_flavor_text.csv which are text blurbs.

The system they've shown here seem to be similar to how the item_fling_effects would work currently. I think this info being added would only be beneficial to the APIs data.

The only suggestion I would have to this, @ninjaman0722, is to add a past value to "item_effect_triggers", "item_mechanics", and maybe "item_effect_targets".

Item triggers definitely change over time, plenty of berries have changed their trigger HP. For example, Figy Berry went from 1/2 HP trigger to 1/4 HP trigger in XY->SM.

Item effects have also changed a lot. I'm not sure if targets ever has, we'd have to look that up.

notblisy avatar Mar 31 '26 11:03 notblisy

The system they've shown here seem to be similar to how the item_fling_effects would work currently. I think this info being added would only be beneficial to the APIs data.

Far clearer to me now, thanks. I think if it is just read only data that the API supplies then it make sense to add.

phalt avatar Mar 31 '26 12:03 phalt

I think the information ninjaman is proposing is adding detailed information on items as endpoints that describe how they're triggered, what those triggers do, and who those triggers effect.

This is exactly right! with how pokeapi is now, i still have to manually write out essentially my own item database in my program for it to work. if i have to make a database with what all items do i might as well contribute to a project that does most of this for me anyway lol

The only suggestion I would have to this, @ninjaman0722, is to add a past value to "item_effect_triggers", "item_mechanics", and maybe "item_effect_targets".

great catch! thanks for pointing this out! i know bulbapedia does a great job at separating gen differences so i will have to look into it a bit.

if i am to do this i would definitely appreciate all the help i can get. ive never really looked at the back end of the api until yesterday and its a lot XD

ninjaman0722 avatar Mar 31 '26 13:03 ninjaman0722

Smogon also does a good job on item triggers/effects if you feel the need to crosscheck bulbapedia whenever, and I find it a bit easier to navigate.

notblisy avatar Mar 31 '26 14:03 notblisy

i have come up with an example of what it would look like for a sitrus berry (using bulbapidia as a reference)

item_id | version_group_id | trigger_id | effect_type_id | value | value_type | is_consumable 135 | 5 | 2 (hp-below-50) | 1 (hp-recovery) | 30 | flat | 1 135 | 5 | 10 (bag-use) | 1 (hp-recovery) | 30 | flat | 1 135 | 8 | 2 (hp-below-50) | 1 (hp-recovery) | 25 | percent-max | 1 135 | 8 | 10 (bag-use) | 1 (hp-recovery) | 25 | percent-max | 1 135 | 8 | 12 (forced-use) | 1 (hp-recovery) | 25 | percent-max | 1 (this covers Fling, Pluck, and Bug Bite,)

ninjaman0722 avatar Mar 31 '26 14:03 ninjaman0722

I'm not a maintainer, but this all makes sense to me.

I do think past values typically use generation ID rather than version groups, so a maintainer might need to weigh in on that. I actually prefer it, though, as it lets us account for changes more with granularity.

notblisy avatar Mar 31 '26 16:03 notblisy

Ok so already this is too simple, so instead of 4 we do 7 new tables

4 Vocabulary Tables

​##item_mechanic_triggers: Defines the timing/window of evaluation. ​Examples: on-hp-change, end-of-turn, on-damage-taken, on-manual-use (Bag), passive-constant.

​##item_mechanic_contexts: Defines where the item can be used. ​Examples: held-in-battle, bag-in-battle, overworld-menu.

​##item_mechanic_effect_types: The action performed. ​Examples: hp-recovery, stat-multiplier, status-apply, trigger-evolution, modify-duration.

​##item_mechanic_targets: Who the effect applies to. ​Examples: self, attacker, party-member, field-global.

3 Core Logic Tables

##item_mechanics: Links item_id to version_group_id, trigger_id, and context_id. ​Fields: id, item_id, version_group_id, trigger_id, context_id, is_removable.

​##item_mechanic_conditions: ​Requirements linked by mechanic_id. Supports logical grouping. ​Fields: mechanic_id, condition_type (e.g., hp-threshold, species-id), operator (==, <=, etc), value, group_id (for AND/OR logic).

##​item_mechanic_effects: The result of a successful trigger. ​Fields: mechanic_id, effect_type_id, target_id, value, value_type (flat, percent-max, multiplier), probability, is_consumed.

This should cover everything (hopefully)

ninjaman0722 avatar Mar 31 '26 18:03 ninjaman0722

i got an example here now for the sitrus berry (holy crap what a headache to get this to work XD)

    "mechanics": [
        {
            "version_group": 5,
            "trigger": {
                "name": "on-damage-taken",
                "url": "http://localhost/api/v2/item-mechanic-trigger/1/"
            },
            "context": {
                "name": "held-in-battle",
                "url": "http://localhost/api/v2/item-mechanic-context/1/"
            },
            "conditions": [
                {
                    "condition_type": "hp-threshold",
                    "operator": "<=",
                    "value": "50",
                    "group_id": 1
                }
            ],
            "effects": [
                {
                    "effect_type": {
                        "name": "hp-recovery",
                        "url": "http://localhost/api/v2/item-mechanic-effect-type/1/"
                    },
                    "target": {
                        "name": "self",
                        "url": "http://localhost/api/v2/item-mechanic-target/1/"
                    },
                    "value": "30.00",
                    "value_type": "flat",
                    "probability": 100,
                    "is_consumed": true
                }
            ]
        },
        {
            "version_group": 11,
            "trigger": {
                "name": "hp-change",
                "url": "http://localhost/api/v2/item-mechanic-trigger/2/"
            },
            "context": {
                "name": "held-in-battle",
                "url": "http://localhost/api/v2/item-mechanic-context/1/"
            },
            "conditions": [
                {
                    "condition_type": "hp-threshold",
                    "operator": "<=",
                    "value": "50",
                    "group_id": 1
                }
            ],
            "effects": [
                {
                    "effect_type": {
                        "name": "hp-recovery",
                        "url": "http://localhost/api/v2/item-mechanic-effect-type/1/"
                    },
                    "target": {
                        "name": "self",
                        "url": "http://localhost/api/v2/item-mechanic-target/1/"
                    },
                    "value": "25.00",
                    "value_type": "percent_max",
                    "probability": 100,
                    "is_consumed": true
                }
            ]
        }
    ]

ninjaman0722 avatar Apr 01 '26 11:04 ninjaman0722

"operator": "<=",

You should make this more generic with a constant - I know that for 90% of programming languages this will make sense, but there is a few esoteric languages out there that will use different syntax. A table of constants like LESS_THAN, EQUAL_TO etc would be far better.

phalt avatar Apr 01 '26 12:04 phalt

i believe i got a good setup in #1466

please look over it and let me know of anything that needs changed or whatever (also im not sure if i am using version_group_id correctly but its easy to change at this state XD)

~~also just noticed i wrote EQUALS and NOT_EQUAL instead of EQUAL_TO and NOT_EQUAL_TO like an absolute bafoon~~

ninjaman0722 avatar Apr 02 '26 00:04 ninjaman0722

im still working on this, here is a soul dew example (im in it too deep to make major changes now so it better be good XD)

"mechanics": [
    {
        "version_group": 5,
        "trigger": {
            "name": "passive",
            "url": "http://localhost/api/v2/item-mechanic-trigger/6/"
        },
        "context": {
            "name": "held-in-battle",
            "url": "http://localhost/api/v2/item-mechanic-context/1/"
        },
        "operation_order": 0,
        "conditions": [
            {
                "condition_type": {
                    "name": "holder-species",
                    "url": "http://localhost/api/v2/mechanic-condition/32/"
                },
                "logic_operator": {
                    "name": "EQUAL_TO",
                    "url": "http://localhost/api/v2/logic-operator/3/"
                },
                "value": "latios",
                "condition_group": 1
            },
            {
                "condition_type": {
                    "name": "holder-species",
                    "url": "http://localhost/api/v2/mechanic-condition/32/"
                },
                "logic_operator": {
                    "name": "EQUAL_TO",
                    "url": "http://localhost/api/v2/logic-operator/3/"
                },
                "value": "latias",
                "condition_group": 2
            }
        ],
        "effects": [
            {
                "effect_type": {
                    "name": "modify-special-attack-stat",
                    "url": "http://localhost/api/v2/item-mechanic-effect-type/29/"
                },
                "target": {
                    "name": "self",
                    "url": "http://localhost/api/v2/item-mechanic-target/1/"
                },
                "value": "1.5",
                "value_type": "multiplier",
                "probability": 100,
                "is_consumed": false
            },
            {
                "effect_type": {
                    "name": "modify-special-defence-stat",
                    "url": "http://localhost/api/v2/item-mechanic-effect-type/37/"
                },
                "target": {
                    "name": "self",
                    "url": "http://localhost/api/v2/item-mechanic-target/1/"
                },
                "value": "1.5",
                "value_type": "multiplier",
                "probability": 100,
                "is_consumed": false
            }
        ]
    },
    {
        "version_group": 17,
        "trigger": {
            "name": "passive",
            "url": "http://localhost/api/v2/item-mechanic-trigger/6/"
        },
        "context": {
            "name": "held-in-battle",
            "url": "http://localhost/api/v2/item-mechanic-context/1/"
        },
        "operation_order": 0,
        "conditions": [
            {
                "condition_type": {
                    "name": "holder-species",
                    "url": "http://localhost/api/v2/mechanic-condition/32/"
                },
                "logic_operator": {
                    "name": "EQUAL_TO",
                    "url": "http://localhost/api/v2/logic-operator/3/"
                },
                "value": "latios",
                "condition_group": 1
            },
            {
                "condition_type": {
                    "name": "outgoing-move-type",
                    "url": "http://localhost/api/v2/mechanic-condition/36/"
                },
                "logic_operator": {
                    "name": "EQUAL_TO",
                    "url": "http://localhost/api/v2/logic-operator/3/"
                },
                "value": "dragon",
                "condition_group": 1
            },
            {
                "condition_type": {
                    "name": "holder-species",
                    "url": "http://localhost/api/v2/mechanic-condition/32/"
                },
                "logic_operator": {
                    "name": "EQUAL_TO",
                    "url": "http://localhost/api/v2/logic-operator/3/"
                },
                "value": "latios",
                "condition_group": 2
            },
            {
                "condition_type": {
                    "name": "outgoing-move-type",
                    "url": "http://localhost/api/v2/mechanic-condition/36/"
                },
                "logic_operator": {
                    "name": "EQUAL_TO",
                    "url": "http://localhost/api/v2/logic-operator/3/"
                },
                "value": "psychic",
                "condition_group": 2
            },
            {
                "condition_type": {
                    "name": "holder-species",
                    "url": "http://localhost/api/v2/mechanic-condition/32/"
                },
                "logic_operator": {
                    "name": "EQUAL_TO",
                    "url": "http://localhost/api/v2/logic-operator/3/"
                },
                "value": "latias",
                "condition_group": 3
            },
            {
                "condition_type": {
                    "name": "outgoing-move-type",
                    "url": "http://localhost/api/v2/mechanic-condition/36/"
                },
                "logic_operator": {
                    "name": "EQUAL_TO",
                    "url": "http://localhost/api/v2/logic-operator/3/"
                },
                "value": "dragon",
                "condition_group": 3
            },
            {
                "condition_type": {
                    "name": "holder-species",
                    "url": "http://localhost/api/v2/mechanic-condition/32/"
                },
                "logic_operator": {
                    "name": "EQUAL_TO",
                    "url": "http://localhost/api/v2/logic-operator/3/"
                },
                "value": "latias",
                "condition_group": 4
            },
            {
                "condition_type": {
                    "name": "outgoing-move-type",
                    "url": "http://localhost/api/v2/mechanic-condition/36/"
                },
                "logic_operator": {
                    "name": "EQUAL_TO",
                    "url": "http://localhost/api/v2/logic-operator/3/"
                },
                "value": "psychic",
                "condition_group": 4
            }
        ],
        "effects": [
            {
                "effect_type": {
                    "name": "modify-damage",
                    "url": "http://localhost/api/v2/item-mechanic-effect-type/30/"
                },
                "target": {
                    "name": "self",
                    "url": "http://localhost/api/v2/item-mechanic-target/1/"
                },
                "value": "4915/4096",
                "value_type": "multiplier",
                "probability": 100,
                "is_consumed": false
            }
        ]
    }
]

ninjaman0722 avatar Apr 10 '26 17:04 ninjaman0722

Ok so with the solid setup I have created for item mechanics, its definitely possible to pretty much copy paste what I've done here and use it for other mechanics like abilities, weather, terrain, hazards, battle conditions, ailments, and some moves with specific mechanics

There are a lot of items so im going to be busy with this for a while, is there any maintainers (or really anyone) that would be willing to help me with this? Its a very simple system once you understand it, the only thing is its a bit tedious for me alone

ninjaman0722 avatar Apr 12 '26 02:04 ninjaman0722

I'm not a maintainer, but just someone who contributes data. That being said, I think that if you have a system to create endpoints that is functional, you can do a PR to put the system in place to the API and allow various contributors to add data with their own PRs at their leisure.

Like it's not a bad thing if you add this system and like 10 items in the PR. At least the API would have those 10 items.

notblisy avatar Apr 14 '26 13:04 notblisy

In that case what i already have in my PR should be a good foundation.

I do have to rename some files tho and maybe add the files for the abilities, weather, etc. that way those also have some kind of foundation

If I have time tonight ill do that and write up a change log and some documentation describing how to add mechanics to the API

ninjaman0722 avatar Apr 14 '26 22:04 ninjaman0722

Hi,can you please assign this task to me, @bhavyajain123 , @Praptijain10

Sagarachdeva911 avatar Sep 08 '26 16:09 Sagarachdeva911