author | truebrain |
Wed, 26 Mar 2008 15:17:40 +0000 | |
branch | noai |
changeset 9823 | 0b7f816cf46f |
parent 9814 | be51ea0adc29 |
child 9833 | 89a64246458f |
permissions | -rw-r--r-- |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
1 |
/* $Id$ */ |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
2 |
|
9430
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
3 |
/** @file ai_industry.cpp handles the query-related of the AIIndustry class */ |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
4 |
|
9430
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
5 |
#include "ai_industry.hpp" |
9649
bc8c06513f5b
(svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents:
9497
diff
changeset
|
6 |
#include "ai_cargo.hpp" |
9814
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
7 |
#include "ai_map.hpp" |
9430
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
8 |
#include "../../industry.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9717
diff
changeset
|
9 |
#include "../../strings_func.h" |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
10 |
#include "table/strings.h" |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
11 |
|
9737
ee408edf3851
(svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents:
9736
diff
changeset
|
12 |
/* static */ IndustryID AIIndustry::GetMaxIndustryID() |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
13 |
{ |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
14 |
return ::GetMaxIndustryIndex(); |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
15 |
} |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
16 |
|
9737
ee408edf3851
(svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents:
9736
diff
changeset
|
17 |
/* static */ int32 AIIndustry::GetIndustryCount() |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
18 |
{ |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
19 |
return ::GetNumIndustries(); |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
20 |
} |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
21 |
|
9497
f6678533ccba
(svn r9369) [NoAI] -Codechange: make some IsValidXXX() function static, so they can be used by the other classes without the need for an instance.
rubidium
parents:
9459
diff
changeset
|
22 |
/* static */ bool AIIndustry::IsValidIndustry(IndustryID industry_id) |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
23 |
{ |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
24 |
return ::IsValidIndustryID(industry_id); |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
25 |
} |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
26 |
|
9737
ee408edf3851
(svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents:
9736
diff
changeset
|
27 |
/* static */ char *AIIndustry::GetName(IndustryID industry_id) |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
28 |
{ |
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9723
diff
changeset
|
29 |
if (!IsValidIndustry(industry_id)) return NULL; |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
30 |
static const int len = 64; |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
31 |
char *industry_name = MallocT<char>(len); |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
32 |
|
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9723
diff
changeset
|
33 |
::SetDParam(0, industry_id); |
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9723
diff
changeset
|
34 |
::GetString(industry_name, STR_INDUSTRY, &industry_name[len - 1]); |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
35 |
|
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
36 |
return industry_name; |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
37 |
} |
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
38 |
|
9650
7e5e1a7ecbff
(svn r10545) [NoAI] -Fix: GetProduction doesn't return a tile, but a value (tnx glx)
truelight
parents:
9649
diff
changeset
|
39 |
/* static */ int32 AIIndustry::GetProduction(IndustryID industry_id, CargoID cargo_id) |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
40 |
{ |
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9723
diff
changeset
|
41 |
if (!IsValidIndustry(industry_id)) return -1; |
9649
bc8c06513f5b
(svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents:
9497
diff
changeset
|
42 |
if (!AICargo::IsValidCargo(cargo_id)) return -1; |
bc8c06513f5b
(svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents:
9497
diff
changeset
|
43 |
|
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
44 |
const Industry *i = ::GetIndustry(industry_id); |
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9723
diff
changeset
|
45 |
const IndustrySpec *indsp = ::GetIndustrySpec(i->type); |
9649
bc8c06513f5b
(svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents:
9497
diff
changeset
|
46 |
|
9795
679ba7cd8426
(svn r12299) [NoAI] -Fix: don't assume any length of produced_cargo and accepts_cargo
truebrain
parents:
9737
diff
changeset
|
47 |
for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) |
9798
28e94a3b2b06
(svn r12302) [NoAI] -Fix: GetProducing is multiplied by 8 for the GUI, so also do it for our API, to make less confusion for people (Progman)
truebrain
parents:
9795
diff
changeset
|
48 |
if (indsp->produced_cargo[j] == cargo_id) return i->production_rate[j] * 8; |
9795
679ba7cd8426
(svn r12299) [NoAI] -Fix: don't assume any length of produced_cargo and accepts_cargo
truebrain
parents:
9737
diff
changeset
|
49 |
|
9649
bc8c06513f5b
(svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents:
9497
diff
changeset
|
50 |
return -1; |
9405
df6f3b4b0038
(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents:
diff
changeset
|
51 |
} |
9649
bc8c06513f5b
(svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents:
9497
diff
changeset
|
52 |
|
9710
ba44f8c1fd52
(svn r11294) [NoAI] -Add: added AIIndustry::IsCargoAccepted + Valuator (on request by Kilinich)
truelight
parents:
9650
diff
changeset
|
53 |
/* static */ bool AIIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id) |
ba44f8c1fd52
(svn r11294) [NoAI] -Add: added AIIndustry::IsCargoAccepted + Valuator (on request by Kilinich)
truelight
parents:
9650
diff
changeset
|
54 |
{ |
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9723
diff
changeset
|
55 |
if (!IsValidIndustry(industry_id)) return false; |
9710
ba44f8c1fd52
(svn r11294) [NoAI] -Add: added AIIndustry::IsCargoAccepted + Valuator (on request by Kilinich)
truelight
parents:
9650
diff
changeset
|
56 |
if (!AICargo::IsValidCargo(cargo_id)) return false; |
ba44f8c1fd52
(svn r11294) [NoAI] -Add: added AIIndustry::IsCargoAccepted + Valuator (on request by Kilinich)
truelight
parents:
9650
diff
changeset
|
57 |
|
ba44f8c1fd52
(svn r11294) [NoAI] -Add: added AIIndustry::IsCargoAccepted + Valuator (on request by Kilinich)
truelight
parents:
9650
diff
changeset
|
58 |
const Industry *i = ::GetIndustry(industry_id); |
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9723
diff
changeset
|
59 |
const IndustrySpec *indsp = ::GetIndustrySpec(i->type); |
9710
ba44f8c1fd52
(svn r11294) [NoAI] -Add: added AIIndustry::IsCargoAccepted + Valuator (on request by Kilinich)
truelight
parents:
9650
diff
changeset
|
60 |
|
9795
679ba7cd8426
(svn r12299) [NoAI] -Fix: don't assume any length of produced_cargo and accepts_cargo
truebrain
parents:
9737
diff
changeset
|
61 |
for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) |
679ba7cd8426
(svn r12299) [NoAI] -Fix: don't assume any length of produced_cargo and accepts_cargo
truebrain
parents:
9737
diff
changeset
|
62 |
if (indsp->accepts_cargo[j] == cargo_id) return true; |
9710
ba44f8c1fd52
(svn r11294) [NoAI] -Add: added AIIndustry::IsCargoAccepted + Valuator (on request by Kilinich)
truelight
parents:
9650
diff
changeset
|
63 |
|
ba44f8c1fd52
(svn r11294) [NoAI] -Add: added AIIndustry::IsCargoAccepted + Valuator (on request by Kilinich)
truelight
parents:
9650
diff
changeset
|
64 |
return false; |
ba44f8c1fd52
(svn r11294) [NoAI] -Add: added AIIndustry::IsCargoAccepted + Valuator (on request by Kilinich)
truelight
parents:
9650
diff
changeset
|
65 |
} |
ba44f8c1fd52
(svn r11294) [NoAI] -Add: added AIIndustry::IsCargoAccepted + Valuator (on request by Kilinich)
truelight
parents:
9650
diff
changeset
|
66 |
|
9649
bc8c06513f5b
(svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents:
9497
diff
changeset
|
67 |
/* static */ TileIndex AIIndustry::GetLocation(IndustryID industry_id) |
bc8c06513f5b
(svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents:
9497
diff
changeset
|
68 |
{ |
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9723
diff
changeset
|
69 |
if (!IsValidIndustry(industry_id)) return INVALID_TILE; |
9649
bc8c06513f5b
(svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents:
9497
diff
changeset
|
70 |
return ::GetIndustry(industry_id)->xy; |
bc8c06513f5b
(svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents:
9497
diff
changeset
|
71 |
} |
9810
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
72 |
|
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
73 |
/* static */ uint16 AIIndustry::GetLastMonthProduction(IndustryID industry_id, CargoID cargo_id) |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
74 |
{ |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
75 |
if (!IsValidIndustry(industry_id)) return false; |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
76 |
if (!AICargo::IsValidCargo(cargo_id)) return false; |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
77 |
|
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
78 |
const Industry *i = ::GetIndustry(industry_id); |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
79 |
const IndustrySpec *indsp = ::GetIndustrySpec(i->type); |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
80 |
|
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
81 |
for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
82 |
if (indsp->produced_cargo[j] == cargo_id) return i->last_month_production[j]; |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
83 |
|
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
84 |
return false; |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
85 |
} |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
86 |
|
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
87 |
/* static */ uint16 AIIndustry::GetLastMonthTransported(IndustryID industry_id, CargoID cargo_id) |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
88 |
{ |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
89 |
if (!IsValidIndustry(industry_id)) return false; |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
90 |
if (!AICargo::IsValidCargo(cargo_id)) return false; |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
91 |
|
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
92 |
const Industry *i = ::GetIndustry(industry_id); |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
93 |
const IndustrySpec *indsp = ::GetIndustrySpec(i->type); |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
94 |
|
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
95 |
for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
96 |
if (indsp->produced_cargo[j] == cargo_id) return i->last_month_transported[j]; |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
97 |
|
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
98 |
return false; |
a84fc1be9e3b
(svn r12327) [NoAI] -Add: GetLastMonth(Production|Transported) (Morloth)
truebrain
parents:
9798
diff
changeset
|
99 |
} |
9814
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
100 |
|
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
101 |
/* static */ int32 AIIndustry::GetDistanceManhattanToTile(IndustryID industry_id, TileIndex tile) |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
102 |
{ |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
103 |
return AIMap::DistanceManhattan(tile, GetLocation(industry_id)); |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
104 |
} |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
105 |
|
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
106 |
/* static */ int32 AIIndustry::GetDistanceSquareToTile(IndustryID industry_id, TileIndex tile) |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
107 |
{ |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
108 |
return AIMap::DistanceSquare(tile, GetLocation(industry_id)); |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9810
diff
changeset
|
109 |
} |