Fix linked cable length when grid origin is outside the bubble
- measure out-of-bubble length to the socket via coord_translate - cache link source position in absolute coordinates (s_abs_pos) - add tests for off-bubble grid origin and shift-hidden movement - remove vacuous OOB length section from cable_survives_target_outside_reality_bubble
This commit is contained in:
+2
-2
@@ -1616,8 +1616,8 @@ class item
|
||||
tripoint_abs_ms t_abs_pos = tripoint_abs_ms::invalid;
|
||||
/// The linked part's mount offset on the target vehicle.
|
||||
point_rel_ms t_mount = point_rel_ms::zero;
|
||||
/// Reality bubble position of the link's source cable item.
|
||||
tripoint_bub_ms s_bub_pos = tripoint_bub_ms::invalid; // NOLINT(cata-serialize)
|
||||
/// Absolute position of the link's source cable item when the length was last checked.
|
||||
tripoint_abs_ms s_abs_pos = tripoint_abs_ms::invalid; // NOLINT(cata-serialize)
|
||||
/// The last turn process_link was called on this cable. Used to find how much time the cable spends outside the reality bubble.
|
||||
time_point last_processed = calendar::turn;
|
||||
/// The current slack of the cable.
|
||||
|
||||
@@ -3743,7 +3743,7 @@ ret_val<void> item::link_to( vehicle &veh, const point_rel_ms &mount, link_state
|
||||
link().t_veh = veh.get_safe_reference();
|
||||
link().t_abs_pos = link().t_veh->pos_abs();
|
||||
link().t_mount = mount;
|
||||
link().s_bub_pos = tripoint_bub_ms::min; // Forces the item to check the length during process_link.
|
||||
link().s_abs_pos = tripoint_abs_ms::min; // Forces item to check length during process_link
|
||||
|
||||
update_link_traits();
|
||||
return ret_val<void>::make_success();
|
||||
@@ -3857,8 +3857,8 @@ void item::update_link_traits()
|
||||
( get_use( "link_up" )->get_actor_ptr() );
|
||||
link().max_length = it_actor->cable_length == -1 ? type->maximum_charges() : it_actor->cable_length;
|
||||
link().efficiency = it_actor->efficiency < MIN_LINK_EFFICIENCY ? 0.0f : it_actor->efficiency;
|
||||
// Reset s_bub_pos to force the item to check the length during process_link.
|
||||
link().s_bub_pos = tripoint_bub_ms::min;
|
||||
// reset s_abs_pos to force a length check in process_link
|
||||
link().s_abs_pos = tripoint_abs_ms::min;
|
||||
link().last_processed = calendar::turn;
|
||||
|
||||
for( const item *cable : cables() ) {
|
||||
@@ -3965,10 +3965,11 @@ bool item::process_link( map &here, Character *carrier, const tripoint_bub_ms &p
|
||||
return false;
|
||||
};
|
||||
|
||||
// Check if the item has moved positions this turn.
|
||||
// check if the item moved since last length check
|
||||
bool length_check_needed = false;
|
||||
if( link().s_bub_pos != pos ) {
|
||||
link().s_bub_pos = pos;
|
||||
const tripoint_abs_ms abs_pos = here.get_abs( pos );
|
||||
if( link().s_abs_pos != abs_pos ) {
|
||||
link().s_abs_pos = abs_pos;
|
||||
length_check_needed = true;
|
||||
}
|
||||
|
||||
@@ -4000,8 +4001,8 @@ bool item::process_link( map &here, Character *carrier, const tripoint_bub_ms &p
|
||||
if( !length_check_needed ) {
|
||||
return false;
|
||||
}
|
||||
link().length = rl_dist( here.get_abs( pos ), link().t_abs_pos ) +
|
||||
link().t_mount.abs().x() + link().t_mount.abs().y();
|
||||
link().length = rl_dist( abs_pos,
|
||||
link().t_abs_pos + t_veh->coord_translate( link().t_mount ) );
|
||||
if( check_length() ) {
|
||||
return reset_link( true, carrier );
|
||||
}
|
||||
|
||||
+134
-17
@@ -1,4 +1,5 @@
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
@@ -10,6 +11,7 @@
|
||||
|
||||
#include "calendar.h"
|
||||
#include "cata_catch.h"
|
||||
#include "cata_scope_helpers.h"
|
||||
#include "character.h"
|
||||
#include "coordinates.h"
|
||||
#include "debug.h"
|
||||
@@ -44,6 +46,7 @@ static const efftype_id effect_blind( "blind" );
|
||||
|
||||
static const itype_id fuel_type_battery( "battery" );
|
||||
static const itype_id itype_ground_solar_panel( "ground_solar_panel" );
|
||||
static const itype_id itype_hotplate( "hotplate" );
|
||||
static const itype_id itype_test_high_drain_lamp( "test_high_drain_lamp" );
|
||||
static const itype_id itype_test_power_cord( "test_power_cord" );
|
||||
static const itype_id itype_test_power_cord_25_loss( "test_power_cord_25_loss" );
|
||||
@@ -657,8 +660,8 @@ TEST_CASE( "cable_survives_target_outside_reality_bubble", "[vehicle][power][gri
|
||||
|
||||
// Where the cord "sits" on the map
|
||||
const tripoint_bub_ms cord_pos( HALF_MAPSIZE_X + 4, HALF_MAPSIZE_Y + 2, 0 );
|
||||
// Initialize s_bub_pos so process_link doesn't trigger a length check
|
||||
cord.link().s_bub_pos = cord_pos;
|
||||
// initialize s_abs_pos so process_link doesn't trigger a length check
|
||||
cord.link().s_abs_pos = here.get_abs( cord_pos );
|
||||
|
||||
// Drop the cord on the map so process() works correctly
|
||||
here.add_item( cord_pos, cord );
|
||||
@@ -695,25 +698,139 @@ TEST_CASE( "cable_survives_target_outside_reality_bubble", "[vehicle][power][gri
|
||||
CHECK( map_cord.has_link_data() );
|
||||
CHECK( map_cord.link().target == link_state::vehicle_port );
|
||||
}
|
||||
}
|
||||
|
||||
SECTION( "OOB length check does not false-positive disconnect" ) {
|
||||
// Set target to OOB but keep the vehicle reference valid.
|
||||
// Move the cord position so length_check_needed triggers.
|
||||
// Stale OOB positions should not cause false over-extension.
|
||||
tripoint_abs_ms oob_pos = here.get_abs( battery_pos ) +
|
||||
tripoint( MAPSIZE_X * 2, MAPSIZE_Y * 2, 0 );
|
||||
REQUIRE_FALSE( here.inbounds( oob_pos ) );
|
||||
TEST_CASE( "cable_length_measured_to_socket_when_grid_origin_leaves_bubble",
|
||||
"[vehicle][power][grid]" )
|
||||
{
|
||||
clear_map_without_vision();
|
||||
clear_avatar();
|
||||
map &here = get_map();
|
||||
Character &player_character = get_player_character();
|
||||
|
||||
map_cord.link().t_abs_pos = oob_pos;
|
||||
// Keep t_veh valid (pointing to the real vehicle)
|
||||
// but force a length check by changing s_bub_pos
|
||||
tripoint_bub_ms moved_pos( HALF_MAPSIZE_X + 5, HALF_MAPSIZE_Y + 2, 0 );
|
||||
GIVEN( "hotplate plugged into the far end of a battery grid that straddles a submap edge" ) {
|
||||
const int row_y = HALF_MAPSIZE_Y;
|
||||
// each new appliance absorbs its neighbor, so the last one placed becomes the origin
|
||||
for( int x = SEEX; x >= SEEX - 4; --x ) {
|
||||
std::optional<item> battery_item( itype_test_storage_battery );
|
||||
REQUIRE( place_appliance( here, tripoint_bub_ms( x, row_y, 0 ),
|
||||
vpart_ap_test_storage_battery, player_character, battery_item ) );
|
||||
}
|
||||
const tripoint_bub_ms socket_pos( SEEX, row_y, 0 );
|
||||
const optional_vpart_position socket_vp = here.veh_at( socket_pos );
|
||||
REQUIRE( socket_vp.has_value() );
|
||||
vehicle &grid = socket_vp->vehicle();
|
||||
for( int x = SEEX - 4; x < SEEX; ++x ) {
|
||||
const optional_vpart_position vp = here.veh_at( tripoint_bub_ms( x, row_y, 0 ) );
|
||||
REQUIRE( vp.has_value() );
|
||||
REQUIRE( &vp->vehicle() == &grid );
|
||||
}
|
||||
const tripoint_abs_ms origin_abs = grid.pos_abs();
|
||||
CAPTURE( grid.pos_bub( here ).to_string(), socket_vp->mount_pos().to_string() );
|
||||
// shift below must drop origin out of the bubble and keep socket in
|
||||
REQUIRE( grid.pos_bub( here ).x() < SEEX );
|
||||
REQUIRE( socket_vp->mount_pos() != point_rel_ms::zero );
|
||||
|
||||
map_cord.process( here, nullptr, moved_pos );
|
||||
const tripoint_bub_ms hotplate_pos( SEEX + 1, row_y, 0 );
|
||||
item hotplate( itype_hotplate );
|
||||
REQUIRE( hotplate.link_to( socket_vp, link_state::vehicle_port ).success() );
|
||||
REQUIRE( hotplate.max_link_length() == 3 );
|
||||
here.add_item( hotplate_pos, hotplate );
|
||||
here.i_at( hotplate_pos ).only_item().process( here, nullptr, hotplate_pos );
|
||||
REQUIRE( here.i_at( hotplate_pos ).only_item().link_length() == 1 );
|
||||
const tripoint_abs_ms hotplate_abs = here.get_abs( hotplate_pos );
|
||||
|
||||
// Cable should survive -- stale OOB positions are unreliable for length
|
||||
CHECK( map_cord.has_link_data() );
|
||||
CHECK( map_cord.link().target == link_state::vehicle_port );
|
||||
WHEN( "the map shifts one submap east" ) {
|
||||
here.shift( point_rel_sm::east );
|
||||
on_out_of_scope restore_map( [&here]() {
|
||||
here.shift( point_rel_sm::west );
|
||||
} );
|
||||
REQUIRE_FALSE( here.inbounds( origin_abs ) );
|
||||
REQUIRE( here.inbounds( hotplate_abs ) );
|
||||
const tripoint_bub_ms shifted_pos = here.get_bub( hotplate_abs );
|
||||
item &shifted = here.i_at( shifted_pos ).only_item();
|
||||
REQUIRE( shifted.link().t_veh );
|
||||
|
||||
AND_WHEN( "hotplate is processed where it lies" ) {
|
||||
shifted.process( here, nullptr, shifted_pos );
|
||||
THEN( "stays plugged in at its true length" ) {
|
||||
CHECK( shifted.link().target == link_state::vehicle_port );
|
||||
CHECK( shifted.link_length() == 1 );
|
||||
}
|
||||
}
|
||||
AND_WHEN( "hotplate saved and loaded back" ) {
|
||||
std::ostringstream os;
|
||||
JsonOut jsout( os );
|
||||
shifted.serialize( jsout );
|
||||
item loaded;
|
||||
JsonValue jv = json_loader::from_string( os.str() );
|
||||
JsonObject jo = jv;
|
||||
loaded.deserialize( jo );
|
||||
here.i_clear( shifted_pos );
|
||||
here.add_item( shifted_pos, loaded );
|
||||
item &reloaded = here.i_at( shifted_pos ).only_item();
|
||||
REQUIRE_FALSE( reloaded.link().t_veh );
|
||||
reloaded.process( here, nullptr, shifted_pos );
|
||||
THEN( "reconnects at its true length" ) {
|
||||
CHECK( reloaded.link().target == link_state::vehicle_port );
|
||||
CHECK( reloaded.link_length() == 1 );
|
||||
}
|
||||
}
|
||||
AND_WHEN( "hotplate is carried one more tile from the socket" ) {
|
||||
shifted.process( here, nullptr, shifted_pos + tripoint_rel_ms::east );
|
||||
THEN( "stays plugged in at the new length" ) {
|
||||
CHECK( shifted.link().target == link_state::vehicle_port );
|
||||
CHECK( shifted.link_length() == 2 );
|
||||
}
|
||||
}
|
||||
AND_WHEN( "hotplate carried past its maximum length" ) {
|
||||
shifted.process( here, nullptr, shifted_pos + tripoint_rel_ms( 3, 0, 0 ) );
|
||||
THEN( "cable resets fully rather than needing to be reeled in" ) {
|
||||
CHECK_FALSE( shifted.has_link_data() );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE( "cable_length_checked_when_source_moves_with_map_shift", "[vehicle][power][grid]" )
|
||||
{
|
||||
clear_map_without_vision();
|
||||
clear_avatar();
|
||||
map &here = get_map();
|
||||
Character &player_character = get_player_character();
|
||||
|
||||
GIVEN( "hotplate plugged into a battery at the map center" ) {
|
||||
const tripoint_bub_ms battery_pos( HALF_MAPSIZE_X, HALF_MAPSIZE_Y, 0 );
|
||||
std::optional<item> battery_item( itype_test_storage_battery );
|
||||
REQUIRE( place_appliance( here, battery_pos, vpart_ap_test_storage_battery,
|
||||
player_character, battery_item ) );
|
||||
const optional_vpart_position battery_vp = here.veh_at( battery_pos );
|
||||
REQUIRE( battery_vp.has_value() );
|
||||
const tripoint_abs_ms battery_abs = battery_vp->vehicle().pos_abs();
|
||||
|
||||
const tripoint_bub_ms hotplate_pos = battery_pos + tripoint_rel_ms::east;
|
||||
item hotplate( itype_hotplate );
|
||||
REQUIRE( hotplate.link_to( battery_vp, link_state::vehicle_port ).success() );
|
||||
REQUIRE( hotplate.max_link_length() == 3 );
|
||||
here.add_item( hotplate_pos, hotplate );
|
||||
item &placed = here.i_at( hotplate_pos ).only_item();
|
||||
placed.process( here, nullptr, hotplate_pos );
|
||||
REQUIRE( placed.link_length() == 1 );
|
||||
|
||||
WHEN( "hotplate moves one submap east as the map shifts with it" ) {
|
||||
// copy stands in for a carried device; original stays behind on its submap
|
||||
item carried = placed;
|
||||
here.shift( point_rel_sm::east );
|
||||
on_out_of_scope restore_map( [&here]() {
|
||||
here.shift( point_rel_sm::west );
|
||||
} );
|
||||
REQUIRE( here.inbounds( battery_abs ) );
|
||||
carried.process( here, nullptr, hotplate_pos );
|
||||
THEN( "over-extended cable comes loose" ) {
|
||||
CHECK( carried.link_length() < 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user