summaryrefslogtreecommitdiff
path: root/vehicle_gui.c
blob: 3edd898efeb4ae4b6b0cacf6f01328370d5b2d87 (plain)
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
#include "stdafx.h"
#include "ttd.h"

#include "vehicle.h"

/* General Vehicle GUI based procedures that are independent of vehicle types */
void InitializeVehiclesGuiList()
{
	bool *i;
	for (i = _train_sort_dirty; i != endof(_train_sort_dirty); i++)
		*i = true;

	for (i = _aircraft_sort_dirty; i != endof(_aircraft_sort_dirty); i++)
		*i = true;

	for (i = _ship_sort_dirty; i != endof(_ship_sort_dirty); i++)
		*i = true;

	for (i = _road_sort_dirty; i != endof(_road_sort_dirty); i++)
		*i = true;

	for (i = _vehicle_sort_dirty; i != endof(_vehicle_sort_dirty); i++)
		*i = true;

	//memset(_train_sort_dirty, true, sizeof(_train_sort_dirty));
}

// draw the vehicle profit button in the vehicle list window.
void DrawVehicleProfitButton(Vehicle *v, int x, int y)
{
	uint32 ormod;

	// draw profit-based colored icons
	if(v->age <= 365 * 2)
		ormod = 0x3158000; // grey
	else if(v->profit_last_year < 0)
		ormod = 0x30b8000; //red
	else if(v->profit_last_year < 10000)
		ormod = 0x30a8000; // yellow
	else
		ormod = 0x30d8000; // green
	DrawSprite((SPR_OPENTTD_BASE + 10) | ormod, x, y);
}

/************ Sorter functions *****************/
int CDECL GeneralOwnerSorter(const void *a, const void *b)
{
	return (*(const SortStruct*)a).owner - (*(const SortStruct*)b).owner;
}

/* Variables you need to set before calling this function!
* 1. (byte)_internal_sort_type:					sorting criteria to sort on
* 2. (bool)_internal_sort_order:				sorting order, descending/ascending
* 3. (uint32)_internal_name_sorter_id:	default StringID of the vehicle when no name is set. eg
*    STR_SV_TRAIN_NAME for trains or STR_SV_AIRCRAFT_NAME for aircraft
*/
int CDECL VehicleUnsortedSorter(const void *a, const void *b)
{
	return DEREF_VEHICLE((*(const SortStruct*)a).index)->index - DEREF_VEHICLE((*(const SortStruct*)b).index)->index;
}

int CDECL VehicleNumberSorter(const void *a, const void *b)
{
	const Vehicle *va = DEREF_VEHICLE((*(const SortStruct*)a).index);
	const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index);
	int r = va->unitnumber - vb->unitnumber;

	if (r == 0) // if the sorting criteria had the same value, sort by unitnumber
		r = va->unitnumber - vb->unitnumber;

	return (_internal_sort_order & 1) ? -r : r;
}

static char _bufcache[64];	// used together with _last_vehicle_idx to hopefully speed up stringsorting
int CDECL VehicleNameSorter(const void *a, const void *b)
{
	const SortStruct *cmp1 = (SortStruct*)a;
	const SortStruct *cmp2 = (SortStruct*)b;
	const Vehicle *va = DEREF_VEHICLE(cmp1->index);
	const Vehicle *vb = DEREF_VEHICLE(cmp2->index);
	char buf1[64] = "\0";
	int r;

	if (va->string_id != _internal_name_sorter_id) {
		SET_DPARAM16(0, va->string_id);
		GetString(buf1, STR_0315);
	}

	if ( cmp2->index != _last_vehicle_idx) {
		_last_vehicle_idx = cmp2->index;
		_bufcache[0] = '\0';
		if (vb->string_id != _internal_name_sorter_id) {
			SET_DPARAM16(0, vb->string_id);
			GetString(_bufcache, STR_0315);
		}
	}

	r =  strcmp(buf1, _bufcache);	// sort by name
	
	if (r == 0) // if the sorting criteria had the same value, sort by unitnumber
		r = va->unitnumber - vb->unitnumber;

	return (_internal_sort_order & 1) ? -r : r;
}

int CDECL VehicleAgeSorter(const void *a, const void *b)
{
	const Vehicle *va = DEREF_VEHICLE((*(const SortStruct*)a).index);
	const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index);
	int r = va->age - vb->age;

	if (r == 0) // if the sorting criteria had the same value, sort by unitnumber
	r = va->unitnumber - vb->unitnumber;

	return (_internal_sort_order & 1) ? -r : r;
}

int CDECL VehicleProfitThisYearSorter(const void *a, const void *b)
{
	const Vehicle *va = DEREF_VEHICLE((*(const SortStruct*)a).index);
	const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index);
	int r = va->profit_this_year - vb->profit_this_year;
	
	if (r == 0) // if the sorting criteria had the same value, sort by unitnumber
		r = va->unitnumber - vb->unitnumber;

	return (_internal_sort_order & 1) ? -r : r;
}

int CDECL VehicleProfitLastYearSorter(const void *a, const void *b)
{
	const Vehicle *va = DEREF_VEHICLE((*(const SortStruct*)a).index);
	const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index);
	int r = va->profit_last_year - vb->profit_last_year;

	if (r == 0) // if the sorting criteria had the same value, sort by unitnumber
		r = va->unitnumber - vb->unitnumber;

	return (_internal_sort_order & 1) ? -r : r;
}

int CDECL VehicleCargoSorter(const void *a, const void *b)
{
	// FIXME - someone write a normal cargo sorter that also works by cargo_cap,
	// FIXME - since I seem to be unable to do so :S
	const Vehicle *va = DEREF_VEHICLE((*(const SortStruct*)a).index);
	const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index);
	int r = 0;
	int i;
	byte _cargo_counta[NUM_CARGO];
	byte _cargo_countb[NUM_CARGO];
	memset(_cargo_counta, 0, sizeof(_cargo_counta));
	memset(_cargo_countb, 0, sizeof(_cargo_countb));

	do {
		_cargo_counta[va->cargo_type]++;
	} while ( (va = va->next) != NULL);

	do {
		_cargo_countb[vb->cargo_type]++;
	} while ( (vb = vb->next) != NULL);

	for (i = 0; i < NUM_CARGO; i++) {
		r = _cargo_counta[i] - _cargo_countb[i];
		if (r != 0)
			break;
	}

	return (_internal_sort_order & 1) ? -r : r;
}

int CDECL VehicleReliabilitySorter(const void *a, const void *b)
{
	const Vehicle *va = DEREF_VEHICLE((*(const SortStruct*)a).index);
	const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index);
	int r = va->reliability - vb->reliability;

	if (r == 0) // if the sorting criteria had the same value, sort by unitnumber
		r = va->unitnumber - vb->unitnumber;

	return (_internal_sort_order & 1) ? -r : r;
}

int CDECL VehicleMaxSpeedSorter(const void *a, const void *b)
{
	const Vehicle *va = DEREF_VEHICLE((*(const SortStruct*)a).index);
	const Vehicle *vb = DEREF_VEHICLE((*(const SortStruct*)b).index);
	int r = va->max_speed - vb->max_speed;

	if (r == 0) // if the sorting criteria had the same value, sort by unitnumber
		r = va->unitnumber - vb->unitnumber;

	return (_internal_sort_order & 1) ? -r : r;
}