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

Implement dynamic levels system #178

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
261 changes: 33 additions & 228 deletions applications/dolphin/helpers/dolphin_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,20 @@

#define TAG "DolphinState"

// Function macros helpers
#define REQUIRED_XP(LEVEL) \
(((LEVEL * LEVEL) + LEVEL) / 2 * LEVEL_XP_REQUIREMENT_INCREMENT - \
(LEVEL * LEVEL_XP_REQUIREMENT_INCREMENT))
#define LEVEL_FOR_XP_DIVIDEND(XP) \
(sqrt(LEVEL_XP_REQUIREMENT_INCREMENT * ((2 * XP) + (LEVEL_XP_REQUIREMENT_INCREMENT / 4))) + \
(LEVEL_XP_REQUIREMENT_INCREMENT / 2))

#define DOLPHIN_STATE_PATH INT_PATH(DOLPHIN_STATE_FILE_NAME)
#define DOLPHIN_STATE_HEADER_MAGIC 0xD0
#define DOLPHIN_STATE_HEADER_VERSION 0x01
#define LEVEL2_THRESHOLD 450
#define LEVEL3_THRESHOLD 700
#define LEVEL4_THRESHOLD 1100
#define LEVEL5_THRESHOLD 1800
#define LEVEL6_THRESHOLD 2300
#define LEVEL7_THRESHOLD 2900
#define LEVEL8_THRESHOLD 3900
#define LEVEL9_THRESHOLD 5000
#define LEVEL10_THRESHOLD 5900
#define LEVEL11_THRESHOLD 7200
#define LEVEL12_THRESHOLD 8400
#define LEVEL13_THRESHOLD 10000
#define LEVEL14_THRESHOLD 11500
#define LEVEL15_THRESHOLD 13000
#define LEVEL16_THRESHOLD 15000
#define LEVEL17_THRESHOLD 18000
#define LEVEL18_THRESHOLD 20000
#define LEVEL19_THRESHOLD 22000
#define LEVEL20_THRESHOLD 25000
#define LEVEL21_THRESHOLD 33000
#define LEVEL22_THRESHOLD 41000
#define LEVEL23_THRESHOLD 50000
#define LEVEL24_THRESHOLD 62000
#define LEVEL25_THRESHOLD 75000
#define LEVEL26_THRESHOLD 90000
#define LEVEL27_THRESHOLD 105000
#define LEVEL28_THRESHOLD 120000
#define LEVEL29_THRESHOLD 135000
#define LEVEL30_THRESHOLD 155000
#define LEVEL_XP_REQUIREMENT_INCREMENT 3
#define LEVEL_MAX 255
#define LEVEL_XP_MAX REQUIRED_XP(LEVEL_MAX)
#define BUTTHURT_MAX 14
#define BUTTHURT_MIN 0

Expand Down Expand Up @@ -107,217 +89,40 @@ uint64_t dolphin_state_timestamp() {
}

bool dolphin_state_is_levelup(uint32_t icounter) {
return (icounter == LEVEL2_THRESHOLD) || (icounter == LEVEL3_THRESHOLD) ||
(icounter == LEVEL4_THRESHOLD) || (icounter == LEVEL5_THRESHOLD) ||
(icounter == LEVEL6_THRESHOLD) || (icounter == LEVEL7_THRESHOLD) ||
(icounter == LEVEL8_THRESHOLD) || (icounter == LEVEL9_THRESHOLD) ||
(icounter == LEVEL10_THRESHOLD) || (icounter == LEVEL11_THRESHOLD) ||
(icounter == LEVEL12_THRESHOLD) || (icounter == LEVEL13_THRESHOLD) ||
(icounter == LEVEL14_THRESHOLD) || (icounter == LEVEL15_THRESHOLD) ||
(icounter == LEVEL16_THRESHOLD) || (icounter == LEVEL17_THRESHOLD) ||
(icounter == LEVEL18_THRESHOLD) || (icounter == LEVEL19_THRESHOLD) ||
(icounter == LEVEL20_THRESHOLD) || (icounter == LEVEL21_THRESHOLD) ||
(icounter == LEVEL22_THRESHOLD) || (icounter == LEVEL23_THRESHOLD) ||
(icounter == LEVEL24_THRESHOLD) || (icounter == LEVEL25_THRESHOLD) ||
(icounter == LEVEL26_THRESHOLD) || (icounter == LEVEL27_THRESHOLD) ||
(icounter == LEVEL28_THRESHOLD) || (icounter == LEVEL29_THRESHOLD) ||
(icounter == LEVEL30_THRESHOLD);
if(icounter > LEVEL_XP_MAX) {
return false;
}

return ((uint32_t)LEVEL_FOR_XP_DIVIDEND(icounter) % LEVEL_XP_REQUIREMENT_INCREMENT) == 0;
}

uint8_t dolphin_get_level(uint32_t icounter) {
if(icounter <= LEVEL2_THRESHOLD) {
return 1;
} else if(icounter <= LEVEL3_THRESHOLD) {
return 2;
} else if(icounter <= LEVEL4_THRESHOLD) {
return 3;
} else if(icounter <= LEVEL5_THRESHOLD) {
return 4;
} else if(icounter <= LEVEL6_THRESHOLD) {
return 5;
} else if(icounter <= LEVEL7_THRESHOLD) {
return 6;
} else if(icounter <= LEVEL8_THRESHOLD) {
return 7;
} else if(icounter <= LEVEL9_THRESHOLD) {
return 8;
} else if(icounter <= LEVEL10_THRESHOLD) {
return 9;
} else if(icounter <= LEVEL11_THRESHOLD) {
return 10;
} else if(icounter <= LEVEL12_THRESHOLD) {
return 11;
} else if(icounter <= LEVEL13_THRESHOLD) {
return 12;
} else if(icounter <= LEVEL14_THRESHOLD) {
return 13;
} else if(icounter <= LEVEL15_THRESHOLD) {
return 14;
} else if(icounter <= LEVEL16_THRESHOLD) {
return 15;
} else if(icounter <= LEVEL17_THRESHOLD) {
return 16;
} else if(icounter <= LEVEL18_THRESHOLD) {
return 16;
} else if(icounter <= LEVEL19_THRESHOLD) {
return 18;
} else if(icounter <= LEVEL20_THRESHOLD) {
return 19;
} else if(icounter <= LEVEL21_THRESHOLD) {
return 20;
} else if(icounter <= LEVEL22_THRESHOLD) {
return 21;
} else if(icounter <= LEVEL23_THRESHOLD) {
return 22;
} else if(icounter <= LEVEL24_THRESHOLD) {
return 23;
} else if(icounter <= LEVEL25_THRESHOLD) {
return 24;
} else if(icounter <= LEVEL26_THRESHOLD) {
return 25;
} else if(icounter <= LEVEL27_THRESHOLD) {
return 26;
} else if(icounter <= LEVEL28_THRESHOLD) {
return 27;
} else if(icounter <= LEVEL29_THRESHOLD) {
return 28;
} else if(icounter <= LEVEL30_THRESHOLD) {
return 29;
} else {
return 30;
if(icounter >= LEVEL_XP_MAX) {
return LEVEL_MAX;
}

return LEVEL_FOR_XP_DIVIDEND(icounter) / LEVEL_XP_REQUIREMENT_INCREMENT;
}

uint32_t dolphin_state_xp_above_last_levelup(uint32_t icounter) {
uint32_t threshold = 0;
if(icounter <= LEVEL2_THRESHOLD) {
threshold = 0;
} else if(icounter <= LEVEL3_THRESHOLD) {
threshold = LEVEL2_THRESHOLD + 1;
} else if(icounter <= LEVEL4_THRESHOLD) {
threshold = LEVEL3_THRESHOLD + 1;
} else if(icounter <= LEVEL5_THRESHOLD) {
threshold = LEVEL4_THRESHOLD + 1;
} else if(icounter <= LEVEL6_THRESHOLD) {
threshold = LEVEL5_THRESHOLD + 1;
} else if(icounter <= LEVEL7_THRESHOLD) {
threshold = LEVEL6_THRESHOLD + 1;
} else if(icounter <= LEVEL8_THRESHOLD) {
threshold = LEVEL7_THRESHOLD + 1;
} else if(icounter <= LEVEL9_THRESHOLD) {
threshold = LEVEL8_THRESHOLD + 1;
} else if(icounter <= LEVEL10_THRESHOLD) {
threshold = LEVEL9_THRESHOLD + 1;
} else if(icounter <= LEVEL11_THRESHOLD) {
threshold = LEVEL10_THRESHOLD + 1;
} else if(icounter <= LEVEL12_THRESHOLD) {
threshold = LEVEL11_THRESHOLD + 1;
} else if(icounter <= LEVEL13_THRESHOLD) {
threshold = LEVEL12_THRESHOLD + 1;
} else if(icounter <= LEVEL14_THRESHOLD) {
threshold = LEVEL13_THRESHOLD + 1;
} else if(icounter <= LEVEL15_THRESHOLD) {
threshold = LEVEL14_THRESHOLD + 1;
} else if(icounter <= LEVEL16_THRESHOLD) {
threshold = LEVEL15_THRESHOLD + 1;
} else if(icounter <= LEVEL17_THRESHOLD) {
threshold = LEVEL16_THRESHOLD + 1;
} else if(icounter <= LEVEL18_THRESHOLD) {
threshold = LEVEL17_THRESHOLD + 1;
} else if(icounter <= LEVEL19_THRESHOLD) {
threshold = LEVEL18_THRESHOLD + 1;
} else if(icounter <= LEVEL20_THRESHOLD) {
threshold = LEVEL19_THRESHOLD + 1;
} else if(icounter <= LEVEL21_THRESHOLD) {
threshold = LEVEL20_THRESHOLD + 1;
} else if(icounter <= LEVEL22_THRESHOLD) {
threshold = LEVEL21_THRESHOLD + 1;
} else if(icounter <= LEVEL23_THRESHOLD) {
threshold = LEVEL22_THRESHOLD + 1;
} else if(icounter <= LEVEL24_THRESHOLD) {
threshold = LEVEL23_THRESHOLD + 1;
} else if(icounter <= LEVEL25_THRESHOLD) {
threshold = LEVEL24_THRESHOLD + 1;
} else if(icounter <= LEVEL26_THRESHOLD) {
threshold = LEVEL25_THRESHOLD + 1;
} else if(icounter <= LEVEL27_THRESHOLD) {
threshold = LEVEL26_THRESHOLD + 1;
} else if(icounter <= LEVEL28_THRESHOLD) {
threshold = LEVEL27_THRESHOLD + 1;
} else if(icounter <= LEVEL29_THRESHOLD) {
threshold = LEVEL28_THRESHOLD + 1;
} else if(icounter <= LEVEL30_THRESHOLD) {
threshold = LEVEL29_THRESHOLD + 1;
} else {
threshold = LEVEL30_THRESHOLD + 1;
if(icounter >= LEVEL_XP_MAX) {
return 0;
}
return icounter - threshold;

uint8_t level = dolphin_get_level(icounter);
uint8_t level_threshold = REQUIRED_XP(level);
return icounter - (level_threshold + 1);
}

uint32_t dolphin_state_xp_to_levelup(uint32_t icounter) {
uint32_t threshold = 0;
if(icounter <= LEVEL2_THRESHOLD) {
threshold = LEVEL2_THRESHOLD;
} else if(icounter <= LEVEL3_THRESHOLD) {
threshold = LEVEL3_THRESHOLD;
} else if(icounter <= LEVEL4_THRESHOLD) {
threshold = LEVEL4_THRESHOLD;
} else if(icounter <= LEVEL5_THRESHOLD) {
threshold = LEVEL5_THRESHOLD;
} else if(icounter <= LEVEL6_THRESHOLD) {
threshold = LEVEL6_THRESHOLD;
} else if(icounter <= LEVEL7_THRESHOLD) {
threshold = LEVEL7_THRESHOLD;
} else if(icounter <= LEVEL8_THRESHOLD) {
threshold = LEVEL8_THRESHOLD;
} else if(icounter <= LEVEL9_THRESHOLD) {
threshold = LEVEL9_THRESHOLD;
} else if(icounter <= LEVEL10_THRESHOLD) {
threshold = LEVEL10_THRESHOLD;
} else if(icounter <= LEVEL11_THRESHOLD) {
threshold = LEVEL11_THRESHOLD;
} else if(icounter <= LEVEL12_THRESHOLD) {
threshold = LEVEL12_THRESHOLD;
} else if(icounter <= LEVEL13_THRESHOLD) {
threshold = LEVEL13_THRESHOLD;
} else if(icounter <= LEVEL14_THRESHOLD) {
threshold = LEVEL14_THRESHOLD;
} else if(icounter <= LEVEL15_THRESHOLD) {
threshold = LEVEL15_THRESHOLD;
} else if(icounter <= LEVEL16_THRESHOLD) {
threshold = LEVEL16_THRESHOLD;
} else if(icounter <= LEVEL17_THRESHOLD) {
threshold = LEVEL17_THRESHOLD;
} else if(icounter <= LEVEL18_THRESHOLD) {
threshold = LEVEL18_THRESHOLD;
} else if(icounter <= LEVEL19_THRESHOLD) {
threshold = LEVEL19_THRESHOLD;
} else if(icounter <= LEVEL20_THRESHOLD) {
threshold = LEVEL20_THRESHOLD;
} else if(icounter <= LEVEL21_THRESHOLD) {
threshold = LEVEL21_THRESHOLD;
} else if(icounter <= LEVEL22_THRESHOLD) {
threshold = LEVEL22_THRESHOLD;
} else if(icounter <= LEVEL23_THRESHOLD) {
threshold = LEVEL23_THRESHOLD;
} else if(icounter <= LEVEL24_THRESHOLD) {
threshold = LEVEL24_THRESHOLD;
} else if(icounter <= LEVEL25_THRESHOLD) {
threshold = LEVEL25_THRESHOLD;
} else if(icounter <= LEVEL26_THRESHOLD) {
threshold = LEVEL26_THRESHOLD;
} else if(icounter <= LEVEL27_THRESHOLD) {
threshold = LEVEL27_THRESHOLD;
} else if(icounter <= LEVEL28_THRESHOLD) {
threshold = LEVEL28_THRESHOLD;
} else if(icounter <= LEVEL29_THRESHOLD) {
threshold = LEVEL29_THRESHOLD;
} else if(icounter <= LEVEL30_THRESHOLD) {
threshold = LEVEL30_THRESHOLD;
} else {
threshold = (uint32_t)-1;
if(icounter >= LEVEL_XP_MAX) {
return 0;
}
return threshold - icounter;

uint8_t level = dolphin_get_level(icounter);
uint8_t next_level = level + 1;
uint8_t next_level_threshold = REQUIRED_XP(next_level);
return next_level_threshold - icounter;
}

void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
Expand Down
Loading