truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" tron@507: #include "table/strings.h" tron@679: #include "map.h" tron@1209: #include "tile.h" truelight@0: #include "vehicle.h" truelight@0: #include "command.h" truelight@0: #include "pathfind.h" matthijs@1247: #include "npf.h" truelight@0: #include "station.h" truelight@0: #include "table/train_cmd.h" truelight@0: #include "gfx.h" truelight@0: #include "news.h" truelight@0: #include "engine.h" truelight@0: #include "player.h" tron@337: #include "sound.h" truelight@1313: #include "depot.h" truelight@0: truelight@0: #define is_firsthead_sprite(spritenum) \ truelight@0: (is_custom_sprite(spritenum) \ truelight@0: ? is_custom_firsthead_sprite(spritenum) \ truelight@0: : _engine_sprite_add[spritenum] == 0) truelight@0: truelight@742: static bool TrainCheckIfLineEnds(Vehicle *v); truelight@0: truelight@0: static const byte _vehicle_initial_x_fract[4] = {10,8,4,8}; truelight@0: static const byte _vehicle_initial_y_fract[4] = {8,4,8,10}; truelight@0: static const byte _state_dir_table[4] = { 0x20, 8, 0x10, 4 }; truelight@0: matthijs@1247: matthijs@1247: /* These two arrays are used for realistic acceleration. XXX: How should they matthijs@1247: * be interpreted? */ celestar@1179: static const byte _curve_neighbours45[8][2] = { celestar@1179: {7, 1}, celestar@1179: {0, 2}, celestar@1179: {1, 3}, celestar@1179: {2, 4}, celestar@1179: {3, 5}, celestar@1179: {4, 6}, celestar@1179: {5, 7}, celestar@1179: {6, 0}, celestar@1179: }; celestar@1179: celestar@1179: static const byte _curve_neighbours90[8][2] = { celestar@1179: {6, 2}, celestar@1179: {7, 3}, celestar@1179: {0, 4}, celestar@1179: {1, 5}, celestar@1179: {2, 6}, celestar@1179: {3, 7}, celestar@1179: {4, 0}, celestar@1179: {5, 1}, celestar@1179: }; celestar@1179: celestar@1179: enum AccelType { celestar@1179: AM_ACCEL, celestar@1179: AM_BRAKE celestar@1179: }; celestar@1179: celestar@1236: static bool TrainShouldStop(Vehicle *v, TileIndex tile) celestar@1236: { celestar@1236: Order *o = &v->current_order; celestar@1236: assert(v->type == VEH_Train); celestar@1236: assert(IsTileType(v->tile, MP_STATION)); celestar@1236: //When does a train drive through a station celestar@1236: //first we deal with the "new nonstop handling" celestar@1236: if ( _patches.new_nonstop && o->flags & OF_NON_STOP && _map2[tile] == o->station ) celestar@1236: return false; celestar@1236: celestar@1236: if (v->last_station_visited == _map2[tile]) celestar@1236: return false; celestar@1236: celestar@1236: if ( _map2[tile] != o->station && (o->flags & OF_NON_STOP || _patches.new_nonstop)) celestar@1236: return false; celestar@1236: celestar@1236: return true; celestar@1236: } celestar@1236: celestar@1179: //new acceleration celestar@1179: static int GetTrainAcceleration(Vehicle *v, bool mode) celestar@1179: { celestar@1179: Vehicle *u = v; celestar@1179: int num = 0; //number of vehicles, change this into the number of axles later celestar@1179: int power = 0; celestar@1179: int mass = 0; celestar@1179: int max_speed = 2000; celestar@1179: int area = 120; celestar@1179: int friction = 35; //[1e-3] celestar@1179: int drag_coeff = 20; //[1e-4] celestar@1179: int incl = 0; celestar@1179: int resistance; celestar@1179: int speed = v->cur_speed; //[mph] celestar@1179: int force = 0x3FFFFFFF; celestar@1179: int pos = 0; celestar@1179: int lastpos = -1; celestar@1179: int curvecount[2] = {0, 0}; celestar@1179: int *dist = NULL; celestar@1179: int sum = 0; celestar@1179: int numcurve = 0; celestar@1179: int i; celestar@1179: celestar@1179: speed *= 10; celestar@1179: speed /= 16; celestar@1179: celestar@1179: //first find the curve speed limit celestar@1179: for (; u->next != NULL; u = u->next, pos++) { celestar@1179: int dir = u->direction; celestar@1179: int ndir = u->next->direction; celestar@1179: celestar@1179: for (i = 0; i < 2; i++) { celestar@1179: if ( _curve_neighbours45[dir][i] == ndir) { celestar@1179: curvecount[i]++; celestar@1179: if (lastpos != -1) { celestar@1179: dist = realloc(dist, sizeof(int) * ++numcurve); celestar@1179: dist[numcurve - 1] = pos - lastpos; celestar@1179: if (pos - lastpos == 1) { celestar@1179: max_speed = 88; celestar@1179: } celestar@1179: } celestar@1179: lastpos = pos; celestar@1179: } celestar@1179: } celestar@1179: celestar@1179: //if we have a 90 degree turn, fix the speed limit to 60 celestar@1179: if ( _curve_neighbours90[dir][0] == ndir || _curve_neighbours90[dir][1] == ndir) { celestar@1179: max_speed = 61; celestar@1179: } celestar@1179: } celestar@1179: celestar@1179: for(i = 0; i < numcurve; i++) { celestar@1179: sum += dist[i]; celestar@1179: } celestar@1179: celestar@1179: if (numcurve > 0) { celestar@1179: sum /= numcurve; celestar@1179: } celestar@1179: celestar@1179: if ((curvecount[0] != 0 || curvecount[1] != 0) && (max_speed > 88)) { celestar@1179: int total = curvecount[0] + curvecount[1]; celestar@1179: if (curvecount[0] == 1 && curvecount[1] == 1) { celestar@1179: max_speed = 0xFFFF; celestar@1179: } else if (total > 1) { celestar@1179: max_speed = 232 - (13 - clamp(sum, 1, 12)) * (13 - clamp(sum, 1, 12)); celestar@1179: } celestar@1179: } celestar@1179: celestar@1179: max_speed += (max_speed / 2) * v->u.rail.railtype; celestar@1179: celestar@1179: if (IsTileType(v->tile, MP_STATION) && v->subtype == TS_Front_Engine) { celestar@1179: static const TileIndexDiffC _station_dir_from_vdir[] = { celestar@1179: {0, 0}, {-1, 0}, {0, 0}, {0, 1}, {0, 0}, {1, 0}, {0, 0}, {0, -1} celestar@1179: }; celestar@1179: celestar@1236: if (TrainShouldStop(v, v->tile)) { celestar@1179: int station_length = 0; celestar@1179: TileIndex tile = v->tile; celestar@1179: int delta_v; celestar@1179: celestar@1179: max_speed = 120; celestar@1179: do { celestar@1179: station_length++; celestar@1179: tile = TILE_ADD(tile, ToTileIndexDiff(_station_dir_from_vdir[v->direction])); celestar@1179: } while (IsTileType(tile, MP_STATION)); celestar@1179: celestar@1179: delta_v = v->cur_speed / (station_length + 1); celestar@1179: if (v->max_speed > (v->cur_speed - delta_v)) celestar@1179: max_speed = v->cur_speed - (delta_v / 10); celestar@1179: celestar@1179: max_speed = max(max_speed, 25 * station_length); celestar@1179: } celestar@1179: } celestar@1179: celestar@1179: for (u = v; u != NULL; u = u->next) { celestar@1179: const RailVehicleInfo *rvi = RailVehInfo(u->engine_type); celestar@1179: int vmass; celestar@1179: celestar@1179: num++; celestar@1179: power += rvi->power * 746; //[W] celestar@1179: drag_coeff += 3; celestar@1179: celestar@1179: if (rvi->max_speed != 0) celestar@1179: max_speed = min(rvi->max_speed, max_speed); celestar@1179: celestar@1179: if (u->u.rail.track == 0x80) celestar@1179: max_speed = 61; celestar@1179: celestar@1179: vmass = rvi->weight; //[t] celestar@1179: vmass += (_cargoc.weights[u->cargo_type] * u->cargo_count) / 16; celestar@1179: mass += vmass; //[t] celestar@1179: celestar@1179: if (!IsTileType(u->tile, MP_TUNNELBRIDGE)) { celestar@1179: if (HASBIT(u->u.rail.flags, VRF_GOINGUP)) { celestar@1179: incl += vmass * 60; //3% slope, quite a bit actually celestar@1179: } else if (HASBIT(u->u.rail.flags, VRF_GOINGDOWN)) { celestar@1179: incl -= vmass * 60; celestar@1179: } celestar@1179: } celestar@1179: } celestar@1179: celestar@1179: celestar@1179: // these are shown in the UI celestar@1179: v->u.rail.cached_weight = mass; celestar@1179: v->u.rail.cached_power = power / 746; celestar@1179: v->max_speed = max_speed; celestar@1179: celestar@1179: celestar@1179: if (v->u.rail.railtype != 2) { celestar@1179: resistance = 13 * mass / 10; celestar@1179: resistance += 60 * num; celestar@1179: resistance += friction * mass * speed / 1000; celestar@1179: resistance += (area * drag_coeff * speed * speed) / 10000; celestar@1179: } else celestar@1179: resistance = (area * (drag_coeff / 2) * speed * speed) / 10000; celestar@1179: resistance += incl; celestar@1179: resistance *= 4; //[N] celestar@1179: celestar@1179: if (speed > 0) { celestar@1179: switch (v->u.rail.railtype) { celestar@1179: case 0: celestar@1179: case 1: celestar@1179: { celestar@1179: force = power / speed; //[N] celestar@1179: force *= 22; celestar@1179: force /= 10; celestar@1179: } break; celestar@1179: case 2: celestar@1179: force = power / 25; celestar@1179: break; celestar@1179: } celestar@1179: } else celestar@1179: //"kickoff" acceleration celestar@1179: force = resistance * 10; celestar@1179: celestar@1179: if (force <= 0) force = 10000; celestar@1179: celestar@1179: if (v->u.rail.railtype != 2) celestar@1179: force = min(force, mass * 10 * 200); celestar@1179: celestar@1179: if (mode == AM_ACCEL) { celestar@1179: return (force - resistance) / (mass * 4); celestar@1179: } else { celestar@1179: return min((-force - resistance) /(mass * 4), (10000 / (mass * 4))); celestar@1179: } celestar@1179: } celestar@1179: truelight@0: void UpdateTrainAcceleration(Vehicle *v) truelight@0: { truelight@0: uint acc, power=0, max_speed=5000, weight=0; truelight@0: Vehicle *u = v; truelight@0: bjarni@1067: assert(v->subtype == TS_Front_Engine); truelight@0: truelight@0: // compute stuff like max speed, power, and weight. truelight@0: do { tron@540: const RailVehicleInfo *rvi = RailVehInfo(u->engine_type); truelight@193: truelight@0: // power is sum of the power for all engines truelight@0: power += rvi->power; truelight@0: truelight@0: // limit the max speed to the speed of the slowest vehicle. truelight@0: if (rvi->max_speed && rvi->max_speed <= max_speed) max_speed = rvi->max_speed; truelight@0: truelight@0: // weight is the sum of the weight of the wagon and the weight of the cargo. truelight@0: weight += rvi->weight; truelight@0: weight += (_cargoc.weights[u->cargo_type] * u->cargo_count) >> 4; truelight@0: truelight@0: } while ( (u=u->next) != NULL); truelight@193: truelight@0: // these are shown in the UI truelight@0: v->u.rail.cached_weight = weight; truelight@0: v->u.rail.cached_power = power; truelight@0: v->max_speed = max_speed; truelight@0: truelight@0: assert(weight != 0); truelight@0: truelight@0: // compute acceleration truelight@0: acc = power / weight * 4; truelight@0: truelight@0: if (acc >= 255) acc=255; truelight@0: if (acc == 0) acc++; truelight@0: truelight@0: v->acceleration = (byte)acc; truelight@0: } truelight@0: truelight@0: int GetTrainImage(Vehicle *v, byte direction) truelight@0: { truelight@0: int img = v->spritenum; truelight@0: int base; truelight@0: truelight@0: if (is_custom_sprite(img)) { truelight@0: base = GetCustomVehicleSprite(v, direction + 4 * is_custom_secondhead_sprite(img)); truelight@0: if (base) return base; truelight@0: img = _engine_original_sprites[v->engine_type]; truelight@0: } truelight@193: truelight@0: base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]); truelight@0: truelight@0: if (v->cargo_count >= (v->cargo_cap >> 1)) truelight@0: base += _wagon_full_adder[img]; truelight@0: return base; truelight@0: } truelight@0: truelight@0: void DrawTrainEngine(int x, int y, int engine, uint32 image_ormod) truelight@0: { tron@540: const RailVehicleInfo *rvi = RailVehInfo(engine); truelight@193: truelight@0: int img = rvi->image_index; truelight@0: uint32 image = 0; truelight@0: truelight@0: if (is_custom_sprite(img)) { darkvater@358: image = GetCustomVehicleIcon(engine, 6); truelight@0: if (!image) img = _engine_original_sprites[engine]; truelight@0: } truelight@0: if (!image) { truelight@0: image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img]; truelight@0: } truelight@0: truelight@0: if (rvi->flags & RVI_MULTIHEAD) { truelight@0: DrawSprite(image | image_ormod, x-14, y); truelight@0: x += 15; truelight@0: image = 0; truelight@0: if (is_custom_sprite(img)) { darkvater@358: image = GetCustomVehicleIcon(engine, 2); truelight@0: if (!image) img = _engine_original_sprites[engine]; truelight@0: } truelight@0: if (!image) { truelight@193: image = ((6 + _engine_sprite_add[img+1]) & _engine_sprite_and[img+1]) + _engine_sprite_base[img+1]; truelight@0: } truelight@0: } truelight@0: DrawSprite(image | image_ormod, x, y); truelight@0: } truelight@0: truelight@0: void DrawTrainEngineInfo(int engine, int x, int y, int maxw) truelight@0: { tron@540: const RailVehicleInfo *rvi = RailVehInfo(engine); truelight@0: int cap; truelight@0: uint multihead = ((rvi->flags & RVI_MULTIHEAD) ? 1 : 0); truelight@0: tron@534: SetDParam(0, ((_price.build_railvehicle >> 3) * rvi->base_cost) >> 5); tron@534: SetDParam(2, rvi->max_speed * 10 >> 4); tron@534: SetDParam(3, rvi->power << multihead); tron@534: SetDParam(1, rvi->weight << multihead); tron@534: tron@534: SetDParam(4, (rvi->running_cost_base * _price.running_rail[rvi->engclass] >> 8) << multihead); truelight@193: truelight@0: cap = rvi->capacity; tron@534: SetDParam(5, STR_8838_N_A); truelight@0: if (cap != 0) { tron@534: SetDParam(6, cap << multihead); tron@534: SetDParam(5, _cargoc.names_long_p[rvi->cargo_type]); truelight@0: } truelight@0: DrawStringMultiCenter(x, y, STR_885B_COST_WEIGHT_T_SPEED_POWER, maxw); truelight@0: } truelight@0: truelight@0: tron@410: static int32 CmdBuildRailWagon(uint engine, uint tile, uint32 flags) truelight@0: { truelight@0: int32 value; truelight@0: Vehicle *v; truelight@0: const RailVehicleInfo *rvi; truelight@0: int dir; truelight@0: const Engine *e; truelight@0: int x,y; truelight@0: darkvater@889: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); darkvater@889: tron@540: rvi = RailVehInfo(engine); truelight@0: value = (rvi->base_cost * _price.build_railwagon) >> 8; truelight@0: truelight@0: if (!(flags & DC_QUERY_COST)) { truelight@0: _error_message = STR_00E1_TOO_MANY_VEHICLES_IN_GAME; truelight@0: truelight@0: v = AllocateVehicle(); truelight@0: if (v == NULL) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: byte img = rvi->image_index; truelight@919: Vehicle *u, *w; truelight@0: truelight@0: v->spritenum = img; truelight@0: truelight@919: u = NULL; truelight@919: truelight@919: FOR_ALL_VEHICLES(w) { truelight@919: if (w->type == VEH_Train && w->tile == (TileIndex)tile && bjarni@1067: w->subtype == TS_Free_Car && w->engine_type == engine) { truelight@919: u = GetLastVehicleInChain(w); truelight@0: break; truelight@0: } truelight@0: } truelight@0: truelight@0: v->engine_type = engine; truelight@0: truelight@0: dir = _map5[tile] & 3; truelight@0: truelight@0: v->direction = (byte)(dir*2+1); truelight@0: v->tile = (TileIndex)tile; truelight@193: tron@926: x = TileX(tile) * 16 | _vehicle_initial_x_fract[dir]; tron@926: y = TileY(tile) * 16 | _vehicle_initial_y_fract[dir]; truelight@0: truelight@0: v->x_pos = x; truelight@0: v->y_pos = y; truelight@0: v->z_pos = GetSlopeZ(x,y); truelight@0: v->owner = _current_player; truelight@0: v->z_height = 6; truelight@0: v->u.rail.track = 0x80; truelight@0: v->vehstatus = VS_HIDDEN | VS_DEFPAL; truelight@0: bjarni@1067: v->subtype = TS_Free_Car; truelight@0: if (u != NULL) { truelight@0: u->next = v; bjarni@1067: v->subtype = TS_Not_First; truelight@0: v->u.rail.first_engine = u->u.rail.first_engine; bjarni@1067: if (v->u.rail.first_engine == 0xffff && u->subtype == TS_Front_Engine ) truelight@0: v->u.rail.first_engine = u->engine_type; truelight@0: } else { truelight@0: v->u.rail.first_engine = 0xffff; truelight@0: } truelight@0: truelight@0: v->cargo_type = rvi->cargo_type; truelight@0: v->cargo_cap = rvi->capacity; truelight@0: v->value = value; truelight@0: // v->day_counter = 0; truelight@0: truelight@0: e = &_engines[engine]; truelight@0: v->u.rail.railtype = e->railtype; truelight@193: truelight@0: v->build_year = _cur_year; truelight@0: v->type = VEH_Train; truelight@0: v->cur_image = 0xAC2; truelight@193: truelight@0: _new_wagon_id = v->index; truelight@0: truelight@0: VehiclePositionChanged(v); truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@0: } truelight@0: } truelight@0: truelight@0: return value; truelight@0: } truelight@0: truelight@0: // Move all free vehicles in the depot to the train truelight@0: static void NormalizeTrainVehInDepot(Vehicle *u) truelight@0: { truelight@0: Vehicle *v; truelight@0: FOR_ALL_VEHICLES(v) { bjarni@1067: if (v->type == VEH_Train && v->subtype == TS_Free_Car && truelight@0: v->tile == u->tile && truelight@0: v->u.rail.track == 0x80) { truelight@0: if (DoCommandByTile(0,v->index | (u->index<<16), 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE) == CMD_ERROR) truelight@0: break; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static const byte _railveh_unk1[] = { truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 1, 1, 0, 0, 0, truelight@0: 0, 0, 0, 0, 1, 0, 1, 0, truelight@0: 0, 1, 1, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 1, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, truelight@0: }; truelight@0: truelight@0: static const byte _railveh_score[] = { truelight@0: 1, 4, 7, 19, 20, 30, 31, 19, truelight@0: 20, 21, 22, 10, 11, 30, 31, 32, truelight@0: 33, 34, 35, 29, 45, 32, 50, 40, truelight@0: 41, 51, 52, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 60, 62, truelight@0: 63, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 70, 71, 72, 73, truelight@0: 74, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, 0, 0, 0, 0, truelight@0: 0, 0, 0, 0, truelight@0: }; truelight@0: truelight@0: truelight@812: int32 EstimateTrainCost(const RailVehicleInfo *rvi) truelight@0: { truelight@0: return (rvi->base_cost * (_price.build_railvehicle >> 3)) >> 5; truelight@0: } truelight@0: tron@1109: void AddRearEngineToMultiheadedTrain(Vehicle *v, Vehicle *u, bool building) bjarni@1060: { bjarni@1060: u->direction = v->direction; bjarni@1060: u->owner = v->owner; bjarni@1060: u->tile = v->tile; bjarni@1060: u->x_pos = v->x_pos; bjarni@1060: u->y_pos = v->y_pos; bjarni@1060: u->z_pos = v->z_pos; bjarni@1060: u->z_height = 6; bjarni@1060: u->u.rail.track = 0x80; bjarni@1060: v->u.rail.first_engine = 0xffff; bjarni@1060: u->vehstatus = v->vehstatus & ~VS_STOPPED; bjarni@1067: u->subtype = TS_Not_First; bjarni@1060: u->spritenum = v->spritenum + 1; bjarni@1060: u->cargo_type = v->cargo_type; bjarni@1060: u->cargo_cap = v->cargo_cap; bjarni@1060: u->u.rail.railtype = v->u.rail.railtype; bjarni@1060: if (building) v->next = u; bjarni@1060: u->engine_type = v->engine_type; bjarni@1060: u->build_year = v->build_year; tron@1109: if (building) bjarni@1060: v->value = u->value = v->value >> 1; bjarni@1060: else bjarni@1060: u->value = v->value; bjarni@1060: u->type = VEH_Train; bjarni@1060: u->cur_image = 0xAC2; bjarni@1060: VehiclePositionChanged(u); bjarni@1060: } bjarni@1060: truelight@0: /* Build a railroad vehicle truelight@0: * p1 = vehicle type id truelight@0: */ truelight@0: truelight@0: int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: const RailVehicleInfo *rvi; truelight@0: int value,dir; truelight@0: Vehicle *v, *u; truelight@1282: UnitID unit_num; truelight@0: Engine *e; matthijs@1330: TileIndex tile = TILE_FROM_XY(x,y); truelight@0: tron@1197: if (!IsEngineBuildable(p1, VEH_Train)) return CMD_ERROR; bjarni@1196: matthijs@1330: if (!IsTileDepotType(tile, TRANSPORT_RAIL)) return CMD_ERROR; bjarni@1223: bjarni@1223: if (_map_owner[tile] != _current_player) return CMD_ERROR; bjarni@1221: truelight@0: _cmd_build_rail_veh_var1 = 0; truelight@193: truelight@0: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); truelight@0: tron@540: rvi = RailVehInfo(p1); truelight@193: truelight@0: if (rvi->flags & RVI_WAGON) { truelight@0: return CmdBuildRailWagon(p1, tile, flags); truelight@0: } truelight@0: truelight@0: value = EstimateTrainCost(rvi); truelight@0: truelight@0: if (!(flags & DC_QUERY_COST)) { truelight@0: v = AllocateVehicle(); truelight@1024: if (v == NULL || IsOrderPoolFull()) truelight@0: return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); truelight@0: truelight@0: unit_num = GetFreeUnitNumber(VEH_Train); truelight@0: if (unit_num > _patches.max_trains) truelight@0: return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: v->unitnumber = unit_num; truelight@0: truelight@0: dir = _map5[tile] & 3; truelight@0: truelight@0: v->direction = (byte)(dir*2+1); truelight@0: v->tile = (TileIndex)tile; truelight@0: v->owner = _current_player; truelight@0: v->x_pos = (x |= _vehicle_initial_x_fract[dir]); truelight@0: v->y_pos = (y |= _vehicle_initial_y_fract[dir]); truelight@0: v->z_pos = GetSlopeZ(x,y); truelight@0: v->z_height = 6; truelight@0: v->u.rail.track = 0x80; truelight@0: v->u.rail.first_engine = 0xffff; truelight@0: v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; truelight@0: v->spritenum = rvi->image_index; truelight@0: v->cargo_type = rvi->cargo_type; truelight@0: v->cargo_cap = rvi->capacity; truelight@0: v->max_speed = rvi->max_speed; truelight@0: v->value = value; truelight@1266: v->last_station_visited = INVALID_STATION; truelight@0: v->dest_tile = 0; truelight@193: truelight@0: v->engine_type = (byte)p1; truelight@0: e = &_engines[p1]; truelight@0: truelight@0: v->reliability = e->reliability; truelight@0: v->reliability_spd_dec = e->reliability_spd_dec; truelight@0: v->max_age = e->lifelength * 366; truelight@193: truelight@0: v->string_id = STR_SV_TRAIN_NAME; truelight@0: v->u.rail.railtype = e->railtype; truelight@0: _new_train_id = v->index; truelight@193: truelight@0: v->service_interval = _patches.servint_trains; truelight@0: v->date_of_last_service = _date; truelight@0: v->build_year = _cur_year; truelight@0: v->type = VEH_Train; truelight@0: v->cur_image = 0xAC2; truelight@0: truelight@0: VehiclePositionChanged(v); truelight@0: bjarni@1060: if (rvi->flags&RVI_MULTIHEAD && (u = AllocateVehicle()) != NULL) bjarni@1060: AddRearEngineToMultiheadedTrain(v, u, true); truelight@0: truelight@0: UpdateTrainAcceleration(v); truelight@0: NormalizeTrainVehInDepot(v); truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, tile); tron@588: RebuildVehicleLists(); truelight@0: InvalidateWindow(WC_COMPANY, v->owner); truelight@0: } truelight@0: } truelight@0: _cmd_build_rail_veh_var1 = _railveh_unk1[p1]; truelight@0: _cmd_build_rail_veh_score = _railveh_score[p1]; bjarni@1128: bjarni@1128: InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window bjarni@1128: truelight@193: return value; truelight@0: } truelight@0: tron@410: static bool IsTunnelTile(TileIndex tile) truelight@0: { tron@1035: return IsTileType(tile, MP_TUNNELBRIDGE) && truelight@0: (_map5[tile]&0x80) == 0; truelight@0: } truelight@0: truelight@0: truelight@1313: int CheckTrainStoppedInDepot(Vehicle *v) truelight@0: { truelight@0: int count; truelight@0: TileIndex tile = v->tile; truelight@193: truelight@0: /* check if stopped in a depot */ matthijs@1330: if (!IsTileDepotType(tile, TRANSPORT_RAIL) || v->cur_speed != 0) { truelight@0: errmsg: truelight@0: _error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED; truelight@0: return -1; truelight@0: } truelight@0: truelight@0: count = 0; truelight@0: do { truelight@0: count++; truelight@193: if (v->u.rail.track != 0x80 || v->tile != (TileIndex)tile || bjarni@1067: (v->subtype == TS_Front_Engine && !(v->vehstatus&VS_STOPPED))) truelight@0: goto errmsg; truelight@0: } while ( (v=v->next) != NULL); truelight@0: truelight@0: return count; truelight@0: } truelight@0: truelight@0: // unlink a rail wagon from the linked list. truelight@0: // returns the new value of first truelight@0: static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first) truelight@0: { truelight@0: // unlinking the first vehicle of the chain? truelight@0: v->u.rail.first_engine = 0xffff; truelight@0: if (v == first) { truelight@0: Vehicle *u; truelight@0: if ((v=v->next) == NULL) return NULL; truelight@0: for (u=v; u; u=u->next) u->u.rail.first_engine = v->engine_type; bjarni@1067: v->subtype = TS_Free_Car; truelight@0: return v; truelight@0: } else { truelight@0: Vehicle *u; truelight@0: for(u=first; u->next!=v; u=u->next) {} truelight@0: u->next = v->next; truelight@0: return first; truelight@0: } truelight@0: } truelight@0: truelight@0: static Vehicle *FindGoodVehiclePos(Vehicle *src) truelight@0: { truelight@0: Vehicle *dst; truelight@0: uint16 eng = src->engine_type; truelight@0: TileIndex tile = src->tile; truelight@0: truelight@0: FOR_ALL_VEHICLES(dst) { bjarni@1067: if (dst->type == VEH_Train && dst->subtype == TS_Free_Car && dst->tile==tile) { truelight@0: // check so all vehicles in the line have the same engine. truelight@0: Vehicle *v = dst; truelight@0: while (v->engine_type == eng) { truelight@0: if ((v = v->next) == NULL) return dst; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: /* p1 & 0xffff= source vehicle index truelight@0: p1 & 0xffff0000 = what wagon to put the wagon AFTER, 0xffff0000 to make a new line truelight@0: p2 & 1 = move all vehicles following the vehicle.. truelight@0: */ truelight@0: truelight@0: int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *src, *dst, *src_head, *dst_head; truelight@0: bool is_loco; truelight@0: bjarni@1237: if (!IsVehicleIndex(p1 & 0xFFFF)) return CMD_ERROR; bjarni@1237: truelight@919: src = GetVehicle(p1 & 0xFFFF); bjarni@1237: truelight@0: if (src->type != VEH_Train) return CMD_ERROR; truelight@0: tron@540: is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON) truelight@0: && is_firsthead_sprite(src->spritenum); truelight@0: truelight@0: // if nothing is selected as destination, try and find a matching vehicle to drag to. truelight@193: if (((int32)p1 >> 16) == -1) { truelight@0: dst = NULL; truelight@0: if (!is_loco) dst = FindGoodVehiclePos(src); truelight@0: } else { truelight@919: dst = GetVehicle(((int32)p1 >> 16)); truelight@0: } truelight@0: truelight@0: // don't move the same vehicle.. truelight@0: if (src == dst) truelight@0: return 0; truelight@193: truelight@0: /* the player must be the owner */ truelight@0: if (!CheckOwnership(src->owner) || (dst!=NULL && !CheckOwnership(dst->owner))) truelight@0: return CMD_ERROR; truelight@0: truelight@0: /* locate the head of the two chains */ truelight@0: src_head = GetFirstVehicleInChain(src); truelight@0: dst_head = NULL; truelight@0: if (dst != NULL) dst_head = GetFirstVehicleInChain(dst); truelight@193: truelight@0: /* check if all vehicles in the source train are stopped */ truelight@1313: if (CheckTrainStoppedInDepot(src_head) < 0) truelight@0: return CMD_ERROR; truelight@0: truelight@0: /* check if all the vehicles in the dest train are stopped, truelight@0: * and that the length of the dest train is no longer than XXX vehicles */ truelight@0: if (dst_head != NULL) { truelight@1313: int num = CheckTrainStoppedInDepot(dst_head); truelight@0: if (num < 0) truelight@0: return CMD_ERROR; truelight@0: bjarni@1067: if (num > (_patches.mammoth_trains ? 100 : 9) && dst_head->subtype == TS_Front_Engine ) truelight@0: return_cmd_error(STR_8819_TRAIN_TOO_LONG); truelight@0: truelight@0: // if it's a multiheaded vehicle we're dragging to, drag to the vehicle before.. truelight@0: while (is_custom_secondhead_sprite(dst->spritenum) truelight@0: || (!is_custom_sprite(dst->spritenum) && _engine_sprite_add[dst->spritenum] != 0)) { truelight@0: Vehicle *v = GetPrevVehicleInChain(dst); truelight@0: if (!v || src == v) break; truelight@0: dst = v; truelight@0: } truelight@0: truelight@0: assert(dst_head->tile == src_head->tile); truelight@0: } truelight@0: truelight@0: // when moving all wagons, we can't have the same src_head and dst_head truelight@0: if (p2 & 1 && src_head == dst_head) truelight@0: return 0; truelight@0: truelight@0: // moving a loco to a new line?, then we need to assign a unitnumber. bjarni@1067: if (dst == NULL && src->subtype != TS_Front_Engine && is_loco) { truelight@1282: UnitID unit_num = GetFreeUnitNumber(VEH_Train); truelight@0: if (unit_num > _patches.max_trains) truelight@0: return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); truelight@0: truelight@0: if (flags & DC_EXEC) truelight@0: src->unitnumber = unit_num; truelight@0: } truelight@0: truelight@0: truelight@0: /* do it? */ truelight@0: if (flags & DC_EXEC) { truelight@0: if (p2 & 1) { truelight@0: // unlink ALL wagons truelight@0: if (src != src_head) { truelight@0: Vehicle *v = src_head; truelight@0: while (v->next != src) v=v->next; truelight@0: v->next = NULL; truelight@0: } truelight@0: } else { truelight@0: // unlink single wagon from linked list truelight@0: UnlinkWagon(src, src_head); truelight@0: src->next = NULL; truelight@0: } truelight@0: truelight@0: if (dst == NULL) { truelight@0: // move the train to an empty line. for locomotives, we set the type to 0. for wagons, 4. truelight@0: if (is_loco) { bjarni@1067: if (src->subtype != TS_Front_Engine) { truelight@1024: // setting the type to 0 also involves setting up the orders field. bjarni@1067: src->subtype = TS_Front_Engine; truelight@1024: assert(src->orders == NULL); truelight@0: src->num_orders = 0; truelight@0: } truelight@0: dst_head = src; truelight@0: } else { bjarni@1067: src->subtype = TS_Free_Car; truelight@0: } truelight@0: src->u.rail.first_engine = 0xffff; truelight@0: } else { bjarni@1067: if (src->subtype == TS_Front_Engine) { truelight@1024: // the vehicle was previously a loco. need to free the order list and delete vehicle windows etc. truelight@0: DeleteWindowById(WC_VEHICLE_VIEW, src->index); truelight@1024: DeleteVehicleOrders(src); truelight@0: } truelight@193: bjarni@1067: src->subtype = TS_Not_First; truelight@0: src->unitnumber = 0; // doesn't occupy a unitnumber anymore. truelight@0: truelight@0: // setup first_engine truelight@0: src->u.rail.first_engine = dst->u.rail.first_engine; bjarni@1067: if (src->u.rail.first_engine == 0xffff && dst->subtype == TS_Front_Engine) truelight@0: src->u.rail.first_engine = dst->engine_type; truelight@0: truelight@0: // link in the wagon(s) in the chain. truelight@0: { truelight@0: Vehicle *v = src; truelight@0: while (v->next != NULL) { truelight@0: v->next->u.rail.first_engine = v->u.rail.first_engine; truelight@0: v = v->next; truelight@0: } truelight@0: v->next = dst->next; truelight@0: } truelight@0: dst->next = src; truelight@0: } truelight@0: bjarni@1067: if (src_head->subtype == TS_Front_Engine) truelight@0: UpdateTrainAcceleration(src_head); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, src_head->index); truelight@0: truelight@0: if (dst_head) { bjarni@1067: if (dst_head->subtype == TS_Front_Engine) truelight@0: UpdateTrainAcceleration(dst_head); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, dst_head->index); celestar@1387: /* Update the refit button */ celestar@1387: InvalidateWindowWidget(WC_VEHICLE_VIEW, dst_head->index, 12); truelight@0: } truelight@0: celestar@1387: /* I added this to so that the refit buttons get updated */ celestar@1387: InvalidateWindowWidget(WC_VEHICLE_VIEW, src_head->index, 12); truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile); tron@737: RebuildVehicleLists(); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: /* p1 = train to start / stop */ truelight@0: int32 CmdStartStopTrain(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; bjarni@1237: truelight@919: v = GetVehicle(p1); truelight@0: bjarni@1237: if (v->type != VEH_Train || !CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: v->u.rail.days_since_order_progr = 0; truelight@0: v->vehstatus ^= VS_STOPPED; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: truelight@0: // p1 = wagon/loco index truelight@0: // p2 = mode truelight@0: // 0: sell just the vehicle truelight@0: // 1: sell the vehicle and all vehicles that follow it in the chain truelight@0: // 2: when selling attached locos, rearrange all vehicles after it to separate lines. truelight@0: int32 CmdSellRailWagon(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v, *first,*last; truelight@0: int32 cost; truelight@0: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; truelight@0: truelight@919: v = GetVehicle(p1); truelight@0: darkvater@1235: if (v->type != VEH_Train || !CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: bjarni@1237: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); bjarni@1237: truelight@0: // get first vehicle in chain truelight@0: first = v; bjarni@1067: if (first->subtype != TS_Front_Engine) { truelight@0: first = GetFirstVehicleInChain(first); signde@242: last = GetLastVehicleInChain(first); signde@242: //now if: signde@242: // 1) we delete a whole a chain, and darkvater@244: // 2) we don't actually try to delete the last engine darkvater@244: // 3) the first and the last vehicle of that train are of the same type, and darkvater@244: // 4) the first and the last vehicle of the chain are not identical darkvater@244: // 5) and of "engine" type (i.e. not a carriage) signde@242: // then let the last vehicle live bjarni@1067: if ( (p2 == 1) && (v != last) && ( last->engine_type == first->engine_type ) && (last != first) && (first->subtype == TS_Front_Engine) ) signde@242: last = GetPrevVehicleInChain(last); signde@242: else signde@242: last = NULL; truelight@0: } else { truelight@0: if (p2 != 1) { truelight@0: // sell last part of multiheaded? truelight@0: last = GetLastVehicleInChain(v); truelight@73: // Check if the end-part is the same engine and check if it is the rear-end truelight@73: if (last->engine_type != first->engine_type || is_firsthead_sprite(last->spritenum)) truelight@0: last = NULL; truelight@0: } else { truelight@0: last = NULL; truelight@0: } truelight@0: } truelight@193: truelight@0: // make sure the vehicle is stopped in the depot truelight@1313: if (CheckTrainStoppedInDepot(first) < 0) truelight@0: return CMD_ERROR; truelight@0: truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: // always redraw the depot. maybe redraw train list truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, first->tile); bjarni@1067: if (first->subtype == TS_Front_Engine) { tron@588: RebuildVehicleLists(); darkvater@164: } truelight@0: // when selling an attached locomotive. we need to delete its window. bjarni@1067: if (v->subtype == TS_Front_Engine) { truelight@0: DeleteWindowById(WC_VEHICLE_VIEW, v->index); truelight@193: truelight@0: // rearrange all vehicles that follow to separate lines. truelight@0: if (p2 == 2) { truelight@0: Vehicle *u,*tmp; truelight@0: u = v->next; truelight@0: while (u != last) { truelight@0: tmp = u; truelight@0: u = u->next; truelight@193: DoCommandByTile(tmp->tile, tmp->index | ((-1)<<16), 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: // delete the vehicles truelight@0: cost = 0; truelight@0: for(;;) { truelight@0: Vehicle *tmp; truelight@0: truelight@0: assert(first); truelight@0: first = UnlinkWagon(v, first); truelight@0: cost -= v->value; truelight@0: tmp = v; truelight@0: DeleteVehicle(tmp); darkvater@244: if ( v == last ) { darkvater@244: last = NULL; darkvater@244: break; darkvater@244: } darkvater@244: if ( (v=v->next) == last || p2 != 1) break; truelight@0: } truelight@0: truelight@0: // delete last vehicle of multiheaded train? truelight@0: if (last) { truelight@0: first = UnlinkWagon(last, first); truelight@0: cost -= last->value; truelight@0: DeleteVehicle(last); truelight@0: } truelight@193: truelight@0: // an attached train changed? bjarni@1067: if (first && first->subtype == TS_Front_Engine) { truelight@0: UpdateTrainAcceleration(first); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, first->index); truelight@0: } truelight@0: } else { truelight@0: cost = 0; truelight@0: for(;;) { truelight@0: cost -= v->value; darkvater@244: if ( v == last ) { darkvater@244: last = NULL; darkvater@244: break; darkvater@244: } darkvater@244: if ( (v=v->next) == last || p2 != 1) break; truelight@0: } truelight@0: if (last) cost -= last->value; truelight@0: } bjarni@1128: InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: static void UpdateTrainDeltaXY(Vehicle *v, int direction) truelight@0: { truelight@0: #define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0) truelight@0: static const uint32 _delta_xy_table[8] = { truelight@0: MKIT(3, 3, -1, -1), truelight@0: MKIT(3, 7, -1, -3), truelight@0: MKIT(3, 3, -1, -1), truelight@0: MKIT(7, 3, -3, -1), truelight@0: MKIT(3, 3, -1, -1), truelight@0: MKIT(3, 7, -1, -3), truelight@0: MKIT(3, 3, -1, -1), truelight@0: MKIT(7, 3, -3, -1), truelight@0: }; truelight@0: #undef MKIT truelight@0: truelight@0: uint32 x = _delta_xy_table[direction]; truelight@0: truelight@0: v->x_offs = (byte)x; truelight@0: v->y_offs = (byte)(x>>=8); truelight@0: v->sprite_width = (byte)(x>>=8); truelight@0: v->sprite_height = (byte)(x>>=8); truelight@0: } truelight@0: truelight@0: static void UpdateVarsAfterSwap(Vehicle *v) truelight@0: { truelight@0: UpdateTrainDeltaXY(v, v->direction); truelight@0: v->cur_image = GetTrainImage(v, v->direction); truelight@0: BeginVehicleMove(v); truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: truelight@193: static void SetLastSpeed(Vehicle *v, int spd) { truelight@0: int old = v->u.rail.last_speed; truelight@0: if (spd != old) { truelight@0: v->u.rail.last_speed = spd; truelight@0: if (_patches.vehicle_speed || !old != !spd) darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: } truelight@0: truelight@954: static void SwapTrainFlags(byte *swap_flag1, byte *swap_flag2) truelight@954: { truelight@954: byte flag1, flag2; truelight@954: truelight@954: flag1 = *swap_flag1; truelight@954: flag2 = *swap_flag2; truelight@954: truelight@954: /* Clear the flags */ truelight@954: CLRBIT(*swap_flag1, VRF_GOINGUP); truelight@954: CLRBIT(*swap_flag1, VRF_GOINGDOWN); truelight@954: CLRBIT(*swap_flag2, VRF_GOINGUP); truelight@954: CLRBIT(*swap_flag2, VRF_GOINGDOWN); truelight@954: truelight@954: /* Reverse the rail-flags (if needed) */ truelight@954: if (HASBIT(flag1, VRF_GOINGUP)) { truelight@954: SETBIT(*swap_flag2, VRF_GOINGDOWN); truelight@954: } else if (HASBIT(flag1, VRF_GOINGDOWN)) { truelight@954: SETBIT(*swap_flag2, VRF_GOINGUP); truelight@954: } truelight@954: if (HASBIT(flag2, VRF_GOINGUP)) { truelight@954: SETBIT(*swap_flag1, VRF_GOINGDOWN); truelight@954: } else if (HASBIT(flag2, VRF_GOINGDOWN)) { truelight@954: SETBIT(*swap_flag1, VRF_GOINGUP); truelight@954: } truelight@954: } truelight@954: truelight@0: static void ReverseTrainSwapVeh(Vehicle *v, int l, int r) truelight@0: { truelight@0: Vehicle *a, *b; truelight@0: truelight@0: /* locate vehicles to swap */ truelight@0: for(a=v; l!=0; l--) { a = a->next; } truelight@0: for(b=v; r!=0; r--) { b = b->next; } truelight@0: truelight@0: if (a != b) { truelight@0: /* swap the hidden bits */ truelight@0: { truelight@0: uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus&VS_HIDDEN); truelight@0: b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus&VS_HIDDEN); truelight@0: a->vehstatus = tmp; truelight@0: } truelight@193: truelight@0: /* swap variables */ truelight@0: swap_byte(&a->u.rail.track, &b->u.rail.track); truelight@0: swap_byte(&a->direction, &b->direction); truelight@0: truelight@0: /* toggle direction */ truelight@0: if (!(a->u.rail.track & 0x80)) a->direction ^= 4; truelight@0: if (!(b->u.rail.track & 0x80)) b->direction ^= 4; truelight@193: truelight@0: /* swap more variables */ tron@1174: swap_int32(&a->x_pos, &b->x_pos); tron@1174: swap_int32(&a->y_pos, &b->y_pos); truelight@0: swap_tile(&a->tile, &b->tile); truelight@0: swap_byte(&a->z_pos, &b->z_pos); truelight@0: truelight@954: SwapTrainFlags(&a->u.rail.flags, &b->u.rail.flags); truelight@954: truelight@0: /* update other vars */ truelight@0: UpdateVarsAfterSwap(a); truelight@0: UpdateVarsAfterSwap(b); truelight@0: } else { truelight@0: if (!(a->u.rail.track & 0x80)) a->direction ^= 4; truelight@193: UpdateVarsAfterSwap(a); truelight@0: } truelight@0: } truelight@0: truelight@744: /* Check if the vehicle is a train and is on the tile we are testing */ truelight@744: static void *TestTrainOnCrossing(Vehicle *v, void *data) truelight@744: { truelight@744: if (v->tile != *(const TileIndex*)data || v->type != VEH_Train) truelight@744: return NULL; truelight@744: truelight@744: return v; truelight@744: } truelight@744: dominik@1103: static void DisableTrainCrossing(TileIndex tile) dominik@1103: { dominik@1103: /* Test if we have a rail/road-crossing */ dominik@1103: if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) { dominik@1103: /* Check if there is a train on the tile itself */ dominik@1103: if (VehicleFromPos(tile, &tile, TestTrainOnCrossing) == NULL) { dominik@1103: /* If light is on, switch light off */ dominik@1103: if (_map5[tile] & 4) { dominik@1103: _map5[tile] &= ~4; dominik@1103: MarkTileDirtyByTile(tile); dominik@1103: } dominik@1103: } dominik@1103: } dominik@1103: } dominik@1103: truelight@0: static void ReverseTrainDirection(Vehicle *v) truelight@0: { truelight@0: int l = 0, r = -1; truelight@0: Vehicle *u; truelight@0: matthijs@1330: if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@0: truelight@743: /* Check if we were approaching a rail/road-crossing */ truelight@743: { truelight@743: TileIndex tile = v->tile; truelight@743: int t; truelight@743: /* Determine the non-diagonal direction in which we will exit this tile */ truelight@743: t = v->direction >> 1; truelight@743: if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) { truelight@743: t = (t - 1) & 3; truelight@743: } truelight@743: /* Calculate next tile */ tron@900: tile += TileOffsByDir(t); dominik@1103: dominik@1103: /* Check if the train left a rail/road-crossing */ dominik@1103: DisableTrainCrossing(tile); truelight@743: } truelight@743: truelight@0: // count number of vehicles truelight@0: u = v; truelight@0: do r++; while ( (u = u->next) != NULL ); truelight@0: truelight@0: /* swap start<>end, start+1<>end-1, ... */ truelight@0: do { truelight@0: ReverseTrainSwapVeh(v, l++, r--); truelight@0: } while (l <= r); truelight@0: matthijs@1330: if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@0: truelight@954: CLRBIT(v->u.rail.flags, VRF_REVERSING); truelight@0: } truelight@0: truelight@0: /* p1 = vehicle */ truelight@0: int32 CmdReverseTrainDirection(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; bjarni@1237: truelight@919: v = GetVehicle(p1); truelight@0: bjarni@1237: if (v->type != VEH_Train || !CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: _error_message = STR_EMPTY; truelight@0: matthijs@1330: // if (v->u.rail.track & 0x80 || IsTileDepotType(v->tile, TRANSPORT_RAIL)) truelight@0: // return CMD_ERROR; truelight@0: truelight@0: if (v->u.rail.crash_anim_pos != 0 || v->breakdown_ctr != 0) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: if (_patches.realistic_acceleration && v->cur_speed != 0) { truelight@954: TOGGLEBIT(v->u.rail.flags, VRF_REVERSING); truelight@0: } else { truelight@0: v->cur_speed = 0; truelight@0: SetLastSpeed(v, 0); truelight@0: ReverseTrainDirection(v); truelight@0: } truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: truelight@0: int32 CmdForceTrainProceed(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; bjarni@1237: truelight@919: v = GetVehicle(p1); truelight@0: bjarni@1237: if (v->type != VEH_Train || !CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) truelight@0: v->u.rail.force_proceed = 0x50; truelight@193: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: // p1 = vehicle to refit bjarni@842: // p2 = new cargo (0xFF) bjarni@842: // p2 = skip check for stopped in hanger (0x0100) truelight@0: int32 CmdRefitRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: int32 cost; truelight@0: uint num; truelight@0: bjarni@842: byte SkipStoppedInDepotCheck = (p2 & 0x100) >> 8; bjarni@842: bjarni@842: p2 = p2 & 0xFF; bjarni@842: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; tron@915: truelight@919: v = GetVehicle(p1); bjarni@1237: truelight@1313: if (v->type != VEH_Train || !CheckOwnership(v->owner) || ((CheckTrainStoppedInDepot(v) < 0) && !(SkipStoppedInDepotCheck))) truelight@0: return CMD_ERROR; truelight@0: bjarni@1237: SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN); bjarni@1237: truelight@0: cost = 0; truelight@0: num = 0; tron@915: truelight@0: do { pasky@491: /* XXX: We also refit all the attached wagons en-masse if they pasky@491: * can be refitted. This is how TTDPatch does it. TODO: Have pasky@491: * some nice [Refit] button near each wagon. --pasky */ tron@540: if ((!(RailVehInfo(v->engine_type)->flags & RVI_WAGON) pasky@491: || (_engine_refit_masks[v->engine_type] & (1 << p2))) pasky@491: && (byte) p2 != v->cargo_type && v->cargo_cap != 0) { dominik@15: cost += (_price.build_railvehicle >> 8); truelight@0: num += v->cargo_cap; truelight@0: if (flags & DC_EXEC) { bjarni@842: //autorefitted train cars wants to keep the cargo bjarni@842: //it will be checked if the cargo is valid in CmdReplaceVehicle bjarni@842: if (!(SkipStoppedInDepotCheck)) bjarni@842: v->cargo_count = 0; truelight@0: v->cargo_type = (byte)p2; truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: } truelight@193: } bjarni@842: // SkipStoppedInDepotCheck is called by CmdReplace and it should only apply to the single car it is called for bjarni@842: } while ( (v=v->next) != NULL || SkipStoppedInDepotCheck ); truelight@0: truelight@0: _returned_refit_amount = num; truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: typedef struct TrainFindDepotData { truelight@0: uint best_length; truelight@0: uint tile; truelight@0: byte owner; truelight@0: } TrainFindDepotData; truelight@0: truelight@0: static bool TrainFindDepotEnumProc(uint tile, TrainFindDepotData *tfdd, int track, uint length, byte *state) truelight@0: { tron@1035: if (IsTileType(tile, MP_RAILWAY) && _map_owner[tile] == tfdd->owner) { truelight@0: if ((_map5[tile] & ~0x3) == 0xC0) { truelight@0: if (length < tfdd->best_length) { truelight@0: tfdd->best_length = length; truelight@0: tfdd->tile = tile; truelight@0: } truelight@0: return true; truelight@0: } truelight@0: truelight@0: // make sure the train doesn't run against a oneway signal truelight@0: if ((_map5[tile] & 0xC0) == 0x40) { matthijs@1247: if (!(_map3_lo[tile] & _signal_along_trackdir[track]) && _map3_lo[tile] & _signal_against_trackdir[track]) truelight@0: return true; truelight@0: } truelight@0: } truelight@0: truelight@0: // stop searching if we've found a destination that is closer already. truelight@0: return length >= tfdd->best_length; truelight@0: } truelight@0: darkvater@308: // returns the tile of a depot to goto to. darkvater@308: static TrainFindDepotData FindClosestTrainDepot(Vehicle *v) truelight@0: { truelight@0: int i; truelight@0: TrainFindDepotData tfdd; truelight@0: uint tile = v->tile; truelight@0: darkvater@308: tfdd.owner = v->owner; darkvater@308: tfdd.best_length = (uint)-1; darkvater@308: matthijs@1330: if (IsTileDepotType(tile, TRANSPORT_RAIL)){ darkvater@308: tfdd.tile = tile; darkvater@308: tfdd.best_length = 0; darkvater@308: return tfdd; darkvater@308: } truelight@0: truelight@0: if (v->u.rail.track == 0x40) { tile = GetVehicleOutOfTunnelTile(v); } truelight@193: matthijs@1247: if (_patches.new_pathfinding_all) { matthijs@1247: NPFFoundTargetData ftd; matthijs@1247: byte trackdir = _track_direction_to_trackdir[FIND_FIRST_BIT(v->u.rail.track)][v->direction]; matthijs@1330: ftd = NPFRouteToDepotBreadthFirst(v->tile, trackdir, TRANSPORT_RAIL, v->owner); matthijs@1247: if (ftd.best_bird_dist == 0) { matthijs@1247: /* Found target */ matthijs@1247: tfdd.tile = ftd.node.tile; matthijs@1247: tfdd.best_length = ftd.best_path_dist; matthijs@1247: } matthijs@1247: } else if (!_patches.new_depot_finding) { truelight@0: // search in all directions truelight@0: for(i=0; i!=4; i++) truelight@0: NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL); truelight@0: } else { truelight@0: // search in the forward direction first. truelight@0: i = v->direction >> 1; truelight@0: if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { i = (i - 1) & 3; } truelight@0: NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL); darkvater@308: if (tfdd.best_length == (uint)-1){ darkvater@308: // search in backwards direction darkvater@308: i = (v->direction^4) >> 1; darkvater@308: if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { i = (i - 1) & 3; } darkvater@308: NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL); darkvater@308: } truelight@0: } truelight@193: darkvater@308: return tfdd; truelight@0: } truelight@0: bjarni@1208: /* Send a train to the nearest depot bjarni@1208: p1 = index of the train bjarni@1208: p2 = bit 0 = do not stop in depot bjarni@1208: bit 1 = set v->set_for_replacement bjarni@1208: bit 2 = clear v->set_for_replacement */ truelight@0: int32 CmdTrainGotoDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { bjarni@1237: Vehicle *v; darkvater@308: TrainFindDepotData tfdd; truelight@0: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; bjarni@1237: bjarni@1237: v = GetVehicle(p1); bjarni@1237: bjarni@1237: if (v->type != VEH_Train || !CheckOwnership(v->owner)) bjarni@1208: return CMD_ERROR; bjarni@1208: bjarni@1208: if (HASBIT(p2, 0)) v->set_for_replacement = true; bjarni@1208: if (HASBIT(p2, 2)) v->set_for_replacement = false; bjarni@1208: bjarni@1208: if (HASBIT(p2, 1) || HASBIT(p2, 2)) return CMD_ERROR; // vehicle has a depot in schedule. It just needed to alter set_for_replacement bjarni@1208: bjarni@1208: tron@555: if (v->current_order.type == OT_GOTO_DEPOT) { truelight@0: if (flags & DC_EXEC) { tron@555: if (v->current_order.flags & OF_UNLOAD) { truelight@0: v->u.rail.days_since_order_progr = 0; truelight@0: v->cur_order_index++; truelight@0: } truelight@193: tron@555: v->current_order.type = OT_DUMMY; tron@555: v->current_order.flags = 0; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: darkvater@308: tfdd = FindClosestTrainDepot(v); darkvater@308: if (tfdd.best_length == (uint)-1) truelight@0: return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO); truelight@0: truelight@0: if (flags & DC_EXEC) { darkvater@308: v->dest_tile = tfdd.tile; tron@555: v->current_order.type = OT_GOTO_DEPOT; bjarni@1208: v->current_order.flags = HASBIT(p2, 0) ? 0 : OF_NON_STOP | OF_FULL_LOAD; truelight@1313: v->current_order.station = GetDepotByTile(tfdd.tile)->index; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@193: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: /* p1 = vehicle truelight@0: * p2 = new service int truelight@0: */ truelight@0: int32 CmdChangeTrainServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { bjarni@1237: Vehicle *v; bjarni@1237: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; bjarni@1237: bjarni@1237: v = GetVehicle(p1); bjarni@1237: bjarni@1237: if (v->type != VEH_Train || !CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: v->service_interval = (uint16)p2; truelight@0: InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 8); truelight@0: } truelight@193: truelight@0: return 0; truelight@0: } truelight@0: tron@1093: void OnTick_Train(void) truelight@0: { truelight@0: _age_cargo_skip_counter = (_age_cargo_skip_counter == 0) ? 184 : (_age_cargo_skip_counter - 1); truelight@0: } truelight@0: truelight@0: static const int8 _vehicle_smoke_pos[16] = { truelight@0: -4, -4, -4, 0, 4, 4, 4, 0, truelight@0: -4, 0, 4, 4, 4, 0,-4,-4, truelight@0: }; truelight@0: truelight@0: static void HandleLocomotiveSmokeCloud(Vehicle *v) truelight@0: { truelight@0: Vehicle *u; truelight@0: truelight@0: if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2) truelight@0: return; truelight@0: truelight@0: u = v; truelight@0: truelight@0: do { truelight@0: int engtype = v->engine_type; truelight@0: truelight@0: // no smoke? tron@540: if (RailVehInfo(engtype)->flags & 2 truelight@0: || _engines[engtype].railtype > 0 truelight@0: || (v->vehstatus&VS_HIDDEN) || (v->u.rail.track & 0xC0) ) truelight@0: continue; truelight@0: tron@540: switch (RailVehInfo(engtype)->engclass) { truelight@0: case 0: truelight@0: // steam smoke. matthijs@1330: if ( (v->tick_counter&0xF) == 0 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile)) { truelight@193: CreateEffectVehicleRel(v, truelight@0: (_vehicle_smoke_pos[v->direction]), truelight@0: (_vehicle_smoke_pos[v->direction+8]), truelight@0: 10, truelight@0: EV_STEAM_SMOKE); truelight@0: } truelight@0: break; truelight@0: truelight@0: case 1: truelight@0: // diesel smoke matthijs@1330: if (u->cur_speed <= 40 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x1E00) { tron@1359: CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE); truelight@0: } truelight@0: break; truelight@0: truelight@0: case 2: truelight@0: // blue spark matthijs@1330: if ( (v->tick_counter&0x3) == 0 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x5B0) { tron@1359: CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK); truelight@0: } truelight@0: break; truelight@0: } truelight@0: } while ( (v = v->next) != NULL ); truelight@0: truelight@0: } truelight@0: truelight@0: static void TrainPlayLeaveStationSound(Vehicle *v) truelight@0: { tron@541: static const SoundFx sfx[] = { tron@541: SND_04_TRAIN, tron@541: SND_0A_TRAIN_HORN, tron@541: SND_0A_TRAIN_HORN tron@541: }; tron@541: truelight@0: int engtype = v->engine_type; truelight@0: truelight@0: switch (_engines[engtype].railtype) { tron@337: case 0: tron@540: SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], v); tron@337: break; tron@337: case 1: tron@337: SndPlayVehicleFx(SND_47_MAGLEV_2, v); tron@337: break; tron@337: case 2: tron@337: SndPlayVehicleFx(SND_41_MAGLEV, v); tron@337: break; truelight@0: } truelight@0: } truelight@0: truelight@0: static bool CheckTrainStayInDepot(Vehicle *v) truelight@0: { truelight@0: Vehicle *u; tron@495: tron@495: // bail out if not all wagons are in the same depot or not in a depot at all truelight@742: for (u = v; u != NULL; u = u->next) tron@495: if (u->u.rail.track != 0x80 || u->tile != v->tile) tron@495: return false; truelight@0: truelight@0: if (v->u.rail.force_proceed == 0) { bjarni@1151: if (++v->load_unload_time_rem < 37) { bjarni@1151: InvalidateWindowClasses(WC_TRAINS_LIST); truelight@0: return true; bjarni@1151: } bjarni@1151: truelight@0: v->load_unload_time_rem = 0; truelight@0: bjarni@1151: if (UpdateSignalsOnSegment(v->tile, v->direction)) { bjarni@1151: InvalidateWindowClasses(WC_TRAINS_LIST); truelight@0: return true; bjarni@1151: } truelight@0: } truelight@0: bjarni@578: VehicleServiceInDepot(v); bjarni@1151: InvalidateWindowClasses(WC_TRAINS_LIST); truelight@0: TrainPlayLeaveStationSound(v); truelight@193: truelight@0: v->u.rail.track = 1; truelight@0: if (v->direction & 2) truelight@0: v->u.rail.track = 2; truelight@193: truelight@0: v->vehstatus &= ~VS_HIDDEN; truelight@0: v->cur_speed = 0; truelight@193: truelight@0: UpdateTrainDeltaXY(v, v->direction); truelight@0: v->cur_image = GetTrainImage(v, v->direction); truelight@0: VehiclePositionChanged(v); truelight@0: UpdateSignalsOnSegment(v->tile, v->direction); truelight@0: UpdateTrainAcceleration(v); truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@0: truelight@0: return false; truelight@0: } truelight@0: matthijs@1247: /* Check for station tiles */ truelight@0: typedef struct TrainTrackFollowerData { truelight@0: TileIndex dest_coords; truelight@0: int station_index; // station index we're heading for truelight@0: uint best_bird_dist; truelight@0: uint best_track_dist; truelight@0: byte best_track; truelight@0: } TrainTrackFollowerData; truelight@0: truelight@0: static bool TrainTrackFollower(uint tile, TrainTrackFollowerData *ttfd, int track, uint length, byte *state){ tron@1035: if (IsTileType(tile, MP_RAILWAY) && (_map5[tile]&0xC0) == 0x40) { truelight@0: // the tile has a signal truelight@0: byte m3 = _map3_lo[tile]; matthijs@1247: if (!(m3 & _signal_along_trackdir[track])) { truelight@0: // if one way signal not pointing towards us, stop going in this direction. matthijs@1247: if (m3 & _signal_against_trackdir[track]) truelight@0: return true; matthijs@1247: } else if (_map2[tile] & _signal_along_trackdir[track]) { truelight@0: // green signal in our direction. either one way or two way. truelight@0: *state = true; matthijs@1247: } else if (m3 & _signal_against_trackdir[track]) { truelight@0: // two way signal. unless we passed another green signal on the way, truelight@0: // stop going in this direction. truelight@0: if (!*state) return true; truelight@0: } truelight@0: } truelight@0: truelight@0: // heading for nowhere? truelight@0: if (ttfd->dest_coords == 0) truelight@0: return false; truelight@0: truelight@0: // did we reach the final station? truelight@166: if ((ttfd->station_index == -1 && tile == ttfd->dest_coords) || tron@1035: (IsTileType(tile, MP_STATION) && IS_BYTE_INSIDE(_map5[tile], 0, 8) && _map2[tile] == ttfd->station_index)) { truelight@166: /* We do not check for dest_coords if we have a station_index, truelight@166: * because in that case the dest_coords are just an truelight@166: * approximation of where the station is */ truelight@0: // found station truelight@0: ttfd->best_bird_dist = 0; truelight@0: if (length < ttfd->best_track_dist) { truelight@0: ttfd->best_track_dist = length; truelight@0: ttfd->best_track = state[1]; truelight@0: } truelight@0: return true; truelight@0: } else { truelight@0: uint dist; truelight@0: truelight@0: // we've actually found the destination already. no point searching in directions longer than this. truelight@0: if (ttfd->best_track_dist != (uint)-1) truelight@0: return length >= ttfd->best_track_dist; truelight@193: truelight@0: // didn't find station tron@1245: dist = DistanceManhattan(tile, ttfd->dest_coords); truelight@0: if (dist < ttfd->best_bird_dist) { truelight@0: ttfd->best_bird_dist = dist; truelight@0: ttfd->best_track = state[1]; truelight@0: } truelight@0: return false; truelight@0: } truelight@0: } truelight@0: truelight@0: static void FillWithStationData(TrainTrackFollowerData *fd, Vehicle *v) truelight@0: { dominik@103: fd->dest_coords = v->dest_tile; tron@555: if (v->current_order.type == OT_GOTO_STATION) tron@555: fd->station_index = v->current_order.station; dominik@103: else dominik@103: fd->station_index = -1; dominik@103: truelight@0: } truelight@0: truelight@0: static const byte _initial_tile_subcoord[6][4][3] = { truelight@0: {{ 15, 8, 1 },{ 0, 0, 0 },{ 0, 8, 5 },{ 0, 0, 0 }}, truelight@0: {{ 0, 0, 0 },{ 8, 0, 3 },{ 0, 0, 0 },{ 8,15, 7 }}, truelight@0: {{ 0, 0, 0 },{ 7, 0, 2 },{ 0, 7, 6 },{ 0, 0, 0 }}, truelight@0: {{ 15, 8, 2 },{ 0, 0, 0 },{ 0, 0, 0 },{ 8,15, 6 }}, truelight@0: {{ 15, 7, 0 },{ 8, 0, 4 },{ 0, 0, 0 },{ 0, 0, 0 }}, truelight@0: {{ 0, 0, 0 },{ 0, 0, 0 },{ 0, 8, 4 },{ 7,15, 0 }}, truelight@0: }; truelight@0: truelight@0: static const uint32 _reachable_tracks[4] = { truelight@0: 0x10091009, truelight@0: 0x00160016, truelight@0: 0x05200520, truelight@0: 0x2A002A00, truelight@0: }; truelight@0: truelight@0: static const byte _search_directions[6][4] = { truelight@0: { 0, 9, 2, 9 }, // track 1 truelight@0: { 9, 1, 9, 3 }, // track 2 truelight@0: { 9, 0, 3, 9 }, // track upper truelight@0: { 1, 9, 9, 2 }, // track lower truelight@0: { 3, 2, 9, 9 }, // track left truelight@0: { 9, 9, 1, 0 }, // track right truelight@0: }; truelight@0: truelight@0: static const byte _pick_track_table[6] = {1, 3, 2, 2, 0, 0}; matthijs@1247: #if PF_BENCHMARK matthijs@1247: unsigned int rdtsc() matthijs@1247: { matthijs@1247: unsigned int high, low; matthijs@1247: matthijs@1247: __asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high)); matthijs@1247: return low; matthijs@1247: } matthijs@1247: #endif matthijs@1247: truelight@0: truelight@0: /* choose a track */ matthijs@1247: static byte ChooseTrainTrack(Vehicle *v, uint tile, int enterdir, byte trackbits) truelight@193: { truelight@0: TrainTrackFollowerData fd; truelight@0: int bits = trackbits; truelight@0: uint best_track; matthijs@1247: #if PF_BENCHMARK truelight@0: int time = rdtsc(); truelight@0: static float f; truelight@0: #endif truelight@0: truelight@0: assert( (bits & ~0x3F) == 0); truelight@0: matthijs@1247: /* quick return in case only one possible track is available */ truelight@0: if (KILL_FIRST_BIT(bits) == 0) truelight@0: return FIND_FIRST_BIT(bits); truelight@0: matthijs@1247: if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */ matthijs@1247: NPFFindStationOrTileData fstd; matthijs@1247: NPFFoundTargetData ftd; matthijs@1247: byte trackdir; matthijs@1247: matthijs@1247: NPFFillWithOrderData(&fstd, v); matthijs@1247: /* The enterdir for the new tile, is the exitdir for the old tile */ matthijs@1247: trackdir = _track_exitdir_to_trackdir[FIND_FIRST_BIT(v->u.rail.track)][enterdir]; matthijs@1247: assert(trackdir != 0xff); matthijs@1247: matthijs@1330: ftd = NPFRouteToStationOrTile(tile - TileOffsByDir(enterdir), trackdir, &fstd, TRANSPORT_RAIL, v->owner); matthijs@1247: if (ftd.best_bird_dist != 0 || ftd.best_trackdir == 0xff) { matthijs@1247: /* Not found, or we are already there. Just do something */ matthijs@1247: //TODO: maybe display error? matthijs@1247: //TODO: go straight ahead if possible? truelight@0: best_track = FIND_FIRST_BIT(bits); truelight@0: } else { matthijs@1247: /* Discard enterdir information, making it a normal track */ matthijs@1247: best_track = ftd.best_trackdir & 7; /* TODO: Wrapper function? */ truelight@0: } truelight@0: } else { matthijs@1247: matthijs@1247: FillWithStationData(&fd, v); matthijs@1247: matthijs@1247: if (_patches.new_pathfinding) { matthijs@1247: /* New train pathfinding */ truelight@0: fd.best_bird_dist = (uint)-1; truelight@0: fd.best_track_dist = (uint)-1; matthijs@1247: fd.best_track = 0xFF; matthijs@1247: NewTrainPathfind(tile - TileOffsByDir(enterdir), enterdir, (TPFEnumProc*)TrainTrackFollower, &fd, NULL); matthijs@1247: matthijs@1247: // printf("Train %d %s\n", v->unitnumber, fd.best_track_dist == -1 ? "NOTFOUND" : "FOUND"); matthijs@1247: matthijs@1247: if (fd.best_track == 0xff) { matthijs@1247: // blaha matthijs@1247: best_track = FIND_FIRST_BIT(bits); matthijs@1247: } else { matthijs@1247: best_track = fd.best_track & 7; matthijs@1247: } matthijs@1247: } else { matthijs@1247: /* Original pathfinding */ matthijs@1247: int i, r; matthijs@1247: uint best_bird_dist = 0; matthijs@1247: uint best_track_dist = 0; matthijs@1247: byte train_dir = v->direction & 3; matthijs@1247: matthijs@1247: matthijs@1247: best_track = (uint)-1; matthijs@1247: matthijs@1247: do { matthijs@1247: i = FIND_FIRST_BIT(bits); matthijs@1247: bits = KILL_FIRST_BIT(bits); matthijs@1247: matthijs@1247: fd.best_bird_dist = (uint)-1; matthijs@1247: fd.best_track_dist = (uint)-1; matthijs@1247: matthijs@1247: NewTrainPathfind(tile, _search_directions[i][enterdir], (TPFEnumProc*)TrainTrackFollower, &fd, NULL); matthijs@1247: if (best_track != (uint)-1) { matthijs@1247: if (best_track_dist == (uint)-1) { matthijs@1247: if (fd.best_track_dist == (uint)-1) { matthijs@1247: /* neither reached the destination, pick the one with the smallest bird dist */ matthijs@1247: if (fd.best_bird_dist > best_bird_dist) goto bad; matthijs@1247: if (fd.best_bird_dist < best_bird_dist) goto good; matthijs@1247: } else { matthijs@1247: /* we found the destination for the first time */ matthijs@1247: goto good; matthijs@1247: } truelight@0: } else { matthijs@1247: if (fd.best_track_dist == (uint)-1) { matthijs@1247: /* didn't find destination, but we've found the destination previously */ matthijs@1247: goto bad; matthijs@1247: } else { matthijs@1247: /* both old & new reached the destination, compare track length */ matthijs@1247: if (fd.best_track_dist > best_track_dist) goto bad; matthijs@1247: if (fd.best_track_dist < best_track_dist) goto good; matthijs@1247: } truelight@0: } matthijs@1247: matthijs@1247: /* if we reach this position, there's two paths of equal value so far. matthijs@1247: * pick one randomly. */ matthijs@1247: r = (byte)Random(); matthijs@1247: if (_pick_track_table[i] == train_dir) r += 80; matthijs@1247: if (_pick_track_table[best_track] == train_dir) r -= 80; matthijs@1247: matthijs@1247: if (r <= 127) goto bad; truelight@0: } matthijs@1247: good:; matthijs@1247: best_track = i; matthijs@1247: best_bird_dist = fd.best_bird_dist; matthijs@1247: best_track_dist = fd.best_track_dist; matthijs@1247: bad:; matthijs@1247: } while (bits != 0); matthijs@1247: // printf("Train %d %s\n", v->unitnumber, best_track_dist == -1 ? "NOTFOUND" : "FOUND"); matthijs@1247: assert(best_track != (uint)-1); matthijs@1247: } truelight@0: } truelight@0: matthijs@1247: #if PF_BENCHMARK truelight@0: time = rdtsc() - time; truelight@0: f = f * 0.99 + 0.01 * time; truelight@0: printf("PF time = %d %f\n", time, f); truelight@0: #endif truelight@0: truelight@0: return best_track; truelight@0: } truelight@0: truelight@0: truelight@0: static bool CheckReverseTrain(Vehicle *v) truelight@0: { truelight@0: TrainTrackFollowerData fd; truelight@0: int i, r; truelight@0: int best_track; truelight@0: uint best_bird_dist = 0; truelight@0: uint best_track_dist = 0; truelight@0: uint reverse, reverse_best; truelight@0: truelight@0: if (_opt.diff.line_reverse_mode != 0 || truelight@0: v->u.rail.track & 0xC0 || truelight@0: !(v->direction & 1)) truelight@0: return false; truelight@0: truelight@0: FillWithStationData(&fd, v); truelight@0: truelight@0: best_track = -1; truelight@0: reverse_best = reverse = 0; truelight@0: truelight@0: assert(v->u.rail.track); truelight@0: truelight@0: i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][v->direction>>1]; truelight@0: matthijs@1247: if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */ matthijs@1247: NPFFindStationOrTileData fstd; matthijs@1247: NPFFoundTargetData ftd; matthijs@1247: byte trackdir, trackdir_rev; matthijs@1247: Vehicle* last = GetLastVehicleInChain(v); matthijs@1247: matthijs@1247: NPFFillWithOrderData(&fstd, v); matthijs@1247: matthijs@1247: trackdir = _track_direction_to_trackdir[FIND_FIRST_BIT(v->u.rail.track)][v->direction]; matthijs@1247: trackdir_rev = REVERSE_TRACKDIR(_track_direction_to_trackdir[FIND_FIRST_BIT(last->u.rail.track)][last->direction]); matthijs@1247: assert(trackdir != 0xff); matthijs@1247: assert(trackdir_rev != 0xff); matthijs@1247: matthijs@1330: ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, last->tile, trackdir_rev, &fstd, TRANSPORT_RAIL, v->owner); matthijs@1247: if (ftd.best_bird_dist != 0) { matthijs@1247: /* We didn't find anything, just keep on going straight ahead */ matthijs@1247: reverse_best = false; matthijs@1247: } else { matthijs@1247: if (ftd.node.user_data[NPF_NODE_FLAGS] & NPF_FLAG_REVERSE) matthijs@1247: reverse_best = true; matthijs@1247: else matthijs@1247: reverse_best = false; matthijs@1247: } matthijs@1247: } else { matthijs@1247: while(true) { matthijs@1247: fd.best_bird_dist = (uint)-1; matthijs@1247: fd.best_track_dist = (uint)-1; matthijs@1247: matthijs@1247: NewTrainPathfind(v->tile, reverse ^ i, (TPFEnumProc*)TrainTrackFollower, &fd, NULL); matthijs@1247: matthijs@1247: if (best_track != -1) { matthijs@1247: if (best_bird_dist != 0) { matthijs@1247: if (fd.best_bird_dist != 0) { matthijs@1247: /* neither reached the destination, pick the one with the smallest bird dist */ matthijs@1247: if (fd.best_bird_dist > best_bird_dist) goto bad; matthijs@1247: if (fd.best_bird_dist < best_bird_dist) goto good; matthijs@1247: } else { matthijs@1247: /* we found the destination for the first time */ matthijs@1247: goto good; matthijs@1247: } truelight@0: } else { matthijs@1247: if (fd.best_bird_dist != 0) { matthijs@1247: /* didn't find destination, but we've found the destination previously */ matthijs@1247: goto bad; matthijs@1247: } else { matthijs@1247: /* both old & new reached the destination, compare track length */ matthijs@1247: if (fd.best_track_dist > best_track_dist) goto bad; matthijs@1247: if (fd.best_track_dist < best_track_dist) goto good; matthijs@1247: } truelight@0: } matthijs@1247: matthijs@1247: /* if we reach this position, there's two paths of equal value so far. matthijs@1247: * pick one randomly. */ matthijs@1247: r = (byte)Random(); matthijs@1247: if (_pick_track_table[i] == (v->direction & 3)) r += 80; matthijs@1247: if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80; matthijs@1247: if (r <= 127) goto bad; truelight@0: } matthijs@1247: good:; matthijs@1247: best_track = i; matthijs@1247: best_bird_dist = fd.best_bird_dist; matthijs@1247: best_track_dist = fd.best_track_dist; matthijs@1247: reverse_best = reverse; matthijs@1247: bad:; matthijs@1247: if (reverse != 0) matthijs@1247: break; matthijs@1247: reverse = 2; truelight@0: } truelight@0: } truelight@0: truelight@0: return reverse_best != 0; truelight@0: } truelight@0: truelight@0: static bool ProcessTrainOrder(Vehicle *v) truelight@0: { truelight@1024: const Order *order; truelight@0: bool result; truelight@0: truelight@0: // These are un-interruptible tron@555: if (v->current_order.type >= OT_GOTO_DEPOT && tron@555: v->current_order.type <= OT_LEAVESTATION) { truelight@1024: // Let a depot order in the orderlist interrupt. tron@555: if (v->current_order.type != OT_GOTO_DEPOT || tron@555: !(v->current_order.flags & OF_UNLOAD)) truelight@0: return false; truelight@0: } truelight@0: tron@555: if (v->current_order.type == OT_GOTO_DEPOT && tron@555: (v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) && bjarni@1208: !VehicleNeedsService(v) && !v->set_for_replacement) { truelight@0: v->cur_order_index++; truelight@0: } truelight@0: darkvater@395: // check if we've reached the waypoint? tron@555: if (v->current_order.type == OT_GOTO_WAYPOINT && v->tile == v->dest_tile) { truelight@0: v->cur_order_index++; truelight@0: } truelight@0: truelight@193: // check if we've reached a non-stop station while TTDPatch nonstop is enabled.. tron@555: if (_patches.new_nonstop && v->current_order.flags & OF_NON_STOP && celestar@1337: v->current_order.station == _map2[v->tile] && IsTileType(v->tile, MP_STATION) ) { truelight@193: v->cur_order_index++; truelight@0: } truelight@0: truelight@0: // Get the current order truelight@0: if (v->cur_order_index >= v->num_orders) truelight@0: v->cur_order_index = 0; truelight@1024: truelight@1024: order = GetVehicleOrder(v, v->cur_order_index); truelight@0: truelight@0: // If no order, do nothing. truelight@1024: if (order == NULL) { tron@555: v->current_order.type = OT_NOTHING; tron@555: v->current_order.flags = 0; truelight@0: v->dest_tile = 0; truelight@0: return false; truelight@0: } truelight@0: truelight@0: // If it is unchanged, keep it. truelight@1024: if (order->type == v->current_order.type && truelight@1024: order->flags == v->current_order.flags && truelight@1024: order->station == v->current_order.station) truelight@0: return false; truelight@0: truelight@0: // Otherwise set it, and determine the destination tile. truelight@1024: v->current_order = *order; truelight@0: truelight@0: v->dest_tile = 0; truelight@0: truelight@0: result = false; truelight@1024: switch (order->type) { truelight@1024: case OT_GOTO_STATION: truelight@1024: if (order->station == v->last_station_visited) truelight@1266: v->last_station_visited = INVALID_STATION; truelight@1024: v->dest_tile = GetStation(order->station)->xy; truelight@1024: result = CheckReverseTrain(v); truelight@1024: break; truelight@1024: truelight@1024: case OT_GOTO_DEPOT: truelight@1313: v->dest_tile = GetDepot(order->station)->xy; truelight@1024: result = CheckReverseTrain(v); truelight@1024: break; truelight@1024: truelight@1024: case OT_GOTO_WAYPOINT: truelight@1024: v->dest_tile = _waypoints[order->station].xy; truelight@1024: result = CheckReverseTrain(v); truelight@1024: break; truelight@0: } truelight@0: truelight@1024: InvalidateVehicleOrder(v); truelight@0: truelight@0: return result; truelight@0: } truelight@0: truelight@0: static void MarkTrainDirty(Vehicle *v) truelight@0: { truelight@0: do { truelight@0: v->cur_image = GetTrainImage(v, v->direction); truelight@0: MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); truelight@0: } while ( (v=v->next) != NULL); truelight@0: } truelight@0: truelight@0: static void HandleTrainLoading(Vehicle *v, bool mode) truelight@0: { tron@555: if (v->current_order.type == OT_NOTHING) truelight@0: return; truelight@193: tron@555: if (v->current_order.type != OT_DUMMY) { tron@555: if (v->current_order.type != OT_LOADING) truelight@0: return; truelight@0: truelight@0: if (mode) truelight@0: return; truelight@0: truelight@0: // don't mark the train as lost if we're loading on the final station. tron@555: if (v->current_order.flags & OF_NON_STOP) truelight@0: v->u.rail.days_since_order_progr = 0; truelight@0: truelight@0: if (--v->load_unload_time_rem) truelight@0: return; truelight@0: tron@555: if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) { matthijs@882: v->u.rail.days_since_order_progr = 0; /* Prevent a train lost message for full loading trains */ truelight@0: SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC); truelight@0: if (LoadUnloadVehicle(v)) { truelight@0: InvalidateWindow(WC_TRAINS_LIST, v->owner); truelight@0: MarkTrainDirty(v); truelight@193: truelight@0: // need to update acceleration since the goods on the train changed. truelight@0: UpdateTrainAcceleration(v); truelight@0: } truelight@0: return; truelight@0: } truelight@193: truelight@0: TrainPlayLeaveStationSound(v); truelight@193: truelight@0: { tron@555: Order b = v->current_order; tron@555: v->current_order.type = OT_LEAVESTATION; tron@555: v->current_order.flags = 0; truelight@193: bjarni@1208: if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) { bjarni@1208: // only the vehicle owner needs to calculate the rest (locally) bjarni@1208: if ((_autoreplace_array[v->engine_type] != v->engine_type) || bjarni@1208: (_patches.autorenew && v->age - v->max_age > (_patches.autorenew_months * 30))) { bjarni@1208: byte flags = 1; bjarni@1208: // the flags means, bit 0 = needs to go to depot, bit 1 = have depot in orders bjarni@1208: if (VehicleHasDepotOrders(v)) SETBIT(flags, 1); bjarni@1208: if (!(HASBIT(flags, 1) && v->set_for_replacement)) { bjarni@1208: _current_player = _local_player; bjarni@1208: DoCommandP(v->tile, v->index, flags, NULL, CMD_TRAIN_GOTO_DEPOT | CMD_SHOW_NO_ERROR); bjarni@1208: _current_player = OWNER_NONE; bjarni@1208: } bjarni@1208: } else { // no need to go to a depot bjarni@1208: if (v->set_for_replacement) { bjarni@1208: // it seems that the user clicked "Stop replacing" bjarni@1208: _current_player = _local_player; bjarni@1208: DoCommandP(v->tile, v->index, 1 | (1 << 2), NULL, CMD_TRAIN_GOTO_DEPOT | CMD_SHOW_NO_ERROR); bjarni@1208: _current_player = OWNER_NONE; bjarni@1208: } bjarni@1208: } bjarni@1208: } truelight@0: // If this was not the final order, don't remove it from the list. tron@555: if (!(b.flags & OF_NON_STOP)) truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: v->u.rail.days_since_order_progr = 0; truelight@0: v->cur_order_index++; truelight@1024: InvalidateVehicleOrder(v); truelight@0: } truelight@0: truelight@0: static int UpdateTrainSpeed(Vehicle *v) truelight@0: { truelight@0: uint spd; truelight@0: uint accel; truelight@0: truelight@954: if (v->vehstatus & VS_STOPPED || HASBIT(v->u.rail.flags, VRF_REVERSING)) { celestar@1179: if (_patches.realistic_acceleration) celestar@1179: accel = GetTrainAcceleration(v, AM_BRAKE) * 2; celestar@1179: else celestar@1179: accel = v->acceleration * -2; truelight@0: } else { celestar@1179: if (_patches.realistic_acceleration) celestar@1179: accel = GetTrainAcceleration(v, AM_ACCEL); celestar@1179: else celestar@1179: accel = v->acceleration; truelight@0: } truelight@0: truelight@0: spd = v->subspeed + accel * 2; truelight@0: v->subspeed = (byte)spd; celestar@1179: { celestar@1179: int tempmax = v->max_speed; celestar@1179: if (v->cur_speed > v->max_speed) celestar@1179: tempmax = v->cur_speed - (v->cur_speed / 10) - 1; celestar@1179: v->cur_speed = spd = clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax); celestar@1179: } truelight@0: truelight@0: if (!(v->direction & 1)) spd = spd * 3 >> 2; truelight@0: truelight@0: spd += v->progress; truelight@0: v->progress = (byte)spd; truelight@0: return (spd >> 8); truelight@0: } truelight@0: truelight@0: static void TrainEnterStation(Vehicle *v, int station) truelight@0: { truelight@0: Station *st; truelight@0: uint32 flags; truelight@0: truelight@0: v->last_station_visited = station; truelight@0: truelight@0: /* check if a train ever visited this station before */ truelight@919: st = GetStation(station); truelight@0: if (!(st->had_vehicle_of_type & HVOT_TRAIN)) { truelight@0: st->had_vehicle_of_type |= HVOT_TRAIN; tron@534: SetDParam(0, st->index); truelight@0: 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); truelight@0: AddNewsItem( truelight@0: STR_8801_CITIZENS_CELEBRATE_FIRST, truelight@0: flags, truelight@0: v->index, truelight@0: 0); truelight@0: } truelight@0: truelight@0: // Did we reach the final destination? tron@555: if (v->current_order.type == OT_GOTO_STATION && tron@555: v->current_order.station == (byte)station) { truelight@0: // Yeah, keep the load/unload flags truelight@0: // Non Stop now means if the order should be increased. tron@555: v->current_order.type = OT_LOADING; tron@555: v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD; tron@555: v->current_order.flags |= OF_NON_STOP; truelight@0: } else { truelight@0: // No, just do a simple load tron@555: v->current_order.type = OT_LOADING; tron@555: v->current_order.flags = 0; truelight@0: } tron@555: v->current_order.station = 0; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC); truelight@0: if (LoadUnloadVehicle(v) != 0) { truelight@0: InvalidateWindow(WC_TRAINS_LIST, v->owner); truelight@0: MarkTrainDirty(v); truelight@0: UpdateTrainAcceleration(v); truelight@0: } darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: truelight@954: static byte AfterSetTrainPos(Vehicle *v, bool new_tile) truelight@0: { truelight@0: byte new_z, old_z; truelight@193: truelight@0: // need this hint so it returns the right z coordinate on bridges. truelight@0: _get_z_hint = v->z_pos; truelight@0: new_z = GetSlopeZ(v->x_pos, v->y_pos); truelight@0: _get_z_hint = 0; truelight@0: truelight@0: old_z = v->z_pos; truelight@0: v->z_pos = new_z; truelight@0: truelight@954: if (new_tile) { truelight@954: CLRBIT(v->u.rail.flags, VRF_GOINGUP); truelight@954: CLRBIT(v->u.rail.flags, VRF_GOINGDOWN); truelight@954: truelight@954: if (new_z != old_z) { truelight@954: SETBIT(v->u.rail.flags, (new_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN); truelight@954: } truelight@0: } truelight@0: truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: return old_z; truelight@0: } truelight@0: truelight@0: static const byte _new_vehicle_direction_table[11] = { truelight@0: 0, 7, 6, 0, truelight@0: 1, 0, 5, 0, truelight@0: 2, 3, 4, truelight@0: }; truelight@0: truelight@0: static int GetNewVehicleDirectionByTile(uint new_tile, uint old_tile) truelight@0: { tron@926: uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 + tron@926: TileX(new_tile) - TileX(old_tile) + 1; truelight@0: assert(offs < 11); truelight@0: return _new_vehicle_direction_table[offs]; truelight@0: } truelight@0: truelight@0: static int GetNewVehicleDirection(Vehicle *v, int x, int y) truelight@0: { truelight@0: uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); truelight@0: assert(offs < 11); truelight@0: return _new_vehicle_direction_table[offs]; truelight@0: } truelight@0: truelight@0: static int GetDirectionToVehicle(Vehicle *v, int x, int y) truelight@0: { truelight@0: byte offs; truelight@0: truelight@0: x -= v->x_pos; truelight@0: if (x >= 0) { truelight@0: offs = (x > 2) ? 0 : 1; truelight@0: } else { truelight@0: offs = (x < -2) ? 2 : 1; truelight@0: } truelight@0: truelight@0: y -= v->y_pos; truelight@0: if (y >= 0) { truelight@0: offs += ((y > 2) ? 0 : 1) * 4; truelight@0: } else { truelight@0: offs += ((y < -2) ? 2 : 1) * 4; truelight@0: } truelight@0: truelight@0: assert(offs < 11); truelight@0: return _new_vehicle_direction_table[offs]; truelight@0: } truelight@0: truelight@0: /* Check if the vehicle is compatible with the specified tile */ tron@1048: static bool CheckCompatibleRail(const Vehicle *v, TileIndex tile) truelight@0: { tron@1214: switch (GetTileType(tile)) { tron@1048: case MP_RAILWAY: tron@1048: case MP_STATION: tron@1048: // normal tracks, jump to owner check tron@1048: break; tron@1048: tron@1048: case MP_TUNNELBRIDGE: tron@1048: if ((_map5[tile] & 0xC0) == 0xC0) { // is bridge middle part? tron@1192: uint height; tron@1192: uint tileh = GetTileSlope(tile, &height); tron@1048: tron@1048: // correct Z position of a train going under a bridge on slopes tron@1394: if (CorrectZ(tileh)) height += 8; tron@1192: tron@1192: if (v->z_pos != height) return true; // train is going over bridge tron@1048: } tron@1048: break; tron@1048: tron@1048: case MP_STREET: tron@1048: // tracks over roads, do owner check of tracks (_map_owner[tile]) tron@1048: return tron@1048: _map_owner[tile] == v->owner && bjarni@1067: (v->subtype != TS_Front_Engine || (_map3_hi[tile] & 0xF) == v->u.rail.railtype); tron@1048: tron@1048: default: tron@1048: return true; tron@1048: } tron@1048: tron@1048: return tron@1048: _map_owner[tile] == v->owner && bjarni@1067: (v->subtype != TS_Front_Engine || (_map3_lo[tile] & 0xF) == v->u.rail.railtype); truelight@0: } truelight@0: truelight@0: typedef struct { truelight@0: byte small_turn, large_turn; truelight@0: byte z_up; // fraction to remove when moving up truelight@0: byte z_down; // fraction to remove when moving down truelight@0: } RailtypeSlowdownParams; truelight@0: truelight@0: static const RailtypeSlowdownParams _railtype_slowdown[3] = { truelight@0: // normal accel truelight@0: {256/4, 256/2, 256/4, 2}, // normal truelight@0: {256/4, 256/2, 256/4, 2}, // monorail truelight@0: {0, 256/2, 256/4, 2}, // maglev truelight@0: }; truelight@0: truelight@0: /* Modify the speed of the vehicle due to a turn */ truelight@0: static void AffectSpeedByDirChange(Vehicle *v, byte new_dir) truelight@0: { truelight@0: byte diff; truelight@0: const RailtypeSlowdownParams *rsp; truelight@0: truelight@0: if (_patches.realistic_acceleration || (diff = (v->direction - new_dir) & 7) == 0) truelight@0: return; truelight@0: truelight@0: rsp = &_railtype_slowdown[v->u.rail.railtype]; truelight@0: v->cur_speed -= ((diff == 1 || diff == 7) ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8; truelight@0: } truelight@0: truelight@0: /* Modify the speed of the vehicle due to a change in altitude */ truelight@0: static void AffectSpeedByZChange(Vehicle *v, byte old_z) truelight@0: { truelight@0: const RailtypeSlowdownParams *rsp; truelight@0: if (old_z == v->z_pos || _patches.realistic_acceleration) truelight@0: return; truelight@0: truelight@0: rsp = &_railtype_slowdown[v->u.rail.railtype]; truelight@0: truelight@0: if (old_z < v->z_pos) { truelight@0: v->cur_speed -= (v->cur_speed * rsp->z_up >> 8); truelight@0: } else { truelight@0: uint16 spd = v->cur_speed + rsp->z_down; truelight@0: if (spd <= v->max_speed) truelight@0: v->cur_speed = spd; truelight@0: } truelight@0: } truelight@0: truelight@0: static const byte _otherside_signal_directions[14] = { truelight@0: 1, 3, 1, 3, 5, 3, 0, 0, truelight@0: 5, 7, 7, 5, 7, 1, truelight@0: }; truelight@0: truelight@0: static void TrainMovedChangeSignals(uint tile, int dir) truelight@0: { truelight@0: int i; tron@1035: if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xC0) == 0x40) { truelight@0: i = FindFirstBit2x64((_map5[tile]+(_map5[tile]<<8)) & _reachable_tracks[dir]); truelight@0: UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]); truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: typedef struct TrainCollideChecker { truelight@0: Vehicle *v, *v_skip; truelight@0: truelight@0: } TrainCollideChecker; truelight@0: tron@1095: static void *FindTrainCollideEnum(Vehicle *v, TrainCollideChecker *tcc) truelight@0: { truelight@0: if (v == tcc->v || v == tcc->v_skip || v->type != VEH_Train || v->u.rail.track==0x80) truelight@0: return 0; truelight@0: truelight@0: if ( myabs(v->z_pos - tcc->v->z_pos) > 6 || truelight@0: myabs(v->x_pos - tcc->v->x_pos) >= 6 || truelight@0: myabs(v->y_pos - tcc->v->y_pos) >= 6) truelight@0: return NULL; truelight@0: return v; truelight@0: } truelight@0: truelight@0: static void SetVehicleCrashed(Vehicle *v) truelight@0: { truelight@0: Vehicle *u; truelight@0: truelight@0: if (v->u.rail.crash_anim_pos != 0) truelight@0: return; truelight@0: truelight@0: v->u.rail.crash_anim_pos++; truelight@193: truelight@0: u = v; truelight@0: BEGIN_ENUM_WAGONS(v) truelight@0: v->vehstatus |= VS_CRASHED; truelight@0: END_ENUM_WAGONS(v) truelight@0: darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, STATUS_BAR); truelight@0: } truelight@0: truelight@0: static int CountPassengersInTrain(Vehicle *v) truelight@0: { truelight@193: int num = 0; truelight@0: BEGIN_ENUM_WAGONS(v) bjarni@1067: if (v->cargo_type == CT_PASSENGERS) num += v->cargo_count; truelight@0: END_ENUM_WAGONS(v) truelight@0: return num; truelight@0: } truelight@0: darkvater@22: /* darkvater@22: * Checks whether the specified tried has a collision with another vehicle. If bjarni@1067: * so, destroys this vehicle, and the other vehicle if its subtype is 0 (TS_Front_Engine). darkvater@22: * Reports the incident in a flashy news item, modifies station ratings and darkvater@22: * plays a sound. darkvater@22: */ truelight@0: static void CheckTrainCollision(Vehicle *v) truelight@0: { truelight@0: TrainCollideChecker tcc; dominik@98: Vehicle *coll,*realcoll; truelight@0: int num; truelight@0: truelight@0: /* can't collide in depot */ truelight@0: if (v->u.rail.track == 0x80) truelight@0: return; truelight@193: truelight@193: if ( !(v->u.rail.track == 0x40) ) dominik@98: assert((uint)TILE_FROM_XY(v->x_pos, v->y_pos) == v->tile); truelight@0: truelight@0: tcc.v = v; truelight@0: tcc.v_skip = v->next; truelight@0: truelight@0: /* find colliding vehicle */ dominik@98: realcoll = coll = VehicleFromPos(TILE_FROM_XY(v->x_pos, v->y_pos), &tcc, (VehicleFromPosProc*)FindTrainCollideEnum); truelight@0: if (coll == NULL) truelight@0: return; truelight@193: truelight@193: truelight@0: coll = GetFirstVehicleInChain(coll); truelight@193: truelight@0: /* it can't collide with its own wagons */ dominik@98: if ( (v == coll) || ( (v->u.rail.track & 0x40) && ( (v->direction & 2) != (realcoll->direction & 2) ) ) ) truelight@0: return; truelight@0: truelight@193: //two drivers + passangers killed in train v truelight@0: num = 2 + CountPassengersInTrain(v); truelight@193: if(!(coll->vehstatus&VS_CRASHED)) truelight@0: //two drivers + passangers killed in train coll (if it was not crashed already) truelight@0: num += 2 + CountPassengersInTrain(coll); truelight@0: truelight@0: SetVehicleCrashed(v); bjarni@1067: if (coll->subtype == TS_Front_Engine) truelight@0: SetVehicleCrashed(coll); truelight@193: truelight@193: tron@534: SetDParam(0, num); truelight@193: truelight@0: AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL, truelight@0: NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0), truelight@0: v->index, truelight@0: 0); truelight@0: truelight@0: ModifyStationRatingAround(v->tile, v->owner, -160, 30); tron@541: SndPlayVehicleFx(SND_13_BIG_CRASH, v); truelight@0: } truelight@0: truelight@0: static void *CheckVehicleAtSignal(Vehicle *v, void *data) truelight@0: { truelight@0: uint32 d = (uint32)data; truelight@0: bjarni@1067: if (v->type == VEH_Train && v->subtype == TS_Front_Engine && v->tile == (TileIndex)(d >> 8)) { truelight@0: byte diff = (v->direction - (byte)d + 2) & 7; truelight@0: if (diff == 2 || (v->cur_speed <= 5 && diff <= 4)) truelight@0: return (void*)1; truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: truelight@0: static void TrainController(Vehicle *v) truelight@0: { truelight@0: Vehicle *prev = NULL; truelight@0: GetNewVehiclePosResult gp; truelight@0: uint32 r, tracks,ts; matthijs@1247: int i, enterdir, newdir, dir; truelight@0: byte chosen_dir; truelight@0: byte chosen_track; truelight@0: byte old_z; truelight@0: darkvater@22: /* For every vehicle after and including the given vehicle */ truelight@0: for(;;) { truelight@0: BeginVehicleMove(v); truelight@193: truelight@0: if (v->u.rail.track != 0x40) { darkvater@22: /* Not inside tunnel */ truelight@0: if (GetNewVehiclePos(v, &gp)) { darkvater@22: /* Staying in the old tile */ truelight@0: if (v->u.rail.track == 0x80) { truelight@0: /* inside depot */ truelight@0: gp.x = v->x_pos; truelight@0: gp.y = v->y_pos; truelight@0: } else { darkvater@22: /* is not inside depot */ truelight@742: truelight@742: if (!TrainCheckIfLineEnds(v)) truelight@742: return; truelight@742: truelight@0: r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); matthijs@1247: if (r & 0x8) { matthijs@1247: //debug("%x & 0x8", r); truelight@0: goto invalid_rail; matthijs@1247: } truelight@0: if (r & 0x2) { truelight@0: TrainEnterStation(v, r >> 8); truelight@0: return; truelight@0: } truelight@0: tron@555: if (v->current_order.type == OT_LEAVESTATION) { tron@555: v->current_order.type = OT_NOTHING; tron@555: v->current_order.flags = 0; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: } truelight@0: } else { truelight@0: /* A new tile is about to be entered. */ truelight@0: matthijs@1247: byte bits; truelight@0: /* Determine what direction we're entering the new tile from */ truelight@0: dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile); matthijs@1247: enterdir = dir >> 1; matthijs@1247: assert(enterdir==0 || enterdir==1 || enterdir==2 || enterdir==3); truelight@193: truelight@0: /* Get the status of the tracks in the new tile and mask truelight@0: * away the bits that aren't reachable. */ matthijs@1247: ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL) & _reachable_tracks[enterdir]; truelight@0: truelight@0: /* Combine the from & to directions. truelight@0: * Now, the lower byte contains the track status, and the byte at bit 16 contains truelight@0: * the signal status. */ truelight@0: tracks = ts|(ts >> 8); matthijs@1247: bits = tracks & 0xFF; matthijs@1247: if (_patches.new_pathfinding_all && _patches.forbid_90_deg && prev == NULL) matthijs@1247: /* We allow wagons to make 90 deg turns, because forbid_90_deg matthijs@1247: * can be switched on halfway a turn */ matthijs@1247: bits &= ~_track_crosses_tracks[FIND_FIRST_BIT(v->u.rail.track)]; matthijs@1247: matthijs@1247: if ( bits == 0) { matthijs@1247: //debug("%x == 0", bits); truelight@0: goto invalid_rail; matthijs@1247: } truelight@0: truelight@0: /* Check if the new tile contrains tracks that are compatible truelight@0: * with the current train, if not, bail out. */ matthijs@1247: if (!CheckCompatibleRail(v, gp.new_tile)) { matthijs@1247: //debug("!CheckCompatibleRail(%p, %x)", v, gp.new_tile); truelight@0: goto invalid_rail; matthijs@1247: } truelight@0: truelight@0: if (prev == NULL) { truelight@0: /* Currently the locomotive is active. Determine which one of the truelight@0: * available tracks to choose */ matthijs@1247: chosen_track = 1 << ChooseTrainTrack(v, gp.new_tile, enterdir, bits); matthijs@1247: assert(chosen_track & tracks); truelight@0: truelight@0: /* Check if it's a red signal and that force proceed is not clicked. */ truelight@0: if ( (tracks>>16)&chosen_track && v->u.rail.force_proceed == 0) goto red_light; truelight@0: } else { truelight@0: static byte _matching_tracks[8] = {0x30, 1, 0xC, 2, 0x30, 1, 0xC, 2}; truelight@193: truelight@0: /* The wagon is active, simply follow the prev vehicle. */ matthijs@1247: chosen_track = (byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & bits); truelight@0: } truelight@0: truelight@0: /* make sure chosen track is a valid track */ truelight@0: assert(chosen_track==1 || chosen_track==2 || chosen_track==4 || chosen_track==8 || chosen_track==16 || chosen_track==32); truelight@0: truelight@0: /* Update XY to reflect the entrance to the new tile, and select the direction to use */ truelight@0: { matthijs@1247: const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir]; truelight@0: gp.x = (gp.x & ~0xF) | b[0]; truelight@0: gp.y = (gp.y & ~0xF) | b[1]; truelight@0: chosen_dir = b[2]; truelight@0: } truelight@193: truelight@0: /* Call the landscape function and tell it that the vehicle entered the tile */ truelight@0: r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); matthijs@1247: if (r&0x8){ matthijs@1247: //debug("%x & 0x8", r); truelight@0: goto invalid_rail; matthijs@1247: } truelight@0: bjarni@1067: if (v->subtype == TS_Front_Engine) v->load_unload_time_rem = 0; truelight@0: truelight@0: if (!(r&0x4)) { truelight@0: v->tile = gp.new_tile; truelight@0: v->u.rail.track = chosen_track; matthijs@1330: assert(v->u.rail.track); truelight@0: } truelight@0: bjarni@1067: if (v->subtype == TS_Front_Engine) matthijs@1247: TrainMovedChangeSignals(gp.new_tile, enterdir); truelight@0: darkvater@22: /* Signals can only change when the first darkvater@22: * (above) or the last vehicle moves. */ truelight@0: if (v->next == NULL) matthijs@1247: TrainMovedChangeSignals(gp.old_tile, (enterdir) ^ 2); truelight@193: truelight@0: if (prev == NULL) { truelight@0: AffectSpeedByDirChange(v, chosen_dir); truelight@0: } truelight@0: truelight@0: v->direction = chosen_dir; truelight@0: } truelight@0: } else { truelight@0: /* in tunnel */ truelight@0: GetNewVehiclePos(v, &gp); truelight@193: tron@1035: if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && truelight@0: !(_map5[gp.new_tile] & 0xF0)) { truelight@0: r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); truelight@0: if (r & 0x4) goto common; truelight@0: } truelight@0: truelight@0: v->x_pos = gp.x; truelight@0: v->y_pos = gp.y; truelight@0: VehiclePositionChanged(v); dominik@98: if (prev == NULL) dominik@98: CheckTrainCollision(v); truelight@0: goto next_vehicle; truelight@0: } truelight@0: common:; truelight@0: truelight@0: /* update image of train, as well as delta XY */ matthijs@1247: newdir = GetNewVehicleDirection(v, gp.x, gp.y); matthijs@1247: UpdateTrainDeltaXY(v, newdir); matthijs@1247: v->cur_image = GetTrainImage(v, newdir); truelight@0: truelight@0: v->x_pos = gp.x; truelight@0: v->y_pos = gp.y; truelight@0: truelight@0: /* update the Z position of the vehicle */ truelight@954: old_z = AfterSetTrainPos(v, (gp.new_tile != gp.old_tile)); truelight@193: truelight@0: if (prev == NULL) { darkvater@22: /* This is the first vehicle in the train */ truelight@0: AffectSpeedByZChange(v, old_z); truelight@0: CheckTrainCollision(v); truelight@0: } truelight@0: darkvater@22: next_vehicle:; truelight@0: /* continue with next vehicle */ truelight@0: prev = v; truelight@0: if ((v=v->next) == NULL) truelight@0: return; truelight@0: } truelight@0: truelight@0: invalid_rail: darkvater@22: /* We've reached end of line?? */ truelight@0: if (prev != NULL) { truelight@0: error("!Disconnecting train"); truelight@0: } truelight@0: goto reverse_train_direction; truelight@0: truelight@0: red_light: { darkvater@22: /* We're in front of a red signal ?? */ truelight@0: /* find the first set bit in ts. need to do it in 2 steps, since truelight@0: * FIND_FIRST_BIT only handles 6 bits at a time. */ truelight@0: i = FindFirstBit2x64(ts); truelight@193: matthijs@1247: if (!(_map3_lo[gp.new_tile] & _signal_against_trackdir[i])) { truelight@0: v->cur_speed = 0; truelight@0: v->subspeed = 0; truelight@0: v->progress = 255-100; truelight@0: if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20) truelight@0: return; matthijs@1247: } else if (_map3_lo[gp.new_tile] & _signal_along_trackdir[i]){ truelight@0: v->cur_speed = 0; truelight@0: v->subspeed = 0; truelight@0: v->progress = 255-10; truelight@0: if (++v->load_unload_time_rem < _patches.wait_twoway_signal * 73) { matthijs@1247: uint o_tile = gp.new_tile + TileOffsByDir(enterdir); truelight@0: /* check if a train is waiting on the other side */ truelight@0: if (VehicleFromPos(o_tile, (void*)( (o_tile<<8) | (dir^4)), (VehicleFromPosProc*)CheckVehicleAtSignal) == NULL) truelight@0: return; truelight@0: } truelight@0: } truelight@0: } truelight@193: truelight@0: reverse_train_direction: truelight@0: v->load_unload_time_rem = 0; truelight@0: v->cur_speed = 0; truelight@0: v->subspeed = 0; truelight@0: ReverseTrainDirection(v); truelight@0: truelight@0: } truelight@0: tron@1430: extern TileIndex CheckTunnelBusy(TileIndex tile, uint *length); dominik@98: Darkvater@1418: /** Darkvater@1418: * Deletes/Clears the last wagon of a crashed train. It takes the engine of the Darkvater@1418: * train, then goes to the last wagon and deletes that. Each call to this function Darkvater@1418: * will remove the last wagon of a crashed train. If this wagon was on a crossing, Darkvater@1418: * or inside a tunnel, recalculate the signals as they might need updating Darkvater@1418: * @param v the @Vehicle of which last wagon is to be removed Darkvater@1418: */ truelight@0: static void DeleteLastWagon(Vehicle *v) truelight@0: { truelight@0: Vehicle *u = v; Darkvater@1418: Darkvater@1418: /* Go to the last wagon and delete the link pointing there Darkvater@1418: * *u is then the one-before-last wagon, and *v the last Darkvater@1418: * one which will physicially be removed */ truelight@0: while (v->next != NULL) { truelight@0: u = v; truelight@0: v = v->next; truelight@0: } truelight@0: u->next = NULL; truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: DeleteWindowById(WC_VEHICLE_VIEW, v->index); tron@588: RebuildVehicleLists(); truelight@0: InvalidateWindow(WC_COMPANY, v->owner); truelight@0: truelight@0: BeginVehicleMove(v); truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(v); truelight@0: Darkvater@1418: if (!(v->u.rail.track & 0xC0)) Darkvater@1418: SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(v->u.rail.track)); truelight@193: dominik@1103: /* Check if the wagon was on a road/rail-crossing and disable it if no others are on it */ dominik@1103: DisableTrainCrossing(v->tile); dominik@1103: Darkvater@1418: if (v->u.rail.track == 0x40) { // inside a tunnel tron@1430: TileIndex endtile = CheckTunnelBusy(v->tile, NULL); tron@1430: tron@1430: if (endtile == INVALID_TILE) // tunnel is busy (error returned) Darkvater@1418: return; Darkvater@1418: tron@1431: switch (v->direction) { tron@1431: case 1: tron@1431: case 5: tron@1431: SetSignalsOnBothDir(v->tile, 0); tron@1431: SetSignalsOnBothDir(endtile, 0); tron@1431: break; tron@1431: tron@1431: case 3: tron@1431: case 7: tron@1431: SetSignalsOnBothDir(v->tile, 1); tron@1431: SetSignalsOnBothDir(endtile, 1); tron@1431: break; tron@1431: tron@1431: default: tron@1431: break; tron@1431: } dominik@98: } truelight@0: } truelight@0: truelight@0: static void ChangeTrainDirRandomly(Vehicle *v) truelight@0: { truelight@0: static int8 _random_dir_change[4] = { -1, 0, 0, 1}; truelight@193: truelight@0: do { dominik@98: //I need to buffer the train direction darkvater@288: if (!(v->u.rail.track & 0x40)) celestar@1137: v->direction = (v->direction + _random_dir_change[Random()&3]) & 7; truelight@0: if (!(v->vehstatus & VS_HIDDEN)) { truelight@0: BeginVehicleMove(v); truelight@0: UpdateTrainDeltaXY(v, v->direction); truelight@0: v->cur_image = GetTrainImage(v, v->direction); truelight@954: AfterSetTrainPos(v, false); truelight@0: } truelight@0: } while ( (v=v->next) != NULL); truelight@0: } truelight@0: truelight@0: static void HandleCrashedTrain(Vehicle *v) truelight@0: { truelight@0: int state = ++v->u.rail.crash_anim_pos, index; truelight@0: uint32 r; truelight@0: Vehicle *u; truelight@193: dominik@98: if ( (state == 4) && (v->u.rail.track != 0x40) ) { tron@1359: CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE); truelight@0: } truelight@0: celestar@1137: if (state <= 200 && (uint16)(r=Random()) <= 0x2492) { truelight@0: index = (r * 10 >> 16); truelight@0: truelight@0: u = v; truelight@0: do { truelight@0: if (--index < 0) { celestar@1137: r = Random(); truelight@0: truelight@0: CreateEffectVehicleRel(u, truelight@0: 2 + ((r>>8)&7), truelight@0: 2 + ((r>>16)&7), truelight@0: 5 + (r&7), tron@1359: EV_EXPLOSION_SMALL); truelight@0: break; truelight@0: } truelight@0: } while ( (u=u->next) != NULL); truelight@0: } truelight@0: truelight@0: if (state <= 240 && !(v->tick_counter&3)) { truelight@0: ChangeTrainDirRandomly(v); truelight@0: } truelight@0: bjarni@1128: if (state >= 4440 && !(v->tick_counter&0x1F)) { truelight@0: DeleteLastWagon(v); bjarni@1128: InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); bjarni@1128: } truelight@0: } truelight@0: truelight@0: static void HandleBrokenTrain(Vehicle *v) truelight@0: { truelight@0: if (v->breakdown_ctr != 1) { truelight@0: v->breakdown_ctr = 1; truelight@0: v->cur_speed = 0; truelight@0: truelight@0: if (v->breakdowns_since_last_service != 255) truelight@0: v->breakdowns_since_last_service++; truelight@193: truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@193: tron@541: SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? tron@541: SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v); truelight@0: truelight@0: if (!(v->vehstatus & VS_HIDDEN)) { truelight@0: Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); truelight@0: if (u) truelight@0: u->u.special.unk0 = v->breakdown_delay * 2; truelight@0: } truelight@0: } truelight@0: truelight@0: if (!(v->tick_counter & 3)) { truelight@0: if (!--v->breakdown_delay) { truelight@0: v->breakdown_ctr = 0; truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static const byte _breakdown_speeds[16] = { truelight@0: 225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15 truelight@0: }; truelight@0: truelight@742: static bool TrainCheckIfLineEnds(Vehicle *v) truelight@0: { truelight@0: uint tile; truelight@0: uint x,y; truelight@0: int t; truelight@0: uint32 ts; truelight@0: truelight@0: if ((uint)(t=v->breakdown_ctr) > 1) { truelight@0: v->vehstatus |= VS_TRAIN_SLOWING; truelight@193: truelight@0: t = _breakdown_speeds[ ((~t) >> 4) & 0xF]; truelight@0: if ((uint16)t <= v->cur_speed) truelight@0: v->cur_speed = t; truelight@0: } else { truelight@0: v->vehstatus &= ~VS_TRAIN_SLOWING; truelight@0: } truelight@0: truelight@0: // exit if inside a tunnel truelight@0: if (v->u.rail.track & 0x40) truelight@742: return true; truelight@0: truelight@0: tile = v->tile; truelight@0: truelight@0: // tunnel entrance? tron@1035: if (IsTileType(tile, MP_TUNNELBRIDGE) && truelight@0: (_map5[tile] & 0xF0) == 0 && (byte)((_map5[tile] & 3)*2+1) == v->direction) truelight@742: return true; truelight@193: truelight@0: // depot? truelight@742: /* XXX -- When enabled, this makes it possible to crash trains of others truelight@742: (by building a depot right against a station) */ tron@1035: /* if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xFC) == 0xC0) truelight@742: return true;*/ truelight@0: darkvater@22: /* Determine the non-diagonal direction in which we will exit this tile */ truelight@0: t = v->direction >> 1; truelight@0: if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) { truelight@0: t = (t - 1) & 3; truelight@0: } darkvater@22: /* Calculate next tile */ tron@900: tile += TileOffsByDir(t); darkvater@22: // determine the track status on the next tile. bjarni@1151: ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _reachable_tracks[t]; truelight@193: darkvater@22: /* Calc position within the current tile ?? */ truelight@0: x = v->x_pos & 0xF; truelight@0: y = v->y_pos & 0xF; truelight@193: truelight@0: switch(v->direction) { truelight@0: case 0: truelight@0: x = (~x) + (~y) + 24; truelight@0: break; truelight@0: case 7: truelight@0: x = y; truelight@0: /* fall through */ truelight@0: case 1: truelight@0: x = (~x) + 16; truelight@0: break; truelight@0: case 2: truelight@0: x = (~x) + y + 8; truelight@0: break; truelight@0: case 3: truelight@0: x = y; truelight@0: break; truelight@0: case 4: truelight@0: x = x + y - 8; truelight@0: break; truelight@0: case 6: truelight@0: x = (~y) + x + 8; truelight@0: break; truelight@0: } truelight@0: truelight@0: if ( (uint16)ts != 0) { truelight@742: /* If we approach a rail-piece which we can't enter, don't enter it! */ truelight@742: if (x + 4 > 15 && !CheckCompatibleRail(v, tile)) { truelight@742: v->cur_speed = 0; truelight@742: ReverseTrainDirection(v); truelight@742: return false; truelight@742: } truelight@0: if ((ts &= (ts >> 16)) == 0) { truelight@0: // make a rail/road crossing red tron@1035: if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) { truelight@0: if (!(_map5[tile] & 4)) { truelight@0: _map5[tile] |= 4; tron@541: SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v); truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@742: return true; truelight@0: } truelight@0: } else if (x + 4 > 15) { truelight@0: v->cur_speed = 0; truelight@0: ReverseTrainDirection(v); truelight@742: return false; truelight@0: } truelight@0: truelight@0: // slow down truelight@0: v->vehstatus |= VS_TRAIN_SLOWING; truelight@0: t = _breakdown_speeds[x & 0xF]; truelight@0: if (!(v->direction&1)) t>>=1; truelight@0: if ((uint16)t < v->cur_speed) truelight@0: v->cur_speed = t; truelight@742: truelight@742: return true; truelight@0: } truelight@0: truelight@0: static void TrainLocoHandler(Vehicle *v, bool mode) truelight@0: { truelight@0: int j; truelight@0: truelight@0: /* train has crashed? */ truelight@0: if (v->u.rail.crash_anim_pos != 0) { truelight@0: if (!mode) HandleCrashedTrain(v); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (v->u.rail.force_proceed != 0) truelight@0: v->u.rail.force_proceed--; truelight@0: truelight@0: /* train is broken down? */ truelight@0: if (v->breakdown_ctr != 0) { truelight@0: if (v->breakdown_ctr <= 2) { truelight@0: HandleBrokenTrain(v); truelight@0: return; truelight@0: } truelight@0: v->breakdown_ctr--; truelight@0: } truelight@0: truelight@954: if (HASBIT(v->u.rail.flags, VRF_REVERSING) && v->cur_speed == 0) { truelight@0: ReverseTrainDirection(v); truelight@0: } truelight@0: truelight@0: /* exit if train is stopped */ truelight@0: if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) truelight@0: return; truelight@0: truelight@0: truelight@0: if (ProcessTrainOrder(v)) { truelight@0: v->load_unload_time_rem = 0; truelight@0: v->cur_speed = 0; truelight@0: v->subspeed = 0; truelight@0: ReverseTrainDirection(v); truelight@0: return; truelight@0: } truelight@0: truelight@0: HandleTrainLoading(v, mode); truelight@0: tron@555: if (v->current_order.type == OT_LOADING) truelight@0: return; truelight@0: truelight@0: if (CheckTrainStayInDepot(v)) truelight@0: return; truelight@0: truelight@0: if (!mode) HandleLocomotiveSmokeCloud(v); truelight@0: truelight@0: j = UpdateTrainSpeed(v); truelight@0: if (j == 0) { truelight@0: // if the vehicle has speed 0, update the last_speed field. truelight@0: if (v->cur_speed != 0) truelight@0: return; truelight@0: } else { truelight@0: TrainCheckIfLineEnds(v); truelight@0: truelight@0: do { truelight@0: TrainController(v); truelight@0: if (v->cur_speed <= 0x100) truelight@0: break; truelight@0: } while (--j != 0); truelight@0: } truelight@0: truelight@0: SetLastSpeed(v, v->cur_speed); truelight@0: } truelight@0: truelight@0: truelight@0: void Train_Tick(Vehicle *v) truelight@0: { truelight@0: if (_age_cargo_skip_counter == 0 && v->cargo_days != 0xff) truelight@0: v->cargo_days++; truelight@0: truelight@0: v->tick_counter++; truelight@0: bjarni@1067: if (v->subtype == TS_Front_Engine) { truelight@0: TrainLocoHandler(v, false); truelight@193: truelight@0: // make sure vehicle wasn't deleted. bjarni@1067: if (v->type == VEH_Train && v->subtype == TS_Front_Engine) truelight@0: TrainLocoHandler(v, true); darkvater@1132: } else if (v->subtype == TS_Free_Car && HASBITS(v->vehstatus, VS_CRASHED)) { darkvater@1132: // Delete flooded standalone wagon darkvater@1132: if (++v->u.rail.crash_anim_pos >= 4400) darkvater@1132: DeleteVehicle(v); truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: static const byte _depot_track_ind[4] = {0,1,0,1}; truelight@0: dominik@715: // Validation for the news item "Train is waiting in depot" tron@1095: static bool ValidateTrainInDepot( uint data_a, uint data_b ) dominik@715: { truelight@919: Vehicle *v = GetVehicle(data_a); celestar@1064: return (v->u.rail.track == 0x80 && (v->vehstatus | VS_STOPPED)); dominik@715: } dominik@715: truelight@0: void TrainEnterDepot(Vehicle *v, uint tile) truelight@0: { truelight@0: SetSignalsOnBothDir(tile, _depot_track_ind[_map5[tile]&3]); truelight@0: bjarni@1067: if (v->subtype != TS_Front_Engine) truelight@0: v = GetFirstVehicleInChain(v); truelight@0: bjarni@578: VehicleServiceInDepot(v); bjarni@578: truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: truelight@0: v->load_unload_time_rem = 0; truelight@0: v->cur_speed = 0; truelight@0: bjarni@842: MaybeReplaceVehicle(v); truelight@0: tron@445: TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT); tron@445: tron@555: if (v->current_order.type == OT_GOTO_DEPOT) { tron@555: Order t; tron@555: truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@193: tron@555: t = v->current_order; tron@555: v->current_order.type = OT_DUMMY; tron@555: v->current_order.flags = 0; tron@555: truelight@1024: if (t.flags & OF_UNLOAD) { // Part of the orderlist? tron@555: v->u.rail.days_since_order_progr = 0; tron@555: v->cur_order_index++; tron@555: } else if (t.flags & OF_FULL_LOAD) { // User initiated? truelight@0: v->vehstatus |= VS_STOPPED; truelight@0: if (v->owner == _local_player) { tron@534: SetDParam(0, v->unitnumber); dominik@715: AddValidatedNewsItem( truelight@0: STR_8814_TRAIN_IS_WAITING_IN_DEPOT, truelight@0: NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), truelight@0: v->index, dominik@715: 0, dominik@715: ValidateTrainInDepot); truelight@0: } truelight@0: } truelight@0: } bjarni@1151: InvalidateWindowClasses(WC_TRAINS_LIST); truelight@0: } truelight@0: truelight@0: static void CheckIfTrainNeedsService(Vehicle *v) truelight@0: { truelight@1313: Depot *depot; darkvater@308: TrainFindDepotData tfdd; truelight@0: bjarni@1208: if (_patches.servint_trains == 0 && !v->set_for_replacement) truelight@0: return; truelight@0: bjarni@1208: if (!VehicleNeedsService(v) && !v->set_for_replacement) truelight@0: return; truelight@0: truelight@0: if (v->vehstatus & VS_STOPPED) truelight@0: return; truelight@0: truelight@1024: if (_patches.gotodepot && VehicleHasDepotOrders(v)) truelight@0: return; truelight@193: truelight@0: // Don't interfere with a depot visit scheduled by the user, or a truelight@0: // depot visit by the order list. tron@555: if (v->current_order.type == OT_GOTO_DEPOT && tron@555: (v->current_order.flags & (OF_FULL_LOAD | OF_UNLOAD)) != 0) truelight@0: return; truelight@0: darkvater@308: tfdd = FindClosestTrainDepot(v); darkvater@308: /* Only go to the depot if it is not too far out of our way. */ darkvater@308: if (tfdd.best_length == (uint)-1 || tfdd.best_length > 16 ) { tron@555: if (v->current_order.type == OT_GOTO_DEPOT) { darkvater@308: /* If we were already heading for a depot but it has darkvater@308: * suddenly moved farther away, we continue our normal darkvater@308: * schedule? */ tron@555: v->current_order.type = OT_DUMMY; tron@555: v->current_order.flags = 0; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: return; truelight@0: } truelight@0: darkvater@308: depot = GetDepotByTile(tfdd.tile); truelight@0: tron@555: if (v->current_order.type == OT_GOTO_DEPOT && truelight@1313: v->current_order.station != depot->index && tron@555: !CHANCE16(3,16)) truelight@0: return; truelight@0: tron@555: v->current_order.type = OT_GOTO_DEPOT; tron@555: v->current_order.flags = OF_NON_STOP; truelight@1313: v->current_order.station = depot->index; darkvater@308: v->dest_tile = tfdd.tile; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: truelight@0: int32 GetTrainRunningCost(Vehicle *v) truelight@0: { truelight@0: int32 cost = 0; truelight@0: truelight@0: do { tron@540: const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); truelight@0: if (rvi->running_cost_base) truelight@0: cost += rvi->running_cost_base * _price.running_rail[rvi->engclass]; truelight@0: } while ( (v=v->next) != NULL ); truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: void OnNewDay_Train(Vehicle *v) truelight@0: { truelight@0: TileIndex tile; truelight@0: truelight@0: if ((++v->day_counter & 7) == 0) truelight@0: DecreaseVehicleValue(v); truelight@0: bjarni@1067: if (v->subtype == TS_Front_Engine) { truelight@0: CheckVehicleBreakdown(v); truelight@0: AgeVehicle(v); truelight@193: truelight@0: CheckIfTrainNeedsService(v); truelight@193: truelight@0: // check if train hasn't advanced in its order list for a set number of days Celestar@1057: if (_patches.lost_train_days && v->num_orders && !(v->vehstatus & (VS_STOPPED | VS_CRASHED) ) && ++v->u.rail.days_since_order_progr >= _patches.lost_train_days && v->owner == _local_player) { truelight@0: v->u.rail.days_since_order_progr = 0; tron@534: SetDParam(0, v->unitnumber); truelight@0: AddNewsItem( truelight@0: STR_TRAIN_IS_LOST, truelight@0: NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), truelight@0: v->index, truelight@0: 0); truelight@0: } truelight@0: celestar@1053: CheckOrders(v->index, OC_INIT); truelight@193: truelight@0: /* update destination */ tron@555: if (v->current_order.type == OT_GOTO_STATION && truelight@919: (tile = GetStation(v->current_order.station)->train_tile) != 0) truelight@0: v->dest_tile = tile; truelight@0: truelight@0: if ((v->vehstatus & VS_STOPPED) == 0) { truelight@0: /* running costs */ truelight@0: int32 cost = GetTrainRunningCost(v) / 364; truelight@0: truelight@0: v->profit_this_year -= cost >> 8; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN); truelight@0: SubtractMoneyFromPlayerFract(v->owner, cost); truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); bjarni@1151: InvalidateWindowClasses(WC_TRAINS_LIST); truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1093: void TrainsYearlyLoop(void) truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { bjarni@1067: if (v->type == VEH_Train && v->subtype == TS_Front_Engine) { truelight@193: truelight@0: // show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list) truelight@0: if (_patches.train_income_warn && v->owner == _local_player && v->age >= 730 && v->profit_this_year < 0) { tron@534: SetDParam(1, v->profit_this_year); tron@534: SetDParam(0, v->unitnumber); truelight@0: AddNewsItem( truelight@0: STR_TRAIN_IS_UNPROFITABLE, truelight@0: NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), truelight@0: v->index, truelight@0: 0); truelight@0: } truelight@0: truelight@0: v->profit_last_year = v->profit_this_year; truelight@0: v->profit_this_year = 0; truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: extern void ShowTrainViewWindow(Vehicle *v); truelight@0: truelight@0: void HandleClickOnTrain(Vehicle *v) truelight@0: { bjarni@1067: if (v->subtype != TS_Front_Engine) v = GetFirstVehicleInChain(v); truelight@0: ShowTrainViewWindow(v); truelight@0: } truelight@0: tron@1093: void InitializeTrains(void) truelight@0: { truelight@0: _age_cargo_skip_counter = 1; truelight@0: }