summaryrefslogtreecommitdiff
path: root/KPFormSimpleCode.cpp
diff options
context:
space:
mode:
authorEric Anderson <ejona86@gmail.com>2009-05-01 17:14:46 -0500
committerEric Anderson <ejona86@gmail.com>2009-05-01 17:14:46 -0500
commit663d9336324110648ffdea7c7092a004353419b5 (patch)
treed35eccc38a36859bc317796a2058497ed9d8c382 /KPFormSimpleCode.cpp
downloadkpl-663d9336324110648ffdea7c7092a004353419b5.tar.gz
kpl-663d9336324110648ffdea7c7092a004353419b5.zip
Initial commit of source from zwzsg
Diffstat (limited to 'KPFormSimpleCode.cpp')
-rw-r--r--KPFormSimpleCode.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/KPFormSimpleCode.cpp b/KPFormSimpleCode.cpp
new file mode 100644
index 0000000..a93671f
--- /dev/null
+++ b/KPFormSimpleCode.cpp
@@ -0,0 +1,96 @@
+//---------------------------------------------------------------------------
+
+#include <vcl.h>
+#pragma hdrstop
+#include "KPFormSimpleCode.h"
+#include "KPLTypes.h"
+#include "KPLFillModSpecific.h"
+#include "WriteScript.h"
+#include "GenerateScript.h"
+#include "process.h"// For spawnlp
+#pragma package(smart_init)
+#pragma resource "*.dfm"
+//---------------------------------------------------------------------------
+TKPLWindowSimple *KPLWindowSimple;
+//---------------------------------------------------------------------------
+__fastcall TKPLWindowSimple::TKPLWindowSimple(TComponent* Owner)
+ : TForm(Owner)
+{
+ KPLFillModSpecific();
+}
+//---------------------------------------------------------------------------
+int GetChosenDifficultyLevel()
+{
+ if(KPLWindowSimple->RB4VeryHard->Checked)
+ return 4;
+ if(KPLWindowSimple->RB3Hard->Checked)
+ return 3;
+ if(KPLWindowSimple->RB2Medium->Checked)
+ return 2;
+ if(KPLWindowSimple->RB1Easy->Checked)
+ return 1;
+ if(KPLWindowSimple->RB0Spec->Checked)
+ return 0;
+ return -1;
+}
+
+DWORD ProcessID=0;
+
+
+int LaunchSpringExecutable()
+{
+
+ HANDLE ProcessHandle;
+
+ // Check that Spring isn't already running
+ ProcessHandle=OpenProcess(PROCESS_QUERY_INFORMATION,false,ProcessID);
+ if(ProcessHandle!=NULL)
+ {
+ AnsiString caption="Exit last game first!";
+ AnsiString text=(AnsiString)"Game already in progress. Please exit "+cKPgame::ExecutableFileName+" first.";
+ Application->MessageBox(text.c_str(),caption.c_str(),MB_OK);
+ return 2;
+ }
+
+ // Create and allocate "game" and its "players" and its "modoptions"
+ cKPgame* game=(cKPgame*)malloc(sizeof(cKPgame));
+ game->players=(cKPplayer**)malloc(32*sizeof(cKPplayer*));
+ for(int p=0;p<32;++p)
+ game->players[p]=(cKPplayer*)malloc(sizeof(cKPplayer));
+ game->ModOptions=(cKPmodoptions*)malloc(sizeof(cKPmodoptions));
+
+ // Generate a random script according to difficulty
+ GenerateScript(game,GetChosenDifficultyLevel());
+
+ // Writing that script on the disk
+ WriteScript(game);
+
+ // Running Spring
+ //ShellExecute(NULL,"open",game->ExecutableFileName,game->ScriptFileName,NULL,SW_SHOWNORMAL);
+ ProcessID=spawnl(P_NOWAIT,cKPgame::ExecutableFileName,game->ExecutableFileName,game->ScriptFileName,NULL);
+
+ // Check that Spring has run
+ ProcessHandle=OpenProcess(PROCESS_QUERY_INFORMATION,false,ProcessID);
+ if(ProcessHandle==NULL)
+ {
+ AnsiString caption=(AnsiString)cKPgame::ExecutableFileName+" not found!";
+ AnsiString text=caption;
+ Application->MessageBox(text.c_str(),caption.c_str(),MB_OK);
+ return 1;
+ }
+
+ //Application->Terminate();
+ return 0;
+}
+
+//---------------------------------------------------------------------------
+void __fastcall TKPLWindowSimple::BBOkClick(TObject *Sender)
+{
+ LaunchSpringExecutable();
+}
+//---------------------------------------------------------------------------
+void __fastcall TKPLWindowSimple::BBCancelClick(TObject *Sender)
+{
+ Application->Terminate();
+}
+//---------------------------------------------------------------------------