2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/** @file script_subsidy.cpp Implementation of ScriptSubsidy. */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-01-22 10:33:16 +00:00
|
|
|
#include "../../stdafx.h"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "script_subsidy.hpp"
|
|
|
|
#include "script_date.hpp"
|
2011-12-19 21:01:12 +00:00
|
|
|
#include "script_industry.hpp"
|
|
|
|
#include "script_town.hpp"
|
|
|
|
#include "script_error.hpp"
|
2009-07-01 18:45:05 +00:00
|
|
|
#include "../../subsidy_base.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../../station_base.h"
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptSubsidy::IsValidSubsidy(SubsidyID subsidy_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2009-08-07 22:23:34 +00:00
|
|
|
return ::Subsidy::IsValidID(subsidy_id);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptSubsidy::IsAwarded(SubsidyID subsidy_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!IsValidSubsidy(subsidy_id)) return false;
|
|
|
|
|
2009-08-07 22:23:34 +00:00
|
|
|
return ::Subsidy::Get(subsidy_id)->IsAwarded();
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 21:01:12 +00:00
|
|
|
/* static */ bool ScriptSubsidy::Create(CargoID cargo_type, SubsidyParticipantType from_type, uint16 from_id, SubsidyParticipantType to_type, uint16 to_id)
|
|
|
|
{
|
|
|
|
EnforcePrecondition(false, ScriptCargo::IsValidCargo(cargo_type));
|
|
|
|
EnforcePrecondition(false, from_type == SPT_INDUSTRY || from_type == SPT_TOWN);
|
|
|
|
EnforcePrecondition(false, to_type == SPT_INDUSTRY || to_type == SPT_TOWN);
|
|
|
|
EnforcePrecondition(false, (from_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(from_id)) || (from_type == SPT_TOWN && ScriptTown::IsValidTown(from_id)));
|
|
|
|
EnforcePrecondition(false, (to_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(to_id)) || (to_type == SPT_TOWN && ScriptTown::IsValidTown(to_id)));
|
|
|
|
|
|
|
|
return ScriptObject::DoCommand(0, from_type | (from_id << 8) | (cargo_type << 24), to_type | (to_id << 8), CMD_CREATE_SUBSIDY);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ ScriptCompany::CompanyID ScriptSubsidy::GetAwardedTo(SubsidyID subsidy_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
if (!IsAwarded(subsidy_id)) return ScriptCompany::COMPANY_INVALID;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return (ScriptCompany::CompanyID)((byte)::Subsidy::Get(subsidy_id)->awarded);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int32 ScriptSubsidy::GetExpireDate(SubsidyID subsidy_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!IsValidSubsidy(subsidy_id)) return -1;
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
int year = ScriptDate::GetYear(ScriptDate::GetCurrentDate());
|
|
|
|
int month = ScriptDate::GetMonth(ScriptDate::GetCurrentDate());
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
month += ::Subsidy::Get(subsidy_id)->remaining;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
year += (month - 1) / 12;
|
|
|
|
month = ((month - 1) % 12) + 1;
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptDate::GetDate(year, month, 1);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ CargoID ScriptSubsidy::GetCargoType(SubsidyID subsidy_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!IsValidSubsidy(subsidy_id)) return CT_INVALID;
|
|
|
|
|
2009-08-07 22:23:34 +00:00
|
|
|
return ::Subsidy::Get(subsidy_id)->cargo_type;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ ScriptSubsidy::SubsidyParticipantType ScriptSubsidy::GetSourceType(SubsidyID subsidy_id)
|
2009-08-08 16:53:22 +00:00
|
|
|
{
|
|
|
|
if (!IsValidSubsidy(subsidy_id)) return SPT_INVALID;
|
|
|
|
|
|
|
|
return (SubsidyParticipantType)(uint)::Subsidy::Get(subsidy_id)->src_type;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int32 ScriptSubsidy::GetSourceIndex(SubsidyID subsidy_id)
|
2009-08-08 16:53:22 +00:00
|
|
|
{
|
|
|
|
if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION;
|
|
|
|
|
|
|
|
return ::Subsidy::Get(subsidy_id)->src;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ ScriptSubsidy::SubsidyParticipantType ScriptSubsidy::GetDestinationType(SubsidyID subsidy_id)
|
2009-08-08 16:53:22 +00:00
|
|
|
{
|
|
|
|
if (!IsValidSubsidy(subsidy_id)) return SPT_INVALID;
|
|
|
|
|
|
|
|
return (SubsidyParticipantType)(uint)::Subsidy::Get(subsidy_id)->dst_type;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int32 ScriptSubsidy::GetDestinationIndex(SubsidyID subsidy_id)
|
2009-08-08 16:53:22 +00:00
|
|
|
{
|
|
|
|
if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION;
|
|
|
|
|
|
|
|
return ::Subsidy::Get(subsidy_id)->dst;
|
|
|
|
}
|