OpenTTD Source  13.1
ship_cmd.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "ship.h"
12 #include "landscape.h"
13 #include "timetable.h"
14 #include "news_func.h"
15 #include "company_func.h"
17 #include "depot_base.h"
18 #include "station_base.h"
19 #include "newgrf_engine.h"
20 #include "pathfinder/yapf/yapf.h"
21 #include "newgrf_sound.h"
22 #include "spritecache.h"
23 #include "strings_func.h"
24 #include "window_func.h"
25 #include "date_func.h"
26 #include "vehicle_func.h"
27 #include "sound_func.h"
28 #include "ai/ai.hpp"
29 #include "game/game.hpp"
30 #include "engine_base.h"
31 #include "company_base.h"
32 #include "tunnelbridge_map.h"
33 #include "zoom_func.h"
34 #include "framerate_type.h"
35 #include "industry.h"
36 #include "industry_map.h"
37 #include "ship_cmd.h"
38 
39 #include "table/strings.h"
40 
41 #include "safeguards.h"
42 
49 {
50  if (HasTileWaterClass(tile)) return GetWaterClass(tile);
51  if (IsTileType(tile, MP_TUNNELBRIDGE)) {
53  return WATER_CLASS_CANAL;
54  }
55  if (IsTileType(tile, MP_RAILWAY)) {
56  assert(GetRailGroundType(tile) == RAIL_GROUND_WATER);
57  return WATER_CLASS_SEA;
58  }
59  NOT_REACHED();
60 }
61 
62 static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
63 
64 template <>
65 bool IsValidImageIndex<VEH_SHIP>(uint8 image_index)
66 {
67  return image_index < lengthof(_ship_sprites);
68 }
69 
70 static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
71 {
73 }
74 
75 static void GetShipIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
76 {
77  const Engine *e = Engine::Get(engine);
78  uint8 spritenum = e->u.ship.image_index;
79 
80  if (is_custom_sprite(spritenum)) {
81  GetCustomVehicleIcon(engine, DIR_W, image_type, result);
82  if (result->IsValid()) return;
83 
84  spritenum = e->original_image_index;
85  }
86 
87  assert(IsValidImageIndex<VEH_SHIP>(spritenum));
88  result->Set(DIR_W + _ship_sprites[spritenum]);
89 }
90 
91 void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
92 {
93  VehicleSpriteSeq seq;
94  GetShipIcon(engine, image_type, &seq);
95 
96  Rect rect;
97  seq.GetBounds(&rect);
98  preferred_x = Clamp(preferred_x,
99  left - UnScaleGUI(rect.left),
100  right - UnScaleGUI(rect.right));
101 
102  seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
103 }
104 
114 void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
115 {
116  VehicleSpriteSeq seq;
117  GetShipIcon(engine, image_type, &seq);
118 
119  Rect rect;
120  seq.GetBounds(&rect);
121 
122  width = UnScaleGUI(rect.Width());
123  height = UnScaleGUI(rect.Height());
124  xoffs = UnScaleGUI(rect.left);
125  yoffs = UnScaleGUI(rect.top);
126 }
127 
128 void Ship::GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
129 {
130  uint8 spritenum = this->spritenum;
131 
132  if (image_type == EIT_ON_MAP) direction = this->rotation;
133 
134  if (is_custom_sprite(spritenum)) {
135  GetCustomVehicleSprite(this, direction, image_type, result);
136  if (result->IsValid()) return;
137 
139  }
140 
141  assert(IsValidImageIndex<VEH_SHIP>(spritenum));
142  result->Set(_ship_sprites[spritenum] + direction);
143 }
144 
145 static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
146 {
147  /* Find the closest depot */
148  const Depot *best_depot = nullptr;
149  /* If we don't have a maximum distance, i.e. distance = 0,
150  * we want to find any depot so the best distance of no
151  * depot must be more than any correct distance. On the
152  * other hand if we have set a maximum distance, any depot
153  * further away than max_distance can safely be ignored. */
154  uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;
155 
156  for (const Depot *depot : Depot::Iterate()) {
157  TileIndex tile = depot->xy;
158  if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
159  uint dist = DistanceManhattan(tile, v->tile);
160  if (dist < best_dist) {
161  best_dist = dist;
162  best_depot = depot;
163  }
164  }
165  }
166 
167  return best_depot;
168 }
169 
170 static void CheckIfShipNeedsService(Vehicle *v)
171 {
172  if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
173  if (v->IsChainInDepot()) {
175  return;
176  }
177 
178  uint max_distance;
182  default: NOT_REACHED();
183  }
184 
185  const Depot *depot = FindClosestShipDepot(v, max_distance);
186 
187  if (depot == nullptr) {
188  if (v->current_order.IsType(OT_GOTO_DEPOT)) {
191  }
192  return;
193  }
194 
196  v->SetDestTile(depot->xy);
198 }
199 
204 {
205  const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);
206 
207  /* Get speed fraction for the current water type. Aqueducts are always canals. */
208  bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA;
209  uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
210  this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);
211 
212  /* Update cargo aging period. */
213  this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period);
214 
215  this->UpdateVisualEffect();
216 }
217 
219 {
220  const Engine *e = this->GetEngine();
221  uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
222  return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
223 }
224 
226 {
227  if ((++this->day_counter & 7) == 0) {
228  DecreaseVehicleValue(this);
229  }
230 
231  CheckVehicleBreakdown(this);
232  AgeVehicle(this);
233  CheckIfShipNeedsService(this);
234 
235  CheckOrders(this);
236 
237  if (this->running_ticks == 0) return;
238 
240 
241  this->profit_this_year -= cost.GetCost();
242  this->running_ticks = 0;
243 
245 
247  /* we need this for the profit */
249 }
250 
252 {
253  if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
254 
255  if (this->IsInDepot()) {
256  /* We'll assume the ship is facing outwards */
257  return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
258  }
259 
260  if (this->state == TRACK_BIT_WORMHOLE) {
261  /* ship on aqueduct, so just use its direction and assume a diagonal track */
263  }
264 
266 }
267 
269 {
270  this->colourmap = PAL_NONE;
271  this->UpdateViewport(true, false);
272  this->UpdateCache();
273 }
274 
275 void Ship::PlayLeaveStationSound(bool force) const
276 {
277  if (PlayVehicleSound(this, VSE_START, force)) return;
278  SndPlayVehicleFx(ShipVehInfo(this->engine_type)->sfx, this);
279 }
280 
282 {
283  if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
284 
285  const Station *st = Station::Get(station);
286  if (CanVehicleUseStation(this, st)) {
287  return st->xy;
288  } else {
289  this->IncrementRealOrderIndex();
290  return 0;
291  }
292 }
293 
295 {
296  static const int8 _delta_xy_table[8][4] = {
297  /* y_extent, x_extent, y_offs, x_offs */
298  { 6, 6, -3, -3}, // N
299  { 6, 32, -3, -16}, // NE
300  { 6, 6, -3, -3}, // E
301  {32, 6, -16, -3}, // SE
302  { 6, 6, -3, -3}, // S
303  { 6, 32, -3, -16}, // SW
304  { 6, 6, -3, -3}, // W
305  {32, 6, -16, -3}, // NW
306  };
307 
308  const int8 *bb = _delta_xy_table[this->rotation];
309  this->x_offs = bb[3];
310  this->y_offs = bb[2];
311  this->x_extent = bb[1];
312  this->y_extent = bb[0];
313  this->z_extent = 6;
314 
315  if (this->direction != this->rotation) {
316  /* If we are rotating, then it is possible the ship was moved to its next position. In that
317  * case, because we are still showing the old direction, the ship will appear to glitch sideways
318  * slightly. We can work around this by applying an additional offset to make the ship appear
319  * where it was before it moved. */
320  this->x_offs -= this->x_pos - this->rotation_x_pos;
321  this->y_offs -= this->y_pos - this->rotation_y_pos;
322  }
323 }
324 
328 static Vehicle *EnsureNoMovingShipProc(Vehicle *v, void *data)
329 {
330  return v->type == VEH_SHIP && (v->vehstatus & (VS_HIDDEN | VS_STOPPED)) == 0 ? v : nullptr;
331 }
332 
333 static bool CheckReverseShip(const Ship *v, Trackdir *trackdir = nullptr)
334 {
335  /* Ask pathfinder for best direction */
336  bool reverse = false;
338  case VPF_NPF: reverse = NPFShipCheckReverse(v, trackdir); break;
339  case VPF_YAPF: reverse = YapfShipCheckReverse(v, trackdir); break;
340  default: NOT_REACHED();
341  }
342  return reverse;
343 }
344 
345 static bool CheckShipLeaveDepot(Ship *v)
346 {
347  if (!v->IsChainInDepot()) return false;
348 
349  /* We are leaving a depot, but have to go to the exact same one; re-enter */
350  if (v->current_order.IsType(OT_GOTO_DEPOT) &&
353  return true;
354  }
355 
356  /* Don't leave depot if no destination set */
357  if (v->dest_tile == 0) return true;
358 
359  /* Don't leave depot if another vehicle is already entering/leaving */
360  /* This helps avoid CPU load if many ships are set to start at the same time */
361  if (HasVehicleOnPos(v->tile, nullptr, &EnsureNoMovingShipProc)) return true;
362 
363  TileIndex tile = v->tile;
364  Axis axis = GetShipDepotAxis(tile);
365 
366  DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis));
367  TileIndex north_neighbour = TILE_ADD(tile, TileOffsByDiagDir(north_dir));
368  DiagDirection south_dir = AxisToDiagDir(axis);
369  TileIndex south_neighbour = TILE_ADD(tile, 2 * TileOffsByDiagDir(south_dir));
370 
371  TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour);
372  TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour);
373  if (north_tracks && south_tracks) {
374  if (CheckReverseShip(v)) north_tracks = TRACK_BIT_NONE;
375  }
376 
377  if (north_tracks) {
378  /* Leave towards north */
379  v->rotation = v->direction = DiagDirToDir(north_dir);
380  } else if (south_tracks) {
381  /* Leave towards south */
382  v->rotation = v->direction = DiagDirToDir(south_dir);
383  } else {
384  /* Both ways blocked */
385  return false;
386  }
387 
388  v->state = AxisToTrackBits(axis);
389  v->vehstatus &= ~VS_HIDDEN;
390 
391  v->cur_speed = 0;
392  v->UpdateViewport(true, true);
394 
399 
400  return false;
401 }
402 
403 static bool ShipAccelerate(Vehicle *v)
404 {
405  uint spd;
406  byte t;
407 
408  spd = std::min<uint>(v->cur_speed + 1, v->vcache.cached_max_speed);
409  spd = std::min<uint>(spd, v->current_order.GetMaxSpeed() * 2);
410 
411  /* updates statusbar only if speed have changed to save CPU time */
412  if (spd != v->cur_speed) {
413  v->cur_speed = spd;
415  }
416 
417  /* Convert direction-independent speed into direction-dependent speed. (old movement method) */
418  spd = v->GetOldAdvanceSpeed(spd);
419 
420  if (spd == 0) return false;
421  if ((byte)++spd == 0) return true;
422 
423  v->progress = (t = v->progress) - (byte)spd;
424 
425  return (t < v->progress);
426 }
427 
433 static void ShipArrivesAt(const Vehicle *v, Station *st)
434 {
435  /* Check if station was ever visited before */
436  if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
437  st->had_vehicle_of_type |= HVOT_SHIP;
438 
439  SetDParam(0, st->index);
441  STR_NEWS_FIRST_SHIP_ARRIVAL,
443  v->index,
444  st->index
445  );
446  AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
447  Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
448  }
449 }
450 
451 
461 static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
462 {
463  assert(IsValidDiagDirection(enterdir));
464 
465  bool path_found = true;
466  Track track;
467 
468  if (v->dest_tile == 0) {
469  /* No destination, don't invoke pathfinder. */
470  track = TrackBitsToTrack(v->state);
471  if (!IsDiagonalTrack(track)) track = TrackToOppositeTrack(track);
472  if (!HasBit(tracks, track)) track = FindFirstTrack(tracks);
473  path_found = false;
474  } else {
475  /* Attempt to follow cached path. */
476  if (!v->path.empty()) {
477  track = TrackdirToTrack(v->path.front());
478 
479  if (HasBit(tracks, track)) {
480  v->path.pop_front();
481  /* HandlePathfindResult() is not called here because this is not a new pathfinder result. */
482  return track;
483  }
484 
485  /* Cached path is invalid so continue with pathfinder. */
486  v->path.clear();
487  }
488 
490  case VPF_NPF: track = NPFShipChooseTrack(v, path_found); break;
491  case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found, v->path); break;
492  default: NOT_REACHED();
493  }
494  }
495 
496  v->HandlePathfindingResult(path_found);
497  return track;
498 }
499 
507 {
508  TrackBits tracks = GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
509 
510  return tracks;
511 }
512 
515  byte x_subcoord;
516  byte y_subcoord;
518 };
525  // DIAGDIR_NE
526  {
527  {15, 8, DIR_NE}, // TRACK_X
528  { 0, 0, INVALID_DIR}, // TRACK_Y
529  { 0, 0, INVALID_DIR}, // TRACK_UPPER
530  {15, 8, DIR_E}, // TRACK_LOWER
531  {15, 7, DIR_N}, // TRACK_LEFT
532  { 0, 0, INVALID_DIR}, // TRACK_RIGHT
533  },
534  // DIAGDIR_SE
535  {
536  { 0, 0, INVALID_DIR}, // TRACK_X
537  { 8, 0, DIR_SE}, // TRACK_Y
538  { 7, 0, DIR_E}, // TRACK_UPPER
539  { 0, 0, INVALID_DIR}, // TRACK_LOWER
540  { 8, 0, DIR_S}, // TRACK_LEFT
541  { 0, 0, INVALID_DIR}, // TRACK_RIGHT
542  },
543  // DIAGDIR_SW
544  {
545  { 0, 8, DIR_SW}, // TRACK_X
546  { 0, 0, INVALID_DIR}, // TRACK_Y
547  { 0, 7, DIR_W}, // TRACK_UPPER
548  { 0, 0, INVALID_DIR}, // TRACK_LOWER
549  { 0, 0, INVALID_DIR}, // TRACK_LEFT
550  { 0, 8, DIR_S}, // TRACK_RIGHT
551  },
552  // DIAGDIR_NW
553  {
554  { 0, 0, INVALID_DIR}, // TRACK_X
555  { 8, 15, DIR_NW}, // TRACK_Y
556  { 0, 0, INVALID_DIR}, // TRACK_UPPER
557  { 8, 15, DIR_W}, // TRACK_LOWER
558  { 0, 0, INVALID_DIR}, // TRACK_LEFT
559  { 7, 15, DIR_N}, // TRACK_RIGHT
560  }
561 };
562 
568 static int ShipTestUpDownOnLock(const Ship *v)
569 {
570  /* Suitable tile? */
571  if (!IsTileType(v->tile, MP_WATER) || !IsLock(v->tile) || GetLockPart(v->tile) != LOCK_PART_MIDDLE) return 0;
572 
573  /* Must be at the centre of the lock */
574  if ((v->x_pos & 0xF) != 8 || (v->y_pos & 0xF) != 8) return 0;
575 
577  assert(IsValidDiagDirection(diagdir));
578 
579  if (DirToDiagDir(v->direction) == diagdir) {
580  /* Move up */
581  return (v->z_pos < GetTileMaxZ(v->tile) * (int)TILE_HEIGHT) ? 1 : 0;
582  } else {
583  /* Move down */
584  return (v->z_pos > GetTileZ(v->tile) * (int)TILE_HEIGHT) ? -1 : 0;
585  }
586 }
587 
593 static bool ShipMoveUpDownOnLock(Ship *v)
594 {
595  /* Moving up/down through lock */
596  int dz = ShipTestUpDownOnLock(v);
597  if (dz == 0) return false;
598 
599  if (v->cur_speed != 0) {
600  v->cur_speed = 0;
602  }
603 
604  if ((v->tick_counter & 7) == 0) {
605  v->z_pos += dz;
606  v->UpdatePosition();
607  v->UpdateViewport(true, true);
608  }
609 
610  return true;
611 }
612 
619 bool IsShipDestinationTile(TileIndex tile, StationID station)
620 {
621  assert(IsDockingTile(tile));
622  /* Check each tile adjacent to docking tile. */
623  for (DiagDirection d = DIAGDIR_BEGIN; d != DIAGDIR_END; d++) {
624  TileIndex t = tile + TileOffsByDiagDir(d);
625  if (!IsValidTile(t)) continue;
626  if (IsDockTile(t) && GetStationIndex(t) == station && IsValidDockingDirectionForDock(t, d)) return true;
627  if (IsTileType(t, MP_INDUSTRY)) {
628  const Industry *i = Industry::GetByTile(t);
629  if (i->neutral_station != nullptr && i->neutral_station->index == station) return true;
630  }
631  if (IsTileType(t, MP_STATION) && IsOilRig(t) && GetStationIndex(t) == station) return true;
632  }
633  return false;
634 }
635 
636 static void ShipController(Ship *v)
637 {
638  uint32 r;
639  Track track;
640  TrackBits tracks;
642 
643  v->tick_counter++;
644  v->current_order_time++;
645 
646  if (v->HandleBreakdown()) return;
647 
648  if (v->vehstatus & VS_STOPPED) return;
649 
650  if (ProcessOrders(v) && CheckReverseShip(v)) goto reverse_direction;
651 
652  v->HandleLoading();
653 
654  if (v->current_order.IsType(OT_LOADING)) return;
655 
656  if (CheckShipLeaveDepot(v)) return;
657 
658  v->ShowVisualEffect();
659 
660  /* Rotating on spot */
661  if (v->direction != v->rotation) {
662  if ((v->tick_counter & 7) == 0) {
663  DirDiff diff = DirDifference(v->direction, v->rotation);
665  /* Invalidate the sprite cache direction to force recalculation of viewport */
667  v->UpdateViewport(true, true);
668  }
669  return;
670  }
671 
672  if (ShipMoveUpDownOnLock(v)) return;
673 
674  if (!ShipAccelerate(v)) return;
675 
676  gp = GetNewVehiclePos(v);
677  if (v->state != TRACK_BIT_WORMHOLE) {
678  /* Not on a bridge */
679  if (gp.old_tile == gp.new_tile) {
680  /* Staying in tile */
681  if (v->IsInDepot()) {
682  gp.x = v->x_pos;
683  gp.y = v->y_pos;
684  } else {
685  /* Not inside depot */
686  r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
687  if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
688 
689  /* A leave station order only needs one tick to get processed, so we can
690  * always skip ahead. */
691  if (v->current_order.IsType(OT_LEAVESTATION)) {
692  v->current_order.Free();
694  /* Test if continuing forward would lead to a dead-end, moving into the dock. */
695  DiagDirection exitdir = VehicleExitDir(v->direction, v->state);
696  TileIndex tile = TileAddByDiagDir(v->tile, exitdir);
697  if (TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0, exitdir)) == TRACK_BIT_NONE) goto reverse_direction;
698  } else if (v->dest_tile != 0) {
699  /* We have a target, let's see if we reached it... */
700  if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
701  DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
702  /* We got within 3 tiles of our target buoy, so let's skip to our
703  * next order */
704  UpdateVehicleTimetable(v, true);
707  } else if (v->current_order.IsType(OT_GOTO_DEPOT) &&
708  v->dest_tile == gp.new_tile) {
709  /* Depot orders really need to reach the tile */
710  if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
712  return;
713  }
714  } else if (v->current_order.IsType(OT_GOTO_STATION) && IsDockingTile(gp.new_tile)) {
715  /* Process station in the orderlist. */
718  v->last_station_visited = st->index;
719  if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
720  ShipArrivesAt(v, st);
721  v->BeginLoading();
722  } else { // leave stations without docks right away
725  }
726  }
727  }
728  }
729  }
730  } else {
731  /* New tile */
732  if (!IsValidTile(gp.new_tile)) goto reverse_direction;
733 
735  assert(diagdir != INVALID_DIAGDIR);
736  tracks = GetAvailShipTracks(gp.new_tile, diagdir);
737  if (tracks == TRACK_BIT_NONE) {
738  Trackdir trackdir = INVALID_TRACKDIR;
739  CheckReverseShip(v, &trackdir);
740  if (trackdir == INVALID_TRACKDIR) goto reverse_direction;
741  static const Direction _trackdir_to_direction[] = {
744  };
745  v->direction = _trackdir_to_direction[trackdir];
746  assert(v->direction != INVALID_DIR);
748  goto direction_changed;
749  }
750 
751  /* Choose a direction, and continue if we find one */
752  track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
753  if (track == INVALID_TRACK) goto reverse_direction;
754 
755  const ShipSubcoordData &b = _ship_subcoord[diagdir][track];
756 
757  gp.x = (gp.x & ~0xF) | b.x_subcoord;
758  gp.y = (gp.y & ~0xF) | b.y_subcoord;
759 
760  /* Call the landscape function and tell it that the vehicle entered the tile */
761  r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
762  if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
763 
764  if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
765  v->tile = gp.new_tile;
766  v->state = TrackToTrackBits(track);
767 
768  /* Update ship cache when the water class changes. Aqueducts are always canals. */
771  if (old_wc != new_wc) v->UpdateCache();
772  }
773 
774  Direction new_direction = b.dir;
775  DirDiff diff = DirDifference(new_direction, v->direction);
776  switch (diff) {
777  case DIRDIFF_SAME:
778  case DIRDIFF_45RIGHT:
779  case DIRDIFF_45LEFT:
780  /* Continue at speed */
781  v->rotation = v->direction = new_direction;
782  break;
783 
784  default:
785  /* Stop for rotation */
786  v->cur_speed = 0;
787  v->direction = new_direction;
788  /* Remember our current location to avoid movement glitch */
789  v->rotation_x_pos = v->x_pos;
790  v->rotation_y_pos = v->y_pos;
791  break;
792  }
793  }
794  } else {
795  /* On a bridge */
797  v->x_pos = gp.x;
798  v->y_pos = gp.y;
799  v->UpdatePosition();
800  if ((v->vehstatus & VS_HIDDEN) == 0) v->Vehicle::UpdateViewport(true);
801  return;
802  }
803 
804  /* Ship is back on the bridge head, we need to consume its path
805  * cache entry here as we didn't have to choose a ship track. */
806  if (!v->path.empty()) v->path.pop_front();
807  }
808 
809  /* update image of ship, as well as delta XY */
810  v->x_pos = gp.x;
811  v->y_pos = gp.y;
812 
813 getout:
814  v->UpdatePosition();
815  v->UpdateViewport(true, true);
816  return;
817 
818 reverse_direction:
819  v->direction = ReverseDir(v->direction);
820 direction_changed:
821  /* Remember our current location to avoid movement glitch */
822  v->rotation_x_pos = v->x_pos;
823  v->rotation_y_pos = v->y_pos;
824  v->cur_speed = 0;
825  v->path.clear();
826  goto getout;
827 }
828 
830 {
832 
833  if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
834 
835  ShipController(this);
836 
837  return true;
838 }
839 
840 void Ship::SetDestTile(TileIndex tile)
841 {
842  if (tile == this->dest_tile) return;
843  this->path.clear();
844  this->dest_tile = tile;
845 }
846 
856 {
857  tile = GetShipDepotNorthTile(tile);
858  if (flags & DC_EXEC) {
859  int x;
860  int y;
861 
862  const ShipVehicleInfo *svi = &e->u.ship;
863 
864  Ship *v = new Ship();
865  *ret = v;
866 
867  v->owner = _current_company;
868  v->tile = tile;
869  x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
870  y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
871  v->x_pos = x;
872  v->y_pos = y;
873  v->z_pos = GetSlopePixelZ(x, y);
874 
875  v->UpdateDeltaXY();
877 
878  v->spritenum = svi->image_index;
880  v->cargo_cap = svi->capacity;
881  v->refit_cap = 0;
882 
883  v->last_station_visited = INVALID_STATION;
884  v->last_loading_station = INVALID_STATION;
885  v->engine_type = e->index;
886 
887  v->reliability = e->reliability;
889  v->max_age = e->GetLifeLengthInDays();
890 
891  v->state = TRACK_BIT_DEPOT;
892 
893  v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_ships);
895  v->build_year = _cur_year;
896  v->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
898 
899  v->UpdateCache();
900 
902  v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
903 
905 
906  v->cargo_cap = e->DetermineCapacity(v);
907 
909 
910  v->UpdatePosition();
911  }
912 
913  return CommandCost();
914 }
915 
916 bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
917 {
918  const Depot *depot = FindClosestShipDepot(this, 0);
919 
920  if (depot == nullptr) return false;
921 
922  if (location != nullptr) *location = depot->xy;
923  if (destination != nullptr) *destination = depot->index;
924 
925  return true;
926 }
VSE_START
@ VSE_START
Vehicle starting, i.e. leaving, the station.
Definition: newgrf_sound.h:19
game.hpp
ChooseShipTrack
static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
Runs the pathfinder to choose a track to continue along.
Definition: ship_cmd.cpp:461
VehicleExitDir
static DiagDirection VehicleExitDir(Direction direction, TrackBits track)
Determine the side in which the vehicle will leave the tile.
Definition: track_func.h:713
Vehicle::IsChainInDepot
virtual bool IsChainInDepot() const
Check whether the whole vehicle chain is in the depot.
Definition: vehicle_base.h:537
BaseStation::facilities
StationFacility facilities
The facilities that this station has.
Definition: base_station_base.h:63
TRACK_BIT_WORMHOLE
@ TRACK_BIT_WORMHOLE
Bitflag for a wormhole (used for tunnels)
Definition: track_type.h:55
PathfinderSettings::npf
NPFSettings npf
pathfinder settings for the new pathfinder
Definition: settings_type.h:472
ShipTestUpDownOnLock
static int ShipTestUpDownOnLock(const Ship *v)
Test if a ship is in the centre of a lock and should move up or down.
Definition: ship_cmd.cpp:568
TILE_ADD
#define TILE_ADD(x, y)
Adds two tiles together.
Definition: map_func.h:244
Station::docking_station
TileArea docking_station
Tile area the docking tiles cover.
Definition: station_base.h:470
DIR_SW
@ DIR_SW
Southwest.
Definition: direction_type.h:31
InvalidateWindowData
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3252
Order::MakeDummy
void MakeDummy()
Makes this order a Dummy order.
Definition: order_cmd.cpp:133
sound_func.h
UpdateVehicleTimetable
void UpdateVehicleTimetable(Vehicle *v, bool travelling)
Update the timetable for the vehicle.
Definition: timetable_cmd.cpp:383
MutableSpriteCache::last_direction
Direction last_direction
Last direction we obtained sprites for.
Definition: vehicle_base.h:193
TRACK_BIT_NONE
@ TRACK_BIT_NONE
No track.
Definition: track_type.h:39
DIRDIFF_REVERSE
@ DIRDIFF_REVERSE
One direction is the opposite of the other one.
Definition: direction_type.h:66
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:71
Rect::Height
int Height() const
Get height of Rect.
Definition: geometry_type.hpp:85
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
TileOffsByDiagDir
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:341
Direction
Direction
Defines the 8 directions on the map.
Definition: direction_type.h:24
Vehicle::y_pos
int32 y_pos
y coordinate.
Definition: vehicle_base.h:284
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3154
MutableSpriteCache::sprite_seq
VehicleSpriteSeq sprite_seq
Vehicle appearance.
Definition: vehicle_base.h:197
GetTileMaxZ
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:141
Ship::Tick
bool Tick()
Calls the tick handler of the vehicle.
Definition: ship_cmd.cpp:829
GetPrice
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition: economy.cpp:961
NT_ARRIVAL_OTHER
@ NT_ARRIVAL_OTHER
First vehicle arrived for competitor.
Definition: news_type.h:23
Vehicle::x_pos
int32 x_pos
x coordinate.
Definition: vehicle_base.h:283
DIRDIFF_45LEFT
@ DIRDIFF_45LEFT
Angle of 45 degrees left.
Definition: direction_type.h:68
ChangeDir
static Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
Definition: direction_func.h:104
HasTileWaterClass
static bool HasTileWaterClass(TileIndex t)
Checks whether the tile has an waterclass associated.
Definition: water_map.h:106
DIR_SE
@ DIR_SE
Southeast.
Definition: direction_type.h:29
ODTFB_SERVICE
@ ODTFB_SERVICE
This depot order is because of the servicing limit.
Definition: order_type.h:95
Ship::FindClosestDepot
bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
Find the closest depot for this vehicle and tell us the location, DestinationID and whether we should...
Definition: ship_cmd.cpp:916
EnsureNoMovingShipProc
static Vehicle * EnsureNoMovingShipProc(Vehicle *v, void *data)
Test-procedure for HasVehicleOnPos to check for any ships which are visible and not stopped by the pl...
Definition: ship_cmd.cpp:328
HasVehicleOnPos
bool HasVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Checks whether a vehicle is on a specific location.
Definition: vehicle.cpp:513
CheckOrders
void CheckOrders(const Vehicle *v)
Check the orders of a vehicle, to see if there are invalid orders and stuff.
Definition: order_cmd.cpp:1689
Engine::reliability_spd_dec
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:41
Order::MakeLeaveStation
void MakeLeaveStation()
Makes this order a Leave Station order.
Definition: order_cmd.cpp:124
company_base.h
_cur_year
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:26
tunnelbridge_map.h
DIRDIFF_SAME
@ DIRDIFF_SAME
Both directions faces to the same direction.
Definition: direction_type.h:63
Vehicle::y_extent
byte y_extent
y-extent of vehicle bounding box
Definition: vehicle_base.h:296
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:125
Station
Station data structure.
Definition: station_base.h:454
Order::GetDestination
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:104
Vehicle::NeedsAutomaticServicing
bool NeedsAutomaticServicing() const
Checks if the current order should be interrupted for a service-in-depot order.
Definition: vehicle.cpp:252
Vehicle::z_pos
int32 z_pos
z coordinate.
Definition: vehicle_base.h:285
VehicleSpriteSeq::Set
void Set(SpriteID sprite)
Assign a single sprite to the sequence.
Definition: vehicle_base.h:165
DIR_NW
@ DIR_NW
Northwest.
Definition: direction_type.h:33
Ship::path
ShipPathCache path
Cached path.
Definition: ship.h:28
Vehicle::random_bits
byte random_bits
Bits used for determining which randomized variational spritegroups to use when drawing.
Definition: vehicle_base.h:313
Vehicle::vehstatus
byte vehstatus
Status.
Definition: vehicle_base.h:332
DIAGDIR_END
@ DIAGDIR_END
Used for iterations.
Definition: direction_type.h:83
VPF_YAPF
@ VPF_YAPF
Yet Another PathFinder.
Definition: vehicle_type.h:61
VS_DEFPAL
@ VS_DEFPAL
Use default vehicle palette.
Definition: vehicle_base.h:37
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
AxisToTrackBits
static TrackBits AxisToTrackBits(Axis a)
Maps an Axis to the corresponding TrackBits value.
Definition: track_func.h:87
Order::Free
void Free()
'Free' the order
Definition: order_cmd.cpp:63
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:121
ship.h
UnScaleGUI
static int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition: zoom_func.h:77
Ship::rotation
Direction rotation
Visible direction.
Definition: ship.h:29
Ship::PlayLeaveStationSound
void PlayLeaveStationSound(bool force=false) const
Play the sound associated with leaving the station.
Definition: ship_cmd.cpp:275
DiagDirToDiagTrackdir
static Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
Definition: track_func.h:536
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
CmdBuildShip
CommandCost CmdBuildShip(DoCommandFlag flags, TileIndex tile, const Engine *e, Vehicle **ret)
Build a ship.
Definition: ship_cmd.cpp:855
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition: tile_type.h:49
IsValidDiagDirection
static bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
Definition: direction_func.h:21
zoom_func.h
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
AddVehicleNewsItem
static void AddVehicleNewsItem(StringID string, NewsType type, VehicleID vehicle, StationID station=INVALID_STATION)
Adds a newsitem referencing a vehicle.
Definition: news_func.h:30
Vehicle::running_ticks
byte running_ticks
Number of ticks this vehicle was not stopped this day.
Definition: vehicle_base.h:330
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:218
DirDifference
static DirDiff DirDifference(Direction d0, Direction d1)
Calculate the difference between two directions.
Definition: direction_func.h:68
VehicleEnterDepot
void VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it,...
Definition: vehicle.cpp:1487
Ship::IsInDepot
bool IsInDepot() const
Check whether the vehicle is in the depot.
Definition: ship.h:48
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:56
TRANSPORT_WATER
@ TRANSPORT_WATER
Transport over water.
Definition: transport_type.h:29
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
Engine::GetLifeLengthInDays
Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition: engine.cpp:437
DIR_W
@ DIR_W
West.
Definition: direction_type.h:32
EngineImageType
EngineImageType
Visualisation contexts of vehicles and engines.
Definition: vehicle_type.h:86
Engine
Definition: engine_base.h:36
Vehicle::cur_speed
uint16 cur_speed
current speed
Definition: vehicle_base.h:307
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:224
Industry
Defines the internal data of a functional industry.
Definition: industry.h:66
Engine::GetDefaultCargoType
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition: engine_base.h:95
VehicleSpriteSeq::Draw
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition: vehicle.cpp:126
Vehicle::owner
Owner owner
Which company owns the vehicle?
Definition: vehicle_base.h:288
VehicleCache::cached_cargo_age_period
uint16 cached_cargo_age_period
Number of ticks before carried cargo is aged.
Definition: vehicle_base.h:126
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:357
DIR_N
@ DIR_N
North.
Definition: direction_type.h:26
GetAvailShipTracks
static TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
Get the available water tracks on a tile for a ship entering a tile.
Definition: ship_cmd.cpp:506
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:196
GetShipDepotDirection
static DiagDirection GetShipDepotDirection(TileIndex t)
Get the direction of the ship depot.
Definition: water_map.h:272
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:355
VehicleServiceInDepot
void VehicleServiceInDepot(Vehicle *v)
Service a vehicle and all subsequent vehicles in the consist.
Definition: vehicle.cpp:162
Industry::neutral_station
Station * neutral_station
Associated neutral station.
Definition: industry.h:69
TrackToTrackBits
static TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition: track_func.h:76
ShipSubcoordData::x_subcoord
byte x_subcoord
New X sub-coordinate on the new tile.
Definition: ship_cmd.cpp:515
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
industry_map.h
GetLockPart
static byte GetLockPart(TileIndex t)
Get the part of a lock.
Definition: water_map.h:331
GetTileTrackStatus
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Definition: landscape.cpp:601
ai.hpp
Order::MakeGoToDepot
void MakeGoToDepot(DepotID destination, OrderDepotTypeFlags order, OrderNonStopFlags non_stop_type=ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS, OrderDepotActionFlags action=ODATF_SERVICE_ONLY, CargoID cargo=CT_NO_REFIT)
Makes this order a Go To Depot order.
Definition: order_cmd.cpp:90
ShipSubcoordData
Structure for ship sub-coordinate data for moving into a new tile via a Diagdir onto a Track.
Definition: ship_cmd.cpp:514
Order::GetMaxSpeed
uint16 GetMaxSpeed() const
Get the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the destination.
Definition: order_base.h:202
Ship::UpdateCache
void UpdateCache()
Update the caches of this ship.
Definition: ship_cmd.cpp:203
GetShipDepotNorthTile
static TileIndex GetShipDepotNorthTile(TileIndex t)
Get the most northern tile of a ship depot.
Definition: water_map.h:294
Engine::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:154
NT_ARRIVAL_COMPANY
@ NT_ARRIVAL_COMPANY
First vehicle arrived for company.
Definition: news_type.h:22
SetWindowWidgetDirty
void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
Mark a particular widget in a particular window as dirty (in need of repainting)
Definition: window.cpp:3167
ENGINE_EXCLUSIVE_PREVIEW
@ ENGINE_EXCLUSIVE_PREVIEW
This vehicle is in the exclusive preview stage, either being used or being offered to a company.
Definition: engine_type.h:182
Ship::rotation_y_pos
int16 rotation_y_pos
NOSAVE: Y Position before rotation.
Definition: ship.h:31
Vehicle::BeginLoading
void BeginLoading()
Prepare everything to begin the loading when arriving at a station.
Definition: vehicle.cpp:2113
GameSettings::pf
PathfinderSettings pf
settings for all pathfinders
Definition: settings_type.h:593
DistanceManhattan
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:157
VS_HIDDEN
@ VS_HIDDEN
Vehicle is not visible.
Definition: vehicle_base.h:34
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
depot_base.h
Vehicle::UpdateVisualEffect
void UpdateVisualEffect(bool allow_power_change=true)
Update the cached visual effect.
Definition: vehicle.cpp:2468
Vehicle::dest_tile
TileIndex dest_tile
Heading for this tile.
Definition: vehicle_base.h:252
DirToDiagDir
static DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
Definition: direction_func.h:166
Vehicle::HandlePathfindingResult
void HandlePathfindingResult(bool path_found)
Handle the pathfinding result, especially the lost status.
Definition: vehicle.cpp:783
TrackBitsToTrack
static Track TrackBitsToTrack(TrackBits tracks)
Converts TrackBits to Track.
Definition: track_func.h:192
timetable.h
AI::NewEvent
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
Definition: ai_core.cpp:236
ship_cmd.h
CommandCost
Common return value for all commands.
Definition: command_type.h:24
WaterClass
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition: water_map.h:47
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
WC_VEHICLE_VIEW
@ WC_VEHICLE_VIEW
Vehicle view; Window numbers:
Definition: window_type.h:332
newgrf_engine.h
Industry::GetByTile
static Industry * GetByTile(TileIndex tile)
Get the industry of the given tile.
Definition: industry.h:144
DIR_E
@ DIR_E
East.
Definition: direction_type.h:28
YapfShipChooseTrack
Track YapfShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, ShipPathCache &path_cache)
Finds the best path for given ship using YAPF.
Definition: yapf_ship.cpp:352
YAPF_TILE_LENGTH
static const int YAPF_TILE_LENGTH
Length (penalty) of one tile with YAPF.
Definition: pathfinder_type.h:28
SubtractMoneyFromCompanyFract
void SubtractMoneyFromCompanyFract(CompanyID company, const CommandCost &cst)
Subtract money from a company, including the money fraction.
Definition: company_cmd.cpp:258
Vehicle::tile
TileIndex tile
Current tile index.
Definition: vehicle_base.h:245
TrackStatusToTrackBits
static TrackBits TrackStatusToTrackBits(TrackStatus ts)
Returns the present-track-information of a TrackStatus.
Definition: track_func.h:362
Ship::MarkDirty
void MarkDirty()
Marks the vehicles to be redrawn and updates cached variables.
Definition: ship_cmd.cpp:268
npf_func.h
EIT_ON_MAP
@ EIT_ON_MAP
Vehicle drawn in viewport.
Definition: vehicle_type.h:87
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:302
VS_CRASHED
@ VS_CRASHED
Vehicle is crashed.
Definition: vehicle_base.h:41
IsDiagonalTrack
static bool IsDiagonalTrack(Track track)
Checks if a given Track is diagonal.
Definition: track_func.h:618
VETS_CANNOT_ENTER
@ VETS_CANNOT_ENTER
The vehicle cannot enter the tile.
Definition: tile_cmd.h:23
VehicleSpriteSeq
Sprite sequence for a vehicle part.
Definition: vehicle_base.h:132
Vehicle::last_station_visited
StationID last_station_visited
The last station we stopped at.
Definition: vehicle_base.h:316
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:54
PFE_GL_SHIPS
@ PFE_GL_SHIPS
Time spent processing ships.
Definition: framerate_type.h:53
NPFSettings::maximum_go_to_depot_penalty
uint32 maximum_go_to_depot_penalty
What is the maximum penalty that may be endured for going to a depot.
Definition: settings_type.h:392
TrackToOppositeTrack
static Track TrackToOppositeTrack(Track t)
Find the opposite track to a given track.
Definition: track_func.h:230
Ship::OnNewDay
void OnNewDay()
Calls the new day handler of the vehicle.
Definition: ship_cmd.cpp:225
Vehicle::GetOldAdvanceSpeed
uint GetOldAdvanceSpeed(uint speed)
Determines the effective direction-specific vehicle movement speed.
Definition: vehicle_base.h:411
Vehicle::current_order
Order current_order
The current order (+ status, like: loading)
Definition: vehicle_base.h:333
WATER_CLASS_CANAL
@ WATER_CLASS_CANAL
Canal.
Definition: water_map.h:49
Vehicle::max_age
Date max_age
Maximum age.
Definition: vehicle_base.h:274
DIR_NE
@ DIR_NE
Northeast.
Definition: direction_type.h:27
LOCK_PART_MIDDLE
@ LOCK_PART_MIDDLE
Middle part of a lock.
Definition: water_map.h:76
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:54
HVOT_SHIP
@ HVOT_SHIP
Station has seen a ship.
Definition: station_type.h:69
Ship::GetImage
void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
Gets the sprite to show for the given direction.
Definition: ship_cmd.cpp:128
WC_VEHICLE_DETAILS
@ WC_VEHICLE_DETAILS
Vehicle details; Window numbers:
Definition: window_type.h:193
Game::NewEvent
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:146
YapfShipCheckReverse
bool YapfShipCheckReverse(const Ship *v, Trackdir *trackdir)
Returns true if it is better to reverse the ship before leaving depot using YAPF.
Definition: yapf_ship.cpp:367
VS_STOPPED
@ VS_STOPPED
Vehicle is stopped by the player.
Definition: vehicle_base.h:35
Vehicle::ShowVisualEffect
void ShowVisualEffect() const
Draw visual effects (smoke and/or sparks) for a vehicle chain.
Definition: vehicle.cpp:2591
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
PROP_SHIP_CARGO_AGE_PERIOD
@ PROP_SHIP_CARGO_AGE_PERIOD
Number of ticks before carried cargo is aged.
Definition: newgrf_properties.h:47
Vehicle::GetEngine
const Engine * GetEngine() const
Retrieves the engine of the vehicle.
Definition: vehicle.cpp:741
VehicleEnterTile
VehicleEnterTileStatus VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y)
Call the tile callback function for a vehicle entering a tile.
Definition: vehicle.cpp:1755
industry.h
DiagdirReachesTracks
static TrackBits DiagdirReachesTracks(DiagDirection diagdir)
Returns all tracks that can be reached when entering a tile from a given (diagonal) direction.
Definition: track_func.h:572
safeguards.h
GetNewVehiclePosResult::new_tile
TileIndex new_tile
Tile of the vehicle after moving.
Definition: vehicle_func.h:78
Vehicle::reliability_spd_dec
uint16 reliability_spd_dec
Reliability decrease speed.
Definition: vehicle_base.h:277
Vehicle::last_loading_station
StationID last_loading_station
Last station the vehicle has stopped at and could possibly leave from with any cargo loaded.
Definition: vehicle_base.h:317
DiagDirToDir
static Direction DiagDirToDir(DiagDirection dir)
Convert a DiagDirection to a Direction.
Definition: direction_func.h:182
IsValidTile
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:161
CommandCost::GetCost
Money GetCost() const
The costs as made up to this moment.
Definition: command_type.h:83
TileAddByDiagDir
static TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition: map_func.h:382
GetTileSlope
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:59
ReverseDiagDir
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Definition: direction_func.h:118
WC_SHIPS_LIST
@ WC_SHIPS_LIST
Ships list; Window numbers:
Definition: window_type.h:313
DIR_S
@ DIR_S
South.
Definition: direction_type.h:30
Vehicle::profit_this_year
Money profit_this_year
Profit this year << 8, low 8 bits are fract.
Definition: vehicle_base.h:254
IsTileOwner
static bool IsTileOwner(TileIndex tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition: tile_map.h:214
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
INVALID_DIAGDIR
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Definition: direction_type.h:84
Ship::UpdateDeltaXY
void UpdateDeltaXY()
Updates the x and y offsets and the size of the sprite used for this vehicle.
Definition: ship_cmd.cpp:294
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:57
INVALID_TRACKDIR
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition: track_type.h:89
DirDiff
DirDiff
Enumeration for the difference between two directions.
Definition: direction_type.h:62
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
Vehicle::HandleLoading
void HandleLoading(bool mode=false)
Handle the loading of the vehicle; when not it skips through dummy orders and does nothing in all oth...
Definition: vehicle.cpp:2324
FACIL_DOCK
@ FACIL_DOCK
Station with a dock.
Definition: station_type.h:57
GetShipSpriteSize
void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
Get the size of the sprite of a ship sprite heading west (used for lists).
Definition: ship_cmd.cpp:114
date_func.h
stdafx.h
ShipSubcoordData::dir
Direction dir
New Direction to move in on the new track.
Definition: ship_cmd.cpp:517
BaseConsist::vehicle_flags
uint16 vehicle_flags
Used for gradual loading and other miscellaneous things (.
Definition: base_consist.h:31
WID_VV_START_STOP
@ WID_VV_START_STOP
Start or stop this vehicle, and show information about the current state.
Definition: vehicle_widget.h:17
Vehicle::sprite_cache
MutableSpriteCache sprite_cache
Cache of sprites and values related to recalculating them, see MutableSpriteCache.
Definition: vehicle_base.h:347
landscape.h
VPF_NPF
@ VPF_NPF
New PathFinder.
Definition: vehicle_type.h:60
IsValidDockingDirectionForDock
bool IsValidDockingDirectionForDock(TileIndex t, DiagDirection d)
Check if a dock tile can be docked from the given direction.
Definition: station_cmd.cpp:2605
WATER_CLASS_SEA
@ WATER_CLASS_SEA
Sea.
Definition: water_map.h:48
Vehicle::y_offs
int8 y_offs
y offset for vehicle sprite
Definition: vehicle_base.h:301
Vehicle::colourmap
SpriteID colourmap
NOSAVE: cached colour mapping.
Definition: vehicle_base.h:269
AxisToDiagDir
static DiagDirection AxisToDiagDir(Axis a)
Converts an Axis to a DiagDirection.
Definition: direction_func.h:232
PROP_SHIP_RUNNING_COST_FACTOR
@ PROP_SHIP_RUNNING_COST_FACTOR
Yearly runningcost.
Definition: newgrf_properties.h:46
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
Vehicle::IncrementRealOrderIndex
void IncrementRealOrderIndex()
Advanced cur_real_order_index to the next real order, keeps care of the wrap-around and invalidates t...
Definition: vehicle_base.h:854
VF_BUILT_AS_PROTOTYPE
@ VF_BUILT_AS_PROTOTYPE
Vehicle is a prototype (accepted as exclusive preview).
Definition: vehicle_base.h:48
Ship::GetVehicleTrackdir
Trackdir GetVehicleTrackdir() const
Returns the Trackdir on which the vehicle is currently located.
Definition: ship_cmd.cpp:251
RAIL_GROUND_WATER
@ RAIL_GROUND_WATER
Grass with a fence and shore or water on the free halftile.
Definition: rail_map.h:499
Vehicle::direction
Direction direction
facing
Definition: vehicle_base.h:286
TRACK_BIT_DEPOT
@ TRACK_BIT_DEPOT
Bitflag for a depot.
Definition: track_type.h:56
TRACK_END
@ TRACK_END
Used for iterations.
Definition: track_type.h:27
PerformanceAccumulator
RAII class for measuring multi-step elements of performance.
Definition: framerate_type.h:114
ProcessOrders
bool ProcessOrders(Vehicle *v)
Handle the orders of a vehicle and determine the next place to go to if needed.
Definition: order_cmd.cpp:2090
spritecache.h
ReverseDir
static Direction ReverseDir(Direction d)
Return the reverse of a direction.
Definition: direction_func.h:54
Vehicle::x_extent
byte x_extent
x-extent of vehicle bounding box
Definition: vehicle_base.h:295
IsDockTile
static bool IsDockTile(TileIndex t)
Is tile t a dock tile?
Definition: station_map.h:295
Vehicle::vcache
VehicleCache vcache
Cache of often used vehicle values.
Definition: vehicle_base.h:345
Ship
All ships have this type.
Definition: ship.h:26
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
vehicle_func.h
station_base.h
newgrf_sound.h
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
Pool::PoolItem<&_depot_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
PALETTE_CRASH
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1598
strings_func.h
IsShipDestinationTile
bool IsShipDestinationTile(TileIndex tile, StationID station)
Test if a tile is a docking tile for the given station.
Definition: ship_cmd.cpp:619
GetWaterClass
static WaterClass GetWaterClass(TileIndex t)
Get the water class at a tile.
Definition: water_map.h:117
Vehicle::tick_counter
byte tick_counter
Increased by one for each tick.
Definition: vehicle_base.h:329
yapf.h
Vehicle::cargo_cap
uint16 cargo_cap
total capacity
Definition: vehicle_base.h:322
_ship_subcoord
static const ShipSubcoordData _ship_subcoord[DIAGDIR_END][TRACK_END]
Ship sub-coordinate data for moving into a new tile via a Diagdir onto a Track.
Definition: ship_cmd.cpp:524
Vehicle::x_offs
int8 x_offs
x offset for vehicle sprite
Definition: vehicle_base.h:300
DiagdirBetweenTiles
static DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
Determines the DiagDirection to get from one tile to another.
Definition: map_func.h:394
DIRDIFF_45RIGHT
@ DIRDIFF_45RIGHT
Angle of 45 degrees right.
Definition: direction_type.h:64
Vehicle::z_extent
byte z_extent
z-extent of vehicle bounding box
Definition: vehicle_base.h:297
VehicleRandomBits
byte VehicleRandomBits()
Get a value for a vehicle's random_bits.
Definition: vehicle.cpp:363
Vehicle::InvalidateNewGRFCacheOfChain
void InvalidateNewGRFCacheOfChain()
Invalidates cached NewGRF variables of all vehicles in the chain (after the current vehicle)
Definition: vehicle_base.h:487
Ship::GetOrderStationLocation
TileIndex GetOrderStationLocation(StationID station)
Determine the location for the station where the vehicle goes to next.
Definition: ship_cmd.cpp:281
PaletteID
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
framerate_type.h
PathfinderSettings::pathfinder_for_ships
uint8 pathfinder_for_ships
the pathfinder to use for ships
Definition: settings_type.h:458
BaseConsist::current_order_time
uint32 current_order_time
How many ticks have passed since this order started.
Definition: base_consist.h:22
Ship::state
TrackBits state
The "track" the ship is following.
Definition: ship.h:27
GetNewVehiclePosResult
Position information of a vehicle after it moved.
Definition: vehicle_func.h:75
WC_VEHICLE_DEPOT
@ WC_VEHICLE_DEPOT
Depot view; Window numbers:
Definition: window_type.h:344
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:53
GetStationIndex
static StationID GetStationIndex(TileIndex t)
Get StationID from a tile.
Definition: station_map.h:28
Vehicle::build_year
Year build_year
Year the vehicle has been built.
Definition: vehicle_base.h:272
NPF_TILE_LENGTH
static const int NPF_TILE_LENGTH
Length (penalty) of one tile with NPF.
Definition: pathfinder_type.h:16
IsLock
static bool IsLock(TileIndex t)
Is there a lock on a given water tile?
Definition: water_map.h:308
ShipVehicleInfo
Information about a ship vehicle.
Definition: engine_type.h:67
DIAGDIR_BEGIN
@ DIAGDIR_BEGIN
Used for iterations.
Definition: direction_type.h:78
DAYS_IN_YEAR
static const int DAYS_IN_YEAR
days per year
Definition: date_type.h:29
TrackDirectionToTrackdir
static Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
Maps a track and a full (8-way) direction to the trackdir that represents the track running in the gi...
Definition: track_func.h:497
GetShipDepotAxis
static Axis GetShipDepotAxis(TileIndex t)
Get the axis of the ship depot.
Definition: water_map.h:248
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:53
VehicleCache::cached_max_speed
uint16 cached_max_speed
Maximum speed of the consist (minimum of the max speed of all vehicles in the consist).
Definition: vehicle_base.h:125
company_func.h
FindFirstTrack
static Track FindFirstTrack(TrackBits tracks)
Returns first Track from TrackBits or INVALID_TRACK.
Definition: track_func.h:176
NPFShipChooseTrack
Track NPFShipChooseTrack(const Ship *v, bool &path_found)
Finds the best path for given ship using NPF.
Definition: npf.cpp:1193
ShipArrivesAt
static void ShipArrivesAt(const Vehicle *v, Station *st)
Ship arrives at a dock.
Definition: ship_cmd.cpp:433
Engine::original_image_index
uint8 original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition: engine_base.h:54
GetNewVehiclePosResult::old_tile
TileIndex old_tile
Current tile of the vehicle.
Definition: vehicle_func.h:77
PlayVehicleSound
bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event, bool force)
Checks whether a NewGRF wants to play a different vehicle sound effect.
Definition: newgrf_sound.cpp:187
TrackBits
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:38
IsDockingTile
static bool IsDockingTile(TileIndex t)
Checks whether the tile is marked as a dockling tile.
Definition: water_map.h:376
TrackdirToTrackdirBits
static TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
Maps a Trackdir to the corresponding TrackdirBits value.
Definition: track_func.h:110
Vehicle::day_counter
byte day_counter
Increased by one for each day.
Definition: vehicle_base.h:328
window_func.h
Depot
Definition: depot_base.h:19
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:386
ShipMoveUpDownOnLock
static bool ShipMoveUpDownOnLock(Ship *v)
Test and move a ship up or down in a lock.
Definition: ship_cmd.cpp:593
PathfinderSettings::yapf
YAPFSettings yapf
pathfinder settings for the yet another pathfinder
Definition: settings_type.h:473
AgeVehicle
void AgeVehicle(Vehicle *v)
Update age of a vehicle.
Definition: vehicle.cpp:1378
Vehicle::progress
byte progress
The percentage (if divided by 256) this vehicle already crossed the tile unit.
Definition: vehicle_base.h:311
Ship::rotation_x_pos
int16 rotation_x_pos
NOSAVE: X Position before rotation.
Definition: ship.h:30
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:197
TILE_HEIGHT
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
Definition: tile_type.h:18
OverflowSafeInt< int64 >
IsOilRig
static bool IsOilRig(TileIndex t)
Is tile t part of an oilrig?
Definition: station_map.h:274
engine_base.h
Ship::GetRunningCost
Money GetRunningCost() const
Gets the running cost of a vehicle.
Definition: ship_cmd.cpp:218
Vehicle::cargo_type
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:320
NPFShipCheckReverse
bool NPFShipCheckReverse(const Ship *v, Trackdir *best_td)
Returns true if it is better to reverse the ship before leaving depot using NPF.
Definition: npf.cpp:1214
Vehicle::spritenum
byte spritenum
currently displayed sprite index 0xfd == custom sprite, 0xfe == custom second head sprite 0xff == res...
Definition: vehicle_base.h:294
ShipVehicleInfo::max_speed
uint16 max_speed
Maximum speed (1 unit = 1/3.2 mph = 0.5 km-ish/h)
Definition: engine_type.h:70
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:70
PROP_SHIP_SPEED
@ PROP_SHIP_SPEED
Max. speed: 1 unit = 1/3.2 mph = 0.5 km-ish/h.
Definition: newgrf_properties.h:44
YAPFSettings::maximum_go_to_depot_penalty
uint32 maximum_go_to_depot_penalty
What is the maximum penalty that may be endured for going to a depot.
Definition: settings_type.h:416
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:52
Vehicle::reliability
uint16 reliability
Reliability.
Definition: vehicle_base.h:276
Vehicle::UpdatePosition
void UpdatePosition()
Update the position of the vehicle.
Definition: vehicle.cpp:1610
Engine::flags
byte flags
Flags of the engine.
Definition: engine_base.h:48
INVALID_DIR
@ INVALID_DIR
Flag for an invalid direction.
Definition: direction_type.h:35
Track
Track
These are used to specify a single track.
Definition: track_type.h:19
Rect::Width
int Width() const
Get width of Rect.
Definition: geometry_type.hpp:79
CanVehicleUseStation
bool CanVehicleUseStation(EngineID engine_type, const Station *st)
Can this station be used by the given engine type?
Definition: vehicle.cpp:2861
Vehicle::HandleBreakdown
bool HandleBreakdown()
Handle all of the aspects of a vehicle breakdown This includes adding smoke and sounds,...
Definition: vehicle.cpp:1312
DecreaseVehicleValue
void DecreaseVehicleValue(Vehicle *v)
Decrease the value of a vehicle.
Definition: vehicle.cpp:1250
GetNewVehiclePosResult::y
int y
x and y position of the vehicle after moving
Definition: vehicle_func.h:76
GetTunnelBridgeTransportType
static TransportType GetTunnelBridgeTransportType(TileIndex t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
Definition: tunnelbridge_map.h:39
TrackdirBitsToTrackBits
static TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition: track_func.h:307
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
IsShipDepotTile
static bool IsShipDepotTile(TileIndex t)
Is it a ship depot tile?
Definition: water_map.h:237
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:69
ShipSubcoordData::y_subcoord
byte y_subcoord
New Y sub-coordinate on the new tile.
Definition: ship_cmd.cpp:516
SetWindowClassesDirty
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition: window.cpp:3180
VehicleSpriteSeq::GetBounds
void GetBounds(Rect *bounds) const
Determine shared bounds of all sprites.
Definition: vehicle.cpp:98
ShipVehicleInfo::ApplyWaterClassSpeedFrac
uint ApplyWaterClassSpeedFrac(uint raw_speed, bool is_ocean) const
Apply ocean/canal speed fraction to a velocity.
Definition: engine_type.h:80
SpecializedVehicle< Ship, VEH_SHIP >::UpdateViewport
void UpdateViewport(bool force_update, bool update_delta)
Update vehicle sprite- and position caches.
Definition: vehicle_base.h:1205
GetEffectiveWaterClass
WaterClass GetEffectiveWaterClass(TileIndex tile)
Determine the effective WaterClass for a ship travelling on a tile.
Definition: ship_cmd.cpp:48
DAY_TICKS
static const int DAY_TICKS
1 day is 74 ticks; _date_fract used to be uint16 and incremented by 885.
Definition: date_type.h:28
EXPENSES_SHIP_RUN
@ EXPENSES_SHIP_RUN
Running costs ships.
Definition: economy_type.h:163
GetDepotIndex
static DepotID GetDepotIndex(TileIndex t)
Get the index of which depot is attached to the tile.
Definition: depot_map.h:52
TrackdirToTrack
static Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:261
Engine::reliability
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:40
OrthogonalTileArea::Contains
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition: tilearea.cpp:104
Vehicle::refit_cap
uint16 refit_cap
Capacity left over from before last refit.
Definition: vehicle_base.h:323
GetInclinedSlopeDirection
static DiagDirection GetInclinedSlopeDirection(Slope s)
Returns the direction of an inclined slope.
Definition: slope_func.h:239
Vehicle::date_of_last_service
Date date_of_last_service
Last date the vehicle had a service at a depot.
Definition: vehicle_base.h:275
GetNewVehiclePos
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
Get position information of a vehicle when moving one pixel in the direction it is facing.
Definition: vehicle.cpp:1701
news_func.h
VETS_ENTERED_WORMHOLE
@ VETS_ENTERED_WORMHOLE
The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/...
Definition: tile_cmd.h:22
INVALID_TRACK
@ INVALID_TRACK
Flag for an invalid track.
Definition: track_type.h:28