dolphin_state.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "dolphin_state.h"
  2. #include <stdint.h>
  3. #include <storage/storage.h>
  4. #include <furi.h>
  5. #include <math.h>
  6. #include <toolbox/saved_struct.h>
  7. #define TAG "DolphinState"
  8. #define DOLPHIN_STATE_PATH "/int/dolphin.state"
  9. #define DOLPHIN_STATE_HEADER_MAGIC 0xD0
  10. #define DOLPHIN_STATE_HEADER_VERSION 0x01
  11. #define DOLPHIN_LVL_THRESHOLD 20.0f
  12. #define LEVEL2_THRESHOLD 20
  13. #define LEVEL3_THRESHOLD 100
  14. #define BUTTHURT_MAX 14
  15. #define BUTTHURT_MIN 0
  16. DolphinState* dolphin_state_alloc() {
  17. return furi_alloc(sizeof(DolphinState));
  18. }
  19. void dolphin_state_free(DolphinState* dolphin_state) {
  20. free(dolphin_state);
  21. }
  22. bool dolphin_state_save(DolphinState* dolphin_state) {
  23. if(!dolphin_state->dirty) {
  24. return true;
  25. }
  26. bool result = saved_struct_save(
  27. DOLPHIN_STATE_PATH,
  28. &dolphin_state->data,
  29. sizeof(DolphinStoreData),
  30. DOLPHIN_STATE_HEADER_MAGIC,
  31. DOLPHIN_STATE_HEADER_VERSION);
  32. if(result) {
  33. FURI_LOG_I(TAG, "State saved");
  34. dolphin_state->dirty = false;
  35. } else {
  36. FURI_LOG_E(TAG, "Failed to save state");
  37. }
  38. return result;
  39. }
  40. bool dolphin_state_load(DolphinState* dolphin_state) {
  41. bool success = saved_struct_load(
  42. DOLPHIN_STATE_PATH,
  43. &dolphin_state->data,
  44. sizeof(DolphinStoreData),
  45. DOLPHIN_STATE_HEADER_MAGIC,
  46. DOLPHIN_STATE_HEADER_VERSION);
  47. if(success) {
  48. if((dolphin_state->data.butthurt > BUTTHURT_MAX) ||
  49. (dolphin_state->data.butthurt < BUTTHURT_MIN)) {
  50. success = false;
  51. }
  52. }
  53. if(!success) {
  54. FURI_LOG_W(TAG, "Reset dolphin-state");
  55. memset(dolphin_state, 0, sizeof(*dolphin_state));
  56. dolphin_state->dirty = true;
  57. }
  58. return success;
  59. }
  60. uint64_t dolphin_state_timestamp() {
  61. RTC_TimeTypeDef time;
  62. RTC_DateTypeDef date;
  63. struct tm current;
  64. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  65. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  66. current.tm_year = date.Year + 100;
  67. current.tm_mday = date.Date;
  68. current.tm_mon = date.Month - 1;
  69. current.tm_hour = time.Hours;
  70. current.tm_min = time.Minutes;
  71. current.tm_sec = time.Seconds;
  72. return mktime(&current);
  73. }
  74. bool dolphin_state_is_levelup(uint32_t icounter) {
  75. return (icounter == LEVEL2_THRESHOLD) || (icounter == LEVEL3_THRESHOLD);
  76. }
  77. uint8_t dolphin_get_level(uint32_t icounter) {
  78. if(icounter <= LEVEL2_THRESHOLD) {
  79. return 1;
  80. } else if(icounter <= LEVEL3_THRESHOLD) {
  81. return 2;
  82. } else {
  83. return 3;
  84. }
  85. }
  86. uint32_t dolphin_state_xp_above_last_levelup(uint32_t icounter) {
  87. uint32_t threshold = 0;
  88. if(icounter <= LEVEL2_THRESHOLD) {
  89. threshold = 0;
  90. } else if(icounter <= LEVEL3_THRESHOLD) {
  91. threshold = LEVEL2_THRESHOLD + 1;
  92. } else {
  93. threshold = LEVEL3_THRESHOLD + 1;
  94. }
  95. return icounter - threshold;
  96. }
  97. uint32_t dolphin_state_xp_to_levelup(uint32_t icounter) {
  98. uint32_t threshold = 0;
  99. if(icounter <= LEVEL2_THRESHOLD) {
  100. threshold = LEVEL2_THRESHOLD;
  101. } else if(icounter <= LEVEL3_THRESHOLD) {
  102. threshold = LEVEL3_THRESHOLD;
  103. } else {
  104. threshold = (uint32_t)-1;
  105. }
  106. return threshold - icounter;
  107. }
  108. bool dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
  109. const DolphinDeedWeight* deed_weight = dolphin_deed_weight(deed);
  110. int32_t icounter = dolphin_state->data.icounter + deed_weight->icounter;
  111. bool level_up = false;
  112. bool mood_changed = false;
  113. if(icounter <= 0) {
  114. icounter = 0;
  115. if(dolphin_state->data.icounter == 0) {
  116. return false;
  117. }
  118. }
  119. uint8_t xp_to_levelup = dolphin_state_xp_to_levelup(dolphin_state->data.icounter);
  120. if(xp_to_levelup) {
  121. level_up = true;
  122. dolphin_state->data.icounter += MIN(xp_to_levelup, deed_weight->icounter);
  123. }
  124. uint32_t new_butthurt = CLAMP(
  125. ((int32_t)dolphin_state->data.butthurt) + deed_weight->butthurt,
  126. BUTTHURT_MAX,
  127. BUTTHURT_MIN);
  128. if(!!dolphin_state->data.butthurt != !!new_butthurt) {
  129. mood_changed = true;
  130. }
  131. dolphin_state->data.butthurt = new_butthurt;
  132. dolphin_state->data.timestamp = dolphin_state_timestamp();
  133. dolphin_state->dirty = true;
  134. return level_up || mood_changed;
  135. }
  136. void dolphin_state_butthurted(DolphinState* dolphin_state) {
  137. if(dolphin_state->data.butthurt < BUTTHURT_MAX) {
  138. dolphin_state->data.butthurt++;
  139. FURI_LOG_I("DolphinState", "Increasing butthurt");
  140. dolphin_state->data.timestamp = dolphin_state_timestamp();
  141. dolphin_state->dirty = true;
  142. }
  143. }
  144. void dolphin_state_increase_level(DolphinState* dolphin_state) {
  145. ++dolphin_state->data.icounter;
  146. dolphin_state->dirty = true;
  147. }