script_subsidy.cpp

Go to the documentation of this file.
00001 /* $Id: script_subsidy.cpp 23628 2011-12-19 21:01:12Z truebrain $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../../stdafx.h"
00013 #include "script_subsidy.hpp"
00014 #include "script_date.hpp"
00015 #include "script_industry.hpp"
00016 #include "script_town.hpp"
00017 #include "script_error.hpp"
00018 #include "../../subsidy_base.h"
00019 #include "../../station_base.h"
00020 
00021 /* static */ bool ScriptSubsidy::IsValidSubsidy(SubsidyID subsidy_id)
00022 {
00023   return ::Subsidy::IsValidID(subsidy_id);
00024 }
00025 
00026 /* static */ bool ScriptSubsidy::IsAwarded(SubsidyID subsidy_id)
00027 {
00028   if (!IsValidSubsidy(subsidy_id)) return false;
00029 
00030   return ::Subsidy::Get(subsidy_id)->IsAwarded();
00031 }
00032 
00033 /* static */ bool ScriptSubsidy::Create(CargoID cargo_type, SubsidyParticipantType from_type, uint16 from_id, SubsidyParticipantType to_type, uint16 to_id)
00034 {
00035   EnforcePrecondition(false, ScriptCargo::IsValidCargo(cargo_type));
00036   EnforcePrecondition(false, from_type == SPT_INDUSTRY || from_type == SPT_TOWN);
00037   EnforcePrecondition(false, to_type == SPT_INDUSTRY || to_type == SPT_TOWN);
00038   EnforcePrecondition(false, (from_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(from_id)) || (from_type == SPT_TOWN && ScriptTown::IsValidTown(from_id)));
00039   EnforcePrecondition(false, (to_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(to_id)) || (to_type == SPT_TOWN && ScriptTown::IsValidTown(to_id)));
00040 
00041   return ScriptObject::DoCommand(0, from_type | (from_id << 8) | (cargo_type << 24), to_type | (to_id << 8), CMD_CREATE_SUBSIDY);
00042 }
00043 
00044 /* static */ ScriptCompany::CompanyID ScriptSubsidy::GetAwardedTo(SubsidyID subsidy_id)
00045 {
00046   if (!IsAwarded(subsidy_id)) return ScriptCompany::COMPANY_INVALID;
00047 
00048   return (ScriptCompany::CompanyID)((byte)::Subsidy::Get(subsidy_id)->awarded);
00049 }
00050 
00051 /* static */ int32 ScriptSubsidy::GetExpireDate(SubsidyID subsidy_id)
00052 {
00053   if (!IsValidSubsidy(subsidy_id)) return -1;
00054 
00055   int year = ScriptDate::GetYear(ScriptDate::GetCurrentDate());
00056   int month = ScriptDate::GetMonth(ScriptDate::GetCurrentDate());
00057 
00058   month += ::Subsidy::Get(subsidy_id)->remaining;
00059 
00060   year += (month - 1) / 12;
00061   month = ((month - 1) % 12) + 1;
00062 
00063   return ScriptDate::GetDate(year, month, 1);
00064 }
00065 
00066 /* static */ CargoID ScriptSubsidy::GetCargoType(SubsidyID subsidy_id)
00067 {
00068   if (!IsValidSubsidy(subsidy_id)) return CT_INVALID;
00069 
00070   return ::Subsidy::Get(subsidy_id)->cargo_type;
00071 }
00072 
00073 /* static */ ScriptSubsidy::SubsidyParticipantType ScriptSubsidy::GetSourceType(SubsidyID subsidy_id)
00074 {
00075   if (!IsValidSubsidy(subsidy_id)) return SPT_INVALID;
00076 
00077   return (SubsidyParticipantType)(uint)::Subsidy::Get(subsidy_id)->src_type;
00078 }
00079 
00080 /* static */ int32 ScriptSubsidy::GetSourceIndex(SubsidyID subsidy_id)
00081 {
00082   if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION;
00083 
00084   return ::Subsidy::Get(subsidy_id)->src;
00085 }
00086 
00087 /* static */ ScriptSubsidy::SubsidyParticipantType ScriptSubsidy::GetDestinationType(SubsidyID subsidy_id)
00088 {
00089   if (!IsValidSubsidy(subsidy_id)) return SPT_INVALID;
00090 
00091   return (SubsidyParticipantType)(uint)::Subsidy::Get(subsidy_id)->dst_type;
00092 }
00093 
00094 /* static */ int32 ScriptSubsidy::GetDestinationIndex(SubsidyID subsidy_id)
00095 {
00096   if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION;
00097 
00098   return ::Subsidy::Get(subsidy_id)->dst;
00099 }