vehicle_gui.c
changeset 842 efc3546bc313
parent 594 1b51f81b4a77
child 843 f51135b17930
equal deleted inserted replaced
841:4874b9ce2765 842:efc3546bc313
     1 #include "stdafx.h"
     1 #include "stdafx.h"
     2 #include "ttd.h"
     2 #include "ttd.h"
     3 #include "table/strings.h"
     3 #include "table/strings.h"
     4 #include "vehicle.h"
     4 #include "vehicle.h"
     5 #include "window.h"
     5 #include "window.h"
       
     6 #include "engine.h"
       
     7 #include "gui.h"
       
     8 #include "command.h"
       
     9 #include "gfx.h"
     6 
    10 
     7 VehicleSortListingTypeFunctions * const _vehicle_sorter[] = {
    11 VehicleSortListingTypeFunctions * const _vehicle_sorter[] = {
     8 	&VehicleUnsortedSorter,
    12 	&VehicleUnsortedSorter,
     9 	&VehicleNumberSorter,
    13 	&VehicleNumberSorter,
    10 	&VehicleNameSorter,
    14 	&VehicleNameSorter,
    27 	STR_SORT_BY_RELIABILITY,
    31 	STR_SORT_BY_RELIABILITY,
    28 	STR_SORT_BY_MAX_SPEED,
    32 	STR_SORT_BY_MAX_SPEED,
    29 	INVALID_STRING_ID
    33 	INVALID_STRING_ID
    30 };
    34 };
    31 
    35 
       
    36 const StringID _rail_types_list[] = {
       
    37 	STR_RAIL_VEHICLES,
       
    38 	STR_MONORAIL_VEHICLES,
       
    39 	STR_MAGLEV_VEHICLES,
       
    40 	INVALID_STRING_ID
       
    41 };
       
    42 
    32 void RebuildVehicleLists(void)
    43 void RebuildVehicleLists(void)
    33 {
    44 {
    34 	Window *w;
    45 	Window *w;
    35 
    46 
    36 	for (w = _windows; w != _last_window; ++w)
    47 	for (w = _windows; w != _last_window; ++w)
   292 
   303 
   293 	VEHICLEUNITNUMBERSORTER(r, va, vb);
   304 	VEHICLEUNITNUMBERSORTER(r, va, vb);
   294 
   305 
   295 	return (_internal_sort_order & 1) ? -r : r;
   306 	return (_internal_sort_order & 1) ? -r : r;
   296 }
   307 }
       
   308 
       
   309 // this define is to match engine.c, but engine.c keeps it to itself
       
   310 // ENGINE_AVAILABLE is used in ReplaceVehicleWndProc
       
   311 #define ENGINE_AVAILABLE ((e->flags & 1 && HASBIT(info->railtype_climates, _opt.landscape)) || HASBIT(e->player_avail, _local_player))
       
   312 
       
   313 /*  if show_outdated is selected, it do not sort psudo engines properly but it draws all engines
       
   314  *	if used compined with show_cars set to false, it will work as intended. Replace window do it like that 
       
   315  *  this was a big hack even before show_outdated was added. Stupid newgrf :p										*/
       
   316 static void train_engine_drawing_loop(int *x, int *y, int *pos, int *sel, int *selected_id, byte railtype, 
       
   317 	uint8 lines_drawn, bool is_engine, bool show_cars, bool show_outdated)
       
   318 {
       
   319 	int i;
       
   320 	byte colour;
       
   321 	
       
   322 	for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
       
   323 		const Engine *e = DEREF_ENGINE(i);
       
   324 		const RailVehicleInfo *rvi = RailVehInfo(i);
       
   325 		const EngineInfo *info = &_engine_info[i];
       
   326 
       
   327 		if ( rvi->power == 0 && !(show_cars) )   // disables display of cars (works since they do not have power)
       
   328 			continue;
       
   329 			
       
   330 		if (*sel == 0) *selected_id = i;
       
   331 
       
   332 
       
   333 		colour = *sel == 0 ? 0xC : 0x10;
       
   334 		if (!(ENGINE_AVAILABLE && show_outdated && RailVehInfo(i)->power && e->railtype == railtype)) {
       
   335 			if (e->railtype != railtype || !(rvi->flags & RVI_WAGON) != is_engine ||
       
   336 				!HASBIT(e->player_avail, _local_player))
       
   337 				continue;
       
   338 		} /*else { 
       
   339 		// TODO find a nice red colour for vehicles being replaced
       
   340 			if ( _autoreplace_array[i] != i )
       
   341 				colour = *sel == 0 ? 0x44 : 0x45;
       
   342 		} */
       
   343 				
       
   344 		if (IS_INT_INSIDE(--*pos, -lines_drawn, 0)) {
       
   345 			DrawString(*x + 59, *y + 2, GetCustomEngineName(i),
       
   346 				colour);
       
   347 			DrawTrainEngine(*x + 29, *y + 6, i,
       
   348 				SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)));
       
   349 			*y += 14;
       
   350 		}
       
   351 		--*sel;
       
   352 	}
       
   353 }
       
   354 
       
   355 
       
   356 static void SetupScrollStuffForReplaceWindow(Window *w)
       
   357 {
       
   358 	byte railtype;
       
   359 	int selected_id[2] = {-1,-1};
       
   360 	int sel[2] = { WP(w,replaceveh_d).sel_index[0], WP(w,replaceveh_d).sel_index[1]};
       
   361 	int count = 0;
       
   362 	int count2 = 0;
       
   363 	int engine_id;
       
   364 				
       
   365 	switch (WP(w,replaceveh_d).vehicletype) {
       
   366 		case VEH_Train: {
       
   367 			railtype = WP(w,replaceveh_d).railtype;
       
   368 			for (engine_id = 0; engine_id < NUM_TRAIN_ENGINES; engine_id++) {
       
   369 				const Engine *e = DEREF_ENGINE(engine_id);
       
   370 				const EngineInfo *info = &_engine_info[engine_id];
       
   371 							
       
   372 				if (ENGINE_AVAILABLE && RailVehInfo(engine_id)->power && e->railtype == railtype) {
       
   373 					count++;
       
   374 					if (sel[0]==0)  selected_id[0] = engine_id;
       
   375 					sel[0]--;
       
   376 					if (HASBIT(e->player_avail, _local_player)) {
       
   377 						if (sel[1]==0)  selected_id[1] = engine_id;
       
   378 							count2++;
       
   379 							sel[1]--;
       
   380 						}
       
   381 					}
       
   382 				}
       
   383 			break;
       
   384 			}
       
   385 		case VEH_Road: {
       
   386 			int num = NUM_ROAD_ENGINES;
       
   387 			Engine *e = &_engines[ROAD_ENGINES_INDEX];
       
   388 			byte cargo;
       
   389 			EngineInfo *info;
       
   390 			engine_id = ROAD_ENGINES_INDEX;
       
   391 
       
   392 			do {
       
   393 				info = &_engine_info[engine_id];
       
   394 				if (ENGINE_AVAILABLE) {
       
   395 					if (sel[0]==0)  selected_id[0] = engine_id;
       
   396 					count++;
       
   397 					sel[0]--;							
       
   398 				}
       
   399 			} while (++engine_id,++e,--num);
       
   400 					
       
   401 			if ( selected_id[0] != -1 ) {   // only draw right array if we have anything in the left one
       
   402 				num = NUM_ROAD_ENGINES;
       
   403 				engine_id = ROAD_ENGINES_INDEX;
       
   404 				e = &_engines[ROAD_ENGINES_INDEX];
       
   405 				cargo = RoadVehInfo(selected_id[0])->cargo_type;
       
   406 					
       
   407 				do {
       
   408 					if ( cargo == RoadVehInfo(engine_id)->cargo_type && HASBIT(e->player_avail, _local_player)) {
       
   409 						count2++;
       
   410 						if (sel[1]==0)  selected_id[1] = engine_id;
       
   411 						sel[1]--;
       
   412 					}
       
   413 				} while (++engine_id,++e,--num);
       
   414 			}					
       
   415 			break;
       
   416 		}
       
   417 	
       
   418 		case VEH_Ship: {
       
   419 			int num = NUM_SHIP_ENGINES;
       
   420 			Engine *e = &_engines[SHIP_ENGINES_INDEX];
       
   421 			byte cargo; 
       
   422 			EngineInfo *info;
       
   423 			engine_id = SHIP_ENGINES_INDEX;
       
   424 			byte refittable;
       
   425 
       
   426 			do {
       
   427 				info = &_engine_info[engine_id];
       
   428 				if (ENGINE_AVAILABLE) {
       
   429 					if ( sel[0] == 0 )  selected_id[0] = engine_id;
       
   430 					count++;
       
   431 					sel[0]--;							
       
   432 				}
       
   433 			} while (++engine_id,++e,--num);
       
   434 					
       
   435 			if ( selected_id[0] != -1 ) {
       
   436 				num = NUM_SHIP_ENGINES;
       
   437 				e = &_engines[SHIP_ENGINES_INDEX];
       
   438 				engine_id = SHIP_ENGINES_INDEX;
       
   439 				cargo = ShipVehInfo(selected_id[0])->cargo_type;
       
   440 				refittable = ShipVehInfo(selected_id[0])->refittable;
       
   441 					
       
   442 				do {											
       
   443 					if (HASBIT(e->player_avail, _local_player) 
       
   444 					&& ( cargo == ShipVehInfo(engine_id)->cargo_type || refittable & ShipVehInfo(engine_id)->refittable)) {
       
   445 									
       
   446 						if ( sel[1]==0)  selected_id[1] = engine_id;
       
   447 						sel[1]--;
       
   448 						count2++;
       
   449 					}
       
   450 				} while (++engine_id,++e,--num);
       
   451 			}
       
   452 			break;
       
   453 		}   //end of ship
       
   454 				
       
   455 		case VEH_Aircraft:{
       
   456 			int num = NUM_AIRCRAFT_ENGINES;
       
   457 			Engine *e = &_engines[AIRCRAFT_ENGINES_INDEX];
       
   458 			EngineInfo *info;
       
   459 			engine_id = AIRCRAFT_ENGINES_INDEX;
       
   460 			byte subtype;
       
   461 
       
   462 			do {
       
   463 				info = &_engine_info[engine_id];
       
   464 				if (ENGINE_AVAILABLE) {
       
   465 					count++;
       
   466 					if (sel[0]==0)  selected_id[0] = engine_id;
       
   467 					sel[0]--;
       
   468 				}
       
   469 			} while (++engine_id,++e,--num);
       
   470 					
       
   471 			if ( selected_id[0] != -1 ) {
       
   472 				num = NUM_AIRCRAFT_ENGINES;
       
   473 				e = &_engines[AIRCRAFT_ENGINES_INDEX];
       
   474 				subtype = AircraftVehInfo(selected_id[0])->subtype;
       
   475 				engine_id = AIRCRAFT_ENGINES_INDEX;
       
   476 				do {
       
   477 					if (HASBIT(e->player_avail, _local_player)) {
       
   478 						if ( (subtype && AircraftVehInfo(engine_id)->subtype) || (!(subtype) && !AircraftVehInfo(engine_id)->subtype) ) {
       
   479 							count2++;
       
   480 							if (sel[1]==0)  selected_id[1] = engine_id;
       
   481 							sel[1]--;
       
   482 						}
       
   483 					}
       
   484 				} while (++engine_id,++e,--num);
       
   485 			}
       
   486 			break;
       
   487 		}
       
   488 	}
       
   489 	// sets up the number of items in each list
       
   490 	SetVScrollCount(w, count);
       
   491 	SetVScroll2Count(w, count2);
       
   492 	WP(w,replaceveh_d).sel_engine[0] = selected_id[0];
       
   493 	WP(w,replaceveh_d).sel_engine[1] = selected_id[1];
       
   494 	
       
   495 	WP(w,replaceveh_d).count[0] = count;
       
   496 	WP(w,replaceveh_d).count[1] = count2;
       
   497 	return;
       
   498 }
       
   499 
       
   500 
       
   501 static void DrawEngineArrayInReplaceWindow(Window *w, int x, int y, int x2, int y2, int pos, int pos2,
       
   502 	int sel1, int sel2, int selected_id1, int selected_id2) 
       
   503 {
       
   504 	int sel[2] = {sel1, sel2};
       
   505 	int selected_id[2] = {selected_id1, selected_id2};
       
   506 	switch (WP(w,replaceveh_d).vehicletype) {
       
   507 		case VEH_Train: {
       
   508 			byte railtype = WP(w,replaceveh_d).railtype;
       
   509 			DrawString(157, 89 + (14 * w->vscroll.cap), _rail_types_list[railtype], 0x10);
       
   510 			/* draw sorting criteria string */
       
   511 
       
   512 			/* Ensure that custom engines which substituted wagons
       
   513 			* are sorted correctly.
       
   514 			* XXX - DO NOT EVER DO THIS EVER AGAIN! GRRR hacking in wagons as
       
   515 			* engines to get more types.. Stays here until we have our own format
       
   516 			* then it is exit!!! */
       
   517 			train_engine_drawing_loop(&x, &y, &pos, &sel[0], &selected_id[0], railtype, w->vscroll.cap, true, false, true); // True engines
       
   518 			train_engine_drawing_loop(&x2, &y2, &pos2, &sel[1], &selected_id[1], railtype, w->vscroll.cap, true, false, false); // True engines
       
   519 			train_engine_drawing_loop(&x2, &y2, &pos2, &sel[1], &selected_id[1], railtype, w->vscroll.cap, false, false, false); // Feeble wagons
       
   520 			break;
       
   521 		}
       
   522 			
       
   523 		case VEH_Road: {
       
   524 			int num = NUM_ROAD_ENGINES;
       
   525 			Engine *e = &_engines[ROAD_ENGINES_INDEX];
       
   526 			int engine_id = ROAD_ENGINES_INDEX;
       
   527 			byte cargo;
       
   528 			EngineInfo *info;
       
   529 				
       
   530 			if ( selected_id[0] >= ROAD_ENGINES_INDEX && selected_id[0] <= SHIP_ENGINES_INDEX )
       
   531 				cargo = RoadVehInfo(selected_id[0])->cargo_type; 
       
   532 
       
   533 			do {
       
   534 				info = &_engine_info[engine_id];
       
   535 				if (ENGINE_AVAILABLE) {
       
   536 					if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
       
   537 						DrawString(x+59, y+2, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
       
   538 						DrawRoadVehEngine(x+29, y+6, engine_id, SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)));
       
   539 						y += 14;
       
   540 					}
       
   541 						
       
   542 					if ( RoadVehInfo(engine_id)->cargo_type == cargo && HASBIT(e->player_avail, _local_player) ) {
       
   543 						if (IS_INT_INSIDE(--pos2, -w->vscroll.cap, 0) && RoadVehInfo(engine_id)->cargo_type == cargo) {
       
   544 							DrawString(x2+59, y2+2, GetCustomEngineName(engine_id), sel[1]==0 ? 0xC : 0x10);
       
   545 							DrawRoadVehEngine(x2+29, y2+6, engine_id, SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)));
       
   546 							y2 += 14;
       
   547 						}
       
   548 						sel[1]--;
       
   549 					}
       
   550 				sel[0]--;
       
   551 				}
       
   552 			} while (++engine_id, ++e,--num);
       
   553 
       
   554 			break;
       
   555 		}
       
   556 				
       
   557 		case VEH_Ship: {
       
   558 			int num = NUM_SHIP_ENGINES;
       
   559 			Engine *e = &_engines[SHIP_ENGINES_INDEX];
       
   560 			int engine_id = SHIP_ENGINES_INDEX;
       
   561 			byte cargo, refittable;  
       
   562 			EngineInfo *info;
       
   563 
       
   564 			if ( selected_id[0] != -1 ) {
       
   565 				cargo = ShipVehInfo(selected_id[0])->cargo_type;
       
   566 				refittable = ShipVehInfo(selected_id[0])->refittable;
       
   567 			}
       
   568 				
       
   569 			do {
       
   570 				info = &_engine_info[engine_id];
       
   571 				if (ENGINE_AVAILABLE) {
       
   572 					if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
       
   573 						DrawString(x+75, y+7, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
       
   574 						DrawShipEngine(x+35, y+10, engine_id, SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)));
       
   575 						y += 24;
       
   576 					}
       
   577 					if ( selected_id[0] != -1 ) {
       
   578 						if (HASBIT(e->player_avail, _local_player) && ( cargo == ShipVehInfo(engine_id)->cargo_type || refittable & ShipVehInfo(engine_id)->refittable)) {
       
   579 							if (IS_INT_INSIDE(--pos2, -w->vscroll.cap, 0)) {
       
   580 								DrawString(x2+75, y2+7, GetCustomEngineName(engine_id), sel[1]==0 ? 0xC : 0x10);
       
   581 								DrawShipEngine(x2+35, y2+10, engine_id, SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)));
       
   582 								y2 += 24;
       
   583 							}
       
   584 							sel[1]--;
       
   585 						}
       
   586 					}
       
   587 					sel[0]--;
       
   588 				}
       
   589 			} while (++engine_id, ++e,--num);
       
   590 		break;
       
   591 		}   //end of ship
       
   592 			
       
   593 		case VEH_Aircraft: {
       
   594 			if ( selected_id[0] != -1 ) {
       
   595 				int num = NUM_AIRCRAFT_ENGINES;
       
   596 				Engine *e = &_engines[AIRCRAFT_ENGINES_INDEX];
       
   597 				int engine_id = AIRCRAFT_ENGINES_INDEX;
       
   598 				byte subtype = AircraftVehInfo(selected_id[0])->subtype;
       
   599 				EngineInfo *info;
       
   600 
       
   601 				do {
       
   602 					info = &_engine_info[engine_id];
       
   603 					if (ENGINE_AVAILABLE) {
       
   604 						if (sel[0]==0) selected_id[0] = engine_id;
       
   605 						if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
       
   606 							DrawString(x+62, y+7, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
       
   607 							DrawAircraftEngine(x+29, y+10, engine_id, SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)));
       
   608 							y += 24;
       
   609 						}
       
   610 						if ( ((subtype && AircraftVehInfo(engine_id)->subtype) || (!(subtype) && !AircraftVehInfo(engine_id)->subtype))
       
   611 							&& HASBIT(e->player_avail, _local_player) ) {
       
   612 							if (sel[1]==0) selected_id[1] = engine_id;
       
   613 							if (IS_INT_INSIDE(--pos2, -w->vscroll.cap, 0)) {
       
   614 								DrawString(x2+62, y2+7, GetCustomEngineName(engine_id), sel[1]==0 ? 0xC : 0x10);
       
   615 								DrawAircraftEngine(x2+29, y2+10, engine_id, SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)));
       
   616 								y2 += 24;
       
   617 							}
       
   618 						sel[1]--;
       
   619 						}
       
   620 					sel[0]--;
       
   621 					}
       
   622 				} while (++engine_id, ++e,--num);
       
   623 			}
       
   624 			break;
       
   625 		}   // end of aircraft
       
   626 	}
       
   627 
       
   628 }
       
   629 static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
       
   630 {
       
   631 	// these 3 variables is used if any of the lists is clicked
       
   632 	uint16 click_scroll_pos = w->vscroll2.pos;
       
   633 	uint16 click_scroll_cap = w->vscroll2.cap;
       
   634 	byte click_side = 1;
       
   635 
       
   636 	switch(e->event) {
       
   637 		case WE_PAINT:	
       
   638 			{
       
   639 				int pos = w->vscroll.pos;
       
   640 				int selected_id[2] = {-1,-1};
       
   641 				int x = 1;
       
   642 				int y = 15;
       
   643 				int pos2 = w->vscroll2.pos;
       
   644 				int x2 = 1 + 228;
       
   645 				int y2 = 15;
       
   646 				int sel[2] = { WP(w,replaceveh_d).sel_index[0], WP(w,replaceveh_d).sel_index[1]};
       
   647 			
       
   648 				SetupScrollStuffForReplaceWindow(w);
       
   649 			
       
   650 				selected_id[0] = WP(w,replaceveh_d).sel_engine[0];
       
   651 				selected_id[1] = WP(w,replaceveh_d).sel_engine[1];
       
   652 			
       
   653 			// sets the selected left item to the top one if it's greater than the number of vehicles in the left side
       
   654 
       
   655 				if ( WP(w,replaceveh_d).count[0] <= sel[0] ) {
       
   656 					if (WP(w,replaceveh_d).count[0]) {
       
   657 						sel[0] = 0;
       
   658 						WP(w,replaceveh_d).sel_index[0] = 0;
       
   659 						w->vscroll.pos = 0;
       
   660 						// now we go back to set selected_id[1] properly
       
   661 						SetWindowDirty(w);
       
   662 						return;
       
   663 					} else { //there are no vehicles in the left window
       
   664 						selected_id[1] = -1;
       
   665 					}
       
   666 				}
       
   667 
       
   668 				if ( WP(w,replaceveh_d).count[1] <= sel[1] ) {
       
   669 					if (WP(w,replaceveh_d).count[1]) {
       
   670 						sel[1] = 0;
       
   671 						WP(w,replaceveh_d).sel_index[1] = 0;
       
   672 						w->vscroll2.pos = 0;
       
   673 						// now we go back to set selected_id[1] properly
       
   674 						SetWindowDirty(w);
       
   675 						return;
       
   676 					} else { //there are no vehicles in the right window
       
   677 						selected_id[1] = -1;
       
   678 					}
       
   679 				}
       
   680 		
       
   681 				if ( selected_id[0] == selected_id[1] || _autoreplace_array[selected_id[0]] == selected_id[1]
       
   682 					|| selected_id[0] == -1 || selected_id[1] == -1 )
       
   683 					SETBIT(w->disabled_state, 4);
       
   684 				else
       
   685 					CLRBIT(w->disabled_state, 4);
       
   686 			
       
   687 				if ( _autoreplace_array[selected_id[0]] == selected_id[0] || selected_id[0] == -1 )
       
   688 					SETBIT(w->disabled_state, 6);
       
   689 				else
       
   690 					CLRBIT(w->disabled_state, 6);
       
   691 		
       
   692 				// now the actual drawing of the window itself takes place
       
   693 				DrawWindowWidgets(w);
       
   694 		
       
   695 		
       
   696 		
       
   697 				// sets up the string for the vehicle that is being replaced to
       
   698 				if ( selected_id[0] != -1 ) {
       
   699 					if ( selected_id[0] == _autoreplace_array[selected_id[0]] )
       
   700 						SetDParam(0, STR_NOT_REPLACING);
       
   701 					else
       
   702 						SetDParam(0, GetCustomEngineName(_autoreplace_array[selected_id[0]]));
       
   703 				} else {
       
   704 					SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
       
   705 				}
       
   706 					
       
   707 					
       
   708 				DrawString(145, (WP(w,replaceveh_d).line_height == 24 ? 67 : 77 ) + ( WP(w,replaceveh_d).line_height * w->vscroll.cap), STR_02BD, 0x10);
       
   709 
       
   710 		
       
   711 				/*	now we draw the two arrays according to what we just counted */
       
   712 				DrawEngineArrayInReplaceWindow(w, x, y, x2, y2, pos, pos2, sel[0], sel[1], selected_id[0], selected_id[1]);
       
   713 				
       
   714 				WP(w,replaceveh_d).sel_engine[0] = selected_id[0];
       
   715 				WP(w,replaceveh_d).sel_engine[1] = selected_id[1];
       
   716 				/* now we draw the info about the vehicles we selected */
       
   717 				switch (WP(w,replaceveh_d).vehicletype) {
       
   718 					case VEH_Train: {
       
   719 						byte i = 0;
       
   720 						int offset = 0;
       
   721 				
       
   722 						for ( i = 0 ; i < 2 ; i++) {
       
   723 							if ( i )
       
   724 							offset = 228;
       
   725 							if (selected_id[i] != -1) {
       
   726 								if (!(RailVehInfo(selected_id[i])->flags & RVI_WAGON)) {
       
   727 									/* it's an engine */
       
   728 									Set_DPARAM_Train_Engine_Build_Window(selected_id[i]);
       
   729 									DrawString(2 + offset, 15 + (14 * w->vscroll.cap), STR_8817_COST_WEIGHT_T_SPEED_POWER, 0);
       
   730 								} else {
       
   731 									/* it's a wagon. Train cars are not replaced with the current GUI, but this code is ready for newgrf if anybody adds that*/
       
   732 									Set_DPARAM_Train_Car_Build_Window(w, selected_id[i]);
       
   733 									DrawString(2 + offset, 15 + (14 * w->vscroll.cap), STR_8821_COST_WEIGHT_T_T_CAPACITY, 0);
       
   734 								}
       
   735 							}
       
   736 						}
       
   737 						break;
       
   738 					}   //end if case  VEH_Train
       
   739 			
       
   740 					case VEH_Road: {
       
   741 						if (selected_id[0] != -1) {
       
   742 							Set_DPARAM_Road_Veh_Build_Window(selected_id[0]);
       
   743 							DrawString(2, 15 + (14 * w->vscroll.cap), STR_9008_COST_SPEED_RUNNING_COST, 0);
       
   744 							if (selected_id[1] != -1) {
       
   745 								Set_DPARAM_Road_Veh_Build_Window(selected_id[1]);
       
   746 								DrawString(2 + 228, 15 + (14 * w->vscroll.cap), STR_9008_COST_SPEED_RUNNING_COST, 0);
       
   747 							}
       
   748 						}
       
   749 						break;
       
   750 					}   // end of VEH_Road
       
   751 			
       
   752 					case VEH_Ship: {
       
   753 						if (selected_id[0] != -1) {
       
   754 							Set_DPARAM_Ship_Build_Window(selected_id[0]);
       
   755 							DrawString(2, 15 + (24 * w->vscroll.cap), STR_980A_COST_SPEED_CAPACITY_RUNNING, 0);
       
   756 							if (selected_id[1] != -1) {
       
   757 								Set_DPARAM_Ship_Build_Window(selected_id[1]);
       
   758 								DrawString(2 + 228, 15 + (24 * w->vscroll.cap), STR_980A_COST_SPEED_CAPACITY_RUNNING, 0);
       
   759 							}
       
   760 						}
       
   761 						break;
       
   762 					}   // end of VEH_Ship 
       
   763 			
       
   764 					case VEH_Aircraft: {
       
   765 						if (selected_id[0] != -1) {
       
   766 							Set_DPARAM_Aircraft_Build_Window(selected_id[0]);
       
   767 							DrawString(2, 15 + (24 * w->vscroll.cap), STR_A007_COST_SPEED_CAPACITY_PASSENGERS, 0);
       
   768 							if (selected_id[1] != -1) {
       
   769 								Set_DPARAM_Aircraft_Build_Window(selected_id[1]);
       
   770 								DrawString(2 + 228, 15 + (24 * w->vscroll.cap), STR_A007_COST_SPEED_CAPACITY_PASSENGERS, 0);
       
   771 							}
       
   772 						}
       
   773 						break;
       
   774 					}   // end of VEH_Aircraft
       
   775 				}
       
   776 			}   // end of paint
       
   777 
       
   778 		case WE_CLICK: {
       
   779 			switch(e->click.widget) {
       
   780 				/*case 0:
       
   781 					DeleteWindowById(WC_REPLACE_VEHICLE, WP(w,replaceveh_d).vehicletype );
       
   782 					break;*/
       
   783 			
       
   784 				case 14: case 15:/* Select sorting criteria dropdown menu */
       
   785 				// finds mask for available engines
       
   786 				{
       
   787 					int engine_avail = 0;
       
   788 					if ( !(HASBIT(_engines[NUM_NORMAL_RAIL_ENGINES + NUM_MONORAIL_ENGINES].player_avail, _local_player))) {
       
   789 						engine_avail = 4;
       
   790 						if ( !(HASBIT(_engines[NUM_NORMAL_RAIL_ENGINES].player_avail, _local_player)))
       
   791 							engine_avail = 6;
       
   792 					}
       
   793 					ShowDropDownMenu(w, _rail_types_list, WP(w,replaceveh_d).railtype, 15, engine_avail, 1);
       
   794 					return;
       
   795 				}
       
   796 				case 4: {
       
   797 					_autoreplace_array[WP(w,replaceveh_d).sel_engine[0]] = WP(w,replaceveh_d).sel_engine[1];
       
   798 					SetWindowDirty(w);
       
   799 					break;
       
   800 				}
       
   801 				
       
   802 				case 6: {
       
   803 					_autoreplace_array[WP(w,replaceveh_d).sel_engine[0]] = WP(w,replaceveh_d).sel_engine[0];
       
   804 					SetWindowDirty(w);
       
   805 					break;
       
   806 				}
       
   807 			
       
   808 				case 7: 
       
   809 					// sets up that the left one was clicked. The default values are for the right one (9)
       
   810 					// this way, the code for 9 handles both sides
       
   811 					click_scroll_pos = w->vscroll.pos;
       
   812 					click_scroll_cap = w->vscroll.cap;
       
   813 					click_side = 0;
       
   814 				case 9: {
       
   815 					uint i = (e->click.pt.y - 14) / WP(w,replaceveh_d).line_height;
       
   816 					if (i < click_scroll_cap) {
       
   817 						WP(w,replaceveh_d).sel_index[click_side] = i + click_scroll_pos;
       
   818 						SetWindowDirty(w);
       
   819 					}
       
   820 				} break;
       
   821 			}
       
   822 		   
       
   823 		} break;
       
   824 
       
   825 		case WE_DROPDOWN_SELECT: { /* we have selected a dropdown item in the list */
       
   826 			//potiential bug: railtypes needs to be activated 0, 1, 2... If one is skipped, it messes up
       
   827 			WP(w,replaceveh_d).railtype = e->dropdown.index;	
       
   828 			SetWindowDirty(w);
       
   829 			break;
       
   830 		}
       
   831 	}
       
   832 }
       
   833 
       
   834 static const Widget _replace_rail_vehicle_widgets[] = {
       
   835 {   WWT_CLOSEBOX,    14,     0,    10,     0,    13, STR_00C5,	STR_018B_CLOSE_WINDOW},
       
   836 {    WWT_CAPTION,    14,    11,   443,     0,    13, STR_REPLACE_VEHICLES,		STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   837 {  WWT_STICKYBOX,    14,   444,   455,     0,    13, 0x0,						STR_STICKY_BUTTON},
       
   838 {      WWT_PANEL,    14,     0,   227,   126,   187, 0x0,						STR_NULL},
       
   839 { WWT_PUSHTXTBTN,    14,     0,   138,   200,   211, STR_REPLACE_VEHICLES_START,STR_REPLACE_HELP_START_BUTTON},
       
   840 {      WWT_PANEL,    14,   139,   316,   188,   199, 0x0,						STR_REPLACE_HELP_REPLACE_INFO_TAB},
       
   841 { WWT_PUSHTXTBTN,    14,   317,   455,   200,   211, STR_REPLACE_VEHICLES_STOP,	STR_REPLACE_HELP_STOP_BUTTON},
       
   842 {     WWT_MATRIX,    14,     0,   216,    14,   125, 0x801,			STR_REPLACE_HELP_LEFT_ARRAY},
       
   843 {  WWT_SCROLLBAR,    14,   217,   227,    14,   125, 0x0,			STR_0190_SCROLL_BAR_SCROLLS_LIST},
       
   844 {     WWT_MATRIX,    14,   228,   455,    14,   125, 0x801,			STR_REPLACE_HELP_RIGHT_ARRAY},
       
   845 { WWT_SCROLL2BAR,    14,   445,   455,    14,   125, 0x0,			STR_0190_SCROLL_BAR_SCROLLS_LIST},
       
   846 {      WWT_PANEL,    14,   228,   455,   126,   187, 0x0,			STR_NULL},
       
   847 // the rest are train specific stuff
       
   848 {      WWT_PANEL,    14,     0,   138,   188,   199, 0x0,			STR_NULL},
       
   849 {      WWT_PANEL,     3,   139,   153,   200,   211, 0x0,			STR_NULL},
       
   850 {      WWT_PANEL,    14,   154,   290,   200,   211, 0x0,			STR_REPLACE_HELP_RAILTYPE},
       
   851 {   WWT_CLOSEBOX,    14,   291,   301,   200,   210, STR_0225,		STR_REPLACE_HELP_RAILTYPE},
       
   852 {      WWT_PANEL,     3,   301,   316,   200,   211, 0x0,			STR_NULL},
       
   853 {      WWT_PANEL,    14,   317,   455,   188,   199, 0x0,			STR_NULL},
       
   854 
       
   855 {   WIDGETS_END},
       
   856 };
       
   857 
       
   858 static const Widget _replace_road_vehicle_widgets[] = {
       
   859 {   WWT_CLOSEBOX,    14,     0,    10,     0,    13, STR_00C5,	STR_018B_CLOSE_WINDOW},
       
   860 {    WWT_CAPTION,    14,    11,   443,     0,    13, STR_REPLACE_VEHICLES,		STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   861 {  WWT_STICKYBOX,    14,   444,   455,     0,    13, 0x0,						STR_STICKY_BUTTON},
       
   862 {      WWT_PANEL,    14,     0,   227,   126,   187, 0x0,						STR_NULL},
       
   863 { WWT_PUSHTXTBTN,    14,     0,   138,   188,   199, STR_REPLACE_VEHICLES_START,STR_REPLACE_HELP_START_BUTTON},
       
   864 {      WWT_PANEL,    14,   139,   316,   188,   199, 0x0,						STR_REPLACE_HELP_REPLACE_INFO_TAB},
       
   865 { WWT_PUSHTXTBTN,    14,   317,   455,   188,   199, STR_REPLACE_VEHICLES_STOP,	STR_REPLACE_HELP_STOP_BUTTON},
       
   866 {     WWT_MATRIX,    14,     0,   216,    14,   125, 0x801,			STR_REPLACE_HELP_LEFT_ARRAY},
       
   867 {  WWT_SCROLLBAR,    14,   217,   227,    14,   125, 0x0,			STR_0190_SCROLL_BAR_SCROLLS_LIST},
       
   868 {     WWT_MATRIX,    14,   228,   455,    14,   125, 0x801,			STR_REPLACE_HELP_RIGHT_ARRAY},
       
   869 { WWT_SCROLL2BAR,    14,   445,   455,    14,   125, 0x0,			STR_0190_SCROLL_BAR_SCROLLS_LIST},
       
   870 {      WWT_PANEL,    14,   228,   455,   126,   187, 0x0,			STR_NULL},
       
   871 {   WIDGETS_END},
       
   872 };
       
   873 
       
   874 static const Widget _replace_ship_aircraft_vehicle_widgets[] = {
       
   875 {   WWT_CLOSEBOX,    14,     0,    10,     0,    13, STR_00C5,	STR_018B_CLOSE_WINDOW},
       
   876 {    WWT_CAPTION,    14,    11,   443,     0,    13, STR_REPLACE_VEHICLES,		STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   877 {  WWT_STICKYBOX,    14,   444,   455,     0,    13, 0x0,						STR_STICKY_BUTTON},
       
   878 {      WWT_PANEL,    14,     0,   227,   110,   161, 0x0,						STR_NULL},
       
   879 { WWT_PUSHTXTBTN,    14,     0,   138,   162,   173, STR_REPLACE_VEHICLES_START,STR_REPLACE_HELP_START_BUTTON},
       
   880 {      WWT_PANEL,    14,   139,   316,   162,   173, 0x0,						STR_REPLACE_HELP_REPLACE_INFO_TAB},
       
   881 { WWT_PUSHTXTBTN,    14,   317,   455,   162,   173, STR_REPLACE_VEHICLES_STOP,	STR_REPLACE_HELP_STOP_BUTTON},
       
   882 {     WWT_MATRIX,    14,     0,   216,    14,   109, 0x401,			STR_REPLACE_HELP_LEFT_ARRAY},
       
   883 {  WWT_SCROLLBAR,    14,   217,   227,    14,   109, 0x0,			STR_0190_SCROLL_BAR_SCROLLS_LIST},
       
   884 {     WWT_MATRIX,    14,   228,   455,    14,   109, 0x401,			STR_REPLACE_HELP_RIGHT_ARRAY},
       
   885 { WWT_SCROLL2BAR,    14,   445,   455,    14,   109, 0x0,			STR_0190_SCROLL_BAR_SCROLLS_LIST},
       
   886 {      WWT_PANEL,    14,   228,   455,   110,   161, 0x0,			STR_NULL},
       
   887 {   WIDGETS_END},
       
   888 };
       
   889 
       
   890 static const WindowDesc _replace_rail_vehicle_desc = {
       
   891 	-1, -1, 456, 212,
       
   892 	WC_REPLACE_VEHICLE,0,
       
   893 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
       
   894 	_replace_rail_vehicle_widgets,
       
   895 	ReplaceVehicleWndProc
       
   896 };
       
   897 
       
   898 static const WindowDesc _replace_road_vehicle_desc = {
       
   899 	-1, -1, 456, 200,
       
   900 	WC_REPLACE_VEHICLE,0,
       
   901 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
       
   902 	_replace_road_vehicle_widgets,
       
   903 	ReplaceVehicleWndProc
       
   904 };
       
   905 
       
   906 static const WindowDesc _replace_ship_aircraft_vehicle_desc = {
       
   907 	-1, -1, 456, 174,
       
   908 	WC_REPLACE_VEHICLE,0,
       
   909 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
       
   910 	_replace_ship_aircraft_vehicle_widgets,
       
   911 	ReplaceVehicleWndProc
       
   912 };
       
   913 
       
   914 
       
   915 void ShowReplaceVehicleWindow(byte vehicletype)
       
   916 {
       
   917 	Window *w;
       
   918 	
       
   919 	DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype );
       
   920 	
       
   921 	switch (vehicletype) {
       
   922 		case VEH_Train:
       
   923 			w = AllocateWindowDescFront(&_replace_rail_vehicle_desc, vehicletype);
       
   924 			w->vscroll.cap  = 8;
       
   925 			WP(w,replaceveh_d).line_height = 14;
       
   926 			break;
       
   927 		case VEH_Road:
       
   928 			w = AllocateWindowDescFront(&_replace_road_vehicle_desc, vehicletype);
       
   929 			w->vscroll.cap  = 8;
       
   930 			WP(w,replaceveh_d).line_height = 14;
       
   931 			break;
       
   932 		case VEH_Ship: case VEH_Aircraft:
       
   933 			w = AllocateWindowDescFront(&_replace_ship_aircraft_vehicle_desc, vehicletype);
       
   934 			w->vscroll.cap  = 4;
       
   935 			WP(w,replaceveh_d).line_height = 24;
       
   936 			break;
       
   937 	}
       
   938 	WP(w,replaceveh_d).vehicletype = vehicletype;
       
   939 	w->vscroll2.cap = w->vscroll.cap;   // these two are always the same
       
   940 }