forked from OpenTTD/OpenTTD
/
industry_cmd.cpp
2762 lines (2354 loc) · 92.4 KB
/
industry_cmd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file industry_cmd.cpp Handling of industry tiles. */
#include "stdafx.h"
#include "clear_map.h"
#include "industry.h"
#include "station_base.h"
#include "train.h"
#include "viewport_func.h"
#include "command_func.h"
#include "town.h"
#include "news_func.h"
#include "cheat_type.h"
#include "genworld.h"
#include "tree_map.h"
#include "newgrf_cargo.h"
#include "newgrf_debug.h"
#include "newgrf_industrytiles.h"
#include "autoslope.h"
#include "water.h"
#include "strings_func.h"
#include "window_func.h"
#include "date_func.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "animated_tile_func.h"
#include "effectvehicle_func.h"
#include "effectvehicle_base.h"
#include "ai/ai.hpp"
#include "core/pool_func.hpp"
#include "subsidy_func.h"
#include "core/backup_type.hpp"
#include "object_base.h"
#include "table/strings.h"
#include "table/industry_land.h"
#include "table/build_industry.h"
IndustryPool _industry_pool("Industry");
INSTANTIATE_POOL_METHODS(Industry)
void ShowIndustryViewWindow(int industry);
void BuildOilRig(TileIndex tile);
static byte _industry_sound_ctr;
static TileIndex _industry_sound_tile;
uint16 Industry::counts[NUM_INDUSTRYTYPES];
IndustrySpec _industry_specs[NUM_INDUSTRYTYPES];
IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
IndustryBuildData _industry_builder; ///< In-game manager of industries.
/**
* This function initialize the spec arrays of both
* industry and industry tiles.
* It adjusts the enabling of the industry too, based on climate availability.
* This will allow for clearer testings
*/
void ResetIndustries()
{
memset(&_industry_specs, 0, sizeof(_industry_specs));
memcpy(&_industry_specs, &_origin_industry_specs, sizeof(_origin_industry_specs));
/* once performed, enable only the current climate industries */
for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
_industry_specs[i].enabled = i < NEW_INDUSTRYOFFSET &&
HasBit(_origin_industry_specs[i].climate_availability, _settings_game.game_creation.landscape);
}
memset(&_industry_tile_specs, 0, sizeof(_industry_tile_specs));
memcpy(&_industry_tile_specs, &_origin_industry_tile_specs, sizeof(_origin_industry_tile_specs));
/* Reset any overrides that have been set. */
_industile_mngr.ResetOverride();
_industry_mngr.ResetOverride();
}
/**
* Retrieve the type for this industry. Although it is accessed by a tile,
* it will return the general type of industry, and not the sprite index
* as would do GetIndustryGfx.
* @param tile that is queried
* @pre IsTileType(tile, MP_INDUSTRY)
* @return general type for this industry, as defined in industry.h
*/
IndustryType GetIndustryType(TileIndex tile)
{
assert(IsTileType(tile, MP_INDUSTRY));
const Industry *ind = Industry::GetByTile(tile);
assert(ind != NULL);
return ind->type;
}
/**
* Accessor for array _industry_specs.
* This will ensure at once : proper access and
* not allowing modifications of it.
* @param thistype of industry (which is the index in _industry_specs)
* @pre thistype < NUM_INDUSTRYTYPES
* @return a pointer to the corresponding industry spec
*/
const IndustrySpec *GetIndustrySpec(IndustryType thistype)
{
assert(thistype < NUM_INDUSTRYTYPES);
return &_industry_specs[thistype];
}
/**
* Accessor for array _industry_tile_specs.
* This will ensure at once : proper access and
* not allowing modifications of it.
* @param gfx of industrytile (which is the index in _industry_tile_specs)
* @pre gfx < INVALID_INDUSTRYTILE
* @return a pointer to the corresponding industrytile spec
*/
const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
{
assert(gfx < INVALID_INDUSTRYTILE);
return &_industry_tile_specs[gfx];
}
Industry::~Industry()
{
if (CleaningPool()) return;
/* Industry can also be destroyed when not fully initialized.
* This means that we do not have to clear tiles either.
* Also we must not decrement industry counts in that case. */
if (this->location.w == 0) return;
TILE_AREA_LOOP(tile_cur, this->location) {
if (IsTileType(tile_cur, MP_INDUSTRY)) {
if (GetIndustryIndex(tile_cur) == this->index) {
DeleteNewGRFInspectWindow(GSF_INDUSTRYTILES, tile_cur);
/* MakeWaterKeepingClass() can also handle 'land' */
MakeWaterKeepingClass(tile_cur, OWNER_NONE);
}
} else if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
DeleteOilRig(tile_cur);
}
}
if (GetIndustrySpec(this->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) {
TileArea ta(this->location.tile - TileDiffXY(min(TileX(this->location.tile), 21), min(TileY(this->location.tile), 21)), 42, 42);
ta.ClampToMap();
/* Remove the farmland and convert it to regular tiles over time. */
TILE_AREA_LOOP(tile_cur, ta) {
if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
GetIndustryIndexOfField(tile_cur) == this->index) {
SetIndustryIndexOfField(tile_cur, INVALID_INDUSTRY);
}
}
}
/* don't let any disaster vehicle target invalid industry */
ReleaseDisastersTargetingIndustry(this->index);
/* Clear the persistent storage. */
delete this->psa;
DecIndustryTypeCount(this->type);
DeleteIndustryNews(this->index);
DeleteWindowById(WC_INDUSTRY_VIEW, this->index);
DeleteNewGRFInspectWindow(GSF_INDUSTRIES, this->index);
DeleteSubsidyWith(ST_INDUSTRY, this->index);
CargoPacket::InvalidateAllFrom(ST_INDUSTRY, this->index);
}
/**
* Invalidating some stuff after removing item from the pool.
* @param index index of deleted item
*/
void Industry::PostDestructor(size_t index)
{
InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
Station::RecomputeIndustriesNearForAll();
}
/**
* Return a random valid industry.
* @return random industry, NULL if there are no industries
*/
/* static */ Industry *Industry::GetRandom()
{
if (Industry::GetNumItems() == 0) return NULL;
int num = RandomRange((uint16)Industry::GetNumItems());
size_t index = MAX_UVALUE(size_t);
while (num >= 0) {
num--;
index++;
/* Make sure we have a valid industry */
while (!Industry::IsValidID(index)) {
index++;
assert(index < Industry::GetPoolSize());
}
}
return Industry::Get(index);
}
static void IndustryDrawSugarMine(const TileInfo *ti)
{
if (!IsIndustryCompleted(ti->tile)) return;
const DrawIndustryAnimationStruct *d = &_draw_industry_spec1[GetAnimationFrame(ti->tile)];
AddChildSpriteScreen(SPR_IT_SUGAR_MINE_SIEVE + d->image_1, PAL_NONE, d->x, 0);
if (d->image_2 != 0) {
AddChildSpriteScreen(SPR_IT_SUGAR_MINE_CLOUDS + d->image_2 - 1, PAL_NONE, 8, 41);
}
if (d->image_3 != 0) {
AddChildSpriteScreen(SPR_IT_SUGAR_MINE_PILE + d->image_3 - 1, PAL_NONE,
_drawtile_proc1[d->image_3 - 1].x, _drawtile_proc1[d->image_3 - 1].y);
}
}
static void IndustryDrawToffeeQuarry(const TileInfo *ti)
{
uint8 x = 0;
if (IsIndustryCompleted(ti->tile)) {
x = _industry_anim_offs_toffee[GetAnimationFrame(ti->tile)];
if (x == 0xFF) {
x = 0;
}
}
AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_SHOVEL, PAL_NONE, 22 - x, 24 + x);
AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_TOFFEE, PAL_NONE, 6, 14);
}
static void IndustryDrawBubbleGenerator( const TileInfo *ti)
{
if (IsIndustryCompleted(ti->tile)) {
AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_BUBBLE, PAL_NONE, 5, _industry_anim_offs_bubbles[GetAnimationFrame(ti->tile)]);
} else {
AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_SPRING, PAL_NONE, 3, 67);
}
}
static void IndustryDrawToyFactory(const TileInfo *ti)
{
const DrawIndustryAnimationStruct *d = &_industry_anim_offs_toys[GetAnimationFrame(ti->tile)];
if (d->image_1 != 0xFF) {
AddChildSpriteScreen(SPR_IT_TOY_FACTORY_CLAY, PAL_NONE, d->x, 96 + d->image_1);
}
if (d->image_2 != 0xFF) {
AddChildSpriteScreen(SPR_IT_TOY_FACTORY_ROBOT, PAL_NONE, 16 - d->image_2 * 2, 100 + d->image_2);
}
AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP, PAL_NONE, 7, d->image_3);
AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP_HOLDER, PAL_NONE, 0, 42);
}
static void IndustryDrawCoalPlantSparks(const TileInfo *ti)
{
if (IsIndustryCompleted(ti->tile)) {
uint8 image = GetAnimationFrame(ti->tile);
if (image != 0 && image < 7) {
AddChildSpriteScreen(image + SPR_IT_POWER_PLANT_TRANSFORMERS,
PAL_NONE,
_coal_plant_sparks[image - 1].x,
_coal_plant_sparks[image - 1].y
);
}
}
}
typedef void IndustryDrawTileProc(const TileInfo *ti);
static IndustryDrawTileProc * const _industry_draw_tile_procs[5] = {
IndustryDrawSugarMine,
IndustryDrawToffeeQuarry,
IndustryDrawBubbleGenerator,
IndustryDrawToyFactory,
IndustryDrawCoalPlantSparks,
};
static void DrawTile_Industry(TileInfo *ti)
{
IndustryGfx gfx = GetIndustryGfx(ti->tile);
Industry *ind = Industry::GetByTile(ti->tile);
const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
/* Retrieve pointer to the draw industry tile struct */
if (gfx >= NEW_INDUSTRYTILEOFFSET) {
/* Draw the tile using the specialized method of newgrf industrytile.
* DrawNewIndustry will return false if ever the resolver could not
* find any sprite to display. So in this case, we will jump on the
* substitute gfx instead. */
if (indts->grf_prop.spritegroup[0] != NULL && DrawNewIndustryTile(ti, ind, gfx, indts)) {
return;
} else {
/* No sprite group (or no valid one) found, meaning no graphics associated.
* Use the substitute one instead */
if (indts->grf_prop.subst_id != INVALID_INDUSTRYTILE) {
gfx = indts->grf_prop.subst_id;
/* And point the industrytile spec accordingly */
indts = GetIndustryTileSpec(gfx);
}
}
}
const DrawBuildingsTileStruct *dits = &_industry_draw_tile_data[gfx << 2 | (indts->anim_state ?
GetAnimationFrame(ti->tile) & INDUSTRY_COMPLETED :
GetIndustryConstructionStage(ti->tile))];
SpriteID image = dits->ground.sprite;
/* DrawFoundation() modifes ti->z and ti->tileh */
if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
/* If the ground sprite is the default flat water sprite, draw also canal/river borders.
* Do not do this if the tile's WaterClass is 'land'. */
if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
DrawWaterClassGround(ti);
} else {
DrawGroundSprite(image, GroundSpritePaletteTransform(image, dits->ground.pal, GENERAL_SPRITE_COLOUR(ind->random_colour)));
}
/* If industries are transparent and invisible, do not draw the upper part */
if (IsInvisibilitySet(TO_INDUSTRIES)) return;
/* Add industry on top of the ground? */
image = dits->building.sprite;
if (image != 0) {
AddSortableSpriteToDraw(image, SpriteLayoutPaletteTransform(image, dits->building.pal, GENERAL_SPRITE_COLOUR(ind->random_colour)),
ti->x + dits->subtile_x,
ti->y + dits->subtile_y,
dits->width,
dits->height,
dits->dz,
ti->z,
IsTransparencySet(TO_INDUSTRIES));
if (IsTransparencySet(TO_INDUSTRIES)) return;
}
{
int proc = dits->draw_proc - 1;
if (proc >= 0) _industry_draw_tile_procs[proc](ti);
}
}
static int GetSlopePixelZ_Industry(TileIndex tile, uint x, uint y)
{
return GetTileMaxPixelZ(tile);
}
static Foundation GetFoundation_Industry(TileIndex tile, Slope tileh)
{
IndustryGfx gfx = GetIndustryGfx(tile);
/* For NewGRF industry tiles we might not be drawing a foundation. We need to
* account for this, as other structures should
* draw the wall of the foundation in this case.
*/
if (gfx >= NEW_INDUSTRYTILEOFFSET) {
const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
if (indts->grf_prop.spritegroup[0] != NULL && HasBit(indts->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, Industry::GetByTile(tile), tile);
if (callback_res == 0) return FOUNDATION_NONE;
}
}
return FlatteningFoundation(tileh);
}
static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
{
IndustryGfx gfx = GetIndustryGfx(tile);
const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
/* When we have to use a callback, we put our data in the next two variables */
CargoID raw_accepts_cargo[lengthof(itspec->accepts_cargo)];
uint8 raw_cargo_acceptance[lengthof(itspec->acceptance)];
/* And then these will always point to a same sized array with the required data */
const CargoID *accepts_cargo = itspec->accepts_cargo;
const uint8 *cargo_acceptance = itspec->acceptance;
if (HasBit(itspec->callback_mask, CBM_INDT_ACCEPT_CARGO)) {
uint16 res = GetIndustryTileCallback(CBID_INDTILE_ACCEPT_CARGO, 0, 0, gfx, Industry::GetByTile(tile), tile);
if (res != CALLBACK_FAILED) {
accepts_cargo = raw_accepts_cargo;
for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_accepts_cargo[i] = GetCargoTranslation(GB(res, i * 5, 5), itspec->grf_prop.grffile);
}
}
if (HasBit(itspec->callback_mask, CBM_INDT_CARGO_ACCEPTANCE)) {
uint16 res = GetIndustryTileCallback(CBID_INDTILE_CARGO_ACCEPTANCE, 0, 0, gfx, Industry::GetByTile(tile), tile);
if (res != CALLBACK_FAILED) {
cargo_acceptance = raw_cargo_acceptance;
for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_cargo_acceptance[i] = GB(res, i * 4, 4);
}
}
const Industry *ind = Industry::GetByTile(tile);
for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) {
CargoID a = accepts_cargo[i];
if (a == CT_INVALID || cargo_acceptance[i] == 0) continue; // work only with valid cargos
/* Add accepted cargo */
acceptance[a] += cargo_acceptance[i];
/* Maybe set 'always accepted' bit (if it's not set already) */
if (HasBit(*always_accepted, a)) continue;
bool accepts = false;
for (uint cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
/* Test whether the industry itself accepts the cargo type */
if (ind->accepts_cargo[cargo_index] == a) {
accepts = true;
break;
}
}
if (accepts) continue;
/* If the industry itself doesn't accept this cargo, set 'always accepted' bit */
SetBit(*always_accepted, a);
}
}
static void GetTileDesc_Industry(TileIndex tile, TileDesc *td)
{
const Industry *i = Industry::GetByTile(tile);
const IndustrySpec *is = GetIndustrySpec(i->type);
td->owner[0] = i->owner;
td->str = is->name;
if (!IsIndustryCompleted(tile)) {
SetDParamX(td->dparam, 0, td->str);
td->str = STR_LAI_TOWN_INDUSTRY_DESCRIPTION_UNDER_CONSTRUCTION;
}
if (is->grf_prop.grffile != NULL) {
td->grf = GetGRFConfig(is->grf_prop.grffile->grfid)->GetName();
}
}
static CommandCost ClearTile_Industry(TileIndex tile, DoCommandFlag flags)
{
Industry *i = Industry::GetByTile(tile);
const IndustrySpec *indspec = GetIndustrySpec(i->type);
/* water can destroy industries
* in editor you can bulldoze industries
* with magic_bulldozer cheat you can destroy industries
* (area around OILRIG is water, so water shouldn't flood it
*/
if ((_current_company != OWNER_WATER && _game_mode != GM_EDITOR &&
!_cheats.magic_bulldozer.value) ||
((flags & DC_AUTO) != 0) ||
(_current_company == OWNER_WATER &&
((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) ||
HasBit(GetIndustryTileSpec(GetIndustryGfx(tile))->slopes_refused, 5)))) {
SetDParam(1, indspec->name);
return_cmd_error(flags & DC_AUTO ? STR_ERROR_GENERIC_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
}
if (flags & DC_EXEC) {
AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
delete i;
}
return CommandCost(EXPENSES_CONSTRUCTION, indspec->GetRemovalCost());
}
static void TransportIndustryGoods(TileIndex tile)
{
Industry *i = Industry::GetByTile(tile);
const IndustrySpec *indspec = GetIndustrySpec(i->type);
bool moved_cargo = false;
StationFinder stations(i->location);
for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
uint cw = min(i->produced_cargo_waiting[j], 255);
if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) {
i->produced_cargo_waiting[j] -= cw;
/* fluctuating economy? */
if (EconomyIsInRecession()) cw = (cw + 1) / 2;
i->this_month_production[j] += cw;
uint am = MoveGoodsToStation(i->produced_cargo[j], cw, ST_INDUSTRY, i->index, stations.GetStations());
i->this_month_transported[j] += am;
moved_cargo |= (am != 0);
}
}
if (moved_cargo && !StartStopIndustryTileAnimation(i, IAT_INDUSTRY_DISTRIBUTES_CARGO)) {
uint newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_production;
if (newgfx != INDUSTRYTILE_NOANIM) {
ResetIndustryConstructionStage(tile);
SetIndustryCompleted(tile, true);
SetIndustryGfx(tile, newgfx);
MarkTileDirtyByTile(tile);
}
}
}
static void AnimateTile_Industry(TileIndex tile)
{
IndustryGfx gfx = GetIndustryGfx(tile);
if (GetIndustryTileSpec(gfx)->animation.status != ANIM_STATUS_NO_ANIMATION) {
AnimateNewIndustryTile(tile);
return;
}
switch (gfx) {
case GFX_SUGAR_MINE_SIEVE:
if ((_tick_counter & 1) == 0) {
byte m = GetAnimationFrame(tile) + 1;
switch (m & 7) {
case 2: SndPlayTileFx(SND_2D_RIP_2, tile); break;
case 6: SndPlayTileFx(SND_29_RIP, tile); break;
}
if (m >= 96) {
m = 0;
DeleteAnimatedTile(tile);
}
SetAnimationFrame(tile, m);
MarkTileDirtyByTile(tile);
}
break;
case GFX_TOFFEE_QUARY:
if ((_tick_counter & 3) == 0) {
byte m = GetAnimationFrame(tile);
if (_industry_anim_offs_toffee[m] == 0xFF) {
SndPlayTileFx(SND_30_CARTOON_SOUND, tile);
}
if (++m >= 70) {
m = 0;
DeleteAnimatedTile(tile);
}
SetAnimationFrame(tile, m);
MarkTileDirtyByTile(tile);
}
break;
case GFX_BUBBLE_CATCHER:
if ((_tick_counter & 1) == 0) {
byte m = GetAnimationFrame(tile);
if (++m >= 40) {
m = 0;
DeleteAnimatedTile(tile);
}
SetAnimationFrame(tile, m);
MarkTileDirtyByTile(tile);
}
break;
/* Sparks on a coal plant */
case GFX_POWERPLANT_SPARKS:
if ((_tick_counter & 3) == 0) {
byte m = GetAnimationFrame(tile);
if (m == 6) {
SetAnimationFrame(tile, 0);
DeleteAnimatedTile(tile);
} else {
SetAnimationFrame(tile, m + 1);
MarkTileDirtyByTile(tile);
}
}
break;
case GFX_TOY_FACTORY:
if ((_tick_counter & 1) == 0) {
byte m = GetAnimationFrame(tile) + 1;
switch (m) {
case 1: SndPlayTileFx(SND_2C_MACHINERY, tile); break;
case 23: SndPlayTileFx(SND_2B_COMEDY_HIT, tile); break;
case 28: SndPlayTileFx(SND_2A_EXTRACT_AND_POP, tile); break;
default:
if (m >= 50) {
int n = GetIndustryAnimationLoop(tile) + 1;
m = 0;
if (n >= 8) {
n = 0;
DeleteAnimatedTile(tile);
}
SetIndustryAnimationLoop(tile, n);
}
}
SetAnimationFrame(tile, m);
MarkTileDirtyByTile(tile);
}
break;
case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
if ((_tick_counter & 3) == 0) {
IndustryGfx gfx = GetIndustryGfx(tile);
gfx = (gfx < 155) ? gfx + 1 : 148;
SetIndustryGfx(tile, gfx);
MarkTileDirtyByTile(tile);
}
break;
case GFX_OILWELL_ANIMATED_1:
case GFX_OILWELL_ANIMATED_2:
case GFX_OILWELL_ANIMATED_3:
if ((_tick_counter & 7) == 0) {
bool b = Chance16(1, 7);
IndustryGfx gfx = GetIndustryGfx(tile);
byte m = GetAnimationFrame(tile) + 1;
if (m == 4 && (m = 0, ++gfx) == GFX_OILWELL_ANIMATED_3 + 1 && (gfx = GFX_OILWELL_ANIMATED_1, b)) {
SetIndustryGfx(tile, GFX_OILWELL_NOT_ANIMATED);
SetIndustryConstructionStage(tile, 3);
DeleteAnimatedTile(tile);
} else {
SetAnimationFrame(tile, m);
SetIndustryGfx(tile, gfx);
MarkTileDirtyByTile(tile);
}
}
break;
case GFX_COAL_MINE_TOWER_ANIMATED:
case GFX_COPPER_MINE_TOWER_ANIMATED:
case GFX_GOLD_MINE_TOWER_ANIMATED: {
int state = _tick_counter & 0x7FF;
if ((state -= 0x400) < 0) return;
if (state < 0x1A0) {
if (state < 0x20 || state >= 0x180) {
byte m = GetAnimationFrame(tile);
if (!(m & 0x40)) {
SetAnimationFrame(tile, m | 0x40);
SndPlayTileFx(SND_0B_MINING_MACHINERY, tile);
}
if (state & 7) return;
} else {
if (state & 3) return;
}
byte m = (GetAnimationFrame(tile) + 1) | 0x40;
if (m > 0xC2) m = 0xC0;
SetAnimationFrame(tile, m);
MarkTileDirtyByTile(tile);
} else if (state >= 0x200 && state < 0x3A0) {
int i = (state < 0x220 || state >= 0x380) ? 7 : 3;
if (state & i) return;
byte m = (GetAnimationFrame(tile) & 0xBF) - 1;
if (m < 0x80) m = 0x82;
SetAnimationFrame(tile, m);
MarkTileDirtyByTile(tile);
}
break;
}
}
}
static void CreateChimneySmoke(TileIndex tile)
{
uint x = TileX(tile) * TILE_SIZE;
uint y = TileY(tile) * TILE_SIZE;
int z = GetTileMaxPixelZ(tile);
CreateEffectVehicle(x + 15, y + 14, z + 59, EV_CHIMNEY_SMOKE);
}
static void MakeIndustryTileBigger(TileIndex tile)
{
byte cnt = GetIndustryConstructionCounter(tile) + 1;
if (cnt != 4) {
SetIndustryConstructionCounter(tile, cnt);
return;
}
byte stage = GetIndustryConstructionStage(tile) + 1;
SetIndustryConstructionCounter(tile, 0);
SetIndustryConstructionStage(tile, stage);
StartStopIndustryTileAnimation(tile, IAT_CONSTRUCTION_STATE_CHANGE);
if (stage == INDUSTRY_COMPLETED) SetIndustryCompleted(tile, true);
MarkTileDirtyByTile(tile);
if (!IsIndustryCompleted(tile)) return;
IndustryGfx gfx = GetIndustryGfx(tile);
if (gfx >= NEW_INDUSTRYTILEOFFSET) {
/* New industries are already animated on construction. */
return;
}
switch (gfx) {
case GFX_POWERPLANT_CHIMNEY:
CreateChimneySmoke(tile);
break;
case GFX_OILRIG_1: {
/* Do not require an industry tile to be after the first two GFX_OILRIG_1
* tiles (like the default oil rig). Do a proper check to ensure the
* tiles belong to the same industry and based on that build the oil rig's
* station. */
TileIndex other = tile + TileDiffXY(0, 1);
if (IsTileType(other, MP_INDUSTRY) &&
GetIndustryGfx(other) == GFX_OILRIG_1 &&
GetIndustryIndex(tile) == GetIndustryIndex(other)) {
BuildOilRig(tile);
}
break;
}
case GFX_TOY_FACTORY:
case GFX_BUBBLE_CATCHER:
case GFX_TOFFEE_QUARY:
SetAnimationFrame(tile, 0);
SetIndustryAnimationLoop(tile, 0);
break;
case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
AddAnimatedTile(tile);
break;
}
}
static void TileLoopIndustry_BubbleGenerator(TileIndex tile)
{
static const int8 _bubble_spawn_location[3][4] = {
{ 11, 0, -4, -14 },
{ -4, -10, -4, 1 },
{ 49, 59, 60, 65 },
};
SndPlayTileFx(SND_2E_EXTRACT_AND_POP, tile);
int dir = Random() & 3;
EffectVehicle *v = CreateEffectVehicleAbove(
TileX(tile) * TILE_SIZE + _bubble_spawn_location[0][dir],
TileY(tile) * TILE_SIZE + _bubble_spawn_location[1][dir],
_bubble_spawn_location[2][dir],
EV_BUBBLE
);
if (v != NULL) v->animation_substate = dir;
}
static void TileLoop_Industry(TileIndex tile)
{
if (IsTileOnWater(tile)) TileLoop_Water(tile);
/* Normally this doesn't happen, but if an industry NewGRF is removed
* an industry that was previously build on water can now be flooded.
* If this happens the tile is no longer an industry tile after
* returning from TileLoop_Water. */
if (!IsTileType(tile, MP_INDUSTRY)) return;
TriggerIndustryTile(tile, INDTILE_TRIGGER_TILE_LOOP);
if (!IsIndustryCompleted(tile)) {
MakeIndustryTileBigger(tile);
return;
}
if (_game_mode == GM_EDITOR) return;
TransportIndustryGoods(tile);
if (StartStopIndustryTileAnimation(tile, IAT_TILELOOP)) return;
IndustryGfx newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
if (newgfx != INDUSTRYTILE_NOANIM) {
ResetIndustryConstructionStage(tile);
SetIndustryGfx(tile, newgfx);
MarkTileDirtyByTile(tile);
return;
}
IndustryGfx gfx = GetIndustryGfx(tile);
switch (gfx) {
case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
switch (gfx) {
case GFX_COAL_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COAL_MINE_TOWER_ANIMATED; break;
case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
case GFX_GOLD_MINE_TOWER_NOT_ANIMATED: gfx = GFX_GOLD_MINE_TOWER_ANIMATED; break;
}
SetIndustryGfx(tile, gfx);
SetAnimationFrame(tile, 0x80);
AddAnimatedTile(tile);
}
break;
case GFX_OILWELL_NOT_ANIMATED:
if (Chance16(1, 6)) {
SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1);
SetAnimationFrame(tile, 0);
AddAnimatedTile(tile);
}
break;
case GFX_COAL_MINE_TOWER_ANIMATED:
case GFX_COPPER_MINE_TOWER_ANIMATED:
case GFX_GOLD_MINE_TOWER_ANIMATED:
if (!(_tick_counter & 0x400)) {
switch (gfx) {
case GFX_COAL_MINE_TOWER_ANIMATED: gfx = GFX_COAL_MINE_TOWER_NOT_ANIMATED; break;
case GFX_COPPER_MINE_TOWER_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_NOT_ANIMATED; break;
case GFX_GOLD_MINE_TOWER_ANIMATED: gfx = GFX_GOLD_MINE_TOWER_NOT_ANIMATED; break;
}
SetIndustryGfx(tile, gfx);
SetIndustryCompleted(tile, true);
SetIndustryConstructionStage(tile, 3);
DeleteAnimatedTile(tile);
}
break;
case GFX_POWERPLANT_SPARKS:
if (Chance16(1, 3)) {
SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
AddAnimatedTile(tile);
}
break;
case GFX_COPPER_MINE_CHIMNEY:
CreateEffectVehicleAbove(TileX(tile) * TILE_SIZE + 6, TileY(tile) * TILE_SIZE + 6, 43, EV_COPPER_MINE_SMOKE);
break;
case GFX_TOY_FACTORY: {
Industry *i = Industry::GetByTile(tile);
if (i->was_cargo_delivered) {
i->was_cargo_delivered = false;
SetIndustryAnimationLoop(tile, 0);
AddAnimatedTile(tile);
}
}
break;
case GFX_BUBBLE_GENERATOR:
TileLoopIndustry_BubbleGenerator(tile);
break;
case GFX_TOFFEE_QUARY:
AddAnimatedTile(tile);
break;
case GFX_SUGAR_MINE_SIEVE:
if (Chance16(1, 3)) AddAnimatedTile(tile);
break;
}
}
static bool ClickTile_Industry(TileIndex tile)
{
ShowIndustryViewWindow(GetIndustryIndex(tile));
return true;
}
static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
{
return 0;
}
static void ChangeTileOwner_Industry(TileIndex tile, Owner old_owner, Owner new_owner)
{
/* If the founder merges, the industry was created by the merged company */
Industry *i = Industry::GetByTile(tile);
if (i->founder == old_owner) i->founder = (new_owner == INVALID_OWNER) ? OWNER_NONE : new_owner;
}
/**
* Check whether the tile is a forest.
* @param tile the tile to investigate.
* @return true if and only if the tile is a forest
*/
bool IsTileForestIndustry(TileIndex tile)
{
/* Check for industry tile */
if (!IsTileType(tile, MP_INDUSTRY)) return false;
const Industry *ind = Industry::GetByTile(tile);
/* Check for organic industry (i.e. not processing or extractive) */
if ((GetIndustrySpec(ind->type)->life_type & INDUSTRYLIFE_ORGANIC) == 0) return false;
/* Check for wood production */
for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
/* The industry produces wood. */
if (ind->produced_cargo[i] != CT_INVALID && CargoSpec::Get(ind->produced_cargo[i])->label == 'WOOD') return true;
}
return false;
}
static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
static bool IsBadFarmFieldTile(TileIndex tile)
{
switch (GetTileType(tile)) {
case MP_CLEAR: return IsClearGround(tile, CLEAR_FIELDS) || IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
default: return true;
}
}
static bool IsBadFarmFieldTile2(TileIndex tile)
{
switch (GetTileType(tile)) {
case MP_CLEAR: return IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
default: return true;
}
}
static void SetupFarmFieldFence(TileIndex tile, int size, byte type, Axis direction)
{
do {
tile = TILE_MASK(tile);
if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
byte or_ = type;
if (or_ == 1 && Chance16(1, 7)) or_ = 2;
if (direction == AXIS_X) {
SetFenceSE(tile, or_);
} else {
SetFenceSW(tile, or_);
}
}
tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
} while (--size);
}
static void PlantFarmField(TileIndex tile, IndustryID industry)
{
if (_settings_game.game_creation.landscape == LT_ARCTIC) {
if (GetTileZ(tile) + 2 >= GetSnowLine()) return;
}
/* determine field size */
uint32 r = (Random() & 0x303) + 0x404;
if (_settings_game.game_creation.landscape == LT_ARCTIC) r += 0x404;
uint size_x = GB(r, 0, 8);
uint size_y = GB(r, 8, 8);
TileArea ta(tile - TileDiffXY(min(TileX(tile), size_x / 2), min(TileY(tile), size_y / 2)), size_x, size_y);
ta.ClampToMap();
if (ta.w == 0 || ta.h == 0) return;
/* check the amount of bad tiles */
int count = 0;
TILE_AREA_LOOP(cur_tile, ta) {
assert(cur_tile < MapSize());
count += IsBadFarmFieldTile(cur_tile);