Batting AI Logic

# Overview

### Pitch processes

1. If the batter hasn't decided to swing yet, then check if the batter should swing.
2. If the batter hasn't decided to swing, then calculate if and where the batter should move within the batter's box.
	1. Calculate the desired location of the batter.
		1. Pre-pitch movement.
		2. During wind-up, move to initial position based on guessing where the pitch will be.
		3. Just after the ball is thrown, determine if the batter will track the ball perfectly or slightly wrong.
		4. For the rest of the pitch, move towards the pitch location (or possibly with a slight offset from the last step).
	2. Move the batter towards the desired location.

# Between Pitch Processes

## Inning importance

There is a concept of inning importance that's used many times throughout the AI Batting algorithm.

## Swing Type

The game determines the type of swing the batter will use using the following algorithm.

### Step 1: Check if special strategy situation (Recalculated for each at bat)

The game has a normal hitting strategy, and a special hitting strategy that it uses for each at bat. There are 3 types of special situations that can trigger the special strategy, and if they apply, they each have probabilities of the batter selecting the special strategy. The batter must have at least 1 team star in their bank to select the special strategy for this at bat

Below are the 3 situations and their probability tables. If multiple situations apply, the higher tables take precedence.

Situation 1: Last inning, 2 out, score tied
 Weak
 Not-Weak

### Step 2: Apply hitting strategy (recalculated each pitch)

#### Normal Strategy

If the probabilities from step 1 fail, then the batter will be in the "Normal strategy" state. In this state, there are two probabilities evaluated. The first probability determines if the potential swing is a slap or a charge swing. If charge is chosen (and something unknown is true), then another probability will be ran to set a still-unknown indicator relating to the swing.

Normal strategy, charge swing probability
 50%

Unknown Indicator probabilities. Only is ran if the result of the other table was true
(This might be the probability to take a pitch, but it's unconfirmed)

#### Special Strategy

If the probability from step 1 succeeds, then the swing distribution depends on the ball/strike count. There are three swing probabilities: potential star swing, potential normal swing, and no swing.

If the game selects a normal swing, then it will use the probabilities from the "Normal Strategy" section above to choose between slap vs charge.

Star Swing Chance/Normal Swing/Auto 100% Take Pitch Probabilities
 Regular
 Strong
 Powerful

# Each Pitch Processes

## Batter Left/Right Position in Batters Box

The decision on where the batter will stand in the box has several different phases.

The pitch locations are classified into 5 categories: off the plate-inside, inside part of home plate, middle of home plate, outside part of home plate, off the plate-outside.

### Phase 1: Pre-Pitch - Random?

This phase hasn't been found yet, but it's assumed it's just random box movement right now.

### Phase 2: During Pitcher Wind-Up - Initial Pitch Location Guess

This phase is ran one 1 frame during the pitcher's wind-up. The frame it's calculated on is 30 frames before the pitch is released for Weak difficulty, 18 frames before for Regular difficulty, 9 frames on Strong, and 6 frames on Powerful.

The initial guess of where the pitch will be is made during this phase. The batter guesses one of the pitch zones, but if it guesses one of the zones off of the plate, it will move the guess to the nearest zone (eg if the zone guessed is off the plate on the right side, then it will choose the right side of home-plate zone). The guess is either based off of the last pitch location, or it's random. When the ball position is guessed, the batter gets a range of where they should move to.

 Balanced
 Power
 Speed
 Technique

### Phase 3: Shortly after ball is thrown - Calculate if batter will track the ball poorly

This calculation happens between 10 and 16 frames after the pitch is thrown. This phase determines how well the batter will track the ball for the rest of the pitch. The results are either that the batter will try to move to the ball's actual position, or the game will calculate an offset from the ball's true position that the batter will try to get to instead.

There are two steps to the probabilities. The RNG has to work for both steps in order for the batter to track the ball poorly.

Step 1 - The probability to move on to step 2. If the probabilities in the table fails, then the batter will track the ball well.

Step 2 - Given that the probability in step 1 was successful, these are the probabilities that the batter will track the ball **poorly**.

If the batter is determined to track this pitch poorly, here are how badly the batter will be off by. The offset will be to the right 1/3 of the time and to the left 2/3 of the time.

 Regular
 Strong
 Powerful

### Phase 4 - Remaining time the ball is in the air - react to location

Every frame, the batter's desired location will update to be the ball's location (plus a potential offset from the previous step).

### Batter movement speed

When the batter isn't at the desired location, then the game will move the batter at a speed of 0.02 on the first frame, and 0.03 on the remaining frames until the batter reaches the desired location. For context, the width of the plate is seemingly 0.8 (but unconfirmed).

## Frame to Swing

The general swing timing decision is based on calculating the desired frame the batter wants to swing at, and then making adjustments based on various factors.

### Bowser/Bowser Jr. Star Pitch - Timing adjustment for the loop

The following probabilities determine if the batter correctly accounts for the frames added by the ball doing its loop: Weak 25%, Regular 40%, Strong 60%, Powerful 70%.

### Timing adjustments based on pitch types

#### Regular pitches, and star pitches not mentioned below

*Pitch type guess*

Firstly, the batter makes a guess at the type of pitch that's going to be thrown. The guess is determined as follows:

Step 1: Chance for the guess to be the same as the previous pitch

Step 2: If the batter didn't guess the same type as the previous pitch, then it chooses a pitch type with the following probabilities, based on the batter's character class:

 Power
 Speed
 Technique

*Early or late swing timings*

If the pitch is a curveball, or the batter guessed the correct type of pitch, then the batter won't receive an adjustment to swing significantly earlier or later (although they still might receive a small adjustment in the next step).

If the pitch is a charge pitch or a change-up, and the batter didn't guess it correctly, then these are the probabilities that the batter will swing **early** for charge pitches and **late** for change-ups (it's unclear why the devs programmed it this way, since the early/late seems backwards from how a human batter would react).

 Power
 Speed
 Technique

*Apply adjustment*

If the batter wasn't fooled into swing early or late in the last step, there is still a small offset to the swing timing.

If the batter was fooled into swinging early or late, then the game picks a random frame from the following ranges to adjust the swing timings

 Regular
 Strong
 Powerful

#### **Mario/Luigi Star Pitch**

The game picks an additional offset based on the following probabilities.

#### **Peach/Daisy/Yoshi/Birdo Star Pitch**

The game randomly picks an offset from the following ranges.

* Weak: -15 frames (late) to 10 frames (early)
* Regular: -13 to 9
* Strong: -10 to 8
* Powerful: -8 to 7 for Peach and Daisy, -5 to 5 for Yoshi and Birdo

## Control stick input

The AI chooses up/down and left/right stick input based on RNG.

### Left/Right input

 Power
 Speed
 Technique

### Up/Down Input

**If less than 2 outs, and runner on 3rd, and not weak difficulty.**

Class=Power
 Strong
 Powerful

Class<>Power
 Strong
 Powerful

**Else (If 2 outs, or no runners on 3rd, or weak difficulty).**

 Power
 Speed
 Technique

# Uncategorized sections

Previous discovered mechanics that need to be reordered within this page.

## Bunting

For bunting to be considered, a certain unknown indicator needs to be set. Whenever this happens, the game will check if the game state is in a certain situation, and if so, it will make an RNG determination.

**Situation**: Score within 2 runs, inning importance > 1, less than 2 outs, and either runner on 1B, or runners on 1B and 2B.

Bunting Probabilities
 Weak
 Regular
 Strong
 Powerful

## Stealing

The game will see if there's a steal opportunity if there's less than 3 balls in the count, and there's a runner on either 1st or 2nd without a runner occupying the next base.

Step 1: Get Stealing Initial Probability
 Weak
 Regular
 Strong
 Powerful

Step 2: Multiply stealing probability by a multiplier based on the **batter's** class

Step 3: If the potential stealing runner has speed > 50, then let x be the number of 10's the speed is over 50 (eg for 70, x = 2). Multiply the stealing probability by 1.x (eg for 70, 1.2x).

Step 4: Get a random number. If the probability comparison is successful, then the runner will steal.

### Perfect Steal

If a steal is triggered, the chances of it being a perfect steal are: