Skip to content

Commit

Permalink
Add Mission Complete message (fixes #198)
Browse files Browse the repository at this point in the history
Fix autosave LastMission bug
- LastMission was always the last autosave
in the file
Change game messages to be cyan and under the automap
  • Loading branch information
cxong committed Dec 10, 2013
1 parent 62fca63 commit ea3ac38
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 23 deletions.
5 changes: 4 additions & 1 deletion src/autosave.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,16 @@ void AutosaveLoad(Autosave *autosave, const char *filename)
printf("Error parsing autosave '%s'\n", filename);
goto bail;
}
// Note: need to load missions before LastMission because the former
// will overwrite the latter, since AutosaveAddMission also
// writes to LastMission
LoadMissionNodes(autosave, root, "Missions");
if (json_find_first_label(root, "LastMission"))
{
LoadMissionNode(
&autosave->LastMission,
json_find_first_label(root, "LastMission")->child);
}
LoadMissionNodes(autosave, root, "Missions");

bail:
json_free_value(&root);
Expand Down
10 changes: 7 additions & 3 deletions src/cdogs/actors.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@ static void PickupObject(TActor * actor, TObject * object)
break;
*/
}
CheckMissionObjective(object->tileItem.flags, OBJECTIVE_COLLECT);
CheckMissionObjective(
&gMission, object->tileItem.flags, OBJECTIVE_COLLECT);
RemoveObject(object);
SoundPlayAt(
&gSoundDevice,
Expand Down Expand Up @@ -588,7 +589,9 @@ int MoveActor(TActor * actor, int x, int y)
0) {
otherCharacter->flags &= ~FLAGS_PRISONER;
CheckMissionObjective(
otherCharacter->tileItem.flags, OBJECTIVE_RESCUE);
&gMission,
otherCharacter->tileItem.flags,
OBJECTIVE_RESCUE);
}
}

Expand Down Expand Up @@ -687,7 +690,8 @@ void InjureActor(TActor * actor, int injury)
SND_HAHAHA,
Vec2iNew(actor->tileItem.x, actor->tileItem.y));
}
CheckMissionObjective(actor->tileItem.flags, OBJECTIVE_KILL);
CheckMissionObjective(
&gMission, actor->tileItem.flags, OBJECTIVE_KILL);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/cdogs/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ color_t colorDarker = { 192, 192, 192, 255 };
color_t colorPurple = { 192, 0, 192, 255 };
color_t colorGray = { 128, 128, 128, 255 };
color_t colorYellow = { 255, 255, 128, 255 };
color_t colorCyan = { 0, 255, 255, 255 };

color_t ColorMult(color_t c, color_t m)
{
Expand Down
1 change: 1 addition & 0 deletions src/cdogs/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extern color_t colorDarker;
extern color_t colorPurple;
extern color_t colorGray;
extern color_t colorYellow;
extern color_t colorCyan;

color_t ColorMult(color_t c, color_t m);
color_t ColorAlphaBlend(color_t a, color_t b);
Expand Down
8 changes: 7 additions & 1 deletion src/cdogs/game_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@

typedef enum
{
GAME_EVENT_SCREEN_SHAKE
GAME_EVENT_SCREEN_SHAKE,
GAME_EVENT_SET_MESSAGE
} GameEventType;

typedef struct
Expand All @@ -44,6 +45,11 @@ typedef struct
union
{
int ShakeAmount;
struct
{
char Message[256];
int Ticks;
} SetMessage;
} u;
} GameEvent;

Expand Down
22 changes: 15 additions & 7 deletions src/cdogs/hud.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,21 @@ void HUDInit(
WallClockInit(&hud->clock);
}

void HUDDisplayMessage(HUD *hud, const char *msg)
void HUDDisplayMessage(HUD *hud, const char *msg, int ticks)
{
strcpy(hud->message, msg);
hud->messageTicks = 140;
hud->messageTicks = ticks;
}

void HUDUpdate(HUD *hud, int ms)
{
hud->messageTicks -= ms;
if (hud->messageTicks < 0)
if (hud->messageTicks >= 0)
{
hud->messageTicks = 0;
hud->messageTicks -= ms;
if (hud->messageTicks < 0)
{
hud->messageTicks = 0;
}
}
FPSCounterUpdate(&hud->fpsCounter, ms);
WallClockUpdate(&hud->clock, ms);
Expand Down Expand Up @@ -513,9 +516,14 @@ void HUDDraw(HUD *hud, int isPaused)
CDogsTextStringAtCenter("Press Esc again to quit");
}

if (hud->messageTicks > 0)
if (hud->messageTicks > 0 || hud->messageTicks == -1)
{
CDogsTextStringSpecial(hud->message, TEXT_XCENTER | TEXT_TOP, 0, 20);
// Draw the message centered, and just below the automap
Vec2i pos = Vec2iNew(
(hud->device->cachedConfig.ResolutionWidth -
TextGetStringWidth(hud->message)) / 2,
AUTOMAP_SIZE + AUTOMAP_PADDING + AUTOMAP_PADDING);
DrawTextStringMasked(hud->message, hud->device, pos, colorCyan);
}

if (hud->config->ShowFPS)
Expand Down
5 changes: 4 additions & 1 deletion src/cdogs/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ void HUDInit(
InterfaceConfig *config,
GraphicsDevice *device,
struct MissionOptions *mission);
void HUDDisplayMessage(HUD *hud, const char *msg);

// Set ticks to -1 to display a message indefinitely
void HUDDisplayMessage(HUD *hud, const char *msg, int ticks);

void HUDUpdate(HUD *hud, int ms);
void HUDDraw(HUD *hud, int isPaused);

Expand Down
15 changes: 13 additions & 2 deletions src/cdogs/mission.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
#include <assert.h>
#include <string.h>
#include <stdlib.h>

#include "game_events.h"
#include "gamedata.h"
#include "map.h"
#include "palette.h"
Expand Down Expand Up @@ -906,19 +908,28 @@ void SetPaletteRanges(int wall_range, int floor_range, int room_range, int alt_r
SetRange(ALT_COLORS, abs(alt_range) % COLORRANGE_COUNT);
}

int CheckMissionObjective(int flags, ObjectiveType type)
int CheckMissionObjective(
struct MissionOptions *options, int flags, ObjectiveType type)
{
int idx;
if (!(flags & TILEITEM_OBJECTIVE))
{
return 0;
}
idx = ObjectiveFromTileItem(flags);
if (gMission.missionData->objectives[idx].type != type)
if (options->missionData->objectives[idx].type != type)
{
return 0;
}
gMission.objectives[idx].done++;
if (CanCompleteMission(options))
{
GameEvent msg;
msg.Type = GAME_EVENT_SET_MESSAGE;
strcpy(msg.u.SetMessage.Message, "Mission Complete");
msg.u.SetMessage.Ticks = -1;
GameEventsEnqueue(&gGameEvents, msg);
}
return 1;
}

Expand Down
3 changes: 2 additions & 1 deletion src/cdogs/mission.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ void SetupMission(int index, int buildTables, CampaignOptions *campaign);
void SetPaletteRanges(int wall_range, int floor_range, int room_range, int alt_range);

// If object is a mission objective, complete it and return true
int CheckMissionObjective(int flags, ObjectiveType type);
int CheckMissionObjective(
struct MissionOptions *options, int flags, ObjectiveType type);
int CanCompleteMission(struct MissionOptions *options);
int IsMissionComplete(struct MissionOptions *options);

Expand Down
3 changes: 2 additions & 1 deletion src/cdogs/objs.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ static void DamageObject(
if (object->structure <= 0)
{
object->structure = 0;
if (CheckMissionObjective(object->tileItem.flags, OBJECTIVE_DESTROY))
if (CheckMissionObjective(
&gMission, object->tileItem.flags, OBJECTIVE_DESTROY))
{
if (player >= 0)
{
Expand Down
16 changes: 10 additions & 6 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,16 +625,20 @@ Vec2i GetPlayerCenter(GraphicsDevice *device, int player)
return center;
}

void HandleGameEvents(GameEventStore *store, int *shakeAmount)
void HandleGameEvents(GameEventStore *store, HUD *hud, int *shakeAmount)
{
int i;
for (i = 0; i < store->count; i++)
{
switch (store->events[i].Type)
GameEvent *e = &store->events[i];
switch (e->Type)
{
case GAME_EVENT_SCREEN_SHAKE:
*shakeAmount = GetShakeAmount(
*shakeAmount, store->events[i].u.ShakeAmount);
*shakeAmount = GetShakeAmount(*shakeAmount, e->u.ShakeAmount);
break;
case GAME_EVENT_SET_MESSAGE:
HUDDisplayMessage(
hud, e->u.SetMessage.Message, e->u.SetMessage.Ticks);
break;
default:
assert(0 && "unknown game event");
Expand All @@ -659,7 +663,7 @@ int gameloop(void)

if (MusicGetStatus(&gSoundDevice) != MUSIC_OK)
{
HUDDisplayMessage(&hud, MusicGetErrorMessage(&gSoundDevice));
HUDDisplayMessage(&hud, MusicGetErrorMessage(&gSoundDevice), 140);
}

missionTime = 0;
Expand Down Expand Up @@ -752,7 +756,7 @@ int gameloop(void)
{
if (!gConfig.Game.SlowMotion || (frames & 1) == 0)
{
HandleGameEvents(&gGameEvents, &shakeAmount);
HandleGameEvents(&gGameEvents, &hud, &shakeAmount);

for (i = 0; i < gOptions.numPlayers; i++)
{
Expand Down

0 comments on commit ea3ac38

Please sign in to comment.