summaryrefslogtreecommitdiff
path: root/src/contest_ai.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contest_ai.c')
-rwxr-xr-xsrc/contest_ai.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/contest_ai.c b/src/contest_ai.c
new file mode 100755
index 000000000..9dfa6c2b9
--- /dev/null
+++ b/src/contest_ai.c
@@ -0,0 +1,113 @@
+#include "global.h"
+#include "contest.h"
+#include "random.h"
+#include "ewram.h"
+
+enum
+{
+ ContestAI_SettingUp,
+ ContestAI_Processing,
+ ContestAI_FinishedProcessing,
+ ContestAI_DoNotProcess
+};
+
+extern u8 *gAIScriptPtr;
+extern u8 *gContestAIs[];
+
+typedef void (* ContestAICmdFunc)(void);
+
+extern const ContestAICmdFunc sContestAICmdTable[]; // TODO: Move table to C file
+
+void ContestAI_DoAIProcessing(void);
+
+void ContestAI_ResetAI(u8 var)
+{
+ int i;
+ memset(eContestAI, 0, sizeof(struct ContestAIInfo));
+
+ for(i = 0; i < 4; i++)
+ eContestAI->unk5[i] = 100;
+
+ eContestAI->unk41 = var;
+ eContestAI->unk40 = 0;
+ eContestAI->flags = gContestMons[eContestAI->unk41].flags;
+}
+
+u8 ContestAI_GetActionToUse(void)
+{
+ while(eContestAI->flags != 0)
+ {
+ if(eContestAI->flags & 1)
+ {
+ eContestAI->aiState = 0;
+ ContestAI_DoAIProcessing();
+ }
+ eContestAI->flags >>= 1;
+ eContestAI->unk10++;
+ eContestAI->unk4 = 0;
+ }
+
+ while (1)
+ {
+ u8 rval = Random() & 3;
+ u8 r2 = eContestAI->unk5[rval];
+ int i;
+ for (i = 0; i < 4; i++)
+ {
+ if (r2 < eContestAI->unk5[i])
+ break;
+ }
+ if (i == 4)
+ return rval;
+ }
+}
+
+void ContestAI_DoAIProcessing(void)
+{
+ while(eContestAI->aiState != ContestAI_FinishedProcessing)
+ {
+ switch(eContestAI->aiState)
+ {
+ case ContestAI_DoNotProcess:
+ break;
+ case ContestAI_SettingUp:
+ gAIScriptPtr = gContestAIs[eContestAI->unk10];
+
+ if(gContestMons[eContestAI->unk41].moves[eContestAI->unk4] == 0)
+ eContestAI->unk2 = 0; // don't process a move that doesn't exist.
+ else
+ eContestAI->unk2 = gContestMons[eContestAI->unk41].moves[eContestAI->unk4];
+ eContestAI->aiState++;
+ break;
+ case ContestAI_Processing:
+ if(eContestAI->unk2 != 0)
+ sContestAICmdTable[*gAIScriptPtr](); // run the command.
+ else
+ {
+ eContestAI->unk5[eContestAI->unk4] = 0; // don't consider a move that doesn't exist.
+ eContestAI->aiAction |= 1;
+ }
+ if(eContestAI->aiAction & 1)
+ {
+ eContestAI->unk4++;
+ if(eContestAI->unk4 < 4)
+ eContestAI->aiState = 0;
+ else
+ eContestAI->aiState++;
+ eContestAI->aiAction &= 0xFE; // TODO: Define action flags
+ }
+ break;
+ }
+ }
+}
+
+u8 sub_8128A7C(u8 var)
+{
+ int i;
+
+ for(i = 0; i < 4; i++)
+ if(shared192D0.unk0[i] == var)
+ break;
+
+ return i;
+}