tbftss/src/game/trophies.c

529 lines
12 KiB
C
Raw Normal View History

2016-03-03 19:03:07 +01:00
/*
Copyright (C) 2015-2016 Parallel Realities
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 "trophies.h"
2016-05-15 09:19:26 +02:00
static void prevPage(void);
static void nextPage(void);
2016-03-03 19:03:07 +01:00
static void loadTrophyData(char *filename);
2016-03-09 12:51:26 +01:00
static void resetAlert(void);
2016-05-15 09:19:26 +02:00
static void setSparkleColor(Trophy *t);
static void nextAlert(void);
2016-03-09 12:51:26 +01:00
static Trophy *alertTrophy;
static SDL_Texture *trophyIcons[TROPHY_MAX];
2016-05-15 09:19:26 +02:00
static SDL_Texture *sparkle;
static SDL_Texture *alertSphere;
2016-03-09 12:51:26 +01:00
static SDL_Rect alertRect;
static int alertTimer;
2016-05-15 09:19:26 +02:00
static int page;
static int awarded;
static int total;
static int boxWidth;
static float sparkleAngle;
static float maxPages;
static Widget *prev;
static Widget *next;
2016-07-19 10:26:19 +02:00
static char *TROPHIES_TEXT;
static char *AWARDED_TEXT;
static char *PAGE_TEXT;
static char *HIDDEN_TEXT;
2016-03-03 19:03:07 +01:00
void initTrophies(void)
{
loadTrophyData("data/trophies/trophies.json");
2016-03-09 12:51:26 +01:00
trophyIcons[TROPHY_BRONZE] = getTexture("gfx/trophies/bronze.png");
trophyIcons[TROPHY_SILVER] = getTexture("gfx/trophies/silver.png");
trophyIcons[TROPHY_GOLD] = getTexture("gfx/trophies/gold.png");
trophyIcons[TROPHY_PLATINUM] = getTexture("gfx/trophies/platinum.png");
2016-05-15 09:19:26 +02:00
trophyIcons[TROPHY_UNEARNED] = getTexture("gfx/trophies/unearned.png");
sparkle = getTexture("gfx/trophies/sparkle.png");
alertSphere = getTexture("gfx/trophies/alertSphere.png");
alertRect.h = 90;
alertRect.y = 10;
2016-06-02 13:34:35 +02:00
sparkleAngle = 0;
2016-07-19 10:26:19 +02:00
TROPHIES_TEXT = _("Trophies");
AWARDED_TEXT = _("Awarded : %d / %d");
PAGE_TEXT = _("Page : %d / %d");
HIDDEN_TEXT = _("[Hidden]");
2016-03-09 12:51:26 +01:00
resetAlert();
2016-03-03 19:03:07 +01:00
}
2016-05-15 09:19:26 +02:00
void initTrophiesDisplay(void)
2016-03-03 19:03:07 +01:00
{
2016-05-15 09:19:26 +02:00
int w, h;
2016-03-03 19:03:07 +01:00
Trophy *t;
2016-05-15 09:19:26 +02:00
boxWidth = total = awarded = 0;
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
{
total++;
if (t->awarded)
{
awarded++;
STRNCPY(t->awardDateStr, timeToDate(t->awardDate), MAX_NAME_LENGTH);
}
textSize(t->description, 18, &w, &h);
boxWidth = MAX(boxWidth, w);
}
boxWidth += 125;
page = 0;
maxPages = total;
maxPages /= TROPHIES_PER_PAGE;
maxPages = ceil(maxPages);
prev = getWidget("prev", "trophies");
prev->action = prevPage;
prev->visible = 0;
next = getWidget("next", "trophies");
next->action = nextPage;
next->visible = 1;
}
static void nextPage(void)
{
page = MIN(page + 1, maxPages - 1);
next->visible = page < maxPages - 1;
prev->visible = 1;
}
static void prevPage(void)
{
page = MAX(0, page - 1);
next->visible = 1;
prev->visible = page > 0;
}
void drawTrophies(void)
{
Trophy *t;
SDL_Rect r;
int start, end, i, x, y;
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 128);
SDL_RenderFillRect(app.renderer, NULL);
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_NONE);
r.w = boxWidth;
r.h = 650;
r.x = (SCREEN_WIDTH / 2) - r.w / 2;
r.y = (SCREEN_HEIGHT / 2) - r.h / 2;
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 0);
SDL_RenderFillRect(app.renderer, &r);
SDL_SetRenderDrawColor(app.renderer, 200, 200, 200, 255);
SDL_RenderDrawRect(app.renderer, &r);
2016-07-19 10:26:19 +02:00
drawText(SCREEN_WIDTH / 2, 40, 28, TA_CENTER, colors.white, TROPHIES_TEXT);
drawText(SCREEN_WIDTH / 2, 83, 16, TA_CENTER, colors.lightGrey, AWARDED_TEXT, awarded, total);
drawText(SCREEN_WIDTH / 2, 110, 16, TA_CENTER, colors.lightGrey, PAGE_TEXT, page + 1, (int)maxPages);
2016-05-15 09:19:26 +02:00
SDL_SetRenderDrawColor(app.renderer, 128, 128, 128, 255);
SDL_RenderDrawLine(app.renderer, r.x, 150, r.x + r.w, 150);
x = r.x + 15;
y = 180;
start = page * TROPHIES_PER_PAGE;
end = start + TROPHIES_PER_PAGE;
i = 0;
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
{
if (i >= start && i < end)
{
if (t->awarded)
{
setSparkleColor(t);
blitRotated(sparkle, x + 32, y + 32, sparkleAngle);
blitRotated(sparkle, x + 32, y + 32, -sparkleAngle);
blitScaled(trophyIcons[t->value], x, y, 64, 64, 0);
2016-05-15 09:19:26 +02:00
drawText(x + 85, y - 10, 20, TA_LEFT, colors.yellow, t->title);
drawText(x + 85, y + 20, 18, TA_LEFT, colors.white, t->description);
drawText(x + 85, y + 48, 18, TA_LEFT, colors.white, t->awardDateStr);
}
else
{
blitScaled(trophyIcons[TROPHY_UNEARNED], x, y, 64, 64, 0);
2016-05-15 09:19:26 +02:00
if (!t->hidden)
{
drawText(x + 85, y - 10, 20, TA_LEFT, colors.lightGrey, t->title);
drawText(x + 85, y + 20, 18, TA_LEFT, colors.darkGrey, t->description);
drawText(x + 85, y + 48, 18, TA_LEFT, colors.darkGrey, "-");
}
else
{
2016-07-19 10:26:19 +02:00
drawText(x + 85, y + 20, 20, TA_LEFT, colors.darkGrey, HIDDEN_TEXT);
2016-05-15 09:19:26 +02:00
}
}
y += 120;
}
i++;
}
drawWidgets("trophies");
}
void awardTrophy(char *id)
{
Trophy *t;
int numRemaining;
numRemaining = 0;
2016-03-03 19:03:07 +01:00
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
{
if (!t->awarded && strcmp(t->id, id) == 0)
{
t->awarded = 1;
t->awardDate = time(NULL);
t->notify = SDL_GetTicks();
/* prevent race condition */
SDL_Delay(1);
2016-05-15 09:19:26 +02:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Awarding trophy '%s'", t->id);
app.saveGame = 1;
}
if (!t->awarded)
{
numRemaining++;
2016-03-03 19:03:07 +01:00
}
}
2016-05-15 09:19:26 +02:00
/* the Platinum will always be the last trophy to unlock */
if (numRemaining == 1)
{
awardTrophy("PLATINUM");
}
2016-03-03 19:03:07 +01:00
}
2016-05-15 09:19:26 +02:00
void doTrophyAlerts(void)
{
if (!alertTrophy)
{
nextAlert();
}
else if (alertTrophy)
{
alertRect.x = MIN(alertRect.x + 24, -1);
if (alertRect.x > -150)
{
alertTimer--;
}
if (alertTimer <= 0)
{
alertTrophy->notify = 0;
resetAlert();
}
}
2016-06-02 13:34:35 +02:00
sparkleAngle = mod(sparkleAngle + 0.25, 360);
2016-05-15 09:19:26 +02:00
}
static void nextAlert(void)
2016-03-03 19:03:07 +01:00
{
2016-05-15 09:19:26 +02:00
int w, h;
2016-03-03 19:03:07 +01:00
Trophy *t;
2016-03-09 12:51:26 +01:00
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
2016-03-03 19:03:07 +01:00
{
2016-03-09 12:51:26 +01:00
if (t->notify)
2016-03-03 19:03:07 +01:00
{
if (!alertTrophy || t->notify < alertTrophy->notify)
2016-03-09 12:51:26 +01:00
{
alertTrophy = t;
}
2016-03-03 19:03:07 +01:00
}
}
2016-05-15 09:19:26 +02:00
if (alertTrophy)
{
playSound(SND_TROPHY);
textSize(alertTrophy->title, 30, &alertRect.w, &h);
textSize(alertTrophy->description, 20, &w, &h);
alertRect.w = MAX(alertRect.w, w);
2016-05-19 09:53:14 +02:00
alertRect.w = MAX(400, alertRect.w);
2016-05-15 09:19:26 +02:00
alertRect.w += 125;
alertRect.x = -alertRect.w;
}
2016-03-03 19:03:07 +01:00
}
2016-03-09 12:51:26 +01:00
static void resetAlert(void)
{
alertTimer = FPS * 3;
2016-05-15 09:19:26 +02:00
alertTrophy = NULL;
2016-03-09 12:51:26 +01:00
}
void drawTrophyAlert(void)
{
2016-05-15 09:19:26 +02:00
int x, y;
2016-03-09 12:51:26 +01:00
if (alertTrophy)
{
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(app.renderer, &alertRect);
SDL_SetRenderDrawColor(app.renderer, 64, 64, 64, SDL_ALPHA_OPAQUE);
SDL_RenderDrawRect(app.renderer, &alertRect);
2016-05-15 09:19:26 +02:00
drawText(alertRect.x + 15, alertRect.y + 5, 30, TA_LEFT, colors.white, alertTrophy->title);
drawText(alertRect.x + 15, alertRect.y + 45, 20, TA_LEFT, colors.white, alertTrophy->description);
setSparkleColor(alertTrophy);
x = alertRect.x + alertRect.w - 72;
y = alertRect.y + 20;
blit(alertSphere, x + 24, y + 24, 1);
blitRotated(sparkle, x + 24, y + 24, sparkleAngle);
blitRotated(sparkle, x + 24, y + 24, -sparkleAngle);
blitScaled(trophyIcons[alertTrophy->value], x, y, 48, 48, 0);
2016-03-09 12:51:26 +01:00
}
}
2016-03-03 19:03:07 +01:00
Trophy *getTrophy(char *id)
{
Trophy *t;
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
{
if (strcmp(t->id, id) == 0)
{
return t;
}
}
return NULL;
}
static void loadTrophyData(char *filename)
{
cJSON *root, *node;
char *text;
Trophy *t, *tail;
2016-05-26 19:18:08 +02:00
int count[TROPHY_MAX];
2016-03-03 19:03:07 +01:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(filename);
2016-03-03 19:03:07 +01:00
root = cJSON_Parse(text);
tail = &game.trophyHead;
2016-05-26 19:18:08 +02:00
memset(count, 0, sizeof(int) * TROPHY_MAX);
2016-03-03 19:03:07 +01:00
for (node = root->child ; node != NULL ; node = node->next)
{
2016-05-15 09:19:26 +02:00
if (cJSON_GetObjectItem(node, "id")->valuestring[0] != '_')
2016-03-09 13:31:33 +01:00
{
2016-05-15 09:19:26 +02:00
t = malloc(sizeof(Trophy));
memset(t, 0, sizeof(Trophy));
STRNCPY(t->id, cJSON_GetObjectItem(node, "id")->valuestring, MAX_NAME_LENGTH);
STRNCPY(t->title, _(cJSON_GetObjectItem(node, "title")->valuestring), MAX_DESCRIPTION_LENGTH);
STRNCPY(t->description, _(cJSON_GetObjectItem(node, "description")->valuestring), MAX_DESCRIPTION_LENGTH);
t->value = lookup(cJSON_GetObjectItem(node, "value")->valuestring);
t->hidden = getJSONValue(node, "hidden", 0);
t->stat = -1;
/* can't use the getJSONValue here, as it could lead to false positives */
if (cJSON_GetObjectItem(node, "stat"))
{
t->stat = lookup(cJSON_GetObjectItem(node, "stat")->valuestring);
t->statValue = cJSON_GetObjectItem(node, "statValue")->valueint;
}
2016-05-26 19:18:08 +02:00
count[t->value]++;
count[TROPHY_UNEARNED]++;
2016-03-09 13:31:33 +01:00
2016-05-15 09:19:26 +02:00
tail->next = t;
tail = t;
}
2016-03-03 19:03:07 +01:00
}
2016-05-26 19:18:08 +02:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Trophies (%d) [Bronze=%d, Silver=%d, Gold=%d, Platinum=%d]", count[TROPHY_UNEARNED], count[TROPHY_BRONZE], count[TROPHY_SILVER], count[TROPHY_GOLD], count[TROPHY_PLATINUM]);
2016-03-03 19:03:07 +01:00
cJSON_Delete(root);
free(text);
}
2016-03-09 13:31:33 +01:00
void awardStatsTrophies(void)
2016-03-09 13:31:33 +01:00
{
Trophy *t;
2016-05-17 20:02:58 +02:00
Tuple *tp;
2016-03-09 13:31:33 +01:00
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
{
2016-05-15 09:19:26 +02:00
if (t->stat != -1 && !t->awarded && (game.stats[t->stat] + battle.stats[t->stat]) >= t->statValue)
2016-03-09 13:31:33 +01:00
{
awardTrophy(t->id);
2016-03-09 13:31:33 +01:00
}
}
2016-05-17 20:02:58 +02:00
/* check to see if we've destroyed one of each common starfighter */
for (tp = game.fighterStatHead.next ; tp != NULL ; tp = tp->next)
{
if (tp->value == 0)
{
return;
}
}
awardTrophy("FREQUENT_FLYER");
2016-03-09 13:31:33 +01:00
}
void awardCampaignTrophies(void)
{
char trophyId[MAX_NAME_LENGTH];
char name[MAX_NAME_LENGTH];
2016-05-15 09:19:26 +02:00
int i, len;
2016-03-10 19:02:19 +01:00
StarSystem *starSystem;
2016-05-15 09:19:26 +02:00
if (game.completedMissions)
{
awardTrophy("CAMPAIGN_1");
}
/* check if all star system missions are completed */
for (starSystem = game.starSystemHead.next ; starSystem != NULL ; starSystem = starSystem->next)
{
if (starSystem->totalMissions && starSystem->completedMissions == starSystem->totalMissions)
{
memset(name, '\0', MAX_NAME_LENGTH);
len = strlen(starSystem->name);
for (i = 0 ; i < len ; i++)
{
name[i] = toupper(starSystem->name[i]);
}
2016-05-15 09:19:26 +02:00
sprintf(trophyId, "CAMPAIGN_%s", name);
awardTrophy(trophyId);
}
}
}
void awardChallengeTrophies(void)
{
char trophyId[MAX_NAME_LENGTH];
int completed;
2016-05-15 09:19:26 +02:00
/* check % of challenges completed - 25% increments*/
completed = (getPercent(game.completedChallenges, game.totalChallenges) / 25) * 25;
sprintf(trophyId, "CHALLENGE_%d", completed);
awardTrophy(trophyId);
}
void awardPostMissionTrophies(void)
{
if (game.currentMission->epic)
{
awardTrophy("EPIC");
2016-05-15 09:19:26 +02:00
if (battle.stats[STAT_PLAYER_KILLED] == 0 && player->flags & EF_COMMON_FIGHTER)
2016-05-15 09:19:26 +02:00
{
awardTrophy("SURVIVOR");
}
}
/*
* Must be a non-challenge mission, a common fighter, must not be Sol, and must not have fired any shots or missiles (and there should have been some enemies present)
2016-05-15 09:19:26 +02:00
*/
if (player->flags & EF_COMMON_FIGHTER && player->missiles && strcmp(game.selectedStarSystem, "Sol") && !battle.stats[STAT_SHOTS_FIRED] && !battle.stats[STAT_MISSILES_FIRED] && battle.numInitialEnemies > 0)
2016-05-15 09:19:26 +02:00
{
awardTrophy("PACIFIST");
}
}
2016-06-03 08:41:44 +02:00
void awardCraftTrophy(void)
{
2016-06-03 08:41:44 +02:00
if (!game.currentMission->challengeData.isChallenge)
{
2016-06-03 08:41:44 +02:00
if (strcmp(game.currentMission->craft, "ATAF") == 0)
{
awardTrophy("ATAF");
}
else if (strcmp(game.currentMission->craft, "Tug") == 0)
{
awardTrophy("TUG");
}
}
2016-06-03 08:41:44 +02:00
else
{
if (strcmp(game.currentMission->craft, "Shuttle") == 0 && battle.stats[STAT_ITEMS_COLLECTED_PLAYER] > 0)
{
awardTrophy("SHUTTLE");
}
}
awardPandoranCraftTrophy();
}
2016-05-15 09:19:26 +02:00
static void setSparkleColor(Trophy *t)
{
switch (t->value)
{
case TROPHY_BRONZE:
SDL_SetTextureColorMod(sparkle, 255, 128, 0);
break;
case TROPHY_SILVER:
SDL_SetTextureColorMod(sparkle, 192, 192, 192);
break;
case TROPHY_GOLD:
SDL_SetTextureColorMod(sparkle, 255, 255, 0);
break;
case TROPHY_PLATINUM:
SDL_SetTextureColorMod(sparkle, 0, 128, 255);
break;
}
}