Skip to content

Commit 5eebf2d

Browse files
committed
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>
1 parent e95af71 commit 5eebf2d

6 files changed

Lines changed: 122 additions & 99 deletions

File tree

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33

44
#pragma once
55

6+
#include <src/include/pokemon_data.h>
7+
8+
typedef enum {
9+
GENDER_MALE,
10+
GENDER_FEMALE,
11+
} Gender;
12+
13+
typedef enum {
14+
GENDER_F0 = 0x00,
15+
GENDER_F12_5 = 0x1F,
16+
GENDER_F25 = 0x3F,
17+
GENDER_F50 = 0x7F,
18+
GENDER_F75 = 0xBF,
19+
GENDER_F100 = 0xFE,
20+
GENDER_UNKNOWN = 0xFF,
21+
} GenderRatio;
22+
623
/* The gender ratio is a bit value, and if the ATK_IV is less than or equal to
724
* the gender ratio, the gender is female.
825
*
@@ -12,9 +29,11 @@
1229
* male only pokemon need to be specifically checked for.
1330
*/
1431

15-
const char* select_gender_is_static(PokemonData* pdata, uint8_t ratio);
32+
const char* pokemon_gender_is_static(PokemonData* pdata, uint8_t ratio);
1633

1734
/* This will return a pointer to a string of the pokemon's current gender */
18-
char* select_gender_get(PokemonData* pdata);
35+
const char* pokemon_gender_get(PokemonData* pdata);
36+
37+
void pokemon_gender_set(PokemonData* pdata, Gender gender);
1938

2039
#endif // POKEMON_GENDER_H

src/include/pokemon_table.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <stdint.h>
55

6-
#include "stats.h"
6+
#include <src/include/stats.h>
77

88
typedef enum {
99
GROWTH_MEDIUM_FAST = 0,
@@ -12,16 +12,6 @@ typedef enum {
1212
GROWTH_SLOW = 5,
1313
} Growth;
1414

15-
typedef enum {
16-
GENDER_F0 = 0x00,
17-
GENDER_F12_5 = 0x1F,
18-
GENDER_F25 = 0x3F,
19-
GENDER_F50 = 0x7F,
20-
GENDER_F75 = 0xBF,
21-
GENDER_F100 = 0xFE,
22-
GENDER_UNKNOWN = 0xFF,
23-
} Gender;
24-
2515
typedef struct pokemon_data_table PokemonTable;
2616

2717
int table_pokemon_pos_get(const PokemonTable* table, uint8_t index);

src/pokemon_gender.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <gui/modules/submenu.h>
2+
3+
#include <src/include/pokemon_app.h>
4+
#include <src/include/pokemon_data.h>
5+
#include <src/include/pokemon_gender.h>
6+
7+
#include <src/scenes/include/pokemon_scene.h>
8+
9+
static const char* gender_str[] = {
10+
"Unknown",
11+
"Female",
12+
"Male",
13+
};
14+
15+
/* This returns a string pointer if the gender is static, NULL if it is not and
16+
* the gender needs to be calculated.
17+
*/
18+
const char* pokemon_gender_is_static(PokemonData* pdata, uint8_t ratio) {
19+
switch(ratio) {
20+
case 0xFF:
21+
return gender_str[0];
22+
case 0xFE:
23+
return gender_str[1];
24+
case 0x00:
25+
if(pokemon_stat_get(pdata, STAT_NUM, NONE) != 0xEB) { // Tyrogue can be either gender
26+
return gender_str[2];
27+
}
28+
break;
29+
default:
30+
break;
31+
}
32+
33+
return NULL;
34+
}
35+
36+
const char* pokemon_gender_get(PokemonData* pdata) {
37+
uint8_t ratio = table_stat_base_get(
38+
pdata->pokemon_table,
39+
pokemon_stat_get(pdata, STAT_NUM, NONE),
40+
STAT_BASE_GENDER_RATIO,
41+
NONE);
42+
uint8_t atk_iv;
43+
const char* rc;
44+
45+
rc = pokemon_gender_is_static(pdata, ratio);
46+
if(rc) return rc;
47+
48+
/* Falling through here means now we need to calculate the gender from
49+
* its ratio and ATK_IV.
50+
*/
51+
atk_iv = pokemon_stat_get(pdata, STAT_ATK_IV, NONE);
52+
if(atk_iv * 17 <= ratio)
53+
return gender_str[1];
54+
else
55+
return gender_str[2];
56+
}
57+
58+
void pokemon_gender_set(PokemonData* pdata, Gender gender) {
59+
60+
uint8_t ratio = table_stat_base_get(
61+
pdata->pokemon_table,
62+
pokemon_stat_get(pdata, STAT_NUM, NONE),
63+
STAT_BASE_GENDER_RATIO,
64+
NONE);
65+
uint8_t atk_iv = pokemon_stat_get(pdata, STAT_ATK_IV, NONE);
66+
67+
/* If we need to make the pokemon a male, increase atk IV until it exceeds
68+
* the gender ratio.
69+
*
70+
* Note that, there is no checking here for impossible situations as the
71+
* scene enter function will immediately quit if its not possible to change
72+
* the gender (the extremes of gender_ratio value).
73+
*
74+
* The check for gender is a percentage, if ATK_IV*(255/15) <= the ratio,
75+
* then the pokemon is a female. The gender ratio values end up being:
76+
* DEF GENDER_F0 EQU 0 percent
77+
* DEF GENDER_F12_5 EQU 12 percent + 1
78+
* DEF GENDER_F25 EQU 25 percent
79+
* DEF GENDER_F50 EQU 50 percent
80+
* DEF GENDER_F75 EQU 75 percent
81+
* DEF GENDER_F100 EQU 100 percent - 1
82+
* Where percent is (255/100)
83+
*/
84+
if(gender == GENDER_MALE) {
85+
while((atk_iv * 17) <= ratio) atk_iv++;
86+
} else {
87+
while((atk_iv * 17) > ratio) atk_iv--;
88+
}
89+
90+
pokemon_stat_set(pdata, STAT_ATK_IV, NONE, atk_iv);
91+
}

src/pokemon_table.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <furi.h>
55

66
#include <pokemon_icons.h>
7+
#include <src/include/pokemon_gender.h>
78
#include <src/include/pokemon_table.h>
89
#include <src/include/stats.h>
910

@@ -26,7 +27,7 @@ struct __attribute__((__packed__)) pokemon_data_table {
2627
const uint8_t type[2];
2728
const uint8_t move[4];
2829
const Growth growth;
29-
const Gender gender_ratio;
30+
const GenderRatio gender_ratio;
3031
};
3132

3233
int table_pokemon_pos_get(const PokemonTable* table, uint8_t index) {

src/scenes/pokemon_gen.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include <src/scenes/include/pokemon_menu.h>
1313
#include <src/scenes/include/pokemon_shiny.h>
14-
#include <src/scenes/include/pokemon_gender.h>
14+
#include <src/include/pokemon_gender.h>
1515
#include <src/scenes/include/pokemon_pokerus.h>
1616
#include <src/include/unown_form.h>
1717

@@ -30,7 +30,7 @@ static void scene_change_from_main_cb(void* context, uint32_t index) {
3030
scene_manager_set_scene_state(pokemon_fap->scene_manager, PokemonSceneLevel, index);
3131
break;
3232
case PokemonSceneGender:
33-
if(select_gender_is_static(
33+
if(pokemon_gender_is_static(
3434
pokemon_fap->pdata,
3535
table_stat_base_get(
3636
pokemon_fap->pdata->pokemon_table,
@@ -184,7 +184,7 @@ void pokemon_scene_gen_on_enter(void* context) {
184184
submenu_add_item(
185185
pokemon_fap->submenu, buf, PokemonSceneShiny, scene_change_from_main_cb, pokemon_fap);
186186

187-
snprintf(buf, sizeof(buf), "Gender: %s", select_gender_get(pokemon_fap->pdata));
187+
snprintf(buf, sizeof(buf), "Gender: %s", pokemon_gender_get(pokemon_fap->pdata));
188188
submenu_add_item(
189189
pokemon_fap->submenu, buf, PokemonSceneGender, scene_change_from_main_cb, pokemon_fap);
190190

src/scenes/pokemon_gender.c

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,14 @@
22

33
#include <src/include/pokemon_app.h>
44
#include <src/include/pokemon_data.h>
5+
#include <src/include/pokemon_gender.h>
56

67
#include <src/scenes/include/pokemon_scene.h>
78

8-
static const char* gender_str[] = {
9-
"Unknown",
10-
"Female",
11-
"Male",
12-
};
13-
14-
/* This returns a string pointer if the gender is static, NULL if it is not and
15-
* the gender needs to be calculated.
16-
*/
17-
const char* select_gender_is_static(PokemonData* pdata, uint8_t ratio) {
18-
switch(ratio) {
19-
case 0xFF:
20-
return gender_str[0];
21-
case 0xFE:
22-
return gender_str[1];
23-
case 0x00:
24-
if(pokemon_stat_get(pdata, STAT_NUM, NONE) != 0xEB) { // Tyrogue can be either gender
25-
return gender_str[2];
26-
}
27-
break;
28-
default:
29-
break;
30-
}
31-
32-
return NULL;
33-
}
34-
35-
const char* select_gender_get(PokemonData* pdata) {
36-
uint8_t ratio = table_stat_base_get(
37-
pdata->pokemon_table,
38-
pokemon_stat_get(pdata, STAT_NUM, NONE),
39-
STAT_BASE_GENDER_RATIO,
40-
NONE);
41-
uint8_t atk_iv;
42-
const char* rc;
43-
44-
rc = select_gender_is_static(pdata, ratio);
45-
if(rc) return rc;
46-
47-
/* Falling through here means now we need to calculate the gender from
48-
* its ratio and ATK_IV.
49-
*/
50-
atk_iv = pokemon_stat_get(pdata, STAT_ATK_IV, NONE);
51-
if(atk_iv * 17 <= ratio)
52-
return gender_str[1];
53-
else
54-
return gender_str[2];
55-
}
56-
579
static void select_gender_selected_callback(void* context, uint32_t index) {
5810
PokemonFap* pokemon_fap = (PokemonFap*)context;
59-
PokemonData* pdata = pokemon_fap->pdata;
60-
uint8_t ratio = table_stat_base_get(
61-
pdata->pokemon_table,
62-
pokemon_stat_get(pdata, STAT_NUM, NONE),
63-
STAT_BASE_GENDER_RATIO,
64-
NONE);
65-
uint8_t atk_iv = pokemon_stat_get(pdata, STAT_ATK_IV, NONE);
66-
67-
/* If we need to make the pokemon a male, increase atk IV until it exceeds
68-
* the gender ratio.
69-
*
70-
* Note that, there is no checking here for impossible situations as the
71-
* scene enter function will immediately quit if its not possible to change
72-
* the gender (the extremes of gender_ratio value).
73-
*
74-
* The check for gender is a percentage, if ATK_IV*(255/15) <= the ratio,
75-
* then the pokemon is a female. The gender ratio values end up being:
76-
* DEF GENDER_F0 EQU 0 percent
77-
* DEF GENDER_F12_5 EQU 12 percent + 1
78-
* DEF GENDER_F25 EQU 25 percent
79-
* DEF GENDER_F50 EQU 50 percent
80-
* DEF GENDER_F75 EQU 75 percent
81-
* DEF GENDER_F100 EQU 100 percent - 1
82-
* Where percent is (255/100)
83-
*/
84-
if(index) {
85-
while((atk_iv * 17) <= ratio) atk_iv++;
86-
} else {
87-
while((atk_iv * 17) > ratio) atk_iv--;
88-
}
8911

90-
pokemon_stat_set(pdata, STAT_ATK_IV, NONE, atk_iv);
12+
pokemon_gender_set(pokemon_fap->pdata, index);
9113

9214
scene_manager_previous_scene(pokemon_fap->scene_manager);
9315
}
@@ -98,10 +20,10 @@ void pokemon_scene_select_gender_on_enter(void* context) {
9820
submenu_reset(pokemon_fap->submenu);
9921

10022
submenu_add_item(
101-
pokemon_fap->submenu, "Female", 0, select_gender_selected_callback, pokemon_fap);
23+
pokemon_fap->submenu, "Female", GENDER_FEMALE, select_gender_selected_callback, pokemon_fap);
10224

10325
submenu_add_item(
104-
pokemon_fap->submenu, "Male", 1, select_gender_selected_callback, pokemon_fap);
26+
pokemon_fap->submenu, "Male", GENDER_MALE, select_gender_selected_callback, pokemon_fap);
10527
}
10628

10729
bool pokemon_scene_select_gender_on_event(void* context, SceneManagerEvent event)

0 commit comments

Comments
 (0)