Files
isaac-port/src/daily_challenge.cpp
T

294 lines
13 KiB
C++

#include "daily_challenge.h"
#include "entity_player.h"
#include "game.h"
#include "level.h"
#include "log.h"
#include "platform.h"
#include "room.h"
#include "seeds.h"
#include <algorithm>
#include <array>
#include <chrono>
constexpr uint32_t DAILY_SHIFT_INDEX = 64;
constexpr uint32_t SPECIAL_DAILY_GROUNDHOG = 2;
constexpr uint32_t SPECIAL_DAILY_EASTER = 6;
constexpr uint32_t SPECIAL_DAILY_THANKSGIVING = 16;
constexpr uint32_t SPECIAL_DAILY_G_FUEL = 52;
constexpr const char* G_FUEL_SEED = "XRA8 J6GH";
constexpr int NOVEMBER = 11;
constexpr int EASTER_LAST_YEAR = 3100;
constexpr int DAILY_ROLL_RANGE = 100;
constexpr int SUNDAY_HARD_ODDS = 6;
// The characters a daily may draw (by its seed modulo 20, skipping the Lost, Lazarus risen and the Soul), and the
// stages a daily ends on by two bits of the seed.
constexpr int DAILY_CHARACTER_RANGE = 20;
constexpr std::array<int, 3> DAILY_SKIPPED_CHARACTERS = {PLAYER_LAZARUS2, PLAYER_THELOST, PLAYER_THESOUL};
constexpr std::array<int, 4> DAILY_END_STAGES = {STAGE_DEPTHS_II, STAGE_WOMB_II, STAGE_SHEOL, STAGE_DARK_ROOM};
// The bounds of the rolls out of 100 picking a daily's end stage: on most days, past which the Chest is picked, and
// on Saturdays.
constexpr std::array<int, 5> STAGE_ROLLS = {20, 40, 50, 65, 80};
constexpr int CHEST_ROLL = 89;
constexpr std::array<int, 8> SATURDAY_ROLLS = {15, 30, 40, 50, 60, 70, 80, 90};
// The weekdays that change a daily's rules.
enum DailyWeekday : int
{
WEEKDAY_SUNDAY,
WEEKDAY_MONDAY,
WEEKDAY_TUESDAY,
WEEKDAY_WEDNESDAY,
WEEKDAY_THURSDAY,
WEEKDAY_FRIDAY,
WEEKDAY_SATURDAY
};
// A date's special daily, by month and day (MMDD), besides Easter, Thanksgiving and G FUEL's.
struct HolidayDate
{
uint32_t monthDay = 0;
uint32_t specialDailyId = 0;
};
constexpr std::array<HolidayDate, 50> HOLIDAY_DATES = {{
{101, 1}, {114, 20}, {116, 21}, {127, 22}, {202, 2}, {209, 23}, {211, 24}, {214, 3}, {218, 25},
{302, 4}, {304, 26}, {317, 5}, {320, 27}, {322, 28}, {401, 7}, {410, 29}, {415, 30}, {422, 31},
{504, 32}, {508, 8}, {513, 33}, {517, 34}, {530, 9}, {605, 35}, {618, 36}, {619, 10}, {625, 11},
{702, 37}, {704, 12}, {711, 38}, {713, 39}, {815, 40}, {816, 41}, {822, 42}, {905, 43}, {917, 44},
{922, 45}, {1006, 46}, {1007, 47}, {1031, 13}, {1101, 14}, {1104, 52}, {1111, 15}, {1113, 48}, {1119, 49},
{1205, 17}, {1206, 18}, {1223, 50}, {1225, 19}, {1231, 51},
}};
// The dates G FUEL's daily also fell on.
constexpr std::array<uint32_t, 2> G_FUEL_DATES = {20230324, 20230414};
// What a special daily sets: the character, end stage, path, difficulty and Mega Satan (-1 keeps the drawn one), no
// shooting, and the treasure rooms (and shops) it leaves out.
struct SpecialDailyRules
{
uint32_t id = 0;
int playerType = -1;
int endStage = -1;
int pathType = -1;
int difficulty = -1;
int isMegaSatan = -1;
bool isCantShoot = false;
bool isTreasureFiltered = false;
bool isShopFiltered = false;
};
constexpr std::array<SpecialDailyRules, 51> SPECIAL_DAILY_RULES = {{
{1, 8, 11, 0, 0, 0, true}, {3, 1, 8, -1, 0, 0}, {4, 0, 11, -1, 0, 1}, {5, 2, 11, -1, 0, 1},
{6, 8, 10, 1, 0, 0, false, true}, {7, 14, 11, 1, 0, 1}, {8, 0, 8, -1, 0, 0, false, true},
{9, 10, 11, 1, 0, 0, true}, {10, 2, 8, -1, 0, 0, false, true}, {11, 10, 6, -1, 0, 0}, {12, 6, 11, -1, 0, 1},
{13, 5, 10, 0, 0, 0}, {14, 10, 11, 0, 1, 0}, {15, 10, 11, -1, 1, 1, false, true},
{16, 1, 11, -1, 0, 1}, {17, 0, 10, 0, 0, 0}, {18, 0, 10, 1, 0, 0}, {19, 0, 11, -1, 0, 1},
{20, 0, -1, -1, -1, -1, false, true}, {21, 15, -1, -1, -1, -1, false, true, true},
{22, 4, -1, -1, -1, -1, false, true, true}, {23, 2, -1, -1, -1, -1, false, true}, {24, 1}, {25, 0},
{26, 1, -1, -1, -1, -1, false, true}, {27, 0, -1, -1, -1, -1, false, true}, {28, 0}, {29, 13},
{30, 8, -1, -1, -1, -1, false, true}, {31, 1, -1, -1, -1, -1, false, true}, {32, 0},
{33, 1, -1, -1, -1, -1, false, true}, {34, 0}, {35, 4}, {36, 2}, {37, 16},
{38, 4, -1, -1, -1, -1, false, true}, {39, 8, -1, -1, -1, -1, false, true},
{40, 1, -1, -1, -1, -1, false, true}, {41, 14}, {42, 0}, {43, 1}, {44, 2}, {45, 1}, {46, 3}, {47, 0},
{48, 0, -1, -1, -1, -1, false, true}, {49, 4, -1, -1, -1, -1, false, true}, {50, 3}, {51, 0},
{SPECIAL_DAILY_G_FUEL, 0, 11, 1, 0, 0},
}};
// A YYYYMMDD date as a calendar date, normalized as mktime would.
static std::chrono::sys_days date_days_get(uint32_t date)
{
using namespace std::chrono;
year_month_day day = year{(int)(date / DATE_YEAR)} / month{date % DATE_YEAR / DATE_PART} / 1;
return sys_days{day} + days{date % DATE_PART - 1};
}
// A calendar date as YYYYMMDD.
static uint32_t date_make(std::chrono::sys_days days)
{
std::chrono::year_month_day day{days};
return (uint32_t)(int)day.year() * DATE_YEAR + (uint32_t)day.month() * DATE_PART + (uint32_t)day.day();
}
// Easter Sunday of a year by Gauss's computus, or the epoch past the years it covers (logged).
static uint32_t easter_get(int year)
{
using namespace std::chrono;
if (year > EASTER_LAST_YEAR)
{
log_printf(LOG_WARN, "Cannot calculate Easter date for year %d", year);
return date_make(sys_days{});
}
int golden = year % 19;
int century = year / 100;
int yearInCentury = year % 100;
int epact = (19 * golden + century - century / 4 - (century - (century + 8) / 25 + 1) / 3 + 15) % 30;
int weekday = (32 + 2 * (century % 4) + 2 * (yearInCentury / 4) - epact - yearInCentury % 4) % 7;
int correction = (golden + 11 * epact + 22 * weekday) / 451;
int days = epact + weekday - 7 * correction + 114;
return date_make(sys_days{std::chrono::year{year} / (days / 31) / (days % 31 + 1)});
}
// 00427870 The special daily a YYYYMMDD date falls on: Thanksgiving (the fourth Thursday of November), Easter,
// G FUEL's, one of the year's fixed dates, or 0 for none.
uint32_t holiday_get_current(uint32_t date)
{
using namespace std::chrono;
int year = (int)year_month_day{date_days_get(date)}.year();
sys_days novemberFirst{std::chrono::year{year} / NOVEMBER / 1};
sys_days thanksgiving = novemberFirst + (Thursday - weekday{novemberFirst}) + weeks{3};
if (date == date_make(thanksgiving)) return SPECIAL_DAILY_THANKSGIVING;
if (date == easter_get(year)) return SPECIAL_DAILY_EASTER;
if (std::ranges::find(G_FUEL_DATES, date) != G_FUEL_DATES.end()) return SPECIAL_DAILY_G_FUEL;
auto holiday = std::ranges::find(HOLIDAY_DATES, date % DATE_YEAR, &HolidayDate::monthDay);
return holiday == HOLIDAY_DATES.end() ? 0 : holiday->specialDailyId;
}
// A character's tainted self, or the character itself (logged) for those without one.
static int tainted_id_get(int playerType)
{
if (playerType >= PLAYER_ISAAC && playerType <= PLAYER_THELOST) return playerType + PLAYER_ISAAC_B;
switch (playerType)
{
case PLAYER_LILITH: return PLAYER_LILITH_B;
case PLAYER_KEEPER: return PLAYER_KEEPER_B;
case PLAYER_APOLLYON: return PLAYER_APOLLYON_B;
case PLAYER_THEFORGOTTEN: return PLAYER_THEFORGOTTEN_B;
case PLAYER_BETHANY: return PLAYER_BETHANY_B;
case PLAYER_JACOB: return PLAYER_JACOB_B;
}
log_printf(LOG_ERROR, "GetTaintedId() could not find tainted counterpart to given ePlayerType (%d) \n",
playerType);
return playerType;
}
// The end stage and path a roll out of 100 picks: the Depths, the Womb, the Void on either path, Sheol or the
// Cathedral, or the Dark Room or the Chest past 89.
static void daily_stage_roll(ChallengeParam& params, RNG& rng, int roll)
{
if (roll < STAGE_ROLLS[0]) params.endStage = STAGE_DEPTHS_II;
else if (roll < STAGE_ROLLS[1]) params.endStage = STAGE_WOMB_II;
else if (roll < STAGE_ROLLS[2])
{
params.endStage = STAGE_VOID;
params.pathType = rng.RandomInt(2) == 0 ? PATH_TYPE_CATHEDRAL : PATH_TYPE_SHEOL;
}
else if (roll < STAGE_ROLLS[4])
{
params.endStage = STAGE_SHEOL;
params.pathType = roll < STAGE_ROLLS[3] ? PATH_TYPE_SHEOL : PATH_TYPE_CATHEDRAL;
}
else
{
params.endStage = STAGE_DARK_ROOM;
params.pathType = roll > CHEST_ROLL ? PATH_TYPE_CATHEDRAL : PATH_TYPE_SHEOL;
}
}
// Saturday's end stage and path, a roll out of 100 in tenths (the first two halves of fifteen), none past 89.
static void saturday_stage_roll(ChallengeParam& params, RNG& rng, int roll)
{
if (roll < SATURDAY_ROLLS[0]) params.endStage = STAGE_DEPTHS_II;
else if (roll < SATURDAY_ROLLS[1]) params.endStage = STAGE_WOMB_II;
else if (roll < SATURDAY_ROLLS[2])
{
params.endStage = STAGE_VOID;
params.pathType = rng.RandomInt(2) == 0 ? PATH_TYPE_CATHEDRAL : PATH_TYPE_SHEOL;
}
else if (roll < SATURDAY_ROLLS[6])
{
int band = (roll - SATURDAY_ROLLS[2]) / (SATURDAY_ROLLS[3] - SATURDAY_ROLLS[2]);
params.endStage = band < 2 ? STAGE_SHEOL : STAGE_DARK_ROOM;
params.pathType = band % 2 ? PATH_TYPE_CATHEDRAL : PATH_TYPE_SHEOL;
}
else if (roll < SATURDAY_ROLLS[7])
{
params.pathType = PATH_TYPE_HOME;
params.endStage = STAGE_HOME;
}
}
// 00427290 Applies a special daily's rules over the drawn ones: G FUEL's also plays with its seed and G FUEL's effect.
static void special_daily_apply(DailyChallenge& daily)
{
ChallengeParam& params = daily.params;
auto rules = std::ranges::find(SPECIAL_DAILY_RULES, daily.specialDailyId, &SpecialDailyRules::id);
if (rules == SPECIAL_DAILY_RULES.end()) return;
auto apply = [](int& value, int rule) { value = rule >= 0 ? rule : value; };
apply(params.playerType, rules->playerType);
apply(params.endStage, rules->endStage);
apply(params.difficulty, rules->difficulty);
if (rules->pathType >= 0) params.pathType = (PathType)rules->pathType;
if (rules->isMegaSatan >= 0) params.isMegaSatan = rules->isMegaSatan;
if (rules->isCantShoot) params.isCanShoot = false;
if (rules->isTreasureFiltered) params.roomset.insert(ROOM_TREASURE);
if (rules->isShopFiltered) params.roomset.insert(ROOM_SHOP);
if (daily.specialDailyId != SPECIAL_DAILY_G_FUEL) return;
params.seedEffects = {SEED_G_FUEL};
daily.seed = Seeds::String2Seed(G_FUEL_SEED);
}
// 00426a50 Draws a date's daily from it (Groundhog Day repeats the day before): a character (tainted on some
// Tuesdays and Saturdays), an end stage and path, then the weekday's twist (Sunday's Mega Satan, maybe hard, Monday's
// Repentance path to the Womb, Wednesday's and Friday's (hard) rolls, Thursday's Home, Saturday's anything), the run's
// seed, and the special daily's rules over them.
// The original stops in the debugger on a zero seed. Here the seed just stays zero.
void DailyChallenge::Init(uint32_t dailyDate)
{
seed = 0;
isPractice = false;
params.Reset();
params.isPathTypeSet = true;
specialDailyId = holiday_get_current(dailyDate);
id = dailyDate;
RNG rng = rng_make(specialDailyId == SPECIAL_DAILY_GROUNDHOG ? dailyDate - 1 : dailyDate, DAILY_SHIFT_INDEX);
do params.playerType = (int)(rng.Next() % DAILY_CHARACTER_RANGE);
while (std::ranges::find(DAILY_SKIPPED_CHARACTERS, params.playerType) != DAILY_SKIPPED_CHARACTERS.end());
params.endStage = DAILY_END_STAGES[rng.Next() & 3];
params.pathType = params.endStage < STAGE_SHEOL || (rng.Next() & 1) ? PATH_TYPE_CATHEDRAL : PATH_TYPE_SHEOL;
params.isMegaSatan = false;
params.difficulty = DIFFICULTY_NORMAL;
switch (std::chrono::weekday{date_days_get(dailyDate)}.c_encoding())
{
case WEEKDAY_SUNDAY:
params.endStage = STAGE_DARK_ROOM;
params.isMegaSatan = true;
if (rng.Next() % SUNDAY_HARD_ODDS == 0) params.difficulty = DIFFICULTY_HARD;
break;
case WEEKDAY_MONDAY:
params.pathType = PATH_TYPE_REPENTANCE;
params.endStage = STAGE_WOMB_II;
break;
case WEEKDAY_TUESDAY:
params.playerType = tainted_id_get(params.playerType);
daily_stage_roll(params, rng, (int)(rng.Next() % DAILY_ROLL_RANGE));
break;
case WEEKDAY_WEDNESDAY:
params.endStage = STAGE_VOID;
params.pathType = rng.RandomInt(2) == 0 ? PATH_TYPE_CATHEDRAL : PATH_TYPE_SHEOL;
daily_stage_roll(params, rng, (int)rng.RandomInt(DAILY_ROLL_RANGE));
break;
case WEEKDAY_THURSDAY:
params.pathType = PATH_TYPE_HOME;
params.endStage = STAGE_HOME;
break;
case WEEKDAY_FRIDAY:
params.difficulty = DIFFICULTY_HARD;
daily_stage_roll(params, rng, (int)rng.RandomInt(DAILY_ROLL_RANGE));
break;
case WEEKDAY_SATURDAY:
saturday_stage_roll(params, rng, (int)rng.RandomInt(DAILY_ROLL_RANGE));
if (rng.RandomInt(2) != 0) params.playerType = tainted_id_get(params.playerType);
if (rng.RandomInt(2) != 0) params.difficulty = DIFFICULTY_HARD;
break;
}
seed = rng.Next();
special_daily_apply(*this);
}