// Hyperbolic Rogue -- Achievements // Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details /** \file achievements.cpp * \brief This file implements routines related to achievements and leaderboards * * This routines are general, i.e., not necessarily Steam-specific. */ #include "hyper.h" namespace hr { #define NUMLEADER 85 EX bool test_achievements = false; EX bool offlineMode = false; EX const char* leadernames[NUMLEADER] = { "Score", "Diamonds", "Gold", "Spice", "Rubies", "Elixirs", "Shards100", "Totems", "Daisies", "Statues", "Feathers", "Sapphires", "Hyperstones", "Time to Win-71", "Turns to Win-71", "Time to 10 Hyperstones-113", "Turns to 10 Hyperstones-113", "Orbs of Yendor", "Fern Flowers", "Royal Jellies", "Powerstones", "Silver", "Wine", "Emeralds", "Grimoires", "Holy Grails", "Red Gems", "Pirate Treasures", "Shmup Score", "Shmup Time to Win", "Shmup Knife to Win", "Bomberbird Eggs", // 31 "Ambers", // 32 "Pearls", // 33 "Hypersian Rugs", // 34 "Garnets", // 35 "Princess Challenge", // 36 "Ivory Figurines", // 37 "Elemental Gems", // 38 "Onyxes", // 39 "Yendor Challenge", // 40 "Pure Tactics Mode", // 41 "Mutant Saplings", // 42 "Fulgurites", // 43 "Shmup Score 2p", // 44 "Coop Shmup Time to Win", // 45 "Black Lotuses", // 46 "Mutant Fruits", // 47 "White Dove Feathers", // 48 "Pure Tactics Mode (shmup)", // 49 "Pure Tactics Mode (2p)", // 50 "Corals", // 51 "Thornless Roses", // 52 "Chaos Mode", // 53 "Tortoise points", // 54 "Dragon Scales", // 55 "Apples", // 56 "Heptagonal Mode", // 57 "Sunken Treasures", // 58 "Ancient Jewelry", // 59 "Golden Eggs", // 60 "Multiplayer Score", // 61 "Statistics", // 62 "Halloween", // 63 "Amethysts", // 64 "Slime Molds", // 65 "Dodecahedra", // 66 "Green Grass", // 67 "Spinel", // 68 "Orb Strategy Score", // 69 "Real time to Win-OS", // 70 "Turns to Win-OS", // 71 "Ancient Weapons", // 72 "Forgotten Relics", // 73 "Lava Lilies", // 74 "Turquoises", // 75 "Chrysoberyls", // 76 "Tasty Jellies", // 77 "Tiger's Eyes", // 78 "Torbernites", // 79 "Meteorites", // 80 "Racing Official Track", // 81 "Gold Balls", // 82 "Lazurite Figurines", // 83 "Water Lilies", // 84 }; #define LB_STATISTICS 62 #define LB_HALLOWEEN 63 #define LB_RACING 81 EX bool haveLeaderboard(int id); EX int get_currentscore(int id); EX void set_priority_board(int id); EX int get_sync_status(); EX bool score_loaded(int id); EX int score_default(int id); EX void upload_score(int id, int v); string achievementMessage[3]; int achievementTimer; /** achievements received this game */ EX vector achievementsReceived; /** Returns true if the given achievement cannot be obtained in the current mode. * @param flags Mode requested by the achievement. */ EX bool wrongMode(char flags) { if(cheater) return true; if(casual) return true; if(flags == rg::global) return false; if(flags != rg::special_geometry) { if(!BITRUNCATED) return true; if(geometry != gNormal) return true; } if(shmup::on != (flags == rg::shmup || flags == rg::racing)) return true; if(racing::on != (flags == rg::racing)) return true; if((!!dual::state) != (flags == rg::dualmode)) return true; #if CAP_DAILY if(daily::on != (flags == rg::daily)) return true; #endif if(randomPatternsMode) return true; if(yendor::on) return true; if(peace::on) return true; if(tactic::on) return true; if(dpgen::in) return true; #if CAP_TOUR if(tour::on) return true; #endif if(flags == rg::special_geometry && !ls::single()) return true; if(flags != rg::special_geometry && ineligible_starting_land) return true; if(flags == rg::chaos && !ls::std_chaos()) return true; if(flags != rg::chaos && flags != rg::special_geometry && !ls::nice_walls()) return true; if((numplayers() > 1) != (flags == rg::multi)) return true; return false; } EX set got_achievements; EX void achievement_gain_once(const string& s, char flags IS(0)) { LATE(achievement_gain_once(s, flags)); if(got_achievements.count(s)) return; if(wrongMode(flags)) { if(test_achievements) println(hlog, "wrong mode for achievement: ", s); else return; } got_achievements.insert(s); achievement_gain(s.c_str(), flags); } EX void achievement_log(const char* s, char flags) { if(wrongMode(flags)) { if(test_achievements) println(hlog, "wrong mode for achievement: ", s); return; } if(test_achievements) { println(hlog, "got achievement:", s); return; } for(int i=0; i= 5. * @param prev previous value of the score. */ EX void achievement_count(const string& s, int current, int prev) { if(cheater && !test_achievements) return; if(casual && !test_achievements) return; if(shmup::on) return; if(randomPatternsMode) return; LATE( achievement_count(s, current, prev); ) if(s == "GOLEM" && current >= 5) achievement_gain("GOLEM2"); if(s == "GOLEM" && current >= 10) achievement_gain("GOLEM3"); if(s == "STAB" && current >= 1) achievement_gain("STABBER1"); if(s == "STAB" && current >= 2) achievement_gain("STABBER2"); if(s == "SLASH" && current >= 2) achievement_gain("SLASH2"); if(s == "STAB" && current >= 4) achievement_gain("STABBER3"); if(s == "MIRRORKILL" && current-prev >= 1) achievement_gain("MIRRORKILL1"); if(s == "MIRRORKILL" && current-prev >= 2) achievement_gain("MIRRORKILL2"); if(s == "MIRRORKILL" && current-prev >= 3) achievement_gain("MIRRORKILL3"); if(s == "FLASH" && current-prev >= 1) achievement_gain("FLASH1"); if(s == "FLASH" && current-prev >= 5) achievement_gain("FLASH2"); if(s == "FLASH" && current-prev >= 10) achievement_gain("FLASH3"); if(s == "LIGHTNING" && current-prev >= 1) achievement_gain("LIGHTNING1"); if(s == "LIGHTNING" && current-prev >= 5) achievement_gain("LIGHTNING2"); if(s == "LIGHTNING" && current-prev >= 10) achievement_gain("LIGHTNING3"); if(s == "MIRAGE" && current >= 35 && geometry == gEuclid) achievement_gain("MIRAGE", rg::special_geometry); if(s == "ORB" && current >= 10) achievement_gain("ORB3"); if(s == "BUG" && current >= 1000) achievement_gain("BUG3"); if(s == "ELEC" && current >= 10) achievement_gain("ELEC3"); } int specific_improved = 0; int specific_what = 0; EX void improve_score(int i, eItem what) { if(offlineMode) return; LATE( improve_score(i, what); ) #ifdef HAVE_ACHIEVEMENTS if(haveLeaderboard(i)) updateHi(what, get_currentscore(i)); if(items[what] && haveLeaderboard(i)) { if(items[what] > get_currentscore(i) && score_loaded(i)) { specific_improved++; specific_what = what; } upload_score(i, items[what]); } #endif } // scores for special challenges EX void achievement_score(int cat, int number) { if(offlineMode) return; #ifdef HAVE_ACHIEVEMENTS if(cheater) return; if(casual) return; LATE( achievement_score(cat, number); ) if(cat == LB_HALLOWEEN) { if(geometry != gSphere && geometry != gElliptic) return; } else { if(geometry) return; if(ineligible_starting_land) return; } if(CHANGED_VARIATION) return; if(randomPatternsMode) return; if(dual::state) return; if(shmup::on && cat != LB_PURE_TACTICS_SHMUP && cat != LB_PURE_TACTICS_COOP && cat != LB_RACING) return; if(yendor::on && cat != LB_YENDOR_CHALLENGE) return; if(tactic::on && cat != LB_PURE_TACTICS && cat != LB_PURE_TACTICS_SHMUP && cat != LB_PURE_TACTICS_COOP) return; if(racing::on && cat != LB_RACING) return; upload_score(cat, number); #endif } EX void improveItemScores() { LATE( improveItemScores(); ) for(int i=1; i<=12; i++) improve_score(i, eItem(i)); improve_score(17, itOrbYendor); improve_score(18, itFernFlower); improve_score(19, itRoyalJelly); improve_score(20, itPower); improve_score(21, itSilver); improve_score(22, itWine); improve_score(23, itEmerald); improve_score(24, itGrimoire); improve_score(25, itHolyGrail); improve_score(26, itRedGem); improve_score(27, itPirate); improve_score(31, itBombEgg); improve_score(32, itCoast); improve_score(33, itWhirlpool); improve_score(34, itPalace); improve_score(35, itFjord); improve_score(37, itIvory); improve_score(38, itElemental); improve_score(39, itZebra); improve_score(42, itMutant); improve_score(43, itFulgurite); if(!isHaunted(cwt.at->land)) improve_score(46, itLotus); improve_score(47, itMutant2); improve_score(48, itWindstone); improve_score(51, itCoral); improve_score(52, itRose); improve_score(54, itBabyTortoise); improve_score(55, itDragon); improve_score(56, itApple); improve_score(58, itKraken); improve_score(59, itBarrow); improve_score(60, itTrollEgg); improve_score(64, itAmethyst); improve_score(65, itSlime); improve_score(66, itDodeca); improve_score(67, itGreenGrass); improve_score(68, itBull); improve_score(72, itTerra); improve_score(73, itBlizzard); improve_score(74, itLavaLily); improve_score(75, itHunting); improve_score(76, itRuins); improve_score(77, itSwitch); improve_score(78, itBrownian); improve_score(79, itVarTreasure); improve_score(80, itWest); improve_score(82, itFrog); improve_score(83, itEclectic); improve_score(84, itWet); } int next_stat_tick; /** gain the final achievements. * @param really false: the user is simply looking at the score; true: the game really ended. */ EX void achievement_final(bool really_final) { if(offlineMode) return; LATE( achievement_final(really_final); ) #ifdef HAVE_ACHIEVEMENTS if(ticks > next_stat_tick) { upload_score(LB_STATISTICS, time(NULL)); next_stat_tick = ticks + 600000; } if(cheater) return; if(casual) return; #if CAP_TOUR if(tour::on) return; #endif if(randomPatternsMode) return; if(peace::on) return; if(yendor::on) return; if(dual::state) return; if(tactic::on) { tactic::record(); tactic::unrecord(); tactic::uploadScore(); return; } #if CAP_DAILY if(daily::on) { daily::uploadscore(really_final); return; } #endif int specialcode = 0; if(shmup::on) specialcode++; if(ls::std_chaos()) specialcode+=2; else if(!ls::nice_walls()) return; if(PURE) specialcode+=4; if(numplayers() > 1) specialcode+=8; if(inv::on) specialcode+=16; if(sphere && specialland == laHalloween) { if(specialcode) return; achievement_score(LB_HALLOWEEN, items[itTreat]); return; } if(ineligible_starting_land) return; if(geometry) return; if(NONSTDVAR) return; // determine the correct leaderboard ID for 'total score' // or return if no leaderboard for the current mode int sid; switch(specialcode) { case 0: sid = 0; break; case 1: sid = 28; break; case 2: sid = 53; break; case 4: sid = 57; break; case 8: sid = 61; break; case 9: sid = 44; break; case 16: sid = 69; break; default: return; } int total_improved = 0; specific_improved = 0; specific_what = 0; if(specialcode == 0) improveItemScores(); int tg = gold(); if(tg && haveLeaderboard(sid)) { if(tg > get_currentscore(sid) && score_loaded(sid)) { if(get_currentscore(sid) <= 0) total_improved += 2; total_improved++; // currentscore[sid] = tg; } upload_score(sid, tg); } if(total_improved >= 2) { #if !ISANDROID addMessage(XLAT("Your total treasure has been recorded in the " LEADERFULL ".")); addMessage(XLAT("Congratulations!")); #endif } else if(total_improved && specific_improved >= 2) addMessage(XLAT("You have improved your total high score and %1 specific high scores!", its(specific_improved))); else if(total_improved && specific_improved) addMessage(XLAT("You have improved your total and '%1' high score!", iinf[specific_what].name)); else if(total_improved) { #if !ISANDROID addMessage(XLAT("You have improved your total high score on " LEADER ". Congratulations!")); #endif } else if(specific_improved >= 2) addMessage(XLAT("You have improved %1 of your specific high scores!", its(specific_improved))); else if(specific_improved) { #if !ISANDROID addMessage(XLAT("You have improved your '%1' high score on " LEADER "!", iinf[specific_what].name)); #endif } #endif } EX bool hadtotalvictory; EX void check_total_victory() { if(!inv::on) return; if(hadtotalvictory) return; if(!items[itOrbYendor]) return; if(!items[itHolyGrail]) return; if(items[itHyperstone] < 50) return; if(!princess::reviveAt) return; LATE( check_total_victory(); ) hadtotalvictory = true; achievement_gain("TOTALVICTORY"); } /** gain the victory achievements. * @param hyper true for the Hyperstone victory, and false for the Orb of Yendor victory. */ EX void achievement_victory(bool hyper) { DEBBI(DF_STEAM, ("achievement_victory")) if(offlineMode) return; #ifdef HAVE_ACHIEVEMENTS if(cheater) return; if(casual) return; if(geometry) return; if(CHANGED_VARIATION) return; if(randomPatternsMode) return; if(hyper && shmup::on) return; if(yendor::on) return; if(peace::on) return; if(tactic::on) return; if(!ls::nice_walls()) return; if(!ineligible_starting_land) return; LATE( achievement_victory(hyper); ) DEBB(DF_STEAM, ("after checks")) int t = getgametime(); if(hyper && shmup::on) return; if(hyper && inv::on) return; int ih1 = hyper ? 15 : inv::on ? 70 : shmup::on ? (numplayers() > 1 ? 45 : 29) : 13; int ih2 = hyper ? 16 : inv::on ? 71 : shmup::on ? 30 : 14; int improved = 0; if(score_loaded(ih1) && score_loaded(ih2)) { if(get_currentscore(ih1) == score_default(ih1) || get_currentscore(ih2) == score_default(ih2)) improved += 4; if(get_currentscore(ih1) > t) { improved++; } if(get_currentscore(ih2) > turncount) { improved+=2; } } if(hyper) addMessage(XLAT("You have collected 10 treasures of each type.")); if(improved) { if(improved >= 4) { if(!hyper) addMessage(XLAT("This is your first victory!")); #if !ISANDROID addMessage(XLAT("This has been recorded in the " LEADERFULL ".")); #endif addMessage(XLAT("The faster you get here, the better you are!")); } else if(improved >= 3) { if(shmup::on) addMessage(XLAT("You have improved both your real time and knife count. Congratulations!")); else addMessage(XLAT("You have improved both your real time and turn count. Congratulations!")); } else if(improved == 1) addMessage(XLAT("You have used less real time than ever before. Congratulations!")); else if(improved == 2) { if(shmup::on) addMessage(XLAT("You have used less knives than ever before. Congratulations!")); else addMessage(XLAT("You have used less turns than ever before. Congratulations!")); } } DEBB(DF_STEAM, ("uploading scores")) upload_score(ih1, t); upload_score(ih2, turncount); #endif } /** call the achievement callbacks */ EX void achievement_pump(); EX string get_rich_presence_text() { #if CAP_DAILY if(daily::on) return "Strange Challenge #" + its(daily::daily_id) + ", score " + its(gold()); #endif if(tour::on) return "Guided Tour"; string res; if(geometry != gNormal || !BITRUNCATED) res = res + full_geometry_name() + " "; if(ls::any_chaos()) res += land_structure_name(false) + " "; // TODO if(shmup::on) res += "shmup "; if(dual::state) res += "dual "; if(randomPatternsMode) res += "random "; if(inv::on) res += "OSM "; if(multi::players > 1) res += "multi "; if(casual) res += "casual "; if(cheater || among(cwt.at->land, laCanvas, laCA)) return res + "(?)"; if(yendor::on) { res += "Yendor Challenge: " + yendor::name(yendor::challenge); if(items[itOrbYendor]) res += " (level " + its(items[itOrbYendor]) + ")"; return res; } if(peace::on) return res + "peaceful"; if(tactic::on) return res + "PTM: " + linf[specialland].name + " $" + its(gold()); if(princess::challenge) return res + "Princess Challenge"; #if CAP_RACING if(racing::on) { using namespace racing; res = res + "racing in " + linf[specialland].name; for(int i=0; iland].name; res += ", " + its(gold()) + " $$$"; return res; } #ifndef HAVE_ACHIEVEMENTS void achievement_pump() {} #endif /** display the last achievement gained. */ EX void achievement_display() { #ifdef HAVE_ACHIEVEMENTS if(achievementTimer) { int col = (ticks - achievementTimer); if(col > 5000) { achievementTimer = 0; return; } if(col > 2500) col = 5000 - col; col /= 10; col *= 0x10101; displayfr(vid.xres/2, vid.yres/4, 2, vid.fsize * 2, achievementMessage[0], col & 0xFFFF00, 8); int w = 2 * vid.fsize; #if !ISMOBILE while(w>3 && textwidth(w, achievementMessage[1]) > vid.xres) w--; #endif displayfr(vid.xres/2, vid.yres/4 + vid.fsize*2, 2, w, achievementMessage[1], col, 8); w = vid.fsize; #if !ISMOBILE while(w>3 && textwidth(w, achievementMessage[2]) > vid.xres) w--; #endif displayfr(vid.xres/2, vid.yres/4 + vid.fsize*4, 2, w, achievementMessage[2], col, 8); } #endif } EX bool isAscending(int i) { return i == 13 || i == 14 || i == 15 || i == 16 || i == 29 || i == 30 || i == 45; } EX int score_default(int i) { if(isAscending(i)) return 1999999999; else return 0; } #ifndef HAVE_ACHIEVEMENTS EX int get_sync_status() { return 0; } EX void set_priority_board(int) { } #endif }