Skip to content

Store Terrain ID instead of Terrain pointer in texture_t (resolves #93)

Every time a new Terrain type is defined, Z_Realloc is called. realloc is not guaranteed to point at the same memory location for each reallocation, which is causing stale pointer problems when it's fetching the terrain pointer from a texture_t object. This affects K_GetTerrainForTextureNum which is used for tripwire detection.

This MR fixes this by storing the terrain ID instead of a terrain pointer in texture_t. Two new functions are created, K_GetDefaultTerrainID and K_GetTerrainIDForTextureName for getting the terrain ID for the purposes it is relevant, and K_GetTerrainForTextureNum is changed to turn this ID into a proper terrain_t using K_GetTerrainByIndex instead.

Because the stale pointer was creating undefined behaviour, there's no consistent reproduction steps - the terrain flags would have garbage data that could turn all sidedefs into tripwires (the most obvious effect) or less obvious effects such as already-existing tripwires not working.

This MR makes custom defined Terrain types work.

Test WAD: JUG_TerrainTest_useTerrainIDinsteadOfTerrainPointer.pk3

Test Steps:

  • Load test wad: addfile JUG_TerrainTest_useTerrainIDinsteadOfTerrainPointer.pk3
  • Load level: map JUG_TERRAINTEST
  • JUGWHAT should have its JugOutrun terrain type which sets SpeedIncrease 100.
  • JUGSTA should have its JugStairSneaker terrain type which sets SneakerPanel true and Bumpy true.
  • Load level: map RR_TESTRUN
  • Run around the level and see if you're not stopped by seemingly-invisible tripwires.
  • Run into the tripwires on the right side of the level to see if they work.

Expected Result:

  • JUGWHAT and JUGSTA's Terrain effects apply,
  • The player isn't randomly stopped by invisible tripwires (caused by undefined behaviour).
  • The tripwires in Test Run work (because undefined behaviour is gone).
  • No more undefined behaviour.

Before:

ringracers0170 ringracers0172

After:

ringracers0171

Edited by JugadorXEI

Merge request reports

Loading