forked from OpenTTD/OpenTTD
/
industry_gui.cpp
2640 lines (2328 loc) · 96.7 KB
/
industry_gui.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_gui.cpp GUIs related to industries. */
#include "stdafx.h"
#include "gui.h"
#include "sound_func.h"
#include "window_func.h"
#include "textbuf_gui.h"
#include "command_func.h"
#include "viewport_func.h"
#include "gfx_func.h"
#include "industry.h"
#include "town.h"
#include "cheat_type.h"
#include "newgrf_industries.h"
#include "newgrf_text.h"
#include "newgrf_debug.h"
#include "strings_func.h"
#include "company_func.h"
#include "tilehighlight_func.h"
#include "string_func.h"
#include "sortlist_type.h"
#include "widgets/dropdown_func.h"
#include "company_base.h"
#include "core/geometry_func.hpp"
#include "core/random_func.hpp"
#include "core/backup_type.hpp"
#include "genworld.h"
#include "smallmap_gui.h"
#include "table/strings.h"
#include "table/sprites.h"
bool _ignore_restrictions;
uint64 _displayed_industries; ///< Communication from the industry chain window to the smallmap window about what industries to display.
assert_compile(NUM_INDUSTRYTYPES <= 64); // Make sure all industry types fit in _displayed_industries.
/** Cargo suffix type (for which window is it requested) */
enum CargoSuffixType {
CST_FUND, ///< Fund-industry window
CST_VIEW, ///< View-industry window
CST_DIR, ///< Industry-directory window
};
static void ShowIndustryCargoesWindow(IndustryType id);
/**
* Gets the string to display after the cargo name (using callback 37)
* @param cargo the cargo for which the suffix is requested
* - 00 - first accepted cargo type
* - 01 - second accepted cargo type
* - 02 - third accepted cargo type
* - 03 - first produced cargo type
* - 04 - second produced cargo type
* @param cst the cargo suffix type (for which window is it requested). @see CargoSuffixType
* @param ind the industry (NULL if in fund window)
* @param ind_type the industry type
* @param indspec the industry spec
* @param suffix is filled with the string to display
* @param suffix_last lastof(suffix)
*/
static void GetCargoSuffix(uint cargo, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, char *suffix, const char *suffix_last)
{
suffix[0] = '\0';
if (HasBit(indspec->callback_mask, CBM_IND_CARGO_SUFFIX)) {
uint16 callback = GetIndustryCallback(CBID_INDUSTRY_CARGO_SUFFIX, 0, (cst << 8) | cargo, const_cast<Industry *>(ind), ind_type, (cst != CST_FUND) ? ind->location.tile : INVALID_TILE);
if (callback == CALLBACK_FAILED || callback == 0x400) return;
if (callback > 0x400) {
ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_CARGO_SUFFIX, callback);
} else if (indspec->grf_prop.grffile->grf_version >= 8 || GB(callback, 0, 8) != 0xFF) {
StartTextRefStackUsage(6);
GetString(suffix, GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + callback), suffix_last);
StopTextRefStackUsage();
}
}
}
/**
* Gets all strings to display after the cargos of industries (using callback 37)
* @param cb_offset The offset for the cargo used in cb37, 0 for accepted cargos, 3 for produced cargos
* @param cst the cargo suffix type (for which window is it requested). @see CargoSuffixType
* @param ind the industry (NULL if in fund window)
* @param ind_type the industry type
* @param indspec the industry spec
* @param cargos array with cargotypes. for CT_INVALID no suffix will be determined
* @param suffixes is filled with the suffixes
*/
template <typename TC, typename TS>
static inline void GetAllCargoSuffixes(uint cb_offset, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, const TC &cargos, TS &suffixes)
{
assert_compile(lengthof(cargos) <= lengthof(suffixes));
for (uint j = 0; j < lengthof(cargos); j++) {
if (cargos[j] != CT_INVALID) {
GetCargoSuffix(cb_offset + j, cst, ind, ind_type, indspec, suffixes[j], lastof(suffixes[j]));
} else {
suffixes[j][0] = '\0';
}
}
}
IndustryType _sorted_industry_types[NUM_INDUSTRYTYPES];
/** Sort industry types by their name. */
static int CDECL IndustryTypeNameSorter(const IndustryType *a, const IndustryType *b)
{
static char industry_name[2][64];
const IndustrySpec *indsp1 = GetIndustrySpec(*a);
SetDParam(0, indsp1->name);
GetString(industry_name[0], STR_JUST_STRING, lastof(industry_name[0]));
const IndustrySpec *indsp2 = GetIndustrySpec(*b);
SetDParam(0, indsp2->name);
GetString(industry_name[1], STR_JUST_STRING, lastof(industry_name[1]));
int r = strnatcmp(industry_name[0], industry_name[1]); // Sort by name (natural sorting).
/* If the names are equal, sort by industry type. */
return (r != 0) ? r : (*a - *b);
}
/**
* Initialize the list of sorted industry types.
*/
void SortIndustryTypes()
{
/* Add each industry type to the list. */
for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
_sorted_industry_types[i] = i;
}
/* Sort industry types by name. */
QSortT(_sorted_industry_types, NUM_INDUSTRYTYPES, &IndustryTypeNameSorter);
}
/**
* Command callback. In case of failure to build an industry, show an error message.
* @param result Result of the command.
* @param tile Tile where the industry is placed.
* @param p1 Additional data of the #CMD_BUILD_INDUSTRY command.
* @param p2 Additional data of the #CMD_BUILD_INDUSTRY command.
*/
void CcBuildIndustry(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
{
if (result.Succeeded()) return;
uint8 indtype = GB(p1, 0, 8);
if (indtype < NUM_INDUSTRYTYPES) {
const IndustrySpec *indsp = GetIndustrySpec(indtype);
if (indsp->enabled) {
SetDParam(0, indsp->name);
ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, result.GetErrorMessage(), WL_INFO, TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE);
}
}
}
/** Names of the widgets of the dynamic place industries gui */
enum DynamicPlaceIndustriesWidgets {
DPIW_MATRIX_WIDGET,
DPIW_SCROLLBAR,
DPIW_INFOPANEL,
DPIW_DISPLAY_WIDGET,
DPIW_FUND_WIDGET,
};
static const NWidgetPart _nested_build_industry_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FUND_INDUSTRY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
EndContainer(),
NWidget(NWID_HORIZONTAL),
NWidget(WWT_MATRIX, COLOUR_DARK_GREEN, DPIW_MATRIX_WIDGET), SetDataTip(0x801, STR_FUND_INDUSTRY_SELECTION_TOOLTIP), SetFill(1, 0), SetResize(1, 1), SetScrollbar(DPIW_SCROLLBAR),
NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, DPIW_SCROLLBAR),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_DARK_GREEN, DPIW_INFOPANEL), SetResize(1, 0),
EndContainer(),
NWidget(NWID_HORIZONTAL),
NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, DPIW_DISPLAY_WIDGET), SetFill(1, 0), SetResize(1, 0),
SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP),
NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, DPIW_FUND_WIDGET), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_NULL),
NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
EndContainer(),
};
/** Window definition of the dynamic place industries gui */
static const WindowDesc _build_industry_desc(
WDP_AUTO, 170, 212,
WC_BUILD_INDUSTRY, WC_NONE,
WDF_CONSTRUCTION,
_nested_build_industry_widgets, lengthof(_nested_build_industry_widgets)
);
/** Build (fund or prospect) a new industry, */
class BuildIndustryWindow : public Window {
int selected_index; ///< index of the element in the matrix
IndustryType selected_type; ///< industry corresponding to the above index
uint16 callback_timer; ///< timer counter for callback eventual verification
bool timer_enabled; ///< timer can be used
uint16 count; ///< How many industries are loaded
IndustryType index[NUM_INDUSTRYTYPES + 1]; ///< Type of industry, in the order it was loaded
bool enabled[NUM_INDUSTRYTYPES + 1]; ///< availability state, coming from CBID_INDUSTRY_PROBABILITY (if ever)
Scrollbar *vscroll;
/** The offset for the text in the matrix. */
static const int MATRIX_TEXT_OFFSET = 17;
void SetupArrays()
{
this->count = 0;
for (uint i = 0; i < lengthof(this->index); i++) {
this->index[i] = INVALID_INDUSTRYTYPE;
this->enabled[i] = false;
}
if (_game_mode == GM_EDITOR) { // give room for the Many Random "button"
this->index[this->count] = INVALID_INDUSTRYTYPE;
this->enabled[this->count] = true;
this->count++;
this->timer_enabled = false;
}
/* Fill the arrays with industries.
* The tests performed after the enabled allow to load the industries
* In the same way they are inserted by grf (if any)
*/
for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
IndustryType ind = _sorted_industry_types[i];
const IndustrySpec *indsp = GetIndustrySpec(ind);
if (indsp->enabled) {
/* Rule is that editor mode loads all industries.
* In game mode, all non raw industries are loaded too
* and raw ones are loaded only when setting allows it */
if (_game_mode != GM_EDITOR && indsp->IsRawIndustry() && _settings_game.construction.raw_industry_construction == 0) {
/* Unselect if the industry is no longer in the list */
if (this->selected_type == ind) this->selected_index = -1;
continue;
}
this->index[this->count] = ind;
this->enabled[this->count] = (_game_mode == GM_EDITOR) || GetIndustryProbabilityCallback(ind, IACT_USERCREATION, 1) > 0;
/* Keep the selection to the correct line */
if (this->selected_type == ind) this->selected_index = this->count;
this->count++;
}
}
/* first indutry type is selected if the current selection is invalid.
* I'll be damned if there are none available ;) */
if (this->selected_index == -1) {
this->selected_index = 0;
this->selected_type = this->index[0];
}
this->vscroll->SetCount(this->count);
}
/** Update status of the fund and display-chain widgets. */
void SetButtons()
{
this->SetWidgetDisabledState(DPIW_FUND_WIDGET, this->selected_type != INVALID_INDUSTRYTYPE && !this->enabled[this->selected_index]);
this->SetWidgetDisabledState(DPIW_DISPLAY_WIDGET, this->selected_type == INVALID_INDUSTRYTYPE && this->enabled[this->selected_index]);
}
public:
BuildIndustryWindow() : Window()
{
this->timer_enabled = _loaded_newgrf_features.has_newindustries;
this->selected_index = -1;
this->selected_type = INVALID_INDUSTRYTYPE;
this->callback_timer = DAY_TICKS;
this->CreateNestedTree(&_build_industry_desc);
this->vscroll = this->GetScrollbar(DPIW_SCROLLBAR);
this->FinishInitNested(&_build_industry_desc, 0);
this->SetButtons();
}
virtual void OnInit()
{
this->SetupArrays();
}
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
{
switch (widget) {
case DPIW_MATRIX_WIDGET: {
Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES);
for (byte i = 0; i < this->count; i++) {
if (this->index[i] == INVALID_INDUSTRYTYPE) continue;
d = maxdim(d, GetStringBoundingBox(GetIndustrySpec(this->index[i])->name));
}
resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
d.width += MATRIX_TEXT_OFFSET + padding.width;
d.height = 5 * resize->height;
*size = maxdim(*size, d);
break;
}
case DPIW_INFOPANEL: {
/* Extra line for cost outside of editor + extra lines for 'extra' information for NewGRFs. */
int height = 2 + (_game_mode == GM_EDITOR ? 0 : 1) + (_loaded_newgrf_features.has_newindustries ? 4 : 0);
Dimension d = {0, 0};
for (byte i = 0; i < this->count; i++) {
if (this->index[i] == INVALID_INDUSTRYTYPE) continue;
const IndustrySpec *indsp = GetIndustrySpec(this->index[i]);
char cargo_suffix[3][512];
GetAllCargoSuffixes(0, CST_FUND, NULL, this->index[i], indsp, indsp->accepts_cargo, cargo_suffix);
StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
byte p = 0;
SetDParam(0, STR_JUST_NOTHING);
SetDParamStr(1, "");
for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
if (indsp->accepts_cargo[j] == CT_INVALID) continue;
if (p > 0) str++;
SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
SetDParamStr(p++, cargo_suffix[j]);
}
d = maxdim(d, GetStringBoundingBox(str));
/* Draw the produced cargoes, if any. Otherwise, will print "Nothing". */
GetAllCargoSuffixes(3, CST_FUND, NULL, this->index[i], indsp, indsp->produced_cargo, cargo_suffix);
str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
p = 0;
SetDParam(0, STR_JUST_NOTHING);
SetDParamStr(1, "");
for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
if (indsp->produced_cargo[j] == CT_INVALID) continue;
if (p > 0) str++;
SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
SetDParamStr(p++, cargo_suffix[j]);
}
d = maxdim(d, GetStringBoundingBox(str));
}
/* Set it to something more sane :) */
size->height = height * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
size->width = d.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
break;
}
case DPIW_FUND_WIDGET: {
Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY);
d = maxdim(d, GetStringBoundingBox(STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY));
d = maxdim(d, GetStringBoundingBox(STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY));
d.width += padding.width;
d.height += padding.height;
*size = maxdim(*size, d);
break;
}
}
}
virtual void SetStringParameters(int widget) const
{
switch (widget) {
case DPIW_FUND_WIDGET:
/* Raw industries might be prospected. Show this fact by changing the string
* In Editor, you just build, while ingame, or you fund or you prospect */
if (_game_mode == GM_EDITOR) {
/* We've chosen many random industries but no industries have been specified */
SetDParam(0, STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY);
} else {
const IndustrySpec *indsp = GetIndustrySpec(this->index[this->selected_index]);
SetDParam(0, (_settings_game.construction.raw_industry_construction == 2 && indsp->IsRawIndustry()) ? STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY : STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY);
}
break;
}
}
virtual void DrawWidget(const Rect &r, int widget) const
{
switch (widget) {
case DPIW_MATRIX_WIDGET:
for (byte i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->count; i++) {
int x = r.left + WD_MATRIX_LEFT;
int y = r.top + WD_MATRIX_TOP + i * this->resize.step_height;
bool selected = this->selected_index == i + this->vscroll->GetPosition();
if (this->index[i + this->vscroll->GetPosition()] == INVALID_INDUSTRYTYPE) {
DrawString(x + MATRIX_TEXT_OFFSET, r.right - WD_MATRIX_RIGHT, y, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES, selected ? TC_WHITE : TC_ORANGE);
continue;
}
const IndustrySpec *indsp = GetIndustrySpec(this->index[i + this->vscroll->GetPosition()]);
/* Draw the name of the industry in white is selected, otherwise, in orange */
DrawString(x + MATRIX_TEXT_OFFSET, r.right - WD_MATRIX_RIGHT, y, indsp->name, selected ? TC_WHITE : TC_ORANGE);
GfxFillRect(x, y + 1, x + 10, y + 7, selected ? PC_WHITE : PC_BLACK);
GfxFillRect(x + 1, y + 2, x + 9, y + 6, indsp->map_colour);
}
break;
case DPIW_INFOPANEL: {
int y = r.top + WD_FRAMERECT_TOP;
int bottom = r.bottom - WD_FRAMERECT_BOTTOM;
int left = r.left + WD_FRAMERECT_LEFT;
int right = r.right - WD_FRAMERECT_RIGHT;
if (this->selected_type == INVALID_INDUSTRYTYPE) {
DrawStringMultiLine(left, right, y, bottom, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_TOOLTIP);
break;
}
const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
if (_game_mode != GM_EDITOR) {
SetDParam(0, indsp->GetConstructionCost());
DrawString(left, right, y, STR_FUND_INDUSTRY_INDUSTRY_BUILD_COST);
y += FONT_HEIGHT_NORMAL;
}
/* Draw the accepted cargoes, if any. Otherwise, will print "Nothing". */
char cargo_suffix[3][512];
GetAllCargoSuffixes(0, CST_FUND, NULL, this->selected_type, indsp, indsp->accepts_cargo, cargo_suffix);
StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
byte p = 0;
SetDParam(0, STR_JUST_NOTHING);
SetDParamStr(1, "");
for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
if (indsp->accepts_cargo[j] == CT_INVALID) continue;
if (p > 0) str++;
SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
SetDParamStr(p++, cargo_suffix[j]);
}
DrawString(left, right, y, str);
y += FONT_HEIGHT_NORMAL;
/* Draw the produced cargoes, if any. Otherwise, will print "Nothing". */
GetAllCargoSuffixes(3, CST_FUND, NULL, this->selected_type, indsp, indsp->produced_cargo, cargo_suffix);
str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
p = 0;
SetDParam(0, STR_JUST_NOTHING);
SetDParamStr(1, "");
for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
if (indsp->produced_cargo[j] == CT_INVALID) continue;
if (p > 0) str++;
SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
SetDParamStr(p++, cargo_suffix[j]);
}
DrawString(left, right, y, str);
y += FONT_HEIGHT_NORMAL;
/* Get the additional purchase info text, if it has not already been queried. */
str = STR_NULL;
if (HasBit(indsp->callback_mask, CBM_IND_FUND_MORE_TEXT)) {
uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_FUND_MORE_TEXT, 0, 0, NULL, this->selected_type, INVALID_TILE);
if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
if (callback_res > 0x400) {
ErrorUnknownCallbackResult(indsp->grf_prop.grffile->grfid, CBID_INDUSTRY_FUND_MORE_TEXT, callback_res);
} else {
str = GetGRFStringID(indsp->grf_prop.grffile->grfid, 0xD000 + callback_res); // No. here's the new string
if (str != STR_UNDEFINED) {
StartTextRefStackUsage(6);
DrawStringMultiLine(left, right, y, bottom, str, TC_YELLOW);
StopTextRefStackUsage();
}
}
}
}
break;
}
}
}
virtual void OnClick(Point pt, int widget, int click_count)
{
switch (widget) {
case DPIW_MATRIX_WIDGET: {
int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, DPIW_MATRIX_WIDGET);
if (y < this->count) { // Is it within the boundaries of available data?
this->selected_index = y;
this->selected_type = this->index[y];
const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
this->SetDirty();
if (_thd.GetCallbackWnd() == this &&
((_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indsp != NULL && indsp->IsRawIndustry()) ||
this->selected_type == INVALID_INDUSTRYTYPE ||
!this->enabled[this->selected_index])) {
/* Reset the button state if going to prospecting or "build many industries" */
this->RaiseButtons();
ResetObjectToPlace();
}
this->SetButtons();
if (this->enabled[this->selected_index] && click_count > 1) this->OnClick(pt, DPIW_FUND_WIDGET, 1);
}
break;
}
case DPIW_DISPLAY_WIDGET:
if (this->selected_type != INVALID_INDUSTRYTYPE) ShowIndustryCargoesWindow(this->selected_type);
break;
case DPIW_FUND_WIDGET: {
if (this->selected_type == INVALID_INDUSTRYTYPE) {
this->HandleButtonClick(DPIW_FUND_WIDGET);
if (Town::GetNumItems() == 0) {
ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_INDUSTRIES, STR_ERROR_MUST_FOUND_TOWN_FIRST, WL_INFO);
} else {
extern void GenerateIndustries();
_generating_world = true;
GenerateIndustries();
_generating_world = false;
}
} else if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && GetIndustrySpec(this->selected_type)->IsRawIndustry()) {
DoCommandP(0, this->selected_type, InteractiveRandom(), CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
this->HandleButtonClick(DPIW_FUND_WIDGET);
} else {
HandlePlacePushButton(this, DPIW_FUND_WIDGET, SPR_CURSOR_INDUSTRY, HT_RECT);
}
break;
}
}
}
virtual void OnResize()
{
/* Adjust the number of items in the matrix depending of the resize */
this->vscroll->SetCapacityFromWidget(this, DPIW_MATRIX_WIDGET);
this->GetWidget<NWidgetCore>(DPIW_MATRIX_WIDGET)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
}
virtual void OnPlaceObject(Point pt, TileIndex tile)
{
bool success = true;
/* We do not need to protect ourselves against "Random Many Industries" in this mode */
const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
uint32 seed = InteractiveRandom();
if (_game_mode == GM_EDITOR) {
/* Show error if no town exists at all */
if (Town::GetNumItems() == 0) {
SetDParam(0, indsp->name);
ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, STR_ERROR_MUST_FOUND_TOWN_FIRST, WL_INFO, pt.x, pt.y);
return;
}
Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
_generating_world = true;
_ignore_restrictions = true;
DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed,
CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY), &CcBuildIndustry);
cur_company.Restore();
_ignore_restrictions = false;
_generating_world = false;
} else {
success = DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed, CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
}
/* If an industry has been built, just reset the cursor and the system */
if (success && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
}
virtual void OnTick()
{
if (_pause_mode != PM_UNPAUSED) return;
if (!this->timer_enabled) return;
if (--this->callback_timer == 0) {
/* We have just passed another day.
* See if we need to update availability of currently selected industry */
this->callback_timer = DAY_TICKS; // restart counter
const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
if (indsp->enabled) {
bool call_back_result = GetIndustryProbabilityCallback(this->selected_type, IACT_USERCREATION, 1) > 0;
/* Only if result does match the previous state would it require a redraw. */
if (call_back_result != this->enabled[this->selected_index]) {
this->enabled[this->selected_index] = call_back_result;
this->SetButtons();
this->SetDirty();
}
}
}
}
virtual void OnTimeout()
{
this->RaiseButtons();
}
virtual void OnPlaceObjectAbort()
{
this->RaiseButtons();
}
/**
* Some data on this window has become invalid.
* @param data Information about the changed data.
* @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
*/
virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
{
if (!gui_scope) return;
this->SetupArrays();
const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
if (indsp == NULL) this->enabled[this->selected_index] = _settings_game.difficulty.industry_density != ID_FUND_ONLY;
this->SetButtons();
}
};
void ShowBuildIndustryWindow()
{
if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
if (BringWindowToFrontById(WC_BUILD_INDUSTRY, 0)) return;
new BuildIndustryWindow();
}
static void UpdateIndustryProduction(Industry *i);
static inline bool IsProductionAlterable(const Industry *i)
{
const IndustrySpec *is = GetIndustrySpec(i->type);
return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
(is->production_rate[0] != 0 || is->production_rate[1] != 0 || is->IsRawIndustry()));
}
/** Names of the widgets of the view industry gui */
enum IndustryViewWidgets {
IVW_CAPTION,
IVW_VIEWPORT,
IVW_INFO,
IVW_GOTO,
IVW_DISPLAY,
};
class IndustryViewWindow : public Window
{
/** Modes for changing production */
enum Editability {
EA_NONE, ///< Not alterable
EA_MULTIPLIER, ///< Allow changing the production multiplier
EA_RATE, ///< Allow changing the production rates
};
/** Specific lines in the info panel */
enum InfoLine {
IL_NONE, ///< No line
IL_MULTIPLIER, ///< Production multiplier
IL_RATE1, ///< Production rate of cargo 1
IL_RATE2, ///< Production rate of cargo 2
};
Editability editable; ///< Mode for changing production
InfoLine editbox_line; ///< The line clicked to open the edit box
InfoLine clicked_line; ///< The line of the button that has been clicked
byte clicked_button; ///< The button that has been clicked (to raise)
int production_offset_y; ///< The offset of the production texts/buttons
int info_height; ///< Height needed for the #IVW_INFO panel
public:
IndustryViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
{
this->flags4 |= WF_DISABLE_VP_SCROLL;
this->editbox_line = IL_NONE;
this->clicked_line = IL_NONE;
this->clicked_button = 0;
this->info_height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + 1; // Info panel has at least two lines text.
this->InitNested(desc, window_number);
NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(IVW_VIEWPORT);
nvp->InitializeViewport(this, Industry::Get(window_number)->location.GetCenterTile(), ZOOM_LVL_INDUSTRY);
this->InvalidateData();
}
virtual void OnPaint()
{
this->DrawWidgets();
if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
NWidgetBase *nwi = this->GetWidget<NWidgetBase>(IVW_INFO);
uint expected = this->DrawInfo(nwi->pos_x, nwi->pos_x + nwi->current_x - 1, nwi->pos_y) - nwi->pos_y;
if (expected > nwi->current_y - 1) {
this->info_height = expected + 1;
this->ReInit();
return;
}
}
/**
* Draw the text in the #IVW_INFO panel.
* @param left Left edge of the panel.
* @param right Right edge of the panel.
* @param top Top edge of the panel.
* @return Expected position of the bottom edge of the panel.
*/
int DrawInfo(uint left, uint right, uint top)
{
Industry *i = Industry::Get(this->window_number);
const IndustrySpec *ind = GetIndustrySpec(i->type);
int y = top + WD_FRAMERECT_TOP;
bool first = true;
bool has_accept = false;
char cargo_suffix[3][512];
if (HasBit(ind->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(ind->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) {
GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
if (i->accepts_cargo[j] == CT_INVALID) continue;
has_accept = true;
if (first) {
DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_FOR_PROCESSING);
y += FONT_HEIGHT_NORMAL;
first = false;
}
SetDParam(0, i->accepts_cargo[j]);
SetDParam(1, i->incoming_cargo_waiting[j]);
SetDParamStr(2, cargo_suffix[j]);
DrawString(left + WD_FRAMETEXT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_STOCKPILE_CARGO);
y += FONT_HEIGHT_NORMAL;
}
} else {
GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
byte p = 0;
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
if (i->accepts_cargo[j] == CT_INVALID) continue;
has_accept = true;
if (p > 0) str++;
SetDParam(p++, CargoSpec::Get(i->accepts_cargo[j])->name);
SetDParamStr(p++, cargo_suffix[j]);
}
if (has_accept) {
DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, str);
y += FONT_HEIGHT_NORMAL;
}
}
GetAllCargoSuffixes(3, CST_VIEW, i, i->type, ind, i->produced_cargo, cargo_suffix);
first = true;
for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] == CT_INVALID) continue;
if (first) {
if (has_accept) y += WD_PAR_VSEP_WIDE;
DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE);
y += FONT_HEIGHT_NORMAL;
if (this->editable == EA_RATE) this->production_offset_y = y;
first = false;
}
SetDParam(0, i->produced_cargo[j]);
SetDParam(1, i->last_month_production[j]);
SetDParamStr(2, cargo_suffix[j]);
SetDParam(3, ToPercent8(i->last_month_pct_transported[j]));
uint x = left + WD_FRAMETEXT_LEFT + (this->editable == EA_RATE ? 30 : 0);
DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_TRANSPORTED);
/* Let's put out those buttons.. */
if (this->editable == EA_RATE) {
DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == IL_RATE1 + j) ? this->clicked_button : 0,
i->production_rate[j] > 0, i->production_rate[j] < 255);
}
y += FONT_HEIGHT_NORMAL;
}
/* Display production multiplier if editable */
if (this->editable == EA_MULTIPLIER) {
y += WD_PAR_VSEP_WIDE;
this->production_offset_y = y;
SetDParam(0, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT));
uint x = left + WD_FRAMETEXT_LEFT + 30;
DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_PRODUCTION_LEVEL);
DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == IL_MULTIPLIER) ? this->clicked_button : 0,
i->prod_level > PRODLEVEL_MINIMUM, i->prod_level < PRODLEVEL_MAXIMUM);
y += FONT_HEIGHT_NORMAL;
}
/* Get the extra message for the GUI */
if (HasBit(ind->callback_mask, CBM_IND_WINDOW_MORE_TEXT)) {
uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT, 0, 0, i, i->type, i->location.tile);
if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
if (callback_res > 0x400) {
ErrorUnknownCallbackResult(ind->grf_prop.grffile->grfid, CBID_INDUSTRY_WINDOW_MORE_TEXT, callback_res);
} else {
StringID message = GetGRFStringID(ind->grf_prop.grffile->grfid, 0xD000 + callback_res);
if (message != STR_NULL && message != STR_UNDEFINED) {
y += WD_PAR_VSEP_WIDE;
StartTextRefStackUsage(6);
/* Use all the available space left from where we stand up to the
* end of the window. We ALSO enlarge the window if needed, so we
* can 'go' wild with the bottom of the window. */
y = DrawStringMultiLine(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, UINT16_MAX, message, TC_BLACK);
StopTextRefStackUsage();
}
}
}
}
return y + WD_FRAMERECT_BOTTOM;
}
virtual void SetStringParameters(int widget) const
{
if (widget == IVW_CAPTION) SetDParam(0, this->window_number);
}
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
{
if (widget == IVW_INFO) size->height = this->info_height;
}
virtual void OnClick(Point pt, int widget, int click_count)
{
switch (widget) {
case IVW_INFO: {
Industry *i = Industry::Get(this->window_number);
InfoLine line = IL_NONE;
switch (this->editable) {
case EA_NONE: break;
case EA_MULTIPLIER:
if (IsInsideBS(pt.y, this->production_offset_y, FONT_HEIGHT_NORMAL)) line = IL_MULTIPLIER;
break;
case EA_RATE:
if (pt.y >= this->production_offset_y) {
int row = (pt.y - this->production_offset_y) / FONT_HEIGHT_NORMAL;
for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] == CT_INVALID) continue;
row--;
if (row < 0) {
line = (InfoLine)(IL_RATE1 + j);
break;
}
}
}
break;
}
if (line == IL_NONE) return;
NWidgetBase *nwi = this->GetWidget<NWidgetBase>(widget);
int left = nwi->pos_x + WD_FRAMETEXT_LEFT;
int right = nwi->pos_x + nwi->current_x - 1 - WD_FRAMERECT_RIGHT;
if (IsInsideMM(pt.x, left, left + 20)) {
/* Clicked buttons, decrease or increase production */
byte button = (pt.x < left + 10) ? 1 : 2;
switch (this->editable) {
case EA_MULTIPLIER:
if (button == 1) {
if (i->prod_level <= PRODLEVEL_MINIMUM) return;
i->prod_level = max<uint>(i->prod_level / 2, PRODLEVEL_MINIMUM);
} else {
if (i->prod_level >= PRODLEVEL_MAXIMUM) return;
i->prod_level = minu(i->prod_level * 2, PRODLEVEL_MAXIMUM);
}
break;
case EA_RATE:
if (button == 1) {
if (i->production_rate[line - IL_RATE1] <= 0) return;
i->production_rate[line - IL_RATE1] = max(i->production_rate[line - IL_RATE1] / 2, 0);
} else {
if (i->production_rate[line - IL_RATE1] >= 255) return;
/* a zero production industry is unlikely to give anything but zero, so push it a little bit */
int new_prod = i->production_rate[line - IL_RATE1] == 0 ? 1 : i->production_rate[line - IL_RATE1] * 2;
i->production_rate[line - IL_RATE1] = minu(new_prod, 255);
}
break;
default: NOT_REACHED();
}
UpdateIndustryProduction(i);
this->SetDirty();
this->flags4 |= WF_TIMEOUT_BEGIN;
this->clicked_line = line;
this->clicked_button = button;
} else if (IsInsideMM(pt.x, left + 30, right)) {
/* clicked the text */
this->editbox_line = line;
switch (this->editable) {
case EA_MULTIPLIER:
SetDParam(0, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT));
ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION_LEVEL, 10, this, CS_ALPHANUMERAL, QSF_NONE);
break;
case EA_RATE:
SetDParam(0, i->production_rate[line - IL_RATE1] * 8);
ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION, 10, this, CS_ALPHANUMERAL, QSF_NONE);
break;
default: NOT_REACHED();
}
}
break;
}
case IVW_GOTO: {
Industry *i = Industry::Get(this->window_number);
if (_ctrl_pressed) {
ShowExtraViewPortWindow(i->location.GetCenterTile());
} else {
ScrollMainWindowToTile(i->location.GetCenterTile());
}
break;
}
case IVW_DISPLAY: {
Industry *i = Industry::Get(this->window_number);
ShowIndustryCargoesWindow(i->type);
break;
}
}
}
virtual void OnTimeout()
{
this->clicked_line = IL_NONE;
this->clicked_button = 0;
this->SetDirty();
}
virtual void OnResize()
{
if (this->viewport != NULL) {
NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(IVW_VIEWPORT);
nvp->UpdateViewportCoordinates(this);
ScrollWindowToTile(Industry::Get(this->window_number)->location.GetCenterTile(), this, true); // Re-center viewport.
}
}
virtual void OnQueryTextFinished(char *str)
{
if (StrEmpty(str)) return;
Industry *i = Industry::Get(this->window_number);
uint value = atoi(str);
switch (this->editbox_line) {
case IL_NONE: NOT_REACHED();
case IL_MULTIPLIER:
i->prod_level = ClampU(RoundDivSU(value * PRODLEVEL_DEFAULT, 100), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
break;
default:
i->production_rate[this->editbox_line - IL_RATE1] = ClampU(RoundDivSU(value, 8), 0, 255);
break;
}
UpdateIndustryProduction(i);
this->SetDirty();
}
/**
* Some data on this window has become invalid.
* @param data Information about the changed data.
* @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
*/
virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
{
if (!gui_scope) return;
const Industry *i = Industry::Get(this->window_number);
if (IsProductionAlterable(i)) {
const IndustrySpec *ind = GetIndustrySpec(i->type);
this->editable = ind->UsesSmoothEconomy() ? EA_RATE : EA_MULTIPLIER;
} else {
this->editable = EA_NONE;
}
}
virtual bool IsNewGRFInspectable() const
{
return ::IsNewGRFInspectable(GSF_INDUSTRIES, this->window_number);
}
virtual void ShowNewGRFInspectWindow() const
{
::ShowNewGRFInspectWindow(GSF_INDUSTRIES, this->window_number);
}
};
static void UpdateIndustryProduction(Industry *i)
{
const IndustrySpec *indspec = GetIndustrySpec(i->type);
if (!indspec->UsesSmoothEconomy()) i->RecomputeProductionMultipliers();
for (byte j = 0; j < lengthof(i->produced_cargo); j++) {