src/vehicle_gui.cpp
branchNewGRF_ports
changeset 6878 7d1ff2f621c7
parent 6877 889301acc299
child 10184 fcf5fb2548eb
equal deleted inserted replaced
6877:889301acc299 6878:7d1ff2f621c7
   143 
   143 
   144 	vl->l.flags &= ~VL_REBUILD;
   144 	vl->l.flags &= ~VL_REBUILD;
   145 	vl->l.flags |= VL_RESORT;
   145 	vl->l.flags |= VL_RESORT;
   146 }
   146 }
   147 
   147 
       
   148 /* cached values for VehicleNameSorter to spare many GetString() calls */
       
   149 static const Vehicle *_last_vehicle[2] = { NULL, NULL };
       
   150 static char           _last_name[2][64] = { "", "" };
       
   151 
   148 void SortVehicleList(vehiclelist_d *vl)
   152 void SortVehicleList(vehiclelist_d *vl)
   149 {
   153 {
   150 	if (!(vl->l.flags & VL_RESORT)) return;
   154 	if (!(vl->l.flags & VL_RESORT)) return;
       
   155 
       
   156 	/* invalidate cached values for name sorter - vehicle names could change */
       
   157 	_last_vehicle[0] = _last_vehicle[1] = NULL;
   151 
   158 
   152 	_internal_sort_order = (vl->l.flags & VL_DESC) != 0;
   159 	_internal_sort_order = (vl->l.flags & VL_DESC) != 0;
   153 	qsort((void*)vl->sort_list, vl->l.list_length, sizeof(vl->sort_list[0]),
   160 	qsort((void*)vl->sort_list, vl->l.list_length, sizeof(vl->sort_list[0]),
   154 		_vehicle_sorter[vl->l.sort_type]);
   161 		_vehicle_sorter[vl->l.sort_type]);
   155 
   162 
   169 	SpriteID pal;
   176 	SpriteID pal;
   170 
   177 
   171 	/* draw profit-based colored icons */
   178 	/* draw profit-based colored icons */
   172 	if (v->age <= 365 * 2) {
   179 	if (v->age <= 365 * 2) {
   173 		pal = PALETTE_TO_GREY;
   180 		pal = PALETTE_TO_GREY;
   174 	} else if (v->profit_last_year < 0) {
   181 	} else if (v->GetDisplayProfitLastYear() < 0) {
   175 		pal = PALETTE_TO_RED;
   182 		pal = PALETTE_TO_RED;
   176 	} else if (v->profit_last_year < 10000) {
   183 	} else if (v->GetDisplayProfitLastYear() < 10000) {
   177 		pal = PALETTE_TO_YELLOW;
   184 		pal = PALETTE_TO_YELLOW;
   178 	} else {
   185 	} else {
   179 		pal = PALETTE_TO_GREEN;
   186 		pal = PALETTE_TO_GREEN;
   180 	}
   187 	}
   181 	DrawSprite(SPR_BLOT, pal, x, y);
   188 	DrawSprite(SPR_BLOT, pal, x, y);
   544 	return (_internal_sort_order & 1) ? -r : r;
   551 	return (_internal_sort_order & 1) ? -r : r;
   545 }
   552 }
   546 
   553 
   547 static int CDECL VehicleNameSorter(const void *a, const void *b)
   554 static int CDECL VehicleNameSorter(const void *a, const void *b)
   548 {
   555 {
   549 	static const Vehicle *last_vehicle[2] = { NULL, NULL };
       
   550 	static char           last_name[2][64] = { "", "" };
       
   551 
       
   552 	const Vehicle* va = *(const Vehicle**)a;
   556 	const Vehicle* va = *(const Vehicle**)a;
   553 	const Vehicle* vb = *(const Vehicle**)b;
   557 	const Vehicle* vb = *(const Vehicle**)b;
   554 	int r;
   558 	int r;
   555 
   559 
   556 	if (va != last_vehicle[0]) {
   560 	if (va != _last_vehicle[0]) {
   557 		last_vehicle[0] = va;
   561 		_last_vehicle[0] = va;
   558 		SetDParam(0, va->index);
   562 		SetDParam(0, va->index);
   559 		GetString(last_name[0], STR_VEHICLE_NAME, lastof(last_name[0]));
   563 		GetString(_last_name[0], STR_VEHICLE_NAME, lastof(_last_name[0]));
   560 	}
   564 	}
   561 
   565 
   562 	if (vb != last_vehicle[1]) {
   566 	if (vb != _last_vehicle[1]) {
   563 		last_vehicle[1] = vb;
   567 		_last_vehicle[1] = vb;
   564 		SetDParam(1, vb->index);
   568 		SetDParam(0, vb->index);
   565 		GetString(last_name[1], STR_VEHICLE_NAME, lastof(last_name[1]));
   569 		GetString(_last_name[1], STR_VEHICLE_NAME, lastof(_last_name[1]));
   566 	}
   570 	}
   567 
   571 
   568 	r = strcmp(last_name[0], last_name[1]); // sort by name
   572 	r = strcmp(_last_name[0], _last_name[1]); // sort by name
   569 
   573 
   570 	VEHICLEUNITNUMBERSORTER(r, va, vb);
   574 	VEHICLEUNITNUMBERSORTER(r, va, vb);
   571 
   575 
   572 	return (_internal_sort_order & 1) ? -r : r;
   576 	return (_internal_sort_order & 1) ? -r : r;
   573 }
   577 }
   585 
   589 
   586 static int CDECL VehicleProfitThisYearSorter(const void *a, const void *b)
   590 static int CDECL VehicleProfitThisYearSorter(const void *a, const void *b)
   587 {
   591 {
   588 	const Vehicle* va = *(const Vehicle**)a;
   592 	const Vehicle* va = *(const Vehicle**)a;
   589 	const Vehicle* vb = *(const Vehicle**)b;
   593 	const Vehicle* vb = *(const Vehicle**)b;
   590 	int r = ClampToI32(va->profit_this_year - vb->profit_this_year);
   594 	int r = ClampToI32(va->GetDisplayProfitThisYear() - vb->GetDisplayProfitThisYear());
   591 
   595 
   592 	VEHICLEUNITNUMBERSORTER(r, va, vb);
   596 	VEHICLEUNITNUMBERSORTER(r, va, vb);
   593 
   597 
   594 	return (_internal_sort_order & 1) ? -r : r;
   598 	return (_internal_sort_order & 1) ? -r : r;
   595 }
   599 }
   596 
   600 
   597 static int CDECL VehicleProfitLastYearSorter(const void *a, const void *b)
   601 static int CDECL VehicleProfitLastYearSorter(const void *a, const void *b)
   598 {
   602 {
   599 	const Vehicle* va = *(const Vehicle**)a;
   603 	const Vehicle* va = *(const Vehicle**)a;
   600 	const Vehicle* vb = *(const Vehicle**)b;
   604 	const Vehicle* vb = *(const Vehicle**)b;
   601 	int r = ClampToI32(va->profit_last_year - vb->profit_last_year);
   605 	int r = ClampToI32(va->GetDisplayProfitLastYear() - vb->GetDisplayProfitLastYear());
   602 
   606 
   603 	VEHICLEUNITNUMBERSORTER(r, va, vb);
   607 	VEHICLEUNITNUMBERSORTER(r, va, vb);
   604 
   608 
   605 	return (_internal_sort_order & 1) ? -r : r;
   609 	return (_internal_sort_order & 1) ? -r : r;
   606 }
   610 }
   642 
   646 
   643 static int CDECL VehicleMaxSpeedSorter(const void *a, const void *b)
   647 static int CDECL VehicleMaxSpeedSorter(const void *a, const void *b)
   644 {
   648 {
   645 	const Vehicle* va = *(const Vehicle**)a;
   649 	const Vehicle* va = *(const Vehicle**)a;
   646 	const Vehicle* vb = *(const Vehicle**)b;
   650 	const Vehicle* vb = *(const Vehicle**)b;
   647 	int max_speed_a = 0xFFFF, max_speed_b = 0xFFFF;
       
   648 	int r;
   651 	int r;
   649 	const Vehicle *ua = va, *ub = vb;
       
   650 
   652 
   651 	if (va->type == VEH_TRAIN && vb->type == VEH_TRAIN) {
   653 	if (va->type == VEH_TRAIN && vb->type == VEH_TRAIN) {
   652 		do {
   654 		r = va->u.rail.cached_max_speed - vb->u.rail.cached_max_speed;
   653 			if (RailVehInfo(ua->engine_type)->max_speed != 0)
       
   654 				max_speed_a = min(max_speed_a, RailVehInfo(ua->engine_type)->max_speed);
       
   655 		} while ((ua = ua->Next()) != NULL);
       
   656 
       
   657 		do {
       
   658 			if (RailVehInfo(ub->engine_type)->max_speed != 0)
       
   659 				max_speed_b = min(max_speed_b, RailVehInfo(ub->engine_type)->max_speed);
       
   660 		} while ((ub = ub->Next()) != NULL);
       
   661 
       
   662 		r = max_speed_a - max_speed_b;
       
   663 	} else {
   655 	} else {
   664 		r = va->max_speed - vb->max_speed;
   656 		r = va->max_speed - vb->max_speed;
   665 	}
   657 	}
   666 
   658 
   667 	VEHICLEUNITNUMBERSORTER(r, va, vb);
   659 	VEHICLEUNITNUMBERSORTER(r, va, vb);
   729 			w->window_number = to_v->index;
   721 			w->window_number = to_v->index;
   730 			SetWindowDirty(w);
   722 			SetWindowDirty(w);
   731 		}
   723 		}
   732 
   724 
   733 		w = FindWindowById(WC_VEHICLE_DETAILS, from_v->index);
   725 		w = FindWindowById(WC_VEHICLE_DETAILS, from_v->index);
       
   726 		if (w != NULL) {
       
   727 			w->window_number = to_v->index;
       
   728 			SetWindowDirty(w);
       
   729 		}
       
   730 
       
   731 		w = FindWindowById(WC_VEHICLE_TIMETABLE, from_v->index);
   734 		if (w != NULL) {
   732 		if (w != NULL) {
   735 			w->window_number = to_v->index;
   733 			w->window_number = to_v->index;
   736 			SetWindowDirty(w);
   734 			SetWindowDirty(w);
   737 		}
   735 		}
   738 	}
   736 	}
   993 	max = min(w->vscroll.pos + w->vscroll.cap, vl->l.list_length);
   991 	max = min(w->vscroll.pos + w->vscroll.cap, vl->l.list_length);
   994 	for (i = w->vscroll.pos; i < max; ++i) {
   992 	for (i = w->vscroll.pos; i < max; ++i) {
   995 		const Vehicle *v = vl->sort_list[i];
   993 		const Vehicle *v = vl->sort_list[i];
   996 		StringID str;
   994 		StringID str;
   997 
   995 
   998 		SetDParam(0, v->profit_this_year);
   996 		SetDParam(0, v->GetDisplayProfitThisYear());
   999 		SetDParam(1, v->profit_last_year);
   997 		SetDParam(1, v->GetDisplayProfitLastYear());
  1000 
   998 
  1001 		DrawVehicleImage(v, x + 19, y + 6, INVALID_VEHICLE, w->widget[VLW_WIDGET_LIST].right - w->widget[VLW_WIDGET_LIST].left - 20, 0);
   999 		DrawVehicleImage(v, x + 19, y + 6, INVALID_VEHICLE, w->widget[VLW_WIDGET_LIST].right - w->widget[VLW_WIDGET_LIST].left - 20, 0);
  1002 		DrawString(x + 19, y + w->resize.step_height - 8, STR_0198_PROFIT_THIS_YEAR_LAST_YEAR, TC_FROMSTRING);
  1000 		DrawString(x + 19, y + w->resize.step_height - 8, STR_0198_PROFIT_THIS_YEAR_LAST_YEAR, TC_FROMSTRING);
  1003 
  1001 
  1004 		if (v->name != NULL) {
  1002 		if (v->name != NULL) {
  1510 
  1508 
  1511 		default: NOT_REACHED();
  1509 		default: NOT_REACHED();
  1512 	}
  1510 	}
  1513 
  1511 
  1514 	/* Draw profit */
  1512 	/* Draw profit */
  1515 	SetDParam(0, v->profit_this_year);
  1513 	SetDParam(0, v->GetDisplayProfitThisYear());
  1516 	SetDParam(1, v->profit_last_year);
  1514 	SetDParam(1, v->GetDisplayProfitLastYear());
  1517 	DrawString(2, 35, _vehicle_translation_table[VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR][v->type], TC_FROMSTRING);
  1515 	DrawString(2, 35, _vehicle_translation_table[VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR][v->type], TC_FROMSTRING);
  1518 
  1516 
  1519 	/* Draw breakdown & reliability */
  1517 	/* Draw breakdown & reliability */
  1520 	SetDParam(0, v->reliability * 100 >> 16);
  1518 	SetDParam(0, v->reliability * 100 >> 16);
  1521 	SetDParam(1, v->breakdowns_since_last_service);
  1519 	SetDParam(1, v->breakdowns_since_last_service);