Przeglądaj źródła

pokemon_gender: Split out accessor functions from scene

Scene doesn't actually need to care about these

Signed-off-by: Kris Bahnsen <Kris@KBEmbedded.com>
Kris Bahnsen 1 rok temu
rodzic
commit
5eebf2d122

+ 21 - 2
src/scenes/include/pokemon_gender.h → src/include/pokemon_gender.h

@@ -3,6 +3,23 @@
 
 #pragma once
 
+#include <src/include/pokemon_data.h>
+
+typedef enum {
+    GENDER_MALE,
+    GENDER_FEMALE,
+} Gender;
+
+typedef enum {
+    GENDER_F0 = 0x00,
+    GENDER_F12_5 = 0x1F,
+    GENDER_F25 = 0x3F,
+    GENDER_F50 = 0x7F,
+    GENDER_F75 = 0xBF,
+    GENDER_F100 = 0xFE,
+    GENDER_UNKNOWN = 0xFF,
+} GenderRatio;
+
 /* The gender ratio is a bit value, and if the ATK_IV is less than or equal to
  * the gender ratio, the gender is female.
  *
@@ -12,9 +29,11 @@
  *   male only pokemon need to be specifically checked for.
  */
 
-const char* select_gender_is_static(PokemonData* pdata, uint8_t ratio);
+const char* pokemon_gender_is_static(PokemonData* pdata, uint8_t ratio);
 
 /* This will return a pointer to a string of the pokemon's current gender */
-char* select_gender_get(PokemonData* pdata);
+const char* pokemon_gender_get(PokemonData* pdata);
+
+void pokemon_gender_set(PokemonData* pdata, Gender gender);
 
 #endif // POKEMON_GENDER_H

+ 1 - 11
src/include/pokemon_table.h

@@ -3,7 +3,7 @@
 
 #include <stdint.h>
 
-#include "stats.h"
+#include <src/include/stats.h>
 
 typedef enum {
     GROWTH_MEDIUM_FAST = 0,
@@ -12,16 +12,6 @@ typedef enum {
     GROWTH_SLOW = 5,
 } Growth;
 
-typedef enum {
-    GENDER_F0 = 0x00,
-    GENDER_F12_5 = 0x1F,
-    GENDER_F25 = 0x3F,
-    GENDER_F50 = 0x7F,
-    GENDER_F75 = 0xBF,
-    GENDER_F100 = 0xFE,
-    GENDER_UNKNOWN = 0xFF,
-} Gender;
-
 typedef struct pokemon_data_table PokemonTable;
 
 int table_pokemon_pos_get(const PokemonTable* table, uint8_t index);

+ 91 - 0
src/pokemon_gender.c

@@ -0,0 +1,91 @@
+#include <gui/modules/submenu.h>
+
+#include <src/include/pokemon_app.h>
+#include <src/include/pokemon_data.h>
+#include <src/include/pokemon_gender.h>
+
+#include <src/scenes/include/pokemon_scene.h>
+
+static const char* gender_str[] = {
+    "Unknown",
+    "Female",
+    "Male",
+};
+
+/* This returns a string pointer if the gender is static, NULL if it is not and
+ * the gender needs to be calculated.
+ */
+const char* pokemon_gender_is_static(PokemonData* pdata, uint8_t ratio) {
+    switch(ratio) {
+    case 0xFF:
+        return gender_str[0];
+    case 0xFE:
+        return gender_str[1];
+    case 0x00:
+        if(pokemon_stat_get(pdata, STAT_NUM, NONE) != 0xEB) { // Tyrogue can be either gender
+            return gender_str[2];
+        }
+        break;
+    default:
+        break;
+    }
+
+    return NULL;
+}
+
+const char* pokemon_gender_get(PokemonData* pdata) {
+    uint8_t ratio = table_stat_base_get(
+        pdata->pokemon_table,
+        pokemon_stat_get(pdata, STAT_NUM, NONE),
+        STAT_BASE_GENDER_RATIO,
+        NONE);
+    uint8_t atk_iv;
+    const char* rc;
+
+    rc = pokemon_gender_is_static(pdata, ratio);
+    if(rc) return rc;
+
+    /* Falling through here means now we need to calculate the gender from
+     * its ratio and ATK_IV.
+     */
+    atk_iv = pokemon_stat_get(pdata, STAT_ATK_IV, NONE);
+    if(atk_iv * 17 <= ratio)
+        return gender_str[1];
+    else
+        return gender_str[2];
+}
+
+void pokemon_gender_set(PokemonData* pdata, Gender gender) {
+
+    uint8_t ratio = table_stat_base_get(
+        pdata->pokemon_table,
+        pokemon_stat_get(pdata, STAT_NUM, NONE),
+        STAT_BASE_GENDER_RATIO,
+        NONE);
+    uint8_t atk_iv = pokemon_stat_get(pdata, STAT_ATK_IV, NONE);
+
+    /* If we need to make the pokemon a male, increase atk IV until it exceeds
+     * the gender ratio.
+     *
+     * Note that, there is no checking here for impossible situations as the
+     * scene enter function will immediately quit if its not possible to change
+     * the gender (the extremes of gender_ratio value).
+     *
+     * The check for gender is a percentage, if ATK_IV*(255/15) <= the ratio,
+     * then the pokemon is a female. The gender ratio values end up being:
+     * DEF GENDER_F0      EQU   0 percent
+     * DEF GENDER_F12_5   EQU  12 percent + 1
+     * DEF GENDER_F25     EQU  25 percent
+     * DEF GENDER_F50     EQU  50 percent
+     * DEF GENDER_F75     EQU  75 percent
+     * DEF GENDER_F100    EQU 100 percent - 1
+     * Where percent is (255/100)
+     */
+    if(gender == GENDER_MALE) {
+        while((atk_iv * 17) <= ratio) atk_iv++;
+    } else {
+        while((atk_iv * 17) > ratio) atk_iv--;
+    }
+
+    pokemon_stat_set(pdata, STAT_ATK_IV, NONE, atk_iv);
+}

+ 2 - 1
src/pokemon_table.c

@@ -4,6 +4,7 @@
 #include <furi.h>
 
 #include <pokemon_icons.h>
+#include <src/include/pokemon_gender.h>
 #include <src/include/pokemon_table.h>
 #include <src/include/stats.h>
 
@@ -26,7 +27,7 @@ struct __attribute__((__packed__)) pokemon_data_table {
     const uint8_t type[2];
     const uint8_t move[4];
     const Growth growth;
-    const Gender gender_ratio;
+    const GenderRatio gender_ratio;
 };
 
 int table_pokemon_pos_get(const PokemonTable* table, uint8_t index) {

+ 3 - 3
src/scenes/pokemon_gen.c

@@ -11,7 +11,7 @@
 
 #include <src/scenes/include/pokemon_menu.h>
 #include <src/scenes/include/pokemon_shiny.h>
-#include <src/scenes/include/pokemon_gender.h>
+#include <src/include/pokemon_gender.h>
 #include <src/scenes/include/pokemon_pokerus.h>
 #include <src/include/unown_form.h>
 
@@ -30,7 +30,7 @@ static void scene_change_from_main_cb(void* context, uint32_t index) {
         scene_manager_set_scene_state(pokemon_fap->scene_manager, PokemonSceneLevel, index);
         break;
     case PokemonSceneGender:
-        if(select_gender_is_static(
+        if(pokemon_gender_is_static(
                pokemon_fap->pdata,
                table_stat_base_get(
                    pokemon_fap->pdata->pokemon_table,
@@ -184,7 +184,7 @@ void pokemon_scene_gen_on_enter(void* context) {
         submenu_add_item(
             pokemon_fap->submenu, buf, PokemonSceneShiny, scene_change_from_main_cb, pokemon_fap);
 
-        snprintf(buf, sizeof(buf), "Gender:         %s", select_gender_get(pokemon_fap->pdata));
+        snprintf(buf, sizeof(buf), "Gender:         %s", pokemon_gender_get(pokemon_fap->pdata));
         submenu_add_item(
             pokemon_fap->submenu, buf, PokemonSceneGender, scene_change_from_main_cb, pokemon_fap);
 

+ 4 - 82
src/scenes/pokemon_gender.c

@@ -2,92 +2,14 @@
 
 #include <src/include/pokemon_app.h>
 #include <src/include/pokemon_data.h>
+#include <src/include/pokemon_gender.h>
 
 #include <src/scenes/include/pokemon_scene.h>
 
-static const char* gender_str[] = {
-    "Unknown",
-    "Female",
-    "Male",
-};
-
-/* This returns a string pointer if the gender is static, NULL if it is not and
- * the gender needs to be calculated.
- */
-const char* select_gender_is_static(PokemonData* pdata, uint8_t ratio) {
-    switch(ratio) {
-    case 0xFF:
-        return gender_str[0];
-    case 0xFE:
-        return gender_str[1];
-    case 0x00:
-        if(pokemon_stat_get(pdata, STAT_NUM, NONE) != 0xEB) { // Tyrogue can be either gender
-            return gender_str[2];
-        }
-        break;
-    default:
-        break;
-    }
-
-    return NULL;
-}
-
-const char* select_gender_get(PokemonData* pdata) {
-    uint8_t ratio = table_stat_base_get(
-        pdata->pokemon_table,
-        pokemon_stat_get(pdata, STAT_NUM, NONE),
-        STAT_BASE_GENDER_RATIO,
-        NONE);
-    uint8_t atk_iv;
-    const char* rc;
-
-    rc = select_gender_is_static(pdata, ratio);
-    if(rc) return rc;
-
-    /* Falling through here means now we need to calculate the gender from
-     * its ratio and ATK_IV.
-     */
-    atk_iv = pokemon_stat_get(pdata, STAT_ATK_IV, NONE);
-    if(atk_iv * 17 <= ratio)
-        return gender_str[1];
-    else
-        return gender_str[2];
-}
-
 static void select_gender_selected_callback(void* context, uint32_t index) {
     PokemonFap* pokemon_fap = (PokemonFap*)context;
-    PokemonData* pdata = pokemon_fap->pdata;
-    uint8_t ratio = table_stat_base_get(
-        pdata->pokemon_table,
-        pokemon_stat_get(pdata, STAT_NUM, NONE),
-        STAT_BASE_GENDER_RATIO,
-        NONE);
-    uint8_t atk_iv = pokemon_stat_get(pdata, STAT_ATK_IV, NONE);
-
-    /* If we need to make the pokemon a male, increase atk IV until it exceeds
-     * the gender ratio.
-     *
-     * Note that, there is no checking here for impossible situations as the
-     * scene enter function will immediately quit if its not possible to change
-     * the gender (the extremes of gender_ratio value).
-     *
-     * The check for gender is a percentage, if ATK_IV*(255/15) <= the ratio,
-     * then the pokemon is a female. The gender ratio values end up being:
-     * DEF GENDER_F0      EQU   0 percent
-     * DEF GENDER_F12_5   EQU  12 percent + 1
-     * DEF GENDER_F25     EQU  25 percent
-     * DEF GENDER_F50     EQU  50 percent
-     * DEF GENDER_F75     EQU  75 percent
-     * DEF GENDER_F100    EQU 100 percent - 1
-     * Where percent is (255/100)
-     */
-    if(index) {
-        while((atk_iv * 17) <= ratio) atk_iv++;
-    } else {
-        while((atk_iv * 17) > ratio) atk_iv--;
-    }
 
-    pokemon_stat_set(pdata, STAT_ATK_IV, NONE, atk_iv);
+    pokemon_gender_set(pokemon_fap->pdata, index);
 
     scene_manager_previous_scene(pokemon_fap->scene_manager);
 }
@@ -98,10 +20,10 @@ void pokemon_scene_select_gender_on_enter(void* context) {
     submenu_reset(pokemon_fap->submenu);
 
     submenu_add_item(
-        pokemon_fap->submenu, "Female", 0, select_gender_selected_callback, pokemon_fap);
+        pokemon_fap->submenu, "Female", GENDER_FEMALE, select_gender_selected_callback, pokemon_fap);
 
     submenu_add_item(
-        pokemon_fap->submenu, "Male", 1, select_gender_selected_callback, pokemon_fap);
+        pokemon_fap->submenu, "Male", GENDER_MALE, select_gender_selected_callback, pokemon_fap);
 }
 
 bool pokemon_scene_select_gender_on_event(void* context, SceneManagerEvent event)