mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-23 15:52:55 +01:00
Implement date API
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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";
|
||||
|
||||
95
src/openrct2/scripting/ScDate.hpp
Normal file
95
src/openrct2/scripting/ScDate.hpp
Normal file
@@ -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
|
||||
@@ -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<ScConsole>(_console), "console");
|
||||
dukglue_register_global(ctx, std::make_shared<ScContext>(_execInfo, _hookEngine), "context");
|
||||
dukglue_register_global(ctx, std::make_shared<ScDate>(), "date");
|
||||
dukglue_register_global(ctx, std::make_shared<ScMap>(ctx), "map");
|
||||
dukglue_register_global(ctx, std::make_shared<ScNetwork>(ctx), "network");
|
||||
dukglue_register_global(ctx, std::make_shared<ScPark>(), "park");
|
||||
|
||||
Reference in New Issue
Block a user