train_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "window_gui.h"
00014 #include "gfx_func.h"
00015 #include "command_func.h"
00016 #include "vehicle_gui.h"
00017 #include "train.h"
00018 #include "strings_func.h"
00019 #include "vehicle_func.h"
00020 #include "window_func.h"
00021 
00022 #include "table/sprites.h"
00023 #include "table/strings.h"
00024 
00032 void CcBuildWagon(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00033 {
00034   if (result.Failed()) return;
00035 
00036   /* find a locomotive in the depot. */
00037   const Vehicle *found = NULL;
00038   const Train *t;
00039   FOR_ALL_TRAINS(t) {
00040     if (t->IsFrontEngine() && t->tile == tile &&
00041         t->track == TRACK_BIT_DEPOT) {
00042       if (found != NULL) return; // must be exactly one.
00043       found = t;
00044     }
00045   }
00046 
00047   /* if we found a loco, */
00048   if (found != NULL) {
00049     found = found->Last();
00050     /* put the new wagon at the end of the loco. */
00051     DoCommandP(0, _new_vehicle_id, found->index, CMD_MOVE_RAIL_VEHICLE);
00052     InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
00053   }
00054 }
00055 
00063 static int HighlightDragPosition(int px, int max_width, VehicleID selection)
00064 {
00065   bool rtl = _current_text_dir == TD_RTL;
00066 
00067   assert(selection != INVALID_VEHICLE);
00068   Point offset;
00069   int dragged_width = Train::Get(selection)->GetDisplayImageWidth(&offset) + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00070 
00071   int drag_hlight_left = rtl ? max(px -dragged_width, 0) : px;
00072   int drag_hlight_right = rtl ? px : min(px + dragged_width, max_width);
00073   int drag_hlight_width = max(drag_hlight_right - drag_hlight_left, 0);
00074 
00075   if (drag_hlight_width > 0) {
00076     GfxFillRect(drag_hlight_left + WD_FRAMERECT_LEFT, WD_FRAMERECT_TOP + 1,
00077         drag_hlight_right - WD_FRAMERECT_RIGHT, 13 - WD_FRAMERECT_BOTTOM, _colour_gradient[COLOUR_GREY][7]);
00078   }
00079 
00080   return drag_hlight_width;
00081 }
00082 
00093 void DrawTrainImage(const Train *v, int left, int right, int y, VehicleID selection, int skip, VehicleID drag_dest)
00094 {
00095   bool rtl = _current_text_dir == TD_RTL;
00096   Direction dir = rtl ? DIR_E : DIR_W;
00097 
00098   DrawPixelInfo tmp_dpi, *old_dpi;
00099   /* Position of highlight box */
00100   int highlight_l = 0;
00101   int highlight_r = 0;
00102   int max_width = right - left + 1;
00103 
00104   if (!FillDrawPixelInfo(&tmp_dpi, left, y, max_width, 14)) return;
00105 
00106   old_dpi = _cur_dpi;
00107   _cur_dpi = &tmp_dpi;
00108 
00109   int px = rtl ? max_width + skip : -skip;
00110   bool sel_articulated = false;
00111   bool dragging = (drag_dest != INVALID_VEHICLE);
00112   bool drag_at_end_of_train = (drag_dest == v->index); // Head index is used to mark dragging at end of train.
00113   for (; v != NULL && (rtl ? px > 0 : px < max_width); v = v->Next()) {
00114     if (dragging && !drag_at_end_of_train && drag_dest == v->index) {
00115       /* Highlight the drag-and-drop destination inside the train. */
00116       int drag_hlight_width = HighlightDragPosition(px, max_width, selection);
00117       px += rtl ? -drag_hlight_width : drag_hlight_width;
00118     }
00119 
00120     Point offset;
00121     int width = Train::From(v)->GetDisplayImageWidth(&offset);
00122 
00123     if (rtl ? px + width > 0 : px - width < max_width) {
00124       PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
00125       DrawSprite(v->GetImage(dir), pal, px + (rtl ? -offset.x : offset.x), 7 + offset.y);
00126     }
00127 
00128     if (!v->IsArticulatedPart()) sel_articulated = false;
00129 
00130     if (v->index == selection) {
00131       /* Set the highlight position */
00132       highlight_l = rtl ? px - width : px;
00133       highlight_r = rtl ? px - 1 : px + width - 1;
00134       sel_articulated = true;
00135     } else if ((_cursor.vehchain && highlight_r != 0) || sel_articulated) {
00136       if (rtl) {
00137         highlight_l -= width;
00138       } else {
00139         highlight_r += width;
00140       }
00141     }
00142 
00143     px += rtl ? -width : width;
00144   }
00145 
00146   if (dragging && drag_at_end_of_train) {
00147     /* Highlight the drag-and-drop destination at the end of the train. */
00148     HighlightDragPosition(px, max_width, selection);
00149   }
00150 
00151   if (highlight_l != highlight_r) {
00152     /* Draw the highlight. Now done after drawing all the engines, as
00153      * the next engine after the highlight could overlap it. */
00154     DrawFrameRect(highlight_l, 0, highlight_r, 13, COLOUR_WHITE, FR_BORDERONLY);
00155   }
00156 
00157   _cur_dpi = old_dpi;
00158 }
00159 
00161 struct CargoSummaryItem {
00162   CargoID cargo;    
00163   StringID subtype; 
00164   uint capacity;    
00165   uint amount;      
00166   StationID source; 
00167 
00169   FORCEINLINE bool operator != (const CargoSummaryItem &other) const
00170   {
00171     return this->cargo != other.cargo || this->subtype != other.subtype;
00172   }
00173 };
00174 
00175 static const uint TRAIN_DETAILS_MIN_INDENT = 32; 
00176 static const uint TRAIN_DETAILS_MAX_INDENT = 72; 
00177 
00179 typedef SmallVector<CargoSummaryItem, 2> CargoSummary;
00181 static CargoSummary _cargo_summary;
00182 
00191 static void TrainDetailsCargoTab(const CargoSummaryItem *item, int left, int right, int y)
00192 {
00193   StringID str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
00194 
00195   if (item->amount > 0) {
00196     SetDParam(0, item->cargo);
00197     SetDParam(1, item->amount);
00198     SetDParam(2, item->source);
00199     SetDParam(3, _settings_game.vehicle.freight_trains);
00200     str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT : STR_VEHICLE_DETAILS_CARGO_FROM;
00201   }
00202 
00203   DrawString(left, right, y, str);
00204 }
00205 
00214 static void TrainDetailsInfoTab(const Train *v, int left, int right, int y, byte line_number)
00215 {
00216   const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
00217   bool show_speed = !UsesWagonOverride(v) && (_settings_game.vehicle.wagon_speed_limits || rvi->railveh_type != RAILVEH_WAGON);
00218   uint16 speed;
00219 
00220   if (rvi->railveh_type == RAILVEH_WAGON) {
00221     SetDParam(0, v->engine_type);
00222     SetDParam(1, v->value);
00223 
00224     if ( show_speed && (speed = GetVehicleProperty(v, PROP_TRAIN_SPEED, rvi->max_speed))) {
00225       SetDParam(2, speed); // StringID++
00226       DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE_AND_SPEED, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00227     } else {
00228       DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00229     }
00230   } else {
00231     switch (line_number) {
00232       case 0:
00233         SetDParam(0, v->engine_type);
00234         SetDParam(1, v->build_year);
00235         SetDParam(2, v->value);
00236 
00237         if (show_speed && (speed = GetVehicleProperty(v, PROP_TRAIN_SPEED, rvi->max_speed))) {
00238           SetDParam(3, speed); // StringID++
00239           DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE_AND_SPEED, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00240         } else {
00241           DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00242         }         
00243         break;
00244 
00245       case 1:
00246         SetDParam(0, v->reliability * 100 >> 16);
00247         SetDParam(1, v->breakdowns_since_last_service);
00248         DrawString(left, right, y, STR_VEHICLE_INFO_RELIABILITY_BREAKDOWNS, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00249         break;
00250 
00251       case 2:
00252         if (v->breakdown_ctr == 1) {
00253           if (_settings_game.vehicle.improved_breakdowns) {
00254             SetDParam(0, STR_VEHICLE_STATUS_BROKEN_DOWN_VEL);
00255             SetDParam(1, STR_BREAKDOWN_TYPE_CRITICAL + v->breakdown_type);
00256             if (v->breakdown_type == BREAKDOWN_LOW_SPEED) {
00257               SetDParam(2, min(v->First()->GetCurrentMaxSpeed(), v->breakdown_severity));
00258             } else if (v->breakdown_type == BREAKDOWN_LOW_POWER) {
00259               SetDParam(2, v->breakdown_severity * 100 / 256);
00260             }
00261           } else {
00262             SetDParam(0, STR_VEHICLE_STATUS_BROKEN_DOWN);
00263           }
00264         } else {
00265           if (HasBit(v->flags, VRF_NEED_REPAIR)) {
00266             SetDParam(0, STR_NEED_REPAIR);
00267             SetDParam(1, v->vcache.cached_max_speed);
00268           } else {
00269             SetDParam(0, STR_RUNNING);
00270           }
00271         }
00272         DrawString(left, right, y, STR_CURRENT_STATUS, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00273         break;
00274       default: NOT_REACHED();
00275     }
00276   }
00277 }
00278 
00287 static void TrainDetailsCapacityTab(const CargoSummaryItem *item, int left, int right, int y)
00288 {
00289   SetDParam(0, item->cargo);
00290   SetDParam(1, item->capacity);
00291   SetDParam(4, item->subtype);
00292   SetDParam(5, _settings_game.vehicle.freight_trains);
00293   DrawString(left, right, y, FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT : STR_VEHICLE_INFO_CAPACITY);
00294 }
00295 
00301 static void GetCargoSummaryOfArticulatedVehicle(const Train *v, CargoSummary *summary)
00302 {
00303   summary->Clear();
00304   do {
00305     if (v->cargo_cap == 0) continue;
00306 
00307     CargoSummaryItem new_item;
00308     new_item.cargo = v->cargo_type;
00309     new_item.subtype = GetCargoSubtypeText(v);
00310 
00311     CargoSummaryItem *item = summary->Find(new_item);
00312     if (item == summary->End()) {
00313       item = summary->Append();
00314       item->cargo = new_item.cargo;
00315       item->subtype = new_item.subtype;
00316       item->capacity = 0;
00317       item->amount = 0;
00318       item->source = INVALID_STATION;
00319     }
00320 
00321     item->capacity += v->cargo_cap;
00322     item->amount += v->cargo.Count();
00323     if (item->source == INVALID_STATION) item->source = v->cargo.Source();
00324   } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
00325 }
00326 
00332 static uint GetLengthOfArticulatedVehicle(const Train *v)
00333 {
00334   uint length = 0;
00335 
00336   do {
00337     length += v->GetDisplayImageWidth();
00338   } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
00339 
00340   return length;
00341 }
00342 
00349 int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab)
00350 {
00351   int num = 0;
00352 
00353   if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab
00354     CargoArray act_cargo;
00355     CargoArray max_cargo;
00356     for (const Vehicle *v = Vehicle::Get(veh_id); v != NULL; v = v->Next()) {
00357       act_cargo[v->cargo_type] += v->cargo.Count();
00358       max_cargo[v->cargo_type] += v->cargo_cap;
00359     }
00360 
00361     /* Set scroll-amount seperately from counting, as to not compute num double
00362      * for more carriages of the same type
00363      */
00364     for (CargoID i = 0; i < NUM_CARGO; i++) {
00365       if (max_cargo[i] > 0) num++; // only count carriages that the train has
00366     }
00367     num++; // needs one more because first line is description string
00368   } else {
00369     for (const Train *v = Train::Get(veh_id); v != NULL; v = v->GetNextVehicle()) {
00370       GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
00371       num += max(1u, _cargo_summary.Length());
00372 
00373       uint length = GetLengthOfArticulatedVehicle(v);
00374       if (length > TRAIN_DETAILS_MAX_INDENT) num++;
00375     }
00376     if (det_tab == 1) num += 2 * Train::Get(veh_id)->tcache.cached_num_engines;
00377   }
00378 
00379   return num;
00380 }
00381 
00393 void DrawTrainDetails(const Train *v, int left, int right, int y, int vscroll_pos, uint16 vscroll_cap, TrainDetailsWindowTabs det_tab)
00394 {
00395   /* draw the first 3 details tabs */
00396   if (det_tab != TDW_TAB_TOTALS) {
00397     bool rtl = _current_text_dir == TD_RTL;
00398     Direction dir = rtl ? DIR_E : DIR_W;
00399     int x = rtl ? right : left;
00400     byte line_number = 0;
00401     int sprite_y_offset = 4 + (FONT_HEIGHT_NORMAL - 10) / 2;
00402     int line_height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
00403     for (; v != NULL && vscroll_pos > -vscroll_cap; v = v->GetNextVehicle()) {
00404       GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
00405 
00406       /* Draw sprites */
00407       uint dx = 0;
00408       int px = x;
00409       const Train *u = v;
00410       do {
00411         Point offset;
00412         int width = u->GetDisplayImageWidth(&offset);
00413         if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap && line_number == 0) {
00414           PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
00415           DrawSprite(u->GetImage(dir), pal, px + (rtl ? -offset.x : offset.x), y - line_height * vscroll_pos + sprite_y_offset + offset.y);
00416         }
00417         px += rtl ? -width : width;
00418         dx += width;
00419         u = u->Next();
00420       } while (u != NULL && u->IsArticulatedPart());
00421 
00422       bool separate_sprite_row = (dx > TRAIN_DETAILS_MAX_INDENT);
00423       if (separate_sprite_row) {
00424         vscroll_pos--;
00425         dx = 0;
00426       }
00427 
00428       uint num_lines = max(1u, _cargo_summary.Length());
00429       for (uint i = 0; i < num_lines; i++) {
00430 // TODO: While i++ is removed from the for loop the game will crash in the info tab when an improved breakdown occurs,
00431 //       but with it present the status line(s) of the engine(s) do(es) not show up in the information tab.
00432 //       Temporary fixes: (or not and)
00433 //        - outcommenting the assert in strings_func.h line 81. (Not very bright, I know ;) )
00434 //        - Put back i++ in for loop above.
00435 //
00436 // For now i++ is put back so some info is missing in the gui. (outcommenting the assert may have other unwanted effects.)
00437         int sprite_width = max<int>(dx, TRAIN_DETAILS_MIN_INDENT) + 3;
00438         int data_left  = left + (rtl ? 0 : sprite_width);
00439         int data_right = right - (rtl ? sprite_width : 0);
00440         if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
00441           int py = y - line_height * vscroll_pos;
00442           if (i > 0 || separate_sprite_row) {
00443             if (vscroll_pos != 0) GfxFillRect(left, py - WD_MATRIX_TOP - 1, right, py - WD_MATRIX_TOP, _colour_gradient[COLOUR_GREY][5]);
00444           }
00445           switch (det_tab) {
00446             case TDW_TAB_CARGO:
00447               if (i < _cargo_summary.Length()) {
00448                 TrainDetailsCargoTab(&_cargo_summary[i], data_left, data_right, py);
00449               } else {
00450                 DrawString(data_left, data_right, py, STR_QUANTITY_N_A, TC_LIGHT_BLUE);
00451               }
00452               break;
00453 
00454             case TDW_TAB_INFO:
00455               if (i == 0) TrainDetailsInfoTab(v, data_left, data_right, py, line_number);
00456               break;
00457 
00458             case TDW_TAB_CAPACITY:
00459               if (i < _cargo_summary.Length()) {
00460                 TrainDetailsCapacityTab(&_cargo_summary[i], data_left, data_right, py);
00461               } else {
00462                 DrawString(data_left, data_right, py, STR_VEHICLE_INFO_NO_CAPACITY);
00463               }
00464               break;
00465 
00466             default: NOT_REACHED();
00467           }
00468         }
00469         if (det_tab != 1 || line_number >= (Train::From(v)->IsWagon() ? 0 : 2)) {
00470           line_number = 0;
00471            i++;
00472         } else {
00473           line_number++;
00474         }
00475         vscroll_pos--;
00476       }
00477     }
00478   } else {
00479     CargoArray act_cargo;
00480     CargoArray max_cargo;
00481     Money feeder_share = 0;
00482 
00483     for (const Vehicle *u = v; u != NULL; u = u->Next()) {
00484       act_cargo[u->cargo_type] += u->cargo.Count();
00485       max_cargo[u->cargo_type] += u->cargo_cap;
00486       feeder_share             += u->cargo.FeederShare();
00487     }
00488 
00489     /* draw total cargo tab */
00490     DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_TEXT);
00491     y += WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
00492 
00493     for (CargoID i = 0; i < NUM_CARGO; i++) {
00494       if (max_cargo[i] > 0 && --vscroll_pos < 0 && vscroll_pos > -vscroll_cap) {
00495         SetDParam(0, i);            // {CARGO} #1
00496         SetDParam(1, act_cargo[i]); // {CARGO} #2
00497         SetDParam(2, i);            // {SHORTCARGO} #1
00498         SetDParam(3, max_cargo[i]); // {SHORTCARGO} #2
00499         SetDParam(4, _settings_game.vehicle.freight_trains);
00500         DrawString(left, right, y, FreightWagonMult(i) > 1 ? STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_MULT : STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY);
00501         y += WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
00502       }
00503     }
00504     SetDParam(0, feeder_share);
00505     DrawString(left, right, y, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
00506   }
00507 }