Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patching the game code to fix crashes #331

Merged
merged 32 commits into from
Sep 26, 2023
Merged

Conversation

Mr-Auto
Copy link
Contributor

@Mr-Auto Mr-Auto commented Sep 16, 2023

Patching crashes when:

  • spawn too many orbs
  • liquid falling out of the level
  • olmec kill and not existence in olmec level/world
  • tiamat kill and not existence in tiamat level
  • add function to skip olmec cutscene
  • add function to skip tiamat cutscene

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 17, 2023

I assume we want them active depending on user settings?
or should i just put them together with the hooks init stuff?

@Dregu
Copy link
Collaborator

Dregu commented Sep 17, 2023

I think these kind of patches that have no effect on the vanilla game should just be enabled always when the api is initialized, so not in PL speedrun mode but otherwise automatically.

@Dregu
Copy link
Collaborator

Dregu commented Sep 17, 2023

I guess it would be perfect that the cutscene never even tries to play if olmec never spawned. And the same for tiamat. I tried the hack with this addition in ui_util.cpp and looks like it worked great even for deleting olmec during the cutscene:

            if (check && in_array(check->type->id, olmecs))
            {
                patch_olmec_kill_crash();
                auto state = State::get().ptr();
                if (state->logic->olmec_cutscene)
                {
                    // if cutscene is still running, perform the last frame of cutscene before killing olmec
                    state->logic->olmec_cutscene->timer = 809;
                    state->logic->olmec_cutscene->perform();
                    state->logic->olmec_cutscene = nullptr;
                }
                destroy_entity_items(check);
                check->destroy();
                return;
            }

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 17, 2023

The issue comes down to ability to skip/disable cutscenes which i think would be a good option in itself

@Dregu
Copy link
Collaborator

Dregu commented Sep 17, 2023

Well I suppose we do have that option already, since the cutscenes are initialized by the spawn_effects theme functions, but those do a lot of things...

@Dregu
Copy link
Collaborator

Dregu commented Sep 17, 2023

And the game still crashes even if the virtual is disabled but olmec is also disabled.

@Mr-Auto Mr-Auto changed the title Fix crash when spawning more than 7 orbs Patching the game code to fix crashes Sep 17, 2023
@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 18, 2023

ok, i have a kind of working code, the issue is, if i nuke the whole function that set's up the cutscene and logic the camera bounds are f***ed

So i guess the best solution is to just hook that function and check if olmec is alive, if not, skip the cutscene and set the camera bounds manually

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 18, 2023

oh wait, that is just the spawn_effects virtual, it set's the camera stuff as it different for shortcut vs normal entrance

@Dregu
Copy link
Collaborator

Dregu commented Sep 18, 2023

Now I can disable olmec + cutscene, only disable the cutscene but have olmec or kill olmec whenever.

patch_olmec_kill_crash()
set_pre_tile_code_callback(function() return true end, "olmec")
state.level_gen.themes[THEME.OLMEC]:set_pre_spawn_effects(function()
  if #get_entities_by(ENT_TYPE.ACTIVEFLOOR_OLMEC, MASK.ACTIVEFLOOR, LAYER.BOTH) == 0 then
    state.level_gen.themes[THEME.DWELLING]:spawn_effects()
    return true
  end
end)

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 18, 2023

i may have even better solution, current problem: character can't move xd

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 18, 2023

skipping spawn_effects makes it so ENT_TYPE_FX_LEADER_FLAG is never spawn and "You feel forgiven." text is never shown (probably more)

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 18, 2023

you can test the new code, no need for theme hook but i can't figure out why i can't control the character

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 18, 2023

ok i figured out what it is, the player have the special behavior ic8 set that prevents him from doing anything

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 18, 2023

the movement is unlocked by the logic cutscene virtual 😭

@Dregu
Copy link
Collaborator

Dregu commented Sep 18, 2023

So ic8 is the cutscene behavior that's also making Ana walk in the intro etc, neat. That probably needs exposing and scripting capabilities.

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 18, 2023

So ic8 is the cutscene behavior that's also making Ana walk in the intro etc, neat. That probably needs exposing and scripting capabilities.

yeah, i think i commented that before. It's different from normal behavior, potentially even being a class based, so sometimes even having additional variables? thou this is just speculation over what I've seen in memory

anyway i may have a very hacky solution for this problem, will try it tomorrow

@Dregu
Copy link
Collaborator

Dregu commented Sep 19, 2023

I'm getting random crashes in olmec now, sometimes when just flying around, sometimes when loading the level. Also the camera bottom bound is still wrong when olmec doesn't spawn. (56.5 instead of 66.75)

patch_olmec_kill_crash()
set_pre_tile_code_callback(function() return true end, "olmec")

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 19, 2023

it's crashing in statemachine, but i think it might be related to the stack, looks like compiled overlunky code uses different convention. Will change the code to use using Detours tomorrow

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 20, 2023

The issue comes from state->unknown27 (the entity lookup), i need to reset it properly or something maybe?

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 22, 2023

Ok, i reverse engineered almost the whole spawn_effects for olmec and tiamat.
Stuff to note:

  • spawn_effects for tiamat will not do anything if state.screen != screen::level
  • spawn_effects for olmec will not do anything if state.screen == screen::transition

I will rewrite the olmec stuff a bit and make similar code for tiamat, there will be no need to loop thru entities or manually set the camera stuff

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 23, 2023

I'm 90% sure that all of this is safe, except for the first thing in olmec fix, i just don't know what the code is for. There is a lot of crap, plus jumps going all over the place

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 23, 2023

Hope we won't need to generate the entity types anymore, cause it will break this.
Or maybe I add the CrushElevator later to the plugin, just so the type exists at least

@Mr-Auto Mr-Auto marked this pull request as ready for review September 24, 2023 17:25
@Dregu
Copy link
Collaborator

Dregu commented Sep 25, 2023

One issue with setting the elevator speed is that you can't set Hunduns speed to match, only y_limit there.

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 25, 2023

One issue with setting the elevator speed is that you can't set Hunduns speed to match, only y_limit there.

Now you can :)
I also added the x increment, but not sure if it's of any use, game does just have it at 0 all the time

@Dregu
Copy link
Collaborator

Dregu commented Sep 25, 2023

I think you skipped something in the hundun rising update that updates the y_level to current bottom during the rising phase. This leads to weird hundun who constantly jumps down to the spawn level and then rises back up to jump down again, falls through the stopped elevator etc.

@Mr-Auto
Copy link
Contributor Author

Mr-Auto commented Sep 25, 2023

Wrong offset, at some point i switched the x/y around cuz they were the other way around, and rising the position uses a pointer to x/y, but forgot to change the code for the y_level update which only cares about the y speed

@Dregu Dregu merged commit f97b195 into spelunky-fyi:main Sep 26, 2023
9 checks passed
@Mr-Auto Mr-Auto deleted the orbs-hack branch September 27, 2023 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants