# ai_subsidy.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_subsidy.cpp 15299 2009-01-31 20:16:06Z smatz $ */
    00002
    00005 #include "ai_subsidy.hpp"
    00006 #include "ai_date.hpp"
    00007 #include "../../economy_func.h"
    00008 #include "../../station_base.h"
    00009 #include "../../cargotype.h"
    00010
    00011 /* static */ bool AISubsidy::IsValidSubsidy(SubsidyID subsidy_id)
    00012 {
    00013   return subsidy_id < lengthof(_subsidies) && _subsidies[subsidy_id].cargo_type != CT_INVALID;
    00014 }
    00015
    00016 /* static */ bool AISubsidy::IsAwarded(SubsidyID subsidy_id)
    00017 {
    00018   if (!IsValidSubsidy(subsidy_id)) return false;
    00019
    00020   return _subsidies[subsidy_id].age >= 12;
    00021 }
    00022
    00023 /* static */ AICompany::CompanyID AISubsidy::GetAwardedTo(SubsidyID subsidy_id)
    00024 {
    00025   if (!IsAwarded(subsidy_id)) return AICompany::COMPANY_INVALID;
    00026
    00027   return (AICompany::CompanyID)((byte)GetStation(_subsidies[subsidy_id].from)->owner);
    00028 }
    00029
    00030 /* static */ int32 AISubsidy::GetExpireDate(SubsidyID subsidy_id)
    00031 {
    00032   if (!IsValidSubsidy(subsidy_id)) return -1;
    00033
    00034   int year = AIDate::GetYear(AIDate::GetCurrentDate());
    00035   int month = AIDate::GetMonth(AIDate::GetCurrentDate());
    00036
    00037   if (IsAwarded(subsidy_id)) {
    00038     month += 24 - _subsidies[subsidy_id].age;
    00039   } else {
    00040     month += 12 - _subsidies[subsidy_id].age;
    00041   }
    00042
    00043   year += (month - 1) / 12;
    00044   month = ((month - 1) % 12) + 1;
    00045
    00046   return AIDate::GetDate(year, month, 1);
    00047 }
    00048
    00049 /* static */ CargoID AISubsidy::GetCargoType(SubsidyID subsidy_id)
    00050 {
    00051   if (!IsValidSubsidy(subsidy_id)) return CT_INVALID;
    00052
    00053   return _subsidies[subsidy_id].cargo_type;
    00054 }
    00055
    00056 /* static */ bool AISubsidy::SourceIsTown(SubsidyID subsidy_id)
    00057 {
    00058   if (!IsValidSubsidy(subsidy_id) || IsAwarded(subsidy_id)) return false;
    00059
    00060   return GetCargo(GetCargoType(subsidy_id))->town_effect == TE_PASSENGERS ||
    00061          GetCargo(GetCargoType(subsidy_id))->town_effect == TE_MAIL;
    00062 }
    00063
    00064 /* static */ int32 AISubsidy::GetSource(SubsidyID subsidy_id)
    00065 {
    00066   if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION;
    00067
    00068   return _subsidies[subsidy_id].from;
    00069 }
    00070
    00071 /* static */ bool AISubsidy::DestinationIsTown(SubsidyID subsidy_id)
    00072 {
    00073   if (!IsValidSubsidy(subsidy_id) || IsAwarded(subsidy_id)) return false;
    00074
    00075   switch (GetCargo(GetCargoType(subsidy_id))->town_effect) {
    00076     case TE_PASSENGERS:
    00077     case TE_MAIL:
    00078     case TE_GOODS:
    00079     case TE_FOOD:
    00080       return true;
    00081     default:
    00082       return false;
    00083   }
    00084 }
    00085
    00086 /* static */ int32 AISubsidy::GetDestination(SubsidyID subsidy_id)
    00087 {
    00088   if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION;
    00089
    00090   return _subsidies[subsidy_id].to;
    00091 }