Skip to content

Commit

Permalink
libcommon: Cleaned up logic for suggesting of user savegame descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Mar 25, 2014
1 parent 693b91a commit 9ce9770
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 61 deletions.
15 changes: 9 additions & 6 deletions doomsday/plugins/common/include/g_common.h
Expand Up @@ -149,11 +149,6 @@ uint G_LogicalMapNumber(uint episode, uint map);
/// @return Logical map number.
uint G_CurrentLogicalMapNumber(void);

/**
* Automatically generate a useful savegame description (map name, map time, etc...).
*/
AutoStr *G_GenerateUserSaveDescription(void);

int G_Ruleset_Skill();
#if !__JHEXEN__
byte G_Ruleset_Fast();
Expand Down Expand Up @@ -231,7 +226,15 @@ bool G_SaveSession(de::String slotId, de::String *userDescription = 0);
*/
bool G_LoadSession(de::String slotId);

uint G_GenerateSessionId(void);
/**
* Chooses a default user description for a saved session.
*
* @param slotId Unique identifier of a saved slot from which the existing description should
* be re-used. Use a zero-length string to disable.
* @param autogenerate @c true= generate a useful description (map name, map time, etc...) if none
* exists for the referenced save @a slotId.
*/
de::String G_DefaultSavedSessionUserDescription(de::String const &slotId, bool autogenerate = true);

/**
* Configures @a metadata according to the current game session configuration.
Expand Down
84 changes: 34 additions & 50 deletions doomsday/plugins/common/src/g_game.cpp
Expand Up @@ -2894,11 +2894,7 @@ static void restorePlayersInHub(playerbackup_t playerBackup[MAXPLAYERS], uint ma
}
#endif // __JHEXEN__

/**
* @param slotId Unique identifier of a saved slot from which the existing description should
* be re-used. Use a zero-length string to disable.
*/
static de::String defaultSavedSessionUserDescription(de::String const &slotId)
de::String G_DefaultSavedSessionUserDescription(de::String const &slotId, bool autogenerate)
{
// If the slot is already in use then choose existing description.
if(G_SaveSlots().has(slotId))
Expand All @@ -2910,13 +2906,40 @@ static de::String defaultSavedSessionUserDescription(de::String const &slotId)
}
}

/// Autogenerate a suitable description. @todo Why is this optional?
if(gaSaveSessionGenerateDescription)
if(!autogenerate) return "";

// Autogenerate a suitable description.
de::String description;

// Include the source file name, for custom maps.
AutoStr const *mapPath = Uri_Compose(gameMapUri);
if(P_MapIsCustom(Str_Text(mapPath)))
{
return Str_Text(G_GenerateUserSaveDescription());
de::String const &mapSourcePath = de::String(Str_Text(P_MapSourceFile(Str_Text(mapPath))));
description += mapSourcePath.fileNameWithoutExtension() + ":";
}

return "";
// Include the map title.
de::String mapTitle = de::String(P_MapTitle(0/*current map*/));
// No map title? Use the identifier. (Some tricksy modders provide us with an empty title).
/// @todo Move this logic engine-side.
if(mapTitle.isEmpty() || mapTitle.at(0) == ' ')
{
mapTitle = Str_Text(mapPath);
}
description += mapTitle;

// Include the game time also.
int time = mapTime / TICRATE;
int const hours = time / 3600; time -= hours * 3600;
int const minutes = time / 60; time -= minutes * 60;
int const seconds = time;
description += de::String("%1:%2:%3")
.arg(hours, 2, 10, QChar('0'))
.arg(minutes, 2, 10, QChar('0'))
.arg(seconds, 2, 10, QChar('0'));

return description;
}

struct savegamesessionworker_params_t
Expand Down Expand Up @@ -2961,7 +2984,7 @@ static int saveGameSessionWorker(void *context)
}
else // We'll generate a suitable description automatically.
{
metadata.set("userDescription", defaultSavedSessionUserDescription(parm.slotId));
metadata.set("userDescription", G_DefaultSavedSessionUserDescription(parm.slotId));
}

// In networked games the server tells the clients to save also.
Expand Down Expand Up @@ -3354,45 +3377,6 @@ bool G_SaveSession(de::String slotId, de::String *userDescription)
return true;
}

AutoStr *G_GenerateUserSaveDescription()
{
int time = mapTime / TICRATE;

int const hours = time / 3600; time -= hours * 3600;
int const minutes = time / 60; time -= minutes * 60;
int const seconds = time;

AutoStr *mapPath = Uri_Compose(gameMapUri);
char const *mapTitle = P_MapTitle(0/*current map*/);

// Still no map title? Use the identifier.
// Some tricksy modders provide us with an empty title...
/// @todo Move this logic engine-side.
if(!mapTitle || !mapTitle[0] || mapTitle[0] == ' ')
{
mapTitle = Str_Text(mapPath);
}

char baseNameBuf[256];
char const *baseName = 0;
if(P_MapIsCustom(Str_Text(mapPath)))
{
F_ExtractFileBase(baseNameBuf, Str_Text(P_MapSourceFile(Str_Text(mapPath))), 256);
baseName = baseNameBuf;
}

AutoStr *str = AutoStr_New();
Str_Appendf(str, "%s%s%s %02i:%02i:%02i", (baseName? baseName : ""),
(baseName? ":" : ""), mapTitle, hours, minutes, seconds);

return str;
}

uint G_GenerateSessionId()
{
return Timer_RealMilliseconds() + (mapTime << 24);
}

void G_ApplyCurrentSessionMetadata(de::game::SessionMetadata &metadata)
{
metadata.clear();
Expand All @@ -3416,7 +3400,7 @@ void G_ApplyCurrentSessionMetadata(de::game::SessionMetadata &metadata)
metadata.set("players", array); // Takes ownership.
#endif

metadata.set("sessionId", G_GenerateSessionId());
metadata.set("sessionId", uint(Timer_RealMilliseconds() + (mapTime << 24)));
}

/**
Expand Down
8 changes: 3 additions & 5 deletions doomsday/plugins/common/src/hu_menu.cpp
Expand Up @@ -5847,15 +5847,13 @@ int Hu_MenuCvarList(mn_object_t *obj, mn_actionid_t action, void *parameters)
return 0;
}

int Hu_MenuSaveSlotEdit(mn_object_t *obj, mn_actionid_t action, void * /*context*/)
int Hu_MenuSaveSlotEdit(mn_object_t *ob, mn_actionid_t action, void * /*context*/)
{
if(MNA_ACTIVE != action) return 1;

// Are we suggesting a new name?
if(cfg.menuGameSaveSuggestDescription)
{
AutoStr *suggestion = G_GenerateUserSaveDescription();
MNEdit_SetText(obj, MNEDIT_STF_NO_ACTION, Str_Text(suggestion));
de::String const description = G_DefaultSavedSessionUserDescription("" /*don't reuse the existing description*/);
MNEdit_SetText(ob, MNEDIT_STF_NO_ACTION, description.toLatin1().constData());
}
return 0;
}
Expand Down

0 comments on commit 9ce9770

Please sign in to comment.