summaryrefslogtreecommitdiff
path: root/KPFormSimpleCode.cpp
blob: a93671f01127cc398840ffb021fbc363cee232d0 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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();
}
//---------------------------------------------------------------------------