9626
|
1 |
/* $Id$ */
|
|
2 |
|
|
3 |
/** @file newgrf_industries.cpp */
|
|
4 |
|
|
5 |
#include "stdafx.h"
|
|
6 |
#include "openttd.h"
|
|
7 |
#include "debug.h"
|
|
8 |
#include "functions.h"
|
|
9 |
#include "macros.h"
|
|
10 |
#include "industry.h"
|
9627
|
11 |
#include "industry_map.h"
|
9626
|
12 |
#include "newgrf.h"
|
|
13 |
#include "newgrf_callbacks.h"
|
|
14 |
#include "newgrf_spritegroup.h"
|
|
15 |
#include "newgrf_industries.h"
|
|
16 |
#include "newgrf_commons.h"
|
|
17 |
|
|
18 |
/* Since the industry IDs defined by the GRF file don't necessarily correlate
|
|
19 |
* to those used by the game, the IDs used for overriding old industries must be
|
|
20 |
* translated when the idustry spec is set. */
|
|
21 |
IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
|
|
22 |
|
|
23 |
/**
|
|
24 |
* Finds the distance for the closest tile with water/land given a tile
|
|
25 |
* @param tile the tile to find the distance too
|
|
26 |
* @param water whether to find water or land
|
|
27 |
* @note FAILS when an industry should be seen as water
|
|
28 |
*/
|
|
29 |
static uint GetClosestWaterDistance(TileIndex tile, bool water)
|
|
30 |
{
|
|
31 |
TileIndex t;
|
|
32 |
uint best_dist;
|
|
33 |
for (t = 1; t < MapSize(); t++) {
|
|
34 |
if (IsTileType(t, MP_WATER) == water) break;
|
|
35 |
}
|
|
36 |
best_dist = DistanceManhattan(tile, t);
|
|
37 |
|
|
38 |
for (; t < MapSize(); t++) {
|
|
39 |
uint dist = DistanceManhattan(tile, t);
|
|
40 |
if (dist < best_dist) {
|
|
41 |
if (IsTileType(t, MP_WATER) == water) best_dist = dist;
|
|
42 |
} else {
|
|
43 |
/* When the Y distance between the current row and the 'source' tile
|
|
44 |
* is larger than the best distance, we've found the best distance */
|
|
45 |
if (TileY(t) - TileY(tile) > best_dist) return best_dist;
|
|
46 |
if (TileX(tile) > TileX(t)) {
|
|
47 |
/* We can safely skip this many tiles; from here all tiles have a
|
|
48 |
* higher or equal distance than the best distance */
|
|
49 |
t |= MapMaxX();
|
|
50 |
continue;
|
|
51 |
} else {
|
|
52 |
/* We can safely skip this many tiles; up to here all tiles have a
|
|
53 |
* higher or equal distance than the best distance */
|
|
54 |
t += best_dist - dist;
|
|
55 |
continue;
|
|
56 |
}
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
|
60 |
return best_dist;
|
|
61 |
}
|
|
62 |
|
9627
|
63 |
/** Make an analysis of a tile and check for its belonging to the same
|
|
64 |
* industry, and/or the same grf file
|
|
65 |
* @param new_tile TileIndex of the tile to query
|
|
66 |
* @param old_tile TileINdex of teh reference tile
|
|
67 |
* @param i Industry to which old_tile belongs to
|
|
68 |
* @return value encoded as per NFO specs */
|
|
69 |
uint32 GetIndustryIDAtOffset(TileIndex new_tile, TileIndex old_tile, const Industry *i)
|
|
70 |
{
|
|
71 |
if (IsTileType(new_tile, MP_INDUSTRY)) { // Is this an industry tile?
|
|
72 |
|
|
73 |
if (GetIndustryIndex(new_tile) == i->index) { // Does it belong to the same industry?
|
|
74 |
IndustryGfx gfx = GetIndustryGfx(new_tile);
|
|
75 |
const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
|
|
76 |
const IndustryTileSpec *indold = GetIndustryTileSpec(GetIndustryGfx(old_tile));
|
|
77 |
|
|
78 |
if (gfx < NEW_INDUSTRYOFFSET) { // Does it belongs to an old type?
|
|
79 |
/* It is an old tile. We have to see if it's been overriden */
|
|
80 |
if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) { // has it been overridden?
|
|
81 |
return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
|
|
82 |
} else { // yes. FInd out if it is from the same grf file or not
|
|
83 |
const IndustryTileSpec *old_tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
|
|
84 |
|
|
85 |
if (old_tile_ovr->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {
|
|
86 |
return old_tile_ovr->grf_prop.local_id; // same grf file
|
|
87 |
} else {
|
|
88 |
return 0xFFFE; // not the same grf file
|
|
89 |
}
|
|
90 |
}
|
|
91 |
} else {
|
|
92 |
if (indtsp->grf_prop.spritegroup != NULL) { // tile has a spritegroup ?
|
|
93 |
if (indtsp->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) { // same industry, same grf ?
|
|
94 |
return indtsp->grf_prop.local_id;
|
|
95 |
} else {
|
|
96 |
return 0xFFFE; // Defined in another grf file
|
|
97 |
}
|
|
98 |
} else { // tile has no spritegroup
|
|
99 |
return 0xFF << 8 | indtsp->grf_prop.subst_id; // so just give him the substitute
|
|
100 |
}
|
|
101 |
}
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
return 0xFFFF; // tile is not an industry one or does not belong to the current industry
|
|
106 |
}
|
|
107 |
|
9626
|
108 |
/** This function implements the industries variables that newGRF defines.
|
|
109 |
* @param variable that is queried
|
|
110 |
* @param parameter unused
|
|
111 |
* @param available will return false if ever the variable asked for does not exist
|
|
112 |
* @param ind is of course the industry we are inquiring
|
|
113 |
* @return the value stored in the corresponding variable*/
|
|
114 |
uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
|
|
115 |
{
|
|
116 |
const Industry *industry = object->u.industry.ind;
|
|
117 |
TileIndex tile = object->u.industry.tile;
|
|
118 |
const IndustrySpec *indspec = GetIndustrySpec(industry->type);
|
|
119 |
|
|
120 |
switch (variable) {
|
|
121 |
case 0x40:
|
|
122 |
case 0x41:
|
|
123 |
case 0x42: { // waiting cargo, but only if those two callback flags are set
|
|
124 |
uint16 callback = indspec->callback_flags;
|
|
125 |
if (callback & (CBM_IND_PRODUCTION_CARGO_ARRIVAL | callback & CBM_IND_PRODUCTION_256_TICKS)) {
|
|
126 |
return max(industry->cargo_waiting[variable - 0x40], (uint16)0x7FFF);
|
|
127 |
} else {
|
|
128 |
return 0;
|
|
129 |
}
|
|
130 |
}
|
|
131 |
/* TODO: somehow determine whether we're in water or not */
|
|
132 |
case 0x43: return GetClosestWaterDistance(tile, true); // Manhattan distance of closes dry/water tile
|
|
133 |
|
9627
|
134 |
/* Get industry ID at offset param */
|
|
135 |
case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->xy), tile, industry);
|
9626
|
136 |
|
|
137 |
case 0x61: return 0; // Get random tile bits at offset param
|
|
138 |
|
|
139 |
case 0x62: // Land info of nearby tiles
|
|
140 |
case 0x63: // Animation stage of nerby tiles
|
|
141 |
case 0x64: break; // Distance of nearest industry of given type
|
|
142 |
/* Get town zone and Manhattan distance of closest town */
|
|
143 |
case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
|
|
144 |
/* Get square of Euclidian distance of closes town */
|
|
145 |
case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);
|
|
146 |
|
|
147 |
/* Count of industry, distance of closest instance
|
|
148 |
* format is rr(reserved) cc(count) dddd(manhattan distance of closest sister)
|
|
149 |
* A lot more should be done, since it has to check for local id, grf id etc...
|
|
150 |
* let's just say it is a beginning ;) */
|
|
151 |
case 0x67: return GetIndustryTypeCount(industry->type) << 16 | 0;
|
|
152 |
|
|
153 |
/* Industry founder information.
|
|
154 |
* 0x10 if randomly created or from a map pre-newindustry.
|
|
155 |
* Else, the company who funded it */
|
|
156 |
case 0xA7: return 0x10;
|
|
157 |
|
|
158 |
case 0xB0: // Date when built since 1920 (in days)
|
|
159 |
case 0xB3: // Construction type
|
|
160 |
case 0xB4: break; // Date last cargo accepted since 1920 (in days)
|
|
161 |
|
|
162 |
/* Industry structure access*/
|
|
163 |
case 0x80: return industry->xy;
|
|
164 |
case 0x81: return GB(industry->xy, 8, 8);
|
|
165 |
/* Pointer to the town the industry is associated with */
|
|
166 |
case 0x82:
|
|
167 |
case 0x83:
|
|
168 |
case 0x84:
|
|
169 |
case 0x85: break; // not supported
|
|
170 |
case 0x86: return industry->width;
|
|
171 |
case 0x87: return industry->height;// xy dimensions
|
|
172 |
/* */
|
|
173 |
case 0x88:
|
|
174 |
case 0x89: return indspec->produced_cargo[variable - 0x88];
|
|
175 |
case 0x8A: return industry->cargo_waiting[0];
|
|
176 |
case 0x8B: return GB(industry->cargo_waiting[0], 8, 8);
|
|
177 |
case 0x8C: return industry->cargo_waiting[1];
|
|
178 |
case 0x8D: return GB(industry->cargo_waiting[1], 8, 8);
|
|
179 |
case 0x8E:
|
|
180 |
case 0x8F: return industry->production_rate[variable - 0x8E];
|
|
181 |
case 0x90:
|
|
182 |
case 0x91:
|
|
183 |
case 0x92: return indspec->accepts_cargo[variable - 0x90];
|
|
184 |
case 0x93: return industry->prod_level;
|
|
185 |
/* amount of cargo produced so far THIS month. */
|
|
186 |
case 0x94: return industry->this_month_production[0];
|
|
187 |
case 0x95: return GB(industry->this_month_production[0], 8, 8);
|
|
188 |
case 0x96: return industry->this_month_production[1];
|
|
189 |
case 0x97: return GB(industry->this_month_production[1], 8, 8);
|
|
190 |
/* amount of cargo transported so far THIS month. */
|
|
191 |
case 0x98: return industry->this_month_transported[0];
|
|
192 |
case 0x99: return GB(industry->this_month_transported[0], 8, 8);
|
|
193 |
case 0x9A: return industry->this_month_transported[1];
|
|
194 |
case 0x9B: return GB(industry->this_month_transported[0], 8, 8);
|
|
195 |
/* fraction of cargo transported LAST month. */
|
|
196 |
case 0x9C:
|
|
197 |
case 0x9D: return industry->last_month_pct_transported[variable - 0x9C];
|
|
198 |
/* amount of cargo produced LAST month. */
|
|
199 |
case 0x9E: return industry->last_month_production[0];
|
|
200 |
case 0x9F: return GB(industry->last_month_production[0], 8, 8);
|
|
201 |
case 0xA0: return industry->last_month_production[1];
|
|
202 |
case 0xA1: return GB(industry->last_month_production[1], 8, 8);
|
|
203 |
/* amount of cargo transported last month. */
|
|
204 |
case 0xA2: return industry->last_month_transported[0];
|
|
205 |
case 0xA3: return GB(industry->last_month_transported[0], 8, 8);
|
|
206 |
case 0xA4: return industry->last_month_transported[1];
|
|
207 |
case 0xA5: return GB(industry->last_month_transported[0], 8, 8);
|
|
208 |
|
|
209 |
case 0xA6: return industry->type;
|
|
210 |
|
|
211 |
case 0xA8: return industry->random_color;
|
|
212 |
case 0xA9: return industry->last_prod_year; // capped?
|
|
213 |
case 0xAA: return industry->counter;
|
|
214 |
case 0xAB: return GB(industry->counter, 8, 8);
|
|
215 |
case 0xAC: return industry->was_cargo_delivered;
|
|
216 |
}
|
|
217 |
|
|
218 |
DEBUG(grf, 1, "Unhandled industry property 0x%X", variable);
|
|
219 |
|
|
220 |
*available = false;
|
|
221 |
return (uint32)-1;
|
|
222 |
}
|
|
223 |
|
|
224 |
static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const SpriteGroup *group)
|
|
225 |
{
|
|
226 |
/* IndustryTile do not have 'real' groups */
|
|
227 |
return NULL;
|
|
228 |
}
|
|
229 |
|
|
230 |
static void NewIndustryResolver(ResolverObject *res, IndustryType ind_id, TileIndex tile, Industry *indus)
|
|
231 |
{
|
|
232 |
res->GetRandomBits = NULL;//IndustryTileGetRandomBits;
|
|
233 |
res->GetTriggers = NULL;//IndustryTileGetTriggers;
|
|
234 |
res->SetTriggers = NULL;//IndustryTileSetTriggers;
|
|
235 |
res->GetVariable = IndustryGetVariable;
|
|
236 |
res->ResolveReal = IndustryResolveReal;
|
|
237 |
|
|
238 |
res->u.industry.tile = tile;
|
|
239 |
res->u.industry.ind = indus;
|
|
240 |
|
|
241 |
res->callback = 0;
|
|
242 |
res->callback_param1 = 0;
|
|
243 |
res->callback_param2 = 0;
|
|
244 |
res->last_value = 0;
|
|
245 |
res->trigger = 0;
|
|
246 |
res->reseed = 0;
|
|
247 |
}
|
|
248 |
|
|
249 |
uint16 GetIndustryCallback(uint16 callback, uint32 param1, uint32 param2, Industry *industry, TileIndex tile)
|
|
250 |
{
|
|
251 |
ResolverObject object;
|
|
252 |
const SpriteGroup *group;
|
|
253 |
|
|
254 |
NewIndustryResolver(&object, industry->type, tile, industry);
|
|
255 |
object.callback = callback;
|
|
256 |
object.callback_param1 = param1;
|
|
257 |
object.callback_param2 = param2;
|
|
258 |
|
|
259 |
group = Resolve(GetIndustrySpec(industry->type)->grf_prop.spritegroup, &object);
|
|
260 |
if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
|
|
261 |
|
|
262 |
return group->g.callback.result;
|
|
263 |
}
|