dolphin_state.c 4.6 KB

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