Skip to content

Commit

Permalink
fix spawn_player position
Browse files Browse the repository at this point in the history
  • Loading branch information
Dregu committed Oct 13, 2023
1 parent a312d78 commit f3cdf00
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
23 changes: 19 additions & 4 deletions src/game_api/spawn_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,18 +662,33 @@ void init_spawn_hooks()
}
}

void spawn_player(int8_t player_slot, float x, float y)
int32_t spawn_player(int8_t player_slot, std::optional<float> x, std::optional<float> y)
{
if (player_slot < 1 || player_slot > 4)
return;
return -1;
auto state = State::get().ptr();
auto& slot = state->items->player_select_slots[player_slot - 1];
if (slot.character < to_id("ENT_TYPE_CHAR_ANA_SPELUNKY") || slot.character > to_id("ENT_TYPE_CHAR_CLASSIC_GUY"))
return -1;
if (state->items->player_count < player_slot)
state->items->player_count = player_slot;
slot.activated = true;

push_spawn_type_flags(SPAWN_TYPE_SCRIPT);
OnScopeExit pop{[]
{ pop_spawn_type_flags(SPAWN_TYPE_SCRIPT); }};

using spawn_player_fun = void(Items*, uint8_t ps, float pos_x, float pos_y);
auto old_x = state->level_gen->spawn_x;
auto old_y = state->level_gen->spawn_y;
state->level_gen->spawn_x = x.value_or(old_x);
state->level_gen->spawn_y = y.value_or(old_y);
auto uid = (int32_t)state->next_entity_uid;
using spawn_player_fun = void(Items*, uint8_t ps);
static auto spawn_player = (spawn_player_fun*)get_address("spawn_player");
spawn_player(get_state_ptr()->items, player_slot - 1, x, y);
spawn_player(get_state_ptr()->items, player_slot - 1);
state->level_gen->spawn_x = old_x;
state->level_gen->spawn_y = old_y;
return uid;
}

int32_t spawn_companion(ENT_TYPE companion_type, float x, float y, LAYER layer)
Expand Down
2 changes: 1 addition & 1 deletion src/game_api/spawn_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void pop_spawn_type_flags(SPAWN_TYPE flags);

void init_spawn_hooks();

void spawn_player(int8_t player_slot, float x, float y);
int32_t spawn_player(int8_t player_slot, std::optional<float> x = std::nullopt, std::optional<float> y = std::nullopt);
int32_t spawn_companion(ENT_TYPE companion_type, float x, float y, LAYER layer);
int32_t spawn_shopkeeper(float x, float y, LAYER layer, ROOM_TEMPLATE room_template = 65);
int32_t spawn_roomowner(ENT_TYPE owner_type, float x, float y, LAYER layer, int16_t room_template = -1);
Expand Down
4 changes: 2 additions & 2 deletions src/injected/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2324,7 +2324,7 @@ void respawn()
if (!found)
{
g_state->items->player_inventories[i].health = 4;
UI::spawn_player(i, g_state->level_gen->spawn_x, g_state->level_gen->spawn_y);
UI::spawn_player(i);
}
}
}
Expand Down Expand Up @@ -8071,7 +8071,7 @@ void render_game_props()
{
g_state->items->player_inventories[i].health = 4;
auto uid = g_state->next_entity_uid;
UI::spawn_player(i, spawn_x, spawn_y);
UI::spawn_player(i);
auto player = get_entity_ptr(uid)->as<Player>();
player->set_position(spawn_x, spawn_y);
}
Expand Down
2 changes: 1 addition & 1 deletion src/injected/ui_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ int32_t UI::spawn_playerghost(ENT_TYPE char_type, float x, float y, LAYER layer,
return uid;
}

void UI::spawn_player(uint8_t player_slot, float x, float y)
void UI::spawn_player(uint8_t player_slot, std::optional<float> x, std::optional<float> y)
{
::spawn_player(player_slot + 1, x, y);
}
Expand Down
2 changes: 1 addition & 1 deletion src/injected/ui_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class UI
static float get_spark_distance(SparkTrap* ent);
static void save_progress();
static int32_t spawn_playerghost(ENT_TYPE char_type, float x, float y, LAYER layer, float vx, float vy);
static void spawn_player(uint8_t player_slot, float x, float y);
static void spawn_player(uint8_t player_slot, std::optional<float> x = std::nullopt, std::optional<float> y = std::nullopt);
static std::pair<float, float> spawn_position();
static void load_death_screen();
};

0 comments on commit f3cdf00

Please sign in to comment.