Files @ r17613:a9b2554a5d79
Branch filter:

Location: cpp/openttd-patchpack/source/src/timetable_cmd.cpp - annotation

rubidium
(svn r22387) -Fix-ish [FS#4601]: Windows' recv seems to return "graceful closed" before having passed the remaining buffer which causes OpenTTD to think all connections are "incorrectly" terminated, i.e. without the "I'm leaving" packet from the client. So let the client wait a tiny bit after sending the "I'm leaving" packet and before gracefully closing the connection
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r8116:df67d3c5e4fd
r17263:a75c87f7e037
r13757:cb9ca7946c57
r8131:7a50db7be0ff
r8144:1432edd15267
r6981:8e1c715f9e05
r8264:d493cb51fe8a
r6981:8e1c715f9e05
r7066:7459a71fc283
r7066:7459a71fc283
r11986:6596046384a5
r10546:a4ec290c1c46
r7066:7459a71fc283
r7066:7459a71fc283
r10546:a4ec290c1c46
r7066:7459a71fc283
r7066:7459a71fc283
r10546:a4ec290c1c46
r7066:7459a71fc283
r7066:7459a71fc283
r10546:a4ec290c1c46
r7066:7459a71fc283
r10033:424b14d0d013
r17184:a6dd7165a9b8
r10033:424b14d0d013
r10033:424b14d0d013
r10033:424b14d0d013
r10033:424b14d0d013
r10033:424b14d0d013
r7066:7459a71fc283
r13024:48c81d0b078a
r7809:a8c0806e350f
r7066:7459a71fc283
r7066:7459a71fc283
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r15878:2d36635f395d
r15878:2d36635f395d
r15878:2d36635f395d
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r15878:2d36635f395d
r13057:58af81fcdcf8
r13057:58af81fcdcf8
r6981:8e1c715f9e05
r11090:9276cea703d4
r6981:8e1c715f9e05
r15878:2d36635f395d
r6981:8e1c715f9e05
r11944:3b8f9f948e8d
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r6981:8e1c715f9e05
r15878:2d36635f395d
r11986:6596046384a5
r16897:d3b3d1abd708
r6981:8e1c715f9e05
r15878:2d36635f395d
r9665:f312055347cc
r10227:5c84b06816db
r10227:5c84b06816db
r15877:3aed46ca22cb
r9665:f312055347cc
r9641:a78fbf5c85cc
r9665:f312055347cc
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r10227:5c84b06816db
r9665:f312055347cc
r9665:f312055347cc
r12622:202e83a6cee7
r9665:f312055347cc
r9665:f312055347cc
r9665:f312055347cc
r9665:f312055347cc
r9665:f312055347cc
r12622:202e83a6cee7
r9665:f312055347cc
r9665:f312055347cc
r9665:f312055347cc
r10227:5c84b06816db
r9665:f312055347cc
r6981:8e1c715f9e05
r10227:5c84b06816db
r10227:5c84b06816db
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r15878:2d36635f395d
r13057:58af81fcdcf8
r13057:58af81fcdcf8
r13057:58af81fcdcf8
r6981:8e1c715f9e05
r11090:9276cea703d4
r6981:8e1c715f9e05
r15878:2d36635f395d
r6981:8e1c715f9e05
r11944:3b8f9f948e8d
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r13755:bf4fbd1c24d2
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r7066:7459a71fc283
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r15858:d75fb1dcda49
r15858:d75fb1dcda49
r15858:d75fb1dcda49
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r15878:2d36635f395d
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r7066:7459a71fc283
r7066:7459a71fc283
r7066:7459a71fc283
r7066:7459a71fc283
r7066:7459a71fc283
r7066:7459a71fc283
r10341:c21fa463ba20
r10341:c21fa463ba20
r10341:c21fa463ba20
r13057:58af81fcdcf8
r13057:58af81fcdcf8
r7066:7459a71fc283
r11090:9276cea703d4
r7066:7459a71fc283
r15878:2d36635f395d
r7066:7459a71fc283
r11944:3b8f9f948e8d
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r14805:8c416234ced2
r7066:7459a71fc283
r7066:7459a71fc283
r10341:c21fa463ba20
r10677:1a6f3c7876cf
r10677:1a6f3c7876cf
r10677:1a6f3c7876cf
r7931:4c17a74c399e
r7929:67846e70abf8
r7066:7459a71fc283
r10677:1a6f3c7876cf
r10341:c21fa463ba20
r7066:7459a71fc283
r13757:cb9ca7946c57
r10341:c21fa463ba20
r7066:7459a71fc283
r7929:67846e70abf8
r10341:c21fa463ba20
r7066:7459a71fc283
r7066:7459a71fc283
r16915:70e55512434f
r16915:70e55512434f
r16915:70e55512434f
r16915:70e55512434f
r16915:70e55512434f
r16915:70e55512434f
r16915:70e55512434f
r11042:9a00dc9e0e93
r7809:a8c0806e350f
r7809:a8c0806e350f
r7066:7459a71fc283
r7066:7459a71fc283
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r16897:d3b3d1abd708
r16897:d3b3d1abd708
r16897:d3b3d1abd708
r16897:d3b3d1abd708
r16897:d3b3d1abd708
r16897:d3b3d1abd708
r7066:7459a71fc283
r10341:c21fa463ba20
r10341:c21fa463ba20
r13757:cb9ca7946c57
r17184:a6dd7165a9b8
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r13757:cb9ca7946c57
r8970:a7dc16677ffd
r13757:cb9ca7946c57
r8970:a7dc16677ffd
r7066:7459a71fc283
r7928:a80e7e05d6c5
r7066:7459a71fc283
r8970:a7dc16677ffd
r10341:c21fa463ba20
r10341:c21fa463ba20
r10341:c21fa463ba20
r10341:c21fa463ba20
r10341:c21fa463ba20
r10341:c21fa463ba20
r10341:c21fa463ba20
r10341:c21fa463ba20
r10341:c21fa463ba20
r10341:c21fa463ba20
r7066:7459a71fc283
r7066:7459a71fc283
r17084:c81e0e9afe2e
r17084:c81e0e9afe2e
r17084:c81e0e9afe2e
r17084:c81e0e9afe2e
r17084:c81e0e9afe2e
r17084:c81e0e9afe2e
r17084:c81e0e9afe2e
r17084:c81e0e9afe2e
r7066:7459a71fc283
r17184:a6dd7165a9b8
r10341:c21fa463ba20
r10341:c21fa463ba20
r17184:a6dd7165a9b8
r10341:c21fa463ba20
r10341:c21fa463ba20
r10341:c21fa463ba20
r7929:67846e70abf8
r10341:c21fa463ba20
r7066:7459a71fc283
r10341:c21fa463ba20
r8970:a7dc16677ffd
r7066:7459a71fc283
r10341:c21fa463ba20
r10341:c21fa463ba20
r7062:6a1de70754de
r7062:6a1de70754de
r7062:6a1de70754de
r7066:7459a71fc283
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r6981:8e1c715f9e05
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r17085:00941d1d17ff
r9941:945cc0c6ccbc
r13024:48c81d0b078a
r7809:a8c0806e350f
r6981:8e1c715f9e05
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file timetable_cmd.cpp Commands related to time tabling. */

#include "stdafx.h"
#include "command_func.h"
#include "company_func.h"
#include "date_func.h"
#include "window_func.h"
#include "vehicle_base.h"

#include "table/strings.h"

static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 time, bool is_journey)
{
	Order *order = v->GetOrder(order_number);
	int delta;

	if (is_journey) {
		delta = time - order->travel_time;
		order->travel_time = time;
	} else {
		delta = time - order->wait_time;
		order->wait_time = time;
	}
	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)) {
			if (is_journey) {
				v->current_order.travel_time = time;
			} else {
				v->current_order.wait_time = time;
			}
		}
		SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
	}
}

/**
 * Add or remove waiting times from 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) - Whether to change the waiting time or the travelling
 *                      time.
 * @param p2 The amount of time to wait.
 * - p2 = (bit  0-15) - Waiting or travelling time as specified by p1 bit 28
 * @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_AUTOMATIC)) return CMD_ERROR;

	bool is_journey = HasBit(p1, 28);

	int wait_time   = order->wait_time;
	int travel_time = order->travel_time;
	if (is_journey) {
		travel_time = GB(p2, 0, 16);
	} else {
		wait_time   = GB(p2, 0, 16);
	}

	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 (flags & DC_EXEC) {
		if (wait_time   != order->wait_time)   ChangeTimetable(v, order_number, wait_time,   false);
		if (travel_time != order->travel_time) ChangeTimetable(v, order_number, travel_time, true);
	}

	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
 * actually takes to complete it. When starting to autofill the current times
 * are cleared and the timetable will start again from scratch.
 * @param tile Not used.
 * @param flags Operation to perform.
 * @param p1 Vehicle index.
 * @param p2 Various bitstuffed elements
 * - p2 = (bit 0) - Set to 1 to enable, 0 to disable autofill.
 * - p2 = (bit 1) - Set to 1 to preserve waiting times in non-destructive mode
 * @param text unused
 * @return the cost of this operation or an error
 */
CommandCost CmdAutofillTimetable(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) {
		if (HasBit(p2, 0)) {
			/* Start autofilling the timetable, which clears the
			 * "timetable has started" bit. Times are not cleared anymore, but are
			 * overwritten when the order is reached now. */
			SetBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
			ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);

			/* Overwrite waiting times only if they got longer */
			if (HasBit(p2, 1)) SetBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);

			v->timetable_start = 0;
			v->lateness_counter = 0;
		} else {
			ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
			ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
		}

		for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
			if (v2 != v) {
				/* Stop autofilling; only one vehicle at a time can perform autofill */
				ClrBit(v2->vehicle_flags, VF_AUTOFILL_TIMETABLE);
				ClrBit(v2->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
			}
			SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
		}
	}

	return CommandCost();
}

void UpdateVehicleTimetable(Vehicle *v, bool travelling)
{
	uint timetabled = travelling ? v->current_order.travel_time : v->current_order.wait_time;
	uint time_taken = v->current_order_time;

	v->current_order_time = 0;

	if (v->current_order.IsType(OT_AUTOMATIC)) return; // no timetabling of auto orders

	VehicleOrderID first_manual_order = 0;
	for (Order *o = v->GetFirstOrder(); o != NULL && o->IsType(OT_AUTOMATIC); o = o->next) {
		++first_manual_order;
	}

	bool just_started = false;

	/* This vehicle is arriving at the first destination in the timetable. */
	if (v->cur_real_order_index == first_manual_order && travelling) {
		/* If the start date hasn't been set, or it was set automatically when
		 * the vehicle last arrived at the first destination, update it to the
		 * current time. Otherwise set the late counter appropriately to when
		 * the vehicle should have arrived. */
		just_started = !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED);

		if (v->timetable_start != 0) {
			v->lateness_counter = (_date - v->timetable_start) * DAY_TICKS + _date_fract;
			v->timetable_start = 0;
		}

		SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
		SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
	}

	if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;

	if (HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) {
		if (travelling && !HasBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME)) {
			/* Need to clear that now as otherwise we are not able to reduce the wait time */
			v->current_order.wait_time = 0;
		}

		if (just_started) return;

		/* Modify station waiting time only if our new value is larger (this is
		 * always the case when we cleared the timetable). */
		if (!v->current_order.IsType(OT_CONDITIONAL) && (travelling || time_taken > v->current_order.wait_time)) {
			/* Round the time taken up to the nearest day, as this will avoid
			 * confusion for people who are timetabling in days, and can be
			 * adjusted later by people who aren't.
			 * For trains/aircraft multiple movement cycles are done in one
			 * tick. This makes it possible to leave the station and process
			 * e.g. a depot order in the same tick, causing it to not fill
			 * the timetable entry like is done for road vehicles/ships.
			 * Thus always make sure at least one tick is used between the
			 * processing of different orders when filling the timetable. */
			time_taken = CeilDiv(max(time_taken, 1U), DAY_TICKS) * DAY_TICKS;

			ChangeTimetable(v, v->cur_real_order_index, time_taken, travelling);
		}

		if (v->cur_real_order_index == first_manual_order && travelling) {
			/* If we just started we would have returned earlier and have not reached
			 * this code. So obviously, we have completed our round: So turn autofill
			 * off again. */
			ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
			ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
		}
		return;
	}

	if (just_started) return;

	/* Vehicles will wait at stations if they arrive early even if they are not
	 * timetabled to wait there, so make sure the lateness counter is updated
	 * when this happens. */
	if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;

	v->lateness_counter -= (timetabled - time_taken);

	/* When we are more late than this timetabled bit takes we (somewhat expensively)
	 * check how many ticks the (fully filled) timetable has. If a timetable cycle is
	 * shorter than the amount of ticks we are late we reduce the lateness by the
	 * length of a full cycle till lateness is less than the length of a timetable
	 * cycle. When the timetable isn't fully filled the cycle will be INVALID_TICKS. */
	if (v->lateness_counter > (int)timetabled) {
		Ticks cycle = v->orders.list->GetTimetableTotalDuration();
		if (cycle != INVALID_TICKS && v->lateness_counter > cycle) {
			v->lateness_counter %= cycle;
		}
	}

	for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
		SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
	}
}