-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.c
81 lines (70 loc) · 2.12 KB
/
ai.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Created by mitchell on 4/8/21.
//
#include "ai.h"
#include "game.h"
#include "gba.h"
#include <stdlib.h>
difficulty difficultyOptions[5] = {
{"Trivial", 0, 100, 5},
{"Easy", 30, 15, 5},
{"Normal", 30, 10, 4},
{"Hard", 20, 8, 3},
{"Expert", 0, 0, 1}
};
static int targetFound = 0;
static int reactionAccumulated = 0;
static int target = 0;
static int entropicStartTime = 0;
static difficulty *currDifficulty;
void resetAI(difficulty *diff) {
entropicStartTime = 0;
currDifficulty = diff;
}
int controlAI(struct body paddle, struct body pong, int pongCharged) {
// int rowDiff = paddle.transform.row + paddle.transform.height / 2 - (pong.transform.row + pong.transform.height / 2);
// if (rowDiff > 0) return -1;
// else if (rowDiff < 0) return 1;
// else return 0;
if (pong.velocity.x < 0 || pong.transform.col > paddle.transform.col + paddle.transform.width) {
targetFound = 0;
reactionAccumulated = 0;
return 0;
}
if (!targetFound) {
reactionAccumulated++;
if (reactionAccumulated >= currDifficulty->reaction) {
findTarget(paddle, pong, pongCharged);
targetFound = 1;
}
}
if (entropicStartTime < AI_ENTROPY_FRAMES) {
entropicStartTime++;
return 1;
}
if (targetFound) {
int rowDiff = paddle.transform.row + paddle.transform.height / 2 - target;
if (abs(rowDiff) < TO_PHYS_COORD(currDifficulty->targetMargin)) return 0;
else if (rowDiff > 0) {
return -1;
}
else if (rowDiff < 0) {
return 1;
}
else return 0;
}
return 0;
}
void findTarget(struct body paddle, struct body pong, int pongCharged) {
int targetRow = pong.transform.row + (paddle.transform.col - pong.transform.col) * pong.velocity.y / (pong.velocity.x * (pongCharged ? 5 : 2) / 2);
int maxRow = TO_PHYS_COORD(HEIGHT) - pong.transform.height;
while (targetRow < 0 || targetRow > maxRow) {
if (targetRow < 0) {
targetRow = -targetRow;
} else {
targetRow = 2 * maxRow - targetRow;
}
}
targetRow += randint(-TO_PHYS_COORD(currDifficulty->error), TO_PHYS_COORD(currDifficulty->error));
target = targetRow;
}