From edfdc1ff0258bef9eb2e79ed46531986149c331c Mon Sep 17 00:00:00 2001 From: Jason Carpenter <5916026+breadbored@users.noreply.github.com> Date: Sun, 10 Sep 2023 14:43:56 -0400 Subject: [PATCH] initial commit --- .gitattributes | 2 + .gitignore | 67 ++++++ LICENSE.md | 98 ++++++++ Makefile | 18 ++ comm.c | 433 ++++++++++++++++++++++++++++++++++ gen1.h | 619 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 1237 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 LICENSE.md create mode 100644 Makefile create mode 100644 comm.c create mode 100644 gen1.h diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61fd106 --- /dev/null +++ b/.gitignore @@ -0,0 +1,67 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf +serial.s +mirror.gb +global.s +compile.bat +comm.sav +comm.noi +comm.gb +comm-semiworking.c +comm-justonelastproblem.c +a.ihx +.vscode/settings.json +.vscode/c_cpp_properties.json +.DS_Store +pokedistro.gb +pokedistro2.gb diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..29005c8 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,98 @@ +### Creative Commons Attribution-NonCommercial 4.0 International Public License + +By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-NonCommercial 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions. + +**Section 1 -- Definitions.** + +1. **Adapted Material** means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image. +2. **Adapter's License** means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License. +3. **Copyright and Similar Rights** means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section [2(b)(1)-(2)](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s2b) are not Copyright and Similar Rights. +4. **Effective Technological Measures** means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements. +5. **Exceptions and Limitations** means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material. +6. **Licensed Material** means the artistic or literary work, database, or other material to which the Licensor applied this Public License. +7. **Licensed Rights** means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license. +8. **Licensor** means the individual(s) or entity(ies) granting rights under this Public License. +9. **NonCommercial** means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of this Public License, the exchange of the Licensed Material for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange. +10. **Share** means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them. +11. **Sui Generis Database Rights** means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world. +12. **You** means the individual or entity exercising the Licensed Rights under this Public License. **Your** has a corresponding meaning. + +**Section 2 -- Scope.** + +1. **License grant**. + 1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to: + 1. reproduce and Share the Licensed Material, in whole or in part, for NonCommercial purposes only; and + 2. produce, reproduce, and Share Adapted Material for NonCommercial purposes only. + 2. Exceptions and Limitations. For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions. + 3. Term. The term of this Public License is specified in Section [6(a)](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s6a). + 4. Media and formats; technical modifications allowed. The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section [2(a)(4)](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s2a4) never produces Adapted Material. + 5. Downstream recipients. + + 1. Offer from the Licensor -- Licensed Material. Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License. + 2. No downstream restrictions. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material. + + 6. No endorsement. Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section [3(a)(1)(A)(i)](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s3a1Ai). +2. **Other rights**. + + 1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise. + 2. Patent and trademark rights are not licensed under this Public License. + 3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties, including when the Licensed Material is used other than for NonCommercial purposes. + +**Section 3 -- License Conditions.** + +Your exercise of the Licensed Rights is expressly made subject to the following conditions. + +1. **Attribution**. + + 1. If You Share the Licensed Material (including in modified form), You must: + + 1. retain the following if it is supplied by the Licensor with the Licensed Material: + 1. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated); + 2. a copyright notice; + 3. a notice that refers to this Public License; + 4. a notice that refers to the disclaimer of warranties; + 5. a URI or hyperlink to the Licensed Material to the extent reasonably practicable; + 2. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and + 3. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License. + 2. You may satisfy the conditions in Section [3(a)(1)](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s3a1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information. + 3. If requested by the Licensor, You must remove any of the information required by Section [3(a)(1)(A)](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s3a1A) to the extent reasonably practicable. + 4. If You Share Adapted Material You produce, the Adapter's License You apply must not prevent recipients of the Adapted Material from complying with this Public License. + +**Section 4 -- Sui Generis Database Rights.** + +Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material: + +1. for the avoidance of doubt, Section [2(a)(1)](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s2a1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database for NonCommercial purposes only; +2. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material; and +3. You must comply with the conditions in Section [3(a)](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s3a) if You Share all or a substantial portion of the contents of the database. + +For the avoidance of doubt, this Section [4](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s4) supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights. + +**Section 5 -- Disclaimer of Warranties and Limitation of Liability.** + +1. **Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You.** +2. **To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You.** + +1. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability. + +**Section 6 -- Term and Termination.** + +1. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically. +2. Where Your right to use the Licensed Material has terminated under Section [6(a)](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s6a), it reinstates: + + 1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or + 2. upon express reinstatement by the Licensor.For the avoidance of doubt, this Section [6(b)](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s6b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License. +3. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License. +4. Sections [1](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s1), [5](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s5), [6](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s6), [7](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s7), and [8](https://creativecommons.org/licenses/by-nc/4.0/legalcode#s8) survive termination of this Public License. + +**Section 7 -- Other Terms and Conditions.** + +1. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed. +2. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License. + +**Section 8 -- Interpretation.** + +1. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License. +2. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions. +3. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor. +4. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c2f106c --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +# CC = /opt/gbdk/bin/lcc -Wa-l -Wl-m -Wl-j +CC = ~/gbdk-2020/build/gbdk/bin/lcc -Wa-l -Wl-m -Wl-j + +BINS = comm.gb + +all: $(BINS) + +compile.bat: Makefile + @echo "REM Automatically generated from Makefile" > compile.bat + @make -sn | sed y/\\//\\\\/ | sed s/mkdir\ -p\/mkdir\/ | grep -v make >> compile.bat + +# Compile and link single file in one pass +%.gb: %.c + $(CC) -o $@ $< + +clean: + rm -f *.o *.lst *.map *.gb *~ *.rel *.cdb *.ihx *.lnk *.sym *.asm *.noi + diff --git a/comm.c b/comm.c new file mode 100644 index 0000000..8ee41ef --- /dev/null +++ b/comm.c @@ -0,0 +1,433 @@ +#include +#include +#include +#include +#include "gen1.h" + +#define PARTY_SIZE 6 +#define POKE_SIZE 44 +#define NAME_LEN 11 +#define SELP_LEN 8 +#define POKE_LEN POKE_SIZE * PARTY_SIZE +#define ORIG_LEN NAME_LEN * PARTY_SIZE +#define NICK_LEN NAME_LEN * PARTY_SIZE +#define FULL_LEN NAME_LEN + SELP_LEN + (POKE_SIZE * PARTY_SIZE) + (NAME_LEN * PARTY_SIZE) + (NAME_LEN * PARTY_SIZE) + +#define SER_REG_DIR (*(uint8_t *)0xFF01) +#define SER_OPT_REG_DIR (*(uint8_t *)0xFF02) + +enum connection_state_t connection_state = NOT_CONNECTED; +enum trade_state_t trade_state = INIT; +uint8_t INPUT_BLOCK[418]; +uint8_t DATA_BLOCK[418]; +int trade_pokemon = -1; +unsigned char name[11] = { + pokechar_B, + pokechar_r, + pokechar_e, + pokechar_a, + pokechar_d, + pokechar_C, + pokechar_o, + pokechar_d, + pokechar_e, + pokechar_s, + pokechar_STOP_BYTE +}; +unsigned char nicknames[11] = { + // Pokemon Nickname + pokechar_M, + pokechar_E, + pokechar_W, + pokechar_STOP_BYTE, + pokechar_NULL_BYTE, + pokechar_NULL_BYTE, + pokechar_NULL_BYTE, + pokechar_NULL_BYTE, + pokechar_NULL_BYTE, + pokechar_NULL_BYTE, + pokechar_NULL_BYTE, +}; + +typedef struct TraderPacket { + // Name must not exceed 10 characters + 1 STOP_BYTE + // Any leftover space after STOP_BYTE must be filled with NULL_BYTE + unsigned char name[11]; + struct SelectedPokemon selected_pokemon; + struct PartyMember pokemon[6]; + unsigned char original_trainer_names[6][11]; + unsigned char pokemon_nicknames[6][11]; +} TraderPacket; + +void party_member_to_bytes(struct PartyMember *pPartyMember, uint8_t *out) { + uint8_t res[44] = { + pPartyMember->pokemon, + (uint8_t) (pPartyMember->current_hp >> 8), + (uint8_t) (pPartyMember->current_hp & 0x00FF), + pPartyMember->level, + pPartyMember->status, + pPartyMember->type1, + pPartyMember->type2, + pPartyMember->catch_rate_or_held_item, + pPartyMember->move1, + pPartyMember->move2, + pPartyMember->move3, + pPartyMember->move4, + (uint8_t) (pPartyMember->original_trainer_id >> 8), + (uint8_t) (pPartyMember->original_trainer_id & 0x00FF), + (uint8_t) ((pPartyMember->experience & 0x00FF0000) >> 16), + (uint8_t) ((pPartyMember->experience & 0x0000FF00) >> 8), + (uint8_t) (pPartyMember->experience & 0x000000FF), + (uint8_t) (pPartyMember->HP_ev >> 8), + (uint8_t) (pPartyMember->HP_ev & 0x00FF), + (uint8_t) (pPartyMember->attack_ev >> 8), + (uint8_t) (pPartyMember->attack_ev & 0x00FF), + (uint8_t) (pPartyMember->defense_ev >> 8), + (uint8_t) (pPartyMember->defense_ev & 0x00FF), + (uint8_t) (pPartyMember->speed_ev >> 8), + (uint8_t) (pPartyMember->speed_ev & 0x00FF), + (uint8_t) (pPartyMember->special_ev >> 8), + (uint8_t) (pPartyMember->special_ev & 0x00FF), + (uint8_t) (((pPartyMember->attack_iv & 0xF) << 4) | (pPartyMember->defense_iv & 0xF)), + (uint8_t) (((pPartyMember->speed_iv & 0xF) << 4) | (pPartyMember->special_iv & 0xF)), + pPartyMember->move1_pp, + pPartyMember->move2_pp, + pPartyMember->move3_pp, + pPartyMember->move4_pp, + pPartyMember->level, + (uint8_t) (pPartyMember->max_hp >> 8), + (uint8_t) (pPartyMember->max_hp & 0x00FF), + (uint8_t) (pPartyMember->attack >> 8), + (uint8_t) (pPartyMember->attack & 0x00FF), + (uint8_t) (pPartyMember->defense >> 8), + (uint8_t) (pPartyMember->defense & 0x00FF), + (uint8_t) (pPartyMember->speed >> 8), + (uint8_t) (pPartyMember->speed & 0x00FF), + (uint8_t) (pPartyMember->special >> 8), + (uint8_t) (pPartyMember->special & 0x00FF), + }; + for (size_t i = 0; i < 44; i++) { + out[i] = res[i]; + } +} + +void selected_pokemon_to_bytes(struct SelectedPokemon *pSelectedPokemon, uint8_t *out) { + uint8_t res[8] = { + pSelectedPokemon->number, + pSelectedPokemon->pokemon[0], + pSelectedPokemon->pokemon[1], + pSelectedPokemon->pokemon[2], + pSelectedPokemon->pokemon[3], + pSelectedPokemon->pokemon[4], + pSelectedPokemon->pokemon[5], + 0xFF, + }; + for (size_t i = 0; i < 8; i++) { + out[i] = res[i]; + } +} + +void trader_packet_to_bytes(struct TraderPacket *pTraderPacket, uint8_t *out) { + uint8_t name_bytes[NAME_LEN]; + uint8_t selected_pokemon_bytes[SELP_LEN]; + uint8_t pokemon_bytes[POKE_LEN]; + uint8_t original_trainer_names_bytes[ORIG_LEN]; + uint8_t pokemon_nicknames_bytes[NICK_LEN]; + + // Serialize the data + for (size_t i = 0; i < NAME_LEN; i++) { + name_bytes[i] = (uint8_t) pTraderPacket->name[i]; + } + + // Calculate the selected_pokemon and pokemon w/ stats and such + for (size_t i = 0; i < PARTY_SIZE; i++) { + uint8_t poke[POKE_SIZE]; + party_member_to_bytes(&pTraderPacket->pokemon[i], poke); + + // Full Party Data (all stats and such) + for (size_t j = 0; j < POKE_SIZE; j++) { + pokemon_bytes[(i * POKE_SIZE) + j] = (uint8_t) poke[j]; + } + } + selected_pokemon_to_bytes(&pTraderPacket->selected_pokemon, selected_pokemon_bytes); + + + for (size_t i = 0; i < PARTY_SIZE; i++) { + for (size_t j = 0; j < NAME_LEN; j++) { + original_trainer_names_bytes[(i * NAME_LEN) + j] = (uint8_t) pTraderPacket->original_trainer_names[i][j]; + } + } + + for (size_t i = 0; i < PARTY_SIZE; i++) { + for (size_t j = 0; j < NAME_LEN; j++) { + pokemon_nicknames_bytes[(i * NAME_LEN) + j] = (uint8_t) pTraderPacket->pokemon_nicknames[i][j]; + } + } + + // Flatten the data + uint8_t res[FULL_LEN + 3]; + + for (size_t i = 0; i < NAME_LEN; i++) { + res[i] = name_bytes[i]; + } + for (size_t i = 0; i < SELP_LEN; i++) { + res[i + NAME_LEN] = selected_pokemon_bytes[i]; + } + for (size_t i = 0; i < POKE_LEN; i++) { + res[i + NAME_LEN + SELP_LEN] = pokemon_bytes[i]; + } + for (size_t i = 0; i < ORIG_LEN; i++) { + res[i + NAME_LEN + SELP_LEN + POKE_LEN] = original_trainer_names_bytes[i]; + } + for (size_t i = 0; i < NICK_LEN; i++) { + res[i + NAME_LEN + SELP_LEN + POKE_LEN + ORIG_LEN] = pokemon_nicknames_bytes[i]; + } + res[FULL_LEN] = 0x00; + res[FULL_LEN + 1] = 0x00; + res[FULL_LEN + 2] = 0x00; + // Write the bytes to *out + for (size_t i = 0; i < (FULL_LEN + 3); i++) { + out[i] = res[i]; + } +} + +uint8_t handle_byte(uint8_t in, size_t *counter) { + uint8_t out[1]; + + switch (connection_state) + { + case NOT_CONNECTED: + switch (in) + { + case PKMN_MASTER: + out[0] = PKMN_SLAVE; + break; + case PKMN_BLANK: + out[0] = PKMN_BLANK; + break; + case PKMN_CONNECTED: + connection_state = CONNECTED; + out[0] = PKMN_CONNECTED; + break; + } + break; + + case CONNECTED: + switch (in) + { + case PKMN_CONNECTED: + out[0] = PKMN_CONNECTED; + break; + case PKMN_TRADE_CENTRE: + // No byte known, just move on the next case + connection_state = TRADE_CENTRE; + break; + case PKMN_COLOSSEUM: + // No byte known, just move on the next case + // This case is not built out and I have no intention to do it + connection_state = COLOSSEUM; + break; + case PKMN_BREAK_LINK: + case PKMN_MASTER: + connection_state = NOT_CONNECTED; + out[0] = PKMN_BREAK_LINK; + break; + + default: + out[0] = in; + break; + } + break; + + case TRADE_CENTRE: + if(trade_state == INIT && in == 0x00) { + trade_state = READY; + out[0] = 0x00; + } else if(trade_state == READY && in == 0xFD) { + trade_state = DETECTED; + out[0] = 0xFD; + } else if(trade_state == DETECTED && in != 0xFD) { + out[0] = in; + trade_state = DATA_TX_RANDOM; + } else if(trade_state == DATA_TX_RANDOM && in == 0xFD) { + trade_state = DATA_TX_WAIT; + out[0] = 0xFD; + (*counter) = 0; + } else if (trade_state == DATA_TX_WAIT && in == 0xFD) { + out[0] = 0x00; + } else if(trade_state == DATA_TX_WAIT && in != 0xFD) { + (*counter) = 0; + // send first byte + out[0] = DATA_BLOCK[(*counter)]; + INPUT_BLOCK[(*counter)] = in; + trade_state = DATA_TX; + (*counter)++; + } else if(trade_state == DATA_TX) { + out[0] = DATA_BLOCK[(*counter)]; + INPUT_BLOCK[(*counter)] = in; + (*counter)++; + if((*counter) == 418) { + trade_state = DATA_TX_PATCH; + } + } else if(trade_state == DATA_TX_PATCH && in == 0xFD) { + (*counter) = 0; + out[0] = 0xFD; + } else if(trade_state == DATA_TX_PATCH && in != 0xFD) { + out[0] = in; + (*counter)++; + if((*counter) == 197) { + trade_state = TRADE_WAIT; + } + (*counter) = 0; + } else if(trade_state == TRADE_WAIT && (in & 0x60) == 0x60) { + if (in == 0x6f) { + trade_state = READY; + out[0] = 0x6f; + } else { + out[0] = 0x60; + trade_pokemon = in - 0x60; + } + } else if(trade_state == TRADE_WAIT && in == 0x00) { + out[0] = 0; + trade_state = TRADE_DONE; + } else if(trade_state == TRADE_DONE && (in & 0x60) == 0x60) { + out[0] = in; + if (in == 0x61) { + trade_pokemon = -1; + trade_state = TRADE_WAIT; + } else { + trade_state = DONE; + } + } else if(trade_state == DONE && in == 0x00) { + out[0] = 0; + trade_state = INIT; + } else { + out[0] = in; + } + break; + + default: + out[0] = in; + break; + } + + return out[0]; +} + +void main(void) +{ + CRITICAL { + add_SIO(nowait_int_handler); // disable waiting VRAM state before return + } + set_interrupts(SIO_IFLAG); // disable other interrupts. note: this disables sprite movement + + /** + * Trader Packet Init + */ + struct TraderPacket traderPacket; + for (size_t i = 0; i < 11; i++) { + traderPacket.name[i] = name[i]; + } + + struct SelectedPokemon *pSelectedPokemon = &traderPacket.selected_pokemon; + pSelectedPokemon->number = 6; + for (size_t i = 0; i < 6; i++) { + pSelectedPokemon->pokemon[i] = MEW; + } + + for (size_t i = 0; i < 6; i++) { + struct PartyMember *pPartyMember = &traderPacket.pokemon[i]; + pPartyMember->pokemon = MEW; + pPartyMember->current_hp = 100; + pPartyMember->max_hp = 130; + pPartyMember->level = 40; + pPartyMember->status = NONE; + pPartyMember->type1 = PSYCHIC_TYPE; + pPartyMember->type2 = PSYCHIC_TYPE; // If only one type, copy the first + pPartyMember->catch_rate_or_held_item = 0xFF; // R/G/B/Y (catch rate), G/S/C (held item), and Stadium (held item) use this byte differently + pPartyMember->move1 = TELEPORT; + pPartyMember->move2 = PSYWAVE; + pPartyMember->move3 = PSYCHIC; + pPartyMember->move4 = FLY; + pPartyMember->original_trainer_id = 0xA455; // In decimal, these are the funny numbers + + // - Experience is complicated. You must look up the Pokemon you are trying to trade + // in the following table and apply the experience points that match the level. + // EXP LVL Table for gen 1: https://pwo-wiki.info/index.php/Generation_I_Experience_Charts + // That source was the best I could find for Gen 1. If you find another, submit a PR or open an issue and I'll fix it + // - Experience is a 24bit number, we will be dropping the MSB to acheive that + pPartyMember->experience = 190148; + + // Effort Values + // These are very specific to the Pokemon and who they battled in the past or what vitamins they were fed + // Luckily, these get recalculated when you level them up, or when you put them in a box and then put them back in your party + // For this example, I will take the max value and scale it to the level (65535 * 0.40) = 26214 + pPartyMember->HP_ev = 26214; + pPartyMember->attack_ev = 26214; + pPartyMember->defense_ev = 26214; + pPartyMember->speed_ev = 26214; + pPartyMember->special_ev = 26214; + + // IVs are a 4 bit number, so the max value is 15 (0-15 = 0b0000-0b1111 = 0x0-0xF) + // These have been broken out for legibility, but will be condensed to only 2 bytes + pPartyMember->attack_iv = 0xF; + pPartyMember->defense_iv = 0xF; + pPartyMember->speed_iv = 0xF; + pPartyMember->special_iv = 0xF; + + pPartyMember->move1_pp = 20; + pPartyMember->move2_pp = 15; + pPartyMember->move3_pp = 10; + pPartyMember->move4_pp = 15; + + pPartyMember->attack = 100; + pPartyMember->defense = 100; + pPartyMember->speed = 100; + pPartyMember->special = 100; + } + + for (size_t i = 0; i < 6; i++) { + for (size_t j = 0; j < 11; j++) { + traderPacket.original_trainer_names[i][j] = name[j]; + } + } + for (size_t i = 0; i < 6; i++) { + for (size_t j = 0; j < 11; j++) { + traderPacket.pokemon_nicknames[i][j] = nicknames[j]; + } + } + + trader_packet_to_bytes(&traderPacket, DATA_BLOCK); + + puts("Poke Distribution"); + puts("Copyright 2023"); + puts("BreadCodes"); + puts(""); + puts("Reset: Press Button"); + + size_t trade_counter = 0; + while(1) { + uint8_t in = _io_in; + + _io_out = handle_byte(in, &trade_counter); + + // No-Op loop to delay the bytes being sent. + // We do this because the serial interface halts operations on + // the other Gameboy and freezes the game, so we need to give it + // time to think. + for (int i = 0; i < 500; i++) { + __asm + NOP + __endasm; + } + + trade_byte(); + + while(_io_status == IO_RECEIVING || _io_status == IO_SENDING); + + if (joypad() > 0) { + connection_state = NOT_CONNECTED; + trade_state = INIT; + } + } +} diff --git a/gen1.h b/gen1.h new file mode 100644 index 0000000..605d665 --- /dev/null +++ b/gen1.h @@ -0,0 +1,619 @@ +#ifndef GEN1_H_ +#define GEN1_H_ + +#include +#include + +#define PKMN_BLANK 0x00 + +#define ITEM_1_HIGHLIGHTED 0xD0 +#define ITEM_2_HIGHLIGHTED 0xD1 +#define ITEM_3_HIGHLIGHTED 0xD2 +#define ITEM_1_SELECTED 0xD4 +#define ITEM_2_SELECTED 0xD5 +#define ITEM_3_SELECTED 0xD6 + +#define PKMN_MASTER 0x01 +#define PKMN_SLAVE 0x02 +#define PKMN_CONNECTED 0x60 +// #define PKMN_CONNECTED 0x03 +#define PKMN_WAIT 0x7F + +#define PKMN_ACTION 0x60 + +#define PKMN_TRADE_CENTRE ITEM_1_SELECTED +#define PKMN_COLOSSEUM ITEM_2_SELECTED +#define PKMN_BREAK_LINK ITEM_3_SELECTED + +#define TRADE_CENTRE_WAIT 0xFD + +#define pokechar_STOP_BYTE 0x50 +// Ends a string +#define pokechar_NULL_BYTE 0x00 +// Nothing, comes after the stop byte to fill in the rest + +// ENG Characters +#define pokechar_SPACE 0x7F +#define pokechar_A 0x80 +#define pokechar_B 0x81 +#define pokechar_C 0x82 +#define pokechar_D 0x83 +#define pokechar_E 0x84 +#define pokechar_F 0x85 +#define pokechar_G 0x86 +#define pokechar_H 0x87 +#define pokechar_I 0x88 +#define pokechar_J 0x89 +#define pokechar_K 0x8A +#define pokechar_L 0x8B +#define pokechar_M 0x8C +#define pokechar_N 0x8D +#define pokechar_O 0x8E +#define pokechar_P 0x8F +#define pokechar_Q 0x90 +#define pokechar_R 0x91 +#define pokechar_S 0x92 +#define pokechar_T 0x93 +#define pokechar_U 0x94 +#define pokechar_V 0x95 +#define pokechar_W 0x96 +#define pokechar_X 0x97 +#define pokechar_Y 0x98 +#define pokechar_Z 0x99 +#define pokechar_OPEN_PARENTHESIS 0x9A +#define pokechar_CLOSE_PARENTHESIS 0x9B +#define pokechar_COLON 0x9C +#define pokechar_SEMICOLON 0x9D +#define pokechar_OPEN_BRACKET 0x9E +#define pokechar_CLOSE_BRACKET 0x9F +#define pokechar_a 0xA0 +#define pokechar_b 0xA1 +#define pokechar_c 0xA2 +#define pokechar_d 0xA3 +#define pokechar_e 0xA4 +#define pokechar_f 0xA5 +#define pokechar_g 0xA6 +#define pokechar_h 0xA7 +#define pokechar_i 0xA8 +#define pokechar_j 0xA9 +#define pokechar_k 0xAa +#define pokechar_l 0xAb +#define pokechar_m 0xAc +#define pokechar_n 0xAd +#define pokechar_o 0xAe +#define pokechar_p 0xAf +#define pokechar_q 0xB0 +#define pokechar_r 0xB1 +#define pokechar_s 0xB2 +#define pokechar_t 0xB3 +#define pokechar_u 0xB4 +#define pokechar_v 0xB5 +#define pokechar_w 0xB6 +#define pokechar_x 0xB7 +#define pokechar_y 0xB8 +#define pokechar_z 0xB9 +#define pokechar_e_accent 0xBA +#define pokechar_apostrophe_d 0xBB +#define pokechar_apostrophe_l 0xBC +#define pokechar_apostrophe_s 0xBD +#define pokechar_apostrophe_t 0xBE +#define pokechar_apostrophe_v 0xBF +#define pokechar_apostrophe 0xE0 +#define pokechar_PK_character 0xE1 +#define pokechar_MN_character 0xE2 +#define pokechar_hyphen 0xE3 +#define pokechar_apostrophe_r 0xE4 +#define pokechar_apostrophe_m 0xE5 +#define pokechar_question_mark 0xE6 +#define pokechar_exclamation_mark 0xE7 + +// NOTE: `period` is never used in names. Instead use `decimal_point` +// . (used as punctuation) +#define pokechar_period 0xE8 + +#define pokechar_hollow_play 0xEC +// ▷ +#define pokechar_solid_play 0xED +// ▶ +#define pokechar_solid_down 0xEE +// ▼ +#define pokechar_male_symbol 0xEF +// ♂ +#define pokechar_pokemon_dollar 0xF0 +#define pokechar_multiply 0xF1 +// × +#define pokechar_decimal_point 0xF2 +// . (used as a decimal point) +#define pokechar_slash_forward 0xF3 +// / +#define pokechar_comma 0xF4 +// , +#define pokechar_female_symbol 0xF5 +// ♀ +#define pokechar_zero 0xF6 +#define pokechar_one 0xF7 +#define pokechar_two 0xF8 +#define pokechar_three 0xF9 +#define pokechar_four 0xFA +#define pokechar_five 0xFB +#define pokechar_six 0xFC +#define pokechar_seven 0xFD +#define pokechar_eight 0xFE +#define pokechar_nine 0xFF +// Characters that weren't removed from JPN when translating to ENG +// Some seem bolded, some are symbols, others are katakana or hiragana letters +#define pokechar_JPN_A 0x60 +#define pokechar_JPN_B 0x61 +#define pokechar_JPN_C 0x62 +#define pokechar_JPN_D 0x63 +#define pokechar_JPN_E 0x64 +#define pokechar_JPN_F 0x65 +#define pokechar_JPN_G 0x66 +#define pokechar_JPN_H 0x67 +#define pokechar_JPN_I 0x68 +#define pokechar_JPN_V 0x69 +#define pokechar_JPN_S 0x6A +#define pokechar_JPN_L 0x6B +#define pokechar_JPN_M 0x6C +#define pokechar_JPN_colon 0x6D +#define pokechar_hiragana_i 0x6E +// ぃ +#define pokechar_hiragana_u 0x6F +// ぅ +#define pokechar_open_single_quote 0x70 +// ‘ +#define pokechar_close_single_quote 0x71 +// ’ +#define pokechar_open_double_quote 0x72 +// “ +#define pokechar_close_double_quote 0x73 +// ” +#define pokechar_dot 0x74 +// ・ +#define pokechar_ellipsis 0x75 +// ⋯ +#define pokechar_hiragana_a 0x76 +// ぁ +#define pokechar_hiragana_e 0x77 +// ぇ +#define pokechar_hiragana_o 0x78 +// ぉ +#define pokechar_katakana_a 0xE9 +// ァ +#define pokechar_katakana_u 0xEA +// ゥ +#define pokechar_katakana_e 0xEB +// ェ + +enum connection_state_t { + TRADE_CENTRE, + NOT_CONNECTED, + CONNECTED, + COLOSSEUM, +}; + +enum trade_state_t { + INIT = 0x00, + READY = 0x01, + DETECTED = 0x02, + DATA_TX = 0x03, + DATA_TX_WAIT = 0x04, + DATA_TX_WAIT_HOLD = 0x05, + DATA_TX_START = 0x06, + DATA_TX_RANDOM = 0x07, + DATA_TX_PATCH = 0x08, + TRADE_WAIT = 0x09, + TRADE_DONE = 0x0A, + DONE = 0x0B +}; + +enum gen_one_dex_t { + NULL_POKEMON = 0xFF, + RHYDON = 0x01, + KANGASKHAN = 0x02, + NIDORAN_MALE = 0x03, + CLEFAIRY = 0x04, + SPEAROW = 0x05, + VOLTORB = 0x06, + NIDOKING = 0x07, + SLOWBRO = 0x08, + IVYSAUR = 0x09, + EXEGGUTOR = 0x0A, + LICKITUNG = 0x0B, + EXEGGCUTE = 0x0C, + GRIMER = 0x0D, + GENGAR = 0x0E, + NIDORAN_FEMALE = 0x0F, + NIDOQUEEN = 0x10, + CUBONE = 0x11, + RHYHORN = 0x12, + LAPRAS = 0x13, + ARCANINE = 0x14, + MEW = 0x15, + GYARADOS = 0x16, + SHELLDER = 0x17, + TENTACOOL = 0x18, + GASTLY = 0x19, + SCYTHER = 0x1A, + STARYU = 0x1B, + BLASTOISE = 0x1C, + PINSIR = 0x1D, + TANGELA = 0x1E, + GROWLITHE = 0x21, + ONIX = 0x22, + FEAROW = 0x23, + PIDGEY = 0x24, + SLOWPOKE = 0x25, + KADABRA = 0x26, + GRAVELER = 0x27, + CHANSEY = 0x28, + MACHOKE = 0x29, + MR_MIME = 0x2A, + HITMONLEE = 0x2B, + HITMONCHAN = 0x2C, + ARBOK = 0x2D, + PARASECT = 0x2E, + PSYDUCK = 0x2F, + DROWZEE = 0x30, + GOLEM = 0x31, + MAGMAR = 0x33, + ELECTABUZZ = 0x35, + MAGNETON = 0x36, + KOFFING = 0x37, + MANKEY = 0x39, + SEEL = 0x3A, + DIGLETT = 0x3B, + TAUROS = 0x3C, + FARFETCH = 0x40, + VENONAT = 0x41, + DRAGONITE = 0x42, + DODUO = 0x46, + POLIWAG = 0x47, + JYNX = 0x48, + MOLTRES = 0x49, + ARTICUNO = 0x4A, + ZAPDOS = 0x4B, + DITTO = 0x4C, + MEOWTH = 0x4D, + KRABBY = 0x4E, + VULPIX = 0x52, + NINETALES = 0x53, + PIKACHU = 0x54, + RAICHU = 0x55, + DRATINI = 0x58, + DRAGONAIR = 0x59, + KABUTO = 0x5A, + KABUTOPS = 0x5B, + HORSEA = 0x5C, + SEADRA = 0x5D, + SANDSHREW = 0x60, + SANDSLASH = 0x61, + OMANYTE = 0x62, + OMASTAR = 0x63, + JIGGLYPUFF = 0x64, + WIGGLYTUFF = 0x65, + EEVEE = 0x66, + FLAREON = 0x67, + JOLTEON = 0x68, + VAPOREON = 0x69, + MACHOP = 0x6A, + ZUBAT = 0x6B, + EKANS = 0x6C, + PARAS = 0x6D, + POLIWHIRL = 0x6E, + POLIWRATH = 0x6F, + WEEDLE = 0x70, + KAKUNA = 0x71, + BEEDRILL = 0x72, + DODRIO = 0x74, + PRIMEAPE = 0x75, + DUGTRIO = 0x76, + VENOMOTH = 0x77, + DEWGONG = 0x78, + CATERPIE = 0x7B, + METAPOD = 0x7C, + BUTTERFREE = 0x7D, + MACHAMP = 0x7E, + GOLDUCK = 0x80, + HYPNO = 0x81, + GOLBAT = 0x82, + MEWTWO = 0x83, + SNORLAX = 0x84, + MAGIKARP = 0x85, + MUK = 0x88, + KINGLER = 0x8A, + CLOYSTER = 0x8B, + ELECTRODE = 0x8D, + CLEFABLE = 0x8E, + WEEZING = 0x8F, + PERSIAN = 0x90, + MAROWAK = 0x91, + HAUNTER = 0x93, + ABRA = 0x94, + ALAKAZAM = 0x95, + PIDGEOTTO = 0x96, + PIDGEOT = 0x97, + STARMIE = 0x98, + BULBASAUR = 0x99, + VENUSAUR = 0x9A, + TENTACRUEL = 0x9B, + GOLDEEN = 0x9D, + SEAKING = 0x9E, + PONYTA = 0xA3, + RAPIDASH = 0xA4, + RATTATA = 0xA5, + RATICATE = 0xA6, + NIDORINO = 0xA7, + NIDORINA = 0xA8, + GEODUDE = 0xA9, + PORYGON = 0xAA, + AERODACTYL = 0xAB, + MAGNEMITE = 0xAD, + CHARMANDER = 0xB0, + SQUIRTLE = 0xB1, + CHARMELEON = 0xB2, + WARTORTLE = 0xB3, + CHARIZARD = 0xB4, + ODDISH = 0xB9, + GLOOM = 0xBA, + VILEPLUME = 0xBB, + BELLSPROUT = 0xBC, + WEEPINBELL = 0xBD, + VICTREEBEL = 0xBE, +}; + +enum poke_move_t { + ABSORB = 0x47, + ACID = 0x33, + ACID_ARMOR = 0x97, + AGILITY = 0x61, + AMNESIA = 0x85, + AURORA_BEAM = 0x3E, + BARRAGE = 0x8C, + BARRIER = 0x70, + BIDE = 0x75, + BIND = 0x14, + BITE = 0x2C, + BLIZZARD = 0x3B, + BODY_SLAM = 0x22, + BONE_CLUB = 0x7D, + BONEMERANG = 0x9B, + BUBBLE = 0x91, + BUBBLEBEAM = 0x3D, + CLAMP = 0x80, + COMET_PUNCH = 0x04, + CONFUSE_RAY = 0x6D, + CONFUSION = 0x5D, + CONSTRICT = 0x84, + CONVERSION = 0xA0, + COUNTER = 0x44, + CRABHAMMER = 0x98, + CUT = 0x0F, + DEFENSE_CURL = 0x6F, + DIG = 0x5B, + DISABLE = 0x32, + DIZZY_PUNCH = 0x92, + DOUBLE_EDGE = 0x26, + DOUBLE_KICK = 0x18, + DOUBLE_SLAP = 0x03, + DOUBLE_TEAM = 0x68, + DRAGON_RAGE = 0x52, + DREAM_EATER = 0x8A, + DRILL_PECK = 0x41, + EARTHQUAKE = 0x59, + EGG_BOMB = 0x79, + EMBER = 0x34, + EXPLOSION = 0x99, + FIRE_BLAST = 0x7E, + FIRE_PUNCH = 0x07, + FIRE_SPIN = 0x53, + FISSURE = 0x5A, + FLAMETHROWER = 0x35, + FLASH = 0x94, + FLY = 0x13, + FOCUS_ENERGY = 0x74, + FURY_ATTACK = 0x1F, + FURY_SWIPES = 0x9A, + GLARE = 0x89, + GROWL = 0x2D, + GROWTH = 0x4A, + GUILLOTINE = 0x0C, + GUST = 0x10, + HARDEN = 0x6A, + HAZE = 0x72, + HEADBUTT = 0x1D, + HI_JUMP_KICK = 0x88, + HORN_ATTACK = 0x1E, + HORN_DRILL = 0x20, + HYDRO_PUMP = 0x38, + HYPER_BEAM = 0x3F, + HYPER_FANG = 0x9E, + HYPNOSIS = 0x5F, + ICE_BEAM = 0x3A, + ICE_PUNCH = 0x08, + JUMP_KICK = 0x1A, + KARATE_CHOP = 0x02, + KINESIS = 0x86, + LEECH_LIFE = 0x8D, + LEECH_SEED = 0x49, + LEER = 0x2B, + LICK = 0x7A, + LIGHT_SCREEN = 0x71, + LOVELY_KISS = 0x8E, + LOW_KICK = 0x43, + MEDITATE = 0x60, + MEGA_DRAIN = 0x48, + MEGA_KICK = 0x19, + MEGA_PUNCH = 0x05, + METRONOME = 0x76, + MIMIC = 0x66, + MINIMIZE = 0x6B, + MIRROR_MOVE = 0x77, + MIST = 0x36, + NIGHT_SHADE = 0x65, + PAYDAY = 0x06, + PECK = 0x40, + PETAL_DANCE = 0x50, + PIN_MISSILE = 0x2A, + POISON_GAS = 0x8B, + POISON_STING = 0x28, + POISON_POWDER = 0x4D, + POUND = 0x01, + PSYBEAM = 0x3C, + PSYCHIC = 0x5E, + PSYWAVE = 0x95, + QUICK_ATTACK = 0x62, + RAGE = 0x63, + RAZOR_LEAF = 0x4B, + RAZOR_WIND = 0x0D, + RECOVER = 0x69, + REFLECT = 0x73, + REST = 0x9C, + ROAR = 0x2E, + ROCK_SLIDE = 0x9D, + ROCK_THROW = 0x58, + ROLLING_KICK = 0x1B, + SAND_ATTACK = 0x1C, + SCRATCH = 0x0A, + SCREECH = 0x67, + SEISMIC_TOSS = 0x45, + SELFDESTRUCT = 0x78, + SHARPEN = 0x9F, + SING = 0x2F, + SKULL_BASH = 0x82, + SKY_ATTACK = 0x8F, + SLAM = 0x15, + SLASH = 0xA3, + SLEEP_POWDER = 0x4F, + SLUDGE = 0x7C, + SMOG = 0x7B, + SMOKESCREEN = 0x6C, + SOFTBOILED = 0x87, + SOLAR_BEAM = 0x4C, + SONIC_BOOM = 0x31, + SPIKE_CANNON = 0x83, + SPLASH = 0x96, + SPORE = 0x93, + STOMP = 0x17, + STRENGTH = 0x46, + STRING_SHOT = 0x51, + STRUGGLE = 0xA5, + STUN_SPORE = 0x4E, + SUBMISSION = 0x42, + SUBSTITUTE = 0xA4, + SUPER_FANG = 0xA2, + SUPERSONIC = 0x30, + SURF = 0x39, + SWIFT = 0x81, + SWORDS_DANCE = 0x0E, + TACKLE = 0x21, + TAIL_WHIP = 0x27, + TAKE_DOWN = 0x24, + TELEPORT = 0x64, + THRASH = 0x25, + THUNDER = 0x57, + THUNDERBOLT = 0x55, + THUNDER_PUNCH = 0x09, + THUNDERSHOCK = 0x54, + THUNDERWAVE = 0x56, + TOXIC = 0x5C, + TRANSFORM = 0x90, + TRI_ATTACK = 0xA1, + TWINEEDLE = 0x29, + VICE_GRIP = 0x0B, + VINE_WHIP = 0x16, + WATER_GUN = 0x37, + WATERFALL = 0x7F, + WHIRLWIND = 0x12, + WING_ATTACK = 0x11, + WITHDRAW = 0x6E, + WRAP = 0x23, +}; + +enum status_condition_t { + NONE = 0x00, + ASLEEP = 0x04, + BURNED = 0x10, + FROZEN = 0x20, + PARALYZED = 0x40, + POISONED = 0x08, +}; + +enum poke_type_t { + NORMAL = 0x00, + FIGHTING = 0x01, + FLYING = 0x02, + POISON = 0x03, + GROUND = 0x04, + ROCK = 0x05, + BUG = 0x07, + GHOST = 0x08, + FIRE = 0x14, + WATER = 0x15, + GRASS = 0x16, + ELECTRIC = 0x17, + PSYCHIC_TYPE = 0x18, + ICE = 0x19, + DRAGON = 0x1A, +}; + +typedef struct SelectedPokemon { + // Number of pokemon in the trainer's party + uint8_t number; + enum gen_one_dex_t pokemon[6]; +} SelectedPokemon; + + +// Luckily it follows the save file structure closely enough +// https://bulbapedia.bulbagarden.net/wiki/Pok%C3%A9mon_data_structure_(Generation_I) +typedef struct PartyMember { + enum gen_one_dex_t pokemon; + uint16_t current_hp; + uint16_t max_hp; + uint8_t level; + enum status_condition_t status; + enum poke_type_t type1; + enum poke_type_t type2; // If only one type, copy the first + uint8_t catch_rate_or_held_item; // R/G/B/Y (catch rate), G/S/C (held item), and Stadium (held item) use this byte differently + enum poke_move_t move1; + enum poke_move_t move2; + enum poke_move_t move3; + enum poke_move_t move4; + uint16_t original_trainer_id; // In decimal, these are the funny numbers + + // - Experience is complicated. You must look up the Pokemon you are trying to trade + // in the following table and apply the experience points that match the level. + // EXP LVL Table for gen 1: https://pwo-wiki.info/index.php/Generation_I_Experience_Charts + // That source was the best I could find for Gen 1. If you find another, submit a PR or open an issue and I'll fix it + // - Experience is a 24bit number, we will be dropping the MSB to acheive that + uint32_t experience; + + // Effort Values + // These are very specific to the Pokemon and who they battled in the past or what vitamins they were fed + // Luckily, these get recalculated when you level them up, or when you put them in a box and then put them back in your party + // For this example, I will take the max value and scale it to the level (65535 * 0.40) = 26214 + uint16_t HP_ev; + uint16_t attack_ev; + uint16_t defense_ev; + uint16_t speed_ev; + uint16_t special_ev; + + // IVs are a 4 bit number, so the max value is 15 (0-15 = 0b0000-0b1111 = 0x0-0xF) + // These have been broken out for legibility, but will be condensed to only 2 bytes + uint8_t attack_iv; + uint8_t defense_iv; + uint8_t speed_iv; + uint8_t special_iv; + + uint8_t move1_pp; + uint8_t move2_pp; + uint8_t move3_pp; + uint8_t move4_pp; + + uint16_t attack; + uint16_t defense; + uint16_t speed; + uint16_t special; +} PartyMember; + +#endif /* GEN1_H_ */ \ No newline at end of file