Skip to content

Commit

Permalink
Make event_test_filter_condition check when the filters run
Browse files Browse the repository at this point in the history
Also add the missing first_time_only=no, which is why the three [moveto]
events didn't trigger the third event three times.
  • Loading branch information
stevecotton authored and Pentarctagon committed Jan 26, 2024
1 parent b2b2052 commit a8cfca2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,70 @@
# The third moveto event's condition is enabled by the second moveto event.
##
# Expected end state:
# All three moveto events each trigger once.
# All three moveto events each trigger once when the first move happens.
# A second move (without resetting the condition) triggers the third moveto again.
#####
{GENERIC_UNIT_TEST event_test_filter_condition (
[event]
name=start
{VARIABLE var 1}
{VARIABLE triggers 0}
{VARIABLE triggers_first 0}
{VARIABLE triggers_second 0}
{VARIABLE triggers_third 0}

# A single move triggers all three events, because the filter for the second
# event is tested after the first event has already run.
[do_command]
[move]
x=7,8
y=3,3
[/move]
[/do_command]
{ASSERT ({VARIABLE_CONDITIONAL triggers_first equals 1})}
{ASSERT ({VARIABLE_CONDITIONAL triggers_second equals 1})}
{ASSERT ({VARIABLE_CONDITIONAL triggers_third equals 1})}

# At this point var equals 3, so only the third event triggers.
[do_command]
[move]
x=8,9
y=3,4
[/move]
[/do_command]
[do_command]
[move]
x=9,8
y=4,4
[/move]
[/do_command]
{RETURN ({VARIABLE_CONDITIONAL triggers equals 3})}
{ASSERT ({VARIABLE_CONDITIONAL triggers_first equals 1})}
{ASSERT ({VARIABLE_CONDITIONAL triggers_second equals 1})}
{ASSERT ({VARIABLE_CONDITIONAL triggers_third equals 2})}

{SUCCEED}
[/event]
[event]
name=moveto
first_time_only=no
[filter_condition]
{VARIABLE_CONDITIONAL var equals 1}
[/filter_condition]
{ASSERT ({VARIABLE_CONDITIONAL var equals 1})}
{VARIABLE var 2}
{VARIABLE_OP triggers add 1}
{VARIABLE_OP triggers_first add 1}
[/event]
[event]
name=moveto
first_time_only=no
[filter_condition]
{VARIABLE_CONDITIONAL var equals 2}
[/filter_condition]
{ASSERT ({VARIABLE_CONDITIONAL var equals 2})}
{VARIABLE var 3}
{VARIABLE_OP triggers add 1}
{VARIABLE_OP triggers_second add 1}
[/event]
[event]
name=moveto
first_time_only=no
[filter_condition]
{VARIABLE_CONDITIONAL var equals 3}
[/filter_condition]
{ASSERT ({VARIABLE_CONDITIONAL var equals 3})}
{VARIABLE_OP triggers add 1}
{VARIABLE_OP triggers_third add 1}
[/event]
)}

Expand Down
51 changes: 9 additions & 42 deletions src/game_board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "preferences/game.hpp"
#include "recall_list_manager.hpp"
#include "units/unit.hpp"
#include "units/animation_component.hpp"
#include "utils/general.hpp"

#include <set>
Expand Down Expand Up @@ -470,63 +471,26 @@ temporary_unit_remover::~temporary_unit_remover()
* the unit is moved (and restored to its previous value upon this object's
* destruction).
*/
temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src, const map_location& dst, int new_moves)
temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src, const map_location& dst, int new_moves, bool stand)
: m_(m)
, src_(src)
, dst_(dst)
, old_moves_(-1)
, temp_(src == dst ? unit_ptr() : m_.extract(dst))
, stand_(stand)
{
auto [iter, success] = m_.move(src_, dst_);

// Set the movement.
if(success) {
old_moves_ = iter->movement_left(true);
iter->set_movement(new_moves);
if(stand_) {
m_.find_unit_ptr(dst_)->anim_comp().set_standing();
}
}
}

temporary_unit_mover::temporary_unit_mover(
game_board& b, const map_location& src, const map_location& dst, int new_moves)
: m_(b.units_)
, src_(src)
, dst_(dst)
, old_moves_(-1)
, temp_(src == dst ? unit_ptr() : m_.extract(dst))
{
auto [iter, success] = m_.move(src_, dst_);

// Set the movement.
if(success) {
old_moves_ = iter->movement_left(true);
iter->set_movement(new_moves);
}
}

/**
* Constructor
* This version does not change (nor restore) the unit's movement.
*/
temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src, const map_location& dst)
: m_(m)
, src_(src)
, dst_(dst)
, old_moves_(-1)
, temp_(src == dst ? unit_ptr() : m_.extract(dst))
{
m_.move(src_, dst_);
}

temporary_unit_mover::temporary_unit_mover(game_board& b, const map_location& src, const map_location& dst)
: m_(b.units_)
, src_(src)
, dst_(dst)
, old_moves_(-1)
, temp_(src == dst ? unit_ptr() : m_.extract(dst))
{
m_.move(src_, dst_);
}

temporary_unit_mover::~temporary_unit_mover()
{
try {
Expand All @@ -535,6 +499,9 @@ temporary_unit_mover::~temporary_unit_mover()
// Restore the movement?
if(success && old_moves_ >= 0) {
iter->set_movement(old_moves_);
if(stand_) {
m_.find_unit_ptr(src_)->anim_comp().set_standing();
}
}

// Restore the extracted unit?
Expand Down
12 changes: 3 additions & 9 deletions src/game_board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,14 @@ struct temporary_unit_remover
*/
struct temporary_unit_mover
{
temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst, int new_moves);
temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst);
temporary_unit_mover(game_board& b, const map_location& src,
const map_location& dst, int new_moves);
temporary_unit_mover(game_board& b, const map_location& src,
const map_location& dst);
virtual ~temporary_unit_mover();
temporary_unit_mover(unit_map& m, const map_location& src, const map_location& dst, int new_moves, bool stand);
virtual ~temporary_unit_mover();

private:
unit_map& m_;
const map_location src_;
const map_location dst_;
int old_moves_;
unit_ptr temp_;
bool stand_;
};
2 changes: 1 addition & 1 deletion src/mouse_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ void mouse_handler::move_action(bool browse)

// block where we temporary move the unit
{
temporary_unit_mover temp_mover(pc_.get_units(), src, attack_from, itor->move_left);
temporary_unit_mover temp_mover(pc_.get_units(), src, attack_from, itor->move_left, true);
choice = show_attack_dialog(attack_from, clicked_u->get_location());
}

Expand Down
3 changes: 3 additions & 0 deletions src/units/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
See the COPYING file for more details.
*/

#include "display.hpp"
#include "log.hpp"
#include "units/id.hpp"
#include "units/unit.hpp"
Expand Down Expand Up @@ -126,6 +127,8 @@ unit_map::umap_retval_pair_t unit_map::move(const map_location& src, const map_l
return std::pair(make_unit_iterator(uit), false);
}

display::get_singleton()->invalidate(src);

self_check();

return std::pair(make_unit_iterator(uit), true);
Expand Down
2 changes: 1 addition & 1 deletion src/whiteboard/move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ void move::apply_temp_modifier(unit_map& unit_map)
// Move the unit
DBG_WB << "Move: Temporarily moving unit " << unit->name() << " [" << unit->id()
<< "] from (" << get_source_hex() << ") to (" << get_dest_hex() <<")";
mover_.reset(new temporary_unit_mover(unit_map, get_source_hex(), get_dest_hex(), calculate_moves_left(*unit)));
mover_.reset(new temporary_unit_mover(unit_map, get_source_hex(), get_dest_hex(), calculate_moves_left(*unit), false));

//Update status of fake unit (not undone by remove_temp_modifiers)
//@todo this contradicts the name "temp_modifiers"
Expand Down

0 comments on commit a8cfca2

Please sign in to comment.