Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use system time for holiday menu screens #39897

Merged
merged 22 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1bbfd5d
Merge pull request #1 from CleverRaven/master
Drewscriver Apr 18, 2020
37c8467
Balance crafting time for dilute fruit juice recipe
Drewscriver Apr 18, 2020
29f3218
Merge pull request #2 from CleverRaven/master
Drewscriver Apr 19, 2020
05ca3c1
Update main menu code to automatically display holiday screen based o…
Drewscriver Apr 25, 2020
ec81c56
Merge pull request #3 from CleverRaven/master
Drewscriver Apr 25, 2020
77beb84
Update .gitignore
Drewscriver Apr 25, 2020
1db4830
Update main_menu.cpp
Drewscriver Apr 25, 2020
2018990
Update main_menu.cpp
Drewscriver Apr 25, 2020
f97357d
Update src/main_menu.cpp
Drewscriver Apr 25, 2020
f936804
Merge pull request #4 from CleverRaven/master
Drewscriver Apr 25, 2020
752ce4b
Updated with clearer easter algorithm and better organization and doc…
Drewscriver Apr 26, 2020
def4571
Astyle formatting
Drewscriver Apr 26, 2020
a26f56e
Simplified code and fixed linux compilation errors, credit to ifreund…
Drewscriver Apr 26, 2020
91457a0
Remove #include <time.h> to appease Clang-tidy
Drewscriver Apr 26, 2020
652b95d
Changed comment formatting. Adjusted Halloween and Christmas display…
Drewscriver Apr 27, 2020
23d27f8
made a table
Drewscriver Apr 29, 2020
6cf5f6d
Update src/main_menu.cpp
Drewscriver Apr 29, 2020
55c302c
Update src/main_menu.cpp
Drewscriver Apr 29, 2020
e59a7b4
Update main_menu.h
Drewscriver Apr 29, 2020
529111e
Remove redundant parentheses
ZhilkinSerg Apr 29, 2020
d4b95a6
Apply suggestions from code review
ZhilkinSerg Apr 29, 2020
c299863
Apply suggestions from code review
ZhilkinSerg Apr 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 87 additions & 2 deletions src/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <functional>
#include <istream>
#include <memory>
#include <ctime>

#include "auto_pickup.h"
#include "avatar.h"
Expand Down Expand Up @@ -44,8 +45,6 @@
#include "wcwidth.h"
#include "worldfactory.h"

static const holiday current_holiday = holiday::none;

void main_menu::on_move() const
{
sfx::play_variant_sound( "menu_move", "default", 100 );
Expand Down Expand Up @@ -243,6 +242,89 @@ std::vector<std::string> main_menu::load_file( const std::string &path,
return result;
}

/* compare against table of easter dates */
bool main_menu::is_easter( int day, int month, int year )
{
if( month == 3 ) {
switch( year ) {
// *INDENT-OFF*
case 2024: return day == 31;
case 2027: return day == 28;
default: break;
// *INDENT-ON*
}
} else if( month == 4 ) {
switch( year ) {
// *INDENT-OFF*
case 2021: return day == 4;
case 2022: return day == 17;
case 2023: return day == 9;
case 2025: return day == 20;
case 2026: return day == 5
ZhilkinSerg marked this conversation as resolved.
Show resolved Hide resolved
case 2028: return day == 16;
case 2029: return day == 1;
case 2030: return day == 21;
default: break;
// *INDENT-ON*
}
}
// in practice, this should not be reached
ZhilkinSerg marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

holiday main_menu::get_holiday_from_time()
{
bool success = false;

std::tm local_time;
std::time_t current_time = std::time( nullptr );

/* necessary to pass LGTM, as threadsafe version of localtime differs by platform */
#if defined(_WIN32)

errno_t err = localtime_s( &local_time, &current_time );
if( err == 0 ) {
success = true;
}

#else

success = !!localtime_r( &current_time, &local_time );

#endif

if( success ) {

const int month = local_time.tm_mon + 1;
const int day = local_time.tm_mday;
const int wday = local_time.tm_wday;
const int year = local_time.tm_year + 1900;

/* check date against holidays */
if( month == 1 && day == 1 ) {
return holiday::new_year;
}
// only run easter date calculation if currently March or April
else if( ( month == 3 || month == 4 ) && is_easter( day, month, year ) ) {
return holiday::easter;
} else if( month == 7 && day == 4 ) {
return holiday::independence_day;
}
// 13 days seems appropriate for Halloween
else if( month == 10 && day >= 19 ) {
return holiday::halloween;
} else if( month == 11 && ( day >= 22 && day <= 28 ) && wday == 4 ) {
return holiday::thanksgiving;
}
// For the 12 days of Christmas, my true love gave to me...
else if( month == 12 && ( day >= 14 && day <= 25 ) ) {
return holiday::christmas;
}
}
// fall through to here if localtime fails, or none of the day tests hit
return holiday::none;
}

void main_menu::init_windows()
{
if( LAST_TERM == point( TERMX, TERMY ) ) {
Expand Down Expand Up @@ -395,6 +477,9 @@ void main_menu::load_char_templates()

bool main_menu::opening_screen()
{
// set holiday based on local system time
current_holiday = get_holiday_from_time();

// Play title music, whoo!
play_music( "title" );

Expand Down
7 changes: 7 additions & 0 deletions src/main_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "input.h"
#include "point.h"
#include "worldfactory.h"
#include "enums.h"

class main_menu
{
Expand Down Expand Up @@ -104,6 +105,12 @@ class main_menu

void init_windows();

/* holiday functions and member variables*/
static bool is_easter( int day, int month, int year );
holiday get_holiday_from_time();

holiday current_holiday = holiday::none;

static std::string halloween_spider();
std::string halloween_graves();
};
Expand Down