Add Lua API function to set the current time of day

This commit is contained in:
Celtic Minstrel 2016-03-02 12:58:00 -05:00
parent ca382018b7
commit fe818f8005
5 changed files with 61 additions and 0 deletions

View file

@ -115,6 +115,11 @@ Version 1.13.4+dev:
(with the exception that it does not outline the hex if true is
passed as the second argument), but this name change was done to
emphasize that it acts on a unit, more than a location.
* New wesnoth.set_time_of_day function which sets the current time
of day, taken either as the time ID (eg "second_watch") or the index
of the time in the overall schedule (eg, 1 would be dawn in the default
schedule). Optional second argument takes a time area ID, to set
local instead of global time.
* WML tables defined in Lua now accept string keys with array values
(where "array" is a table whose keys are all integers). This joins
the elements of the array with commas and produces a single string

View file

@ -4037,6 +4037,52 @@ int game_lua_kernel::intf_replace_schedule(lua_State * L)
return 0;
}
int game_lua_kernel::intf_set_time_of_day(lua_State * L)
{
if(!game_display_) {
return 0;
}
std::string area_id;
size_t area_i;
if (lua_isstring(L, 2)) {
area_id = lua_tostring(L, 1);
std::vector<std::string> area_ids = resources::tod_manager->get_area_ids();
area_i = std::find(area_ids.begin(), area_ids.end(), area_id) - area_ids.begin();
if(area_i >= area_ids.size()) {
return luaL_argerror(L, 1, "invalid time area ID");
}
}
int is_num = false;
int new_time = lua_tonumberx(L, 1, &is_num) - 1;
const std::vector<time_of_day>& times = area_id.empty()
? game_display_->get_tod_man().times()
: game_display_->get_tod_man().times(area_i);
int num_times = times.size();
if(!is_num) {
std::string time_id = luaL_checkstring(L, 1);
new_time = 0;
for(const time_of_day& time : times) {
if(time_id == time.id) {
break;
}
new_time++;
}
if(new_time >= num_times) {
return luaL_argerror(L, 1, "invalid time of day ID");
}
}
if(new_time < 0 || new_time >= num_times) {
return luaL_argerror(L, 1, "invalid time of day index");
}
if(area_id.empty()) {
resources::tod_manager->set_current_time(new_time);
} else {
resources::tod_manager->set_current_time(new_time, area_i);
}
return 0;
}
int game_lua_kernel::intf_scroll(lua_State * L)
{
vconfig cfg = luaW_checkvconfig(L, 1);
@ -4343,6 +4389,7 @@ game_lua_kernel::game_lua_kernel(CVideo * video, game_state & gs, play_controlle
{ "scroll", &dispatch<&game_lua_kernel::intf_scroll > },
{ "scroll_to_tile", &dispatch<&game_lua_kernel::intf_scroll_to_tile > },
{ "select_hex", &dispatch<&game_lua_kernel::intf_select_hex > },
{ "set_time_of_day", &dispatch<&game_lua_kernel::intf_set_time_of_day > },
{ "deselect_hex", &dispatch<&game_lua_kernel::intf_deselect_hex > },
{ "select_unit", &dispatch<&game_lua_kernel::intf_select_unit > },
{ "skip_messages", &dispatch<&game_lua_kernel::intf_skip_messages > },

View file

@ -153,6 +153,7 @@ class game_lua_kernel : public lua_kernel_base
int intf_label(lua_State *L);
int intf_redraw(lua_State *L);
int intf_replace_schedule(lua_State *l);
int intf_set_time_of_day(lua_State *L);
int intf_scroll(lua_State *L);
int intf_get_all_vars(lua_State *L);
int impl_theme_item(lua_State *L, std::string name);

View file

@ -485,6 +485,13 @@ void tod_manager::set_current_time(int time, int area_index) {
set_current_time(time, areas_[area_index]);
}
void tod_manager::set_current_time(int time, const std::string& area_id) {
for (area_time_of_day& area : areas_) {
if (area.id == area_id)
set_current_time(time, area);
}
}
void tod_manager::set_current_time(int time, area_time_of_day& area) {
assert(time < static_cast<int>(area.times.size()) );
if (area.times[time].lawful_bonus != area.times[area.currentTime].lawful_bonus) {

View file

@ -47,6 +47,7 @@ class tod_manager : public savegame::savegame_config
void set_current_time(int time);
void set_current_time(int time, int area_index);
void set_current_time(int time, const std::string& area_id);
void set_area_id(int area_index, const std::string& id);
/**