diff --git a/src/script/api/ai/ai_date.hpp.sq b/src/script/api/ai/ai_date.hpp.sq index 3210a6950f..7bdf9519d6 100644 --- a/src/script/api/ai/ai_date.hpp.sq +++ b/src/script/api/ai/ai_date.hpp.sq @@ -21,6 +21,9 @@ void SQAIDate_Register(Squirrel *engine) SQAIDate.PreRegister(engine); SQAIDate.AddConstructor(engine, "x"); + SQAIDate.DefSQConst(engine, ScriptDate::DATE_INVALID, "DATE_INVALID"); + + SQAIDate.DefSQStaticMethod(engine, &ScriptDate::IsValidDate, "IsValidDate", 2, ".i"); SQAIDate.DefSQStaticMethod(engine, &ScriptDate::GetCurrentDate, "GetCurrentDate", 1, "."); SQAIDate.DefSQStaticMethod(engine, &ScriptDate::GetYear, "GetYear", 2, ".i"); SQAIDate.DefSQStaticMethod(engine, &ScriptDate::GetMonth, "GetMonth", 2, ".i"); diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp index a40996c0f8..5c9baa616a 100644 --- a/src/script/api/ai_changelog.hpp +++ b/src/script/api/ai_changelog.hpp @@ -20,6 +20,8 @@ * 1.4.0 is not yet released. The following changes are not set in stone yet. * * API additions: + * \li AIDate::DATE_INVALID + * \li AIDate::IsValidDate * \li AIStation::HasCargoRating * \li AITile::GetTerrainType * \li AITown::FoundTown diff --git a/src/script/api/game/game_date.hpp.sq b/src/script/api/game/game_date.hpp.sq index 43af86afa7..dc40989da0 100644 --- a/src/script/api/game/game_date.hpp.sq +++ b/src/script/api/game/game_date.hpp.sq @@ -21,6 +21,9 @@ void SQGSDate_Register(Squirrel *engine) SQGSDate.PreRegister(engine); SQGSDate.AddConstructor(engine, "x"); + SQGSDate.DefSQConst(engine, ScriptDate::DATE_INVALID, "DATE_INVALID"); + + SQGSDate.DefSQStaticMethod(engine, &ScriptDate::IsValidDate, "IsValidDate", 2, ".i"); SQGSDate.DefSQStaticMethod(engine, &ScriptDate::GetCurrentDate, "GetCurrentDate", 1, "."); SQGSDate.DefSQStaticMethod(engine, &ScriptDate::GetYear, "GetYear", 2, ".i"); SQGSDate.DefSQStaticMethod(engine, &ScriptDate::GetMonth, "GetMonth", 2, ".i"); diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp index 20a28edcde..4ce3205143 100644 --- a/src/script/api/game_changelog.hpp +++ b/src/script/api/game_changelog.hpp @@ -21,6 +21,8 @@ * * API additions: * \li GSCompany::ChangeBankBalance + * \li GSDate::DATE_INVALID + * \li GSDate::IsValidDate * \li GSGoal::GT_STORY_PAGE * \li GSGoal::IsCompleted * \li GSGoal::SetCompleted diff --git a/src/script/api/script_basestation.cpp b/src/script/api/script_basestation.cpp index d46d717a0c..5134243a96 100644 --- a/src/script/api/script_basestation.cpp +++ b/src/script/api/script_basestation.cpp @@ -52,9 +52,9 @@ return ::BaseStation::Get(station_id)->xy; } -/* static */ int32 ScriptBaseStation::GetConstructionDate(StationID station_id) +/* static */ ScriptDate::Date ScriptBaseStation::GetConstructionDate(StationID station_id) { - if (!IsValidBaseStation(station_id)) return -1; + if (!IsValidBaseStation(station_id)) return ScriptDate::DATE_INVALID; - return ::BaseStation::Get(station_id)->build_date; + return (ScriptDate::Date)::BaseStation::Get(station_id)->build_date; } diff --git a/src/script/api/script_basestation.hpp b/src/script/api/script_basestation.hpp index e610280790..9676829f67 100644 --- a/src/script/api/script_basestation.hpp +++ b/src/script/api/script_basestation.hpp @@ -13,6 +13,7 @@ #define SCRIPT_BASESTATION_HPP #include "script_text.hpp" +#include "script_date.hpp" /** * Base class for stations and waypoints. @@ -73,7 +74,7 @@ public: * @param station_id The station to look at. * @return The last date some part of this station was build. */ - static int32 GetConstructionDate(StationID station_id); + static ScriptDate::Date GetConstructionDate(StationID station_id); }; #endif /* SCRIPT_BASESTATION_HPP */ diff --git a/src/script/api/script_date.cpp b/src/script/api/script_date.cpp index 1f80b40d7e..6ff92debac 100644 --- a/src/script/api/script_date.cpp +++ b/src/script/api/script_date.cpp @@ -14,45 +14,50 @@ #include "script_date.hpp" #include "../../date_func.h" -/* static */ int32 ScriptDate::GetCurrentDate() +/* static */ bool ScriptDate::IsValidDate(Date date) { - return ::_date; + return date >= 0; } -/* static */ int32 ScriptDate::GetYear(int32 date) +/* static */ ScriptDate::Date ScriptDate::GetCurrentDate() { - if (date < 0) return -1; + return (ScriptDate::Date)_date; +} + +/* static */ int32 ScriptDate::GetYear(ScriptDate::Date date) +{ + if (date < 0) return DATE_INVALID; ::YearMonthDay ymd; ::ConvertDateToYMD(date, &ymd); return ymd.year; } -/* static */ int32 ScriptDate::GetMonth(int32 date) +/* static */ int32 ScriptDate::GetMonth(ScriptDate::Date date) { - if (date < 0) return -1; + if (date < 0) return DATE_INVALID; ::YearMonthDay ymd; ::ConvertDateToYMD(date, &ymd); return ymd.month + 1; } -/* static */ int32 ScriptDate::GetDayOfMonth(int32 date) +/* static */ int32 ScriptDate::GetDayOfMonth(ScriptDate::Date date) { - if (date < 0) return -1; + if (date < 0) return DATE_INVALID; ::YearMonthDay ymd; ::ConvertDateToYMD(date, &ymd); return ymd.day; } -/* static */ int32 ScriptDate::GetDate(int32 year, int32 month, int32 day_of_month) +/* static */ ScriptDate::Date ScriptDate::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 < 0 || year > MAX_YEAR) return -1; + if (month < 1 || month > 12) return DATE_INVALID; + if (day_of_month < 1 || day_of_month > 31) return DATE_INVALID; + if (year < 0 || year > MAX_YEAR) return DATE_INVALID; - return ::ConvertYMDToDate(year, month - 1, day_of_month); + return (ScriptDate::Date)::ConvertYMDToDate(year, month - 1, day_of_month); } /* static */ int32 ScriptDate::GetSystemTime() diff --git a/src/script/api/script_date.hpp b/src/script/api/script_date.hpp index 488e35edae..b402b0a2b2 100644 --- a/src/script/api/script_date.hpp +++ b/src/script/api/script_date.hpp @@ -13,6 +13,7 @@ #define SCRIPT_DATE_HPP #include "script_object.hpp" +#include "../../date_type.h" /** * Class that handles all date related (calculation) functions. @@ -27,6 +28,21 @@ */ class ScriptDate : public ScriptObject { public: + /** + * Date data type is an integer value. Use ScriptDate::GetDate to + * compose valid date values for a known year, month and day. + */ + enum Date { + DATE_INVALID = ::INVALID_DATE, ///< 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 @@ -34,28 +50,28 @@ public: * 100 but not by 400. * @return The current date. */ - static int32 GetCurrentDate(); + static Date 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); + static int32 GetYear(Date 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); + static int32 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 int32 GetDayOfMonth(int32 date); + static int32 GetDayOfMonth(Date date); /** * Get the date given a year, month and day of month. @@ -64,7 +80,7 @@ public: * @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); + static Date GetDate(int32 year, int32 month, int32 day_of_month); /** * Get the time of the host system. diff --git a/src/script/api/script_engine.cpp b/src/script/api/script_engine.cpp index a34d0910f3..138628547b 100644 --- a/src/script/api/script_engine.cpp +++ b/src/script/api/script_engine.cpp @@ -171,11 +171,11 @@ return ::Engine::Get(engine_id)->GetDisplayMaxTractiveEffort(); } -/* static */ int32 ScriptEngine::GetDesignDate(EngineID engine_id) +/* static */ ScriptDate::Date ScriptEngine::GetDesignDate(EngineID engine_id) { - if (!IsValidEngine(engine_id)) return -1; + if (!IsValidEngine(engine_id)) return ScriptDate::DATE_INVALID; - return ::Engine::Get(engine_id)->intro_date; + return (ScriptDate::Date)::Engine::Get(engine_id)->intro_date; } /* static */ ScriptVehicle::VehicleType ScriptEngine::GetVehicleType(EngineID engine_id) diff --git a/src/script/api/script_engine.hpp b/src/script/api/script_engine.hpp index 058d23082c..5f703e159d 100644 --- a/src/script/api/script_engine.hpp +++ b/src/script/api/script_engine.hpp @@ -15,6 +15,7 @@ #include "script_vehicle.hpp" #include "script_rail.hpp" #include "script_airport.hpp" +#include "script_date.hpp" /** * Class that handles all engine related functions. @@ -174,7 +175,7 @@ public: * @pre IsValidEngine(engine_id). * @return The date this engine was designed. */ - static int32 GetDesignDate(EngineID engine_id); + static ScriptDate::Date GetDesignDate(EngineID engine_id); /** * Get the type of an engine. diff --git a/src/script/api/script_story_page.cpp b/src/script/api/script_story_page.cpp index 034cfdf2a6..17b7b82736 100644 --- a/src/script/api/script_story_page.cpp +++ b/src/script/api/script_story_page.cpp @@ -132,14 +132,15 @@ return company; } -/* static */ int32 ScriptStoryPage::GetDate(StoryPageID story_page_id) +/* static */ ScriptDate::Date ScriptStoryPage::GetDate(StoryPageID story_page_id) { - EnforcePrecondition(-1, IsValidStoryPage(story_page_id)); + EnforcePrecondition(ScriptDate::DATE_INVALID, IsValidStoryPage(story_page_id)); + EnforcePrecondition(ScriptDate::DATE_INVALID, ScriptObject::GetCompany() == OWNER_DEITY); - return StoryPage::Get(story_page_id)->date; + return (ScriptDate::Date)StoryPage::Get(story_page_id)->date; } -/* static */ bool ScriptStoryPage::SetDate(StoryPageID story_page_id, int32 date) +/* static */ bool ScriptStoryPage::SetDate(StoryPageID story_page_id, ScriptDate::Date date) { EnforcePrecondition(false, IsValidStoryPage(story_page_id)); EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY); diff --git a/src/script/api/script_story_page.hpp b/src/script/api/script_story_page.hpp index 49d4a639a6..c22d4c198e 100644 --- a/src/script/api/script_story_page.hpp +++ b/src/script/api/script_story_page.hpp @@ -13,6 +13,7 @@ #define SCRIPT_STORY_HPP #include "script_company.hpp" +#include "script_date.hpp" #include "../../story_type.h" #include "../../story_base.h" @@ -154,17 +155,17 @@ public: * @return The date * @pre IsValidStoryPage(story_page_id). */ - static int32 GetDate(StoryPageID story_page_id); + static ScriptDate::Date GetDate(StoryPageID story_page_id); /** * Update date of a story page. The date is shown in the top left of the page * @param story_page_id The story page to set the date for. - * @param date Page date (@see ScriptDate) + * @param date Date to display at the top of story page or ScriptDate::DATE_INVALID to disable showing date on this page. (also, @see ScriptDate) * @return True if the action succeeded. * @pre No ScriptCompanyMode may be in scope. * @pre IsValidStoryPage(story_page_id). */ - static bool SetDate(StoryPageID story_page_id, int32 date); + static bool SetDate(StoryPageID story_page_id, ScriptDate::Date date); /** * Update title of a story page. The title is shown in the page selector drop down. diff --git a/src/script/api/script_subsidy.cpp b/src/script/api/script_subsidy.cpp index 8ec074d4d0..146ba690d4 100644 --- a/src/script/api/script_subsidy.cpp +++ b/src/script/api/script_subsidy.cpp @@ -48,9 +48,9 @@ return (ScriptCompany::CompanyID)((byte)::Subsidy::Get(subsidy_id)->awarded); } -/* static */ int32 ScriptSubsidy::GetExpireDate(SubsidyID subsidy_id) +/* static */ ScriptDate::Date ScriptSubsidy::GetExpireDate(SubsidyID subsidy_id) { - if (!IsValidSubsidy(subsidy_id)) return -1; + if (!IsValidSubsidy(subsidy_id)) return ScriptDate::DATE_INVALID; int year = ScriptDate::GetYear(ScriptDate::GetCurrentDate()); int month = ScriptDate::GetMonth(ScriptDate::GetCurrentDate()); diff --git a/src/script/api/script_subsidy.hpp b/src/script/api/script_subsidy.hpp index 3069ad918f..16a6794409 100644 --- a/src/script/api/script_subsidy.hpp +++ b/src/script/api/script_subsidy.hpp @@ -13,6 +13,7 @@ #define SCRIPT_SUBSIDY_HPP #include "script_company.hpp" +#include "script_date.hpp" /** * Class that handles all subsidy related functions. @@ -83,7 +84,7 @@ public: * @note The return value of this function will change if the subsidy is * awarded. */ - static int32 GetExpireDate(SubsidyID subsidy_id); + static ScriptDate::Date GetExpireDate(SubsidyID subsidy_id); /** * Get the cargo type that has to be transported in order to be awarded this diff --git a/src/script/api/template/template_date.hpp.sq b/src/script/api/template/template_date.hpp.sq index 976ddb7a50..cca4a258b1 100644 --- a/src/script/api/template/template_date.hpp.sq +++ b/src/script/api/template/template_date.hpp.sq @@ -12,6 +12,10 @@ #include "../script_date.hpp" namespace SQConvert { + /* Allow enums to be used as Squirrel parameters */ + template <> inline ScriptDate::Date GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (ScriptDate::Date)tmp; } + template <> inline int Return(HSQUIRRELVM vm, ScriptDate::Date res) { sq_pushinteger(vm, (int32)res); return 1; } + /* Allow ScriptDate to be used as Squirrel parameter */ template <> inline ScriptDate *GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptDate *)instance; } template <> inline ScriptDate &GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptDate *)instance; } diff --git a/src/story_gui.cpp b/src/story_gui.cpp index 9f84e57b0a..872ed0a9a2 100644 --- a/src/story_gui.cpp +++ b/src/story_gui.cpp @@ -519,8 +519,10 @@ public: int y_offset = - this->vscroll->GetPosition(); /* Date */ - SetDParam(0, page->date); - DrawString(0, right - x, y_offset, STR_JUST_DATE_LONG, TC_BLACK); + if (page->date != INVALID_DATE) { + SetDParam(0, page->date); + DrawString(0, right - x, y_offset, STR_JUST_DATE_LONG, TC_BLACK); + } y_offset += line_height; /* Title */