tbftss/src/challenges/challenges.c

294 lines
6.4 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
2016-02-21 16:50:27 +01:00
Copyright (C) 2015-2016 Parallel Realities
2015-10-20 13:51:49 +02:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "challenges.h"
static void updateTimeChallenge(Challenge *c);
static void updateAccuracyChallenge(Challenge *c);
static void updateArmourChallenge(Challenge *c);
static void updateLossesChallenge(Challenge *c);
static void updatePlayerKillsChallenge(Challenge *c);
static void updateDisabledChallenge(Challenge *c);
2016-02-28 14:02:57 +01:00
static void completeChallenge(void);
static void failChallenge(void);
static void updateChallenges(void);
2015-10-20 13:51:49 +02:00
static char *getFormattedChallengeDescription(const char *format, ...);
char *getChallengeDescription(Challenge *c);
static char descriptionBuffer[MAX_DESCRIPTION_LENGTH];
2016-02-29 11:47:41 +01:00
static const char *challengeDescription[CHALLENGE_MAX];
2015-10-20 13:51:49 +02:00
2016-02-27 13:14:29 +01:00
void initChallenges(void)
{
Mission *mission, *tail;
char **filenames;
char path[MAX_FILENAME_LENGTH];
int count, i;
2016-02-29 11:47:41 +01:00
challengeDescription[CHALLENGE_ARMOUR] = _("Retain at least %d%% armour");
challengeDescription[CHALLENGE_TIME] = _("Complete challenge in %d seconds or less");
challengeDescription[CHALLENGE_ACCURACY] = _("Attain a %d%% hit accuracy");
challengeDescription[CHALLENGE_NO_LOSSES] = _("Do not lose any team mates");
challengeDescription[CHALLENGE_1_LOSS] = _("Do not lose more than 1 team mate");
challengeDescription[CHALLENGE_LOSSES] = _("Do not lose more than %d team mates");
challengeDescription[CHALLENGE_PLAYER_KILLS] = _("Take down %d enemy targets");
challengeDescription[CHALLENGE_DISABLE] = _("Disable %d or more enemy fighters");
challengeDescription[CHALLENGE_TIME_MINS] = _("Complete challenge in %d minutes or less");
2016-02-27 13:14:29 +01:00
tail = &game.challengeMissionHead;
filenames = getFileList(getFileLocation("data/challenges"), &count);
2016-02-27 13:14:29 +01:00
for (i = 0 ; i < count ; i++)
{
sprintf(path, "data/challenges/%s", filenames[i]);
mission = loadMissionMeta(path);
tail->next = mission;
tail = mission;
free(filenames[i]);
}
free(filenames);
}
2016-02-28 14:02:57 +01:00
void doChallenges(void)
{
if (battle.challengeData.isChallenge && battle.status == MS_IN_PROGRESS)
{
if (battle.challengeData.timeLimit > 0 && battle.stats[STAT_TIME] / FPS >= battle.challengeData.timeLimit)
{
failChallenge();
}
if (battle.challengeData.killLimit > 0 && battle.stats[STAT_ENEMIES_KILLED_PLAYER] >= battle.challengeData.killLimit)
{
completeChallenge();
}
if (battle.status != MS_IN_PROGRESS)
{
updateChallenges();
}
}
}
static void updateChallenges(void)
2015-10-20 13:51:49 +02:00
{
Challenge *c;
for (c = game.currentMission->challengeHead.next ; c != NULL ; c = c->next)
{
if (!c->passed)
{
switch (c->type)
{
case CHALLENGE_TIME:
case CHALLENGE_TIME_MINS:
2015-10-20 13:51:49 +02:00
updateTimeChallenge(c);
break;
case CHALLENGE_ACCURACY:
updateAccuracyChallenge(c);
break;
case CHALLENGE_ARMOUR:
updateArmourChallenge(c);
break;
case CHALLENGE_NO_LOSSES:
case CHALLENGE_1_LOSS:
case CHALLENGE_LOSSES:
updateLossesChallenge(c);
break;
case CHALLENGE_PLAYER_KILLS:
updatePlayerKillsChallenge(c);
break;
case CHALLENGE_DISABLE:
updateDisabledChallenge(c);
break;
2015-10-20 13:51:49 +02:00
}
}
}
}
static void updateTimeChallenge(Challenge *c)
{
switch (c->type)
2015-10-20 13:51:49 +02:00
{
case CHALLENGE_TIME:
2016-02-27 17:16:21 +01:00
if (battle.stats[STAT_TIME] / FPS < c->value)
{
c->passed = 1;
}
break;
case CHALLENGE_TIME_MINS:
2016-02-27 17:16:21 +01:00
if ((battle.stats[STAT_TIME] / FPS) / 60 < c->value)
{
c->passed = 1;
}
break;
2015-10-20 13:51:49 +02:00
}
}
static void updateAccuracyChallenge(Challenge *c)
{
float percent;
percent = battle.stats[STAT_SHOTS_HIT];
percent /= battle.stats[STAT_SHOTS_FIRED];
2015-10-20 13:51:49 +02:00
percent *= 100;
2016-02-27 17:16:21 +01:00
if (percent >= c->value)
2015-10-20 13:51:49 +02:00
{
c->passed = 1;
}
}
static void updateArmourChallenge(Challenge *c)
{
float percent;
percent = player->health;
percent /= player->maxHealth;
percent *= 100;
2016-02-27 17:16:21 +01:00
if (percent >= c->value)
2015-10-20 13:51:49 +02:00
{
c->passed = 1;
}
}
static void updateLossesChallenge(Challenge *c)
{
if (!c->passed)
{
2016-02-27 17:16:21 +01:00
c->passed = battle.stats[STAT_ALLIES_KILLED] <= c->value;
2015-10-20 13:51:49 +02:00
}
}
static void updatePlayerKillsChallenge(Challenge *c)
{
if (!c->passed)
{
2016-02-27 17:16:21 +01:00
c->passed = battle.stats[STAT_ENEMIES_KILLED_PLAYER] >= c->value;
2015-10-20 13:51:49 +02:00
}
}
static void updateDisabledChallenge(Challenge *c)
{
if (!c->passed)
{
2016-02-27 17:16:21 +01:00
c->passed = battle.stats[STAT_ENEMIES_DISABLED] >= c->value;
}
}
2015-10-20 13:51:49 +02:00
char *getChallengeDescription(Challenge *c)
{
2016-02-27 17:16:21 +01:00
return getFormattedChallengeDescription(challengeDescription[c->type], c->value);
2015-10-20 13:51:49 +02:00
}
2016-02-27 17:16:21 +01:00
Challenge *getChallenge(Mission *mission, int type, int value)
2015-10-20 13:51:49 +02:00
{
2016-02-27 17:16:21 +01:00
Challenge *c;
2015-10-20 13:51:49 +02:00
2016-02-27 17:16:21 +01:00
for (c = mission->challengeHead.next ; c != NULL ; c = c->next)
2015-10-20 13:51:49 +02:00
{
2016-02-27 17:16:21 +01:00
if (c->type == type && c->value == value)
2015-10-20 13:51:49 +02:00
{
2016-02-27 17:16:21 +01:00
return c;
2015-10-20 13:51:49 +02:00
}
}
return NULL;
}
static char *getFormattedChallengeDescription(const char *format, ...)
{
va_list args;
memset(&descriptionBuffer, '\0', sizeof(descriptionBuffer));
va_start(args, format);
vsprintf(descriptionBuffer, format, args);
va_end(args);
return descriptionBuffer;
}
2016-02-28 14:02:57 +01:00
2016-02-29 11:47:41 +01:00
void updateChallengeMissions(void)
{
Mission *m;
Challenge *c;
for (m = game.challengeMissionHead.next ; m != NULL ; m = m->next)
{
m->totalChallenges = m->completedChallenges = 0;
for (c = m->challengeHead.next ; c != NULL ; c = c->next)
{
m->totalChallenges++;
if (c->passed)
{
m->completedChallenges++;
}
}
}
}
2016-02-28 14:02:57 +01:00
static void completeChallenge(void)
{
if (battle.status == MS_IN_PROGRESS)
{
battle.status = MS_COMPLETE;
battle.missionFinishedTimer = FPS;
selectWidget("continue", "battleWon");
game.stats[STAT_CHALLENGES_COMPLETED]++;
retreatAllies();
retreatEnemies();
player->flags |= EF_IMMORTAL;
}
}
static void failChallenge(void)
{
if (battle.status == MS_IN_PROGRESS)
{
battle.status = MS_FAILED;
battle.missionFinishedTimer = FPS;
selectWidget("retry", "battleLost");
retreatAllies();
retreatEnemies();
player->flags |= EF_IMMORTAL;
}
}