Skip to content

Commit

Permalink
Avoid destructive recalculations during poly height changes; fixes #492
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopper262 committed Aug 29, 2024
1 parent 2825da5 commit fe20606
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Source_Files/GameWorld/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -1292,8 +1292,8 @@ void precalculate_map_indexes(void);

void touch_polygon(short polygon_index);
void recalculate_redundant_polygon_data(short polygon_index);
void recalculate_redundant_endpoint_data(short endpoint_index);
void recalculate_redundant_line_data(short line_index);
void recalculate_redundant_endpoint_data(short endpoint_index, bool only_for_heights = false);
void recalculate_redundant_line_data(short line_index, bool only_for_heights = false);
void recalculate_redundant_side_data(short side_index, short line_index);

void calculate_endpoint_polygon_owners(short endpoint_index, short *first_index, short *index_count);
Expand Down
23 changes: 16 additions & 7 deletions Source_Files/GameWorld/map_constructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ void recalculate_redundant_polygon_data(
/* calculates solidity, highest adjacent floor and lowest adjacent ceiling; not to be called
at runtime. */
void recalculate_redundant_endpoint_data(
short endpoint_index)
short endpoint_index,
bool only_for_heights)
{
struct endpoint_data *endpoint= get_endpoint_data(endpoint_index);
world_distance highest_adjacent_floor_height= INT16_MIN;
Expand Down Expand Up @@ -278,9 +279,12 @@ void recalculate_redundant_endpoint_data(
}
}

SET_ENDPOINT_SOLIDITY(endpoint, solid);
SET_ENDPOINT_TRANSPARENCY(endpoint, transparent);
SET_ENDPOINT_ELEVATION(endpoint, elevation);
if (!only_for_heights)
{
SET_ENDPOINT_SOLIDITY(endpoint, solid);
SET_ENDPOINT_TRANSPARENCY(endpoint, transparent);
SET_ENDPOINT_ELEVATION(endpoint, elevation);
}
endpoint->highest_adjacent_floor_height= highest_adjacent_floor_height;
endpoint->lowest_adjacent_ceiling_height= lowest_adjacent_ceiling_height;
endpoint->supporting_polygon_index= supporting_polygon_index;
Expand All @@ -289,7 +293,8 @@ void recalculate_redundant_endpoint_data(
/* calculates line length, highest adjacent floor and lowest adjacent ceiling and calls
recalculate_redundant_side_data() on the line’s sides */
void recalculate_redundant_line_data(
short line_index)
short line_index,
bool only_for_heights)
{
struct line_data *line= get_line_data(line_index);
struct side_data *clockwise_side= NULL, *counterclockwise_side= NULL;
Expand All @@ -299,8 +304,9 @@ void recalculate_redundant_line_data(
bool transparent_texture= false;

/* recalculate line length */
line->length= distance2d(&(get_endpoint_data(line->endpoint_indexes[0])->vertex),
&(get_endpoint_data(line->endpoint_indexes[1])->vertex));
if (only_for_heights)
line->length= distance2d(&(get_endpoint_data(line->endpoint_indexes[0])->vertex),
&(get_endpoint_data(line->endpoint_indexes[1])->vertex));

/* find highest adjacent floor and lowest adjacent ceiling */
{
Expand Down Expand Up @@ -341,6 +347,9 @@ void recalculate_redundant_line_data(
}
}

if (only_for_heights)
return;

if (line->clockwise_polygon_side_index!=NONE)
{
recalculate_redundant_side_data(line->clockwise_polygon_side_index, line_index);
Expand Down
13 changes: 7 additions & 6 deletions Source_Files/Lua/lua_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,8 @@ static int Lua_Polygon_Floor_Set_Height(lua_State *L)
polygon->floor_height = static_cast<world_distance>(lua_tonumber(L,2)*WORLD_ONE);
for (short i = 0; i < polygon->vertex_count; ++i)
{
recalculate_redundant_endpoint_data(polygon->endpoint_indexes[i]);
recalculate_redundant_line_data(polygon->line_indexes[i]);
recalculate_redundant_endpoint_data(polygon->endpoint_indexes[i], true);
recalculate_redundant_line_data(polygon->line_indexes[i], true);
}
return 0;
}
Expand Down Expand Up @@ -893,8 +893,8 @@ static int Lua_Polygon_Ceiling_Set_Height(lua_State *L)
polygon->ceiling_height = static_cast<world_distance>(lua_tonumber(L,2)*WORLD_ONE);
for (short i = 0; i < polygon->vertex_count; ++i)
{
recalculate_redundant_endpoint_data(polygon->endpoint_indexes[i]);
recalculate_redundant_line_data(polygon->line_indexes[i]);
recalculate_redundant_endpoint_data(polygon->endpoint_indexes[i], true);
recalculate_redundant_line_data(polygon->line_indexes[i], true);
}
return 0;
}
Expand Down Expand Up @@ -1270,6 +1270,7 @@ const luaL_Reg Lua_Polygon_Sides_Metatable[] = {
{0, 0}
};

static void update_line_redundancy(short line_index);
int Lua_Polygon_Change_Height(lua_State* L)
{
if (!lua_isnumber(L, 2) || !lua_isnumber(L, 3))
Expand All @@ -1287,8 +1288,8 @@ int Lua_Polygon_Change_Height(lua_State* L)
auto polygon = get_polygon_data(polygon_index);
for (auto i = 0; i < polygon->vertex_count; ++i)
{
recalculate_redundant_line_data(polygon->line_indexes[i]);
recalculate_redundant_endpoint_data(polygon->endpoint_indexes[i]);
recalculate_redundant_line_data(polygon->line_indexes[i], true);
recalculate_redundant_endpoint_data(polygon->endpoint_indexes[i], true);
}
}

Expand Down

3 comments on commit fe20606

@treellama
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think it is important to keep these. These are the same checks that are run when a (non-Forge merged) map is loaded, so it's important to keep the map in a consistent state when Lua modifies it.

@treellama
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrk confirms this breaks AOPID "pretty hilariously". Can we revert this? I'll leave some thoughts on #492

@Hopper262
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect AOPID is running into a second bug exposed by fixing this one. The precalculated impassibility/exclusion info can go out of date when heights change, and solid lines shortcut the logic so you don't see incorrect step heights and clipping into low/high sides. I'll get the latter bug written up -- you can trigger it in stock 1.10 -- so we can discuss further.

Reverting this until we reach a fuller solution.

Please sign in to comment.