Skip to content

Commit

Permalink
pokemon: Refactor party trade struct setup/use
Browse files Browse the repository at this point in the history
No longer declared in header, causes problems for multiple
inclusions. Added as a pokemon_fap struct member.

Trade was updated to use this struct member. For sections of code
where the flat array is still expected, the TradeBlock struct
is cast as a flat pointer.

The struct initialization is also cut down heavily to only specify
the bare minimum needed for the first pokemon in the party.
  • Loading branch information
kbembedded committed Sep 15, 2023
1 parent bb3c88f commit fd28f23
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 164 deletions.
47 changes: 47 additions & 0 deletions pokemon_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,50 @@ const PokemonTable pokemon_table[] = {
{},
};

TradeBlock OUTPUT_BLOCK = {
.trainer_name = {F_, l_, i_, p_, p_, e_, r_, TERM_, 0x00, 0x00, 0x00},
.party_cnt = 1,
/* Only the first pokemon is ever used even though there are 7 bytes here.
* If the remaining 6 bytes are _not_ 0xff, then the trade window renders
* garbage for the Flipper's party.
*/
.party_members = {0x15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
/* Only the first pokemon is set up, even though there are 6 total party members */
.party =
{
{.species = 0x4a,
.hp = 0x2c01,
.level = 0x4a,
.status_condition = 0x0,
.type = {0x14, 0x08},
.catch_held = 0x1f,
.move = {0x7e, 0x38, 0x09, 0x19},
.orig_trainer = 0xd204,
.exp = {0x3, 0xd, 0x40},
.hp_ev = 0xffff,
.atk_ev = 0xffff,
.def_ev = 0xffff,
.spd_ev = 0xffff,
.special_ev = 0xffff,
.iv = 0xffff,
.move_pp = {0xc0, 0xc0, 0xc0, 0xc0},
.level_again = 0x4a,
.max_hp = 0x2c01,
.atk = 0x9600,
.def = 0x9700,
.spd = 0x9800,
.special = 0x9900},
},
/* Only the first pokemon has an OT name and nickname even though there are 6 members */
/* NOTE: I think this shouldn't exceed 7 chars */
.ot_name =
{
{.str = {F_, l_, i_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
},
.nickname = {
{.str = {F_, l_, o_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
}};

uint32_t pokemon_exit_confirm_view(void* context) {
UNUSED(context);
return AppViewExitConfirm;
Expand All @@ -181,6 +225,9 @@ PokemonFap* pokemon_alloc() {
// Set up pointer to pokemon table
pokemon_fap->pokemon_table = pokemon_table;

// Set up trade party struct
pokemon_fap->trade_party = &OUTPUT_BLOCK;

// Select Pokemon View
pokemon_fap->select_view = select_pokemon_alloc(pokemon_fap);
view_set_previous_callback(select_pokemon_get_view(pokemon_fap), pokemon_exit_confirm_view);
Expand Down
9 changes: 9 additions & 0 deletions pokemon_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <gui/icon.h>
#include <pokemon_icons.h>

#include "pokemon_data.h"

#define TAG "Pokemon"

struct pokemon_data_table {
Expand Down Expand Up @@ -40,6 +42,13 @@ struct pokemon_fap {
/* Table of pokemon data for Gen I */
const PokemonTable* pokemon_table;

/* Struct for holding trade data */
/* NOTE: There may be some runtime memory savings by adding more intelligence
* to views/trade and slimming down this struct to only contain the single
* pokemon data rather than the full 6 member party data.
*/
TradeBlock* trade_party;

/* The currently selected pokemon */
int curr_pokemon;

Expand Down
161 changes: 1 addition & 160 deletions pokemon_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,165 +142,6 @@ struct __attribute__((__packed__)) trade_data_block {
struct name nickname[6];
};

struct trade_data_block DATA_BLOCK2 =
{.trainer_name = {F_, l_, i_, p_, p_, e_, r_, TERM_, 0x00, 0x00, 0x00},
.party_cnt = 1,
.party_members = {0x15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
.party =
{
{.species = 0x4a,
.hp = 0x2c01,
.level = 0x4a,
.status_condition = 0x0,
.type = {0x14, 0x08},
.catch_held = 0x1f,
.move = {0x7e, 0x38, 0x09, 0x19},
.orig_trainer = 0xd204,
.exp = {0x3, 0xd, 0x40},
.hp_ev = 0xffff,
.atk_ev = 0xffff,
.def_ev = 0xffff,
.spd_ev = 0xffff,
.special_ev = 0xffff,
.iv = 0xffff,
.move_pp = {0xc0, 0xc0, 0xc0, 0xc0},
.level_again = 0x4a,
.max_hp = 0x2c01,
.atk = 0x9600,
.def = 0x9700,
.spd = 0x9800,
.special = 0x9900},
{.species = 0x4a,
.hp = 0x2c01,
.level = 0x4a,
.status_condition = 0x0,
.type = {0x14, 0x08},
.catch_held = 0x1f,
.move = {0x7e, 0x38, 0x09, 0x19},
.orig_trainer = 0xd204,
.exp = {0x3, 0xd, 0x40},
.hp_ev = 0xffff,
.atk_ev = 0xffff,
.def_ev = 0xffff,
.spd_ev = 0xffff,
.special_ev = 0xffff,
.iv = 0xffff,
.move_pp = {0xc0, 0xc0, 0xc0, 0xc0},
.level_again = 0x4a,
.max_hp = 0x2c01,
.atk = 0x9600,
.def = 0x9700,
.spd = 0x9800,
.special = 0x9900},
{.species = 0x4a,
.hp = 0x2c01,
.level = 0x4a,
.status_condition = 0x0,
.type = {0x14, 0x08},
.catch_held = 0x1f,
.move = {0x7e, 0x38, 0x09, 0x19},
.orig_trainer = 0xd204,
.exp = {0x3, 0xd, 0x40},
.hp_ev = 0xffff,
.atk_ev = 0xffff,
.def_ev = 0xffff,
.spd_ev = 0xffff,
.special_ev = 0xffff,
.iv = 0xffff,
.move_pp = {0xc0, 0xc0, 0xc0, 0xc0},
.level_again = 0x4a,
.max_hp = 0x2c01,
.atk = 0x9600,
.def = 0x9700,
.spd = 0x9800,
.special = 0x9900},
{.species = 0x4a,
.hp = 0x2c01,
.level = 0x4a,
.status_condition = 0x0,
.type = {0x14, 0x08},
.catch_held = 0x1f,
.move = {0x7e, 0x38, 0x09, 0x19},
.orig_trainer = 0xd204,
.exp = {0x3, 0xd, 0x40},
.hp_ev = 0xffff,
.atk_ev = 0xffff,
.def_ev = 0xffff,
.spd_ev = 0xffff,
.special_ev = 0xffff,
.iv = 0xffff,
.move_pp = {0xc0, 0xc0, 0xc0, 0xc0},
.level_again = 0x4a,
.max_hp = 0x2c01,
.atk = 0x9600,
.def = 0x9700,
.spd = 0x9800,
.special = 0x9900},
{.species = 0x4a,
.hp = 0x2c01,
.level = 0x4a,
.status_condition = 0x0,
.type = {0x14, 0x08},
.catch_held = 0x1f,
.move = {0x7e, 0x38, 0x09, 0x19},
.orig_trainer = 0xd204,
.exp = {0x3, 0xd, 0x40},
.hp_ev = 0xffff,
.atk_ev = 0xffff,
.def_ev = 0xffff,
.spd_ev = 0xffff,
.special_ev = 0xffff,
.iv = 0xffff,
.move_pp = {0xc0, 0xc0, 0xc0, 0xc0},
.level_again = 0x4a,
.max_hp = 0x2c01,
.atk = 0x9600,
.def = 0x9700,
.spd = 0x9800,
.special = 0x9900},
{.species = 0x4a,
.hp = 0x2c01,
.level = 0x4a,
.status_condition = 0x0,
.type = {0x14, 0x08},
.catch_held = 0x1f,
.move = {0x7e, 0x38, 0x09, 0x19},
.orig_trainer = 0xd204,
.exp = {0x3, 0xd, 0x40},
.hp_ev = 0xffff,
.atk_ev = 0xffff,
.def_ev = 0xffff,
.spd_ev = 0xffff,
.special_ev = 0xffff,
.iv = 0xffff,
.move_pp = {0xc0, 0xc0, 0xc0, 0xc0},
.level_again = 0x4a,
.max_hp = 0x2c01,
.atk = 0x9600,
.def = 0x9700,
.spd = 0x9800,
.special = 0x9900},
},
/* NOTE: I think this shouldn't exceed 7 chars */
.ot_name =
{
{.str = {F_, l_, i_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
{.str = {F_, l_, i_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
{.str = {F_, l_, i_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
{.str = {F_, l_, i_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
{.str = {F_, l_, i_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
{.str = {F_, l_, i_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
},
.nickname = {
{.str = {F_, l_, o_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
{.str = {F_, l_, o_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
{.str = {F_, l_, o_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
{.str = {F_, l_, o_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
{.str = {F_, l_, o_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
{.str = {F_, l_, o_, p_, p_, e_, r_, TERM_, 0x00, 0x00}},
}};

unsigned char INPUT_BLOCK[405];
unsigned char* DATA_BLOCK = (unsigned char*)&DATA_BLOCK2;
typedef struct trade_data_block TradeBlock;

#endif /* POKEMON_DATA_H */
2 changes: 1 addition & 1 deletion views/select_pokemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static void select_pokemon_render_callback(Canvas* canvas, void* model) {
snprintf(pokedex_num, sizeof(pokedex_num), "#%03d", current_index + 1);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(
canvas, 55, 54 / 2, AlignLeft, AlignTop, pokemon_table[current_index].name);
canvas, 55, 54 / 2, AlignLeft, AlignTop, pokemon_fap->pokemon_table[current_index].name);

canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 55, 38, AlignLeft, AlignTop, pokedex_num);
Expand Down
10 changes: 7 additions & 3 deletions views/trade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ volatile int counter = 0;
volatile bool procesing = true;
volatile connection_state_t connection_state = NOT_CONNECTED;
volatile trade_centre_state_t trade_centre_state = INIT;
unsigned char INPUT_BLOCK[405];

void screen_gameboy_connect(Canvas* const canvas) {
canvas_draw_frame(canvas, 0, 0, 128, 64);
Expand Down Expand Up @@ -184,6 +185,7 @@ byte getMenuResponse(byte in) {

byte getTradeCentreResponse(byte in, void* context) {
PokemonFap* pokemon_fap = (PokemonFap*)context;
uint8_t* trade_party_flat = (uint8_t*)pokemon_fap->trade_party;
byte send = in;

furi_assert(context);
Expand Down Expand Up @@ -227,15 +229,15 @@ byte getTradeCentreResponse(byte in, void* context) {
if((in & 0xF0) != 0xF0) {
counter = 0;
INPUT_BLOCK[counter] = in;
send = DATA_BLOCK[counter];
send = trade_party_flat[counter];
counter++;
trade_centre_state = SENDING_DATA;
}
break;

case SENDING_DATA:
INPUT_BLOCK[counter] = in;
send = DATA_BLOCK[counter];
send = trade_party_flat[counter];
counter++;
if(counter == 405) //TODO: replace with sizeof struct rather than static number
trade_centre_state = SENDING_PATCH_DATA;
Expand Down Expand Up @@ -357,7 +359,9 @@ void trade_enter_callback(void* context) {
pokemon_fap->connected = false;
pokemon_fap->gameboy_status = GAMEBOY_INITIAL;

DATA_BLOCK[12] = pokemon_fap->pokemon_table[pokemon_fap->curr_pokemon].species;
pokemon_fap->trade_party->party_members[0] =
pokemon_fap->pokemon_table[pokemon_fap->curr_pokemon].species;

// B3 (Pin6) / SO (2)
furi_hal_gpio_write(&GAME_BOY_SO, false);
furi_hal_gpio_init(&GAME_BOY_SO, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
Expand Down

0 comments on commit fd28f23

Please sign in to comment.