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 -
JUGWHATshould have itsJugOutrunterrain type which setsSpeedIncrease 100. -
JUGSTAshould have itsJugStairSneakerterrain type which setsSneakerPanel trueandBumpy 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:
-
JUGWHATandJUGSTA'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:
After: