blobwarsAttrition/src/hub/postMission.c

283 lines
5.2 KiB
C
Raw Permalink Normal View History

2018-02-18 10:29:26 +01:00
/*
2019-06-02 17:13:34 +02:00
Copyright (C) 2018-2019 Parallel Realities
2018-02-18 10:29:26 +01: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 "postMission.h"
static void logic(void);
static void draw(void);
static void updateMissionStatus(void);
2018-02-22 08:45:57 +01:00
static int getPostMissionStatus(void);
static void saveGameAndWorld(void);
2018-02-21 09:16:18 +01:00
2018-02-20 09:14:35 +01:00
static int status;
2018-02-21 09:16:18 +01:00
static float missionCompleteY;
static Atlas *background;
static Texture *atlasTexture;
static float oNum;
static int canContinue;
2018-02-18 10:29:26 +01:00
void initPostMission(void)
{
startSectionTransition();
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
atlasTexture = getTexture("gfx/atlas/atlas.png");
background = getImageFromAtlas("gfx/radar/background.png");
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
updateMissionStatus();
2019-06-02 17:13:34 +02:00
2018-03-20 20:26:14 +01:00
if (world.state == WS_GAME_COMPLETE)
{
saveGameAndWorld();
2018-03-20 20:26:14 +01:00
destroyWorld();
2019-06-02 17:13:34 +02:00
2018-03-20 20:26:14 +01:00
initEnding();
}
else if (world.state != WS_QUIT)
2018-02-21 09:16:18 +01:00
{
app.restrictTrophyAlert = 0;
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
canContinue = 0;
2019-06-02 17:13:34 +02:00
2018-02-22 08:45:57 +01:00
oNum = 0;
2019-06-02 17:13:34 +02:00
2018-12-22 22:53:19 +01:00
missionCompleteY = UI_HEIGHT;
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
playSound(SND_MISSION_COMPLETE, 0);
2019-06-02 17:13:34 +02:00
2018-03-15 09:01:04 +01:00
app.delegate.logic = &logic;
app.delegate.draw = &draw;
2019-06-02 17:13:34 +02:00
saveGameAndWorld();
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
endSectionTransition();
}
2018-02-21 23:04:01 +01:00
else
{
if (world.isReturnVisit)
{
saveGameAndWorld();
2018-02-21 23:04:01 +01:00
}
else
{
restoreGameState();
2019-06-02 17:13:34 +02:00
saveGame(0);
2018-02-21 23:04:01 +01:00
}
2019-06-02 17:13:34 +02:00
2018-02-21 23:04:01 +01:00
destroyWorld();
initHub();
}
2018-02-18 10:29:26 +01:00
}
static void saveGameAndWorld(void)
{
char *src, *dest;
2019-06-02 17:13:34 +02:00
saveGame(1);
2019-06-02 17:13:34 +02:00
2018-05-01 19:15:56 +02:00
if (game.plus == PLUS_NONE)
{
saveWorld();
2018-05-01 19:15:56 +02:00
src = buildFormattedString("%s/%d/%s.json.tmp", app.saveDir, game.saveSlot, world.id);
dest = buildFormattedString("%s/%d/%s.json", app.saveDir, game.saveSlot, world.id);
if (!renameFile(src, dest))
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to save file '%s'", dest);
exit(1);
}
2019-06-02 17:13:34 +02:00
2018-05-01 19:15:56 +02:00
free(src);
free(dest);
}
2019-06-02 17:13:34 +02:00
src = buildFormattedString("%s/%d/game.json.tmp", app.saveDir, game.saveSlot);
dest = buildFormattedString("%s/%d/game.json", app.saveDir, game.saveSlot, world.id);
if (!renameFile(src, dest))
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to save file '%s'", dest);
exit(1);
}
2019-06-02 17:13:34 +02:00
free(src);
free(dest);
}
2018-02-25 13:12:31 +01:00
void retryMission(void)
{
restoreGameState();
2019-06-02 17:13:34 +02:00
saveGame(0);
2019-06-02 17:13:34 +02:00
2018-02-25 13:12:31 +01:00
initWorld();
}
void returnToHub(void)
{
restoreGameState();
2019-06-02 17:13:34 +02:00
saveGame(0);
2019-06-02 17:13:34 +02:00
2018-02-25 13:12:31 +01:00
destroyWorld();
initHub();
}
2018-02-18 10:29:26 +01:00
static void updateMissionStatus(void)
{
Tuple *t;
2019-06-02 17:13:34 +02:00
2018-02-18 10:29:26 +01:00
for (t = game.missionStatusHead.next ; t != NULL ; t = t->next)
{
if (strcmp(t->key, world.id) == 0)
{
2018-02-22 08:45:57 +01:00
t->value.i = status = getPostMissionStatus();
2018-02-18 10:29:26 +01:00
return;
}
}
2019-06-02 17:13:34 +02:00
2018-02-18 10:29:26 +01:00
t = malloc(sizeof(Tuple));
memset(t, 0, sizeof(Tuple));
game.missionStatusTail->next = t;
game.missionStatusTail = t;
2019-06-02 17:13:34 +02:00
2018-02-18 10:29:26 +01:00
STRNCPY(t->key, world.id, MAX_NAME_LENGTH);
2018-02-22 08:45:57 +01:00
t->value.i = status = getPostMissionStatus();
2018-02-18 10:29:26 +01:00
}
2018-02-20 09:14:35 +01:00
static void logic(void)
2018-02-18 10:29:26 +01:00
{
2018-02-21 09:16:18 +01:00
int done;
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
done = (status == MS_INCOMPLETE);
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
missionCompleteY = limit(missionCompleteY - 10, 50, SCREEN_HEIGHT);
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
if (missionCompleteY == 50)
{
oNum += 0.1;
}
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
if (canContinue && isAcceptControl())
{
done = 1;
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
clearControls();
}
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
if (done)
{
destroyWorld();
2018-02-18 10:29:26 +01:00
2018-02-21 09:16:18 +01:00
initHub();
}
2018-02-18 10:29:26 +01:00
}
2018-02-20 09:14:35 +01:00
static void draw(void)
{
2018-02-21 09:16:18 +01:00
Objective *o;
SDL_Color c;
char *status;
int x, y, w, i;
2019-06-02 17:13:34 +02:00
2018-12-22 22:53:19 +01:00
blitRectScaled(atlasTexture->texture, 0, 0, app.config.winWidth, app.config.winHeight, &background->rect, 0);
2019-06-02 17:13:34 +02:00
2018-12-22 22:53:19 +01:00
SDL_SetRenderTarget(app.renderer, app.uiBuffer);
2019-06-02 17:13:34 +02:00
2018-12-22 22:53:19 +01:00
drawText(UI_WIDTH / 2, missionCompleteY, 45, TA_CENTER, colors.white, app.strings[ST_MISSION_COMPLETE]);
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
i = 0;
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
if (missionCompleteY == 50)
{
w = 800;
2018-12-22 22:53:19 +01:00
x = (UI_WIDTH - w) / 2;
2018-02-21 09:16:18 +01:00
y = 150;
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
for (o = world.objectiveHead.next ; o != NULL ; o = o->next)
{
c = o->required ? colors.red : colors.white;
2018-04-01 18:41:45 +02:00
status = app.strings[ST_INCOMPLETE];
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
if (o->currentValue >= o->targetValue)
{
c = colors.green;
2018-04-01 18:41:45 +02:00
status = app.strings[ST_COMPLETE];
2018-02-21 09:16:18 +01:00
}
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
drawText(x + 20, y, 24, TA_LEFT, c, o->description);
2018-12-22 22:53:19 +01:00
drawText(UI_WIDTH / 2 + 100, y, 24, TA_LEFT, c, "%d / %d", MIN(o->currentValue, o->targetValue), o->targetValue);
2018-02-21 09:16:18 +01:00
drawText(x + w - 20, y, 24, TA_RIGHT, c, status);
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
y += 55;
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
if (oNum < ++i)
{
return;
}
}
2019-06-02 17:13:34 +02:00
2018-12-22 22:53:19 +01:00
drawText(UI_WIDTH / 2, UI_HEIGHT - 80, 24, TA_CENTER, colors.white, app.strings[ST_PRESS_FIRE]);
2019-06-02 17:13:34 +02:00
2018-02-21 09:16:18 +01:00
canContinue = 1;
}
2019-06-02 17:13:34 +02:00
2018-12-22 22:53:19 +01:00
SDL_SetRenderTarget(app.renderer, app.backBuffer);
2018-02-20 09:14:35 +01:00
}
2018-02-22 08:45:57 +01:00
static int getPostMissionStatus(void)
{
Objective *o;
Entity *e;
int status;
status = MS_COMPLETE;
for (o = world.objectiveHead.next ; o != NULL ; o = o->next)
{
if (o->required && o->currentValue < o->targetValue)
{
return MS_INCOMPLETE;
}
if (o->currentValue < o->totalValue)
{
status = MS_PARTIAL;
}
}
if (status == MS_COMPLETE)
{
for (e = world.entityHead.next ; e != NULL ; e = e->next)
{
2018-03-01 23:30:33 +01:00
if (e->type == ET_HEART || e->type == ET_CELL)
2018-02-22 08:45:57 +01:00
{
return MS_MISSING_HEART_CELL;
}
}
}
return status;
}