Changeset - r19165:1353a268dd5f
[Not reviewed]
master
0 1 0
michi_cc - 12 years ago 2012-03-20 13:11:20
michi_cc@openttd.org
(svn r24053) -Fix (r23947) [FS#5111]: Crash when timetabling a maximum travel speed of 0.
1 file changed with 1 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/timetable_cmd.cpp
Show inline comments
 
@@ -25,192 +25,193 @@
 
 * @param order_number The index of the timetable in the order list.
 
 * @param val          The new data of the timetable entry.
 
 * @param mtf          Which part of the timetable entry to change.
 
 */
 
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val, ModifyTimetableFlags mtf)
 
{
 
	Order *order = v->GetOrder(order_number);
 
	int delta = 0;
 

	
 
	switch (mtf) {
 
		case MTF_WAIT_TIME:
 
			delta = val - order->wait_time;
 
			order->wait_time = val;
 
			break;
 

	
 
		case MTF_TRAVEL_TIME:
 
			delta = val - order->travel_time;
 
			order->travel_time = val;
 
			break;
 

	
 
		case MTF_TRAVEL_SPEED:
 
			order->max_speed = val;
 
			break;
 

	
 
		default:
 
			NOT_REACHED();
 
	}
 
	v->orders.list->UpdateOrderTimetable(delta);
 

	
 
	for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
 
		if (v->cur_real_order_index == order_number && v->current_order.Equals(*order)) {
 
			switch (mtf) {
 
				case MTF_WAIT_TIME:
 
					v->current_order.wait_time = val;
 
					break;
 

	
 
				case MTF_TRAVEL_TIME:
 
					v->current_order.travel_time = val;
 
					break;
 

	
 
				case MTF_TRAVEL_SPEED:
 
					v->current_order.max_speed = val;
 
					break;
 

	
 
				default:
 
					NOT_REACHED();
 
			}
 
		}
 
		SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
 
	}
 
}
 

	
 
/**
 
 * Change timetable data of an order.
 
 * @param tile Not used.
 
 * @param flags Operation to perform.
 
 * @param p1 Various bitstuffed elements
 
 * - p1 = (bit  0-19) - Vehicle with the orders to change.
 
 * - p1 = (bit 20-27) - Order index to modify.
 
 * - p1 = (bit 28-29) - Timetable data to change (@see ModifyTimetableFlags)
 
 * @param p2 The amount of time to wait.
 
 * - p2 = (bit  0-15) - The data to modify as specified by p1 bits 28-29.
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	VehicleID veh = GB(p1, 0, 20);
 

	
 
	Vehicle *v = Vehicle::GetIfValid(veh);
 
	if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
 

	
 
	CommandCost ret = CheckOwnership(v->owner);
 
	if (ret.Failed()) return ret;
 

	
 
	VehicleOrderID order_number = GB(p1, 20, 8);
 
	Order *order = v->GetOrder(order_number);
 
	if (order == NULL || order->IsType(OT_IMPLICIT)) return CMD_ERROR;
 

	
 
	ModifyTimetableFlags mtf = Extract<ModifyTimetableFlags, 28, 2>(p1);
 
	if (mtf >= MTF_END) return CMD_ERROR;
 

	
 
	int wait_time   = order->wait_time;
 
	int travel_time = order->travel_time;
 
	int max_speed   = order->max_speed;
 
	switch (mtf) {
 
		case MTF_WAIT_TIME:
 
			wait_time = GB(p2, 0, 16);
 
			break;
 

	
 
		case MTF_TRAVEL_TIME:
 
			travel_time = GB(p2, 0, 16);
 
			break;
 

	
 
		case MTF_TRAVEL_SPEED:
 
			max_speed = GB(p2, 0, 16);
 
			if (max_speed == 0) max_speed = UINT16_MAX; // Disable speed limit.
 
			break;
 

	
 
		default:
 
			NOT_REACHED();
 
	}
 

	
 
	if (wait_time != order->wait_time) {
 
		switch (order->GetType()) {
 
			case OT_GOTO_STATION:
 
				if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_ERROR_TIMETABLE_NOT_STOPPING_HERE);
 
				break;
 

	
 
			case OT_CONDITIONAL:
 
				break;
 

	
 
			default: return_cmd_error(STR_ERROR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
 
		}
 
	}
 

	
 
	if (travel_time != order->travel_time && order->IsType(OT_CONDITIONAL)) return CMD_ERROR;
 
	if (max_speed != order->max_speed && (order->IsType(OT_CONDITIONAL) || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		if (wait_time   != order->wait_time)   ChangeTimetable(v, order_number, wait_time,   MTF_WAIT_TIME);
 
		if (travel_time != order->travel_time) ChangeTimetable(v, order_number, travel_time, MTF_TRAVEL_TIME);
 
		if (max_speed   != order->max_speed)   ChangeTimetable(v, order_number, max_speed,   MTF_TRAVEL_SPEED);
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
/**
 
 * Clear the lateness counter to make the vehicle on time.
 
 * @param tile Not used.
 
 * @param flags Operation to perform.
 
 * @param p1 Various bitstuffed elements
 
 * - p1 = (bit  0-19) - Vehicle with the orders to change.
 
 * @param p2 unused
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	VehicleID veh = GB(p1, 0, 20);
 

	
 
	Vehicle *v = Vehicle::GetIfValid(veh);
 
	if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
 

	
 
	CommandCost ret = CheckOwnership(v->owner);
 
	if (ret.Failed()) return ret;
 

	
 
	if (flags & DC_EXEC) {
 
		v->lateness_counter = 0;
 
		SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
/**
 
 * Set the start date of the timetable.
 
 * @param tile Not used.
 
 * @param flags Operation to perform.
 
 * @param p1 Vehicle id.
 
 * @param p2 The timetable start date.
 
 * @param text Not used.
 
 * @return The error or cost of the operation.
 
 */
 
CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
 
	if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
 

	
 
	CommandCost ret = CheckOwnership(v->owner);
 
	if (ret.Failed()) return ret;
 

	
 
	/* Don't let a timetable start more than 15 years into the future or 1 year in the past. */
 
	Date start_date = (Date)p2;
 
	if (start_date < 0 || start_date > MAX_DAY) return CMD_ERROR;
 
	if (start_date - _date > 15 * DAYS_IN_LEAP_YEAR) return CMD_ERROR;
 
	if (_date - start_date > DAYS_IN_LEAP_YEAR) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		v->lateness_counter = 0;
 
		ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
 
		v->timetable_start = start_date;
 

	
 
		SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 

	
 
/**
 
 * Start or stop filling the timetable automatically from the time the vehicle
0 comments (0 inline, 0 general)