author | glx |
Mon, 21 Apr 2008 21:15:50 +0000 | |
branch | noai |
changeset 10294 | 7798ae816af8 |
parent 9826 | 9707ad4c9b60 |
child 10429 | 1b99254f9607 |
permissions | -rw-r--r-- |
9732 | 1 |
/* $Id$ */ |
2 |
||
3 |
#include "stdafx.h" |
|
4 |
#include "openttd.h" |
|
5 |
#include "variables.h" |
|
6 |
#include "landscape.h" |
|
7 |
#include "debug.h" |
|
8 |
#include "newgrf.h" |
|
9 |
#include "newgrf_callbacks.h" |
|
10 |
#include "newgrf_commons.h" |
|
11 |
#include "newgrf_spritegroup.h" |
|
12 |
#include "newgrf_generic.h" |
|
13 |
#include "tile_map.h" |
|
14 |
#include <list> |
|
15 |
||
16 |
||
17 |
struct GenericCallback { |
|
18 |
const GRFFile *file; |
|
19 |
const SpriteGroup *group; |
|
20 |
||
21 |
GenericCallback(const GRFFile *file, const SpriteGroup *group) : |
|
22 |
file(file), |
|
23 |
group(group) |
|
24 |
{ } |
|
25 |
}; |
|
26 |
||
27 |
typedef std::list<GenericCallback> GenericCallbackList; |
|
28 |
||
29 |
static GenericCallbackList _gcl[GSF_END]; |
|
30 |
||
31 |
||
32 |
/** |
|
33 |
* Reset all generic feature callback sprite groups. |
|
34 |
*/ |
|
35 |
void ResetGenericCallbacks() |
|
36 |
{ |
|
37 |
for (uint8 feature = 0; feature < lengthof(_gcl); feature++) { |
|
38 |
_gcl[feature].clear(); |
|
39 |
} |
|
40 |
} |
|
41 |
||
42 |
||
43 |
/** |
|
44 |
* Add a generic feature callback sprite group to the appropriate feature list. |
|
45 |
* @param feature |
|
46 |
* @param file |
|
47 |
* @param group |
|
48 |
*/ |
|
49 |
void AddGenericCallback(uint8 feature, const GRFFile *file, const SpriteGroup *group) |
|
50 |
{ |
|
51 |
if (feature >= lengthof(_gcl)) { |
|
52 |
grfmsg(5, "AddGenericCallback: Unsupported feature 0x%02X", feature); |
|
53 |
return; |
|
54 |
} |
|
55 |
||
56 |
/* Generic feature callbacks are evaluated in reverse (i.e. the last group |
|
57 |
* to be added is evaluated first, etc) thus we push the group to the |
|
58 |
* beginning of the list so a standard iterator will do the right thing. */ |
|
59 |
_gcl[feature].push_front(GenericCallback(file, group)); |
|
60 |
} |
|
61 |
||
62 |
||
63 |
static uint32 GenericCallbackGetRandomBits(const ResolverObject *object) |
|
64 |
{ |
|
65 |
return 0; |
|
66 |
} |
|
67 |
||
68 |
||
69 |
static uint32 GenericCallbackGetTriggers(const ResolverObject *object) |
|
70 |
{ |
|
71 |
return 0; |
|
72 |
} |
|
73 |
||
74 |
||
75 |
static void GenericCallbackSetTriggers(const ResolverObject *object, int triggers) |
|
76 |
{ |
|
77 |
return; |
|
78 |
} |
|
79 |
||
80 |
||
81 |
static uint32 GenericCallbackGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) |
|
82 |
{ |
|
83 |
switch (variable) { |
|
84 |
case 0x40: return object->u.generic.cargo_type; |
|
85 |
||
86 |
case 0x80: return object->u.generic.cargo_type; |
|
87 |
case 0x81: return object->u.generic.cargo_type; |
|
88 |
case 0x82: return object->u.generic.default_selection; |
|
89 |
case 0x83: return object->u.generic.src_industry; |
|
90 |
case 0x84: return object->u.generic.dst_industry; |
|
91 |
case 0x85: return object->u.generic.distance; |
|
92 |
case 0x86: return object->u.generic.event; |
|
93 |
case 0x87: return object->u.generic.count; |
|
94 |
case 0x88: return object->u.generic.station_size; |
|
95 |
||
96 |
default: break; |
|
97 |
} |
|
98 |
||
99 |
DEBUG(grf, 1, "Unhandled generic feature property 0x%02X", variable); |
|
100 |
||
101 |
*available = false; |
|
102 |
return 0; |
|
103 |
} |
|
104 |
||
105 |
||
106 |
static const SpriteGroup *GenericCallbackResolveReal(const ResolverObject *object, const SpriteGroup *group) |
|
107 |
{ |
|
108 |
if (group->g.real.num_loaded == 0) return NULL; |
|
109 |
||
110 |
return group->g.real.loaded[0]; |
|
111 |
} |
|
112 |
||
113 |
||
114 |
static inline void NewGenericResolver(ResolverObject *res) |
|
115 |
{ |
|
116 |
res->GetRandomBits = &GenericCallbackGetRandomBits; |
|
117 |
res->GetTriggers = &GenericCallbackGetTriggers; |
|
118 |
res->SetTriggers = &GenericCallbackSetTriggers; |
|
119 |
res->GetVariable = &GenericCallbackGetVariable; |
|
120 |
res->ResolveReal = &GenericCallbackResolveReal; |
|
121 |
||
122 |
res->callback = CBID_NO_CALLBACK; |
|
123 |
res->callback_param1 = 0; |
|
124 |
res->callback_param2 = 0; |
|
125 |
res->last_value = 0; |
|
126 |
res->trigger = 0; |
|
127 |
res->reseed = 0; |
|
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
128 |
res->count = 0; |
9732 | 129 |
} |
130 |
||
131 |
||
132 |
/** Follow a generic feature callback list and return the first successful |
|
133 |
* answer |
|
134 |
* @param feature GRF Feature of callback |
|
135 |
* @param object pre-populated resolver object |
|
136 |
* @param file address of GRFFile object if file reference is needed, NULL is valid |
|
137 |
* @return callback value if successful or CALLBACK_FAILED |
|
138 |
*/ |
|
139 |
static uint16 GetGenericCallbackResult(uint8 feature, ResolverObject *object, const GRFFile **file) |
|
140 |
{ |
|
141 |
assert(feature < lengthof(_gcl)); |
|
142 |
||
143 |
/* Test each feature callback sprite group. */ |
|
144 |
for (GenericCallbackList::const_iterator it = _gcl[feature].begin(); it != _gcl[feature].end(); ++it) { |
|
145 |
const SpriteGroup *group = it->group; |
|
146 |
group = Resolve(group, object); |
|
147 |
if (group == NULL || group->type != SGT_CALLBACK) continue; |
|
148 |
||
149 |
/* Return NewGRF file if necessary */ |
|
150 |
if (file != NULL) *file = it->file; |
|
151 |
||
152 |
return group->g.callback.result; |
|
153 |
} |
|
154 |
||
155 |
/* No callback returned a valid result, so we've failed. */ |
|
156 |
return CALLBACK_FAILED; |
|
157 |
} |
|
158 |
||
159 |
||
160 |
/** |
|
161 |
* 'Execute' an AI purchase selection callback |
|
162 |
*/ |
|
163 |
uint16 GetAiPurchaseCallbackResult(uint8 feature, CargoID cargo_type, uint8 default_selection, IndustryType src_industry, IndustryType dst_industry, uint8 distance, AIConstructionEvent event, uint8 count, uint8 station_size, const GRFFile **file) |
|
164 |
{ |
|
165 |
ResolverObject object; |
|
166 |
||
167 |
NewGenericResolver(&object); |
|
168 |
||
169 |
object.callback = CBID_GENERIC_AI_PURCHASE_SELECTION; |
|
170 |
object.u.generic.cargo_type = cargo_type; |
|
171 |
object.u.generic.default_selection = default_selection; |
|
172 |
object.u.generic.src_industry = src_industry; |
|
173 |
object.u.generic.dst_industry = dst_industry; |
|
174 |
object.u.generic.distance = distance; |
|
175 |
object.u.generic.event = event; |
|
176 |
object.u.generic.count = count; |
|
177 |
object.u.generic.station_size = station_size; |
|
178 |
||
10294 | 179 |
uint16 callback = GetGenericCallbackResult(feature, &object, file); |
180 |
if (callback != CALLBACK_FAILED) callback = GB(callback, 0, 8); |
|
181 |
return callback; |
|
9732 | 182 |
} |