1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Guest, PO has ceased our separate tiering and adopted Smogon tiers for SM. More information and updates here.

    Dismiss Notice

[Battle Scripting] Approaching battle AI

Discussion in 'Server and Client Scripting' started by Maribel Hearn, Feb 10, 2016.

  1. Maribel Hearn

    Maribel Hearn Scripter Developer Developer

    Joined:
    Sep 9, 2012
    Messages:
    33
    Likes Received:
    26
    PO Trainer Name:
    Maribel Hearn
    I've written a simple battle AI script, using the existing random move AI as a base. It improves on the AI by having knowledge of type advantages and related pitfalls. It still has the probability of 7/8 to use a move and the probability of 1/8 to switch every turn, but instead of having the moves and switches randomised, it uses its knowledge to select the best move and best switch-in for the current turn. This kind of AI strategy is a so-called hill climbing strategy; it follows one particular pattern in every turn to try to get as close as possible to its goal, and does not look back or ahead; it only knows the current situation in a turn.

    How it works
    When the AI uses a move (7/8 probability), it selects the move that has the highest type effectiveness on the foe's currently active Pokemon. The closer to the top left a move is, the higher its priority; if the move on the top left has the highest effectiveness but other moves are tied with it, the top left move will be chosen. This way, one can prioritise moves when giving the AI a team to battle with. For example, it is generally a good idea to give said priority to moves that gain STAB. On the calculation of type effectiveness, abilities that alter it are factored in; the AI will never just use an Electric type move on a Pokemon with Volt Absorb, for instance. The AI will already assume such an ability if the foe Pokemon can have it.

    When the AI decides to switch (1/8 probability), it switches into the Pokemon that the foe has the lowest type effectiveness against, i.e. a wall. Also for this functionality, it holds that the closer to the top left a Pokemon is, the higher its priority will be. When one of the Pokemon on the team of the AI is knocked out, however, it switches in the Pokemon that has the highest type effectiveness on the foe instead. To put it short, on a random switch the AI performs a defensive switch, while on a knockout it will do an offensive switch. The AI will also perform an offensive switch in the event that its current Pokemon cannot damage the foe because of immunity.

    Currently, this AI script does not support moves in the Other category, so the AI should be a given a team that has only damaging attacks.

    How to use
    Once you have loaded the AI battle script, all you have to do is writing down the name of the alt that you want to be AI in AIalt.txt, which is located in the client scripts folder. This can also be done by running the /alt <name> command within a battle window. There is also the /verbose command which toggles whether the AI is on verbose mode or not (i.e. it will print messages that show how it is checking effectiveness values), and an /eval <code> command to run JavaScript code within a battle window, for testing or debugging purposes. Because of the extremely small size of the commands I have not bothered writing a proper command parser for them.

    Further ideas
    Of course, this kind of AI is simple and easily predictable outside of its probability to switch, but it's a start. An approach to battle AI. To implement Other moves, each kind of Other move would need at least one exact rule to allow the AI to determine whether it will use that move or not. When is Thunder Wave useful? When is Substitute useful? When is boosting useful? When is Toxic useful? etc. There would have to be thresholds for these moves to let the AI decide on using them, which would not be easy to find the right values for.

    To make the AI much smarter, however, prediction would have to be implemented. The idea on how to do this is simple: one simply gives the AI access to the usage stats, and it will know which set a Pokemon will be running, with some percentage of certainty. This way, the AI would be able to try to predict moves and counter them. On top of this, when a foe Pokemon uses a move, the AI could remember that it has that move and use that knowledge to its advantage. Then again, this kind of AI that uses usage stats could be countered by using an uncommon moveset, or even a moveset that is never used at all.

    Implementing all this would eat a whole lot of time (especially all of the rules or thresholds for every single kind of Other move, because each one has a different use) and may or may not be very complicated, but it is definitely possible. There are more approaches to AI, but these are just a few ideas. Please note that this does not mean that I will try to actually implement them! I'm merely explaining possibilities! :P

    ===============================================================================
    Links:
    My hill climbing AI script
     
    Last edited: Aug 10, 2017
    Binix, Kite, Sam Holmes and 7 others like this.
  2. Edna

    Edna Chasing the Dragon Forum Moderator Forum Moderator

    Joined:
    Feb 20, 2013
    Messages:
    1,746
    Likes Received:
    1,809
    PO Trainer Name:
    Edna
    Seems sick, good luck for this!
     
  3. Pumone

    Pumone Member

    Joined:
    Aug 30, 2015
    Messages:
    60
    Likes Received:
    13
    I'm very happy to see your fantastic work to finally create a good AI bot to challenge.

    Your instructions are very clear, but when you say "all you have to do is writing down the name of the alt that you want to be AI in AIalt.txt" I get lost.

    So I copied your code and pasted it in Plugins->Script Window->Client Scripts, but after this I can't get how to go on, do I have to edit the script and replace AIalt.txt with (for example) Pumone.txt?

    Also when I'm in the battle window and I write /alt Pumone it doen't make Pumone an AI bot.

    Final point, Plugins->Script Window->Battle Scripts need to be empty? No code here?

    Again, thank you so much for your big effort to make this real =)
     
  4. Maribel Hearn

    Maribel Hearn Scripter Developer Developer

    Joined:
    Sep 9, 2012
    Messages:
    33
    Likes Received:
    26
    PO Trainer Name:
    Maribel Hearn
    I thought it was clear enough that it was a battle script. Oh well. :P You're supposed to paste the script into the Battle Scripts tab, not Client Scripts. AIalt.txt is created when the script is run (in a battle) but you could also create the file manually. Then, you can either manually write the AI alt inside that file and save it, or use the /alt command that I wrote for it (can be used within a battle window).

    Sorry for the lack of clarity on this!
     
    Pumone likes this.
  5. Pumone

    Pumone Member

    Joined:
    Aug 30, 2015
    Messages:
    60
    Likes Received:
    13
    Ooh ok now it works well!! Thank you so much, I stay alert for future updates =)
     
  6. Maribel Hearn

    Maribel Hearn Scripter Developer Developer

    Joined:
    Sep 9, 2012
    Messages:
    33
    Likes Received:
    26
    PO Trainer Name:
    Maribel Hearn
    I just updated the AI to make it use an offensive boosting move when the current Pokemon knows one and the foe's type effectiveness on it is lower than 1, so it's safe to boost. I've also made it properly use the moves Fake Out and Last Resort; more moves with a special effect like these two may follow in the future.
     
    Pumone and Nightfall Alicorn like this.
  7. Pumone

    Pumone Member

    Joined:
    Aug 30, 2015
    Messages:
    60
    Likes Received:
    13
    How to fix the bug that makes the AI bot disconnect and forfeit the battle?

    Also does AI bot support mega evolutions?
     
    Last edited: Feb 15, 2016
  8. Pumone

    Pumone Member

    Joined:
    Aug 30, 2015
    Messages:
    60
    Likes Received:
    13
    Also, with this script can you make AI vs AI battle and watch the battle as a spectator?
     
  9. Nightfall Alicorn

    Nightfall Alicorn Left Pokémon Online, most likely not coming back.

    Joined:
    Oct 15, 2013
    Messages:
    491
    Likes Received:
    171
    PO Trainer Name:
    Nightmare Moon
    If you don't specify what alts to ignore they should battle each other yes.
     
    Pumone likes this.
  10. Pumone

    Pumone Member

    Joined:
    Aug 30, 2015
    Messages:
    60
    Likes Received:
    13
    Ok so I find a very easy way, I can't script, but by reading the entire code I just changed "false" to "true" in this:

    // If the player name is not the AI alt, do not use AI
    if (client.ownName() != AIalt) {
    useAI = true;
    }

    That's it!
     
    Nightfall Alicorn likes this.
  11. Maribel Hearn

    Maribel Hearn Scripter Developer Developer

    Joined:
    Sep 9, 2012
    Messages:
    33
    Likes Received:
    26
    PO Trainer Name:
    Maribel Hearn
    Yeah, you could simply get rid of that entire if statement to remove the name check, which means that every alt you use will be AI. I'll still want to make it possible to put multiple alts into the text file though.
     
    Pumone likes this.
  12. Disaster Area

    Disaster Area Badged Deucer

    Joined:
    May 10, 2013
    Messages:
    615
    Likes Received:
    226
    PO Trainer Name:
    Disaster Area
    Kite and sulcata like this.
  13. Maribel Hearn

    Maribel Hearn Scripter Developer Developer

    Joined:
    Sep 9, 2012
    Messages:
    33
    Likes Received:
    26
    PO Trainer Name:
    Maribel Hearn
    Oh, that sounds really cool. I am familiar with Minimax, I thought of the possibility of using it for Pokemon and what it would be like, actually. Without using prediction it would be incredibly slow, though, but since you have 5 minutes to take a turn on PO, it shouldn't matter. Given the RNG involved in many moves, however, it would be very complicated to take into consideration all of the chance factors on pretty much every possible future game state.
     
  14. Disaster Area

    Disaster Area Badged Deucer

    Joined:
    May 10, 2013
    Messages:
    615
    Likes Received:
    226
    PO Trainer Name:
    Disaster Area
    Yeah

    the original programmer i think left his code on there somewhere, and he's not too hard to catch on IRC as david stone in the #smogon channel. Anyway it's probably a wise move to work on what he's done so far since it sounds like he managed to do really well with what he did there, it'd take you an awful while to catch up (even if you try a different approach probably)
     
  15. Pumone

    Pumone Member

    Joined:
    Aug 30, 2015
    Messages:
    60
    Likes Received:
    13
    Sorry for repost on this old thread, but I think it can be important and funny for some people, me first! Can someone tell me if there are any news about AI battling?
     
  16. cyclops

    cyclops New Member

    Joined:
    Aug 9, 2019
    Messages:
    1
    Likes Received:
    0
    Hi! I know is been a long time but i really need help on this code i play battle bot vs bot but each pokemon can just use one moves i don't understand