0
|
1 |
#include "stdafx.h"
|
|
2 |
#include "ttd.h"
|
|
3 |
#include "viewport.h"
|
|
4 |
#include "command.h"
|
|
5 |
#include "industry.h"
|
|
6 |
#include "town.h"
|
|
7 |
#include "vehicle.h"
|
|
8 |
#include "news.h"
|
|
9 |
#include "saveload.h"
|
|
10 |
#include "economy.h"
|
|
11 |
|
|
12 |
byte _industry_sound_ctr;
|
|
13 |
TileIndex _industry_sound_tile;
|
|
14 |
|
|
15 |
void ShowIndustryViewWindow(int industry);
|
|
16 |
void BuildOilRig(uint tile);
|
|
17 |
void DeleteOilRig(uint tile);
|
|
18 |
|
|
19 |
typedef struct DrawIndustryTileStruct {
|
|
20 |
uint32 sprite_1;
|
|
21 |
uint32 sprite_2;
|
|
22 |
|
|
23 |
byte subtile_xy;
|
|
24 |
byte width_height;
|
|
25 |
byte dz;
|
|
26 |
byte proc;
|
|
27 |
} DrawIndustryTileStruct;
|
|
28 |
|
|
29 |
|
|
30 |
typedef struct DrawIndustrySpec1Struct {
|
|
31 |
byte x;
|
|
32 |
byte image_1;
|
|
33 |
byte image_2;
|
|
34 |
byte image_3;
|
|
35 |
} DrawIndustrySpec1Struct;
|
|
36 |
|
|
37 |
typedef struct DrawIndustrySpec4Struct {
|
|
38 |
byte image_1;
|
|
39 |
byte image_2;
|
|
40 |
byte image_3;
|
|
41 |
} DrawIndustrySpec4Struct;
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
#include "table/industry_land.h"
|
|
46 |
|
|
47 |
|
|
48 |
typedef struct IndustryTileTable {
|
|
49 |
TileIndexDiff ti;
|
|
50 |
byte map5;
|
|
51 |
} IndustryTileTable;
|
|
52 |
|
|
53 |
typedef struct IndustrySpec {
|
|
54 |
const IndustryTileTable * const * table;
|
|
55 |
byte num_table;
|
|
56 |
byte a,b,c;
|
|
57 |
byte produced_cargo[2];
|
|
58 |
byte production_rate[2];
|
|
59 |
byte accepts_cargo[3];
|
|
60 |
byte check_proc;
|
|
61 |
} IndustrySpec;
|
|
62 |
|
|
63 |
#include "table/build_industry.h"
|
|
64 |
|
|
65 |
static const byte _industry_close_mode[37] = {
|
|
66 |
1,0,2,1,2,1,2,2,2,1,1,1,0,2,2,1,0,1,1,1,1,1,0,2,1,2,1,2,1,1,0,2,1,2,1,1,1,
|
|
67 |
};
|
|
68 |
|
|
69 |
static const StringID _industry_prod_up_strings[] = {
|
|
70 |
STR_4836_NEW_COAL_SEAM_FOUND_AT,
|
|
71 |
STR_4835_INCREASES_PRODUCTION,
|
|
72 |
STR_4835_INCREASES_PRODUCTION,
|
|
73 |
STR_4835_INCREASES_PRODUCTION,
|
|
74 |
STR_4835_INCREASES_PRODUCTION,
|
|
75 |
STR_4837_NEW_OIL_RESERVES_FOUND,
|
|
76 |
STR_4835_INCREASES_PRODUCTION,
|
|
77 |
STR_4835_INCREASES_PRODUCTION,
|
|
78 |
STR_4835_INCREASES_PRODUCTION,
|
|
79 |
STR_4838_IMPROVED_FARMING_METHODS,
|
|
80 |
STR_4835_INCREASES_PRODUCTION,
|
|
81 |
STR_4837_NEW_OIL_RESERVES_FOUND,
|
|
82 |
STR_4835_INCREASES_PRODUCTION,
|
|
83 |
STR_4835_INCREASES_PRODUCTION,
|
|
84 |
STR_4835_INCREASES_PRODUCTION,
|
|
85 |
STR_4835_INCREASES_PRODUCTION,
|
|
86 |
STR_4835_INCREASES_PRODUCTION,
|
|
87 |
STR_4835_INCREASES_PRODUCTION,
|
|
88 |
STR_4835_INCREASES_PRODUCTION,
|
|
89 |
STR_4838_IMPROVED_FARMING_METHODS,
|
|
90 |
STR_4838_IMPROVED_FARMING_METHODS,
|
|
91 |
STR_4835_INCREASES_PRODUCTION,
|
|
92 |
STR_4835_INCREASES_PRODUCTION,
|
|
93 |
STR_4835_INCREASES_PRODUCTION,
|
|
94 |
STR_4838_IMPROVED_FARMING_METHODS,
|
|
95 |
STR_4835_INCREASES_PRODUCTION,
|
|
96 |
STR_4838_IMPROVED_FARMING_METHODS,
|
|
97 |
STR_4835_INCREASES_PRODUCTION,
|
|
98 |
STR_4838_IMPROVED_FARMING_METHODS,
|
|
99 |
STR_4835_INCREASES_PRODUCTION,
|
|
100 |
STR_4835_INCREASES_PRODUCTION,
|
|
101 |
STR_4835_INCREASES_PRODUCTION,
|
|
102 |
STR_4835_INCREASES_PRODUCTION,
|
|
103 |
STR_4835_INCREASES_PRODUCTION,
|
|
104 |
STR_4835_INCREASES_PRODUCTION,
|
|
105 |
STR_4835_INCREASES_PRODUCTION,
|
|
106 |
STR_4835_INCREASES_PRODUCTION,
|
|
107 |
};
|
|
108 |
|
|
109 |
static const StringID _industry_prod_down_strings[] = {
|
|
110 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
111 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
112 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
113 |
STR_483A_INSECT_INFESTATION_CAUSES,
|
|
114 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
115 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
116 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
117 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
118 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
119 |
STR_483A_INSECT_INFESTATION_CAUSES,
|
|
120 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
121 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
122 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
123 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
124 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
125 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
126 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
127 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
128 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
129 |
STR_483A_INSECT_INFESTATION_CAUSES,
|
|
130 |
STR_483A_INSECT_INFESTATION_CAUSES,
|
|
131 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
132 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
133 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
134 |
STR_483A_INSECT_INFESTATION_CAUSES,
|
|
135 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
136 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
137 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
138 |
STR_483A_INSECT_INFESTATION_CAUSES,
|
|
139 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
140 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
141 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
142 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
143 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
144 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
145 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
146 |
STR_4839_PRODUCTION_DOWN_BY_50,
|
|
147 |
};
|
|
148 |
|
|
149 |
static const StringID _industry_close_strings[] = {
|
|
150 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
151 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
152 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
153 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
154 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
155 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
156 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
157 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
158 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
159 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
160 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
161 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
162 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
163 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
164 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
165 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
166 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
167 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
168 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
169 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
170 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
171 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
172 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
173 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
174 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
175 |
STR_4834_LACK_OF_NEARBY_TREES_CAUSES,
|
|
176 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
177 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
178 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
179 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
180 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
181 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
182 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
183 |
STR_4833_SUPPLY_PROBLEMS_CAUSE_TO,
|
|
184 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
185 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE,
|
|
186 |
STR_4832_ANNOUNCES_IMMINENT_CLOSURE ,
|
|
187 |
};
|
|
188 |
|
|
189 |
|
|
190 |
static void IndustryDrawTileProc1(TileInfo *ti)
|
|
191 |
{
|
|
192 |
const DrawIndustrySpec1Struct *d;
|
|
193 |
uint32 image;
|
|
194 |
|
|
195 |
if (!(_map_owner[ti->tile] & 0x80))
|
|
196 |
return;
|
|
197 |
|
|
198 |
d = &_draw_industry_spec1[_map3_lo[ti->tile]];
|
|
199 |
|
|
200 |
AddChildSpriteScreen(0x12A7 + d->image_1, d->x, 0);
|
|
201 |
|
|
202 |
if ( (image = d->image_2) != 0)
|
|
203 |
AddChildSpriteScreen(0x12B0 + image - 1, 8, 41);
|
|
204 |
|
|
205 |
if ( (image = d->image_3) != 0)
|
|
206 |
AddChildSpriteScreen(0x12AC + image - 1,
|
|
207 |
_drawtile_proc1_x[image-1], _drawtile_proc1_y[image-1]);
|
|
208 |
}
|
|
209 |
|
|
210 |
static void IndustryDrawTileProc2(TileInfo *ti)
|
|
211 |
{
|
|
212 |
int x = 0;
|
|
213 |
|
|
214 |
if (_map_owner[ti->tile] & 0x80) {
|
|
215 |
x = _industry_anim_offs[_map3_lo[ti->tile]];
|
|
216 |
if ( (byte)x == 0xFF)
|
|
217 |
x = 0;
|
|
218 |
}
|
|
219 |
|
|
220 |
AddChildSpriteScreen(0x129F, 22-x, x+24);
|
|
221 |
AddChildSpriteScreen(0x129E, 6, 0xE);
|
|
222 |
}
|
|
223 |
|
|
224 |
static void IndustryDrawTileProc3(TileInfo *ti)
|
|
225 |
{
|
|
226 |
if (_map_owner[ti->tile] & 0x80) {
|
|
227 |
AddChildSpriteScreen(0x128B, 5,
|
|
228 |
_industry_anim_offs_2[_map3_lo[ti->tile]]);
|
|
229 |
}
|
|
230 |
AddChildSpriteScreen(0x30B928A, 3, 67);
|
|
231 |
}
|
|
232 |
|
|
233 |
static void IndustryDrawTileProc4(TileInfo *ti)
|
|
234 |
{
|
|
235 |
const DrawIndustrySpec4Struct *d;
|
|
236 |
|
|
237 |
d = &_industry_anim_offs_3[_map3_lo[ti->tile]];
|
|
238 |
|
|
239 |
if (d->image_1 != 0xFF) {
|
|
240 |
AddChildSpriteScreen(0x126F, 0x32 - d->image_1 * 2, 0x60 + d->image_1);
|
|
241 |
}
|
|
242 |
|
|
243 |
if (d->image_2 != 0xFF) {
|
|
244 |
AddChildSpriteScreen(0x1270, 0x10 - d->image_2*2, 100 + d->image_2);
|
|
245 |
}
|
|
246 |
|
|
247 |
AddChildSpriteScreen(0x126E, 7, d->image_3);
|
|
248 |
AddChildSpriteScreen(0x126D, 0, 42);
|
|
249 |
}
|
|
250 |
|
|
251 |
static void DrawCoalPlantSparkles(TileInfo *ti)
|
|
252 |
{
|
|
253 |
int image = _map_owner[ti->tile];
|
|
254 |
if (image & 0x80) {
|
|
255 |
image = (image >> 2) & 0x1F;
|
|
256 |
if (image != 0 && image < 7) {
|
|
257 |
AddChildSpriteScreen(image + 0x806,
|
|
258 |
_coal_plant_sparkles_x[image-1],
|
|
259 |
_coal_plant_sparkles_y[image-1]
|
|
260 |
);
|
|
261 |
}
|
|
262 |
}
|
|
263 |
}
|
|
264 |
|
|
265 |
typedef void IndustryDrawTileProc(TileInfo *ti);
|
|
266 |
static IndustryDrawTileProc * const _industry_draw_tile_procs[5] = {
|
|
267 |
IndustryDrawTileProc1,
|
|
268 |
IndustryDrawTileProc2,
|
|
269 |
IndustryDrawTileProc3,
|
|
270 |
IndustryDrawTileProc4,
|
|
271 |
DrawCoalPlantSparkles,
|
|
272 |
};
|
|
273 |
|
|
274 |
static void DrawTile_Industry(TileInfo *ti)
|
|
275 |
{
|
|
276 |
Industry *ind;
|
|
277 |
const DrawIndustryTileStruct *dits;
|
|
278 |
byte z;
|
|
279 |
uint32 image, ormod;
|
|
280 |
|
|
281 |
/* Pointer to industry */
|
|
282 |
ind = DEREF_INDUSTRY(_map2[ti->tile]);
|
|
283 |
ormod = (ind->color_map+0x307)<<16;
|
|
284 |
|
|
285 |
/* Retrieve pointer to the draw industry tile struct */
|
|
286 |
dits = &_industry_draw_tile_data[(ti->map5<<2) | (_map_owner[ti->tile]&3)];
|
|
287 |
|
|
288 |
image = dits->sprite_1;
|
|
289 |
if (image&0x8000 && (image & 0xFFFF0000) == 0)
|
|
290 |
image |= ormod;
|
|
291 |
|
|
292 |
z = ti->z;
|
|
293 |
/* Add bricks below the industry? */
|
|
294 |
if (ti->tileh & 0xF) {
|
|
295 |
AddSortableSpriteToDraw((ti->tileh & 0xF) + 0x3DD, ti->x, ti->y, 16, 16, 7, z);
|
|
296 |
AddChildSpriteScreen(image, 0x1F, 1);
|
|
297 |
z += 8;
|
|
298 |
} else {
|
|
299 |
/* Else draw regular ground */
|
|
300 |
DrawGroundSprite(image);
|
|
301 |
}
|
|
302 |
|
|
303 |
/* Add industry on top of the ground? */
|
|
304 |
if ((image = dits->sprite_2) != 0) {
|
|
305 |
|
|
306 |
if (image&0x8000 && (image & 0xFFFF0000) == 0)
|
|
307 |
image |= ormod;
|
|
308 |
|
|
309 |
if (!(_display_opt & DO_TRANS_BUILDINGS))
|
|
310 |
image = (image & 0x3FFF) | 0x3224000;
|
|
311 |
|
|
312 |
AddSortableSpriteToDraw(image,
|
|
313 |
ti->x | (dits->subtile_xy>>4),
|
|
314 |
ti->y | (dits->subtile_xy&0xF),
|
|
315 |
(dits->width_height>>4)+1,
|
|
316 |
(dits->width_height&0xF)+1,
|
|
317 |
dits->dz,
|
|
318 |
z);
|
|
319 |
|
|
320 |
if (!(_display_opt & DO_TRANS_BUILDINGS))
|
|
321 |
return;
|
|
322 |
}
|
|
323 |
|
|
324 |
/* TTDBUG: strange code here, return if AddSortableSpriteToDraw failed? */
|
|
325 |
{
|
|
326 |
int proc;
|
|
327 |
if ((proc=dits->proc-1) >= 0 )
|
|
328 |
_industry_draw_tile_procs[proc](ti);
|
|
329 |
}
|
|
330 |
}
|
|
331 |
|
|
332 |
|
|
333 |
static uint GetSlopeZ_Industry(TileInfo *ti) {
|
|
334 |
return GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + ti->z;
|
|
335 |
}
|
|
336 |
|
|
337 |
static void GetAcceptedCargo_Industry(uint tile, AcceptedCargo *ac)
|
|
338 |
{
|
|
339 |
int m5 = _map5[tile];
|
|
340 |
int a;
|
|
341 |
|
|
342 |
a = _industry_map5_accepts_1[m5];
|
|
343 |
if (a >= 0) {
|
|
344 |
ac->type_1 = a;
|
|
345 |
ac->amount_1 = (a == 0) ? 1 : 8;
|
|
346 |
}
|
|
347 |
|
|
348 |
a = _industry_map5_accepts_2[m5];
|
|
349 |
if (a >= 0) {
|
|
350 |
ac->type_2 = a;
|
|
351 |
ac->amount_2 = 8;
|
|
352 |
}
|
|
353 |
|
|
354 |
a = _industry_map5_accepts_3[m5];
|
|
355 |
if (a >= 0) {
|
|
356 |
ac->type_3 = a;
|
|
357 |
ac->amount_3 = 8;
|
|
358 |
}
|
|
359 |
}
|
|
360 |
|
|
361 |
static void GetTileDesc_Industry(uint tile, TileDesc *td)
|
|
362 |
{
|
|
363 |
Industry *i = DEREF_INDUSTRY(_map2[tile]);
|
|
364 |
|
|
365 |
td->owner = i->owner;
|
|
366 |
td->str = STR_4802_COAL_MINE + i->type;
|
|
367 |
if ((_map_owner[tile] & 0x80) == 0) {
|
|
368 |
SET_DPARAMX16(td->dparam, 0, td->str);
|
|
369 |
td->str = STR_2058_UNDER_CONSTRUCTION;
|
|
370 |
}
|
|
371 |
}
|
|
372 |
|
|
373 |
static int32 ClearTile_Industry(uint tile, byte flags)
|
|
374 |
{
|
|
375 |
Industry *i = DEREF_INDUSTRY(_map2[tile]);
|
|
376 |
|
|
377 |
if ((_current_player == OWNER_WATER || _game_mode != GM_EDITOR) && !_cheats.magic_bulldozer.value) {
|
|
378 |
SET_DPARAM16(0, STR_4802_COAL_MINE + i->type);
|
|
379 |
return_cmd_error(STR_4800_IN_THE_WAY);
|
|
380 |
}
|
|
381 |
|
|
382 |
if (flags & DC_EXEC) {
|
|
383 |
DeleteIndustry(i);
|
|
384 |
}
|
|
385 |
return 0;
|
|
386 |
}
|
|
387 |
|
|
388 |
/* p1 index of industry to destroy */
|
|
389 |
/* Destroy Industry button costing money removed per request of dominik
|
|
390 |
int32 CmdDestroyIndustry(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
391 |
{
|
|
392 |
uint tile = TILE_FROM_XY(x,y);
|
|
393 |
Industry *i = DEREF_INDUSTRY((uint16)p1);
|
|
394 |
Town *t = ClosestTownFromTile(tile, (uint)-1); // find closest town to penaltize (ALWAYS penaltize)
|
|
395 |
|
|
396 |
// check if you're allowed to remove the industry. Minimum amount
|
|
397 |
// of ratings to remove the industry depends on difficulty setting
|
|
398 |
if (!CheckforTownRating(tile, flags, t, INDUSTRY_REMOVE))
|
|
399 |
return CMD_ERROR;
|
|
400 |
|
|
401 |
if (flags & DC_EXEC) {
|
|
402 |
DeleteIndustry(i);
|
|
403 |
CreateEffectVehicleAbove(x + 8,y + 8, 2, EV_DEMOLISH);
|
|
404 |
ChangeTownRating(t, -1500, -1000); // penalty is 1500
|
|
405 |
}
|
|
406 |
|
|
407 |
return (_price.build_industry >> 5) * _industry_type_costs[i->type]*2;
|
|
408 |
}
|
|
409 |
*/
|
|
410 |
|
|
411 |
static const byte _industry_min_cargo[] = {
|
|
412 |
5, 5, 5, 30, 5, 5, 5, 5,
|
|
413 |
5, 5, 5, 5, 2, 5, 5, 5,
|
|
414 |
5, 5, 5, 15, 15, 5, 5, 5,
|
|
415 |
5, 5, 30, 5, 30, 5, 5, 5,
|
|
416 |
5, 5, 5, 5, 5,
|
|
417 |
};
|
|
418 |
|
|
419 |
void TransportIndustryGoods(uint tile)
|
|
420 |
{
|
|
421 |
Industry *i;
|
|
422 |
int type;
|
|
423 |
uint cw, am;
|
|
424 |
byte m5;
|
|
425 |
|
|
426 |
i = DEREF_INDUSTRY(_map2[tile]);
|
|
427 |
|
|
428 |
type = i->type;
|
|
429 |
cw = min(i->cargo_waiting[0], 255);
|
|
430 |
if (cw > _industry_min_cargo[type]/* && i->produced_cargo[0] != 0xFF*/) {
|
|
431 |
i->cargo_waiting[0] -= cw;
|
|
432 |
|
|
433 |
/* fluctuating economy? */
|
|
434 |
if (_economy.fluct <= 0) cw = (cw + 1) >> 1;
|
|
435 |
|
|
436 |
i->last_mo_production[0] += cw;
|
|
437 |
|
|
438 |
am = MoveGoodsToStation(i->xy, i->width, i->height, i->produced_cargo[0], cw);
|
|
439 |
i->last_mo_transported[0] += am;
|
|
440 |
if (am != 0 && (m5 = _industry_produce_map5[_map5[tile]]) != 0xFF) {
|
|
441 |
_map5[tile] = m5;
|
|
442 |
_map_owner[tile] = 0x80;
|
|
443 |
MarkTileDirtyByTile(tile);
|
|
444 |
}
|
|
445 |
}
|
|
446 |
|
|
447 |
type = i->type;
|
|
448 |
cw = min(i->cargo_waiting[1], 255);
|
|
449 |
if (cw > _industry_min_cargo[type]) {
|
|
450 |
i->cargo_waiting[1] -= cw;
|
|
451 |
|
|
452 |
if (_economy.fluct <= 0) cw = (cw + 1) >> 1;
|
|
453 |
|
|
454 |
i->last_mo_production[1] += cw;
|
|
455 |
|
|
456 |
am = MoveGoodsToStation(i->xy, i->width, i->height, i->produced_cargo[1], cw);
|
|
457 |
i->last_mo_transported[1] += am;
|
|
458 |
}
|
|
459 |
}
|
|
460 |
|
|
461 |
|
|
462 |
static void AnimateTile_Industry(uint tile)
|
|
463 |
{
|
|
464 |
byte m,n;
|
|
465 |
|
|
466 |
switch(_map5[tile]) {
|
|
467 |
case 174:
|
|
468 |
if ((_tick_counter & 1) == 0) {
|
|
469 |
m = _map3_lo[tile] + 1;
|
|
470 |
|
|
471 |
switch(m & 7) {
|
|
472 |
case 2: SndPlayTileFx(45, tile); break;
|
|
473 |
case 6: SndPlayTileFx(41, tile); break;
|
|
474 |
}
|
|
475 |
|
|
476 |
if (m >= 96) {
|
|
477 |
m = 0;
|
|
478 |
DeleteAnimatedTile(tile);
|
|
479 |
}
|
|
480 |
_map3_lo[tile] = m;
|
|
481 |
|
|
482 |
MarkTileDirtyByTile(tile);
|
|
483 |
}
|
|
484 |
break;
|
|
485 |
|
|
486 |
case 165:
|
|
487 |
if ((_tick_counter & 3) == 0) {
|
|
488 |
m = _map3_lo[tile];
|
|
489 |
|
|
490 |
if (_industry_anim_offs[m] == 0xFF) {
|
|
491 |
SndPlayTileFx(48, tile);
|
|
492 |
}
|
|
493 |
|
|
494 |
if (++m >= 70) {
|
|
495 |
m = 0;
|
|
496 |
DeleteAnimatedTile(tile);
|
|
497 |
}
|
|
498 |
_map3_lo[tile] = m;
|
|
499 |
|
|
500 |
MarkTileDirtyByTile(tile);
|
|
501 |
}
|
|
502 |
break;
|
|
503 |
|
|
504 |
case 162:
|
|
505 |
if ((_tick_counter&1) == 0) {
|
|
506 |
m = _map3_lo[tile];
|
|
507 |
|
|
508 |
if (++m >= 40) {
|
|
509 |
m = 0;
|
|
510 |
DeleteAnimatedTile(tile);
|
|
511 |
}
|
|
512 |
_map3_lo[tile] = m;
|
|
513 |
|
|
514 |
MarkTileDirtyByTile(tile);
|
|
515 |
}
|
|
516 |
break;
|
|
517 |
|
|
518 |
// Sparks on a coal plant
|
|
519 |
case 10:
|
|
520 |
if ((_tick_counter & 3) == 0) {
|
|
521 |
m = _map_owner[tile];
|
|
522 |
if ((m & (31<<2)) == (6 << 2)) {
|
|
523 |
_map_owner[tile] = m&~(31<<2);
|
|
524 |
DeleteAnimatedTile(tile);
|
|
525 |
} else {
|
|
526 |
_map_owner[tile] = m + (1<<2);
|
|
527 |
MarkTileDirtyByTile(tile);
|
|
528 |
}
|
|
529 |
}
|
|
530 |
break;
|
|
531 |
|
|
532 |
case 143:
|
|
533 |
if ((_tick_counter & 1) == 0) {
|
|
534 |
m = _map3_lo[tile] + 1;
|
|
535 |
|
|
536 |
if (m == 1) {
|
|
537 |
SndPlayTileFx(44, tile);
|
|
538 |
} else if (m == 23) {
|
|
539 |
SndPlayTileFx(43, tile);
|
|
540 |
} else if (m == 28) {
|
|
541 |
SndPlayTileFx(42, tile);
|
|
542 |
}
|
|
543 |
|
|
544 |
if (m >= 50 && (m=0,++_map3_hi[tile] >= 8)) {
|
|
545 |
_map3_hi[tile] = 0;
|
|
546 |
DeleteAnimatedTile(tile);
|
|
547 |
}
|
|
548 |
_map3_lo[tile] = m;
|
|
549 |
MarkTileDirtyByTile(tile);
|
|
550 |
}
|
|
551 |
break;
|
|
552 |
|
|
553 |
case 148: case 149: case 150: case 151:
|
|
554 |
case 152: case 153: case 154: case 155:
|
|
555 |
if ((_tick_counter & 3) == 0) {
|
|
556 |
m = _map5[tile] + 1;
|
|
557 |
if (m == 155+1) m = 148;
|
|
558 |
_map5[tile] = m;
|
|
559 |
|
|
560 |
MarkTileDirtyByTile(tile);
|
|
561 |
}
|
|
562 |
break;
|
|
563 |
|
|
564 |
case 30: case 31: case 32:
|
|
565 |
if ((_tick_counter & 7) == 0) {
|
|
566 |
bool b = CHANCE16(1,7);
|
|
567 |
m = _map_owner[tile];
|
|
568 |
m = (m & 3) + 1;
|
|
569 |
n = _map5[tile];
|
|
570 |
if (m == 4 && (m=0,++n) == 32+1 && (n=30,b)) {
|
|
571 |
_map_owner[tile] = 0x83;
|
|
572 |
_map5[tile] = 29;
|
|
573 |
DeleteAnimatedTile(tile);
|
|
574 |
} else {
|
|
575 |
_map5[tile] = n;
|
|
576 |
_map_owner[tile] = (_map_owner[tile] & ~3) | m;
|
|
577 |
MarkTileDirtyByTile(tile);
|
|
578 |
}
|
|
579 |
}
|
|
580 |
break;
|
|
581 |
|
|
582 |
case 88:
|
|
583 |
case 48:
|
|
584 |
case 1: {
|
|
585 |
int state = _tick_counter & 0x7FF;
|
|
586 |
|
|
587 |
if ((state -= 0x400) < 0)
|
|
588 |
return;
|
|
589 |
|
|
590 |
if (state < 0x1A0) {
|
|
591 |
if (state < 0x20 || state >= 0x180) {
|
|
592 |
if (! (_map_owner[tile] & 0x40)) {
|
|
593 |
_map_owner[tile] |= 0x40;
|
|
594 |
SndPlayTileFx(9, tile);
|
|
595 |
}
|
|
596 |
if (state & 7)
|
|
597 |
return;
|
|
598 |
} else {
|
|
599 |
if (state & 3)
|
|
600 |
return;
|
|
601 |
}
|
|
602 |
m = (_map_owner[tile] + 1) | 0x40;
|
|
603 |
if (m > 0xC2) m = 0xC0;
|
|
604 |
_map_owner[tile] = m;
|
|
605 |
MarkTileDirtyByTile(tile);
|
|
606 |
} else if (state >= 0x200 && state < 0x3A0) {
|
|
607 |
int i;
|
|
608 |
i = (state < 0x220 || state >= 0x380) ? 7 : 3;
|
|
609 |
if (state & i)
|
|
610 |
return;
|
|
611 |
|
|
612 |
m = (_map_owner[tile] & 0xBF) - 1;
|
|
613 |
if (m < 0x80) m = 0x82;
|
|
614 |
_map_owner[tile] = m;
|
|
615 |
MarkTileDirtyByTile(tile);
|
|
616 |
}
|
|
617 |
} break;
|
|
618 |
}
|
|
619 |
}
|
|
620 |
|
|
621 |
static void MakeIndustryTileBiggerCase8(uint tile)
|
|
622 |
{
|
|
623 |
TileInfo ti;
|
|
624 |
FindLandscapeHeight(&ti, GET_TILE_X(tile)*16, GET_TILE_Y(tile)*16);
|
|
625 |
CreateEffectVehicle(ti.x + 15, ti.y + 14, ti.z + 59 + (ti.tileh != 0 ? 8 : 0), EV_INDUSTRYSMOKE);
|
|
626 |
}
|
|
627 |
|
|
628 |
static void MakeIndustryTileBigger(uint tile, byte size)
|
|
629 |
{
|
|
630 |
byte b = (byte)((size + (1<<2)) & (3<<2));
|
|
631 |
|
|
632 |
if (b != 0) {
|
|
633 |
_map_owner[tile] = b | (size & 3);
|
|
634 |
return;
|
|
635 |
}
|
|
636 |
|
|
637 |
size = (size + 1) & 3;
|
|
638 |
if (size == 3) size |= 0x80;
|
|
639 |
_map_owner[tile] = size | b;
|
|
640 |
|
|
641 |
MarkTileDirtyByTile(tile);
|
|
642 |
|
|
643 |
if (!(_map_owner[tile] & 0x80))
|
|
644 |
return;
|
|
645 |
|
|
646 |
switch(_map5[tile]) {
|
|
647 |
case 8:
|
|
648 |
MakeIndustryTileBiggerCase8(tile);
|
|
649 |
break;
|
|
650 |
|
|
651 |
case 24:
|
|
652 |
if (_map5[tile + TILE_XY(0,1)] == 24) {
|
|
653 |
BuildOilRig(tile);
|
|
654 |
}
|
|
655 |
break;
|
|
656 |
|
|
657 |
case 143:
|
|
658 |
case 162:
|
|
659 |
case 165:
|
|
660 |
_map3_lo[tile] = 0;
|
|
661 |
_map3_hi[tile] = 0;
|
|
662 |
break;
|
|
663 |
|
|
664 |
case 148: case 149: case 150: case 151:
|
|
665 |
case 152: case 153: case 154: case 155:
|
|
666 |
AddAnimatedTile(tile);
|
|
667 |
break;
|
|
668 |
}
|
|
669 |
}
|
|
670 |
|
|
671 |
|
|
672 |
|
|
673 |
static void TileLoopIndustryCase161(uint tile)
|
|
674 |
{
|
|
675 |
int dir;
|
|
676 |
Vehicle *v;
|
|
677 |
static const int8 _tileloop_ind_case_161[12] = {
|
|
678 |
11, 0, -4, -14,
|
|
679 |
-4, -10, -4, 1,
|
|
680 |
49, 59, 60, 65,
|
|
681 |
};
|
|
682 |
|
|
683 |
SndPlayTileFx(46, tile);
|
|
684 |
|
|
685 |
dir = Random() & 3;
|
|
686 |
|
|
687 |
v = CreateEffectVehicleAbove(
|
|
688 |
GET_TILE_X(tile)*16 + _tileloop_ind_case_161[dir + 0],
|
|
689 |
GET_TILE_Y(tile)*16 + _tileloop_ind_case_161[dir + 4],
|
|
690 |
_tileloop_ind_case_161[dir + 8],
|
|
691 |
EV_INDUSTRY_SMOKE
|
|
692 |
);
|
|
693 |
|
|
694 |
if (v != NULL)
|
|
695 |
v->u.special.unk2 = dir;
|
|
696 |
}
|
|
697 |
|
|
698 |
static void TileLoop_Industry(uint tile)
|
|
699 |
{
|
|
700 |
byte n;
|
|
701 |
|
|
702 |
if (!(_map_owner[tile] & 0x80)) {
|
|
703 |
MakeIndustryTileBigger(tile, _map_owner[tile]);
|
|
704 |
return;
|
|
705 |
}
|
|
706 |
|
|
707 |
if (_game_mode == GM_EDITOR)
|
|
708 |
return;
|
|
709 |
|
|
710 |
TransportIndustryGoods(tile);
|
|
711 |
|
|
712 |
n = _industry_map5_animation_next[_map5[tile]];
|
|
713 |
if (n != 255) {
|
|
714 |
_map5[tile] = n;
|
|
715 |
_map_owner[tile] = 0;
|
|
716 |
MarkTileDirtyByTile(tile);
|
|
717 |
return;
|
|
718 |
}
|
|
719 |
|
|
720 |
#define SET_AND_ANIMATE(tile,a,b) { _map5[tile]=a; _map_owner[tile]=b; AddAnimatedTile(tile); }
|
|
721 |
#define SET_AND_UNANIMATE(tile,a,b) { _map5[tile]=a; _map_owner[tile]=b; DeleteAnimatedTile(tile); }
|
|
722 |
|
|
723 |
switch(_map5[tile]) {
|
|
724 |
case 0:
|
|
725 |
if (!(_tick_counter & 0x400) && CHANCE16(1,2))
|
|
726 |
SET_AND_ANIMATE(tile,1,0x80);
|
|
727 |
break;
|
|
728 |
|
|
729 |
case 47:
|
|
730 |
if (!(_tick_counter & 0x400) && CHANCE16(1,2))
|
|
731 |
SET_AND_ANIMATE(tile,0x30,0x80);
|
|
732 |
break;
|
|
733 |
|
|
734 |
case 79:
|
|
735 |
if (!(_tick_counter & 0x400) && CHANCE16(1,2))
|
|
736 |
SET_AND_ANIMATE(tile,0x58,0x80);
|
|
737 |
break;
|
|
738 |
|
|
739 |
case 29:
|
|
740 |
if (CHANCE16(1,6))
|
|
741 |
SET_AND_ANIMATE(tile,0x1E,0x80);
|
|
742 |
break;
|
|
743 |
|
|
744 |
case 1:
|
|
745 |
if (!(_tick_counter & 0x400))
|
|
746 |
SET_AND_UNANIMATE(tile, 0, 0x83);
|
|
747 |
break;
|
|
748 |
|
|
749 |
case 48:
|
|
750 |
if (!(_tick_counter & 0x400))
|
|
751 |
SET_AND_UNANIMATE(tile, 0x2F, 0x83);
|
|
752 |
break;
|
|
753 |
|
|
754 |
case 88:
|
|
755 |
if (!(_tick_counter & 0x400))
|
|
756 |
SET_AND_UNANIMATE(tile, 0x4F, 0x83);
|
|
757 |
break;
|
|
758 |
|
|
759 |
case 10:
|
|
760 |
if (CHANCE16(1,3)) {
|
|
761 |
SndPlayTileFx(10, tile);
|
|
762 |
AddAnimatedTile(tile);
|
|
763 |
}
|
|
764 |
break;
|
|
765 |
|
|
766 |
case 49: {
|
|
767 |
CreateEffectVehicleAbove(GET_TILE_X(tile)*16 + 6, GET_TILE_Y(tile)*16 + 6, 43, EV_SMOKE_3);
|
|
768 |
} break;
|
|
769 |
|
|
770 |
|
|
771 |
case 143: {
|
|
772 |
Industry *i = DEREF_INDUSTRY(_map2[tile]);
|
|
773 |
if (i->was_cargo_delivered) {
|
|
774 |
i->was_cargo_delivered = false;
|
|
775 |
if ((_map3_hi[tile]|_map3_lo[tile]) != 0)
|
|
776 |
_map3_hi[tile] = 0;
|
|
777 |
AddAnimatedTile(tile);
|
|
778 |
}
|
|
779 |
}
|
|
780 |
break;
|
|
781 |
|
|
782 |
case 161:
|
|
783 |
TileLoopIndustryCase161(tile);
|
|
784 |
break;
|
|
785 |
|
|
786 |
case 165:
|
|
787 |
AddAnimatedTile(tile);
|
|
788 |
break;
|
|
789 |
|
|
790 |
case 174:
|
|
791 |
if (CHANCE16(1,3)) {
|
|
792 |
AddAnimatedTile(tile);
|
|
793 |
}
|
|
794 |
break;
|
|
795 |
}
|
|
796 |
}
|
|
797 |
|
|
798 |
|
|
799 |
static void ClickTile_Industry(uint tile)
|
|
800 |
{
|
|
801 |
ShowIndustryViewWindow(_map2[tile]);
|
|
802 |
}
|
|
803 |
|
|
804 |
static uint32 GetTileTrackStatus_Industry(uint tile, int mode)
|
|
805 |
{
|
|
806 |
return 0;
|
|
807 |
}
|
|
808 |
|
|
809 |
static void GetProducedCargo_Industry(uint tile, byte *b)
|
|
810 |
{
|
|
811 |
Industry *i = DEREF_INDUSTRY(_map2[tile]);
|
|
812 |
b[0] = i->produced_cargo[0];
|
|
813 |
b[1] = i->produced_cargo[1];
|
|
814 |
}
|
|
815 |
|
|
816 |
static void ChangeTileOwner_Industry(uint tile, byte old_player, byte new_player)
|
|
817 |
{
|
|
818 |
/* not used */
|
|
819 |
}
|
|
820 |
|
|
821 |
void DeleteIndustry(Industry *i)
|
|
822 |
{
|
|
823 |
int index = i - _industries;
|
|
824 |
|
|
825 |
BEGIN_TILE_LOOP(tile_cur, i->width, i->height, i->xy);
|
|
826 |
if (IS_TILETYPE(tile_cur, MP_INDUSTRY)) {
|
|
827 |
if (_map2[tile_cur] == (byte)index) {
|
|
828 |
DoClearSquare(tile_cur);
|
|
829 |
}
|
|
830 |
} else if (IS_TILETYPE(tile_cur, MP_STATION) && _map5[tile_cur] == 0x4B) {
|
|
831 |
DeleteOilRig(tile_cur);
|
|
832 |
}
|
|
833 |
END_TILE_LOOP(tile_cur, i->width, i->height, i->xy);
|
|
834 |
|
|
835 |
i->xy = 0;
|
|
836 |
_industry_sort_dirty = true;
|
|
837 |
DeleteSubsidyWithIndustry(index);
|
|
838 |
DeleteWindowById(WC_INDUSTRY_VIEW, index);
|
|
839 |
InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
|
|
840 |
}
|
|
841 |
|
|
842 |
static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
|
|
843 |
|
|
844 |
static bool IsBadFarmFieldTile(uint tile)
|
|
845 |
{
|
|
846 |
if (IS_TILETYPE(tile,MP_CLEAR)) {
|
|
847 |
byte m5 = _map5[tile] & 0x1C;
|
|
848 |
if (m5 == 0xC || m5 == 0x10)
|
|
849 |
return true;
|
|
850 |
return false;
|
|
851 |
} else if (IS_TILETYPE(tile,MP_TREES)) {
|
|
852 |
return false;
|
|
853 |
} else {
|
|
854 |
return true;
|
|
855 |
}
|
|
856 |
}
|
|
857 |
|
|
858 |
static bool IsBadFarmFieldTile2(uint tile)
|
|
859 |
{
|
|
860 |
if (IS_TILETYPE(tile,MP_CLEAR)) {
|
|
861 |
byte m5 = _map5[tile] & 0x1C;
|
|
862 |
if (m5 == 0x10)
|
|
863 |
return true;
|
|
864 |
return false;
|
|
865 |
} else if (IS_TILETYPE(tile,MP_TREES)) {
|
|
866 |
return false;
|
|
867 |
} else {
|
|
868 |
return true;
|
|
869 |
}
|
|
870 |
}
|
|
871 |
|
|
872 |
static void SetupFarmFieldFence(uint tile, int size, byte type, int direction)
|
|
873 |
{
|
|
874 |
byte or, and;
|
|
875 |
|
|
876 |
do {
|
|
877 |
tile = TILE_MASK(tile);
|
|
878 |
|
|
879 |
if (IS_TILETYPE(tile, MP_CLEAR) || IS_TILETYPE(tile, MP_TREES)) {
|
|
880 |
|
|
881 |
or = type;
|
|
882 |
if (or == 1 && (uint16)Random() <= 9362) or = 2;
|
|
883 |
|
|
884 |
or <<= 2;
|
|
885 |
and = (byte)~0x1C;
|
|
886 |
if (direction) {
|
|
887 |
or <<= 3;
|
|
888 |
and = (byte)~0xE0;
|
|
889 |
}
|
|
890 |
_map3_hi[tile] = (_map3_hi[tile] & and) | or;
|
|
891 |
}
|
|
892 |
|
|
893 |
tile += direction ? TILE_XY(0,1) : TILE_XY(1,0);
|
|
894 |
} while (--size);
|
|
895 |
}
|
|
896 |
|
|
897 |
static void PlantFarmField(uint tile)
|
|
898 |
{
|
|
899 |
uint size_x, size_y;
|
|
900 |
uint32 r;
|
|
901 |
int count;
|
|
902 |
int type, type2;
|
|
903 |
|
|
904 |
if (_opt.landscape == LT_HILLY) {
|
|
905 |
if (GetTileZ(tile) >= (_opt.snow_line - 16))
|
|
906 |
return;
|
|
907 |
}
|
|
908 |
|
|
909 |
/* determine field size */
|
|
910 |
r = (Random() & 0x303) + 0x404;
|
|
911 |
if (_opt.landscape == LT_HILLY) r += 0x404;
|
|
912 |
size_x = (byte)r;
|
|
913 |
size_y = r >> 8;
|
|
914 |
|
|
915 |
/* offset tile to match size */
|
|
916 |
tile = tile - TILE_XY(size_x>>1, size_y>>1);
|
|
917 |
|
|
918 |
/* check the amount of bad tiles */
|
|
919 |
count = 0;
|
|
920 |
BEGIN_TILE_LOOP(cur_tile, size_x, size_y, tile)
|
|
921 |
cur_tile = TILE_MASK(cur_tile);
|
|
922 |
count += IsBadFarmFieldTile(cur_tile);
|
|
923 |
END_TILE_LOOP(cur_tile, size_x, size_y, tile)
|
|
924 |
if ((uint)(count * 2) >= size_x * size_y)
|
|
925 |
return;
|
|
926 |
|
|
927 |
/* determine type of field */
|
|
928 |
r = Random();
|
|
929 |
type = ((r & 0xE0) | 0xF);
|
|
930 |
type2 = ((byte)(r >> 8) * 9) >> 8;
|
|
931 |
|
|
932 |
/* make field */
|
|
933 |
BEGIN_TILE_LOOP(cur_tile, size_x, size_y, tile)
|
|
934 |
cur_tile = TILE_MASK(cur_tile);
|
|
935 |
if (!IsBadFarmFieldTile2(cur_tile)) {
|
|
936 |
ModifyTile(cur_tile,
|
|
937 |
MP_SETTYPE(MP_CLEAR) |
|
|
938 |
MP_MAP2_CLEAR | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAPOWNER | MP_MAP5,
|
|
939 |
type2, /* map3_lo */
|
|
940 |
OWNER_NONE, /* map_owner */
|
|
941 |
type); /* map5 */
|
|
942 |
}
|
|
943 |
END_TILE_LOOP(cur_tile, size_x, size_y, tile)
|
|
944 |
|
|
945 |
type = 3;
|
|
946 |
if (_opt.landscape != LT_HILLY && _opt.landscape != LT_DESERT) {
|
|
947 |
type = _plantfarmfield_type[Random() & 0xF];
|
|
948 |
}
|
|
949 |
|
|
950 |
SetupFarmFieldFence(tile-TILE_XY(1,0), size_y, type, 1);
|
|
951 |
SetupFarmFieldFence(tile-TILE_XY(0,1), size_x, type, 0);
|
|
952 |
SetupFarmFieldFence(tile+TILE_XY(1,0) * (size_x-1), size_y, type, 1);
|
|
953 |
SetupFarmFieldFence(tile+TILE_XY(0,1) * (size_y-1), size_x, type, 0);
|
|
954 |
}
|
|
955 |
|
|
956 |
static void MaybePlantFarmField(Industry *i)
|
|
957 |
{
|
|
958 |
uint tile;
|
|
959 |
if (CHANCE16(1,8)) {
|
|
960 |
tile = TileAddWrap(i->xy, TILE_XY(i->width>>1, i->height>>1) + (Random()&TILE_XY(31,31))-TILE_XY(16,16));
|
|
961 |
if (tile != TILE_WRAPPED)
|
|
962 |
PlantFarmField(tile);
|
|
963 |
}
|
|
964 |
}
|
|
965 |
|
|
966 |
static void ChopLumberMillTrees(Industry *i)
|
|
967 |
{
|
|
968 |
static const TileIndexDiff _chop_dir[4] = { TILE_XY(0,1), TILE_XY(1,0), TILE_XY(0,-1), TILE_XY(-1,0) };
|
|
969 |
|
|
970 |
uint tile = i->xy;
|
|
971 |
int dir, a, j;
|
|
972 |
|
|
973 |
if ((_map_owner[tile] & 0x80) == 0)
|
|
974 |
return;
|
|
975 |
|
|
976 |
/* search outwards as a rectangular spiral */
|
|
977 |
for(a=1; a!=41; a+=2) {
|
|
978 |
for(dir=0; dir!=4; dir++) {
|
|
979 |
j = a;
|
|
980 |
do {
|
|
981 |
tile = TILE_MASK(tile);
|
|
982 |
if (IS_TILETYPE(tile, MP_TREES)) {
|
|
983 |
/* found a tree */
|
|
984 |
|
|
985 |
_current_player = OWNER_NONE;
|
|
986 |
_industry_sound_ctr = 1;
|
|
987 |
_industry_sound_tile = tile;
|
|
988 |
SndPlayTileFx(56, tile);
|
|
989 |
|
|
990 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
991 |
SetMapExtraBits(tile, 0);
|
|
992 |
|
|
993 |
i->cargo_waiting[0] = min(0xffff, i->cargo_waiting[0] + 45);
|
|
994 |
return;
|
|
995 |
}
|
|
996 |
tile += _chop_dir[dir];
|
|
997 |
} while (--j);
|
|
998 |
}
|
|
999 |
tile -= TILE_XY(1,1);
|
|
1000 |
}
|
|
1001 |
}
|
|
1002 |
|
|
1003 |
static const byte _industry_sounds[37][2] = {
|
|
1004 |
{0},
|
|
1005 |
{0},
|
|
1006 |
{1,38},
|
|
1007 |
{0},
|
|
1008 |
{0},
|
|
1009 |
{0},
|
|
1010 |
{1,1},
|
|
1011 |
{1,1},
|
|
1012 |
{0},
|
|
1013 |
{3,34},
|
|
1014 |
{0},
|
|
1015 |
{0},
|
|
1016 |
{0},
|
|
1017 |
{0},
|
|
1018 |
{1,38},
|
|
1019 |
{0},
|
|
1020 |
{0},
|
|
1021 |
{0},
|
|
1022 |
{0},
|
|
1023 |
{0},
|
|
1024 |
{0},
|
|
1025 |
{0},
|
|
1026 |
{0},
|
|
1027 |
{1,1},
|
|
1028 |
{0},
|
|
1029 |
{0},
|
|
1030 |
{0},
|
|
1031 |
{0},
|
|
1032 |
{0},
|
|
1033 |
{0},
|
|
1034 |
{0},
|
|
1035 |
{0},
|
|
1036 |
{1,51},
|
|
1037 |
{0},
|
|
1038 |
{0},
|
|
1039 |
{0},
|
|
1040 |
{0},
|
|
1041 |
};
|
|
1042 |
|
|
1043 |
|
|
1044 |
static void ProduceIndustryGoods(Industry *i)
|
|
1045 |
{
|
|
1046 |
uint32 r;
|
|
1047 |
uint num;
|
|
1048 |
|
|
1049 |
/* play a sound? */
|
|
1050 |
if ((i->counter & 0x3F) == 0) {
|
|
1051 |
if (CHANCE16R(1,14,r) && (num=_industry_sounds[i->type][0]) != 0) {
|
|
1052 |
SndPlayTileFx(
|
|
1053 |
_industry_sounds[i->type][1] + (((r >> 16) * num) >> 16),
|
|
1054 |
i->xy);
|
|
1055 |
}
|
|
1056 |
}
|
|
1057 |
|
|
1058 |
i->counter--;
|
|
1059 |
|
|
1060 |
/* produce some cargo */
|
|
1061 |
if ((i->counter & 0xFF) == 0) {
|
|
1062 |
i->cargo_waiting[0] = min(0xffff, i->cargo_waiting[0] + i->production_rate[0]);
|
|
1063 |
i->cargo_waiting[1] = min(0xffff, i->cargo_waiting[1] + i->production_rate[1]);
|
|
1064 |
|
|
1065 |
if (i->type == IT_FARM)
|
|
1066 |
MaybePlantFarmField(i);
|
|
1067 |
else if (i->type == IT_LUMBER_MILL && (i->counter & 0x1FF) == 0)
|
|
1068 |
ChopLumberMillTrees(i);
|
|
1069 |
}
|
|
1070 |
}
|
|
1071 |
|
|
1072 |
void OnTick_Industry()
|
|
1073 |
{
|
|
1074 |
Industry *i;
|
|
1075 |
|
|
1076 |
if (_industry_sound_ctr != 0) {
|
|
1077 |
_industry_sound_ctr++;
|
|
1078 |
|
|
1079 |
if (_industry_sound_ctr == 75) {
|
|
1080 |
SndPlayTileFx(55, _industry_sound_tile);
|
|
1081 |
} else if (_industry_sound_ctr == 160) {
|
|
1082 |
_industry_sound_ctr = 0;
|
|
1083 |
SndPlayTileFx(54, _industry_sound_tile);
|
|
1084 |
}
|
|
1085 |
}
|
|
1086 |
|
|
1087 |
if (_game_mode == GM_EDITOR)
|
|
1088 |
return;
|
|
1089 |
|
|
1090 |
for(i=_industries; i != endof(_industries); i++) {
|
|
1091 |
if (i->xy != 0)
|
|
1092 |
ProduceIndustryGoods(i);
|
|
1093 |
}
|
|
1094 |
}
|
|
1095 |
|
|
1096 |
|
|
1097 |
static bool CheckNewIndustry_NULL(uint tile, int type)
|
|
1098 |
{
|
|
1099 |
return true;
|
|
1100 |
}
|
|
1101 |
|
|
1102 |
static bool CheckNewIndustry_Forest(uint tile, int type)
|
|
1103 |
{
|
|
1104 |
if (_opt.landscape == LT_HILLY) {
|
|
1105 |
if (GetTileZ(tile) < (_opt.snow_line + 16) ) {
|
|
1106 |
_error_message = STR_4831_FOREST_CAN_ONLY_BE_PLANTED;
|
|
1107 |
return false;
|
|
1108 |
}
|
|
1109 |
}
|
|
1110 |
return true;
|
|
1111 |
}
|
|
1112 |
|
|
1113 |
static bool CheckNewIndustry_Oilwell(uint tile, int type)
|
|
1114 |
{
|
|
1115 |
int x,y;
|
|
1116 |
if (type != IT_OIL_RIG && _game_mode == GM_EDITOR)
|
|
1117 |
return true;
|
|
1118 |
|
|
1119 |
x = GET_TILE_X(tile);
|
|
1120 |
y = GET_TILE_Y(tile);
|
|
1121 |
|
|
1122 |
if ( x < 15 || y < 15 || x > 238 || y > 238)
|
|
1123 |
return true;
|
|
1124 |
|
|
1125 |
_error_message = STR_483B_CAN_ONLY_BE_POSITIONED;
|
|
1126 |
return false;
|
|
1127 |
}
|
|
1128 |
|
|
1129 |
static bool CheckNewIndustry_Farm(uint tile, int type)
|
|
1130 |
{
|
|
1131 |
if (_opt.landscape == LT_HILLY) {
|
|
1132 |
if (GetTileZ(tile) >= (_opt.snow_line - 16)) {
|
|
1133 |
_error_message = STR_0239_SITE_UNSUITABLE;
|
|
1134 |
return false;
|
|
1135 |
}
|
|
1136 |
}
|
|
1137 |
return true;
|
|
1138 |
}
|
|
1139 |
|
|
1140 |
static bool CheckNewIndustry_Plantation(uint tile, int type)
|
|
1141 |
{
|
|
1142 |
if (GetMapExtraBits(tile) == 1) {
|
|
1143 |
_error_message = STR_0239_SITE_UNSUITABLE;
|
|
1144 |
return false;
|
|
1145 |
}
|
|
1146 |
|
|
1147 |
return true;
|
|
1148 |
}
|
|
1149 |
|
|
1150 |
static bool CheckNewIndustry_Water(uint tile, int type)
|
|
1151 |
{
|
|
1152 |
if (GetMapExtraBits(tile) != 1) {
|
|
1153 |
_error_message = STR_0318_CAN_ONLY_BE_BUILT_IN_DESERT;
|
|
1154 |
return false;
|
|
1155 |
}
|
|
1156 |
|
|
1157 |
return true;
|
|
1158 |
}
|
|
1159 |
|
|
1160 |
static bool CheckNewIndustry_Lumbermill(uint tile, int type)
|
|
1161 |
{
|
|
1162 |
if (GetMapExtraBits(tile) != 2) {
|
|
1163 |
_error_message = STR_0317_CAN_ONLY_BE_BUILT_IN_RAINFOREST;
|
|
1164 |
return false;
|
|
1165 |
}
|
|
1166 |
return true;
|
|
1167 |
}
|
|
1168 |
|
|
1169 |
static bool CheckNewIndustry_BubbleGen(uint tile, int type)
|
|
1170 |
{
|
|
1171 |
if (GetTileZ(tile) > 32) {
|
|
1172 |
return false;
|
|
1173 |
}
|
|
1174 |
return true;
|
|
1175 |
}
|
|
1176 |
|
|
1177 |
typedef bool CheckNewIndustryProc(uint tile, int type);
|
|
1178 |
static CheckNewIndustryProc * const _check_new_industry_procs[] = {
|
|
1179 |
CheckNewIndustry_NULL,
|
|
1180 |
CheckNewIndustry_Forest,
|
|
1181 |
CheckNewIndustry_Oilwell,
|
|
1182 |
CheckNewIndustry_Farm,
|
|
1183 |
CheckNewIndustry_Plantation,
|
|
1184 |
CheckNewIndustry_Water,
|
|
1185 |
CheckNewIndustry_Lumbermill,
|
|
1186 |
CheckNewIndustry_BubbleGen,
|
|
1187 |
};
|
|
1188 |
|
|
1189 |
static bool CheckSuitableIndustryPos(uint tile)
|
|
1190 |
{
|
|
1191 |
int x = GET_TILE_X(tile);
|
|
1192 |
int y = GET_TILE_Y(tile);
|
|
1193 |
|
|
1194 |
if ( x < 2 || y < 2 || x > TILE_X_MAX - 3 || y > TILE_Y_MAX - 3) {
|
|
1195 |
_error_message = STR_0239_SITE_UNSUITABLE;
|
|
1196 |
return false;
|
|
1197 |
}
|
|
1198 |
|
|
1199 |
return true;
|
|
1200 |
}
|
|
1201 |
|
|
1202 |
static Town *CheckMultipleIndustryInTown(uint tile, int type)
|
|
1203 |
{
|
|
1204 |
Town *t;
|
|
1205 |
Industry *i;
|
|
1206 |
|
|
1207 |
t = ClosestTownFromTile(tile, (uint)-1);
|
|
1208 |
|
|
1209 |
if (_patches.multiple_industry_per_town)
|
|
1210 |
return t;
|
|
1211 |
|
|
1212 |
for(i=_industries; i != endof(_industries); i++) {
|
|
1213 |
if (i->xy != 0 &&
|
|
1214 |
i->type == (byte)type &&
|
|
1215 |
i->town == t) {
|
|
1216 |
_error_message = STR_0287_ONLY_ONE_ALLOWED_PER_TOWN;
|
|
1217 |
return NULL;
|
|
1218 |
}
|
|
1219 |
}
|
|
1220 |
|
|
1221 |
return t;
|
|
1222 |
}
|
|
1223 |
|
|
1224 |
static const byte _industry_map5_bits[] = {
|
|
1225 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1226 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1227 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1228 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1229 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1230 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1231 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1232 |
16, 16, 4, 2, 16, 16, 16, 16,
|
|
1233 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1234 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1235 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1236 |
16, 4, 2, 16, 16, 16, 16, 16,
|
|
1237 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1238 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1239 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1240 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1241 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1242 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1243 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1244 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1245 |
16, 16, 16, 16, 16, 16, 16, 16,
|
|
1246 |
16, 16, 16, 16, 16, 16, 16,
|
|
1247 |
};
|
|
1248 |
|
|
1249 |
static bool CheckIfIndustryTilesAreFree(uint tile, const IndustryTileTable *it, int type, Town *t)
|
|
1250 |
{
|
|
1251 |
TileInfo ti;
|
|
1252 |
uint cur_tile;
|
|
1253 |
|
|
1254 |
_error_message = STR_0239_SITE_UNSUITABLE;
|
|
1255 |
|
|
1256 |
do {
|
|
1257 |
cur_tile = tile + it->ti;
|
|
1258 |
if (!IsValidTile(cur_tile)) {
|
|
1259 |
if (it->map5 == 0xff)
|
|
1260 |
continue;
|
|
1261 |
return false;
|
|
1262 |
}
|
|
1263 |
|
|
1264 |
FindLandscapeHeightByTile(&ti, cur_tile);
|
|
1265 |
|
|
1266 |
if (it->map5 == 0xFF) {
|
|
1267 |
if (ti.type != MP_WATER || ti.tileh != 0)
|
|
1268 |
return false;
|
|
1269 |
} else {
|
|
1270 |
if (!EnsureNoVehicle(cur_tile))
|
|
1271 |
return false;
|
|
1272 |
|
|
1273 |
if (type == IT_OIL_RIG) {
|
|
1274 |
if (ti.type != MP_WATER || ti.map5 != 0)
|
|
1275 |
return false;
|
|
1276 |
} else {
|
|
1277 |
if (ti.type == MP_WATER && ti.map5 == 0)
|
|
1278 |
return false;
|
|
1279 |
if (ti.tileh & 0x10)
|
|
1280 |
return false;
|
|
1281 |
|
|
1282 |
if (ti.tileh != 0) {
|
|
1283 |
int t;
|
|
1284 |
byte bits = _industry_map5_bits[it->map5];
|
|
1285 |
|
|
1286 |
if (bits & 0x10)
|
|
1287 |
return false;
|
|
1288 |
|
|
1289 |
t = ~ti.tileh;
|
|
1290 |
|
|
1291 |
if (bits & 1 && (t & (1+8)))
|
|
1292 |
return false;
|
|
1293 |
|
|
1294 |
if (bits & 2 && (t & (4+8)))
|
|
1295 |
return false;
|
|
1296 |
|
|
1297 |
if (bits & 4 && (t & (1+2)))
|
|
1298 |
return false;
|
|
1299 |
|
|
1300 |
if (bits & 8 && (t & (2+4)))
|
|
1301 |
return false;
|
|
1302 |
}
|
|
1303 |
|
|
1304 |
if (type == IT_BANK) {
|
|
1305 |
if (ti.type != MP_HOUSE || t->population < 1200) {
|
|
1306 |
_error_message = STR_029D_CAN_ONLY_BE_BUILT_IN_TOWNS;
|
|
1307 |
return false;
|
|
1308 |
}
|
|
1309 |
} else if (type == IT_BANK_2) {
|
|
1310 |
if (ti.type != MP_HOUSE) {
|
|
1311 |
_error_message = STR_030D_CAN_ONLY_BE_BUILT_IN_TOWNS;
|
|
1312 |
return false;
|
|
1313 |
}
|
|
1314 |
} else if (type == IT_TOY_SHOP) {
|
|
1315 |
if (GetTileDist1D(t->xy, cur_tile) > 9)
|
|
1316 |
return false;
|
|
1317 |
if (ti.type != MP_HOUSE) goto do_clear;
|
|
1318 |
} else if (type == IT_WATER_TOWER) {
|
|
1319 |
if (ti.type != MP_HOUSE) {
|
|
1320 |
_error_message = STR_0316_CAN_ONLY_BE_BUILT_IN_TOWNS;
|
|
1321 |
return false;
|
|
1322 |
}
|
|
1323 |
} else {
|
|
1324 |
do_clear:
|
|
1325 |
if (DoCommandByTile(cur_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR) == CMD_ERROR)
|
|
1326 |
return false;
|
|
1327 |
}
|
|
1328 |
}
|
|
1329 |
}
|
|
1330 |
} while ( (++it)->ti != -0x8000);
|
|
1331 |
|
|
1332 |
return true;
|
|
1333 |
}
|
|
1334 |
|
|
1335 |
static bool CheckIfTooCloseToIndustry(uint tile, int type)
|
|
1336 |
{
|
|
1337 |
Industry *i;
|
|
1338 |
const IndustrySpec *spec;
|
|
1339 |
spec = &_industry_spec[type];
|
|
1340 |
|
|
1341 |
// accepting industries won't be close, not even with patch
|
|
1342 |
if (_patches.same_industry_close && (spec->accepts_cargo[0] == 0xFF) )
|
|
1343 |
return true;
|
|
1344 |
|
|
1345 |
for(i=_industries; i != endof(_industries); i++) {
|
|
1346 |
// check if an industry that accepts the same goods is nearby
|
|
1347 |
if (i->xy != 0 &&
|
|
1348 |
(GetTileDist1D(tile, i->xy) <= 14) &&
|
|
1349 |
spec->accepts_cargo[0] != 0xFF &&
|
|
1350 |
spec->accepts_cargo[0] == i->accepts_cargo[0]) {
|
|
1351 |
_error_message = STR_INDUSTRY_TOO_CLOSE;
|
|
1352 |
return false;
|
|
1353 |
}
|
|
1354 |
|
|
1355 |
// check "not close to" field.
|
|
1356 |
if (i->xy != 0 &&
|
|
1357 |
(i->type == spec->a || i->type == spec->b || i->type == spec->c) &&
|
|
1358 |
GetTileDist1D(tile, i->xy) <= 14) {
|
|
1359 |
_error_message = STR_INDUSTRY_TOO_CLOSE;
|
|
1360 |
return false;
|
|
1361 |
}
|
|
1362 |
}
|
|
1363 |
return true;
|
|
1364 |
}
|
|
1365 |
|
|
1366 |
static Industry *AllocateIndustry()
|
|
1367 |
{
|
|
1368 |
Industry *i;
|
|
1369 |
|
|
1370 |
for(i=_industries; i != endof(_industries); i++) {
|
|
1371 |
if (i->xy == 0) {
|
|
1372 |
int index = i - _industries;
|
|
1373 |
if (index > _total_industries) _total_industries++;
|
|
1374 |
return i;
|
|
1375 |
}
|
|
1376 |
}
|
|
1377 |
return NULL;
|
|
1378 |
}
|
|
1379 |
|
|
1380 |
static void DoCreateNewIndustry(Industry *i, uint tile, int type, const IndustryTileTable *it, Town *t, byte owner)
|
|
1381 |
{
|
|
1382 |
const IndustrySpec *spec;
|
|
1383 |
uint32 r;
|
|
1384 |
uint cur_tile;
|
|
1385 |
int j;
|
|
1386 |
|
|
1387 |
i->xy = tile;
|
|
1388 |
i->width = i->height = 0;
|
|
1389 |
i->type = type;
|
|
1390 |
|
|
1391 |
spec = &_industry_spec[type];
|
|
1392 |
|
|
1393 |
i->produced_cargo[0] = spec->produced_cargo[0];
|
|
1394 |
i->produced_cargo[1] = spec->produced_cargo[1];
|
|
1395 |
i->accepts_cargo[0] = spec->accepts_cargo[0];
|
|
1396 |
i->accepts_cargo[1] = spec->accepts_cargo[1];
|
|
1397 |
i->accepts_cargo[2] = spec->accepts_cargo[2];
|
|
1398 |
i->production_rate[0] = spec->production_rate[0];
|
|
1399 |
i->production_rate[1] = spec->production_rate[1];
|
|
1400 |
|
|
1401 |
if (_patches.smooth_economy) {
|
|
1402 |
i->production_rate[0] = min((RandomRange(256) + 128) * i->production_rate[0] >> 8 , 255);
|
|
1403 |
i->production_rate[1] = min((RandomRange(256) + 128) * i->production_rate[1] >> 8 , 255);
|
|
1404 |
}
|
|
1405 |
|
|
1406 |
i->town = t;
|
|
1407 |
i->owner = owner;
|
|
1408 |
|
|
1409 |
r = Random() & 0xFFF;
|
|
1410 |
i->color_map = (byte)(r >> 8);
|
|
1411 |
i->counter = (uint16)r;
|
|
1412 |
i->cargo_waiting[0] = 0;
|
|
1413 |
i->cargo_waiting[1] = 0;
|
|
1414 |
i->last_mo_production[0] = 0;
|
|
1415 |
i->last_mo_production[1] = 0;
|
|
1416 |
i->last_mo_transported[0] = 0;
|
|
1417 |
i->last_mo_transported[1] = 0;
|
|
1418 |
i->pct_transported[0] = 0;
|
|
1419 |
i->pct_transported[1] = 0;
|
|
1420 |
i->total_transported[0] = 0;
|
|
1421 |
i->total_transported[1] = 0;
|
|
1422 |
i->was_cargo_delivered = false;
|
|
1423 |
i->last_prod_year = _cur_year;
|
|
1424 |
i->total_production[0] = i->production_rate[0] * 8;
|
|
1425 |
i->total_production[1] = i->production_rate[1] * 8;
|
|
1426 |
|
|
1427 |
if (_generating_world == 0)
|
|
1428 |
i->total_production[0] = i->total_production[1] = 0;
|
|
1429 |
|
|
1430 |
i->prod_level = 0x10;
|
|
1431 |
|
|
1432 |
do {
|
|
1433 |
cur_tile = tile + it->ti;
|
|
1434 |
|
|
1435 |
if (it->map5 != 0xFF) {
|
|
1436 |
byte size;
|
|
1437 |
|
|
1438 |
size = GET_TILE_X((TileIndex)it->ti);
|
|
1439 |
if (size > i->width) i->width = size;
|
|
1440 |
size = GET_TILE_Y((TileIndex)it->ti);
|
|
1441 |
if (size > i->height)i->height = size;
|
|
1442 |
|
|
1443 |
DoCommandByTile(cur_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
1444 |
|
|
1445 |
_map_type_and_height[cur_tile] = (_map_type_and_height[cur_tile]&~0xF0) | (MP_INDUSTRY<<4);
|
|
1446 |
_map5[cur_tile] = it->map5;
|
|
1447 |
_map2[cur_tile] = i - _industries;
|
|
1448 |
_map_owner[cur_tile] = _generating_world ? 0x1E : 0; /* maturity */
|
|
1449 |
}
|
|
1450 |
} while ( (++it)->ti != -0x8000);
|
|
1451 |
|
|
1452 |
i->width++;
|
|
1453 |
i->height++;
|
|
1454 |
|
|
1455 |
if (i->type == IT_FARM || i->type == IT_FARM_2) {
|
|
1456 |
tile = i->xy + TILE_XY((i->width >> 1), (i->height >> 1));
|
|
1457 |
for(j=0; j!=50; j++) {
|
|
1458 |
uint new_tile = TileAddWrap(tile, (Random() & TILE_XY(31,31)) - TILE_XY(16,16));
|
|
1459 |
if (new_tile != TILE_WRAPPED)
|
|
1460 |
PlantFarmField(new_tile);
|
|
1461 |
}
|
|
1462 |
}
|
|
1463 |
_industry_sort_dirty = true;
|
|
1464 |
InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
|
|
1465 |
}
|
|
1466 |
|
|
1467 |
/* p1 = industry type 0-36 */
|
|
1468 |
int32 CmdBuildIndustry(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
1469 |
{
|
|
1470 |
uint tile = TILE_FROM_XY(x,y);
|
|
1471 |
Town *t;
|
|
1472 |
int num;
|
|
1473 |
const IndustryTileTable * const *itt;
|
|
1474 |
const IndustryTileTable *it;
|
|
1475 |
Industry *i;
|
|
1476 |
const IndustrySpec *spec;
|
|
1477 |
|
|
1478 |
SET_EXPENSES_TYPE(EXPENSES_OTHER);
|
|
1479 |
|
|
1480 |
if (!CheckSuitableIndustryPos(tile))
|
|
1481 |
return CMD_ERROR;
|
|
1482 |
|
|
1483 |
spec = &_industry_spec[p1];
|
|
1484 |
|
|
1485 |
if (!_check_new_industry_procs[spec->check_proc](tile, p1))
|
|
1486 |
return CMD_ERROR;
|
|
1487 |
|
|
1488 |
if ((t=CheckMultipleIndustryInTown(tile, p1)) == NULL)
|
|
1489 |
return CMD_ERROR;
|
|
1490 |
|
|
1491 |
num = spec->num_table;
|
|
1492 |
itt = spec->table;
|
|
1493 |
|
|
1494 |
do {
|
|
1495 |
if (--num < 0)
|
|
1496 |
return_cmd_error(STR_0239_SITE_UNSUITABLE);
|
|
1497 |
} while (!CheckIfIndustryTilesAreFree(tile, it=itt[num], p1, t));
|
|
1498 |
|
|
1499 |
|
|
1500 |
if (!CheckIfTooCloseToIndustry(tile, p1))
|
|
1501 |
return CMD_ERROR;
|
|
1502 |
|
|
1503 |
if ( (i = AllocateIndustry()) == NULL)
|
|
1504 |
return CMD_ERROR;
|
|
1505 |
|
|
1506 |
if (flags & DC_EXEC)
|
|
1507 |
DoCreateNewIndustry(i, tile, p1, it, t, 0x10);
|
|
1508 |
|
|
1509 |
return (_price.build_industry >> 5) * _industry_type_costs[p1];
|
|
1510 |
}
|
|
1511 |
|
|
1512 |
|
|
1513 |
Industry *CreateNewIndustry(uint tile, int type)
|
|
1514 |
{
|
|
1515 |
Town *t;
|
|
1516 |
const IndustryTileTable *it;
|
|
1517 |
Industry *i;
|
|
1518 |
|
|
1519 |
const IndustrySpec *spec;
|
|
1520 |
|
|
1521 |
if (!CheckSuitableIndustryPos(tile))
|
|
1522 |
return NULL;
|
|
1523 |
|
|
1524 |
spec = &_industry_spec[type];
|
|
1525 |
|
|
1526 |
if (!_check_new_industry_procs[spec->check_proc](tile, type))
|
|
1527 |
return NULL;
|
|
1528 |
|
|
1529 |
if (!(t=CheckMultipleIndustryInTown(tile, type)))
|
|
1530 |
return NULL;
|
|
1531 |
|
|
1532 |
/* pick a random layout */
|
|
1533 |
it = spec->table[(spec->num_table * (uint16)Random()) >> 16];
|
|
1534 |
|
|
1535 |
if (!CheckIfIndustryTilesAreFree(tile, it, type, t))
|
|
1536 |
return NULL;
|
|
1537 |
|
|
1538 |
if (!CheckIfTooCloseToIndustry(tile, type))
|
|
1539 |
return NULL;
|
|
1540 |
|
|
1541 |
if ( (i = AllocateIndustry()) == NULL)
|
|
1542 |
return NULL;
|
|
1543 |
|
|
1544 |
DoCreateNewIndustry(i, tile, type, it, t, 0x10);
|
|
1545 |
|
|
1546 |
return i;
|
|
1547 |
}
|
|
1548 |
|
|
1549 |
static const byte _numof_industry_table[3][12] = {
|
|
1550 |
{0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5},
|
|
1551 |
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
1552 |
{0, 2, 3, 4, 6, 7, 8, 9, 10, 10, 10},
|
|
1553 |
};
|
|
1554 |
|
|
1555 |
static void PlaceInitialIndustry(byte type, int amount)
|
|
1556 |
{
|
|
1557 |
int num = _numof_industry_table[_opt.diff.number_industries][amount];
|
|
1558 |
|
|
1559 |
assert(num > 0);
|
|
1560 |
|
|
1561 |
do {
|
|
1562 |
int i = 2000;
|
|
1563 |
do {
|
|
1564 |
if (CreateNewIndustry(TILE_MASK(Random()), type) != NULL)
|
|
1565 |
break;
|
|
1566 |
} while (--i != 0);
|
|
1567 |
} while (--num);
|
|
1568 |
}
|
|
1569 |
|
|
1570 |
void GenerateIndustries()
|
|
1571 |
{
|
|
1572 |
const byte *b;
|
|
1573 |
|
|
1574 |
b = _industry_create_table[_opt.landscape];
|
|
1575 |
do {
|
|
1576 |
PlaceInitialIndustry(b[1], b[0]);
|
|
1577 |
} while ( (b+=2)[0] != 0);
|
|
1578 |
}
|
|
1579 |
|
|
1580 |
static void ExtChangeIndustryProduction(Industry *i)
|
|
1581 |
{
|
|
1582 |
bool closeit;
|
|
1583 |
int j;
|
|
1584 |
|
|
1585 |
if (_industry_close_mode[i->type] == 0) return;
|
|
1586 |
|
|
1587 |
closeit = true;
|
|
1588 |
|
|
1589 |
if (_industry_close_mode[i->type] == 2) {
|
|
1590 |
if ( (byte)(_cur_year - i->last_prod_year) < 5 || !CHANCE16(1,180))
|
|
1591 |
closeit = false;
|
|
1592 |
} else {
|
|
1593 |
for (j=0; j != 2 && i->produced_cargo[j]!=255; j++){
|
|
1594 |
uint32 r;
|
|
1595 |
int change,percent,old;
|
|
1596 |
int mag;
|
|
1597 |
|
|
1598 |
change = old = i->production_rate[j];
|
|
1599 |
if (CHANCE16R(20,1024,r))change -= ((RandomRange(50) + 10)*old) >> 8;
|
|
1600 |
if (CHANCE16I(20+(i->pct_transported[j]*20>>8),1024,r>>16)) change += ((RandomRange(50) + 10)*old) >> 8;
|
|
1601 |
|
|
1602 |
// make sure it doesn't exceed 255 or goes below 0
|
|
1603 |
change = clamp(change, 0, 255);
|
|
1604 |
if (change == old) {
|
|
1605 |
closeit = false;
|
|
1606 |
continue;
|
|
1607 |
}
|
|
1608 |
|
|
1609 |
percent = change*100/old - 100;
|
|
1610 |
i->production_rate[j] = change;
|
|
1611 |
|
|
1612 |
if (change >= _industry_spec[i->type].production_rate[j]/4)
|
|
1613 |
closeit = false;
|
|
1614 |
|
|
1615 |
mag = abs(percent);
|
|
1616 |
if (mag >= 10) {
|
|
1617 |
SET_DPARAM16(3, mag);
|
|
1618 |
SET_DPARAM16(0,_cargoc.names_s[i->produced_cargo[j]]);
|
|
1619 |
SET_DPARAM16(1, i->town->index);
|
|
1620 |
SET_DPARAM16(2, i->type + STR_4802_COAL_MINE);
|
|
1621 |
AddNewsItem(percent>=0 ? STR_INDUSTRY_PROD_GOUP : STR_INDUSTRY_PROD_GODOWN,
|
|
1622 |
NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_TILE, NT_ECONOMY, 0),
|
|
1623 |
i->xy + TILE_XY(1,1), 0);
|
|
1624 |
}
|
|
1625 |
}
|
|
1626 |
}
|
|
1627 |
|
|
1628 |
if (closeit) {
|
|
1629 |
i->prod_level = 0;
|
|
1630 |
SET_DPARAM16(1, i->type + STR_4802_COAL_MINE);
|
|
1631 |
SET_DPARAM16(0, i->town->index);
|
|
1632 |
AddNewsItem(_industry_close_strings[i->type], NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_TILE, NT_ECONOMY, 0), i->xy + TILE_XY(1,1), 0);
|
|
1633 |
}
|
|
1634 |
}
|
|
1635 |
|
|
1636 |
|
|
1637 |
static void UpdateIndustryStatistics(Industry *i)
|
|
1638 |
{
|
|
1639 |
byte pct;
|
|
1640 |
|
|
1641 |
if (i->produced_cargo[0] != 0xFF) {
|
|
1642 |
pct = 0;
|
|
1643 |
if (i->last_mo_production[0] != 0) {
|
|
1644 |
i->last_prod_year = _cur_year;
|
|
1645 |
pct = min(i->last_mo_transported[0] * 256 / i->last_mo_production[0],255);
|
|
1646 |
}
|
|
1647 |
i->pct_transported[0] = pct;
|
|
1648 |
|
|
1649 |
i->total_production[0] = i->last_mo_production[0];
|
|
1650 |
i->last_mo_production[0] = 0;
|
|
1651 |
|
|
1652 |
i->total_transported[0] = i->last_mo_transported[0];
|
|
1653 |
i->last_mo_transported[0] = 0;
|
|
1654 |
}
|
|
1655 |
|
|
1656 |
if (i->produced_cargo[1] != 0xFF) {
|
|
1657 |
pct = 0;
|
|
1658 |
if (i->last_mo_production[1] != 0) {
|
|
1659 |
i->last_prod_year = _cur_year;
|
|
1660 |
pct = min(i->last_mo_transported[1] * 256 / i->last_mo_production[1],255);
|
|
1661 |
}
|
|
1662 |
i->pct_transported[1] = pct;
|
|
1663 |
|
|
1664 |
i->total_production[1] = i->last_mo_production[1];
|
|
1665 |
i->last_mo_production[1] = 0;
|
|
1666 |
|
|
1667 |
i->total_transported[1] = i->last_mo_transported[1];
|
|
1668 |
i->last_mo_transported[1] = 0;
|
|
1669 |
}
|
|
1670 |
|
|
1671 |
|
|
1672 |
if ( i->produced_cargo[0] != 0xFF || i->produced_cargo[1] != 0xFF )
|
|
1673 |
InvalidateWindow(WC_INDUSTRY_VIEW, i - _industries);
|
|
1674 |
|
|
1675 |
if (i->prod_level == 0)
|
|
1676 |
DeleteIndustry(i);
|
|
1677 |
else if (_patches.smooth_economy)
|
|
1678 |
ExtChangeIndustryProduction(i);
|
|
1679 |
}
|
|
1680 |
|
|
1681 |
static const byte _new_industry_rand[4][32] = {
|
|
1682 |
{12,12,12,12,12,12,12, 0, 0, 6, 6, 9, 9, 3, 3, 3,18,18, 4, 4, 2, 2, 5, 5, 5, 5, 5, 5, 1, 1, 8, 8},
|
|
1683 |
{16,16,16, 0, 0, 0, 9, 9, 9, 9,13,13, 3, 3, 3, 3,15,15,15, 4, 4,11,11,11,11,11,14,14, 1, 1, 7, 7},
|
|
1684 |
{21,21,21,24,22,22,22,22,23,23,12,12,12, 4, 4,19,19,19,13,13,20,20,20,11,11,11,17,17,17,10,10,10},
|
|
1685 |
{30,30,30,36,36,31,31,31,27,27,27,28,28,28,26,26,26,34,34,34,35,35,35,29,29,29,32,32,32,33,33,33},
|
|
1686 |
};
|
|
1687 |
|
|
1688 |
static void MaybeNewIndustry(uint32 r)
|
|
1689 |
{
|
|
1690 |
int type;
|
|
1691 |
int j;
|
|
1692 |
Industry *i;
|
|
1693 |
|
|
1694 |
type = _new_industry_rand[_opt.landscape][(r >> 16) & 0x1F];
|
|
1695 |
if (type == IT_OIL_WELL && _date > 10958)
|
|
1696 |
return;
|
|
1697 |
|
|
1698 |
if (type == IT_OIL_RIG && _date < 14610)
|
|
1699 |
return;
|
|
1700 |
|
|
1701 |
j = 2000;
|
|
1702 |
_current_player = OWNER_NONE;
|
|
1703 |
for(;;) {
|
|
1704 |
i = CreateNewIndustry(TILE_MASK(Random()), type);
|
|
1705 |
if (i != NULL)
|
|
1706 |
break;
|
|
1707 |
if (--j == 0)
|
|
1708 |
return;
|
|
1709 |
}
|
|
1710 |
|
|
1711 |
SET_DPARAM16(0, type + STR_4802_COAL_MINE);
|
|
1712 |
SET_DPARAM16(1, i->town->index);
|
|
1713 |
AddNewsItem( STR_482D_NEW_UNDER_CONSTRUCTION + (type == IT_FOREST), NEWS_FLAGS(NM_THIN,NF_VIEWPORT|NF_TILE,NT_ECONOMY,0), i->xy, 0);
|
|
1714 |
}
|
|
1715 |
|
|
1716 |
static void MaybeCloseIndustry(Industry *i)
|
|
1717 |
{
|
|
1718 |
uint32 r;
|
|
1719 |
StringID str;
|
|
1720 |
int type = i->type;
|
|
1721 |
|
|
1722 |
if (_industry_close_mode[type] == 1) {
|
|
1723 |
/* decrease or increase */
|
|
1724 |
if (type == IT_OIL_WELL && _opt.landscape == LT_NORMAL) goto decrease_production;
|
|
1725 |
if (CHANCE16I(1,3,r=Random())) {
|
|
1726 |
if ((i->pct_transported[0] > 153) ^ CHANCE16I(1,3,r>>16)) {
|
|
1727 |
|
|
1728 |
/* Increase production */
|
|
1729 |
if (i->prod_level != 0x80) {
|
|
1730 |
byte b;
|
|
1731 |
|
|
1732 |
i->prod_level <<= 1;
|
|
1733 |
|
|
1734 |
b = i->production_rate[0]*2;
|
|
1735 |
if (i->production_rate[0] >= 128) b=255;
|
|
1736 |
i->production_rate[0] = b;
|
|
1737 |
b = i->production_rate[1]*2;
|
|
1738 |
if (i->production_rate[1] >= 128) b=255;
|
|
1739 |
i->production_rate[1] = b;
|
|
1740 |
|
|
1741 |
str = _industry_prod_up_strings[type];
|
|
1742 |
goto add_news;
|
|
1743 |
}
|
|
1744 |
} else {
|
|
1745 |
decrease_production:
|
|
1746 |
|
|
1747 |
/* Decrease production */
|
|
1748 |
if (i->prod_level == 4) goto close_industry;
|
|
1749 |
i->prod_level>>=1;
|
|
1750 |
i->production_rate[0] = (i->production_rate[0]+1) >> 1;
|
|
1751 |
i->production_rate[1] = (i->production_rate[1]+1) >> 1;
|
|
1752 |
|
|
1753 |
str = _industry_prod_down_strings[type];
|
|
1754 |
goto add_news;
|
|
1755 |
}
|
|
1756 |
}
|
|
1757 |
} else if (_industry_close_mode[type] > 1) {
|
|
1758 |
/* maybe close */
|
|
1759 |
if ( (byte)(_cur_year - i->last_prod_year) >= 5 && CHANCE16(1,2)) {
|
|
1760 |
close_industry:
|
|
1761 |
i->prod_level = 0;
|
|
1762 |
str = _industry_close_strings[type];
|
|
1763 |
add_news:
|
|
1764 |
SET_DPARAM16(1, type + STR_4802_COAL_MINE);
|
|
1765 |
SET_DPARAM16(0, i->town->index);
|
|
1766 |
AddNewsItem(str, NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_TILE, NT_ECONOMY, 0), i->xy + TILE_XY(1,1), 0);
|
|
1767 |
}
|
|
1768 |
}
|
|
1769 |
}
|
|
1770 |
|
|
1771 |
void IndustryMonthlyLoop()
|
|
1772 |
{
|
|
1773 |
Industry *i;
|
|
1774 |
|
|
1775 |
for(i=_industries; i != endof(_industries); i++) {
|
|
1776 |
if (i->xy != 0)
|
|
1777 |
UpdateIndustryStatistics(i);
|
|
1778 |
}
|
|
1779 |
|
|
1780 |
i = DEREF_INDUSTRY(RandomRange(lengthof(_industries)));
|
|
1781 |
|
|
1782 |
if (i->xy == 0) {
|
|
1783 |
uint32 r;
|
|
1784 |
if (CHANCE16I(1,9,r=Random()))
|
|
1785 |
MaybeNewIndustry(r);
|
|
1786 |
} else if (!_patches.smooth_economy) {
|
|
1787 |
MaybeCloseIndustry(i);
|
|
1788 |
}
|
|
1789 |
// production-change
|
|
1790 |
_industry_sort_dirty = true;
|
|
1791 |
InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
|
|
1792 |
}
|
|
1793 |
|
|
1794 |
|
|
1795 |
void InitializeIndustries()
|
|
1796 |
{
|
|
1797 |
memset(_industries, 0, sizeof(_industries));
|
|
1798 |
_total_industries = 0;
|
|
1799 |
_industry_sort_dirty = true;
|
|
1800 |
}
|
|
1801 |
|
|
1802 |
const TileTypeProcs _tile_type_industry_procs = {
|
|
1803 |
DrawTile_Industry, /* draw_tile_proc */
|
|
1804 |
GetSlopeZ_Industry, /* get_slope_z_proc */
|
|
1805 |
ClearTile_Industry, /* clear_tile_proc */
|
|
1806 |
GetAcceptedCargo_Industry, /* get_accepted_cargo_proc */
|
|
1807 |
GetTileDesc_Industry, /* get_tile_desc_proc */
|
|
1808 |
GetTileTrackStatus_Industry,/* get_tile_track_status_proc */
|
|
1809 |
ClickTile_Industry, /* click_tile_proc */
|
|
1810 |
AnimateTile_Industry, /* animate_tile_proc */
|
|
1811 |
TileLoop_Industry, /* tile_loop_proc */
|
|
1812 |
ChangeTileOwner_Industry, /* change_tile_owner_proc */
|
|
1813 |
GetProducedCargo_Industry, /* get_produced_cargo_proc */
|
|
1814 |
NULL, /* vehicle_enter_tile_proc */
|
|
1815 |
NULL, /* vehicle_leave_tile_proc */
|
|
1816 |
};
|
|
1817 |
|
|
1818 |
static const byte _industry_desc[] = {
|
|
1819 |
SLE_VAR(Industry,xy, SLE_UINT16),
|
|
1820 |
SLE_VAR(Industry,width, SLE_UINT8),
|
|
1821 |
SLE_VAR(Industry,height, SLE_UINT8),
|
|
1822 |
SLE_REF(Industry,town, REF_TOWN),
|
|
1823 |
SLE_ARR(Industry,produced_cargo, SLE_UINT8, 2),
|
|
1824 |
SLE_ARR(Industry,cargo_waiting, SLE_UINT16, 2),
|
|
1825 |
SLE_ARR(Industry,production_rate, SLE_UINT8, 2),
|
|
1826 |
SLE_ARR(Industry,accepts_cargo, SLE_UINT8, 3),
|
|
1827 |
SLE_VAR(Industry,prod_level, SLE_UINT8),
|
|
1828 |
SLE_ARR(Industry,last_mo_production,SLE_UINT16, 2),
|
|
1829 |
SLE_ARR(Industry,last_mo_transported,SLE_UINT16, 2),
|
|
1830 |
SLE_ARR(Industry,pct_transported,SLE_UINT8, 2),
|
|
1831 |
SLE_ARR(Industry,total_production,SLE_UINT16, 2),
|
|
1832 |
SLE_ARR(Industry,total_transported,SLE_UINT16, 2),
|
|
1833 |
|
|
1834 |
SLE_VAR(Industry,counter, SLE_UINT16),
|
|
1835 |
|
|
1836 |
SLE_VAR(Industry,type, SLE_UINT8),
|
|
1837 |
SLE_VAR(Industry,owner, SLE_UINT8),
|
|
1838 |
SLE_VAR(Industry,color_map, SLE_UINT8),
|
|
1839 |
SLE_VAR(Industry,last_prod_year, SLE_UINT8),
|
|
1840 |
SLE_VAR(Industry,was_cargo_delivered,SLE_UINT8),
|
|
1841 |
|
|
1842 |
// reserve extra space in savegame here. (currently 32 bytes)
|
|
1843 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 4, 2, 255),
|
|
1844 |
|
|
1845 |
SLE_END()
|
|
1846 |
};
|
|
1847 |
|
|
1848 |
static void Save_INDY()
|
|
1849 |
{
|
|
1850 |
Industry *ind;
|
|
1851 |
int i;
|
|
1852 |
// Write the vehicles
|
|
1853 |
for(ind=_industries,i=0; i!=lengthof(_industries); ind++,i++) {
|
|
1854 |
if (ind->xy != 0) {
|
|
1855 |
SlSetArrayIndex(i);
|
|
1856 |
SlObject(ind, _industry_desc);
|
|
1857 |
}
|
|
1858 |
}
|
|
1859 |
}
|
|
1860 |
|
|
1861 |
static void Load_INDY()
|
|
1862 |
{
|
|
1863 |
int index;
|
|
1864 |
_total_industries = 0;
|
|
1865 |
while ((index = SlIterateArray()) != -1) {
|
|
1866 |
SlObject(DEREF_INDUSTRY(index), _industry_desc);
|
|
1867 |
if (index + 1 > _total_industries)
|
|
1868 |
_total_industries = index + 1;
|
|
1869 |
}
|
|
1870 |
}
|
|
1871 |
|
|
1872 |
const ChunkHandler _industry_chunk_handlers[] = {
|
|
1873 |
{ 'INDY', Save_INDY, Load_INDY, CH_ARRAY | CH_LAST},
|
|
1874 |
};
|
|
1875 |
|