Skip to content

acdemeg/FifaBot

Repository files navigation

FifaBot

GitHub Workflow Status (with event) GitHub release (with filter) GitHub Release Date - Published_At GitHub Repo stars GitHub code size in bytes GitHub repo size GitHub all releases

The repository contains an AI bot for the game Fifa19, but this approach will work for others as well FIFA series games. The bot analyzes the 2D layout of the field (it must be turned on) to evaluate the game situations, then makes decisions and triggers keyboard events to control the game process. The bot is written without using third-party libraries like OpenCV, only dependencies are included for testing and code generation.

Read this in other languages:

Eng

Rus

How to run

Startup requirements: java 17+
Run from the project directory: gradlew run
Extra options:
  1. -logging - all screenshots will be placed in USER_HOME/logs/TestImages, the log will be written to logs/fifa_bot.log
  2. -replayer - the application will be launched in Replayer mode
  3. -production - any logs will be disabled in the application
  4. -visual-log - will create a merged image with an overlaid log in USER_HOME/logs/TestImages. See ImageLogProducer

Note: The bot can be launched as an .exe file (self-extracting archive compiled using jlink), downloaded in the Releases tab

⚠️ Important: The bot is configured to work in 1920x1080 resolution with 100% scaling

List of modules

Collapse/Expand

Main - entry point for the application, creation and initialization of basic objects

ImageAnalysis - the class analyzes the image of the field diagram

GeometryUtils- class provides helper methods for 2D geometry

ImageUtils - the class provides helper methods for serialization/deserialization of game state to/from .dat file, getting image pixel color, methods for organizing replay analysis, etc.

GameInfo - data class that stores information about the current game situations - coordinates of players, ball, active player, etc.

GameHistory - class stores previous game states and previous decisions

GameAction - class responsible for creating a game actions (pass, hit, move, etc.)

DecisionMaker - the class is responsible for making the best decision on based on data from GameInfo

ActionProducer - the class is responsible for generating device events input. Currently only keyboard is available

ImageLogProducer - the class creates a full_game.png image displaying a visual log of the game. This is necessary for debugging purposes and analysis of the "replay" from USER_HOME/logs/TestImages

ColorsEnum - listing the color ranges for which objects are identified during image analysis

ControlsEnum - listing possible game actions and their groups

GameConstantsEnum - stores static information unchangeable over time (coordinates of the center of the field, penalty area, initial delay of actions, etc.)

GeomEnum - stores auxiliary enumerations for defining directions

How it works

Collapse/Expand

The main game loop starts and the bot takes a screenshot of the field as BufferedImage, for example⚽:

state_1

The bot analyzed the screenshot and received the following information:

playmates=[       opposites=[       activePlayer=[x=33,y=63] 
  [x=65,y=6]        [x=134,y=23]    ball=[x=36,y=64]
  [x=108,y=7]       [x=165,y=35]    isPlaymateBallPossession=true
  [x=117,y=49]      [x=165,y=61]    isShadingField=false
  [x=65,y=53]       [x=92,y=65]     isNobodyBallPossession=false
  [x=33,y=63]       [x=155,y=65]    playmateSide=LEFT_PLAYMATE_SIDE
  [x=140,y=67]      [x=214,y=73]
  [x=106,y=72]      [x=165,y=77]
  [x=82,y=94]       [x=141,y=81]
  [x=53,y=96]       [x=104,y=86]
  [x=65,y=134]      [x=165,y=103]
  [x=108,y=134]]    [x=138,y=118]]

Based on this information, a set of suitable actions was identified:

[
  [
     controls=[MOVE_UP, MOVE_RIGHT, ATTACK_SHORT_PASS_HEADER],
     actionTargetPlayer=[x=65,y=53]
  ],
  [
     controls=[MOVE_RIGHT],
     actionTargetPlayer=[x=33,y=63]
  ], 
  [
     controls=[ATTACK_PROTECT_BALL],
     actionTargetPlayer=[x=33,y=63]
  ]
]

From a set of actions, the bot selects the highest priority:

[
   controls=[MOVE_UP, MOVE_RIGHT, ATTACK_SHORT_PASS_HEADER],
   actionTargetPlayer=[x=65,y=53]
]

After executing the action, we get a new state⚽:

state_2

The bot performed the action correctly - gave a pass to the player who was in the upper right corner from active player in the previous screenshot, then the cycle repeats.

Example log and debugging

Collapse/Expand

This is what part of the log looks like with the described states📁:

log

For the purpose of debugging and analyzing the behavior of the bot, a class was written ImageLogProducer allowing you to link a text log with an image of the game state.

An example of a visual log for the states described above🔎:

full_game

⚠️ Important: The USER_HOME/logs/TestImages folder should contain quite limited the number of images corresponding to a certain game interval, otherwise for ImageLogProducer may not be enough RAM create too long final image

Managing and setting up the game client

The bot uses the following gameplay control scheme🔧:

Collapse/Expand

SPRINT - VK_E

MOVE_UP - VK_UP

MOVE_DOWN - VK_DOWN

MOVE_LEFT - VK_LEFT

MOVE_RIGHT - VK_RIGHT

MOVE_UP_PAD - VK_NUMPAD8

MOVE_DOWN_PAD - VK_NUMPAD2

MOVE_LEFT_PAD - VK_NUMPAD4

MOVE_RIGHT_PAD - VK_NUMPAD6

TACTICS - VK_J

MENTALITY_LEFT - VK_K

MENTALITY_RIGHT - VK_L

CUSTOM_TACTICS - VK_CAPS_LOCK

SWITCH_FROM_GK - VK_F

ATTACK_THROUGH_BALL - VK_A

ATTACK_LOB_PASS_CROSS_HEADER - VK_S

ATTACK_SHOOT_VOLLEY_HEADER - VK_W

ATTACK_SHORT_PASS_HEADER - VK_D

ATTACK_PLAYER_RUN_MODIFIER - VK_SPACE

ATTACK_FINESSE_SHOT_MODIFIER - VK_Q

ATTACK_PROTECT_BALL - VK_SHIFT

DEFENCE_RUSH_GK - VK_A

DEFENCE_SLIDING_TACKLE - VK_S

DEFENCE_TACKLE_PUSH_OR_PULL - VK_W

DEFENCE_CONTAIN - VK_D

DEFENCE_CHANGE_PLAYER - VK_SPACE

DEFENCE_TEAMMATE_CONTAIN - VK_Q

DEFENCE_JOCKEY - VK_SHIFT

CHIP_SHOT - VK_SPACE + VK_W

FINESSE_SHOT - VK_Q + VK_W

LOW_SHOT - VK_SPACE + VK_Q + VK_W

THREADED_THROUGH_PASS - VK_Q + VK_A

LOBBED_THROUGH_PASS - VK_SPACE + VK_A

DRIVEN_LOB_PASS_CROSS - VK_Q + VK_S

HIGH_LOB_CROSS - VK_SPACE + VK_S

LOW_CROSS - VK_S + VK_S

SCOOP_LOB - VK_SHIFT + VK_S

CANCEL - VK_SHIFT + VK_E

FLAIR_PASS - VK_SHIFT + VK_D

FLAIR_SHOOT - VK_SHIFT + VK_W

DRIVEN_GROUND_PASS - VK_Q + VK_D

RUNNING_JOCKEY - VK_SHIFT + VK_E

You need to create the appropriate configuration in the game client🎮:

Collapse/Expand

controls_eng_1

controls_eng_2

controls_eng_3

The 2D field layout must be turned on and the players' colors must be red - for you, blue - for the enemy, control device - Keyboard only, FullHD resolution, 100% scaling🖥️:

Collapse/Expand

settting_eng

Gameplay

Difficulty level - beginner

Collapse/Expand

Bot gameplay