(svn r12437) [NoAI] -Add: functions to get the current date and to determine the year/month/day from that date. noai
authorrubidium
Thu, 27 Mar 2008 05:15:06 +0000
branchnoai
changeset 9825 cc77111ebd85
parent 9824 2c2a5a27c4eb
child 9826 9707ad4c9b60
(svn r12437) [NoAI] -Add: functions to get the current date and to determine the year/month/day from that date.
projects/openttd_vs80.vcproj
projects/openttd_vs90.vcproj
source.list
src/ai/ai_squirrel.cpp
src/ai/api/ai_date.cpp
src/ai/api/ai_date.hpp
src/ai/api/ai_date.hpp.sq
--- a/projects/openttd_vs80.vcproj	Wed Mar 26 15:30:02 2008 +0000
+++ b/projects/openttd_vs80.vcproj	Thu Mar 27 05:15:06 2008 +0000
@@ -1956,6 +1956,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_date.hpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_engine.hpp"
 				>
 			</File>
@@ -2112,6 +2116,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_date.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_engine.cpp"
 				>
 			</File>
--- a/projects/openttd_vs90.vcproj	Wed Mar 26 15:30:02 2008 +0000
+++ b/projects/openttd_vs90.vcproj	Thu Mar 27 05:15:06 2008 +0000
@@ -1953,6 +1953,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_date.hpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_engine.hpp"
 				>
 			</File>
@@ -2109,6 +2113,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_date.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_engine.cpp"
 				>
 			</File>
--- a/source.list	Wed Mar 26 15:30:02 2008 +0000
+++ b/source.list	Thu Mar 27 05:15:06 2008 +0000
@@ -404,6 +404,7 @@
 ai/api/ai_cargolist.hpp
 ai/api/ai_company.hpp
 ai/api/ai_controller.hpp
+ai/api/ai_date.hpp
 ai/api/ai_engine.hpp
 ai/api/ai_enginelist.hpp
 ai/api/ai_event.hpp
@@ -444,6 +445,7 @@
 ai/api/ai_cargolist.cpp
 ai/api/ai_company.cpp
 ai/api/ai_controller.cpp
+ai/api/ai_date.cpp
 ai/api/ai_engine.cpp
 ai/api/ai_enginelist.cpp
 ai/api/ai_event.cpp
--- a/src/ai/ai_squirrel.cpp	Wed Mar 26 15:30:02 2008 +0000
+++ b/src/ai/ai_squirrel.cpp	Thu Mar 27 05:15:06 2008 +0000
@@ -32,6 +32,7 @@
 #include "api/ai_cargolist.hpp.sq"
 #include "api/ai_company.hpp.sq"
 #include "api/ai_controller.hpp.sq"
+#include "api/ai_date.hpp.sq"
 #include "api/ai_engine.hpp.sq"
 #include "api/ai_enginelist.hpp.sq"
 #include "api/ai_event.hpp.sq"
@@ -222,6 +223,7 @@
 	SQAICargoList_Register(this->engine);
 	SQAICompany_Register(this->engine);
 	SQAIController_Register(this->engine);
+	SQAIDate_Register(this->engine);
 	SQAIEngine_Register(this->engine);
 	SQAIEngineList_Register(this->engine);
 	SQAIEvent_Register(this->engine);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_date.cpp	Thu Mar 27 05:15:06 2008 +0000
@@ -0,0 +1,47 @@
+/* $Id$ */
+
+/** @file ai_date.cpp handles the functions of the AIDate class */
+
+#include "ai_date.hpp"
+#include "../../date_func.h"
+
+/* static */ int32 AIDate::GetCurrentDate()
+{
+	return ::_date;
+}
+
+/* static */ int32 AIDate::GetYear(int32 date)
+{
+	if (date < 0) return -1;
+
+	::YearMonthDay ymd;
+	::ConvertDateToYMD(date, &ymd);
+	return ymd.year;
+}
+
+/* static */ int32 AIDate::GetMonth(int32 date)
+{
+	if (date < 0) return -1;
+
+	::YearMonthDay ymd;
+	::ConvertDateToYMD(date, &ymd);
+	return ymd.month + 1;
+}
+
+/* static */ int32 AIDate::GetDayOfMonth(int32 date)
+{
+	if (date < 0) return -1;
+
+	::YearMonthDay ymd;
+	::ConvertDateToYMD(date, &ymd);
+	return ymd.day;
+}
+
+/* static */ int32 AIDate::GetDate(int32 year, int32 month, int32 day_of_month)
+{
+	if (month < 1 || month > 12) return -1;
+	if (day_of_month < 1 || day_of_month > 31) return -1;
+	if (year < 1 || year > MAX_YEAR) return -1;
+
+	return ::ConvertYMDToDate(year, month - 1, day_of_month);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_date.hpp	Thu Mar 27 05:15:06 2008 +0000
@@ -0,0 +1,67 @@
+/* $Id$ */
+
+/** @file ai_date.hpp declaration of class for AIDate class */
+
+#ifndef AI_DATE_HPP
+#define AI_DATE_HPP
+
+#include "ai_object.hpp"
+
+/**
+ * Class that handles all date related (calculation) functions.
+ *
+ * @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 AIDate : public AIObject {
+public:
+	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIDate"; }
+
+	/**
+	 * 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 a date.
+	 */
+	static int32 GetCurrentDate();
+
+	/**
+	 * Get the year of the given date.
+	 * @param date the date to get the year of.
+	 * @return the year.
+	 */
+	static int32 GetYear(int32 date);
+
+	/**
+	 * Get the month of the given date.
+	 * @param date the date to get the month of.
+	 * @return the month.
+	 */
+	static int32 GetMonth(int32 date);
+
+	/**
+	 * Get the day (of the month) of the given date.
+	 * @param date the date to get the day of.
+	 * @return the day.
+	 */
+	static int32 GetDayOfMonth(int32 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 int32 GetDate(int32 year, int32 month, int32 day_of_month);
+};
+
+#endif /* AI_DATE_HPP */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_date.hpp.sq	Thu Mar 27 05:15:06 2008 +0000
@@ -0,0 +1,25 @@
+#include "ai_date.hpp"
+
+namespace SQConvert {
+	/* Allow AIDate to be used as Squirrel parameter */
+	template <> AIDate *GetParam(ForceType<AIDate *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIDate *)instance; }
+	template <> AIDate &GetParam(ForceType<AIDate &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIDate *)instance; }
+	template <> const AIDate *GetParam(ForceType<const AIDate *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIDate *)instance; }
+	template <> const AIDate &GetParam(ForceType<const AIDate &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIDate *)instance; }
+	template <> int Return<AIDate *>(HSQUIRRELVM vm, AIDate *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIDate", res, NULL, DefSQDestructorCallback<AIDate>); return 1; }
+}; // namespace SQConvert
+
+void SQAIDate_Register(Squirrel *engine) {
+	DefSQClass <AIDate> SQAIDate("AIDate");
+	SQAIDate.PreRegister(engine);
+	SQAIDate.AddConstructor<void (AIDate::*)(), 1>(engine, "x");
+
+	SQAIDate.DefSQStaticMethod(engine, &AIDate::GetClassName,   "GetClassName",   1, "x");
+	SQAIDate.DefSQStaticMethod(engine, &AIDate::GetCurrentDate, "GetCurrentDate", 1, "x");
+	SQAIDate.DefSQStaticMethod(engine, &AIDate::GetYear,        "GetYear",        2, "xi");
+	SQAIDate.DefSQStaticMethod(engine, &AIDate::GetMonth,       "GetMonth",       2, "xi");
+	SQAIDate.DefSQStaticMethod(engine, &AIDate::GetDayOfMonth,  "GetDayOfMonth",  2, "xi");
+	SQAIDate.DefSQStaticMethod(engine, &AIDate::GetDate,        "GetDate",        4, "xiii");
+
+	SQAIDate.PostRegister(engine);
+}