Changeset - r18913:fd2dc944ef3a
[Not reviewed]
master
0 4 0
michi_cc - 12 years ago 2012-01-08 12:47:54
michi_cc@openttd.org
(svn r23773) -Change: [NewGRF] Update all cached train properties if a train vehicle enters a new railtype.
4 files changed with 5 insertions and 21 deletions:
0 comments (0 inline, 0 general)
src/rail_cmd.cpp
Show inline comments
 
@@ -1649,25 +1649,25 @@ CommandCost CmdConvertRail(TileIndex til
 
				cost.AddCost(RailConvertCost(type, totype));
 
				break;
 
		}
 

	
 
		for (uint i = 0; i < vehicles_affected.Length(); ++i) {
 
			TryPathReserve(vehicles_affected[i], true);
 
		}
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		/* Railtype changed, update trains as when entering different track */
 
		for (Train **v = affected_trains.Begin(); v != affected_trains.End(); v++) {
 
			(*v)->RailtypeChanged();
 
			(*v)->ConsistChanged(true);
 
		}
 
	}
 

	
 
	delete iter;
 
	return (cost.GetCost() == 0) ? error : cost;
 
}
 

	
 
static CommandCost RemoveTrainDepot(TileIndex tile, DoCommandFlag flags)
 
{
 
	if (_current_company != OWNER_WATER) {
 
		CommandCost ret = CheckTileOwnership(tile);
 
		if (ret.Failed()) return ret;
src/saveload/vehicle_sl.cpp
Show inline comments
 
@@ -521,24 +521,27 @@ void FixupTrainLengths()
 

	
 
				/* If the next wagon is still in a depot, check if it shouldn't be outside already. */
 
				if (next != NULL && next->track == TRACK_BIT_DEPOT) {
 
					int d = TicksToLeaveDepot(u);
 
					if (d <= 0) {
 
						/* Next vehicle should have left the depot already, show it and pull forward. */
 
						next->vehstatus &= ~VS_HIDDEN;
 
						next->track = TrackToTrackBits(GetRailDepotTrack(next->tile));
 
						for (int i = 0; i >= d; i--) TrainController(next, NULL);
 
					}
 
				}
 
			}
 

	
 
			/* Update all cached properties after moving the vehicle chain around. */
 
			Train::From(v)->ConsistChanged(true);
 
		}
 
	}
 
}
 

	
 
static uint8  _cargo_days;
 
static uint16 _cargo_source;
 
static uint32 _cargo_source_xy;
 
static uint16 _cargo_count;
 
static uint16 _cargo_paid_for;
 
static Money  _cargo_feeder_share;
 
static uint32 _cargo_loaded_at_xy;
 

	
src/train.h
Show inline comments
 
@@ -107,26 +107,24 @@ struct Train FINAL : public GroundVehicl
 
	void OnNewDay();
 
	uint Crash(bool flooded = false);
 
	Trackdir GetVehicleTrackdir() const;
 
	TileIndex GetOrderStationLocation(StationID station);
 
	bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
 

	
 
	void ReserveTrackUnderConsist() const;
 

	
 
	int GetCurveSpeedLimit() const;
 

	
 
	void ConsistChanged(bool same_length);
 

	
 
	void RailtypeChanged();
 

	
 
	int UpdateSpeed();
 

	
 
	void UpdateAcceleration();
 

	
 
	int GetCurrentMaxSpeed() const;
 

	
 
	/**
 
	 * Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
 
	 * @return Next vehicle in the consist.
 
	 */
 
	inline Train *GetNextUnit() const
 
	{
src/train_cmd.cpp
Show inline comments
 
@@ -104,38 +104,24 @@ void CheckTrainsLengths()
 
							DoCommandP(0, PM_PAUSED_ERROR, 1, CMD_PAUSE);
 
						}
 
						/* Break so we warn only once for each train. */
 
						break;
 
					}
 
				}
 
			}
 
		}
 
	}
 
}
 

	
 
/**
 
 * Update visual effect, power and acceleration caches.
 
 * Called when a vehicle in the consist enters a different railtype.
 
 */
 
void Train::RailtypeChanged()
 
{
 
	for (Train *u = this; u != NULL; u = u->Next()) {
 
		/* The wagon-is-powered-state should not change, so the weight does not change. */
 
		u->UpdateVisualEffect(false);
 
	}
 
	this->PowerChanged();
 
	if (this->IsFrontEngine()) this->UpdateAcceleration();
 
}
 

	
 
/**
 
 * Recalculates the cached stuff of a train. Should be called each time a vehicle is added
 
 * to/removed from the chain, and when the game is loaded.
 
 * Note: this needs to be called too for 'wagon chains' (in the depot, without an engine)
 
 * @param same_length should length of vehicles stay the same?
 
 */
 
void Train::ConsistChanged(bool same_length)
 
{
 
	uint16 max_speed = UINT16_MAX;
 

	
 
	assert(this->IsFrontEngine() || this->IsFreeWagon());
 

	
 
	const RailVehicleInfo *rvi_v = RailVehInfo(this->engine_type);
 
@@ -1580,27 +1566,24 @@ void ReverseTrainSwapVeh(Train *v, int l
 

	
 
		SwapTrainFlags(&a->gv_flags, &b->gv_flags);
 

	
 
		UpdateStatusAfterSwap(a);
 
		UpdateStatusAfterSwap(b);
 
	} else {
 
		/* Swap GVF_GOINGUP_BIT/GVF_GOINGDOWN_BIT.
 
		 * This is a little bit redundant way, a->gv_flags will
 
		 * be (re)set twice, but it reduces code duplication */
 
		SwapTrainFlags(&a->gv_flags, &a->gv_flags);
 
		UpdateStatusAfterSwap(a);
 
	}
 

	
 
	/* Update power of the train in case tiles were different rail type. */
 
	v->RailtypeChanged();
 
}
 

	
 

	
 
/**
 
 * Check if the vehicle is a train
 
 * @param v vehicle on tile
 
 * @return v if it is a train, NULL otherwise
 
 */
 
static Vehicle *TrainOnTileEnum(Vehicle *v, void *)
 
{
 
	return (v->type == VEH_TRAIN) ? v : NULL;
 
}
 
@@ -3237,25 +3220,25 @@ bool TrainController(Train *v, Vehicle *
 
					Trackdir tdir = TrackDirectionToTrackdir(track, chosen_dir);
 
					if (v->IsFrontEngine() && HasPbsSignalOnTrackdir(gp.new_tile, tdir)) {
 
						SetSignalStateByTrackdir(gp.new_tile, tdir, SIGNAL_STATE_RED);
 
						MarkTileDirtyByTile(gp.new_tile);
 
					}
 

	
 
					/* Clear any track reservation when the last vehicle leaves the tile */
 
					if (v->Next() == NULL) ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());
 

	
 
					v->tile = gp.new_tile;
 

	
 
					if (GetTileRailType(gp.new_tile) != GetTileRailType(gp.old_tile)) {
 
						v->First()->RailtypeChanged();
 
						v->First()->ConsistChanged(true);
 
					}
 

	
 
					v->track = chosen_track;
 
					assert(v->track);
 
				}
 

	
 
				/* We need to update signal status, but after the vehicle position hash
 
				 * has been updated by UpdateInclination() */
 
				update_signals_crossing = true;
 

	
 
				if (chosen_dir != v->direction) {
 
					if (prev == NULL && _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) {
0 comments (0 inline, 0 general)