author | truebrain |
Fri, 18 Jul 2008 01:00:03 +0000 | |
branch | noai |
changeset 11166 | 17960948c3af |
parent 10971 | aaf89f8c59b9 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10370
diff
changeset
|
3 |
/** @file ship_cmd.cpp Handling of ships. */ |
9574 | 4 |
|
0 | 5 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1802
diff
changeset
|
6 |
#include "openttd.h" |
3961
e2783f244ac6
(svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents:
3955
diff
changeset
|
7 |
#include "ship.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
8 |
#include "tile_cmd.h" |
9599 | 9 |
#include "landscape.h" |
9629 | 10 |
#include "timetable.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
11 |
#include "command_func.h" |
0 | 12 |
#include "pathfind.h" |
3442
c494e7246d7b
(svn r4272) -Codechange: Moved the map-accessing stuff from station.h into station_map.h
celestar
parents:
3421
diff
changeset
|
13 |
#include "station_map.h" |
9837
c9ec4f82e0d0
(svn r12503) [NoAI] -Sync: with trunk r12461:12501.
rubidium
parents:
9826
diff
changeset
|
14 |
#include "station_base.h" |
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
15 |
#include "news_func.h" |
9837
c9ec4f82e0d0
(svn r12503) [NoAI] -Sync: with trunk r12461:12501.
rubidium
parents:
9826
diff
changeset
|
16 |
#include "engine_func.h" |
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10370
diff
changeset
|
17 |
#include "engine_base.h" |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
18 |
#include "player_func.h" |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
19 |
#include "player_base.h" |
1247 | 20 |
#include "npf.h" |
10249
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10181
diff
changeset
|
21 |
#include "depot_base.h" |
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10181
diff
changeset
|
22 |
#include "depot_func.h" |
2159
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2150
diff
changeset
|
23 |
#include "vehicle_gui.h" |
2962
dbd168a4703a
(svn r3524) - Split newgrf features from engine.[ch] into newgrf_engine.[ch], and add the new files to project files.
peter1138
parents:
2916
diff
changeset
|
24 |
#include "newgrf_engine.h" |
3454
c2447e8c1e23
(svn r4289) -Codechange: Make use of water_map accessors for ships. Make a mental note to clean ship_cmd
celestar
parents:
3442
diff
changeset
|
25 |
#include "water_map.h" |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
26 |
#include "yapf/yapf.h" |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
27 |
#include "debug.h" |
3989
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
28 |
#include "newgrf_callbacks.h" |
4244
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
29 |
#include "newgrf_text.h" |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4574
diff
changeset
|
30 |
#include "newgrf_sound.h" |
6223
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
31 |
#include "spritecache.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
32 |
#include "strings_func.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
33 |
#include "functions.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
34 |
#include "window_func.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
35 |
#include "date_func.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
36 |
#include "vehicle_func.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
37 |
#include "sound_func.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
38 |
#include "variables.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
39 |
#include "autoreplace_gui.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
40 |
#include "gfx_func.h" |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
41 |
#include "settings_type.h" |
9837
c9ec4f82e0d0
(svn r12503) [NoAI] -Sync: with trunk r12461:12501.
rubidium
parents:
9826
diff
changeset
|
42 |
#include "order_func.h" |
10294 | 43 |
#include "effectvehicle_func.h" |
10370
fa4f88090694
(svn r12911) [NoAI] -Add: added AIEventVehicle(List|WaitingInDepot|Unprofitable) (Yexo)
truebrain
parents:
10355
diff
changeset
|
44 |
#include "ai/ai.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
45 |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
46 |
#include "table/strings.h" |
0 | 47 |
|
48 |
static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D}; |
|
49 |
||
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
50 |
static const TrackBits _ship_sometracks[4] = { |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
51 |
TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_LEFT, // 0x19, // DIAGDIR_NE |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
52 |
TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_LEFT, // 0x16, // DIAGDIR_SE |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
53 |
TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT, // 0x25, // DIAGDIR_SW |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
54 |
TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_RIGHT, // 0x2A, // DIAGDIR_NW |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
55 |
}; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
56 |
|
9732 | 57 |
static inline TrackBits GetTileShipTrackStatus(TileIndex tile) |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
58 |
{ |
9732 | 59 |
return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0)); |
0 | 60 |
} |
61 |
||
10294 | 62 |
static SpriteID GetShipIcon(EngineID engine) |
0 | 63 |
{ |
10294 | 64 |
uint8 spritenum = ShipVehInfo(engine)->image_index; |
0 | 65 |
|
66 |
if (is_custom_sprite(spritenum)) { |
|
10294 | 67 |
SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W); |
68 |
if (sprite != 0) return sprite; |
|
0 | 69 |
|
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10370
diff
changeset
|
70 |
spritenum = GetEngine(engine)->image_index; |
0 | 71 |
} |
10294 | 72 |
|
73 |
return 6 + _ship_sprites[spritenum]; |
|
74 |
} |
|
75 |
||
76 |
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal) |
|
77 |
{ |
|
78 |
DrawSprite(GetShipIcon(engine), pal, x, y); |
|
0 | 79 |
} |
80 |
||
6223
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
81 |
/** Get the size of the sprite of a ship sprite heading west (used for lists) |
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
82 |
* @param engine The engine to get the sprite from |
9574 | 83 |
* @param width The width of the sprite |
84 |
* @param height The height of the sprite |
|
6223
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
85 |
*/ |
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
86 |
void GetShipSpriteSize(EngineID engine, uint &width, uint &height) |
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
87 |
{ |
10294 | 88 |
const Sprite *spr = GetSprite(GetShipIcon(engine)); |
6223
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
89 |
|
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
90 |
width = spr->width; |
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
91 |
height = spr->height; |
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
92 |
} |
92d2073c3d7e
(svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents:
6195
diff
changeset
|
93 |
|
10294 | 94 |
SpriteID Ship::GetImage(Direction direction) const |
0 | 95 |
{ |
10294 | 96 |
uint8 spritenum = this->spritenum; |
0 | 97 |
|
98 |
if (is_custom_sprite(spritenum)) { |
|
10294 | 99 |
SpriteID sprite = GetCustomVehicleSprite(this, direction); |
100 |
if (sprite != 0) return sprite; |
|
0 | 101 |
|
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10370
diff
changeset
|
102 |
spritenum = GetEngine(this->engine_type)->image_index; |
0 | 103 |
} |
10294 | 104 |
|
0 | 105 |
return _ship_sprites[spritenum] + direction; |
106 |
} |
|
107 |
||
2630 | 108 |
static const Depot* FindClosestShipDepot(const Vehicle* v) |
0 | 109 |
{ |
10776 | 110 |
if (_settings_game.pf.pathfinder_for_ships == VPF_NPF) { /* NPF is used */ |
9732 | 111 |
Trackdir trackdir = GetVehicleTrackdir(v); |
112 |
NPFFoundTargetData ftd = NPFRouteToDepotTrialError(v->tile, trackdir, false, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPES); |
|
113 |
||
114 |
if (ftd.best_bird_dist == 0) return GetDepotByTile(ftd.node.tile); /* Found target */ |
|
115 |
||
116 |
return NULL; /* Did not find target */ |
|
117 |
} |
|
118 |
||
119 |
/* OPF or YAPF - find the closest depot */ |
|
120 |
||
2630 | 121 |
const Depot* depot; |
122 |
const Depot* best_depot = NULL; |
|
9732 | 123 |
uint best_dist = UINT_MAX; |
0 | 124 |
|
9732 | 125 |
FOR_ALL_DEPOTS(depot) { |
126 |
TileIndex tile = depot->xy; |
|
10249
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10181
diff
changeset
|
127 |
if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) { |
9732 | 128 |
uint dist = DistanceManhattan(tile, v->tile); |
129 |
if (dist < best_dist) { |
|
130 |
best_dist = dist; |
|
131 |
best_depot = depot; |
|
0 | 132 |
} |
133 |
} |
|
134 |
} |
|
9732 | 135 |
|
0 | 136 |
return best_depot; |
137 |
} |
|
138 |
||
139 |
static void CheckIfShipNeedsService(Vehicle *v) |
|
140 |
{ |
|
10776 | 141 |
if (_settings_game.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return; |
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
142 |
if (v->IsInDepot()) { |
4529
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
143 |
VehicleServiceInDepot(v); |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
144 |
return; |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
145 |
} |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
146 |
|
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
147 |
const Depot *depot = FindClosestShipDepot(v); |
0 | 148 |
|
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
149 |
if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) { |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
150 |
if (v->current_order.IsType(OT_GOTO_DEPOT)) { |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
151 |
v->current_order.MakeDummy(); |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
152 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
0 | 153 |
} |
154 |
return; |
|
155 |
} |
|
156 |
||
10142
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
157 |
v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE); |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
158 |
v->dest_tile = depot->xy; |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
159 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
0 | 160 |
} |
161 |
||
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
162 |
void Ship::OnNewDay() |
0 | 163 |
{ |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
164 |
if ((++this->day_counter & 7) == 0) |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
165 |
DecreaseVehicleValue(this); |
0 | 166 |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
167 |
CheckVehicleBreakdown(this); |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
168 |
AgeVehicle(this); |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
169 |
CheckIfShipNeedsService(this); |
19
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
170 |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
171 |
CheckOrders(this); |
0 | 172 |
|
9732 | 173 |
if (this->running_ticks == 0) return; |
0 | 174 |
|
9732 | 175 |
CommandCost cost(EXPENSES_SHIP_RUN, GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running * this->running_ticks / (364 * DAY_TICKS)); |
176 |
||
177 |
this->profit_this_year -= cost.GetCost(); |
|
178 |
this->running_ticks = 0; |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
179 |
|
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
180 |
SubtractMoneyFromPlayerFract(this->owner, cost); |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
181 |
|
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
182 |
InvalidateWindow(WC_VEHICLE_DETAILS, this->index); |
9574 | 183 |
/* we need this for the profit */ |
1055
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
184 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 185 |
} |
186 |
||
187 |
static void HandleBrokenShip(Vehicle *v) |
|
188 |
{ |
|
189 |
if (v->breakdown_ctr != 1) { |
|
190 |
v->breakdown_ctr = 1; |
|
191 |
v->cur_speed = 0; |
|
192 |
||
193 |
if (v->breakdowns_since_last_service != 255) |
|
194 |
v->breakdowns_since_last_service++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
195 |
|
0 | 196 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
197 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
198 |
|
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4574
diff
changeset
|
199 |
if (!PlayVehicleSound(v, VSE_BREAKDOWN)) { |
10776 | 200 |
SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ? |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4574
diff
changeset
|
201 |
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v); |
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4574
diff
changeset
|
202 |
} |
0 | 203 |
|
204 |
if (!(v->vehstatus & VS_HIDDEN)) { |
|
205 |
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); |
|
10294 | 206 |
if (u != NULL) u->u.effect.animation_state = v->breakdown_delay * 2; |
0 | 207 |
} |
208 |
} |
|
209 |
||
210 |
if (!(v->tick_counter & 1)) { |
|
211 |
if (!--v->breakdown_delay) { |
|
212 |
v->breakdown_ctr = 0; |
|
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1128
diff
changeset
|
213 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
0 | 214 |
} |
215 |
} |
|
216 |
} |
|
217 |
||
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
218 |
void Ship::MarkDirty() |
0 | 219 |
{ |
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9629
diff
changeset
|
220 |
this->cur_image = this->GetImage(this->direction); |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
221 |
MarkSingleVehicleDirty(this); |
0 | 222 |
} |
223 |
||
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
224 |
static void PlayShipSound(const Vehicle *v) |
0 | 225 |
{ |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4574
diff
changeset
|
226 |
if (!PlayVehicleSound(v, VSE_START)) { |
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4574
diff
changeset
|
227 |
SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v); |
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4574
diff
changeset
|
228 |
} |
0 | 229 |
} |
230 |
||
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
231 |
void Ship::PlayLeaveStationSound() const |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
232 |
{ |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
233 |
PlayShipSound(this); |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
234 |
} |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
235 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
236 |
TileIndex Ship::GetOrderStationLocation(StationID station) |
0 | 237 |
{ |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
238 |
if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION; |
3005
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2962
diff
changeset
|
239 |
|
10181
54df587fef5d
(svn r12712) [NoAI] -Sync with trunk r12672:12711.
rubidium
parents:
10142
diff
changeset
|
240 |
const Station *st = GetStation(station); |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
241 |
if (st->dock_tile != 0) { |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
242 |
return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile))); |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
243 |
} else { |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
244 |
this->cur_order_index++; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
245 |
return 0; |
0 | 246 |
} |
247 |
} |
|
248 |
||
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
249 |
void Ship::UpdateDeltaXY(Direction direction) |
0 | 250 |
{ |
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
251 |
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0) |
0 | 252 |
static const uint32 _delta_xy_table[8] = { |
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
253 |
MKIT( 6, 6, -3, -3), |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
254 |
MKIT( 6, 32, -3, -16), |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
255 |
MKIT( 6, 6, -3, -3), |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
256 |
MKIT(32, 6, -16, -3), |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
257 |
MKIT( 6, 6, -3, -3), |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
258 |
MKIT( 6, 32, -3, -16), |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
259 |
MKIT( 6, 6, -3, -3), |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
260 |
MKIT(32, 6, -16, -3), |
0 | 261 |
}; |
262 |
#undef MKIT |
|
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
263 |
|
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
264 |
uint32 x = _delta_xy_table[direction]; |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
265 |
this->x_offs = GB(x, 0, 8); |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
266 |
this->y_offs = GB(x, 8, 8); |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
267 |
this->x_extent = GB(x, 16, 8); |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
268 |
this->y_extent = GB(x, 24, 8); |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
269 |
this->z_extent = 6; |
0 | 270 |
} |
271 |
||
4725
f7284b86833f
(svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents:
4712
diff
changeset
|
272 |
void RecalcShipStuff(Vehicle *v) |
0 | 273 |
{ |
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
274 |
v->UpdateDeltaXY(v->direction); |
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9629
diff
changeset
|
275 |
v->cur_image = v->GetImage(v->direction); |
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
276 |
v->MarkDirty(); |
0 | 277 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
278 |
} |
|
279 |
||
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
280 |
static const TileIndexDiffC _ship_leave_depot_offs[] = { |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
281 |
{-1, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
282 |
{ 0, -1} |
0 | 283 |
}; |
284 |
||
285 |
static void CheckShipLeaveDepot(Vehicle *v) |
|
286 |
{ |
|
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
287 |
if (!v->IsInDepot()) return; |
0 | 288 |
|
10355
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
10294
diff
changeset
|
289 |
TileIndex tile = v->tile; |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
10294
diff
changeset
|
290 |
Axis axis = GetShipDepotAxis(tile); |
0 | 291 |
|
10355
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
10294
diff
changeset
|
292 |
/* Check first (north) side */ |
3454
c2447e8c1e23
(svn r4289) -Codechange: Make use of water_map accessors for ships. Make a mental note to clean ship_cmd
celestar
parents:
3442
diff
changeset
|
293 |
if (_ship_sometracks[axis] & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis])))) { |
10355
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
10294
diff
changeset
|
294 |
v->direction = ReverseDir(AxisToDirection(axis)); |
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
10294
diff
changeset
|
295 |
/* Check second (south) side */ |
3454
c2447e8c1e23
(svn r4289) -Codechange: Make use of water_map accessors for ships. Make a mental note to clean ship_cmd
celestar
parents:
3442
diff
changeset
|
296 |
} else if (_ship_sometracks[axis + 2] & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[axis])))) { |
10355
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
10294
diff
changeset
|
297 |
v->direction = AxisToDirection(axis); |
0 | 298 |
} else { |
299 |
return; |
|
300 |
} |
|
10355
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
10294
diff
changeset
|
301 |
|
ee4b5f7a5bf2
(svn r12896) [NoAI] -Sync: with trunk r12824:r12895.
rubidium
parents:
10294
diff
changeset
|
302 |
v->u.ship.state = AxisToTrackBits(axis); |
0 | 303 |
v->vehstatus &= ~VS_HIDDEN; |
304 |
||
305 |
v->cur_speed = 0; |
|
306 |
RecalcShipStuff(v); |
|
307 |
||
308 |
PlayShipSound(v); |
|
578
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
309 |
VehicleServiceInDepot(v); |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4725
diff
changeset
|
310 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
1055
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
311 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 312 |
} |
313 |
||
314 |
static bool ShipAccelerate(Vehicle *v) |
|
315 |
{ |
|
316 |
uint spd; |
|
317 |
byte t; |
|
318 |
||
9601
b499fdd106d5
(svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents:
9599
diff
changeset
|
319 |
spd = min(v->cur_speed + 1, GetVehicleProperty(v, 0x0B, v->max_speed)); |
0 | 320 |
|
9574 | 321 |
/*updates statusbar only if speed have changed to save CPU time */ |
0 | 322 |
if (spd != v->cur_speed) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
323 |
v->cur_speed = spd; |
10776 | 324 |
if (_settings_client.gui.vehicle_speed) |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
325 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
0 | 326 |
} |
327 |
||
9574 | 328 |
/* Decrease somewhat when turning */ |
2639 | 329 |
if (!(v->direction & 1)) spd = spd * 3 / 4; |
0 | 330 |
|
2639 | 331 |
if (spd == 0) return false; |
332 |
if ((byte)++spd == 0) return true; |
|
0 | 333 |
|
334 |
v->progress = (t = v->progress) - (byte)spd; |
|
335 |
||
336 |
return (t < v->progress); |
|
337 |
} |
|
338 |
||
9629 | 339 |
static CommandCost EstimateShipCost(EngineID engine_type) |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
340 |
{ |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
341 |
return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine_type, 0x0A, ShipVehInfo(engine_type)->base_cost) * (_price.ship_base >> 3) >> 5); |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
342 |
} |
0 | 343 |
|
2630 | 344 |
static void ShipArrivesAt(const Vehicle* v, Station* st) |
0 | 345 |
{ |
346 |
/* Check if station was ever visited before */ |
|
347 |
if (!(st->had_vehicle_of_type & HVOT_SHIP)) { |
|
348 |
st->had_vehicle_of_type |= HVOT_SHIP; |
|
2639 | 349 |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
350 |
SetDParam(0, st->index); |
0 | 351 |
AddNewsItem( |
352 |
STR_9833_CITIZENS_CELEBRATE_FIRST, |
|
10645 | 353 |
(v->owner == _local_player) ? NS_ARRIVAL_PLAYER : NS_ARRIVAL_OTHER, |
0 | 354 |
v->index, |
355 |
0); |
|
10971
aaf89f8c59b9
(svn r13525) [NoAI] -Add: added AIEventEngineAvailable and AIEventStationFirstVehicle
glx
parents:
10920
diff
changeset
|
356 |
AI_Event(v->owner, new AIEventStationFirstVehicle(st->index, v->index)); |
0 | 357 |
} |
358 |
} |
|
359 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
360 |
struct PathFindShip { |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
361 |
TileIndex skiptile; |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
362 |
TileIndex dest_coords; |
0 | 363 |
uint best_bird_dist; |
364 |
uint best_length; |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
365 |
}; |
0 | 366 |
|
9732 | 367 |
static bool ShipTrackFollower(TileIndex tile, PathFindShip *pfs, int track, uint length) |
0 | 368 |
{ |
9574 | 369 |
/* Found dest? */ |
0 | 370 |
if (tile == pfs->dest_coords) { |
371 |
pfs->best_bird_dist = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
372 |
|
0 | 373 |
pfs->best_length = minu(pfs->best_length, length); |
374 |
return true; |
|
375 |
} |
|
376 |
||
9574 | 377 |
/* Skip this tile in the calculation */ |
0 | 378 |
if (tile != pfs->skiptile) { |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1237
diff
changeset
|
379 |
pfs->best_bird_dist = minu(pfs->best_bird_dist, DistanceMaxPlusManhattan(pfs->dest_coords, tile)); |
0 | 380 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
381 |
|
0 | 382 |
return false; |
383 |
} |
|
384 |
||
385 |
static const byte _ship_search_directions[6][4] = { |
|
386 |
{ 0, 9, 2, 9 }, |
|
387 |
{ 9, 1, 9, 3 }, |
|
388 |
{ 9, 0, 3, 9 }, |
|
389 |
{ 1, 9, 9, 2 }, |
|
390 |
{ 3, 2, 9, 9 }, |
|
391 |
{ 9, 9, 1, 0 }, |
|
392 |
}; |
|
393 |
||
394 |
static const byte _pick_shiptrack_table[6] = {1, 3, 2, 2, 0, 0}; |
|
395 |
||
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
396 |
static uint FindShipTrack(Vehicle *v, TileIndex tile, DiagDirection dir, TrackBits bits, TileIndex skiptile, Track *track) |
0 | 397 |
{ |
398 |
PathFindShip pfs; |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
399 |
Track i, best_track; |
0 | 400 |
uint best_bird_dist = 0; |
401 |
uint best_length = 0; |
|
402 |
uint r; |
|
403 |
byte ship_dir = v->direction & 3; |
|
404 |
||
405 |
pfs.dest_coords = v->dest_tile; |
|
406 |
pfs.skiptile = skiptile; |
|
407 |
||
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
408 |
best_track = INVALID_TRACK; |
0 | 409 |
|
410 |
do { |
|
5849
58039c9dc565
(svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents:
5838
diff
changeset
|
411 |
i = RemoveFirstTrack(&bits); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
412 |
|
0 | 413 |
pfs.best_bird_dist = (uint)-1; |
414 |
pfs.best_length = (uint)-1; |
|
415 |
||
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
416 |
FollowTrack(tile, PATHFIND_FLAGS_SHIP_MODE | PATHFIND_FLAGS_DISABLE_TILE_HASH, TRANSPORT_WATER, 0, (DiagDirection)_ship_search_directions[i][dir], (TPFEnumProc*)ShipTrackFollower, NULL, &pfs); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
417 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
418 |
if (best_track != INVALID_TRACK) { |
0 | 419 |
if (pfs.best_bird_dist != 0) { |
420 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
|
421 |
if (pfs.best_bird_dist > best_bird_dist) goto bad; |
|
422 |
if (pfs.best_bird_dist < best_bird_dist) goto good; |
|
423 |
} else { |
|
424 |
if (pfs.best_length > best_length) goto bad; |
|
425 |
if (pfs.best_length < best_length) goto good; |
|
426 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
427 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
428 |
/* if we reach this position, there's two paths of equal value so far. |
0 | 429 |
* pick one randomly. */ |
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2049
diff
changeset
|
430 |
r = GB(Random(), 0, 8); |
0 | 431 |
if (_pick_shiptrack_table[i] == ship_dir) r += 80; |
432 |
if (_pick_shiptrack_table[best_track] == ship_dir) r -= 80; |
|
433 |
if (r <= 127) goto bad; |
|
434 |
} |
|
435 |
good:; |
|
436 |
best_track = i; |
|
437 |
best_bird_dist = pfs.best_bird_dist; |
|
438 |
best_length = pfs.best_length; |
|
439 |
bad:; |
|
440 |
||
441 |
} while (bits != 0); |
|
442 |
||
443 |
*track = best_track; |
|
444 |
return best_bird_dist; |
|
445 |
} |
|
446 |
||
9732 | 447 |
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypes railtypes) |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
448 |
{ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
449 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
450 |
void* perf = NpfBeginInterval(); |
9732 | 451 |
NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, ignore_start_tile, target, type, 0, owner, railtypes); |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
452 |
int t = NpfEndInterval(perf); |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5259
diff
changeset
|
453 |
DEBUG(yapf, 4, "[NPFW] %d us - %d rounds - %d open - %d closed -- ", t, 0, _aystar_stats_open_size, _aystar_stats_closed_size); |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
454 |
return ret; |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
455 |
} |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
456 |
|
9574 | 457 |
/** returns the track to choose on the next tile, or -1 when it's better to |
1247 | 458 |
* reverse. The tile given is the tile we are about to enter, enterdir is the |
459 |
* direction in which we are entering the tile */ |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
460 |
static Track ChooseShipTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks) |
0 | 461 |
{ |
9732 | 462 |
assert(IsValidDiagDirection(enterdir)); |
0 | 463 |
|
10776 | 464 |
switch (_settings_game.pf.pathfinder_for_ships) { |
9732 | 465 |
case VPF_YAPF: { /* YAPF */ |
466 |
Trackdir trackdir = YapfChooseShipTrack(v, tile, enterdir, tracks); |
|
467 |
if (trackdir != INVALID_TRACKDIR) return TrackdirToTrack(trackdir); |
|
468 |
} break; |
|
1247 | 469 |
|
9732 | 470 |
case VPF_NPF: { /* NPF */ |
471 |
NPFFindStationOrTileData fstd; |
|
472 |
Trackdir trackdir = GetVehicleTrackdir(v); |
|
473 |
assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot |
|
1247 | 474 |
|
9732 | 475 |
NPFFillWithOrderData(&fstd, v); |
1247 | 476 |
|
9732 | 477 |
NPFFoundTargetData ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_WATER, v->owner, INVALID_RAILTYPES); |
478 |
||
1698
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1530
diff
changeset
|
479 |
/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains |
9732 | 480 |
* the direction we need to take to get there, if ftd.best_bird_dist is not 0, |
481 |
* we did not find our target, but ftd.best_trackdir contains the direction leading |
|
482 |
* to the tile closest to our target. */ |
|
483 |
if (ftd.best_trackdir != 0xff) return TrackdirToTrack(ftd.best_trackdir); /* TODO: Wrapper function? */ |
|
484 |
} break; |
|
1247 | 485 |
|
9732 | 486 |
default: |
487 |
case VPF_OPF: { /* OPF */ |
|
488 |
TileIndex tile2 = TILE_ADD(tile, -TileOffsByDiagDir(enterdir)); |
|
489 |
Track track; |
|
1247 | 490 |
|
9732 | 491 |
/* Let's find out how far it would be if we would reverse first */ |
492 |
TrackBits b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagDir(enterdir)] & v->u.ship.state; |
|
493 |
||
494 |
uint distr = UINT_MAX; // distance if we reversed |
|
495 |
if (b != 0) { |
|
496 |
distr = FindShipTrack(v, tile2, ReverseDiagDir(enterdir), b, tile, &track); |
|
497 |
if (distr != UINT_MAX) distr++; // penalty for reversing |
|
498 |
} |
|
499 |
||
500 |
/* And if we would not reverse? */ |
|
501 |
uint dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track); |
|
502 |
||
503 |
if (dist <= distr) return track; |
|
504 |
} break; |
|
0 | 505 |
} |
9732 | 506 |
|
507 |
return INVALID_TRACK; /* We could better reverse */ |
|
0 | 508 |
} |
509 |
||
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3147
diff
changeset
|
510 |
static const Direction _new_vehicle_direction_table[] = { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
511 |
DIR_N , DIR_NW, DIR_W , INVALID_DIR, |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
512 |
DIR_NE, DIR_N , DIR_SW, INVALID_DIR, |
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3147
diff
changeset
|
513 |
DIR_E , DIR_SE, DIR_S |
0 | 514 |
}; |
515 |
||
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
516 |
static Direction ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile) |
0 | 517 |
{ |
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
518 |
uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 + |
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
519 |
TileX(new_tile) - TileX(old_tile) + 1; |
0 | 520 |
assert(offs < 11 && offs != 3 && offs != 7); |
521 |
return _new_vehicle_direction_table[offs]; |
|
522 |
} |
|
523 |
||
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
524 |
static Direction ShipGetNewDirection(Vehicle *v, int x, int y) |
0 | 525 |
{ |
526 |
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); |
|
527 |
assert(offs < 11 && offs != 3 && offs != 7); |
|
528 |
return _new_vehicle_direction_table[offs]; |
|
529 |
} |
|
530 |
||
9732 | 531 |
static inline TrackBits GetAvailShipTracks(TileIndex tile, int dir) |
0 | 532 |
{ |
9732 | 533 |
return GetTileShipTrackStatus(tile) & _ship_sometracks[dir]; |
0 | 534 |
} |
535 |
||
536 |
static const byte _ship_subcoord[4][6][3] = { |
|
537 |
{ |
|
538 |
{15, 8, 1}, |
|
539 |
{ 0, 0, 0}, |
|
540 |
{ 0, 0, 0}, |
|
541 |
{15, 8, 2}, |
|
542 |
{15, 7, 0}, |
|
543 |
{ 0, 0, 0}, |
|
544 |
}, |
|
545 |
{ |
|
546 |
{ 0, 0, 0}, |
|
547 |
{ 8, 0, 3}, |
|
548 |
{ 7, 0, 2}, |
|
549 |
{ 0, 0, 0}, |
|
550 |
{ 8, 0, 4}, |
|
551 |
{ 0, 0, 0}, |
|
552 |
}, |
|
553 |
{ |
|
554 |
{ 0, 8, 5}, |
|
555 |
{ 0, 0, 0}, |
|
556 |
{ 0, 7, 6}, |
|
557 |
{ 0, 0, 0}, |
|
558 |
{ 0, 0, 0}, |
|
559 |
{ 0, 8, 4}, |
|
560 |
}, |
|
561 |
{ |
|
562 |
{ 0, 0, 0}, |
|
9601
b499fdd106d5
(svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents:
9599
diff
changeset
|
563 |
{ 8, 15, 7}, |
0 | 564 |
{ 0, 0, 0}, |
9601
b499fdd106d5
(svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents:
9599
diff
changeset
|
565 |
{ 8, 15, 6}, |
0 | 566 |
{ 0, 0, 0}, |
9601
b499fdd106d5
(svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents:
9599
diff
changeset
|
567 |
{ 7, 15, 0}, |
0 | 568 |
} |
569 |
}; |
|
570 |
||
571 |
static void ShipController(Vehicle *v) |
|
572 |
{ |
|
573 |
uint32 r; |
|
574 |
const byte *b; |
|
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3147
diff
changeset
|
575 |
Direction dir; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
576 |
Track track; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
577 |
TrackBits tracks; |
0 | 578 |
|
579 |
v->tick_counter++; |
|
9629 | 580 |
v->current_order_time++; |
0 | 581 |
|
582 |
if (v->breakdown_ctr != 0) { |
|
583 |
if (v->breakdown_ctr <= 2) { |
|
584 |
HandleBrokenShip(v); |
|
585 |
return; |
|
586 |
} |
|
10294 | 587 |
if (!v->current_order.IsType(OT_LOADING)) v->breakdown_ctr--; |
0 | 588 |
} |
589 |
||
2639 | 590 |
if (v->vehstatus & VS_STOPPED) return; |
0 | 591 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
592 |
ProcessOrders(v); |
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
593 |
v->HandleLoading(); |
0 | 594 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9837
diff
changeset
|
595 |
if (v->current_order.IsType(OT_LOADING)) return; |
0 | 596 |
|
597 |
CheckShipLeaveDepot(v); |
|
598 |
||
2639 | 599 |
if (!ShipAccelerate(v)) return; |
0 | 600 |
|
601 |
BeginVehicleMove(v); |
|
602 |
||
6479 | 603 |
GetNewVehiclePosResult gp = GetNewVehiclePos(v); |
10920 | 604 |
if (v->u.ship.state != TRACK_BIT_WORMHOLE) { |
605 |
/* Not on a bridge */ |
|
606 |
if (gp.old_tile == gp.new_tile) { |
|
607 |
/* Staying in tile */ |
|
608 |
if (v->IsInDepot()) { |
|
609 |
gp.x = v->x_pos; |
|
610 |
gp.y = v->y_pos; |
|
611 |
} else { |
|
612 |
/* Not inside depot */ |
|
613 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
614 |
if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction; |
|
0 | 615 |
|
10920 | 616 |
/* A leave station order only needs one tick to get processed, so we can |
617 |
* always skip ahead. */ |
|
618 |
if (v->current_order.IsType(OT_LEAVESTATION)) { |
|
619 |
v->current_order.Free(); |
|
620 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
|
621 |
} else if (v->dest_tile != 0) { |
|
622 |
/* We have a target, let's see if we reached it... */ |
|
623 |
if (v->current_order.IsType(OT_GOTO_STATION) && |
|
624 |
IsBuoyTile(v->dest_tile) && |
|
625 |
DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) { |
|
626 |
/* We got within 3 tiles of our target buoy, so let's skip to our |
|
627 |
* next order */ |
|
628 |
UpdateVehicleTimetable(v, true); |
|
629 |
v->cur_order_index++; |
|
630 |
v->current_order.MakeDummy(); |
|
631 |
InvalidateVehicleOrder(v); |
|
632 |
} else { |
|
633 |
/* Non-buoy orders really need to reach the tile */ |
|
634 |
if (v->dest_tile == gp.new_tile) { |
|
635 |
if (v->current_order.IsType(OT_GOTO_DEPOT)) { |
|
636 |
if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) { |
|
637 |
VehicleEnterDepot(v); |
|
638 |
return; |
|
639 |
} |
|
640 |
} else if (v->current_order.IsType(OT_GOTO_STATION)) { |
|
641 |
v->last_station_visited = v->current_order.GetDestination(); |
|
1751
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
642 |
|
10920 | 643 |
/* Process station in the orderlist. */ |
644 |
Station *st = GetStation(v->current_order.GetDestination()); |
|
645 |
if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations |
|
646 |
ShipArrivesAt(v, st); |
|
647 |
v->BeginLoading(); |
|
648 |
} else { // leave stations without docks right aways |
|
649 |
v->current_order.MakeLeaveStation(); |
|
650 |
v->cur_order_index++; |
|
651 |
InvalidateVehicleOrder(v); |
|
652 |
} |
|
1751
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
653 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
654 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
655 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
656 |
} |
0 | 657 |
} |
10920 | 658 |
} else { |
659 |
DiagDirection diagdir; |
|
660 |
/* New tile */ |
|
661 |
if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) { |
|
662 |
goto reverse_direction; |
|
663 |
} |
|
664 |
||
665 |
dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile); |
|
666 |
assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW); |
|
667 |
diagdir = DirToDiagDir(dir); |
|
668 |
tracks = GetAvailShipTracks(gp.new_tile, diagdir); |
|
669 |
if (tracks == TRACK_BIT_NONE) goto reverse_direction; |
|
670 |
||
671 |
/* Choose a direction, and continue if we find one */ |
|
672 |
track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks); |
|
673 |
if (track == INVALID_TRACK) goto reverse_direction; |
|
674 |
||
675 |
b = _ship_subcoord[diagdir][track]; |
|
676 |
||
677 |
gp.x = (gp.x & ~0xF) | b[0]; |
|
678 |
gp.y = (gp.y & ~0xF) | b[1]; |
|
679 |
||
680 |
/* Call the landscape function and tell it that the vehicle entered the tile */ |
|
681 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
682 |
if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction; |
|
683 |
||
684 |
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) { |
|
685 |
v->tile = gp.new_tile; |
|
686 |
v->u.ship.state = TrackToTrackBits(track); |
|
687 |
} |
|
688 |
||
689 |
v->direction = (Direction)b[2]; |
|
0 | 690 |
} |
691 |
} else { |
|
10920 | 692 |
/* On a bridge */ |
693 |
if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) { |
|
694 |
v->x_pos = gp.x; |
|
695 |
v->y_pos = gp.y; |
|
696 |
VehiclePositionChanged(v); |
|
697 |
if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v); |
|
698 |
return; |
|
6320
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
699 |
} |
0 | 700 |
} |
701 |
||
702 |
/* update image of ship, as well as delta XY */ |
|
703 |
dir = ShipGetNewDirection(v, gp.x, gp.y); |
|
704 |
v->x_pos = gp.x; |
|
705 |
v->y_pos = gp.y; |
|
706 |
v->z_pos = GetSlopeZ(gp.x, gp.y); |
|
707 |
||
708 |
getout: |
|
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
709 |
v->UpdateDeltaXY(dir); |
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9629
diff
changeset
|
710 |
v->cur_image = v->GetImage(dir); |
0 | 711 |
VehiclePositionChanged(v); |
712 |
EndVehicleMove(v); |
|
713 |
return; |
|
714 |
||
715 |
reverse_direction: |
|
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3147
diff
changeset
|
716 |
dir = ReverseDir(v->direction); |
0 | 717 |
v->direction = dir; |
718 |
goto getout; |
|
719 |
} |
|
720 |
||
721 |
static void AgeShipCargo(Vehicle *v) |
|
722 |
{ |
|
2639 | 723 |
if (_age_cargo_skip_counter != 0) return; |
9629 | 724 |
v->cargo.AgeCargo(); |
0 | 725 |
} |
726 |
||
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9629
diff
changeset
|
727 |
void Ship::Tick() |
0 | 728 |
{ |
9732 | 729 |
if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++; |
730 |
||
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9629
diff
changeset
|
731 |
AgeShipCargo(this); |
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9629
diff
changeset
|
732 |
ShipController(this); |
0 | 733 |
} |
734 |
||
735 |
||
6573 | 736 |
void ShipsYearlyLoop() |
0 | 737 |
{ |
738 |
Vehicle *v; |
|
739 |
||
740 |
FOR_ALL_VEHICLES(v) { |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
741 |
if (v->type == VEH_SHIP) { |
10370
fa4f88090694
(svn r12911) [NoAI] -Add: added AIEventVehicle(List|WaitingInDepot|Unprofitable) (Yexo)
truebrain
parents:
10355
diff
changeset
|
742 |
if (v->age >= 730 && v->GetDisplayProfitThisYear() < 0) { |
fa4f88090694
(svn r12911) [NoAI] -Add: added AIEventVehicle(List|WaitingInDepot|Unprofitable) (Yexo)
truebrain
parents:
10355
diff
changeset
|
743 |
AI_Event(v->owner, new AIEventVehicleUnprofitable(v->index)); |
fa4f88090694
(svn r12911) [NoAI] -Add: added AIEventVehicle(List|WaitingInDepot|Unprofitable) (Yexo)
truebrain
parents:
10355
diff
changeset
|
744 |
} |
0 | 745 |
v->profit_last_year = v->profit_this_year; |
746 |
v->profit_this_year = 0; |
|
747 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
748 |
} |
|
749 |
} |
|
750 |
} |
|
751 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
752 |
/** Build a ship. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3476
diff
changeset
|
753 |
* @param tile tile of depot where ship is built |
9574 | 754 |
* @param flags type of operation |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
755 |
* @param p1 ship type being built (engine) |
3816
2cdb29b57c1f
(svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents:
3645
diff
changeset
|
756 |
* @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
757 |
*/ |
9629 | 758 |
CommandCost CmdBuildShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 759 |
{ |
9629 | 760 |
CommandCost value; |
1282
e7a73ee62d2f
(svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents:
1266
diff
changeset
|
761 |
UnitID unit_num; |
0 | 762 |
Engine *e; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
763 |
|
9574 | 764 |
if (!IsEngineBuildable(p1, VEH_SHIP, _current_player)) return_cmd_error(STR_SHIP_NOT_AVAILABLE); |
1196
115f46e3807d
(svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents:
1151
diff
changeset
|
765 |
|
0 | 766 |
value = EstimateShipCost(p1); |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
767 |
if (flags & DC_QUERY_COST) return value; |
0 | 768 |
|
1443
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1401
diff
changeset
|
769 |
/* The ai_new queries the vehicle cost before building the route, |
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1401
diff
changeset
|
770 |
* so we must check against cheaters no sooner than now. --pasky */ |
10249
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10181
diff
changeset
|
771 |
if (!IsShipDepotTile(tile)) return CMD_ERROR; |
1901
fb05044cf5c3
(svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents:
1891
diff
changeset
|
772 |
if (!IsTileOwner(tile, _current_player)) return CMD_ERROR; |
1443
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1401
diff
changeset
|
773 |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
774 |
unit_num = HasBit(p2, 0) ? 0 : GetFreeUnitNumber(VEH_SHIP); |
3816
2cdb29b57c1f
(svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents:
3645
diff
changeset
|
775 |
|
10776 | 776 |
if (!Vehicle::AllocateList(NULL, 1) || unit_num > _settings_game.vehicle.max_ships) |
0 | 777 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
778 |
|
0 | 779 |
if (flags & DC_EXEC) { |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3476
diff
changeset
|
780 |
int x; |
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3476
diff
changeset
|
781 |
int y; |
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3476
diff
changeset
|
782 |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
783 |
const ShipVehicleInfo *svi = ShipVehInfo(p1); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
784 |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
785 |
Vehicle *v = new Ship(); |
0 | 786 |
v->unitnumber = unit_num; |
787 |
||
788 |
v->owner = _current_player; |
|
789 |
v->tile = tile; |
|
3645
86af43f87885
(svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents:
3610
diff
changeset
|
790 |
x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2; |
86af43f87885
(svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents:
3610
diff
changeset
|
791 |
y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2; |
0 | 792 |
v->x_pos = x; |
793 |
v->y_pos = y; |
|
9601
b499fdd106d5
(svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents:
9599
diff
changeset
|
794 |
v->z_pos = GetSlopeZ(x, y); |
0 | 795 |
|
9732 | 796 |
v->running_ticks = 0; |
797 |
||
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
798 |
v->UpdateDeltaXY(v->direction); |
0 | 799 |
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
800 |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
801 |
v->spritenum = svi->image_index; |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
802 |
v->cargo_type = svi->cargo_type; |
3870
766d7bd6c299
(svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents:
3816
diff
changeset
|
803 |
v->cargo_subtype = 0; |
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
804 |
v->cargo_cap = svi->capacity; |
9629 | 805 |
v->value = value.GetCost(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
806 |
|
1266
9dc5638fe8cc
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1247
diff
changeset
|
807 |
v->last_station_visited = INVALID_STATION; |
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
808 |
v->max_speed = svi->max_speed; |
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2464
diff
changeset
|
809 |
v->engine_type = p1; |
0 | 810 |
|
1926
68d60188a22f
(svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents:
1901
diff
changeset
|
811 |
e = GetEngine(p1); |
0 | 812 |
v->reliability = e->reliability; |
813 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
814 |
v->max_age = e->lifelength * 366; |
|
2564 | 815 |
_new_vehicle_id = v->index; |
0 | 816 |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
817 |
v->name = NULL; |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6317
diff
changeset
|
818 |
v->u.ship.state = TRACK_BIT_DEPOT; |
0 | 819 |
|
10776 | 820 |
v->service_interval = _settings_game.vehicle.servint_ships; |
0 | 821 |
v->date_of_last_service = _date; |
4329
9759d5c52010
(svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents:
4293
diff
changeset
|
822 |
v->build_year = _cur_year; |
0 | 823 |
v->cur_image = 0x0E5E; |
2804
b932fe148d36
(svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents:
2791
diff
changeset
|
824 |
v->random_bits = VehicleRandomBits(); |
0 | 825 |
|
6502
7ca154aca2b7
(svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents:
6501
diff
changeset
|
826 |
v->vehicle_flags = 0; |
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
827 |
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE); |
6502
7ca154aca2b7
(svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents:
6501
diff
changeset
|
828 |
|
9624 | 829 |
v->cargo_cap = GetVehicleProperty(v, 0x0D, svi->capacity); |
830 |
||
0 | 831 |
VehiclePositionChanged(v); |
832 |
||
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4725
diff
changeset
|
833 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
10645 | 834 |
InvalidateWindowClassesData(WC_SHIPS_LIST, 0); |
0 | 835 |
InvalidateWindow(WC_COMPANY, v->owner); |
2618
7546b838230b
(svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents:
2574
diff
changeset
|
836 |
if (IsLocalPlayer()) |
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
837 |
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Ship window |
6195
b90cf92697b9
(svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents:
6173
diff
changeset
|
838 |
|
b90cf92697b9
(svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents:
6173
diff
changeset
|
839 |
GetPlayer(_current_player)->num_engines[p1]++; |
0 | 840 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
841 |
|
0 | 842 |
return value; |
843 |
} |
|
844 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
845 |
/** Sell a ship. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3476
diff
changeset
|
846 |
* @param tile unused |
9574 | 847 |
* @param flags type of operation |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
848 |
* @param p1 vehicle ID to be sold |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
849 |
* @param p2 unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
850 |
*/ |
9629 | 851 |
CommandCost CmdSellShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 852 |
{ |
853 |
Vehicle *v; |
|
854 |
||
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
855 |
if (!IsValidVehicleID(p1)) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
856 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
857 |
v = GetVehicle(p1); |
0 | 858 |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
859 |
if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 860 |
|
9718
f82a4facea8b
(svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents:
9704
diff
changeset
|
861 |
if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE); |
f82a4facea8b
(svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents:
9704
diff
changeset
|
862 |
|
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
863 |
if (!v->IsStoppedInDepot()) { |
0 | 864 |
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); |
3961
e2783f244ac6
(svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents:
3955
diff
changeset
|
865 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
866 |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
867 |
CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value); |
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
868 |
|
0 | 869 |
if (flags & DC_EXEC) { |
870 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
10645 | 871 |
InvalidateWindowClassesData(WC_SHIPS_LIST, 0); |
0 | 872 |
InvalidateWindow(WC_COMPANY, v->owner); |
873 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
5256
90226d8da8fb
(svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents:
5215
diff
changeset
|
874 |
DeleteDepotHighlightOfVehicle(v); |
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9686
diff
changeset
|
875 |
delete v; |
0 | 876 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
877 |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
878 |
return ret; |
0 | 879 |
} |
880 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
881 |
/** Start/Stop a ship. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3476
diff
changeset
|
882 |
* @param tile unused |
9574 | 883 |
* @param flags type of operation |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
884 |
* @param p1 ship ID to start/stop |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
885 |
* @param p2 unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
886 |
*/ |
9629 | 887 |
CommandCost CmdStartStopShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 888 |
{ |
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
889 |
if (!IsValidVehicleID(p1)) return CMD_ERROR; |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
890 |
|
10294 | 891 |
Vehicle *v = GetVehicle(p1); |
0 | 892 |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
893 |
if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 894 |
|
4244
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
895 |
/* Check if this ship can be started/stopped. The callback will fail or |
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
896 |
* return 0xFF if it can. */ |
10294 | 897 |
uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v); |
898 |
if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF) { |
|
4244
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
899 |
StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback); |
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
900 |
return_cmd_error(error); |
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
901 |
} |
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
902 |
|
0 | 903 |
if (flags & DC_EXEC) { |
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
904 |
if (v->IsStoppedInDepot()) { |
3139
4c950c7ec5c9
(svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents:
3005
diff
changeset
|
905 |
DeleteVehicleNews(p1, STR_981C_SHIP_IS_WAITING_IN_DEPOT); |
4c950c7ec5c9
(svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents:
3005
diff
changeset
|
906 |
} |
4c950c7ec5c9
(svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents:
3005
diff
changeset
|
907 |
|
0 | 908 |
v->vehstatus ^= VS_STOPPED; |
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
909 |
v->cur_speed = 0; |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
910 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
0 | 911 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
912 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 913 |
} |
914 |
||
9629 | 915 |
return CommandCost(); |
0 | 916 |
} |
917 |
||
10142
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
918 |
bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse) |
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
919 |
{ |
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
920 |
const Depot *depot = FindClosestShipDepot(this); |
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
921 |
|
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
922 |
if (depot == NULL) return false; |
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
923 |
|
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
924 |
if (location != NULL) *location = depot->xy; |
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
925 |
if (destination != NULL) *destination = depot->index; |
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
926 |
|
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
927 |
return true; |
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
928 |
} |
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
929 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
930 |
/** Send a ship to the depot. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3476
diff
changeset
|
931 |
* @param tile unused |
9574 | 932 |
* @param flags type of operation |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
933 |
* @param p1 vehicle ID to send to the depot |
4451
66603f0f732e
(svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents:
4412
diff
changeset
|
934 |
* @param p2 various bitmasked elements |
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
935 |
* - p2 bit 0-3 - DEPOT_ flags (see vehicle.h) |
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
936 |
* - p2 bit 8-10 - VLW flag (for mass goto depot) |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
937 |
*/ |
9629 | 938 |
CommandCost CmdSendShipToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 939 |
{ |
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
940 |
if (p2 & DEPOT_MASS_SEND) { |
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
941 |
/* Mass goto depot requested */ |
4546
9345e0569655
(svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents:
4544
diff
changeset
|
942 |
if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR; |
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
943 |
return SendAllVehiclesToDepot(VEH_SHIP, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1); |
4463
3a70624c40eb
(svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents:
4451
diff
changeset
|
944 |
} |
3a70624c40eb
(svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents:
4451
diff
changeset
|
945 |
|
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
946 |
if (!IsValidVehicleID(p1)) return CMD_ERROR; |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
947 |
|
10142
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
948 |
Vehicle *v = GetVehicle(p1); |
4510
071bca98c577
(svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents:
4506
diff
changeset
|
949 |
|
10142
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
950 |
if (v->type != VEH_SHIP) return CMD_ERROR; |
0 | 951 |
|
10142
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
952 |
return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK)); |
0 | 953 |
} |
954 |
||
955 |
||
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
956 |
/** Refits a ship to the specified cargo type. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3476
diff
changeset
|
957 |
* @param tile unused |
9574 | 958 |
* @param flags type of operation |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
959 |
* @param p1 vehicle ID of the ship to refit |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
960 |
* @param p2 various bitstuffed elements |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
961 |
* - p2 = (bit 0-7) - the new cargo type to refit to (p2 & 0xFF) |
3954
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
962 |
* - p2 = (bit 8-15) - the new cargo subtype to refit to |
9620
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
963 |
* - p2 = (bit 16) - refit only this vehicle (ignored) |
31e38d28a0af
(svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents:
9601
diff
changeset
|
964 |
* @return cost of refit or error |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
965 |
*/ |
9629 | 966 |
CommandCost CmdRefitShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 967 |
{ |
968 |
Vehicle *v; |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
969 |
CommandCost cost(EXPENSES_SHIP_RUN); |
3954
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
970 |
CargoID new_cid = GB(p2, 0, 8); //gets the cargo number |
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
971 |
byte new_subtype = GB(p2, 8, 8); |
3989
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
972 |
uint16 capacity = CALLBACK_FAILED; |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
973 |
|
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
974 |
if (!IsValidVehicleID(p1)) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
975 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
976 |
v = GetVehicle(p1); |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
977 |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
978 |
if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR; |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
979 |
if (!v->IsStoppedInDepot()) return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
980 |
if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE); |
0 | 981 |
|
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
982 |
/* Check cargo */ |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
983 |
if (!ShipVehInfo(v->engine_type)->refittable) return CMD_ERROR; |
9476 | 984 |
if (new_cid >= NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR; |
915 | 985 |
|
3989
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
986 |
/* Check the refit capacity callback */ |
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
987 |
if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) { |
3989
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
988 |
/* Back up the existing cargo type */ |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
989 |
CargoID temp_cid = v->cargo_type; |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
990 |
byte temp_subtype = v->cargo_subtype; |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
991 |
v->cargo_type = new_cid; |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
992 |
v->cargo_subtype = new_subtype; |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
993 |
|
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
994 |
capacity = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v); |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
995 |
|
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
996 |
/* Restore the cargo type */ |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
997 |
v->cargo_type = temp_cid; |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
998 |
v->cargo_subtype = temp_subtype; |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
999 |
} |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
1000 |
|
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
1001 |
if (capacity == CALLBACK_FAILED) { |
9624 | 1002 |
capacity = GetVehicleProperty(v, 0x0D, ShipVehInfo(v->engine_type)->capacity); |
3989
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
1003 |
} |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
1004 |
_returned_refit_capacity = capacity; |
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
1005 |
|
4845
49105d3b5a9a
(svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents:
4739
diff
changeset
|
1006 |
if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) { |
4544
b2d5ad597e4d
(svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents:
4529
diff
changeset
|
1007 |
cost = GetRefitCost(v->engine_type); |
0 | 1008 |
} |
1009 |
||
1010 |
if (flags & DC_EXEC) { |
|
3989
a5d50d90fd21
(svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents:
3961
diff
changeset
|
1011 |
v->cargo_cap = capacity; |
9629 | 1012 |
v->cargo.Truncate((v->cargo_type == new_cid) ? capacity : 0); |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1013 |
v->cargo_type = new_cid; |
3954
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
1014 |
v->cargo_subtype = new_subtype; |
0 | 1015 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
3954
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
1016 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
10645 | 1017 |
InvalidateWindowClassesData(WC_SHIPS_LIST, 0); |
0 | 1018 |
} |
1019 |
||
1020 |
return cost; |
|
1021 |
||
1022 |
} |