summaryrefslogtreecommitdiff
path: root/GenerateScript.cpp
blob: 35a0a83d5506a04a902180827a592479abb724d1 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//---------------------------------------------------------------------------
#include "GenerateScript.h"
#include "ColorSpacesConversions.h"
#include <stdlib.h>
#ifndef __WIN32__
    #include <unistd.h>
    #include <time.h>
#endif
#include <stdio.h>
//---------------------------------------------------------------------------
char description[256];
char description_bis[256];
const char* difficulty_string="";
//---------------------------------------------------------------------------

int GetMaxAllyTeam(cKPgame* game)
{
	int maxteamfound=0;
	int p;
	for(p=0;p<game->NbrPlayers;++p)
		if(game->players[p]->allyteam>maxteamfound)
			maxteamfound=game->players[p]->allyteam;
	return maxteamfound;
}

int GenerateRandNum(int lower_bound,int higher_bound)
{
	if(lower_bound>higher_bound)
	{
		int bound=higher_bound;
		higher_bound=lower_bound;
		lower_bound=bound;
	}
	if(lower_bound==higher_bound)
		return lower_bound;
	return lower_bound+(rand()%(1+higher_bound-lower_bound));
}

cKPmap* GenerateRandMapChoice()
{
    int m,w,d;
    for(w=m=0;m<nbrKPmaps;++m)
        w+=KPMapList[m]->Weight;
    d=GenerateRandNum(1,w);
    for(w=m=0;m<nbrKPmaps;++m)
    {
        w+=KPMapList[m]->Weight;
        if(w>=d)
            return KPMapList[m];
    }
    return (cKPmap*)0;
}

void PickRandomColors(cKPgame* game)
{
    unsigned char cHSL[3];
    int* randorder=(int*)malloc(game->NbrPlayers*sizeof(int));
    int* listavailable=(int*)malloc(game->NbrPlayers*sizeof(int));
    for(int p=0;p<game->NbrPlayers;++p)
        listavailable[p]=p;
    for(int p=0;p<game->NbrPlayers;++p)
    {
        int g=GenerateRandNum(0,game->NbrPlayers-1-p);
        randorder[p]=listavailable[g];
        for(int k=g;k<game->NbrPlayers-1;++k)
            listavailable[k]=listavailable[k+1];
    }
    unsigned char HueOffset=GenerateRandNum(0,255);
    for(int p=0;p<game->NbrPlayers;++p)
    {
        cHSL[0]=((long)HueOffset+((p*256)/game->NbrPlayers))%256;// Hue
        cHSL[1]=255;
        cHSL[2]=GenerateRandNum(96,192);
        RGBfromHSL(game->players[randorder[p]]->color,cHSL);
    }
    free(randorder);
    free(listavailable);
}

void GenerateScript(cKPgame* game,int difficulty,cKPmap* map)
{
    int max_players_per_team;
	int nbr_allies;
	int nbr_enemies;
	int p;
	int evilplayer;
	int player_num;
    cKPfaction* faction0;
    cKPfaction* faction1;

	// Seed random number generator
	srand((unsigned)time(NULL));

    game->map=GenerateRandMapChoice();
    game->isSpeccing=0;
    game->ModOptions->ons=0;
    game->ModOptions->minelauncher=1;
    game->ModOptions->nx=1;
    game->EndCondition=1;

    max_players_per_team=game->map->MaxPlayers/2;
	//nbr_allies=nbr_enemies=max_players_per_team;

    if(difficulty<0)
        difficulty=GenerateRandNum(1,4);

	switch(difficulty)
	{
		case 0:// Spectate
			nbr_allies=GenerateRandNum(1,max_players_per_team);
			nbr_enemies=nbr_allies;
			player_num=GenerateRandNum(0,nbr_enemies+nbr_allies-1);
			evilplayer=GenerateRandNum(0,1);
            game->isSpeccing=1;
            game->ModOptions->ons=GenerateRandNum(0,4)?0:2;
			difficulty_string="Spectating AI battling themselves";
			break;
		case 1:// Easy
			nbr_allies=GenerateRandNum(2,max_players_per_team);
			nbr_enemies=nbr_allies-1;
			player_num=2*GenerateRandNum(0,nbr_allies-1);
			evilplayer=0;
            game->ModOptions->ons=GenerateRandNum(0,3)?0:2;
			difficulty_string="Easy";
			break;
		case 2:// Medium
			nbr_allies=GenerateRandNum(1,max_players_per_team);
			nbr_enemies=nbr_allies;
			player_num=GenerateRandNum(0,nbr_allies+nbr_enemies-1);
			evilplayer=GenerateRandNum(0,1);
			difficulty_string="Medium";
			break;
		case 3:// Hard
			nbr_allies=GenerateRandNum(1,max_players_per_team-1);
			nbr_enemies=GenerateRandNum(nbr_allies+1,max_players_per_team);
			player_num=1+2*GenerateRandNum(0,nbr_allies-1);
			evilplayer=GenerateRandNum(0,1);
			difficulty_string="Hard";
			break;
		case 4:// Very Hard
			nbr_enemies=GenerateRandNum(3,max_players_per_team+1);
   			nbr_allies=GenerateRandNum(1,nbr_enemies-2);
			player_num=1+2*GenerateRandNum(0,nbr_allies-1);
			evilplayer=GenerateRandNum(0,1);
            game->ModOptions->ons=4;
			difficulty_string="Very Hard";
			break;
		default:// 1v1
			nbr_allies=1;
			nbr_enemies=1;
			player_num=GenerateRandNum(0,1);
			evilplayer=GenerateRandNum(0,1);
			difficulty_string="??";
			break;
	}

    game->NbrPlayers=(nbr_allies+nbr_enemies);
    game->PlayerTeamNum=player_num;
    faction0=KPFactionList[0];
    faction1=KPFactionList[1];
    if(difficulty!=1 && GenerateRandNum(0,2))// 1/3+2/3*1/2 = 2*3
    {
        if(GenerateRandNum(0,1))
            faction1=KPFactionList[2];
        else
            faction0=KPFactionList[2];
    }

	for(p=0;p<(nbr_allies+nbr_enemies);++p)
	{
		int enemy=(p+player_num)%2;
		if(nbr_enemies<nbr_allies)
		{
			if(p>2*nbr_enemies)
				enemy=0;
		}
		if(nbr_allies<nbr_enemies)
		{
			if(p>2*nbr_allies)
				enemy=1;
		}
		game->players[p]->allyteam=enemy;
		game->players[p]->faction=(enemy+evilplayer)%2?faction1:faction0;
	}
    PickRandomColors(game);

	if((game->isSpeccing)||(player_num<0)||(player_num>nbr_allies+nbr_enemies))
	{// Spec
		sprintf(description,"%s: %dv%d",difficulty_string,nbr_allies,nbr_enemies);
	}
	else
	{// Not Spec
		sprintf(description,"%s: %dv%d - Player is n°%d in [%d..%d] and %s (%s)",difficulty_string,nbr_allies,nbr_enemies,player_num,0,nbr_allies+nbr_enemies-1,evilplayer?"evil":"good",game->players[player_num]->faction->name);
	}
    game->description=description;
}