|
1 /* $Id$ */ |
|
2 |
|
3 /** @file ai_subsidy.cpp Implementation of AISubsidy. */ |
|
4 |
|
5 #include "ai_subsidy.hpp" |
|
6 #include "ai_error.hpp" |
|
7 #include "ai_company.hpp" |
|
8 #include "ai_date.hpp" |
|
9 #include "../../openttd.h" |
|
10 #include "../../economy_func.h" |
|
11 #include "../../station_base.h" |
|
12 #include "../../cargotype.h" |
|
13 |
|
14 /* static */ bool AISubsidy::IsValidSubsidy(SubsidyID subsidy_id) |
|
15 { |
|
16 return subsidy_id < lengthof(_subsidies) && _subsidies[subsidy_id].cargo_type != CT_INVALID; |
|
17 } |
|
18 |
|
19 /* static */ bool AISubsidy::IsAwarded(SubsidyID subsidy_id) |
|
20 { |
|
21 if (!IsValidSubsidy(subsidy_id)) return false; |
|
22 |
|
23 return _subsidies[subsidy_id].age >= 12; |
|
24 } |
|
25 |
|
26 /* static */ AICompany::CompanyIndex AISubsidy::GetAwardedTo(SubsidyID subsidy_id) |
|
27 { |
|
28 if (!IsAwarded(subsidy_id)) return AICompany::INVALID_COMPANY; |
|
29 |
|
30 return (AICompany::CompanyIndex)((byte)GetStation(_subsidies[subsidy_id].from)->owner); |
|
31 } |
|
32 |
|
33 /* static */ int32 AISubsidy::GetExpireDate(SubsidyID subsidy_id) |
|
34 { |
|
35 if (!IsValidSubsidy(subsidy_id)) return -1; |
|
36 |
|
37 int year = AIDate::GetYear(AIDate::GetCurrentDate()); |
|
38 int month = AIDate::GetMonth(AIDate::GetCurrentDate()); |
|
39 |
|
40 if (IsAwarded(subsidy_id)) { |
|
41 month += 24 - _subsidies[subsidy_id].age; |
|
42 } else { |
|
43 month += 12 - _subsidies[subsidy_id].age; |
|
44 } |
|
45 |
|
46 year += month / 12; |
|
47 month = month % 12; |
|
48 |
|
49 return AIDate::GetDate(year, month, 1); |
|
50 } |
|
51 |
|
52 /* static */ CargoID AISubsidy::GetCargoType(SubsidyID subsidy_id) |
|
53 { |
|
54 if (!IsValidSubsidy(subsidy_id)) return CT_INVALID; |
|
55 |
|
56 return _subsidies[subsidy_id].cargo_type; |
|
57 } |
|
58 |
|
59 /* static */ bool AISubsidy::SourceIsTown(SubsidyID subsidy_id) |
|
60 { |
|
61 if (!IsValidSubsidy(subsidy_id) || IsAwarded(subsidy_id)) return false; |
|
62 |
|
63 return GetCargo(GetCargoType(subsidy_id))->town_effect == TE_PASSENGERS || |
|
64 GetCargo(GetCargoType(subsidy_id))->town_effect == TE_MAIL; |
|
65 } |
|
66 |
|
67 /* static */ int32 AISubsidy::GetSource(SubsidyID subsidy_id) |
|
68 { |
|
69 if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION; |
|
70 |
|
71 return _subsidies[subsidy_id].from; |
|
72 } |
|
73 |
|
74 /* static */ bool AISubsidy::DestinationIsTown(SubsidyID subsidy_id) |
|
75 { |
|
76 if (!IsValidSubsidy(subsidy_id) || IsAwarded(subsidy_id)) return false; |
|
77 |
|
78 switch (GetCargo(GetCargoType(subsidy_id))->town_effect) { |
|
79 case TE_PASSENGERS: |
|
80 case TE_MAIL: |
|
81 case TE_GOODS: |
|
82 case TE_FOOD: |
|
83 return true; |
|
84 default: |
|
85 return false; |
|
86 } |
|
87 } |
|
88 |
|
89 /* static */ int32 AISubsidy::GetDestination(SubsidyID subsidy_id) |
|
90 { |
|
91 if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION; |
|
92 |
|
93 return _subsidies[subsidy_id].to; |
|
94 } |