//--------------------------------------------------------------------------- #include "GenerateScript.h" #include "ColorSpacesConversions.h" #include #ifndef __WIN32__ #include #include #endif #include //--------------------------------------------------------------------------- char description[256]; char description_bis[256]; const char* difficulty_string=""; //--------------------------------------------------------------------------- int GetMaxAllyTeam(cKPgame* game) { int maxteamfound=0; int p; for(p=0;pNbrPlayers;++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;mWeight; d=GenerateRandNum(1,w); for(w=m=0;mWeight; 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;pNbrPlayers;++p) listavailable[p]=p; for(int p=0;pNbrPlayers;++p) { int g=GenerateRandNum(0,game->NbrPlayers-1-p); randorder[p]=listavailable[g]; for(int k=g;kNbrPlayers-1;++k) listavailable[k]=listavailable[k+1]; } unsigned char HueOffset=GenerateRandNum(0,255); for(int p=0;pNbrPlayers;++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_enemies2*nbr_enemies) enemy=0; } if(nbr_allies2*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; }