diff --git a/src/openrct2/GameState.cpp b/src/openrct2/GameState.cpp index bdeeaff041..2523a03dac 100644 --- a/src/openrct2/GameState.cpp +++ b/src/openrct2/GameState.cpp @@ -272,8 +272,15 @@ void GameState::UpdateLogic() auto& hookEngine = GetContext()->GetScriptEngine().GetHookEngine(); hookEngine.Call(HOOK_TYPE::INTERVAL_TICK); + auto day = _date.GetDay(); + date_update(); - _date = Date(gDateMonthTicks, gDateMonthTicks); + _date = Date(gDateMonthsElapsed, gDateMonthTicks); + + if (day != _date.GetDay()) + { + hookEngine.Call(HOOK_TYPE::INTERVAL_DAY); + } scenario_update(); climate_update(); diff --git a/src/openrct2/scripting/Plugin.cpp b/src/openrct2/scripting/Plugin.cpp index 7c74139646..5a0e2e0e09 100644 --- a/src/openrct2/scripting/Plugin.cpp +++ b/src/openrct2/scripting/Plugin.cpp @@ -26,7 +26,7 @@ Plugin::Plugin(duk_context* context, const std::string& path) void Plugin::Load() { - std::string projectedVariables = "console,context,map,network,park"; + std::string projectedVariables = "console,context,date,map,network,park"; if (!gOpenRCT2Headless) { projectedVariables += ",ui"; diff --git a/src/openrct2/scripting/ScDate.hpp b/src/openrct2/scripting/ScDate.hpp new file mode 100644 index 0000000000..455466753d --- /dev/null +++ b/src/openrct2/scripting/ScDate.hpp @@ -0,0 +1,95 @@ +/***************************************************************************** + * Copyright (c) 2020 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#include "../Context.h" +#include "../Date.h" +#include "../Game.h" +#include "../GameState.h" +#include "../common.h" +#include "../localisation/Date.h" +#include "Duktape.hpp" + +namespace OpenRCT2::Scripting +{ + class ScDate + { + public: + static void Register(duk_context* ctx) + { + dukglue_register_property(ctx, &monthsElapsed_get, &monthsElapsed_set, "monthsElapsed"); + dukglue_register_property(ctx, &monthProgress_get, &monthProgress_set, "monthProgress"); + dukglue_register_property(ctx, &yearsElapsed_get, nullptr, "yearsElapsed"); + dukglue_register_property(ctx, &ticksElapsed_get, nullptr, "ticksElapsed"); + dukglue_register_property(ctx, &day_get, nullptr, "day"); + dukglue_register_property(ctx, &month_get, nullptr, "month"); + dukglue_register_property(ctx, &year_get, nullptr, "year"); + } + + private: + uint32_t monthsElapsed_get() + { + const auto& date = GetDate(); + return date.GetMonthsElapsed(); + } + + void monthsElapsed_set(uint32_t value) + { + gDateMonthsElapsed = value; + } + + uint32_t monthProgress_get() + { + const auto& date = GetDate(); + return date.GetMonthTicks(); + } + + void monthProgress_set(int32_t value) + { + gDateMonthTicks = value; + } + + uint32_t yearsElapsed_get() + { + const auto& date = GetDate(); + return date.GetMonthsElapsed() / 8; + } + + uint32_t ticksElapsed_get() + { + return gCurrentTicks; + } + + int32_t day_get() + { + const auto& date = GetDate(); + return date.GetDay() + 1; + } + + int32_t month_get() + { + const auto& date = GetDate(); + return date.GetMonth(); + } + + int32_t year_get() + { + const auto& date = GetDate(); + return date.GetYear() + 1; + } + + private: + const Date& GetDate() + { + auto gameState = GetContext()->GetGameState(); + return gameState->GetDate(); + } + }; +} // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index fa17633c84..19a23ec4ef 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -18,6 +18,7 @@ #include "Duktape.hpp" #include "ScConsole.hpp" #include "ScContext.hpp" +#include "ScDate.hpp" #include "ScDisposable.hpp" #include "ScMap.hpp" #include "ScNetwork.hpp" @@ -62,6 +63,7 @@ void ScriptEngine::Initialise() auto ctx = (duk_context*)_context; ScConsole::Register(ctx); ScContext::Register(ctx); + ScDate::Register(ctx); ScDisposable::Register(ctx); ScMap::Register(ctx); ScNetwork::Register(ctx); @@ -76,6 +78,7 @@ void ScriptEngine::Initialise() dukglue_register_global(ctx, std::make_shared(_console), "console"); dukglue_register_global(ctx, std::make_shared(_execInfo, _hookEngine), "context"); + dukglue_register_global(ctx, std::make_shared(), "date"); dukglue_register_global(ctx, std::make_shared(ctx), "map"); dukglue_register_global(ctx, std::make_shared(ctx), "network"); dukglue_register_global(ctx, std::make_shared(), "park");