mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-08-12 17:19:09 +00:00
Codechange: Split ScriptDate file into Calendar and Economy files
This commit is contained in:
@@ -159,7 +159,8 @@ add_files(
|
||||
script_company.hpp
|
||||
script_companymode.hpp
|
||||
script_controller.hpp
|
||||
script_date.hpp
|
||||
script_date_calendar.hpp
|
||||
script_date_economy.hpp
|
||||
script_depotlist.hpp
|
||||
script_engine.hpp
|
||||
script_enginelist.hpp
|
||||
@@ -233,7 +234,8 @@ add_files(
|
||||
script_company.cpp
|
||||
script_companymode.cpp
|
||||
script_controller.cpp
|
||||
script_date.cpp
|
||||
script_date_calendar.cpp
|
||||
script_date_economy.cpp
|
||||
script_depotlist.cpp
|
||||
script_engine.cpp
|
||||
script_enginelist.cpp
|
||||
|
@@ -18,6 +18,25 @@
|
||||
* This version is not yet released. The following changes are not set in stone yet.
|
||||
*
|
||||
* API additions:
|
||||
* \li GSIndustry::GetConstructionDate
|
||||
* \li GSAsyncMode
|
||||
* \li GSCompanyMode::IsValid
|
||||
* \li GSCompanyMode::IsDeity
|
||||
* \li AIDateEconomy::DATE_INVALID
|
||||
* \li AIDateEconomy::IsValidDate
|
||||
* \li AIDateEconomy::GetCurrentDate
|
||||
* \li AIDateEconomy::GetYear
|
||||
* \li AIDateEconomy::GetMonth
|
||||
* \li AIDateEconomy::GetDayOfMonth
|
||||
* \li AIDateEconomy::GetDate
|
||||
* \li AIDateEconomy::GetSystemTime
|
||||
* \li AIDateCalendar::DATE_INVALID
|
||||
* \li AIDateCalendar::IsValidDate
|
||||
* \li AIDateCalendar::GetCurrentDate
|
||||
* \li AIDateCalendar::GetYear
|
||||
* \li AIDateCalendar::GetMonth
|
||||
* \li AIDateCalendar::GetDayOfMonth
|
||||
* \li AIDateCalendar::GetDate
|
||||
* \li AITown::ROAD_LAYOUT_RANDOM
|
||||
* \li AIVehicle::IsPrimaryVehicle
|
||||
*
|
||||
|
@@ -22,6 +22,21 @@
|
||||
* \li GSAsyncMode
|
||||
* \li GSCompanyMode::IsValid
|
||||
* \li GSCompanyMode::IsDeity
|
||||
* \li GSDateEconomy::DATE_INVALID
|
||||
* \li GSDateEconomy::IsValidDate
|
||||
* \li GSDateEconomy::GetCurrentDate
|
||||
* \li GSDateEconomy::GetYear
|
||||
* \li GSDateEconomy::GetMonth
|
||||
* \li GSDateEconomy::GetDayOfMonth
|
||||
* \li GSDateEconomy::GetDate
|
||||
* \li GSDateEconomy::GetSystemTime
|
||||
* \li GSDateCalendar::DATE_INVALID
|
||||
* \li GSDateCalendar::IsValidDate
|
||||
* \li GSDateCalendar::GetCurrentDate
|
||||
* \li GSDateCalendar::GetYear
|
||||
* \li GSDateCalendar::GetMonth
|
||||
* \li GSDateCalendar::GetDayOfMonth
|
||||
* \li GSDateCalendar::GetDate
|
||||
* \li GSTown::ROAD_LAYOUT_RANDOM
|
||||
* \li GSVehicle::IsPrimaryVehicle
|
||||
* \li GSOrder::SetOrderJumpTo
|
||||
|
@@ -5,27 +5,27 @@
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file script_date.cpp Implementation of ScriptDate. */
|
||||
/** @file script_date_calendar.cpp Implementation of ScriptDateCalendar. */
|
||||
|
||||
#include "../../stdafx.h"
|
||||
#include "script_date.hpp"
|
||||
#include "script_date_calendar.hpp"
|
||||
#include "../../timer/timer_game_calendar.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include "../../safeguards.h"
|
||||
|
||||
/* static */ bool ScriptDate::IsValidDate(Date date)
|
||||
/* static */ bool ScriptDateCalendar::IsValidDate(Date date)
|
||||
{
|
||||
return date >= 0;
|
||||
}
|
||||
|
||||
/* static */ ScriptDate::Date ScriptDate::GetCurrentDate()
|
||||
/* static */ ScriptDateCalendar::Date ScriptDateCalendar::GetCurrentDate()
|
||||
{
|
||||
return (ScriptDate::Date)TimerGameCalendar::date.base();
|
||||
return (ScriptDateCalendar::Date)TimerGameCalendar::date.base();
|
||||
}
|
||||
|
||||
/* static */ SQInteger ScriptDate::GetYear(ScriptDate::Date date)
|
||||
/* static */ SQInteger ScriptDateCalendar::GetYear(ScriptDateCalendar::Date date)
|
||||
{
|
||||
if (date < 0) return DATE_INVALID;
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
return ymd.year.base();
|
||||
}
|
||||
|
||||
/* static */ SQInteger ScriptDate::GetMonth(ScriptDate::Date date)
|
||||
/* static */ SQInteger ScriptDateCalendar::GetMonth(ScriptDateCalendar::Date date)
|
||||
{
|
||||
if (date < 0) return DATE_INVALID;
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
return ymd.month + 1;
|
||||
}
|
||||
|
||||
/* static */ SQInteger ScriptDate::GetDayOfMonth(ScriptDate::Date date)
|
||||
/* static */ SQInteger ScriptDateCalendar::GetDayOfMonth(ScriptDateCalendar::Date date)
|
||||
{
|
||||
if (date < 0) return DATE_INVALID;
|
||||
|
||||
@@ -52,18 +52,11 @@
|
||||
return ymd.day;
|
||||
}
|
||||
|
||||
/* static */ ScriptDate::Date ScriptDate::GetDate(SQInteger year, SQInteger month, SQInteger day_of_month)
|
||||
/* static */ ScriptDateCalendar::Date ScriptDateCalendar::GetDate(SQInteger year, SQInteger month, SQInteger day_of_month)
|
||||
{
|
||||
if (month < 1 || month > 12) return DATE_INVALID;
|
||||
if (day_of_month < 1 || day_of_month > 31) return DATE_INVALID;
|
||||
if (year < 0 || year > CalendarTime::MAX_YEAR) return DATE_INVALID;
|
||||
|
||||
return (ScriptDate::Date)::TimerGameCalendar::ConvertYMDToDate(year, month - 1, day_of_month).base();
|
||||
}
|
||||
|
||||
/* static */ SQInteger ScriptDate::GetSystemTime()
|
||||
{
|
||||
time_t t;
|
||||
time(&t);
|
||||
return t;
|
||||
return (ScriptDateCalendar::Date)::TimerGameCalendar::ConvertYMDToDate(year, month - 1, day_of_month).base();
|
||||
}
|
84
src/script/api/script_date_calendar.hpp
Normal file
84
src/script/api/script_date_calendar.hpp
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file script_date_calendar.hpp Everything to query and manipulate date related information. */
|
||||
|
||||
#ifndef SCRIPT_DATE_CALENDAR_HPP
|
||||
#define SCRIPT_DATE_CALENDAR_HPP
|
||||
|
||||
#include "script_object.hpp"
|
||||
#include "../../timer/timer_game_calendar.h"
|
||||
|
||||
/**
|
||||
* Class that handles all TimerGameCalendar related (calculation) functions.
|
||||
* @api ai game
|
||||
*
|
||||
* @note Months and days of month are 1-based; the first month of the
|
||||
* year is 1 and the first day of the month is also 1.
|
||||
* @note Years are zero based; they start with the year 0.
|
||||
* @note Dates can be used to determine the number of days between
|
||||
* two different moments in time because they count the number
|
||||
* of days since the year 0.
|
||||
*/
|
||||
class ScriptDateCalendar : public ScriptObject {
|
||||
public:
|
||||
/**
|
||||
* Date data type is an integer value. Use ScriptDateCalendar::GetDate to
|
||||
* compose valid date values for a known year, month and day.
|
||||
*/
|
||||
enum Date {
|
||||
DATE_INVALID = ::CalendarTime::INVALID_DATE.base(), ///< A value representing an invalid date.
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates if a date value represent a valid date.
|
||||
* @param date The date to validate.
|
||||
* @return True if the date is valid, otherwise false
|
||||
*/
|
||||
static bool IsValidDate(Date date);
|
||||
|
||||
/**
|
||||
* Get the current date.
|
||||
* This is the number of days since epoch under the assumption that
|
||||
* there is a leap year every 4 years, except when dividable by
|
||||
* 100 but not by 400.
|
||||
* @return The current date.
|
||||
*/
|
||||
static Date GetCurrentDate();
|
||||
|
||||
/**
|
||||
* Get the year of the given date.
|
||||
* @param date The date to get the year of.
|
||||
* @return The year.
|
||||
*/
|
||||
static SQInteger GetYear(Date date);
|
||||
|
||||
/**
|
||||
* Get the month of the given date.
|
||||
* @param date The date to get the month of.
|
||||
* @return The month.
|
||||
*/
|
||||
static SQInteger GetMonth(Date date);
|
||||
|
||||
/**
|
||||
* Get the day (of the month) of the given date.
|
||||
* @param date The date to get the day of.
|
||||
* @return The day.
|
||||
*/
|
||||
static SQInteger GetDayOfMonth(Date date);
|
||||
|
||||
/**
|
||||
* Get the date given a year, month and day of month.
|
||||
* @param year The year of the to-be determined date.
|
||||
* @param month The month of the to-be determined date.
|
||||
* @param day_of_month The day of month of the to-be determined date.
|
||||
* @return The date.
|
||||
*/
|
||||
static Date GetDate(SQInteger year, SQInteger month, SQInteger day_of_month);
|
||||
};
|
||||
|
||||
#endif /* SCRIPT_DATE_CALENDAR_HPP */
|
69
src/script/api/script_date_economy.cpp
Normal file
69
src/script/api/script_date_economy.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file script_date_economy.cpp Implementation of ScriptDateEconomy. */
|
||||
|
||||
#include "../../stdafx.h"
|
||||
#include "script_date_economy.hpp"
|
||||
#include "../../timer/timer_game_economy.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include "../../safeguards.h"
|
||||
|
||||
/* static */ bool ScriptDateEconomy::IsValidDate(Date date)
|
||||
{
|
||||
return date >= 0;
|
||||
}
|
||||
|
||||
/* static */ ScriptDateEconomy::Date ScriptDateEconomy::GetCurrentDate()
|
||||
{
|
||||
return (ScriptDateEconomy::Date)TimerGameCalendar::date.base();
|
||||
}
|
||||
|
||||
/* static */ SQInteger ScriptDateEconomy::GetYear(ScriptDateEconomy::Date date)
|
||||
{
|
||||
if (date < 0) return DATE_INVALID;
|
||||
|
||||
::TimerGameCalendar::YearMonthDay ymd;
|
||||
::TimerGameCalendar::ConvertDateToYMD(date, &ymd);
|
||||
return ymd.year.base();
|
||||
}
|
||||
|
||||
/* static */ SQInteger ScriptDateEconomy::GetMonth(ScriptDateEconomy::Date date)
|
||||
{
|
||||
if (date < 0) return DATE_INVALID;
|
||||
|
||||
::TimerGameCalendar::YearMonthDay ymd;
|
||||
::TimerGameCalendar::ConvertDateToYMD(date, &ymd);
|
||||
return ymd.month + 1;
|
||||
}
|
||||
|
||||
/* static */ SQInteger ScriptDateEconomy::GetDayOfMonth(ScriptDateEconomy::Date date)
|
||||
{
|
||||
if (date < 0) return DATE_INVALID;
|
||||
|
||||
::TimerGameCalendar::YearMonthDay ymd;
|
||||
::TimerGameCalendar::ConvertDateToYMD(date, &ymd);
|
||||
return ymd.day;
|
||||
}
|
||||
|
||||
/* static */ ScriptDateEconomy::Date ScriptDateEconomy::GetDate(SQInteger year, SQInteger month, SQInteger day_of_month)
|
||||
{
|
||||
if (month < 1 || month > 12) return DATE_INVALID;
|
||||
if (day_of_month < 1 || day_of_month > 31) return DATE_INVALID;
|
||||
if (year < 0 || year > CalendarTime::MAX_YEAR) return DATE_INVALID;
|
||||
|
||||
return (ScriptDateEconomy::Date)::TimerGameCalendar::ConvertYMDToDate(year, month - 1, day_of_month).base();
|
||||
}
|
||||
|
||||
/* static */ SQInteger ScriptDateEconomy::GetSystemTime()
|
||||
{
|
||||
time_t t;
|
||||
time(&t);
|
||||
return t;
|
||||
}
|
@@ -5,13 +5,13 @@
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file script_date.hpp Everything to query and manipulate date related information. */
|
||||
/** @file script_date_economy.hpp Everything to query and manipulate date related information. */
|
||||
|
||||
#ifndef SCRIPT_DATE_HPP
|
||||
#define SCRIPT_DATE_HPP
|
||||
#ifndef SCRIPT_DATE_ECONOMY_HPP
|
||||
#define SCRIPT_DATE_ECONOMY_HPP
|
||||
|
||||
#include "script_object.hpp"
|
||||
#include "timer/timer_game_calendar.h"
|
||||
#include "../../timer/timer_game_economy.h"
|
||||
|
||||
/**
|
||||
* Class that handles all date related (calculation) functions.
|
||||
@@ -24,10 +24,10 @@
|
||||
* two different moments in time because they count the number
|
||||
* of days since the year 0.
|
||||
*/
|
||||
class ScriptDate : public ScriptObject {
|
||||
class ScriptDateEconomy : public ScriptObject {
|
||||
public:
|
||||
/**
|
||||
* Date data type is an integer value. Use ScriptDate::GetDate to
|
||||
* Date data type is an integer value. Use ScriptDateEconomy::GetDate to
|
||||
* compose valid date values for a known year, month and day.
|
||||
*/
|
||||
enum Date {
|
||||
@@ -89,4 +89,4 @@ public:
|
||||
static SQInteger GetSystemTime();
|
||||
};
|
||||
|
||||
#endif /* SCRIPT_DATE_HPP */
|
||||
#endif /* SCRIPT_DATE_ECONOMY_HPP */
|
Reference in New Issue
Block a user