WohlSoft / LunaLua

LunaLua - LunaDLL with Lua, is a free extension mod for SMBX 1.3 game engine, core of the X2 project.
https://codehaus.moe/
GNU General Public License v3.0
33 stars 12 forks source link

NPC spawning fixes #54

Open MrDoubleA232 opened 1 year ago

MrDoubleA232 commented 1 year ago

Fixes the "frame perfect despawn" glitch, which causes an NPC to not spawn if it comes on screen later in the same frame that it despawned. Includes a toggle to disable the fix. Also fixes a small issue with the despawn timer discussed in the SMBX2 dev channel earlier today, and gives proper names to some of the "unknown" NPC fields.

ds-sloth commented 1 year ago

I don't think that the NPC momentum onscreen call currently handles SingleCoop mode (camera type 6) correctly. Other than that, I just patched a similar fix into TheXTech and it seems to work well.

If you're injecting your code into the Deactivate function, this will also fix a related issue where NPCs that went offscreen during FreezeNPCs (timer switch, stopwatch, CaptainN cheat) wouldn't respawn until they'd been offscreen during normal level play.

ds-sloth commented 10 months ago

Responding to Rednaxela's comment in the X2 server, I should note: on our side we actually ended up disabling the FreezeNPCs fix in TheXTech for now.

It handles cases like the above showcase well, but the FreezeNPCs NPCUpdate code doesn't call the various initialization code for certain NPCs like piranha plants or cheap cheaps.

Until we refactor the NPC code deeply enough to gather all initialization code and be able to call it, any fix for the FreezeNPCs despawn bug will result in some incorrect behavior.

Secondarily, the point I was making about SingleCoop is just that on line 3258 in this diff, camera idx should be 2 if camera type is 6 and single coop is 2. Right now you'd be checking only the P1 camera, so if P2 is active you might accidentally allow an NPC to respawn while onscreen.

MrDoubleA232 commented 10 months ago

Makes sense. I suppose that that initialisation quirk also causes issues in the case of the screen slowly scrolling after section resizing?

ds-sloth commented 10 months ago

Yes, but only in rare cases. I should have been clearer, but a little more than half of the "initialization" code I mentioned actually runs in the same UpdateNPCs cycle that calls Deactivate, following Deactivate being called. Maybe that half should be called "resetting code". So it's harmful to either activate or deactivate an NPC during FreezeNPCs.

To directly answer your question about qScreen, I think that the most dangerous case would be a qScreen event that brings NPCs onscreen that had been incompletely despawned during FreezeNPCs. That should definitely affect plants at least (they mostly use "resetting code"). I think that cheeps mostly have "initialization code" so they'd be affected by qScreen even if they hadn't been despawned during FreezeNPCs. All of these predictions should be verified though, Redigit's logic can be very surprising at times.