2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file script_subsidy.cpp Implementation of ScriptSubsidy. */
10 #include "../../stdafx.h"
11 #include "script_subsidy.hpp"
12 #include "script_date.hpp"
13 #include "script_industry.hpp"
14 #include "script_town.hpp"
15 #include "script_error.hpp"
16 #include "../../subsidy_base.h"
17 #include "../../station_base.h"
18 #include "../../subsidy_cmd.h"
20 #include "../../safeguards.h"
22 /* static */ bool ScriptSubsidy::IsValidSubsidy(SubsidyID subsidy_id
)
24 return ::Subsidy::IsValidID(subsidy_id
);
27 /* static */ bool ScriptSubsidy::IsAwarded(SubsidyID subsidy_id
)
29 if (!IsValidSubsidy(subsidy_id
)) return false;
31 return ::Subsidy::Get(subsidy_id
)->IsAwarded();
34 /* static */ bool ScriptSubsidy::Create(CargoID cargo_type
, SubsidyParticipantType from_type
, SQInteger from_id
, SubsidyParticipantType to_type
, SQInteger to_id
)
36 EnforceDeityMode(false);
37 EnforcePrecondition(false, ScriptCargo::IsValidCargo(cargo_type
));
38 EnforcePrecondition(false, from_type
== SPT_INDUSTRY
|| from_type
== SPT_TOWN
);
39 EnforcePrecondition(false, to_type
== SPT_INDUSTRY
|| to_type
== SPT_TOWN
);
40 EnforcePrecondition(false, (from_type
== SPT_INDUSTRY
&& ScriptIndustry::IsValidIndustry(from_id
)) || (from_type
== SPT_TOWN
&& ScriptTown::IsValidTown(from_id
)));
41 EnforcePrecondition(false, (to_type
== SPT_INDUSTRY
&& ScriptIndustry::IsValidIndustry(to_id
)) || (to_type
== SPT_TOWN
&& ScriptTown::IsValidTown(to_id
)));
43 return ScriptObject::Command
<CMD_CREATE_SUBSIDY
>::Do(cargo_type
, (::SourceType
)from_type
, from_id
, (::SourceType
)to_type
, to_id
);
46 /* static */ ScriptCompany::CompanyID
ScriptSubsidy::GetAwardedTo(SubsidyID subsidy_id
)
48 if (!IsAwarded(subsidy_id
)) return ScriptCompany::COMPANY_INVALID
;
50 return (ScriptCompany::CompanyID
)((uint8_t)::Subsidy::Get(subsidy_id
)->awarded
);
53 /* static */ ScriptDate::Date
ScriptSubsidy::GetExpireDate(SubsidyID subsidy_id
)
55 if (!IsValidSubsidy(subsidy_id
)) return ScriptDate::DATE_INVALID
;
57 TimerGameEconomy::YearMonthDay ymd
= TimerGameEconomy::ConvertDateToYMD(TimerGameEconomy::date
);
59 auto m
= ymd
.month
+ ::Subsidy::Get(subsidy_id
)->remaining
;
60 ymd
.month
= (m
- 1) % 12 + 1;
61 ymd
.year
+= TimerGameEconomy::Year
{(m
- 1) / 12};
63 return (ScriptDate::Date
)TimerGameEconomy::ConvertYMDToDate(ymd
.year
, ymd
.month
, ymd
.day
).base();
66 /* static */ CargoID
ScriptSubsidy::GetCargoType(SubsidyID subsidy_id
)
68 if (!IsValidSubsidy(subsidy_id
)) return INVALID_CARGO
;
70 return ::Subsidy::Get(subsidy_id
)->cargo_type
;
73 /* static */ ScriptSubsidy::SubsidyParticipantType
ScriptSubsidy::GetSourceType(SubsidyID subsidy_id
)
75 if (!IsValidSubsidy(subsidy_id
)) return SPT_INVALID
;
77 return (SubsidyParticipantType
)(uint
)::Subsidy::Get(subsidy_id
)->src_type
;
80 /* static */ SQInteger
ScriptSubsidy::GetSourceIndex(SubsidyID subsidy_id
)
82 if (!IsValidSubsidy(subsidy_id
)) return INVALID_SOURCE
;
84 return ::Subsidy::Get(subsidy_id
)->src
;
87 /* static */ ScriptSubsidy::SubsidyParticipantType
ScriptSubsidy::GetDestinationType(SubsidyID subsidy_id
)
89 if (!IsValidSubsidy(subsidy_id
)) return SPT_INVALID
;
91 return (SubsidyParticipantType
)(uint
)::Subsidy::Get(subsidy_id
)->dst_type
;
94 /* static */ SQInteger
ScriptSubsidy::GetDestinationIndex(SubsidyID subsidy_id
)
96 if (!IsValidSubsidy(subsidy_id
)) return INVALID_SOURCE
;
98 return ::Subsidy::Get(subsidy_id
)->dst
;