Skip to content

Commit

Permalink
Fix timer:set_suspended_with_map() not working for entity timers
Browse files Browse the repository at this point in the history
  • Loading branch information
christopho committed Jun 26, 2018
1 parent bf6115b commit e7735f3
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Engine changes
* Fix enemies unable to move on non-blocking streams.
* Fix streams stopping when using the sword several times.
* Fix stairs activating when the hero is not exactly aligned.
* Fix timer:set_suspended_with_map() not working for entity timers (#1158).
* Fix random movement giving the same path for all entities on Mac (#1083).
* Fix circle_movement:get_direction4() not working (#1163).
* Fix possible crash when reloading very old savegame files (#1064).
Expand Down
1 change: 1 addition & 0 deletions include/solarus/lua/LuaContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ class LuaContext {
void update_timers();
void notify_timers_map_suspended(bool suspended);
void set_entity_timers_suspended(Entity& entity, bool suspended);
void set_entity_timers_suspended_as_map(Entity& entity, bool suspended);
void do_timer_callback(const TimerPtr& timer);

// Menus.
Expand Down
4 changes: 2 additions & 2 deletions src/core/MainLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ void MainLoop::check_input() {
notify_input(*event);
event = InputEvent::get_event();
}

/*
// Check Lua requests.
if (!lua_commands.empty()) {
std::lock_guard<std::mutex> lock(lua_commands_mutex);
Expand All @@ -458,7 +458,7 @@ void MainLoop::check_input() {
++num_lua_commands_done;
}
lua_commands.clear();
}
}*/
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/entities/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3411,7 +3411,7 @@ void Entity::set_suspended(bool suspended) {

// Suspend/unsuspend timers.
if (is_on_map()) {
get_lua_context()->set_entity_timers_suspended(*this, suspended || !is_enabled());
get_lua_context()->set_entity_timers_suspended_as_map(*this, suspended || !is_enabled());
}

if (!suspended) {
Expand Down
28 changes: 28 additions & 0 deletions src/lua/TimerApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,34 @@ void LuaContext::set_entity_timers_suspended(
}
}

/**
* \brief Suspends or resumes the timers attached to a map entity.
*
* This takes into account the Timer::is_suspended_with_map() property.
*
* \param entity A map entity.
* \param suspended \c true to suspend its timers
* (unless Timer::is_suspended_with_map() is false), \c false to resume them.
*/
void LuaContext::set_entity_timers_suspended_as_map(
Entity& entity, bool suspended
) {
if (!suspended) {
set_entity_timers_suspended(entity, suspended);
return;
}

// Suspend timers except the ones that ignore the map being suspended.
for (const auto& kvp: timers) {
const TimerPtr& timer = kvp.first;
if (kvp.second.context == &entity) {
if (timer->is_suspended_with_map()) {
timer->set_suspended(suspended);
}
}
}
}

/**
* \brief Executes the callback of a timer.
*
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ set(lua_test_maps
"bugs/1062_enemy_set_attack_consequence_callback"
"bugs/1076_treasure_dialog_optional"
"bugs/1094_entity_properties"
"bugs/1158_entity_timer_suspended"
"bugs/1162_custom_entity_collision_wrong_order"
"bugs/1163_circular_movement_get_direction4"
"bugs/1181_hurt_enemy_after_custom_attack"
Expand Down
37 changes: 37 additions & 0 deletions tests/testing_quest/data/maps/bugs/1158_entity_timer_suspended.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
properties{
x = 0,
y = 0,
width = 320,
height = 240,
min_layer = 0,
max_layer = 2,
tileset = "castle",
}

tile{
layer = 0,
x = 0,
y = 0,
width = 320,
height = 240,
pattern = "3",
}

destination{
name = "destination",
layer = 0,
x = 24,
y = 29,
direction = 3,
}

npc{
name = "robyne",
layer = 0,
x = 88,
y = 29,
direction = 3,
subtype = 1,
sprite = "main_heroes/robyne",
}

36 changes: 36 additions & 0 deletions tests/testing_quest/data/maps/bugs/1158_entity_timer_suspended.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local map = ...
local game = map:get_game()

function map:on_opening_transition_finished()

assert(not game:is_suspended())

local robyne_timer_suspend = sol.timer.start(robyne, 1000, function()
assert(not game:is_suspended())
sol.main.exit()
end)
robyne_timer_suspend:set_suspended_with_map(true)
assert(robyne_timer_suspend:is_suspended_with_map())

local robyne_timer_no_suspend = sol.timer.start(robyne, 1000, function()
end)
robyne_timer_no_suspend:set_suspended_with_map(false)
assert(not robyne_timer_no_suspend:is_suspended_with_map())

local map_timer_1 = sol.timer.start(map, 500, function()
assert(game:is_suspended())
assert(robyne_timer_suspend:is_suspended())
assert(not robyne_timer_no_suspend:is_suspended())
end)
map_timer_1:set_suspended_with_map(false)
assert(not map_timer_1:is_suspended_with_map())

local map_timer_2 = sol.timer.start(map, 2000, function()
assert(game:is_suspended())
game:set_paused(false)
end)
map_timer_2:set_suspended_with_map(false)
assert(not map_timer_1:is_suspended_with_map())

game:set_paused(true)
end
1 change: 1 addition & 0 deletions tests/testing_quest/data/project_db.dat
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ map{ id = "bugs/1042_multiple_facing_entities_random_picked", description = "#10
map{ id = "bugs/1062_enemy_set_attack_consequence_callback", description = "#1062: Add callback parameter to enemy:set_attack_consequence" }
map{ id = "bugs/1076_treasure_dialog_optional", description = "#1076: Treasure dialog should be optional" }
map{ id = "bugs/1094_entity_properties", description = "#1094: Entity user-defined properties" }
map{ id = "bugs/1158_entity_timer_suspended", description = "#1158: timer:set_suspended_with_map(false) not working with entity timers" }
map{ id = "bugs/1162_custom_entity_collision_wrong_order", description = "#1162: Wrong sprite order in custom entity collision callback" }
map{ id = "bugs/1163_circular_movement_get_direction4", description = "#1163: circular_movement:get_direction4() not working" }
map{ id = "bugs/1181_hurt_enemy_after_custom_attack", description = "#1181: enemy:hurt() does not work after a custom attack" }
Expand Down

0 comments on commit e7735f3

Please sign in to comment.