truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" truelight@0: #include "vehicle.h" truelight@0: #include "command.h" truelight@0: #include "pathfind.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" truelight@0: 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@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: truelight@0: static const byte _signal_onedir[14] = { truelight@0: 0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0, truelight@0: 0x40, 0x40, 0x40, 0x10, 0x80, 0x20 truelight@0: }; truelight@0: truelight@0: static const byte _signal_otherdir[14] = { truelight@0: 0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0, truelight@0: 0x80, 0x80, 0x80, 0x20, 0x40, 0x10 truelight@0: }; truelight@0: 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: truelight@0: assert(v->subtype == 0); truelight@0: truelight@0: // compute stuff like max speed, power, and weight. truelight@0: do { truelight@0: const RailVehicleInfo *rvi = &_rail_vehicle_info[u->engine_type]; truelight@0: 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@0: 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: #define F_GRAV 9.82f truelight@0: #define F_THETA 0.05f truelight@0: truelight@0: #define F_HP_KW 0.74569f truelight@0: #define F_KPH_MS 0.27778f truelight@0: #define F_MU 0.3f truelight@0: truelight@0: #define F_COEF_FRIC 0.04f truelight@0: #define F_COEF_ROLL 0.18f truelight@0: truelight@0: #define F_CURVE_FACTOR (1/96.f) truelight@0: truelight@0: bool IsTunnelTile(TileIndex tile); truelight@0: truelight@0: static int GetRealisticAcceleration(Vehicle *v) truelight@0: { truelight@0: uint emass = 0; truelight@0: Vehicle *u = v; truelight@0: float f = 0.0f, spd; truelight@0: int curves = 0; truelight@0: truelight@0: assert(v->subtype == 0); truelight@0: truelight@0: // compute inclination force and number of curves. truelight@0: do { truelight@0: const RailVehicleInfo *rvi = &_rail_vehicle_info[u->engine_type]; truelight@0: uint mass = rvi->weight + ((_cargoc.weights[u->cargo_type] * u->cargo_count) >> 4); truelight@0: if (rvi->power) emass += mass; truelight@0: truelight@0: if (u->u.rail.flags & VRF_GOINGUP) { truelight@0: f += (float)mass * ( -F_GRAV * F_THETA); truelight@0: } else if (u->u.rail.flags & VRF_GOINGDOWN) { truelight@0: f += (float)mass * ( F_GRAV * F_THETA); truelight@0: } truelight@0: truelight@0: // compute curve penalty.. truelight@0: if (u->next != NULL) { truelight@0: uint diff = (u->direction - u->next->direction) & 7; truelight@0: if (diff) { truelight@0: curves += (diff == 1 || diff == 7) ? 1 : 3; truelight@0: } truelight@0: } truelight@0: } while ((u = u->next) != NULL); truelight@0: truelight@0: spd = (float)(v->cur_speed ? v->cur_speed : 1); truelight@0: truelight@0: // compute tractive effort truelight@0: { truelight@0: float te = (float)v->u.rail.cached_power * (F_HP_KW/F_KPH_MS) / spd; truelight@0: float te2 = (float)emass * (F_MU * F_GRAV); truelight@0: if (te > te2) te = te2; truelight@0: f += te; truelight@0: } truelight@0: truelight@0: // add air resistance truelight@0: { truelight@0: float cx = 1.0f; // NOT DONE truelight@0: truelight@0: // air resistance is doubled in tunnels. truelight@0: if (v->vehstatus == 0x40) cx *= 2; truelight@0: truelight@0: f -= cx * spd * spd * (F_KPH_MS * F_KPH_MS * 0.001f); truelight@0: } truelight@0: truelight@0: // after this f contains the acceleration. truelight@0: f /= (float)v->u.rail.cached_weight; truelight@0: truelight@0: // add friction to sum of forces (avoid mul by weight). (0.001 because we want kN) truelight@0: f -= (F_COEF_FRIC * F_GRAV * 0.001f + (F_COEF_ROLL * F_KPH_MS * F_GRAV * 0.001f) * spd); truelight@0: truelight@0: // penalty for curves? truelight@0: if (curves) truelight@0: f -= (float)min(curves, 8) * F_CURVE_FACTOR; truelight@0: truelight@0: return (int)(f * (1.0/(F_KPH_MS * 0.015f)) + 0.5f); truelight@0: } 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@0: 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: { truelight@0: const RailVehicleInfo *rvi = &_rail_vehicle_info[engine]; truelight@0: truelight@0: int img = rvi->image_index; truelight@0: uint32 image = 0; truelight@0: truelight@0: if (is_custom_sprite(img)) { truelight@0: image = GetCustomEngineSprite(engine, -1, CID_PURCHASE, 0, 0, 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)) { truelight@0: image = GetCustomEngineSprite(engine, -1, CID_PURCHASE, 0, 0, 2); truelight@0: if (!image) img = _engine_original_sprites[engine]; truelight@0: } truelight@0: if (!image) { truelight@0: 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: { truelight@0: const RailVehicleInfo *rvi = &_rail_vehicle_info[engine]; truelight@0: int cap; truelight@0: uint multihead = ((rvi->flags & RVI_MULTIHEAD) ? 1 : 0); truelight@0: truelight@0: SET_DPARAM32(0, ((_price.build_railvehicle >> 3) * rvi->base_cost) >> 5); truelight@0: SET_DPARAM16(2, rvi->max_speed * 10 >> 4); truelight@0: SET_DPARAM16(3, rvi->power << multihead); truelight@0: SET_DPARAM16(1, rvi->weight << multihead); truelight@0: truelight@0: SET_DPARAM32(4, (rvi->running_cost_base * _price.running_rail[rvi->engclass] >> 8) << multihead); truelight@0: truelight@0: cap = rvi->capacity; truelight@0: SET_DPARAM16(5, STR_8838_N_A); truelight@0: if (cap != 0) { truelight@0: SET_DPARAM16(6, cap << multihead); truelight@0: SET_DPARAM16(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: truelight@0: 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: truelight@0: rvi = &_rail_vehicle_info[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@0: Vehicle *u; truelight@0: truelight@0: v->spritenum = img; truelight@0: truelight@0: u = _vehicles; truelight@0: for(;;) { truelight@0: if (u->type == VEH_Train && u->tile == (TileIndex)tile && truelight@0: u->subtype == 4 && u->engine_type == engine) { truelight@0: u = GetLastVehicleInChain(u); truelight@0: break; truelight@0: } truelight@0: truelight@0: if (++u == endof(_vehicles)) { truelight@0: u = NULL; 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@0: truelight@0: x = GET_TILE_X(tile)*16 | _vehicle_initial_x_fract[dir]; truelight@0: y = GET_TILE_Y(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: truelight@0: v->subtype = 4; truelight@0: if (u != NULL) { truelight@0: u->next = v; truelight@0: v->subtype = 2; truelight@0: v->u.rail.first_engine = u->u.rail.first_engine; truelight@0: if (v->u.rail.first_engine == 0xffff && u->subtype == 0) 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@0: truelight@0: v->build_year = _cur_year; truelight@0: v->type = VEH_Train; truelight@0: v->cur_image = 0xAC2; truelight@0: 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) { truelight@0: if (v->type == VEH_Train && v->subtype==4 && 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@0: static int32 EstimateTrainCost(const RailVehicleInfo *rvi) truelight@0: { truelight@0: return (rvi->base_cost * (_price.build_railvehicle >> 3)) >> 5; truelight@0: } truelight@0: 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@0: byte unit_num; truelight@0: Engine *e; truelight@0: uint tile; truelight@0: truelight@0: _cmd_build_rail_veh_var1 = 0; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); truelight@0: truelight@0: tile = TILE_FROM_XY(x,y); truelight@0: rvi = &_rail_vehicle_info[p1]; truelight@0: 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@0: if (v == NULL || _ptr_to_next_order >= endof(_order_array)) 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->subtype = 0; 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->cargo_count = 0; truelight@0: v->value = value; truelight@0: // v->day_counter = 0; truelight@0: // v->next_order = 0; truelight@0: // v->next_station = 0; truelight@0: // v->load_unload_time_rem = 0; truelight@0: // v->progress = 0; truelight@0: // v->targetairport = 0; truelight@0: // v->crash_anim_pos = 0; truelight@0: v->last_station_visited = 0xff; truelight@0: v->dest_tile = 0; truelight@0: // v->profit_last_year = 0; truelight@0: // v->profit_this_year = 0; truelight@0: 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@0: truelight@0: v->string_id = STR_SV_TRAIN_NAME; truelight@0: // v->cur_speed = 0; truelight@0: // v->subspeed = 0; truelight@0: v->u.rail.railtype = e->railtype; truelight@0: _new_train_id = v->index; truelight@0: // v->cur_order_index = 0; truelight@0: // v->num_orders = 0; truelight@0: truelight@0: *(v->schedule_ptr = _ptr_to_next_order++) = 0; truelight@0: // v->next_in_chain = 0xffff; truelight@0: // v->next = NULL; truelight@0: truelight@0: v->service_interval = _patches.servint_trains; truelight@0: // v->breakdown_ctr = 0; truelight@0: // v->breakdowns_since_last_service = 0; truelight@0: // v->unk4D = 0; 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: truelight@0: if (rvi->flags&RVI_MULTIHEAD && (u=AllocateVehicle()) != NULL) { truelight@0: u->direction = v->direction; truelight@0: u->owner = v->owner; truelight@0: u->tile = v->tile; truelight@0: u->x_pos = v->x_pos; truelight@0: u->y_pos = v->y_pos; truelight@0: u->z_pos = v->z_pos; truelight@0: u->z_height = 6; truelight@0: u->u.rail.track = 0x80; truelight@0: v->u.rail.first_engine = 0xffff; truelight@0: u->vehstatus = VS_HIDDEN | VS_DEFPAL; truelight@0: u->subtype = 2; truelight@0: u->spritenum = v->spritenum + 1; truelight@0: u->cargo_type = v->cargo_type; truelight@0: u->cargo_cap = v->cargo_cap; truelight@0: u->u.rail.railtype = v->u.rail.railtype; truelight@0: // u->next_in_chain = 0xffff; truelight@0: v->next = u; truelight@0: u->engine_type = v->engine_type; truelight@0: u->build_year = v->build_year; truelight@0: v->value = u->value = v->value >> 1; truelight@0: // u->day_counter = 0; truelight@0: u->type = VEH_Train; truelight@0: u->cur_image = 0xAC2; truelight@0: VehiclePositionChanged(u); truelight@0: } truelight@0: truelight@0: UpdateTrainAcceleration(v); truelight@0: NormalizeTrainVehInDepot(v); truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, tile); truelight@0: InvalidateWindow(WC_TRAINS_LIST, v->owner); 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]; truelight@0: return value; truelight@0: } truelight@0: truelight@0: truelight@0: bool IsTrainDepotTile(TileIndex tile) truelight@0: { truelight@0: return IS_TILETYPE(tile, MP_RAILWAY) && truelight@0: (_map5[tile] & 0xFC) == 0xC0; truelight@0: } truelight@0: truelight@0: bool IsTunnelTile(TileIndex tile) truelight@0: { truelight@0: return IS_TILETYPE(tile, MP_TUNNELBRIDGE) && truelight@0: (_map5[tile]&0x80) == 0; truelight@0: } truelight@0: truelight@0: truelight@0: int CheckStoppedInDepot(Vehicle *v) truelight@0: { truelight@0: int count; truelight@0: TileIndex tile = v->tile; truelight@0: truelight@0: /* check if stopped in a depot */ truelight@0: if (!IsTrainDepotTile(tile) || 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@0: if (v->u.rail.track != 0x80 || v->tile != (TileIndex)tile || truelight@0: (v->subtype==0 && !(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; truelight@0: v->subtype = 4; 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) { truelight@0: if (dst->type==VEH_Train && dst->subtype==4 && 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: truelight@0: src = &_vehicles[p1 & 0xffff]; truelight@0: if (src->type != VEH_Train) return CMD_ERROR; truelight@0: truelight@0: is_loco = !(_rail_vehicle_info[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@0: if (((int32)p1 >> 16) == -1) { truelight@0: dst = NULL; truelight@0: if (!is_loco) dst = FindGoodVehiclePos(src); truelight@0: } else { truelight@0: dst = &_vehicles[((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@0: 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@0: truelight@0: /* check if all vehicles in the source train are stopped */ truelight@0: if (CheckStoppedInDepot(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@0: int num = CheckStoppedInDepot(dst_head); truelight@0: if (num < 0) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (num > (_patches.mammoth_trains ? 100 : 9) && dst_head->subtype==0) 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. truelight@0: if (dst == NULL && src->subtype != 0 && is_loco) { truelight@0: uint 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) { truelight@0: if (src->subtype != 0) { truelight@0: // setting the type to 0 also involves setting up the schedule_ptr field. truelight@0: src->subtype = 0; truelight@0: assert(src->schedule_ptr == NULL); truelight@0: *(src->schedule_ptr = _ptr_to_next_order++) = 0; truelight@0: src->num_orders = 0; truelight@0: } truelight@0: dst_head = src; truelight@0: } else { truelight@0: src->subtype = 4; truelight@0: } truelight@0: src->u.rail.first_engine = 0xffff; truelight@0: } else { truelight@0: if (src->subtype == 0) { truelight@0: // the vehicle was previously a loco. need to free the schedule list and delete vehicle windows etc. truelight@0: DeleteWindowById(WC_VEHICLE_VIEW, src->index); truelight@0: DeleteVehicleSchedule(src); truelight@0: } truelight@0: truelight@0: src->subtype = 2; 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; truelight@0: if (src->u.rail.first_engine == 0xffff && dst->subtype == 0) 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: truelight@0: if (src_head->subtype == 0) truelight@0: UpdateTrainAcceleration(src_head); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, src_head->index); truelight@0: truelight@0: if (dst_head) { truelight@0: if (dst_head->subtype == 0) truelight@0: UpdateTrainAcceleration(dst_head); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, dst_head->index); truelight@0: } truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile); truelight@0: InvalidateWindow(WC_TRAINS_LIST, _current_player); 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: truelight@0: v = &_vehicles[p1]; truelight@0: truelight@0: if (!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; truelight@0: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); 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: truelight@0: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); truelight@0: truelight@0: v = &_vehicles[p1]; truelight@0: truelight@0: if (!CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: // get first vehicle in chain truelight@0: first = v; truelight@0: if (first->subtype != 0) { truelight@0: first = GetFirstVehicleInChain(first); truelight@0: 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@0: truelight@0: // make sure the vehicle is stopped in the depot truelight@0: if (CheckStoppedInDepot(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); truelight@0: if (first->subtype == 0) InvalidateWindow(WC_TRAINS_LIST, first->owner); truelight@0: // when selling an attached locomotive. we need to delete its window. truelight@0: if (v->subtype == 0) { truelight@0: DeleteWindowById(WC_VEHICLE_VIEW, v->index); truelight@0: 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@0: 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: v = v->next; truelight@0: DeleteVehicle(tmp); truelight@0: if (v == NULL || 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@0: truelight@0: // an attached train changed? truelight@0: if (first && first->subtype == 0) { 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; truelight@0: if ((v=v->next) == NULL || p2 != 1) break; truelight@0: } truelight@0: if (last) cost -= last->value; truelight@0: } 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@0: 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) truelight@0: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); truelight@0: } truelight@0: } truelight@0: 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@0: 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@0: truelight@0: /* swap more variables */ truelight@0: swap_int16(&a->x_pos, &b->x_pos); truelight@0: swap_int16(&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@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@0: UpdateVarsAfterSwap(a); truelight@0: } truelight@0: } truelight@0: truelight@0: static void ReverseTrainDirection(Vehicle *v) truelight@0: { truelight@0: int l = 0, r = -1; truelight@0: Vehicle *u; truelight@0: truelight@0: if (IsTrainDepotTile(v->tile)) truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@0: 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: truelight@0: if (IsTrainDepotTile(v->tile)) truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@0: truelight@0: 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: truelight@0: v = &_vehicles[p1]; truelight@0: truelight@0: if (!CheckOwnership(v->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: _error_message = STR_EMPTY; truelight@0: truelight@0: // if (v->u.rail.track & 0x80 || IsTrainDepotTile(v->tile)) 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@0: 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: truelight@0: v = &_vehicles[p1]; truelight@0: truelight@0: if (!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@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: // p1 = vehicle to refit truelight@0: // p2 = new cargo truelight@0: 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: truelight@0: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); truelight@0: truelight@0: v = &_vehicles[p1]; truelight@0: if (!CheckOwnership(v->owner) || CheckStoppedInDepot(v) < 0) truelight@0: return CMD_ERROR; truelight@0: truelight@0: cost = 0; truelight@0: num = 0; truelight@0: truelight@0: do { truelight@0: if (!(_rail_vehicle_info[v->engine_type].flags & RVI_WAGON) && (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) { truelight@0: v->cargo_count = 0; truelight@0: v->cargo_type = (byte)p2; truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: } truelight@0: } truelight@0: } while ( (v=v->next) != NULL); truelight@0: truelight@0: _returned_refit_amount = num; truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: int GetDepotByTile(uint tile) truelight@0: { truelight@0: Depot *d; truelight@0: int i=0; truelight@0: for(d=_depots; d->xy != (TileIndex)tile; d++) { i++; } truelight@0: return i; 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: { truelight@0: if (IS_TILETYPE(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) { truelight@0: if (!(_map3_lo[tile] & _signal_onedir[track]) && _map3_lo[tile] & _signal_otherdir[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: truelight@0: // returns the tile of a depot to goto to truelight@0: static uint FindClosestTrainDepot(Vehicle *v) truelight@0: { truelight@0: int i; truelight@0: TrainFindDepotData tfdd; truelight@0: uint tile = v->tile; truelight@0: truelight@0: if (IsTrainDepotTile(tile)) truelight@0: return tile; truelight@0: truelight@0: if (v->u.rail.track == 0x40) { tile = GetVehicleOutOfTunnelTile(v); } truelight@0: truelight@0: tfdd.owner = v->owner; truelight@0: tfdd.best_length = (uint)-1; truelight@0: truelight@0: 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: if (tfdd.best_length != (uint)-1) truelight@0: return tfdd.tile; 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); truelight@0: if (tfdd.best_length != (uint)-1) truelight@0: return tfdd.tile; truelight@0: truelight@0: // search in backwards direction truelight@0: i = (v->direction^4) >> 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); truelight@0: if (tfdd.best_length != (uint)-1) truelight@0: return tfdd.tile; truelight@0: } truelight@0: truelight@0: return (uint)-1; truelight@0: } truelight@0: truelight@0: int32 CmdTrainGotoDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v = &_vehicles[p1]; truelight@0: uint depot_tile; truelight@0: truelight@0: if ((v->next_order & OT_MASK) == OT_GOTO_DEPOT) { truelight@0: if (flags & DC_EXEC) { truelight@0: if (v->next_order & OF_UNLOAD) { truelight@0: v->u.rail.days_since_order_progr = 0; truelight@0: v->cur_order_index++; truelight@0: } truelight@0: truelight@0: v->next_order = OT_DUMMY; truelight@0: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: truelight@0: depot_tile = FindClosestTrainDepot(v); truelight@0: if (depot_tile == (uint)-1) truelight@0: return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO); truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: v->dest_tile = depot_tile; truelight@0: v->next_order = OF_NON_STOP | OF_FULL_LOAD | OT_GOTO_DEPOT; truelight@0: v->next_order_param = GetDepotByTile(depot_tile); truelight@0: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); truelight@0: } truelight@0: 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: { truelight@0: Vehicle *v = &_vehicles[p1]; truelight@0: truelight@0: if (!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@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: void OnTick_Train() 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? truelight@0: if (_rail_vehicle_info[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: truelight@0: switch (_rail_vehicle_info[engtype].engclass) { truelight@0: case 0: truelight@0: // steam smoke. truelight@0: if ( (v->tick_counter&0xF) == 0 && !IsTrainDepotTile(v->tile) && !IsTunnelTile(v->tile)) { truelight@0: 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 truelight@0: if (u->cur_speed <= 40 && !IsTrainDepotTile(v->tile) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x1E00) { truelight@0: CreateEffectVehicleRel(v, 0,0,10, EV_SMOKE_3); truelight@0: } truelight@0: break; truelight@0: truelight@0: case 2: truelight@0: // blue spark truelight@0: if ( (v->tick_counter&0x3) == 0 && !IsTrainDepotTile(v->tile) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x5B0) { truelight@0: CreateEffectVehicleRel(v, 0,0,10, EV_SMOKE_2); 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: { truelight@0: static const byte sfx[3] = { 0x2, 0x8, 0x8 }; truelight@0: int engtype = v->engine_type; truelight@0: truelight@0: switch (_engines[engtype].railtype) { truelight@0: case 0: SndPlayVehicleFx(sfx[_rail_vehicle_info[engtype].engclass], v); break; truelight@0: case 1: SndPlayVehicleFx(0x41, v); break; truelight@0: case 2: SndPlayVehicleFx(0x47, v); break; truelight@0: } truelight@0: } truelight@0: truelight@0: static bool CheckTrainStayInDepot(Vehicle *v) truelight@0: { truelight@0: Vehicle *u; truelight@0: if (v->u.rail.track != 0x80) // first wagon (eg engine) in depot truelight@0: return false; truelight@0: truelight@0: // make sure that all vehicles are in the depot truelight@0: u = GetLastVehicleInChain(v); truelight@0: if (u->u.rail.track != 0x80) truelight@0: return false; truelight@0: truelight@0: // fix hung train if both ends are in depots (when here first wagon and last wagon is in depot) truelight@0: // both first and last should be in the same depot, eg on the same tile truelight@0: if (v->tile != u->tile) truelight@0: return false; truelight@0: truelight@0: if (v->u.rail.force_proceed == 0) { truelight@0: if (++v->load_unload_time_rem < 37) truelight@0: return true; truelight@0: v->load_unload_time_rem = 0; truelight@0: truelight@0: if (UpdateSignalsOnSegment(v->tile, v->direction)) truelight@0: return true; truelight@0: } truelight@0: truelight@0: TrainPlayLeaveStationSound(v); truelight@0: truelight@0: v->u.rail.track = 1; truelight@0: if (v->direction & 2) truelight@0: v->u.rail.track = 2; truelight@0: truelight@0: v->vehstatus &= ~VS_HIDDEN; truelight@0: v->cur_speed = 0; truelight@0: 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: 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){ truelight@0: if (IS_TILETYPE(tile, MP_RAILWAY) && (_map5[tile]&0xC0) == 0x40) { truelight@0: // the tile has a signal truelight@0: byte m3 = _map3_lo[tile]; truelight@0: if (!(m3 & _signal_onedir[track])) { truelight@0: // if one way signal not pointing towards us, stop going in this direction. truelight@0: if (m3 & _signal_otherdir[track]) truelight@0: return true; truelight@0: } else if (_map2[tile] & _signal_onedir[track]) { truelight@0: // green signal in our direction. either one way or two way. truelight@0: *state = true; truelight@0: } else if (m3 & _signal_otherdir[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@0: if (tile == ttfd->dest_coords || truelight@0: (IS_TILETYPE(tile, MP_STATION) && IS_BYTE_INSIDE(_map5[tile], 0, 8) && _map2[tile] == ttfd->station_index)) { 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@0: truelight@0: // didn't find station truelight@0: dist = GetTileDist(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: { truelight@0: uint tile; truelight@0: truelight@0: fd->dest_coords = tile = v->dest_tile; truelight@0: fd->station_index = -1; truelight@0: truelight@0: if (IS_TILETYPE(tile, MP_STATION) && IS_BYTE_INSIDE(_map5[tile], 0, 8) ) truelight@0: fd->station_index = _map2[tile]; 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}; truelight@0: truelight@0: /* choose a track */ truelight@0: static byte ChooseTrainTrack(Vehicle *v, uint tile, int direction, byte trackbits) truelight@0: { truelight@0: TrainTrackFollowerData fd; truelight@0: int bits = trackbits; truelight@0: uint best_track; truelight@0: #if 0 truelight@0: int time = rdtsc(); truelight@0: static float f; truelight@0: #endif truelight@0: truelight@0: assert( (bits & ~0x3F) == 0); truelight@0: truelight@0: /* quick return in case only one possible direction is available */ truelight@0: if (KILL_FIRST_BIT(bits) == 0) truelight@0: return FIND_FIRST_BIT(bits); truelight@0: truelight@0: FillWithStationData(&fd, v); truelight@0: truelight@0: if (_patches.new_pathfinding) { truelight@0: fd.best_bird_dist = (uint)-1; truelight@0: fd.best_track_dist = (uint)-1; truelight@0: fd.best_track = 0xFF; truelight@0: NewTrainPathfind(tile - _tileoffs_by_dir[direction], direction, (TPFEnumProc*)TrainTrackFollower, &fd, NULL); truelight@0: truelight@0: // printf("Train %d %s\n", v->unitnumber, fd.best_track_dist == -1 ? "NOTFOUND" : "FOUND"); truelight@0: truelight@0: if (fd.best_track == 0xff) { truelight@0: // blaha truelight@0: best_track = FIND_FIRST_BIT(bits); truelight@0: } else { truelight@0: best_track = fd.best_track & 7; truelight@0: } truelight@0: } else { truelight@0: int i, r; truelight@0: uint best_bird_dist = 0; truelight@0: uint best_track_dist = 0; truelight@0: byte train_dir = v->direction & 3; truelight@0: truelight@0: truelight@0: best_track = -1; truelight@0: truelight@0: do { truelight@0: i = FIND_FIRST_BIT(bits); truelight@0: bits = KILL_FIRST_BIT(bits); truelight@0: truelight@0: fd.best_bird_dist = (uint)-1; truelight@0: fd.best_track_dist = (uint)-1; truelight@0: truelight@0: NewTrainPathfind(tile, _search_directions[i][direction], (TPFEnumProc*)TrainTrackFollower, &fd, NULL); truelight@0: if (best_track != -1) { truelight@0: if (best_track_dist == -1) { truelight@0: if (fd.best_track_dist == -1) { truelight@0: /* neither reached the destination, pick the one with the smallest bird dist */ truelight@0: if (fd.best_bird_dist > best_bird_dist) goto bad; truelight@0: if (fd.best_bird_dist < best_bird_dist) goto good; truelight@0: } else { truelight@0: /* we found the destination for the first time */ truelight@0: goto good; truelight@0: } truelight@0: } else { truelight@0: if (fd.best_track_dist == -1) { truelight@0: /* didn't find destination, but we've found the destination previously */ truelight@0: goto bad; truelight@0: } else { truelight@0: /* both old & new reached the destination, compare track length */ truelight@0: if (fd.best_track_dist > best_track_dist) goto bad; truelight@0: if (fd.best_track_dist < best_track_dist) goto good; truelight@0: } truelight@0: } truelight@0: truelight@0: /* if we reach this position, there's two paths of equal value so far. truelight@0: * pick one randomly. */ truelight@0: r = (byte)Random(); truelight@0: if (_pick_track_table[i] == train_dir) r += 80; truelight@0: if (_pick_track_table[best_track] == train_dir) r -= 80; truelight@0: truelight@0: if (r <= 127) goto bad; truelight@0: } truelight@0: good:; truelight@0: best_track = i; truelight@0: best_bird_dist = fd.best_bird_dist; truelight@0: best_track_dist = fd.best_track_dist; truelight@0: bad:; truelight@0: } while (bits != 0); truelight@0: // printf("Train %d %s\n", v->unitnumber, best_track_dist == -1 ? "NOTFOUND" : "FOUND"); truelight@0: assert(best_track != -1); truelight@0: } truelight@0: truelight@0: #if 0 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: truelight@0: while(true) { truelight@0: fd.best_bird_dist = (uint)-1; truelight@0: fd.best_track_dist = (uint)-1; truelight@0: truelight@0: NewTrainPathfind(v->tile, reverse ^ i, (TPFEnumProc*)TrainTrackFollower, &fd, NULL); truelight@0: truelight@0: if (best_track != -1) { truelight@0: if (best_bird_dist != 0) { truelight@0: if (fd.best_bird_dist != 0) { truelight@0: /* neither reached the destination, pick the one with the smallest bird dist */ truelight@0: if (fd.best_bird_dist > best_bird_dist) goto bad; truelight@0: if (fd.best_bird_dist < best_bird_dist) goto good; truelight@0: } else { truelight@0: /* we found the destination for the first time */ truelight@0: goto good; truelight@0: } truelight@0: } else { truelight@0: if (fd.best_bird_dist != 0) { truelight@0: /* didn't find destination, but we've found the destination previously */ truelight@0: goto bad; truelight@0: } else { truelight@0: /* both old & new reached the destination, compare track length */ truelight@0: if (fd.best_track_dist > best_track_dist) goto bad; truelight@0: if (fd.best_track_dist < best_track_dist) goto good; truelight@0: } truelight@0: } truelight@0: truelight@0: /* if we reach this position, there's two paths of equal value so far. truelight@0: * pick one randomly. */ truelight@0: r = (byte)Random(); truelight@0: if (_pick_track_table[i] == (v->direction & 3)) r += 80; truelight@0: if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80; truelight@0: if (r <= 127) goto bad; truelight@0: } truelight@0: good:; truelight@0: best_track = i; truelight@0: best_bird_dist = fd.best_bird_dist; truelight@0: best_track_dist = fd.best_track_dist; truelight@0: reverse_best = reverse; truelight@0: bad:; truelight@0: if (reverse != 0) truelight@0: break; truelight@0: reverse = 2; truelight@0: } truelight@0: truelight@0: return reverse_best != 0; truelight@0: } truelight@0: truelight@0: static bool ProcessTrainOrder(Vehicle *v) truelight@0: { truelight@0: uint order; truelight@0: bool result; truelight@0: truelight@0: // These are un-interruptible truelight@0: if ((v->next_order & OT_MASK) >= OT_GOTO_DEPOT && (v->next_order & OT_MASK) <= OT_LEAVESTATION) { truelight@0: truelight@0: // Let a depot order in the schedule interrupt. truelight@0: if ((v->next_order & (OT_MASK|OF_UNLOAD)) != (OT_GOTO_DEPOT|OF_UNLOAD)) truelight@0: return false; truelight@0: } truelight@0: truelight@0: if ((v->next_order & (OT_MASK|OF_UNLOAD|OF_FULL_LOAD)) == (OT_GOTO_DEPOT|OF_UNLOAD|OF_FULL_LOAD) && truelight@0: v->date_of_last_service+v->service_interval > _date) { truelight@0: v->cur_order_index++; truelight@0: } truelight@0: truelight@0: // check if we've reached the checkpoint? truelight@0: if ((v->next_order & OT_MASK) == OT_GOTO_CHECKPOINT && v->tile == v->dest_tile) { truelight@0: v->cur_order_index++; truelight@0: } truelight@0: truelight@0: // check if we've reached a non-stop station while TTDPatch nonstop is enabled.. truelight@0: if (_patches.new_nonstop && (v->next_order & OF_NON_STOP) && v->next_order_param == _map2[v->tile]) { truelight@0: 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@0: order = v->schedule_ptr[v->cur_order_index]; truelight@0: truelight@0: // If no order, do nothing. truelight@0: if (order == 0) { truelight@0: v->next_order = OT_NOTHING; truelight@0: v->dest_tile = 0; truelight@0: return false; truelight@0: } truelight@0: truelight@0: // If it is unchanged, keep it. truelight@0: if (order == (uint)((v->next_order | (v->next_order_param<<8)))) truelight@0: return false; truelight@0: truelight@0: // Otherwise set it, and determine the destination tile. truelight@0: v->next_order = (byte)order; truelight@0: v->next_order_param = (byte)(order >> 8); truelight@0: truelight@0: v->dest_tile = 0; truelight@0: truelight@0: result = false; truelight@0: if ((order & OT_MASK) == OT_GOTO_STATION) { truelight@0: if ( (byte)(order >> 8) == v->last_station_visited) truelight@0: v->last_station_visited = 0xFF; truelight@0: v->dest_tile = DEREF_STATION(order >> 8)->xy; truelight@0: result = CheckReverseTrain(v); truelight@0: } else if ((order & OT_MASK) == OT_GOTO_DEPOT) { truelight@0: v->dest_tile = _depots[order >> 8].xy; truelight@0: result = CheckReverseTrain(v); truelight@0: } else if ((order & OT_MASK) == OT_GOTO_CHECKPOINT) { truelight@0: v->dest_tile = _checkpoints[order >> 8].xy; truelight@0: result = CheckReverseTrain(v); truelight@0: } truelight@0: truelight@0: InvalidateVehicleOrderWidget(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: { truelight@0: if (v->next_order == OT_NOTHING) truelight@0: return; truelight@0: truelight@0: if (v->next_order != OT_DUMMY) { truelight@0: if ((v->next_order&OT_MASK) != 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. truelight@0: if (v->next_order & 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: truelight@0: if (v->next_order&OF_FULL_LOAD && CanFillVehicle(v)) { 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@0: 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@0: truelight@0: TrainPlayLeaveStationSound(v); truelight@0: truelight@0: { truelight@0: byte b = v->next_order; truelight@0: v->next_order = OT_LEAVESTATION; truelight@0: truelight@0: // If this was not the final order, don't remove it from the list. truelight@0: if (!(b & 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@0: InvalidateVehicleOrderWidget(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@0: if (v->vehstatus & VS_STOPPED || v->u.rail.flags & VRF_REVERSING) { truelight@0: accel = -v->acceleration * 2; truelight@0: } else { truelight@0: accel = v->acceleration; truelight@0: if (_patches.realistic_acceleration) { truelight@0: accel = GetRealisticAcceleration(v); truelight@0: } truelight@0: } truelight@0: truelight@0: spd = v->subspeed + accel * 2; truelight@0: v->subspeed = (byte)spd; truelight@0: v->cur_speed = spd = clamp(v->cur_speed + ((int)spd >> 8), 0, v->max_speed); 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@0: st = DEREF_STATION(station); truelight@0: if (!(st->had_vehicle_of_type & HVOT_TRAIN)) { truelight@0: st->had_vehicle_of_type |= HVOT_TRAIN; truelight@0: SET_DPARAM16(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? truelight@0: if ((v->next_order&OT_MASK) == OT_GOTO_STATION && v->next_order_param == (byte)station) { truelight@0: // Yeah, keep the load/unload flags truelight@0: // Non Stop now means if the order should be increased. truelight@0: v->next_order = (v->next_order & (OF_FULL_LOAD|OF_UNLOAD)) | OF_NON_STOP | OT_LOADING; truelight@0: } else { truelight@0: // No, just do a simple load truelight@0: v->next_order = OT_LOADING; truelight@0: } truelight@0: v->next_order_param = 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: } truelight@0: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); truelight@0: } truelight@0: truelight@0: static byte AfterSetTrainPos(Vehicle *v) truelight@0: { truelight@0: byte new_z, old_z; truelight@0: 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@0: v->u.rail.flags &= ~(VRF_GOINGUP | VRF_GOINGDOWN); truelight@0: truelight@0: if (new_z != old_z) { truelight@0: v->u.rail.flags |= (new_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN; 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: { truelight@0: uint offs = (GET_TILE_Y(new_tile) - GET_TILE_Y(old_tile) + 1) * 4 + truelight@0: GET_TILE_X(new_tile) - GET_TILE_X(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 */ truelight@0: static bool CheckCompatibleRail(Vehicle *v, uint tile) truelight@0: { truelight@0: if (IS_TILETYPE(tile, MP_RAILWAY) || truelight@0: IS_TILETYPE(tile, MP_STATION)) { truelight@0: truelight@0: } else if (IS_TILETYPE(tile, MP_TUNNELBRIDGE)) { truelight@0: if ((_map5[tile] & 0xC0) == 0xC0) {// is bridge middle part? truelight@0: TileInfo ti; truelight@0: FindLandscapeHeightByTile(&ti, tile); truelight@0: truelight@0: // correct Z position of a train going under a bridge on slopes truelight@0: if (CORRECT_Z(ti.tileh)) truelight@0: ti.z += 8; truelight@0: truelight@0: if(v->z_pos != ti.z) // train is going over bridge truelight@0: return true; truelight@0: } truelight@0: } else truelight@0: return true; truelight@0: truelight@0: if (_map_owner[tile] != v->owner || truelight@0: (v->subtype == 0 && (_map3_lo[tile] & 0xF) != v->u.rail.railtype)) truelight@0: return false; truelight@0: truelight@0: return true; 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; truelight@0: if (IS_TILETYPE(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: truelight@0: 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@0: truelight@0: u = v; truelight@0: BEGIN_ENUM_WAGONS(v) truelight@0: v->vehstatus |= VS_CRASHED; truelight@0: END_ENUM_WAGONS(v) truelight@0: truelight@0: InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, 4); truelight@0: } truelight@0: truelight@0: static int CountPassengersInTrain(Vehicle *v) truelight@0: { truelight@0: int num = 0; truelight@0: BEGIN_ENUM_WAGONS(v) truelight@0: if (v->cargo_type == 0) 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 darkvater@22: * so, destroys this vehicle, and the other vehicle if its subtype is 0 (?). 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; truelight@0: Vehicle *coll; 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@0: truelight@0: 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 */ truelight@0: coll = VehicleFromPos(v->tile, &tcc, (VehicleFromPosProc*)FindTrainCollideEnum); truelight@0: if (coll == NULL) truelight@0: return; truelight@0: truelight@0: coll = GetFirstVehicleInChain(coll); truelight@0: truelight@0: /* it can't collide with its own wagons */ truelight@0: if (v == coll) truelight@0: return; truelight@0: truelight@0: //two drivers + passangers killed in train v truelight@0: num = 2 + CountPassengersInTrain(v); truelight@0: 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); truelight@0: if (coll->subtype == 0) truelight@0: SetVehicleCrashed(coll); truelight@0: truelight@0: truelight@0: SET_DPARAM16(0, num); truelight@0: 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); truelight@0: SndPlayVehicleFx(17, v); truelight@0: } truelight@0: truelight@0: static void *CheckVehicleAtSignal(Vehicle *v, void *data) truelight@0: { truelight@0: uint32 d = (uint32)data; truelight@0: truelight@0: if (v->type == VEH_Train && v->subtype == 0 && 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; truelight@0: int dir, i; 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@0: 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@0: r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); truelight@0: if (r & 0x8) truelight@0: goto invalid_rail; truelight@0: if (r & 0x2) { truelight@0: TrainEnterStation(v, r >> 8); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (v->next_order == OT_LEAVESTATION) { truelight@0: v->next_order = OT_NOTHING; truelight@0: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); truelight@0: } truelight@0: } truelight@0: } else { truelight@0: /* A new tile is about to be entered. */ truelight@0: truelight@0: /* Determine what direction we're entering the new tile from */ truelight@0: dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile); truelight@0: assert(dir==1 || dir==3 || dir==5 || dir==7); truelight@0: truelight@0: /* Get the status of the tracks in the new tile and mask truelight@0: * away the bits that aren't reachable. */ truelight@0: ts = GetTileTrackStatus(gp.new_tile, 0) & _reachable_tracks[dir >> 1]; 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); truelight@0: if ( (byte) tracks == 0) truelight@0: goto invalid_rail; truelight@0: truelight@0: /* Check if the new tile contrains tracks that are compatible truelight@0: * with the current train, if not, bail out. */ truelight@0: if (!CheckCompatibleRail(v, gp.new_tile)) truelight@0: goto invalid_rail; 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 */ truelight@0: chosen_track = 1 << ChooseTrainTrack(v, gp.new_tile, dir>>1, (byte)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@0: truelight@0: /* The wagon is active, simply follow the prev vehicle. */ truelight@0: chosen_track = (byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & tracks); 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: { truelight@0: const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][dir>>1]; 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@0: 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); truelight@0: if (r&0x8) truelight@0: goto invalid_rail; truelight@0: truelight@0: if (v->subtype == 0) 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; truelight@0: } truelight@0: truelight@0: if (v->subtype == 0) truelight@0: TrainMovedChangeSignals(gp.new_tile, dir>>1); 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) truelight@0: TrainMovedChangeSignals(gp.old_tile, (dir>>1) ^ 2); truelight@0: 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@0: truelight@0: if (IS_TILETYPE(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); truelight@0: goto next_vehicle; truelight@0: } truelight@0: common:; truelight@0: truelight@0: /* update image of train, as well as delta XY */ truelight@0: dir = GetNewVehicleDirection(v, gp.x, gp.y); truelight@0: UpdateTrainDeltaXY(v, dir); truelight@0: v->cur_image = GetTrainImage(v, dir); 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@0: old_z = AfterSetTrainPos(v); truelight@0: 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@0: truelight@0: if (!(_map3_lo[gp.new_tile] & _signal_otherdir[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; truelight@0: } else if (_map3_lo[gp.new_tile] & _signal_onedir[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) { truelight@0: uint o_tile = gp.new_tile + _tileoffs_by_dir[dir>>1]; 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@0: 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: truelight@0: static void DeleteLastWagon(Vehicle *v) truelight@0: { truelight@0: Vehicle *u = v; truelight@0: int t; truelight@0: 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); truelight@0: InvalidateWindow(WC_TRAINS_LIST, v->owner); truelight@0: InvalidateWindow(WC_COMPANY, v->owner); truelight@0: truelight@0: BeginVehicleMove(v); truelight@0: EndVehicleMove(v); truelight@0: DeleteVehicle(v); truelight@0: truelight@0: if (!((t=v->u.rail.track) & 0xC0)) { truelight@0: SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(t)); truelight@0: } 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@0: truelight@0: do { truelight@0: 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@0: AfterSetTrainPos(v); 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@0: truelight@0: if (state == 4) { truelight@0: CreateEffectVehicleRel(v, 4, 4, 8, EV_CRASHED_SMOKE); truelight@0: } truelight@0: truelight@0: 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) { truelight@0: 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), truelight@0: EV_DEMOLISH); 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: truelight@0: if (state >= 4440 && !(v->tick_counter&0x1F)) truelight@0: DeleteLastWagon(v); 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@0: truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: truelight@0: SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? 0xE : 0x3A, 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@0: static void 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@0: 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@0: return; truelight@0: truelight@0: tile = v->tile; truelight@0: truelight@0: // tunnel entrance? truelight@0: if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && truelight@0: (_map5[tile] & 0xF0) == 0 && (byte)((_map5[tile] & 3)*2+1) == v->direction) truelight@0: return; truelight@0: truelight@0: // depot? truelight@0: if (IS_TILETYPE(tile, MP_RAILWAY) && (_map5[tile] & 0xFC) == 0xC0) truelight@0: return; 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 */ truelight@0: tile += _tileoffs_by_dir[t]; darkvater@22: // determine the track status on the next tile. truelight@0: ts = GetTileTrackStatus(tile, 0) & _reachable_tracks[t]; truelight@0: darkvater@22: /* Calc position within the current tile ?? */ truelight@0: x = v->x_pos & 0xF; truelight@0: y = v->y_pos & 0xF; truelight@0: 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@0: if ((ts &= (ts >> 16)) == 0) { truelight@0: // make a rail/road crossing red truelight@0: if (IS_TILETYPE(tile, MP_STREET) && (_map5[tile] & 0xF0)==0x10) { truelight@0: if (!(_map5[tile] & 4)) { truelight@0: _map5[tile] |= 4; truelight@0: SndPlayVehicleFx(12, v); truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@0: return; truelight@0: } truelight@0: } else if (x + 4 > 15) { truelight@0: v->cur_speed = 0; truelight@0: ReverseTrainDirection(v); truelight@0: return; 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@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@0: if (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: truelight@0: if ((v->next_order & OT_MASK) == 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: truelight@0: if (v->subtype == 0) { truelight@0: TrainLocoHandler(v, false); truelight@0: truelight@0: // make sure vehicle wasn't deleted. truelight@0: if (v->type == VEH_Train && v->subtype == 0) truelight@0: TrainLocoHandler(v, true); truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: static const byte _depot_track_ind[4] = {0,1,0,1}; truelight@0: truelight@0: void TrainEnterDepot(Vehicle *v, uint tile) truelight@0: { truelight@0: byte t; truelight@0: truelight@0: SetSignalsOnBothDir(tile, _depot_track_ind[_map5[tile]&3]); truelight@0: truelight@0: if (v->subtype != 0) truelight@0: v = GetFirstVehicleInChain(v); truelight@0: truelight@0: v->date_of_last_service = _date; truelight@0: v->breakdowns_since_last_service = 0; truelight@0: v->reliability = _engines[v->engine_type].reliability; 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: truelight@0: MaybeRenewVehicle(v, EstimateTrainCost(&_rail_vehicle_info[v->engine_type])); truelight@0: truelight@0: if ((v->next_order&OT_MASK) == OT_GOTO_DEPOT) { truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@0: truelight@0: t = v->next_order; truelight@0: v->next_order = OT_DUMMY; truelight@0: truelight@0: // Part of the schedule? truelight@0: if (t & OF_UNLOAD) { v->u.rail.days_since_order_progr = 0; v->cur_order_index++; } truelight@0: truelight@0: // User initiated? truelight@0: else if (t & OF_FULL_LOAD) { truelight@0: v->vehstatus |= VS_STOPPED; truelight@0: if (v->owner == _local_player) { truelight@0: SET_DPARAM16(0, v->unitnumber); truelight@0: AddNewsItem( 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, truelight@0: 0); truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void CheckIfTrainNeedsService(Vehicle *v) truelight@0: { truelight@0: uint tile; truelight@0: byte depot; truelight@0: truelight@76: if (_patches.servint_trains == 0) truelight@0: return; truelight@0: truelight@0: if (v->date_of_last_service + v->service_interval > _date) truelight@0: return; truelight@0: truelight@0: if (v->vehstatus & VS_STOPPED) truelight@0: return; truelight@0: truelight@76: if (_patches.gotodepot && ScheduleHasDepotOrders(v->schedule_ptr)) truelight@0: return; truelight@0: truelight@0: // Don't interfere with a depot visit scheduled by the user, or a truelight@0: // depot visit by the order list. truelight@0: if ((v->next_order & OT_MASK) == OT_GOTO_DEPOT && truelight@0: (v->next_order & (OF_FULL_LOAD|OF_UNLOAD)) != 0) truelight@0: return; truelight@0: truelight@0: tile = FindClosestTrainDepot(v); truelight@0: if (tile == (uint)-1 || GetTileDist(v->tile, tile) > 12) { truelight@0: if ((v->next_order & OT_MASK) == OT_GOTO_DEPOT) { truelight@0: v->next_order = OT_DUMMY; truelight@0: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); truelight@0: } truelight@0: return; truelight@0: } truelight@0: truelight@0: depot = GetDepotByTile(tile); truelight@0: truelight@0: if ((v->next_order & OT_MASK) == OT_GOTO_DEPOT && v->next_order_param != depot && !CHANCE16(3,16)) truelight@0: return; truelight@0: truelight@0: v->next_order = OT_GOTO_DEPOT | OF_NON_STOP; truelight@0: v->next_order_param = depot; truelight@0: v->dest_tile = tile; truelight@0: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4); truelight@0: } truelight@0: truelight@0: int32 GetTrainRunningCost(Vehicle *v) truelight@0: { truelight@0: int32 cost = 0; truelight@0: truelight@0: do { truelight@0: const RailVehicleInfo *rvi = &_rail_vehicle_info[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: truelight@0: if (v->subtype == 0) { truelight@0: CheckVehicleBreakdown(v); truelight@0: AgeVehicle(v); truelight@0: truelight@0: CheckIfTrainNeedsService(v); truelight@0: truelight@0: // check if train hasn't advanced in its order list for a set number of days truelight@0: if (_patches.lost_train_days && v->num_orders && !(v->vehstatus & VS_STOPPED) && ++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; truelight@0: SET_DPARAM16(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: dominik@19: CheckOrders(v); dominik@19: truelight@0: /* update destination */ truelight@0: if ((v->next_order & OT_MASK) == OT_GOTO_STATION && truelight@0: (tile=DEREF_STATION(v->next_order_param)->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); truelight@0: InvalidateWindow(WC_TRAINS_LIST, v->owner); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: void TrainsYearlyLoop() truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->type == VEH_Train && v->subtype == 0) { truelight@0: 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) { truelight@0: SET_DPARAM32(1, v->profit_this_year); truelight@0: SET_DPARAM16(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: { truelight@0: if (v->subtype != 0) v = GetFirstVehicleInChain(v); truelight@0: ShowTrainViewWindow(v); truelight@0: } truelight@0: truelight@0: void InitializeTrains() truelight@0: { truelight@0: _age_cargo_skip_counter = 1; truelight@0: } truelight@0: truelight@0: int ScheduleHasDepotOrders(uint16 *schedule) truelight@0: { truelight@0: for (;*schedule!=0;schedule++) truelight@0: if ((*schedule&OT_MASK) == OT_GOTO_DEPOT) truelight@0: return true; truelight@0: return false; truelight@0: }