author | darkvater |
Mon, 06 Sep 2004 18:15:13 +0000 | |
changeset 164 | 0cbdf3c9bde1 |
parent 159 | 139cf78bfb28 |
child 193 | 0a7025304867 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "vehicle.h" |
|
4 |
#include "command.h" |
|
5 |
#include "pathfind.h" |
|
6 |
#include "station.h" |
|
7 |
#include "gfx.h" |
|
8 |
#include "news.h" |
|
9 |
#include "engine.h" |
|
10 |
#include "gui.h" |
|
11 |
#include "player.h" |
|
12 |
||
13 |
||
14 |
static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D}; |
|
15 |
static const byte _ship_sometracks[4] = {0x19, 0x16, 0x25, 0x2A}; |
|
16 |
||
17 |
static byte GetTileShipTrackStatus(uint tile) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
18 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); |
0 | 19 |
return r | r >> 8; |
20 |
} |
|
21 |
||
22 |
void DrawShipEngine(int x, int y, int engine, uint32 image_ormod) |
|
23 |
{ |
|
24 |
int spritenum = ship_vehicle_info(engine).image_index; |
|
25 |
||
26 |
if (is_custom_sprite(spritenum)) { |
|
27 |
int sprite = GetCustomEngineSprite(engine, 0xffff, CID_PURCHASE, 0, 0, 6); |
|
28 |
||
29 |
if (sprite) { |
|
30 |
DrawSprite(sprite | image_ormod, x, y); |
|
31 |
return; |
|
32 |
} |
|
33 |
spritenum = _engine_original_sprites[engine]; |
|
34 |
} |
|
35 |
DrawSprite((6 + _ship_sprites[spritenum]) | image_ormod, x, y); |
|
36 |
} |
|
37 |
||
38 |
void DrawShipEngineInfo(int engine, int x, int y, int maxw) |
|
39 |
{ |
|
40 |
ShipVehicleInfo *svi = &ship_vehicle_info(engine); |
|
41 |
SET_DPARAM32(0, svi->base_cost * (_price.ship_base>>3)>>5); |
|
42 |
SET_DPARAM16(1, svi->max_speed * 10 >> 5); |
|
43 |
SET_DPARAM16(2, _cargoc.names_long_p[svi->cargo_type]); |
|
44 |
SET_DPARAM16(3, svi->capacity); |
|
45 |
SET_DPARAM32(4, svi->running_cost * _price.ship_running >> 8); |
|
46 |
DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw); |
|
47 |
} |
|
48 |
||
49 |
int GetShipImage(Vehicle *v, byte direction) |
|
50 |
{ |
|
51 |
int spritenum = v->spritenum; |
|
52 |
||
53 |
if (is_custom_sprite(spritenum)) { |
|
54 |
int sprite = GetCustomVehicleSprite(v, direction); |
|
55 |
||
56 |
if (sprite) return sprite; |
|
57 |
spritenum = _engine_original_sprites[v->engine_type]; |
|
58 |
} |
|
59 |
return _ship_sprites[spritenum] + direction; |
|
60 |
} |
|
61 |
||
62 |
static int FindClosestShipDepot(Vehicle *v) |
|
63 |
{ |
|
64 |
uint tile, dist, best_dist = (uint)-1; |
|
65 |
int best_depot = -1; |
|
66 |
int i; |
|
67 |
byte owner = v->owner; |
|
68 |
uint tile2 = v->tile; |
|
69 |
||
70 |
for(i=0; i!=lengthof(_depots); i++) { |
|
71 |
tile = _depots[i].xy; |
|
72 |
if (IS_TILETYPE(tile, MP_WATER) && _map_owner[tile] == owner) { |
|
73 |
dist = GetTileDist(tile, tile2); |
|
74 |
if (dist < best_dist) { |
|
75 |
best_dist = dist; |
|
76 |
best_depot = i; |
|
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
return best_depot; |
|
82 |
} |
|
83 |
||
84 |
static void CheckIfShipNeedsService(Vehicle *v) |
|
85 |
{ |
|
86 |
int i; |
|
87 |
||
76
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
19
diff
changeset
|
88 |
if (_patches.servint_ships == 0) |
11
836bc4b37b5b
(svn r12) Change: removed patch no_train_service. Instead you can set the default service interval for any vehicle type to 'disabled'.
dominik
parents:
0
diff
changeset
|
89 |
return; |
836bc4b37b5b
(svn r12) Change: removed patch no_train_service. Instead you can set the default service interval for any vehicle type to 'disabled'.
dominik
parents:
0
diff
changeset
|
90 |
|
156
8fef5e5752d6
(svn r157) -Feature: [1009708] Percent-based service intervals. Send a vehicle to depot after it has lost X% of its reliability (mivlad)
darkvater
parents:
76
diff
changeset
|
91 |
if (SERVICE_INTERVAL) |
0 | 92 |
return; |
93 |
||
94 |
if (v->vehstatus & VS_STOPPED) |
|
95 |
return; |
|
96 |
||
97 |
if ((v->next_order & (OT_MASK | OF_FULL_LOAD)) == (OT_GOTO_DEPOT | OF_FULL_LOAD)) |
|
98 |
return; |
|
99 |
||
76
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
19
diff
changeset
|
100 |
if (_patches.gotodepot && ScheduleHasDepotOrders(v->schedule_ptr)) |
0 | 101 |
return; |
102 |
||
103 |
i = FindClosestShipDepot(v); |
|
104 |
||
105 |
if (i < 0 || GetTileDist(v->tile, (&_depots[i])->xy) > 12) { |
|
106 |
if ((v->next_order & OT_MASK) == OT_GOTO_DEPOT) { |
|
107 |
v->next_order = OT_DUMMY; |
|
108 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); |
|
109 |
} |
|
110 |
return; |
|
111 |
} |
|
112 |
||
113 |
v->next_order = OT_GOTO_DEPOT | OF_NON_STOP; |
|
114 |
v->next_order_param = (byte)i; |
|
115 |
v->dest_tile = (&_depots[i])->xy; |
|
116 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); |
|
117 |
} |
|
118 |
||
119 |
void OnNewDay_Ship(Vehicle *v) |
|
120 |
{ |
|
121 |
int32 cost; |
|
122 |
||
123 |
if ((++v->day_counter & 7) == 0) |
|
124 |
DecreaseVehicleValue(v); |
|
125 |
||
126 |
CheckVehicleBreakdown(v); |
|
127 |
AgeVehicle(v); |
|
128 |
CheckIfShipNeedsService(v); |
|
129 |
||
19
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
130 |
CheckOrders(v); |
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
131 |
|
0 | 132 |
if (v->vehstatus & VS_STOPPED) |
133 |
return; |
|
134 |
||
19
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
135 |
|
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
136 |
|
0 | 137 |
cost = ship_vehicle_info(v->engine_type).running_cost * _price.ship_running / 364; |
138 |
v->profit_this_year -= cost >> 8; |
|
139 |
||
140 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN); |
|
141 |
SubtractMoneyFromPlayerFract(v->owner, cost); |
|
142 |
||
143 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
144 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
|
145 |
} |
|
146 |
||
147 |
static void HandleBrokenShip(Vehicle *v) |
|
148 |
{ |
|
149 |
if (v->breakdown_ctr != 1) { |
|
150 |
v->breakdown_ctr = 1; |
|
151 |
v->cur_speed = 0; |
|
152 |
||
153 |
if (v->breakdowns_since_last_service != 255) |
|
154 |
v->breakdowns_since_last_service++; |
|
155 |
||
156 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
157 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
158 |
||
159 |
SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? 0xE : 0x3A, v); |
|
160 |
||
161 |
if (!(v->vehstatus & VS_HIDDEN)) { |
|
162 |
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); |
|
163 |
if (u) |
|
164 |
u->u.special.unk0 = v->breakdown_delay * 2; |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
if (!(v->tick_counter & 1)) { |
|
169 |
if (!--v->breakdown_delay) { |
|
170 |
v->breakdown_ctr = 0; |
|
171 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
static void MarkShipDirty(Vehicle *v) |
|
177 |
{ |
|
178 |
v->cur_image = GetShipImage(v, v->direction); |
|
179 |
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); |
|
180 |
} |
|
181 |
||
182 |
static void PlayShipSound(Vehicle *v) |
|
183 |
{ |
|
184 |
SndPlayVehicleFx(ship_vehicle_info(v->engine_type).sfx, v); |
|
185 |
} |
|
186 |
||
187 |
static const TileIndexDiff _dock_offs[] = { |
|
188 |
TILE_XY(2, 0), |
|
189 |
TILE_XY(-2, 0), |
|
190 |
TILE_XY(0, 2), |
|
191 |
TILE_XY(2, 0), |
|
192 |
TILE_XY(0, -2), |
|
193 |
TILE_XY(0,0), |
|
194 |
TILE_XY(0,0), |
|
195 |
TILE_XY(0,0), |
|
196 |
}; |
|
197 |
||
198 |
static void ProcessShipOrder(Vehicle *v) |
|
199 |
{ |
|
200 |
uint order; |
|
201 |
Station *st; |
|
202 |
||
203 |
if ((v->next_order & OT_MASK) >= OT_GOTO_DEPOT && (v->next_order & OT_MASK) <= OT_LEAVESTATION) { |
|
204 |
if ((v->next_order & (OT_MASK|OF_UNLOAD)) != (OT_GOTO_DEPOT|OF_UNLOAD)) |
|
205 |
return; |
|
206 |
} |
|
207 |
||
208 |
if ((v->next_order & (OT_MASK|OF_UNLOAD|OF_FULL_LOAD)) == (OT_GOTO_DEPOT|OF_UNLOAD|OF_FULL_LOAD) && |
|
156
8fef5e5752d6
(svn r157) -Feature: [1009708] Percent-based service intervals. Send a vehicle to depot after it has lost X% of its reliability (mivlad)
darkvater
parents:
76
diff
changeset
|
209 |
SERVICE_INTERVAL) { |
0 | 210 |
v->cur_order_index++; |
211 |
} |
|
212 |
||
213 |
||
214 |
if (v->cur_order_index >= v->num_orders) |
|
215 |
v->cur_order_index = 0; |
|
216 |
||
217 |
order = v->schedule_ptr[v->cur_order_index]; |
|
218 |
||
219 |
if (order == 0) { |
|
220 |
v->next_order = OT_NOTHING; |
|
221 |
v->dest_tile = 0; |
|
222 |
return; |
|
223 |
} |
|
224 |
||
225 |
if (order == (uint)((v->next_order | (v->next_order_param<<8)))) |
|
226 |
return; |
|
227 |
||
228 |
v->next_order = (byte)order; |
|
229 |
v->next_order_param = (byte)(order >> 8); |
|
230 |
||
231 |
if ((order & OT_MASK) == OT_GOTO_STATION) { |
|
232 |
if ( (byte)(order >> 8) == v->last_station_visited) |
|
233 |
v->last_station_visited = 0xFF; |
|
234 |
||
235 |
st = DEREF_STATION(order >> 8); |
|
236 |
if (st->dock_tile != 0) { |
|
237 |
v->dest_tile = TILE_ADD(st->dock_tile, _dock_offs[_map5[st->dock_tile]-0x4B]); |
|
238 |
} |
|
239 |
} else if ((order & OT_MASK) == OT_GOTO_DEPOT) { |
|
240 |
v->dest_tile = _depots[order >> 8].xy; |
|
241 |
} else { |
|
242 |
v->dest_tile = 0; |
|
243 |
} |
|
244 |
InvalidateVehicleOrderWidget(v); |
|
245 |
} |
|
246 |
||
247 |
static void HandleShipLoading(Vehicle *v) |
|
248 |
{ |
|
249 |
if (v->next_order == OT_NOTHING) |
|
250 |
return; |
|
251 |
||
252 |
if (v->next_order != OT_DUMMY) { |
|
253 |
if ((v->next_order&OT_MASK) != OT_LOADING) |
|
254 |
return; |
|
255 |
||
256 |
if (--v->load_unload_time_rem) |
|
257 |
return; |
|
258 |
||
259 |
if (v->next_order&OF_FULL_LOAD && CanFillVehicle(v)) { |
|
260 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); |
|
261 |
if (LoadUnloadVehicle(v)) { |
|
262 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
|
263 |
MarkShipDirty(v); |
|
264 |
} |
|
265 |
return; |
|
266 |
} |
|
267 |
PlayShipSound(v); |
|
268 |
||
269 |
{ |
|
270 |
byte b = v->next_order; |
|
271 |
v->next_order = OT_LEAVESTATION; |
|
272 |
if (!(b & OF_NON_STOP)) |
|
273 |
return; |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
v->cur_order_index++; |
|
278 |
InvalidateVehicleOrderWidget(v); |
|
279 |
} |
|
280 |
||
281 |
static void UpdateShipDeltaXY(Vehicle *v, int dir) |
|
282 |
{ |
|
283 |
#define MKIT(d,c,b,a) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0) |
|
284 |
static const uint32 _delta_xy_table[8] = { |
|
285 |
MKIT( -3, -3, 6, 6), |
|
286 |
MKIT(-16, -3, 32, 6), |
|
287 |
MKIT( -3, -3, 6, 6), |
|
288 |
MKIT( -3,-16, 6, 32), |
|
289 |
MKIT( -3, -3, 6, 6), |
|
290 |
MKIT(-16, -3, 32, 6), |
|
291 |
MKIT( -3, -3, 6, 6), |
|
292 |
MKIT( -3,-16, 6, 32), |
|
293 |
}; |
|
294 |
#undef MKIT |
|
295 |
uint32 x = _delta_xy_table[dir]; |
|
296 |
v->x_offs = (byte)x; |
|
297 |
v->y_offs = (byte)(x>>=8); |
|
298 |
v->sprite_width = (byte)(x>>=8); |
|
299 |
v->sprite_height = (byte)(x>>=8); |
|
300 |
} |
|
301 |
||
302 |
static void RecalcShipStuff(Vehicle *v) |
|
303 |
{ |
|
304 |
UpdateShipDeltaXY(v, v->direction); |
|
305 |
v->cur_image = GetShipImage(v, v->direction); |
|
306 |
MarkShipDirty(v); |
|
307 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
308 |
} |
|
309 |
||
310 |
static const TileIndexDiff _ship_leave_depot_offs[] = { |
|
311 |
TILE_XY(-1,0), |
|
312 |
TILE_XY(0,-1), |
|
313 |
}; |
|
314 |
||
315 |
static void CheckShipLeaveDepot(Vehicle *v) |
|
316 |
{ |
|
317 |
uint tile; |
|
318 |
int d; |
|
319 |
uint m; |
|
320 |
||
321 |
if (v->u.ship.state != 0x80) |
|
322 |
return; |
|
323 |
||
324 |
tile = v->tile; |
|
325 |
d = (_map5[tile]&2) ? 1 : 0; |
|
326 |
||
327 |
// Check first side |
|
328 |
if (_ship_sometracks[d] & GetTileShipTrackStatus(TILE_ADD(tile,_ship_leave_depot_offs[d]))) { |
|
329 |
m = (d==0) ? 0x101 : 0x207; |
|
330 |
// Check second side |
|
331 |
} else if (_ship_sometracks[d+2] & GetTileShipTrackStatus(TILE_ADD(tile, -2*_ship_leave_depot_offs[d]))) { |
|
332 |
m = (d==0) ? 0x105 : 0x203; |
|
333 |
} else { |
|
334 |
return; |
|
335 |
} |
|
336 |
v->direction = (byte)m; |
|
337 |
v->u.ship.state = (byte)(m >> 8); |
|
338 |
v->vehstatus &= ~VS_HIDDEN; |
|
339 |
||
340 |
v->cur_speed = 0; |
|
341 |
RecalcShipStuff(v); |
|
342 |
||
343 |
PlayShipSound(v); |
|
344 |
} |
|
345 |
||
346 |
static bool ShipAccelerate(Vehicle *v) |
|
347 |
{ |
|
348 |
uint spd; |
|
349 |
byte t; |
|
350 |
||
351 |
spd = min(v->cur_speed + 1, v->max_speed); |
|
352 |
||
353 |
//updates statusbar only if speed have changed to save CPU time |
|
354 |
if (spd != v->cur_speed) { |
|
355 |
v->cur_speed = spd; |
|
356 |
if (_patches.vehicle_speed) |
|
357 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); |
|
358 |
} |
|
359 |
||
360 |
// Decrease somewhat when turning |
|
361 |
if (!(v->direction&1)) { |
|
362 |
spd = spd * 3 >> 2; |
|
363 |
} |
|
364 |
||
365 |
if (spd == 0) |
|
366 |
return false; |
|
367 |
||
368 |
if ((byte)++spd == 0) |
|
369 |
return true; |
|
370 |
||
371 |
v->progress = (t = v->progress) - (byte)spd; |
|
372 |
||
373 |
return (t < v->progress); |
|
374 |
} |
|
375 |
||
376 |
||
377 |
static int32 EstimateShipCost(uint16 engine_type); |
|
378 |
||
379 |
static void ShipEnterDepot(Vehicle *v) |
|
380 |
{ |
|
381 |
byte t; |
|
382 |
||
383 |
v->u.ship.state = 0x80; |
|
384 |
v->vehstatus |= VS_HIDDEN; |
|
385 |
v->cur_speed = 0; |
|
386 |
RecalcShipStuff(v); |
|
387 |
||
388 |
v->date_of_last_service = _date; |
|
389 |
v->breakdowns_since_last_service = 0; |
|
390 |
v->reliability = _engines[v->engine_type].reliability; |
|
391 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
392 |
||
393 |
MaybeRenewVehicle(v, EstimateShipCost(v->engine_type)); |
|
394 |
||
395 |
if ((v->next_order&OT_MASK) == OT_GOTO_DEPOT) { |
|
396 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
397 |
||
398 |
t = v->next_order; |
|
399 |
v->next_order = OT_DUMMY; |
|
400 |
||
401 |
if (t&OF_UNLOAD) { v->cur_order_index++; } |
|
402 |
||
403 |
else if (t & 0x40) { |
|
404 |
v->vehstatus |= VS_STOPPED; |
|
405 |
if (v->owner == _local_player) { |
|
406 |
SET_DPARAM16(0, v->unitnumber); |
|
407 |
AddNewsItem( |
|
408 |
STR_981C_SHIP_IS_WAITING_IN_DEPOT, |
|
409 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
410 |
v->index, |
|
411 |
0); |
|
412 |
} |
|
413 |
} |
|
414 |
} |
|
415 |
} |
|
416 |
||
417 |
static void ShipArrivesAt(Vehicle *v, Station *st) |
|
418 |
{ |
|
419 |
/* Check if station was ever visited before */ |
|
420 |
if (!(st->had_vehicle_of_type & HVOT_SHIP)) { |
|
421 |
uint32 flags; |
|
422 |
st->had_vehicle_of_type |= HVOT_SHIP; |
|
423 |
SET_DPARAM16(0, st->index); |
|
424 |
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0); |
|
425 |
AddNewsItem( |
|
426 |
STR_9833_CITIZENS_CELEBRATE_FIRST, |
|
427 |
flags, |
|
428 |
v->index, |
|
429 |
0); |
|
430 |
} |
|
431 |
} |
|
432 |
||
433 |
typedef struct { |
|
434 |
uint skiptile; |
|
435 |
uint dest_coords; |
|
436 |
uint best_bird_dist; |
|
437 |
uint best_length; |
|
438 |
} PathFindShip; |
|
439 |
||
440 |
//extern void dbg_store_path(); |
|
441 |
||
442 |
static bool ShipTrackFollower(uint tile, PathFindShip *pfs, int track, uint length, byte *state) |
|
443 |
{ |
|
444 |
// Found dest? |
|
445 |
if (tile == pfs->dest_coords) { |
|
446 |
pfs->best_bird_dist = 0; |
|
447 |
||
448 |
// if (length < pfs->best_length) |
|
449 |
// dbg_store_path(); |
|
450 |
||
451 |
pfs->best_length = minu(pfs->best_length, length); |
|
452 |
return true; |
|
453 |
} |
|
454 |
||
455 |
// Skip this tile in the calculation |
|
456 |
if (tile != pfs->skiptile) { |
|
457 |
pfs->best_bird_dist = minu(pfs->best_bird_dist, GetTileDist1Db(pfs->dest_coords, tile)); |
|
458 |
} |
|
459 |
||
460 |
return false; |
|
461 |
} |
|
462 |
||
463 |
static const byte _ship_search_directions[6][4] = { |
|
464 |
{ 0, 9, 2, 9 }, |
|
465 |
{ 9, 1, 9, 3 }, |
|
466 |
{ 9, 0, 3, 9 }, |
|
467 |
{ 1, 9, 9, 2 }, |
|
468 |
{ 3, 2, 9, 9 }, |
|
469 |
{ 9, 9, 1, 0 }, |
|
470 |
}; |
|
471 |
||
472 |
static const byte _pick_shiptrack_table[6] = {1, 3, 2, 2, 0, 0}; |
|
473 |
||
474 |
static uint FindShipTrack(Vehicle *v, uint tile, int dir, uint bits, uint skiptile, int *track) |
|
475 |
{ |
|
476 |
PathFindShip pfs; |
|
477 |
int i, best_track; |
|
478 |
uint best_bird_dist = 0; |
|
479 |
uint best_length = 0; |
|
480 |
uint r; |
|
481 |
byte ship_dir = v->direction & 3; |
|
482 |
||
483 |
pfs.dest_coords = v->dest_tile; |
|
484 |
pfs.skiptile = skiptile; |
|
485 |
||
486 |
best_track = -1; |
|
487 |
||
488 |
do { |
|
489 |
i = FIND_FIRST_BIT(bits); |
|
490 |
bits = KILL_FIRST_BIT(bits); |
|
491 |
||
492 |
pfs.best_bird_dist = (uint)-1; |
|
493 |
pfs.best_length = (uint)-1; |
|
494 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
495 |
FollowTrack(tile, 0x3800 | TRANSPORT_WATER, _ship_search_directions[i][dir], (TPFEnumProc*)ShipTrackFollower, NULL, &pfs); |
0 | 496 |
|
497 |
if (best_track >= 0) { |
|
498 |
if (pfs.best_bird_dist != 0) { |
|
499 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
|
500 |
if (pfs.best_bird_dist > best_bird_dist) goto bad; |
|
501 |
if (pfs.best_bird_dist < best_bird_dist) goto good; |
|
502 |
} else { |
|
503 |
if (pfs.best_length > best_length) goto bad; |
|
504 |
if (pfs.best_length < best_length) goto good; |
|
505 |
} |
|
506 |
||
507 |
/* if we reach this position, there's two paths of equal value so far. |
|
508 |
* pick one randomly. */ |
|
509 |
r = (byte)Random(); |
|
510 |
if (_pick_shiptrack_table[i] == ship_dir) r += 80; |
|
511 |
if (_pick_shiptrack_table[best_track] == ship_dir) r -= 80; |
|
512 |
if (r <= 127) goto bad; |
|
513 |
} |
|
514 |
good:; |
|
515 |
best_track = i; |
|
516 |
best_bird_dist = pfs.best_bird_dist; |
|
517 |
best_length = pfs.best_length; |
|
518 |
bad:; |
|
519 |
||
520 |
} while (bits != 0); |
|
521 |
||
522 |
*track = best_track; |
|
523 |
return best_bird_dist; |
|
524 |
} |
|
525 |
||
526 |
static int ChooseShipTrack(Vehicle *v, uint tile, int dir, uint tracks) |
|
527 |
{ |
|
528 |
uint b; |
|
529 |
uint tot_dist, dist; |
|
530 |
int track; |
|
531 |
uint tile2; |
|
532 |
||
533 |
assert(dir>=0 && dir<=3); |
|
534 |
tile2 = TILE_ADD(tile, -_tileoffs_by_dir[dir]); |
|
535 |
dir ^= 2; |
|
536 |
tot_dist = (uint)-1; |
|
537 |
b = GetTileShipTrackStatus(tile2) & _ship_sometracks[dir] & v->u.ship.state; |
|
538 |
if (b != 0) { |
|
539 |
dist = FindShipTrack(v, tile2, dir, b, tile, &track); |
|
540 |
if (dist != (uint)-1) |
|
541 |
tot_dist = dist + 1; |
|
542 |
} |
|
543 |
dist = FindShipTrack(v, tile, dir^2, tracks, 0, &track); |
|
544 |
if (dist > tot_dist) |
|
545 |
return -1; |
|
546 |
return track; |
|
547 |
} |
|
548 |
||
549 |
static const byte _new_vehicle_direction_table[11] = { |
|
550 |
0, 7, 6, 0, |
|
551 |
1, 0, 5, 0, |
|
552 |
2, 3, 4, |
|
553 |
}; |
|
554 |
||
555 |
static int ShipGetNewDirectionFromTiles(uint new_tile, uint old_tile) |
|
556 |
{ |
|
557 |
uint offs = (GET_TILE_Y(new_tile) - GET_TILE_Y(old_tile) + 1) * 4 + |
|
558 |
GET_TILE_X(new_tile) - GET_TILE_X(old_tile) + 1; |
|
559 |
assert(offs < 11 && offs != 3 && offs != 7); |
|
560 |
return _new_vehicle_direction_table[offs]; |
|
561 |
} |
|
562 |
||
563 |
static int ShipGetNewDirection(Vehicle *v, int x, int y) |
|
564 |
{ |
|
565 |
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); |
|
566 |
assert(offs < 11 && offs != 3 && offs != 7); |
|
567 |
return _new_vehicle_direction_table[offs]; |
|
568 |
} |
|
569 |
||
570 |
static int GetAvailShipTracks(uint tile, int dir) |
|
571 |
{ |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
572 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); |
0 | 573 |
return (byte) ((r | r >> 8)) & _ship_sometracks[dir]; |
574 |
} |
|
575 |
||
576 |
static const byte _ship_subcoord[4][6][3] = { |
|
577 |
{ |
|
578 |
{15, 8, 1}, |
|
579 |
{ 0, 0, 0}, |
|
580 |
{ 0, 0, 0}, |
|
581 |
{15, 8, 2}, |
|
582 |
{15, 7, 0}, |
|
583 |
{ 0, 0, 0}, |
|
584 |
}, |
|
585 |
{ |
|
586 |
{ 0, 0, 0}, |
|
587 |
{ 8, 0, 3}, |
|
588 |
{ 7, 0, 2}, |
|
589 |
{ 0, 0, 0}, |
|
590 |
{ 8, 0, 4}, |
|
591 |
{ 0, 0, 0}, |
|
592 |
}, |
|
593 |
{ |
|
594 |
{ 0, 8, 5}, |
|
595 |
{ 0, 0, 0}, |
|
596 |
{ 0, 7, 6}, |
|
597 |
{ 0, 0, 0}, |
|
598 |
{ 0, 0, 0}, |
|
599 |
{ 0, 8, 4}, |
|
600 |
}, |
|
601 |
{ |
|
602 |
{ 0, 0, 0}, |
|
603 |
{ 8,15, 7}, |
|
604 |
{ 0, 0, 0}, |
|
605 |
{ 8,15, 6}, |
|
606 |
{ 0, 0, 0}, |
|
607 |
{ 7,15, 0}, |
|
608 |
} |
|
609 |
}; |
|
610 |
||
611 |
||
612 |
static void ShipController(Vehicle *v) |
|
613 |
{ |
|
614 |
GetNewVehiclePosResult gp; |
|
615 |
uint32 r; |
|
616 |
const byte *b; |
|
617 |
int dir,track,tracks; |
|
618 |
||
619 |
v->tick_counter++; |
|
620 |
||
621 |
if (v->breakdown_ctr != 0) { |
|
622 |
if (v->breakdown_ctr <= 2) { |
|
623 |
HandleBrokenShip(v); |
|
624 |
return; |
|
625 |
} |
|
626 |
v->breakdown_ctr--; |
|
627 |
} |
|
628 |
||
629 |
if (v->vehstatus & VS_STOPPED) |
|
630 |
return; |
|
631 |
||
632 |
ProcessShipOrder(v); |
|
633 |
HandleShipLoading(v); |
|
634 |
||
635 |
if ((v->next_order & OT_MASK) == OT_LOADING) |
|
636 |
return; |
|
637 |
||
638 |
CheckShipLeaveDepot(v); |
|
639 |
||
640 |
if (!ShipAccelerate(v)) |
|
641 |
return; |
|
642 |
||
643 |
BeginVehicleMove(v); |
|
644 |
||
645 |
if (GetNewVehiclePos(v, &gp)) { |
|
646 |
// staying in tile |
|
647 |
if (v->u.ship.state == 0x80) { |
|
648 |
gp.x = v->x_pos; |
|
649 |
gp.y = v->y_pos; |
|
650 |
} else { |
|
651 |
/* isnot inside depot */ |
|
652 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
653 |
if (r & 0x8) goto reverse_direction; |
|
654 |
||
655 |
if (v->dest_tile != 0 && v->dest_tile == gp.new_tile) { |
|
656 |
if ((v->next_order & OT_MASK) == OT_GOTO_DEPOT) { |
|
657 |
if ((gp.x&0xF)==8 && (gp.y&0xF)==8) { |
|
658 |
ShipEnterDepot(v); |
|
659 |
return; |
|
660 |
} |
|
661 |
} else if ((v->next_order & OT_MASK) == OT_GOTO_STATION) { |
|
662 |
Station *st; |
|
663 |
||
664 |
v->last_station_visited = v->next_order_param; |
|
665 |
||
666 |
st = DEREF_STATION(v->next_order_param); |
|
667 |
if (!(st->had_vehicle_of_type & HVOT_BUOY)) { |
|
668 |
v->next_order = (v->next_order & (OF_FULL_LOAD|OF_UNLOAD)) | OF_NON_STOP | OT_LOADING; |
|
669 |
ShipArrivesAt(v, st); |
|
670 |
||
671 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); |
|
672 |
if (LoadUnloadVehicle(v)) { |
|
673 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
|
674 |
MarkShipDirty(v); |
|
675 |
} |
|
676 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); |
|
677 |
} else { |
|
678 |
v->next_order = OT_LEAVESTATION; |
|
679 |
v->cur_order_index++; |
|
680 |
InvalidateVehicleOrderWidget(v); |
|
681 |
} |
|
682 |
goto else_end; |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
if (v->next_order == OT_LEAVESTATION) { |
|
687 |
v->next_order = OT_NOTHING; |
|
688 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); |
|
689 |
} |
|
690 |
} |
|
691 |
} else { |
|
692 |
// new tile |
|
693 |
if (GET_TILE_X(gp.new_tile) == 0xFF || |
|
694 |
(byte)GET_TILE_Y(gp.new_tile) == 0xFF) |
|
695 |
goto reverse_direction; |
|
696 |
||
697 |
dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile); |
|
698 |
assert(dir == 1 || dir == 3 || dir == 5 || dir == 7); |
|
699 |
dir>>=1; |
|
700 |
tracks = GetAvailShipTracks(gp.new_tile, dir); |
|
701 |
if (tracks == 0) |
|
702 |
goto reverse_direction; |
|
703 |
||
704 |
// Choose a direction, and continue if we find one |
|
705 |
track = ChooseShipTrack(v, gp.new_tile, dir, tracks); |
|
706 |
if (track < 0) |
|
707 |
goto reverse_direction; |
|
708 |
||
709 |
b = _ship_subcoord[dir][track]; |
|
710 |
||
711 |
gp.x = (gp.x&~0xF) | b[0]; |
|
712 |
gp.y = (gp.y&~0xF) | b[1]; |
|
713 |
||
714 |
/* Call the landscape function and tell it that the vehicle entered the tile */ |
|
715 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
716 |
if (r&0x8) goto reverse_direction; |
|
717 |
||
718 |
if (!(r&0x4)) { |
|
719 |
v->tile = gp.new_tile; |
|
720 |
v->u.ship.state = 1 << track; |
|
721 |
} |
|
722 |
||
723 |
v->direction = b[2]; |
|
724 |
} |
|
725 |
else_end:; |
|
726 |
||
727 |
/* update image of ship, as well as delta XY */ |
|
728 |
dir = ShipGetNewDirection(v, gp.x, gp.y); |
|
729 |
v->x_pos = gp.x; |
|
730 |
v->y_pos = gp.y; |
|
731 |
v->z_pos = GetSlopeZ(gp.x, gp.y); |
|
732 |
||
733 |
getout: |
|
734 |
UpdateShipDeltaXY(v, dir); |
|
735 |
v->cur_image = GetShipImage(v, dir); |
|
736 |
VehiclePositionChanged(v); |
|
737 |
EndVehicleMove(v); |
|
738 |
return; |
|
739 |
||
740 |
reverse_direction: |
|
741 |
dir = v->direction ^ 4; |
|
742 |
v->direction = dir; |
|
743 |
goto getout; |
|
744 |
} |
|
745 |
||
746 |
static void AgeShipCargo(Vehicle *v) |
|
747 |
{ |
|
748 |
if (_age_cargo_skip_counter != 0) |
|
749 |
return; |
|
750 |
if (v->cargo_days != 255) |
|
751 |
v->cargo_days++; |
|
752 |
} |
|
753 |
||
754 |
void Ship_Tick(Vehicle *v) |
|
755 |
{ |
|
756 |
AgeShipCargo(v); |
|
757 |
ShipController(v); |
|
758 |
} |
|
759 |
||
760 |
void OnTick_Ship() |
|
761 |
{ |
|
762 |
// unused |
|
763 |
} |
|
764 |
||
765 |
||
766 |
void HandleClickOnShip(Vehicle *v) |
|
767 |
{ |
|
768 |
ShowShipViewWindow(v); |
|
769 |
} |
|
770 |
||
771 |
void ShipsYearlyLoop() |
|
772 |
{ |
|
773 |
Vehicle *v; |
|
774 |
||
775 |
FOR_ALL_VEHICLES(v) { |
|
776 |
if (v->type == VEH_Ship) { |
|
777 |
v->profit_last_year = v->profit_this_year; |
|
778 |
v->profit_this_year = 0; |
|
779 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
780 |
} |
|
781 |
} |
|
782 |
} |
|
783 |
||
784 |
static int32 EstimateShipCost(uint16 engine_type) |
|
785 |
{ |
|
786 |
return ship_vehicle_info(engine_type).base_cost * (_price.ship_base>>3)>>5; |
|
787 |
} |
|
788 |
||
789 |
// p1 = type to build |
|
790 |
int32 CmdBuildShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
791 |
{ |
|
792 |
int32 value; |
|
793 |
Vehicle *v; |
|
794 |
uint unit_num; |
|
795 |
uint tile = TILE_FROM_XY(x,y); |
|
796 |
Engine *e; |
|
797 |
||
798 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
799 |
||
800 |
value = EstimateShipCost(p1); |
|
801 |
if (flags & DC_QUERY_COST) |
|
802 |
return value; |
|
803 |
||
804 |
v = AllocateVehicle(); |
|
805 |
if (v == NULL || _ptr_to_next_order >= endof(_order_array) || |
|
806 |
(unit_num = GetFreeUnitNumber(VEH_Ship)) > _patches.max_ships) |
|
807 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
|
808 |
||
809 |
if (flags & DC_EXEC) { |
|
810 |
v->unitnumber = unit_num; |
|
811 |
||
812 |
v->owner = _current_player; |
|
813 |
v->tile = tile; |
|
814 |
x = GET_TILE_X(tile)*16 + 8; |
|
815 |
y = GET_TILE_Y(tile)*16 + 8; |
|
816 |
v->x_pos = x; |
|
817 |
v->y_pos = y; |
|
818 |
v->z_pos = GetSlopeZ(x,y); |
|
819 |
||
820 |
v->z_height = 6; |
|
821 |
v->sprite_width = 6; |
|
822 |
v->sprite_height = 6; |
|
823 |
v->x_offs = -3; |
|
824 |
v->y_offs = -3; |
|
825 |
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; |
|
826 |
||
827 |
v->spritenum = ship_vehicle_info(p1).image_index; |
|
828 |
v->cargo_type = ship_vehicle_info(p1).cargo_type; |
|
829 |
v->cargo_cap = ship_vehicle_info(p1).capacity; |
|
830 |
v->value = value; |
|
831 |
||
832 |
v->last_station_visited = 255; |
|
833 |
v->max_speed = ship_vehicle_info(p1).max_speed; |
|
834 |
v->engine_type = (byte)p1; |
|
835 |
||
836 |
e = &_engines[p1]; |
|
837 |
v->reliability = e->reliability; |
|
838 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
839 |
v->max_age = e->lifelength * 366; |
|
840 |
_new_ship_id = v->index; |
|
841 |
||
842 |
v->string_id = STR_SV_SHIP_NAME; |
|
843 |
v->u.ship.state = 0x80; |
|
844 |
*(v->schedule_ptr = _ptr_to_next_order++) = 0; |
|
845 |
||
846 |
v->service_interval = _patches.servint_ships; |
|
847 |
v->date_of_last_service = _date; |
|
848 |
v->build_year = _cur_year; |
|
849 |
v->cur_image = 0x0E5E; |
|
850 |
v->type = VEH_Ship; |
|
851 |
||
852 |
VehiclePositionChanged(v); |
|
853 |
||
854 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
164
0cbdf3c9bde1
(svn r165) -Feature: Option to sort vehicles in vehicle-list window by different criteria. Total independent sort for all types and players. Periodic resort of list every 10 TTD days. Thank you for your graphical inspiration follow and buxo (since none of you provided any code).
darkvater
parents:
159
diff
changeset
|
855 |
_vehicle_sort_dirty[VEHSHIP] = true; // build a ship |
0 | 856 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
857 |
InvalidateWindow(WC_COMPANY, v->owner); |
|
858 |
} |
|
859 |
||
860 |
return value; |
|
861 |
} |
|
862 |
||
863 |
int32 CmdSellShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
864 |
{ |
|
865 |
Vehicle *v; |
|
866 |
||
867 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
868 |
||
869 |
v = &_vehicles[p1]; |
|
870 |
||
871 |
if (!CheckOwnership(v->owner)) |
|
872 |
return CMD_ERROR; |
|
873 |
||
874 |
if (!IsShipDepotTile(v->tile) || v->u.road.state != 0x80 || !(v->vehstatus&VS_STOPPED)) |
|
875 |
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); |
|
876 |
||
877 |
if (flags & DC_EXEC) { |
|
878 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
164
0cbdf3c9bde1
(svn r165) -Feature: Option to sort vehicles in vehicle-list window by different criteria. Total independent sort for all types and players. Periodic resort of list every 10 TTD days. Thank you for your graphical inspiration follow and buxo (since none of you provided any code).
darkvater
parents:
159
diff
changeset
|
879 |
_vehicle_sort_dirty[VEHSHIP] = true; // sell a ship |
0 | 880 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
881 |
InvalidateWindow(WC_COMPANY, v->owner); |
|
882 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
883 |
DeleteVehicle(v); |
|
884 |
} |
|
885 |
||
886 |
return -(int32)v->value; |
|
887 |
} |
|
888 |
||
889 |
// p1 = vehicle |
|
890 |
int32 CmdStartStopShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
891 |
{ |
|
892 |
Vehicle *v; |
|
893 |
||
894 |
v = &_vehicles[p1]; |
|
895 |
||
896 |
if (!CheckOwnership(v->owner)) |
|
897 |
return CMD_ERROR; |
|
898 |
||
899 |
if (flags & DC_EXEC) { |
|
900 |
v->vehstatus ^= VS_STOPPED; |
|
901 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); |
|
902 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
903 |
} |
|
904 |
||
905 |
return 0; |
|
906 |
} |
|
907 |
||
908 |
int32 CmdSendShipToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
909 |
{ |
|
910 |
Vehicle *v; |
|
911 |
int depot; |
|
912 |
||
913 |
v = &_vehicles[p1]; |
|
914 |
||
915 |
if (!CheckOwnership(v->owner)) |
|
916 |
return CMD_ERROR; |
|
917 |
||
918 |
if ((v->next_order&OT_MASK) == OT_GOTO_DEPOT) { |
|
919 |
if (flags & DC_EXEC) { |
|
920 |
if (v->next_order&OF_UNLOAD) {v->cur_order_index++;} |
|
921 |
v->next_order = OT_DUMMY; |
|
922 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); |
|
923 |
} |
|
924 |
} else { |
|
925 |
depot = FindClosestShipDepot(v); |
|
926 |
if (depot < 0) |
|
927 |
return_cmd_error(STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT); |
|
928 |
||
929 |
if (flags & DC_EXEC) { |
|
930 |
v->dest_tile = _depots[depot].xy; |
|
931 |
v->next_order = OF_NON_STOP | OF_FULL_LOAD | OT_GOTO_DEPOT; |
|
932 |
v->next_order_param = depot; |
|
933 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); |
|
934 |
} |
|
935 |
} |
|
936 |
||
937 |
return 0; |
|
938 |
} |
|
939 |
||
940 |
int32 CmdChangeShipServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
941 |
{ |
|
942 |
Vehicle *v; |
|
943 |
||
944 |
v = &_vehicles[p1]; |
|
945 |
||
946 |
if (!CheckOwnership(v->owner)) |
|
947 |
return CMD_ERROR; |
|
948 |
||
949 |
if (flags & DC_EXEC) { |
|
950 |
v->service_interval = (uint16)p2; |
|
951 |
InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7); |
|
952 |
} |
|
953 |
||
954 |
return 0; |
|
955 |
} |
|
956 |
||
957 |
||
958 |
// p1 = vehicle |
|
959 |
// p2 = new cargo |
|
960 |
int32 CmdRefitShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
961 |
{ |
|
962 |
Vehicle *v; |
|
963 |
int32 cost; |
|
964 |
||
965 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN); |
|
966 |
||
967 |
v = &_vehicles[p1]; |
|
968 |
if (!CheckOwnership(v->owner)) |
|
969 |
return CMD_ERROR; |
|
970 |
||
971 |
if (!IsShipDepotTile(v->tile) || |
|
972 |
!(v->vehstatus&VS_STOPPED) || |
|
973 |
v->u.ship.state != 0x80) |
|
974 |
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); |
|
975 |
||
976 |
cost = 0; |
|
977 |
if (IS_HUMAN_PLAYER(v->owner) && (byte)p2 != v->cargo_type) { |
|
978 |
cost = _price.ship_base >> 7; |
|
979 |
} |
|
980 |
||
981 |
if (flags & DC_EXEC) { |
|
982 |
v->cargo_count = 0; |
|
983 |
v->cargo_type = (byte)p2; |
|
984 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
985 |
} |
|
986 |
||
987 |
return cost; |
|
988 |
||
989 |
} |
|
990 |
||
991 |