Skip to content

Commit

Permalink
Add Lua API function to set the current time of day
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Apr 2, 2016
1 parent ca38201 commit fe818f8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changelog
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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 > },
Expand Down
1 change: 1 addition & 0 deletions src/scripting/game_lua_kernel.hpp
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/tod_manager.cpp
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/tod_manager.hpp
Expand Up @@ -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);

/**
Expand Down

0 comments on commit fe818f8

Please sign in to comment.