BasicBot 0.2.2b, updated save/load code. Will auto-load/save on open/close, once a bug has been fixed. Need help fixing the WM_INITDIALOG event (-> mailinglist).

This commit is contained in:
qfox 2006-09-01 02:57:35 +00:00
parent 091d0b225d
commit a2f48e180f
4 changed files with 101 additions and 61 deletions

View File

@ -37,7 +37,7 @@
// go up each time something is released to the public, so you'll // go up each time something is released to the public, so you'll
// know your version is the latest or whatever. Version will be // know your version is the latest or whatever. Version will be
// put in the project as well. // put in the project as well.
static char BBversion[] = "0.2.2"; static char BBversion[] = "0.2.2b";
static HWND hwndBasicBot = 0; // qfox: GUI handle static HWND hwndBasicBot = 0; // qfox: GUI handle
static bool BotRunning = false; // qfox: Is the bot computing or not? I think... static bool BotRunning = false; // qfox: Is the bot computing or not? I think...
@ -45,8 +45,8 @@ static bool BotRunning = false; // qfox: Is the bot computing or not? I think.
static uint32 rand1=1, rand2=0x1337EA75, rand3=0x0BADF00D; static uint32 rand1=1, rand2=0x1337EA75, rand3=0x0BADF00D;
#define BOT_MAXCODE 1024 // qfox: max length of code, 1kb may be too little... #define BOT_MAXCODE 1024 // qfox: max length of code, 1kb may be too little...
#define BOT_FORMULAS 21 #define BOT_FORMULAS 27 // qfox: number of code fields in the array
static char Formula[BOT_FORMULAS][BOT_MAXCODE]; // qfox: These hold the 21 formula's entered in the GUI: static char Formula[BOT_FORMULAS][BOT_MAXCODE]; // qfox: These hold the BOT_FORMULAS formula's entered in the GUI:
static int CODE_1_A = 0, static int CODE_1_A = 0,
CODE_1_B = 1, CODE_1_B = 1,
CODE_1_SELECT = 2, CODE_1_SELECT = 2,
@ -67,9 +67,13 @@ static int CODE_1_A = 0,
CODE_MAX = 17, CODE_MAX = 17,
CODE_TIE1 = 18, CODE_TIE1 = 18,
CODE_TIE2 = 19, CODE_TIE2 = 19,
CODE_TIE3 = 20; CODE_TIE3 = 20,
static char ExtraCode[BOT_MAXCODE]; // qfox: Contains the extra code, executed after dealing CODE_X = 21,
// with input. CODE_Y = 22,
CODE_Z = 23,
CODE_P = 24,
CODE_Q = 25,
CODE_EXTRA = 26;
#define BOT_MAXCOUNTERS 256 #define BOT_MAXCOUNTERS 256
static int BotCounter[BOT_MAXCOUNTERS]; // qfox: The counters. All ints, maybe change this to static int BotCounter[BOT_MAXCOUNTERS]; // qfox: The counters. All ints, maybe change this to
@ -1285,7 +1289,7 @@ void UpdateBasicBot()
} }
// luke: Run extra commands // luke: Run extra commands
EvaluateFormula(ExtraCode); EvaluateFormula(Formula[CODE_EXTRA]);
// qfox: remember the buttons pressed in this frame // qfox: remember the buttons pressed in this frame
LastButtonPressed = BotInput[1]; LastButtonPressed = BotInput[1];
} }
@ -1355,7 +1359,7 @@ void CheckCode()
EvaluateFormula(Formula[i]); EvaluateFormula(Formula[i]);
} }
debugS("Error at extra code"); debugS("Error at extra code");
EvaluateFormula(ExtraCode); EvaluateFormula(Formula[CODE_EXTRA]);
if (!EvaluateError) if (!EvaluateError)
{ {
debugS("Code syntax ok!"); debugS("Code syntax ok!");
@ -1383,8 +1387,6 @@ static void SaveBasicBot()
ofn.lpstrInitialDir=BasicBotDir; ofn.lpstrInitialDir=BasicBotDir;
if(GetSaveFileName(&ofn)) if(GetSaveFileName(&ofn))
{ {
int i,j;
//Save the directory //Save the directory
if(ofn.nFileOffset < 1024) if(ofn.nFileOffset < 1024)
{ {
@ -1394,7 +1396,8 @@ static void SaveBasicBot()
BasicBotDir[ofn.nFileOffset]=0; BasicBotDir[ofn.nFileOffset]=0;
} }
//quick get length of nameo int i;
//luke: quick get length of nameo
for(i=0;i<2048;i++) for(i=0;i<2048;i++)
{ {
if(nameo[i] == 0) if(nameo[i] == 0)
@ -1403,7 +1406,7 @@ static void SaveBasicBot()
} }
} }
//add .bot if nameo doesn't have it //luke: add .bot if fn doesn't have it
if((i < 4 || nameo[i-4] != '.') && i < 2040) if((i < 4 || nameo[i-4] != '.') && i < 2040)
{ {
nameo[i] = '.'; nameo[i] = '.';
@ -1412,8 +1415,18 @@ static void SaveBasicBot()
nameo[i+3] = 't'; nameo[i+3] = 't';
nameo[i+4] = 0; nameo[i+4] = 0;
} }
SaveBasicBotFile(nameo);
FILE *fp=FCEUD_UTF8fopen(nameo,"wb"); }
}
/**
* qfox: save to supplied filename
*/
static void SaveBasicBotFile(char fn[])
{
FILE *fp=FCEUD_UTF8fopen(fn,"wb");
if (fp != NULL)
{
int i,j;
fputc('b',fp); fputc('b',fp);
fputc('o',fp); fputc('o',fp);
fputc('t',fp); fputc('t',fp);
@ -1425,14 +1438,9 @@ static void SaveBasicBot()
fputc(Formula[i][j],fp); fputc(Formula[i][j],fp);
} }
} }
for(j=0;j<BOT_MAXCODE;j++)
{
fputc(ExtraCode[j],fp);
}
fclose(fp); fclose(fp);
} }
} }
/** /**
* luke: Loads a previously saved file * luke: Loads a previously saved file
* qfox: code seems good * qfox: code seems good
@ -1457,8 +1465,6 @@ static void LoadBasicBot()
if(GetOpenFileName(&ofn)) if(GetOpenFileName(&ofn))
{ {
int i,j;
// luke: Save the directory // luke: Save the directory
if(ofn.nFileOffset < 1024) if(ofn.nFileOffset < 1024)
{ {
@ -1468,7 +1474,27 @@ static void LoadBasicBot()
BasicBotDir[ofn.nFileOffset]=0; BasicBotDir[ofn.nFileOffset]=0;
} }
FILE *fp=FCEUD_UTF8fopen(nameo,"rb"); // qfox: now actually load the file
LoadBasicBotFile(nameo);
}
}
/**
* qfox: load a file by supplying the filename
*/
static void LoadBasicBotFile(char fn[])
{
int i,j;
// qfox: open the file
FILE *fp=FCEUD_UTF8fopen(fn,"rb");
if (fp != NULL)
{
// qfox: if the first four chars of the file are not
// "bot0", it's not a valid botfile. only check
// done at the moment.
if(fgetc(fp) != 'b' if(fgetc(fp) != 'b'
|| fgetc(fp) != 'o' || fgetc(fp) != 'o'
|| fgetc(fp) != 't' || fgetc(fp) != 't'
@ -1478,25 +1504,23 @@ static void LoadBasicBot()
return; return;
} }
// qfox: read the codes for the buttons for player one and
// two, and the end/result/ties.
for(i=0;i<BOT_FORMULAS;i++) for(i=0;i<BOT_FORMULAS;i++)
{ {
// qfox: read BOT_MAXCODE bytes per codefield
for(j=0;j<BOT_MAXCODE;j++) for(j=0;j<BOT_MAXCODE;j++)
{ {
Formula[i][j] = fgetc(fp); Formula[i][j] = fgetc(fp);
// qfox: if the char is 128, its 0 (?)
if(Formula[i][j] & 128) if(Formula[i][j] & 128)
{ {
// qfox: figure out wtf this is
Formula[i][j] = 0; Formula[i][j] = 0;
} }
} }
} }
for(j=0;j<BOT_MAXCODE;j++) // qfox: release lock and resources
{
ExtraCode[j] = fgetc(fp);
if(ExtraCode[j] & 128)
{
ExtraCode[j] = 0;
}
}
fclose(fp); fclose(fp);
} }
} }
@ -1538,21 +1562,25 @@ static void FromGUI()
if (hwndBasicBot) if (hwndBasicBot)
{ {
int i; int i;
// qfox: buttons
for(i = 0; i < 8; i++) for(i = 0; i < 8; i++)
{ {
// qfox: id 1000-1008 are for the buttons // qfox: id 1000-1008 are for the buttons
GetDlgItemText(hwndBasicBot,1000+i,Formula[i + (EditPlayerOne?0:8)], BOT_MAXCODE); GetDlgItemText(hwndBasicBot,1000+i,Formula[i + (EditPlayerOne?0:8)], BOT_MAXCODE);
} }
for(i = 0; i < 4; i++) GetDlgItemText(hwndBasicBot, GUI_BOT_END, Formula[CODE_STOP], BOT_MAXCODE);
{ GetDlgItemText(hwndBasicBot, GUI_BOT_MAX, Formula[CODE_MAX], BOT_MAXCODE);
// qfox: id 1010-1014 are for goals GetDlgItemText(hwndBasicBot, GUI_BOT_TIE1, Formula[CODE_TIE1], BOT_MAXCODE);
GetDlgItemText(hwndBasicBot,1010+i,Formula[CODE_MAX+i], BOT_MAXCODE); GetDlgItemText(hwndBasicBot, GUI_BOT_TIE2, Formula[CODE_TIE2], BOT_MAXCODE);
} GetDlgItemText(hwndBasicBot, GUI_BOT_TIE3, Formula[CODE_TIE3], BOT_MAXCODE);
// qfox: id 1009 is for "end when" GetDlgItemText(hwndBasicBot, GUI_BOT_EXTRA, Formula[CODE_EXTRA], BOT_MAXCODE);
GetDlgItemText(hwndBasicBot, 1009, Formula[CODE_STOP], BOT_MAXCODE); GetDlgItemText(hwndBasicBot, GUI_BOT_X, Formula[CODE_X], BOT_MAXCODE);
GetDlgItemText(hwndBasicBot, GUI_BOT_Y, Formula[CODE_Y], BOT_MAXCODE);
GetDlgItemText(hwndBasicBot, GUI_BOT_EXTRA, ExtraCode, BOT_MAXCODE); GetDlgItemText(hwndBasicBot, GUI_BOT_Z, Formula[CODE_Z], BOT_MAXCODE);
GetDlgItemText(hwndBasicBot, GUI_BOT_P, Formula[CODE_P], BOT_MAXCODE);
GetDlgItemText(hwndBasicBot, GUI_BOT_Q, Formula[CODE_Q], BOT_MAXCODE);
// qfox: function, because we call this from a different button as well
UpdateStatics(); UpdateStatics();
} }
UpdateFromGUI = false; UpdateFromGUI = false;
@ -1563,27 +1591,27 @@ static void FromGUI()
**/ **/
static void ToGUI() static void ToGUI()
{ {
if (hwndBasicBot && Formula && ExtraCode) if (hwndBasicBot && Formula && Formula[CODE_EXTRA])
{ {
int i; int i;
// qfox: buttons
for(i = 0; i < 8; i++) for(i = 0; i < 8; i++)
{ {
SetDlgItemText(hwndBasicBot,1000+i,Formula[i + (EditPlayerOne?0:8)]); SetDlgItemText(hwndBasicBot,1000+i,Formula[i + (EditPlayerOne?0:8)]);
} }
for(i = 0; i < 4; i++) SetDlgItemText(hwndBasicBot, GUI_BOT_END, Formula[CODE_STOP]);
{ SetDlgItemText(hwndBasicBot, GUI_BOT_MAX, Formula[CODE_MAX]);
SetDlgItemText(hwndBasicBot,1010+i,Formula[CODE_MAX+i]); SetDlgItemText(hwndBasicBot, GUI_BOT_TIE1, Formula[CODE_TIE1]);
} SetDlgItemText(hwndBasicBot, GUI_BOT_TIE2, Formula[CODE_TIE2]);
// qfox: end when field SetDlgItemText(hwndBasicBot, GUI_BOT_TIE3, Formula[CODE_TIE3]);
SetDlgItemText(hwndBasicBot, 1009, Formula[CODE_STOP]); SetDlgItemText(hwndBasicBot, GUI_BOT_EXTRA, Formula[CODE_EXTRA]);
SetDlgItemText(hwndBasicBot, GUI_BOT_EXTRA, ExtraCode); // qfox: static variables
SetDlgItemText(hwndBasicBot, GUI_BOT_X, Formula[CODE_X]);
SetDlgItemInt(hwndBasicBot, GUI_BOT_X, X, TRUE); SetDlgItemText(hwndBasicBot, GUI_BOT_Y, Formula[CODE_Y]);
SetDlgItemInt(hwndBasicBot, GUI_BOT_Y, Y, TRUE); SetDlgItemText(hwndBasicBot, GUI_BOT_Z, Formula[CODE_Z]);
SetDlgItemInt(hwndBasicBot, GUI_BOT_Z, Z, TRUE); SetDlgItemText(hwndBasicBot, GUI_BOT_P, Formula[CODE_P]);
SetDlgItemInt(hwndBasicBot, GUI_BOT_P, P, TRUE); SetDlgItemText(hwndBasicBot, GUI_BOT_Q, Formula[CODE_Q]);
SetDlgItemInt(hwndBasicBot, GUI_BOT_Q, Q, TRUE);
} }
} }
@ -1595,13 +1623,11 @@ static void ToGUI()
static void InitVars() static void InitVars()
{ {
memset(Formula, 0, BOT_FORMULAS*BOT_MAXCODE); memset(Formula, 0, BOT_FORMULAS*BOT_MAXCODE);
memset(ExtraCode, 0, BOT_MAXCODE);
int i; int i;
for(i = 0; i < BOT_FORMULAS; i++) for(i = 0; i < BOT_FORMULAS; i++)
{ {
Formula[i][0] = '0'; Formula[i][0] = '0';
} }
ExtraCode[0] = '0';
Formula[CODE_STOP][0] = 'f'; Formula[CODE_STOP][0] = 'f';
Formula[CODE_STOP][1] = 'r'; Formula[CODE_STOP][1] = 'r';
Formula[CODE_STOP][2] = 'a'; Formula[CODE_STOP][2] = 'a';
@ -1639,12 +1665,15 @@ static BOOL CALLBACK BasicBotCallB(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARA
// todo: load last used settings -> will have to figure out the load/save part first. // todo: load last used settings -> will have to figure out the load/save part first.
// todo: why the hell is this event not firing? // todo: why the hell is this event not firing?
CheckDlgButton(hwndBasicBot, GUI_BOT_P1, 0); // select the player1 radiobutton CheckDlgButton(hwndBasicBot, GUI_BOT_P1, 0); // select the player1 radiobutton
LoadBasicBotFile("default.bot");
ToGUI();
// test echo, to botmode toggle button, doesnt work
SetDlgItemText(hwndBasicBot,GUI_BOT_BOTMODE,"hello");
break; break;
case WM_CLOSE: case WM_CLOSE:
case WM_QUIT: case WM_QUIT:
// todo: save last used settings
// qfox: before closing, it quickly copies the contents to the variables... for some reason.
FromGUI(); FromGUI();
SaveBasicBotFile("default.bot");
DestroyWindow(hwndBasicBot); DestroyWindow(hwndBasicBot);
hwndBasicBot=0; hwndBasicBot=0;
break; break;
@ -1717,9 +1746,6 @@ static BOOL CALLBACK BasicBotCallB(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARA
} }
break; break;
case GUI_BOT_CLOSE: case GUI_BOT_CLOSE:
FromGUI(); // qfox: might tear this out later. seems a little pointless.
DestroyWindow(hwndBasicBot);
hwndBasicBot=0;
break; break;
case GUI_BOT_BOTMODE: case GUI_BOT_BOTMODE:
// qfox: toggle external input mode // qfox: toggle external input mode
@ -1740,6 +1766,11 @@ static BOOL CALLBACK BasicBotCallB(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARA
// qfox: the small update button above the static variables ("U"), // qfox: the small update button above the static variables ("U"),
// only update the statics. // only update the statics.
UpdateStatics(); UpdateStatics();
break;
case GUI_BOT_TEST:
MessageBox(hwndBasicBot, "Showing", "Hrm", MB_OK);
SetDlgItemText(hwndBasicBot,GUI_BOT_BOTMODE,"hello");
break;
default: default:
break; break;
} }
@ -1781,7 +1812,6 @@ void CreateBasicBot()
hwndBasicBot=CreateDialog(fceu_hInstance,"BASICBOT",NULL,BasicBotCallB); hwndBasicBot=CreateDialog(fceu_hInstance,"BASICBOT",NULL,BasicBotCallB);
EditPlayerOne = true; EditPlayerOne = true;
BotRunning = false; BotRunning = false;
ToGUI();
} }
} }

View File

@ -13,4 +13,8 @@
static void FromGUI(); static void FromGUI();
static void UpdateStatics(); static void UpdateStatics();
void UpdateExternalButton(); void UpdateExternalButton();
static void LoadBasicBotFile(char fn[]);
static void LoadBasicBot();
static void SaveBasicBotFile(char fn[]);
static void SaveBasicBot();
#endif // _BASICBOT_H_ #endif // _BASICBOT_H_

Binary file not shown.

View File

@ -5,10 +5,15 @@
#define GUI_BOT_CLOSE 1 #define GUI_BOT_CLOSE 1
#define IDI_ICON1 101 #define IDI_ICON1 101
#define IDI_ICON2 102 #define IDI_ICON2 102
#define GUI_BOT_END 1009
#define GUI_BOT_X 1010 #define GUI_BOT_X 1010
#define GUI_BOT_TIE1 1011
#define GUI_BOT_TIE2 1012
#define GUI_BOT_TIE3 1013
#define GUI_BOT_SAVE 1014 #define GUI_BOT_SAVE 1014
#define GUI_BOT_LOAD 1015 #define GUI_BOT_LOAD 1015
#define GUI_BOT_RUN 1016 #define GUI_BOT_RUN 1016
#define GUI_BOT_MAX 1017
#define GUI_BOT_Y 1018 #define GUI_BOT_Y 1018
#define GUI_BOT_KEYS 1019 #define GUI_BOT_KEYS 1019
#define GUI_BOT_EXTRA 1020 #define GUI_BOT_EXTRA 1020
@ -19,6 +24,7 @@
#define GUI_BOT_BOTMODE 1025 #define GUI_BOT_BOTMODE 1025
#define GUI_BOT_P 1026 #define GUI_BOT_P 1026
#define GUI_BOT_Q 1027 #define GUI_BOT_Q 1027
#define GUI_BOT_TEST 1028
#define GUI_BOT_P1 1035 #define GUI_BOT_P1 1035
#define GUI_BOT_CLEAR 1036 #define GUI_BOT_CLEAR 1036
#define GUI_BOT_ATTEMPTS 1037 #define GUI_BOT_ATTEMPTS 1037