2005-07-24 16:12:37 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file train_gui.cpp GUI for trains. */
|
2007-04-04 05:21:14 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "stdafx.h"
|
2007-12-19 21:45:46 +01:00
|
|
|
#include "window_gui.h"
|
2008-01-09 10:45:45 +01:00
|
|
|
#include "gfx_func.h"
|
2007-12-21 22:50:46 +01:00
|
|
|
#include "command_func.h"
|
2004-12-10 19:16:08 +01:00
|
|
|
#include "vehicle_gui.h"
|
2005-11-19 00:41:03 +01:00
|
|
|
#include "train.h"
|
2006-02-03 13:55:21 +01:00
|
|
|
#include "newgrf_engine.h"
|
2007-12-21 20:49:27 +01:00
|
|
|
#include "strings_func.h"
|
2007-12-27 14:35:39 +01:00
|
|
|
#include "vehicle_func.h"
|
2008-04-29 23:31:29 +02:00
|
|
|
#include "engine_base.h"
|
2008-05-18 18:51:44 +02:00
|
|
|
#include "window_func.h"
|
2009-01-31 21:16:06 +01:00
|
|
|
#include "settings_type.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2008-01-13 02:21:35 +01:00
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
2005-06-24 14:38:35 +02:00
|
|
|
void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2006-06-27 23:25:53 +02:00
|
|
|
if (!success) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-04-04 05:21:14 +02:00
|
|
|
/* find a locomotive in the depot. */
|
2008-05-25 11:40:44 +02:00
|
|
|
const Vehicle *found = NULL;
|
|
|
|
const Vehicle *v;
|
2004-08-09 19:04:08 +02:00
|
|
|
FOR_ALL_VEHICLES(v) {
|
2007-03-08 17:27:54 +01:00
|
|
|
if (v->type == VEH_TRAIN && IsFrontEngine(v) &&
|
2004-08-09 19:04:08 +02:00
|
|
|
v->tile == tile &&
|
2007-02-13 11:46:45 +01:00
|
|
|
v->u.rail.track == TRACK_BIT_DEPOT) {
|
2006-06-27 23:25:53 +02:00
|
|
|
if (found != NULL) return; // must be exactly one.
|
2004-08-09 19:04:08 +02:00
|
|
|
found = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-04 05:21:14 +02:00
|
|
|
/* if we found a loco, */
|
2004-08-09 19:04:08 +02:00
|
|
|
if (found != NULL) {
|
|
|
|
found = GetLastVehicleInChain(found);
|
2007-04-04 05:21:14 +02:00
|
|
|
/* put the new wagon at the end of the loco. */
|
2008-12-28 15:37:19 +01:00
|
|
|
DoCommandP(0, _new_vehicle_id | (found->index << 16), 0, CMD_MOVE_RAIL_VEHICLE);
|
2008-05-18 18:51:44 +02:00
|
|
|
InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 14:38:35 +02:00
|
|
|
void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2005-11-14 20:48:04 +01:00
|
|
|
if (!success) return;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
const Vehicle *v = Vehicle::Get(_new_vehicle_id);
|
2004-08-09 19:04:08 +02:00
|
|
|
if (tile == _backup_orders_tile) {
|
|
|
|
_backup_orders_tile = 0;
|
2007-09-28 23:15:45 +02:00
|
|
|
RestoreVehicleOrders(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2007-08-29 22:50:58 +02:00
|
|
|
ShowVehicleViewWindow(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2005-11-04 08:38:26 +01:00
|
|
|
/**
|
|
|
|
* Get the number of pixels for the given wagon length.
|
|
|
|
* @param len Length measured in 1/8ths of a standard wagon.
|
|
|
|
* @return Number of pixels across.
|
|
|
|
*/
|
2007-07-25 02:16:30 +02:00
|
|
|
int WagonLengthToPixels(int len)
|
|
|
|
{
|
2006-05-14 22:58:12 +02:00
|
|
|
return (len * _traininfo_vehicle_width) / 8;
|
2005-11-04 08:38:26 +01:00
|
|
|
}
|
|
|
|
|
2007-12-27 14:35:39 +01:00
|
|
|
void DrawTrainImage(const Vehicle *v, int x, int y, VehicleID selection, int count, int skip)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2006-05-30 23:19:46 +02:00
|
|
|
DrawPixelInfo tmp_dpi, *old_dpi;
|
|
|
|
int dx = -(skip * 8) / _traininfo_vehicle_width;
|
2006-06-20 09:07:28 +02:00
|
|
|
/* Position of highlight box */
|
|
|
|
int highlight_l = 0;
|
|
|
|
int highlight_r = 0;
|
2006-05-30 23:19:46 +02:00
|
|
|
|
2006-08-28 09:33:51 +02:00
|
|
|
if (!FillDrawPixelInfo(&tmp_dpi, x - 2, y - 1, count + 1, 14)) return;
|
2006-05-30 23:19:46 +02:00
|
|
|
|
|
|
|
count = (count * 8) / _traininfo_vehicle_width;
|
|
|
|
|
|
|
|
old_dpi = _cur_dpi;
|
|
|
|
_cur_dpi = &tmp_dpi;
|
2005-06-07 00:44:11 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
do {
|
2006-05-30 23:19:46 +02:00
|
|
|
int width = v->u.rail.cached_veh_length;
|
2005-06-07 00:44:11 +02:00
|
|
|
|
2006-05-30 23:19:46 +02:00
|
|
|
if (dx + width > 0) {
|
|
|
|
if (dx <= count) {
|
2007-01-14 20:57:49 +01:00
|
|
|
SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
|
2007-07-01 21:11:47 +02:00
|
|
|
DrawSprite(v->GetImage(DIR_W), pal, 16 + WagonLengthToPixels(dx), 7 + (is_custom_sprite(RailVehInfo(v->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0));
|
2006-06-20 09:07:28 +02:00
|
|
|
if (v->index == selection) {
|
|
|
|
/* Set the highlight position */
|
|
|
|
highlight_l = WagonLengthToPixels(dx) + 1;
|
|
|
|
highlight_r = WagonLengthToPixels(dx + width) + 1;
|
2008-04-05 13:27:50 +02:00
|
|
|
} else if (_cursor.vehchain && highlight_r != 0) {
|
|
|
|
highlight_r += WagonLengthToPixels(width);
|
2006-06-20 09:07:28 +02:00
|
|
|
}
|
2005-06-07 00:44:11 +02:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2006-05-30 23:19:46 +02:00
|
|
|
dx += width;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-08-30 15:03:56 +02:00
|
|
|
v = v->Next();
|
2006-05-30 23:19:46 +02:00
|
|
|
} while (dx < count && v != NULL);
|
|
|
|
|
2006-06-20 09:07:28 +02:00
|
|
|
if (highlight_l != highlight_r) {
|
|
|
|
/* Draw the highlight. Now done after drawing all the engines, as
|
|
|
|
* the next engine after the highlight could overlap it. */
|
2008-08-01 05:43:53 +02:00
|
|
|
DrawFrameRect(highlight_l, 0, highlight_r, 13, COLOUR_WHITE, FR_BORDERONLY);
|
2006-06-20 09:07:28 +02:00
|
|
|
}
|
|
|
|
|
2006-05-30 23:19:46 +02:00
|
|
|
_cur_dpi = old_dpi;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2009-03-22 11:37:51 +01:00
|
|
|
/**
|
|
|
|
* Draw the details cargo tab for the given vehicle at the given position
|
|
|
|
*
|
|
|
|
* @param v current vehicle
|
|
|
|
* @param left The left most coordinate to draw
|
|
|
|
* @param right The right most coordinate to draw
|
|
|
|
* @param y The y coordinate
|
|
|
|
*/
|
|
|
|
static void TrainDetailsCargoTab(const Vehicle *v, int left, int right, int y)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
if (v->cargo_cap != 0) {
|
2009-04-22 01:40:56 +02:00
|
|
|
StringID str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
|
2006-08-31 17:22:03 +02:00
|
|
|
|
2007-06-22 13:58:59 +02:00
|
|
|
if (!v->cargo.Empty()) {
|
2004-12-02 23:53:07 +01:00
|
|
|
SetDParam(0, v->cargo_type);
|
2007-06-22 13:58:59 +02:00
|
|
|
SetDParam(1, v->cargo.Count());
|
|
|
|
SetDParam(2, v->cargo.Source());
|
2008-05-29 17:13:28 +02:00
|
|
|
SetDParam(3, _settings_game.vehicle.freight_trains);
|
2009-04-22 01:40:56 +02:00
|
|
|
str = FreightWagonMult(v->cargo_type) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT : STR_VEHICLE_DETAILS_CARGO_FROM;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2009-04-26 16:52:56 +02:00
|
|
|
DrawString(left, right, y, str);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-22 11:37:51 +01:00
|
|
|
/**
|
|
|
|
* Draw the details info tab for the given vehicle at the given position
|
|
|
|
*
|
|
|
|
* @param v current vehicle
|
|
|
|
* @param left The left most coordinate to draw
|
|
|
|
* @param right The right most coordinate to draw
|
|
|
|
* @param y The y coordinate
|
|
|
|
*/
|
|
|
|
static void TrainDetailsInfoTab(const Vehicle *v, int left, int right, int y)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-01-30 12:53:35 +01:00
|
|
|
if (RailVehInfo(v->engine_type)->railveh_type == RAILVEH_WAGON) {
|
2007-06-25 16:46:32 +02:00
|
|
|
SetDParam(0, v->engine_type);
|
2007-06-21 19:25:17 +02:00
|
|
|
SetDParam(1, v->value);
|
2009-04-26 17:26:19 +02:00
|
|
|
DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE);
|
2006-08-31 17:22:03 +02:00
|
|
|
} else {
|
2007-06-25 16:46:32 +02:00
|
|
|
SetDParam(0, v->engine_type);
|
2006-08-20 21:05:28 +02:00
|
|
|
SetDParam(1, v->build_year);
|
2007-06-21 19:25:17 +02:00
|
|
|
SetDParam(2, v->value);
|
2009-04-26 17:26:19 +02:00
|
|
|
DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE);
|
2004-09-10 21:02:27 +02:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2009-03-22 11:37:51 +01:00
|
|
|
/**
|
|
|
|
* Draw the details capacity tab for the given vehicle at the given position
|
|
|
|
*
|
|
|
|
* @param v current vehicle
|
|
|
|
* @param left The left most coordinate to draw
|
|
|
|
* @param right The right most coordinate to draw
|
|
|
|
* @param y The y coordinate
|
|
|
|
*/
|
|
|
|
static void TrainDetailsCapacityTab(const Vehicle *v, int left, int right, int y)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
if (v->cargo_cap != 0) {
|
2006-10-20 13:53:29 +02:00
|
|
|
SetDParam(0, v->cargo_type);
|
2004-12-02 23:53:07 +01:00
|
|
|
SetDParam(1, v->cargo_cap);
|
2009-04-20 23:29:41 +02:00
|
|
|
SetDParam(4, GetCargoSubtypeText(v));
|
|
|
|
SetDParam(5, _settings_game.vehicle.freight_trains);
|
2009-04-26 16:52:56 +02:00
|
|
|
DrawString(left, right, y, FreightWagonMult(v->cargo_type) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT : STR_VEHICLE_INFO_CAPACITY);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-06 01:26:45 +02:00
|
|
|
int GetTrainDetailsWndVScroll(VehicleID veh_id, byte det_tab)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2006-08-31 17:22:03 +02:00
|
|
|
AcceptedCargo act_cargo;
|
|
|
|
AcceptedCargo max_cargo;
|
2007-09-06 01:26:45 +02:00
|
|
|
int num = 0;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-08-31 17:22:03 +02:00
|
|
|
if (det_tab == 3) { // Total cargo tab
|
2007-09-06 01:26:45 +02:00
|
|
|
memset(max_cargo, 0, sizeof(max_cargo));
|
|
|
|
memset(act_cargo, 0, sizeof(act_cargo));
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
for (const Vehicle *v = Vehicle::Get(veh_id) ; v != NULL ; v = v->Next()) {
|
2007-09-06 01:26:45 +02:00
|
|
|
act_cargo[v->cargo_type] += v->cargo.Count();
|
|
|
|
max_cargo[v->cargo_type] += v->cargo_cap;
|
|
|
|
}
|
2006-08-31 17:22:03 +02:00
|
|
|
|
|
|
|
/* Set scroll-amount seperately from counting, as to not compute num double
|
|
|
|
* for more carriages of the same type
|
|
|
|
*/
|
2007-03-21 14:19:01 +01:00
|
|
|
for (CargoID i = 0; i < NUM_CARGO; i++) {
|
2006-08-31 17:22:03 +02:00
|
|
|
if (max_cargo[i] > 0) num++; // only count carriages that the train has
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2006-08-28 20:53:03 +02:00
|
|
|
num++; // needs one more because first line is description string
|
2006-08-31 17:22:03 +02:00
|
|
|
} else {
|
2009-05-17 01:34:14 +02:00
|
|
|
for (const Vehicle *v = Vehicle::Get(veh_id) ; v != NULL ; v = v->Next()) {
|
2007-09-06 01:26:45 +02:00
|
|
|
if (!IsArticulatedPart(v) || v->cargo_cap != 0) num++;
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-09-06 01:26:45 +02:00
|
|
|
return num;
|
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2009-03-22 11:37:51 +01:00
|
|
|
/**
|
|
|
|
* Draw the details for the given vehicle at the given position
|
|
|
|
*
|
|
|
|
* @param v current vehicle
|
|
|
|
* @param left The left most coordinate to draw
|
|
|
|
* @param right The right most coordinate to draw
|
|
|
|
* @param y The y coordinate
|
|
|
|
*/
|
|
|
|
void DrawTrainDetails(const Vehicle *v, int left, int right, int y, int vscroll_pos, uint16 vscroll_cap, byte det_tab)
|
2007-09-06 01:26:45 +02:00
|
|
|
{
|
2007-04-04 05:21:14 +02:00
|
|
|
/* draw the first 3 details tabs */
|
2004-08-09 19:04:08 +02:00
|
|
|
if (det_tab != 3) {
|
2007-09-06 01:26:45 +02:00
|
|
|
const Vehicle *u = v;
|
2009-03-22 11:37:51 +01:00
|
|
|
int x = 1;
|
2006-02-01 08:36:15 +01:00
|
|
|
for (;;) {
|
2007-09-06 01:26:45 +02:00
|
|
|
if (--vscroll_pos < 0 && vscroll_pos >= -vscroll_cap) {
|
2005-11-05 17:07:26 +01:00
|
|
|
int dx = 0;
|
2006-08-31 17:22:03 +02:00
|
|
|
|
2005-11-05 17:07:26 +01:00
|
|
|
u = v;
|
|
|
|
do {
|
2007-01-14 20:57:49 +01:00
|
|
|
SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
|
2007-07-01 21:11:47 +02:00
|
|
|
DrawSprite(u->GetImage(DIR_W), pal, x + WagonLengthToPixels(4 + dx), y + 6 + (is_custom_sprite(RailVehInfo(u->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0));
|
2005-11-05 17:07:26 +01:00
|
|
|
dx += u->u.rail.cached_veh_length;
|
2007-08-30 15:03:56 +02:00
|
|
|
u = u->Next();
|
2006-10-17 09:33:05 +02:00
|
|
|
} while (u != NULL && IsArticulatedPart(u) && u->cargo_cap == 0);
|
2006-08-31 17:22:03 +02:00
|
|
|
|
2008-04-07 14:36:50 +02:00
|
|
|
int px = x + WagonLengthToPixels(dx) + 2;
|
|
|
|
int py = y + 2;
|
2006-08-31 17:22:03 +02:00
|
|
|
switch (det_tab) {
|
|
|
|
default: NOT_REACHED();
|
2009-03-22 11:37:51 +01:00
|
|
|
case 0: TrainDetailsCargoTab( v, px, right, py); break;
|
2006-10-17 09:33:05 +02:00
|
|
|
case 1:
|
2007-04-04 05:21:14 +02:00
|
|
|
/* Only show name and value for the 'real' part */
|
2006-10-17 09:33:05 +02:00
|
|
|
if (!IsArticulatedPart(v)) {
|
2009-03-22 11:37:51 +01:00
|
|
|
TrainDetailsInfoTab(v, px, right, py);
|
2006-10-17 09:33:05 +02:00
|
|
|
}
|
|
|
|
break;
|
2009-03-22 11:37:51 +01:00
|
|
|
case 2: TrainDetailsCapacityTab(v, px, right, py); break;
|
2006-08-31 17:22:03 +02:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
y += 14;
|
2006-10-17 09:33:05 +02:00
|
|
|
|
|
|
|
v = u;
|
|
|
|
} else {
|
2007-04-04 05:21:14 +02:00
|
|
|
/* Move to the next line */
|
2006-10-17 09:33:05 +02:00
|
|
|
do {
|
2007-08-30 15:03:56 +02:00
|
|
|
v = v->Next();
|
2006-10-17 09:33:05 +02:00
|
|
|
} while (v != NULL && IsArticulatedPart(v) && v->cargo_cap == 0);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2006-08-31 17:22:03 +02:00
|
|
|
if (v == NULL) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2006-02-02 09:03:10 +01:00
|
|
|
} else {
|
2007-09-06 01:26:45 +02:00
|
|
|
AcceptedCargo act_cargo;
|
|
|
|
AcceptedCargo max_cargo;
|
2008-08-18 18:52:40 +02:00
|
|
|
Money feeder_share = 0;
|
2007-09-06 01:26:45 +02:00
|
|
|
|
|
|
|
memset(max_cargo, 0, sizeof(max_cargo));
|
|
|
|
memset(act_cargo, 0, sizeof(act_cargo));
|
|
|
|
|
|
|
|
for (const Vehicle *u = v; u != NULL ; u = u->Next()) {
|
|
|
|
act_cargo[u->cargo_type] += u->cargo.Count();
|
|
|
|
max_cargo[u->cargo_type] += u->cargo_cap;
|
2008-08-18 18:52:40 +02:00
|
|
|
feeder_share += u->cargo.FeederShare();
|
2007-09-06 01:26:45 +02:00
|
|
|
}
|
|
|
|
|
2007-04-04 05:21:14 +02:00
|
|
|
/* draw total cargo tab */
|
2009-04-26 16:52:56 +02:00
|
|
|
DrawString(left, right, y + 2, STR_TOTAL_CAPACITY_TEXT);
|
2007-03-21 14:19:01 +01:00
|
|
|
for (CargoID i = 0; i < NUM_CARGO; i++) {
|
2007-09-06 01:26:45 +02:00
|
|
|
if (max_cargo[i] > 0 && --vscroll_pos < 0 && vscroll_pos > -vscroll_cap) {
|
2004-08-09 19:04:08 +02:00
|
|
|
y += 14;
|
2006-08-31 17:22:03 +02:00
|
|
|
SetDParam(0, i); // {CARGO} #1
|
|
|
|
SetDParam(1, act_cargo[i]); // {CARGO} #2
|
|
|
|
SetDParam(2, i); // {SHORTCARGO} #1
|
|
|
|
SetDParam(3, max_cargo[i]); // {SHORTCARGO} #2
|
2008-05-29 17:13:28 +02:00
|
|
|
SetDParam(4, _settings_game.vehicle.freight_trains);
|
2009-04-26 16:52:56 +02:00
|
|
|
DrawString(left, right, y + 2, FreightWagonMult(i) > 1 ? STR_TOTAL_CAPACITY_MULT : STR_TOTAL_CAPACITY);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2006-02-02 09:03:10 +01:00
|
|
|
}
|
2008-08-18 18:52:40 +02:00
|
|
|
SetDParam(0, feeder_share);
|
2009-04-26 16:52:56 +02:00
|
|
|
DrawString(left, right, y + 15, STR_FEEDER_CARGO_VALUE);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|