Skip to content

Commit

Permalink
Add basic arceus support
Browse files Browse the repository at this point in the history
  • Loading branch information
zaksabeast committed Jan 30, 2022
1 parent 98b417e commit 51c542d
Show file tree
Hide file tree
Showing 19 changed files with 766 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libs/csight-core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/csight-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ crate-type = ["staticlib"]
num_enum = { version = "0.5", default-features = false }
cstr_core = { version = "0.2.4", default-features = false, features = ["alloc"] }
no_std_io = { git = "https://github.com/zaksabeast/no_std_io.git", rev = "77499d6" }
pkm-rs = { git = "https://github.com/zaksabeast/pkm-rs.git", rev = "d8c9316" }
pkm-rs = { git = "https://github.com/zaksabeast/pkm-rs.git", rev = "f9b89aa" }
safe-transmute = { version = "0.11", default-features = false }

[target.'cfg(target_os = "horizon")'.dependencies]
Expand Down
28 changes: 28 additions & 0 deletions libs/csight-core/c-export/arceus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "pk9.h"
#include "rng_tracker.h"

#ifdef __cplusplus
#include <memory>

namespace csight::arceus {
extern "C" {
#endif

pk9_t *arceus_read_party_pokemon(u8 index);
pk9_t *arceus_read_wild_pokemon();
xoroshiro_tracker_t *arceus_get_main_rng_tracker();

#ifdef __cplusplus
}

std::shared_ptr<Pk9> read_party_pokemon(u8 index) { return std::make_shared<Pk9>(arceus_read_party_pokemon(index)); }

std::shared_ptr<Pk9> read_wild_pokemon() { return std::make_shared<Pk9>(arceus_read_wild_pokemon()); }

std::shared_ptr<RngTracker> get_main_rng_tracker() {
return std::make_shared<RngTracker>((void *)arceus_get_main_rng_tracker(), RngType::Xoroshiro);
}
};
#endif
3 changes: 3 additions & 0 deletions libs/csight-core/c-export/csight-core.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once

#include "arceus.h"
#include "bdsp.h"
#include "den.h"
#include "pk8.h"
#include "pk9.h"
#include "rng_tracker.h"
#include "spawn.h"
#include "swsh.h"
#include "trainer_info.h"
#include <switch.h>
Expand Down
130 changes: 130 additions & 0 deletions libs/csight-core/c-export/pk9.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#pragma once

#include "pkx.h"
#include <switch.h>

#ifdef __cplusplus

#include <array>
#include <optional>
#include <string>

namespace csight {
extern "C" {
#endif

typedef struct pk9 pk9_t;

void free_pk9(pk9_t *ptr);

u32 pk9_encryption_constant(pk9_t *ptr);
void pk9_species_string(pk9_t *ptr, char *out, size_t out_size);
u16 pk9_tid(pk9_t *ptr);
u16 pk9_sid(pk9_t *ptr);
void pk9_ability_string(pk9_t *ptr, char *out, size_t out_size);
u8 pk9_ability_number(pk9_t *ptr);
u32 pk9_pid(pk9_t *ptr);
void pk9_nature_string(pk9_t *ptr, char *out, size_t out_size);
void pk9_minted_nature_string(pk9_t *ptr, char *out, size_t out_size);
u8 pk9_gender(pk9_t *ptr);
Stats pk9_evs(pk9_t *ptr);
Stats pk9_ivs(pk9_t *ptr);
void pk9_move1_string(pk9_t *ptr, char *out, size_t out_size);
void pk9_move2_string(pk9_t *ptr, char *out, size_t out_size);
void pk9_move3_string(pk9_t *ptr, char *out, size_t out_size);
void pk9_move4_string(pk9_t *ptr, char *out, size_t out_size);
u8 pk9_language(pk9_t *ptr);
u8 pk9_current_friendship(pk9_t *ptr);
bool pk9_is_shiny(pk9_t *ptr);
bool pk9_is_egg(pk9_t *ptr);
bool find_pk9_raid_seed(u64 *out, pk9_t *ptr);

#ifdef __cplusplus
}

class Pk9 : public Pkx {
public:
static const size_t StoredSize = 328;

Pk9(pk9_t *pk9) : m_pk9(pk9) { }
~Pk9() { free_pk9(m_pk9); }

u32 EncryptionConstant() { return pk9_encryption_constant(m_pk9); }

std::string SpeciesString() {
char text[13];
pk9_species_string(m_pk9, text, 13);
return std::string(text);
}

u16 Tid() { return pk9_tid(m_pk9); }

u16 Sid() { return pk9_sid(m_pk9); }

std::string AbilityString() {
char text[17];
pk9_ability_string(m_pk9, text, 17);
return std::string(text);
}

u8 AbilityNumber() { return pk9_ability_number(m_pk9); }

u32 Pid() { return pk9_pid(m_pk9); }

std::string NatureString() {
char text[8];
pk9_nature_string(m_pk9, text, 8);
return std::string(text);
}

std::string MintedNatureString() {
char text[8];
pk9_minted_nature_string(m_pk9, text, 8);
return std::string(text);
}

u8 Gender() { return pk9_gender(m_pk9); }

Stats Evs() { return pk9_evs(m_pk9); }

Stats Ivs() { return pk9_ivs(m_pk9); }

std::string Move1String() {
char text[28];
pk9_move1_string(m_pk9, text, 28);
return std::string(text);
}

std::string Move2String() {
char text[28];
pk9_move2_string(m_pk9, text, 28);
return std::string(text);
}

std::string Move3String() {
char text[28];
pk9_move3_string(m_pk9, text, 28);
return std::string(text);
}

std::string Move4String() {
char text[28];
pk9_move4_string(m_pk9, text, 28);
return std::string(text);
}

u8 CurrentFriendship() { return pk9_current_friendship(m_pk9); }

u8 Language() { return pk9_language(m_pk9); }

bool IsShiny() { return pk9_is_shiny(m_pk9); }

bool IsEgg() { return pk9_is_egg(m_pk9); }

std::optional<u64> FindRaidSeed() { return std::nullopt; }

private:
pk9_t *m_pk9;
};
};
#endif
71 changes: 71 additions & 0 deletions libs/csight-core/c-export/spawn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <memory>
#include <switch.h>
#include <vector>

#ifdef __cplusplus

namespace csight::arceus {
extern "C" {
#endif
typedef struct arceus_spawn arceus_spawn_t;

u64 spawn_get_hash(arceus_spawn_t *ptr);
u64 spawn_get_seed(arceus_spawn_t *ptr);
bool spawn_get_is_active(arceus_spawn_t *ptr);
arceus_spawn_t *free_spawn(arceus_spawn_t *ptr);
arceus_spawn_t *arceus_read_spawn(size_t index);
arceus_spawn_t *arceus_read_next_active_spawn(size_t start_index, size_t *found_index);
size_t arceus_read_active_spawn_count();
size_t arceus_read_spawn_count();

#ifdef __cplusplus
}

class Spawn {
public:
static const size_t Size = 0x18;

Spawn(size_t index, bool find_next_active = false) {
if (find_next_active) {
m_spawn = arceus_read_next_active_spawn(index, &m_index);
} else {
m_spawn = arceus_read_spawn(index);
m_index = index;
}
}
~Spawn() { free_spawn(m_spawn); }

size_t Index() { return m_index; }

u64 Hash() { return spawn_get_hash(m_spawn); }

u64 Seed() { return spawn_get_seed(m_spawn); }

bool IsActive() { return spawn_get_is_active(m_spawn); }

private:
arceus_spawn_t *m_spawn;
size_t m_index;
};

// TODO: clean. This was super dirty and quick
std::vector<std::shared_ptr<Spawn>> readActiveSpawns(size_t start_index, size_t end_index) {
std::vector<std::shared_ptr<Spawn>> result;

for (size_t next_index = start_index; next_index < end_index;) {
auto spawn = std::make_shared<Spawn>(next_index, true);

if (!spawn->IsActive()) {
break;
}

next_index = spawn->Index() + 1;
result.push_back(spawn);
}

return result;
}
};
#endif
7 changes: 7 additions & 0 deletions libs/csight-core/src/arceus/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod read;
pub use read::*;

mod offsets;
pub mod rng;

mod spawn;
9 changes: 9 additions & 0 deletions libs/csight-core/src/arceus/offsets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use num_enum::IntoPrimitive;

#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive)]
#[repr(u64)]
pub enum Offset {
PlayerSingleton = 0x4268000,
BattleSingleton = 0x4267f00,
SpawnSingleton = 0x4267ee0,
}
Loading

0 comments on commit 51c542d

Please sign in to comment.