First implementation of conditional order checking slot occupancy
(cherry picked from commit 524e3e56cb4a357ba60335f91dce1e00cb8d22f2) Some code adjustments: serialisation/storage, formatting See: #40
This commit is contained in:

committed by
Jonathan G Rennison

parent
2e4ac325e2
commit
78588d8ce5
@@ -38,6 +38,7 @@
|
||||
#include "viewport_func.h"
|
||||
#include "order_cmd.h"
|
||||
#include "vehiclelist.h"
|
||||
#include "tracerestrict.h"
|
||||
|
||||
#include "table/strings.h"
|
||||
|
||||
@@ -1003,10 +1004,18 @@ CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
|
||||
OrderConditionComparator occ = new_order.GetConditionComparator();
|
||||
if (occ >= OCC_END) return CMD_ERROR;
|
||||
switch (new_order.GetConditionVariable()) {
|
||||
case OCV_SLOT_OCCUPANCY: {
|
||||
TraceRestrictSlotID slot = new_order.GetXData();
|
||||
if (slot != INVALID_TRACE_RESTRICT_SLOT_ID && !TraceRestrictSlot::IsValidID(slot)) return CMD_ERROR;
|
||||
if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
case OCV_CARGO_WAITING:
|
||||
case OCV_CARGO_ACCEPTANCE:
|
||||
if (!CargoSpec::Get(new_order.GetConditionValue())->IsValid()) return CMD_ERROR;
|
||||
/* FALL THROUGH */
|
||||
|
||||
case OCV_REQUIRES_SERVICE:
|
||||
if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
|
||||
break;
|
||||
@@ -1480,8 +1489,8 @@ CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
|
||||
* the maximum vehicle order id is 254.
|
||||
* @param p2 various bitstuffed elements
|
||||
* - p2 = (bit 0 - 3) - what data to modify (@see ModifyOrderFlags)
|
||||
* - p2 = (bit 4 - 15) - the data to modify
|
||||
* - p2 = (bit 16 - 23) - a CargoID for cargo type orders (MOF_CARGO_TYPE_UNLOAD or MOF_CARGO_TYPE_LOAD)
|
||||
* - p2 = (bit 4 - 19) - the data to modify
|
||||
* - p2 = (bit 20 - 27) - a CargoID for cargo type orders (MOF_CARGO_TYPE_UNLOAD or MOF_CARGO_TYPE_LOAD)
|
||||
* @param text unused
|
||||
* @return the cost of this operation or an error
|
||||
*/
|
||||
@@ -1490,8 +1499,8 @@ CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
|
||||
VehicleOrderID sel_ord = GB(p1, 20, 8);
|
||||
VehicleID veh = GB(p1, 0, 20);
|
||||
ModifyOrderFlags mof = Extract<ModifyOrderFlags, 0, 4>(p2);
|
||||
uint16 data = GB(p2, 4, 11);
|
||||
CargoID cargo_id = (mof == MOF_CARGO_TYPE_UNLOAD || mof == MOF_CARGO_TYPE_LOAD) ? (CargoID) GB(p2, 16, 8) : (CargoID) CT_INVALID;
|
||||
uint16 data = GB(p2, 4, 16);
|
||||
CargoID cargo_id = (mof == MOF_CARGO_TYPE_UNLOAD || mof == MOF_CARGO_TYPE_LOAD) ? (CargoID) GB(p2, 20, 8) : (CargoID) CT_INVALID;
|
||||
|
||||
if (mof >= MOF_END) return CMD_ERROR;
|
||||
|
||||
@@ -1583,6 +1592,7 @@ CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
|
||||
case OCV_REQUIRES_SERVICE:
|
||||
case OCV_CARGO_ACCEPTANCE:
|
||||
case OCV_CARGO_WAITING:
|
||||
case OCV_SLOT_OCCUPANCY:
|
||||
if (data != OCC_IS_TRUE && data != OCC_IS_FALSE) return CMD_ERROR;
|
||||
break;
|
||||
|
||||
@@ -1604,6 +1614,10 @@ CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
|
||||
if (data > 100) return CMD_ERROR;
|
||||
break;
|
||||
|
||||
case OCV_SLOT_OCCUPANCY:
|
||||
if (data != INVALID_TRACE_RESTRICT_SLOT_ID && !TraceRestrictSlot::IsValidID(data)) return CMD_ERROR;
|
||||
break;
|
||||
|
||||
case OCV_CARGO_ACCEPTANCE:
|
||||
case OCV_CARGO_WAITING:
|
||||
if (!(data < NUM_CARGO && CargoSpec::Get(data)->IsValid())) return CMD_ERROR;
|
||||
@@ -1684,6 +1698,7 @@ CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
|
||||
case MOF_COND_VARIABLE: {
|
||||
/* Check whether old conditional variable had a cargo as value */
|
||||
bool old_var_was_cargo = (order->GetConditionVariable() == OCV_CARGO_ACCEPTANCE || order->GetConditionVariable() == OCV_CARGO_WAITING);
|
||||
bool old_var_was_slot = (order->GetConditionVariable() == OCV_SLOT_OCCUPANCY);
|
||||
order->SetConditionVariable((OrderConditionVariable)data);
|
||||
|
||||
OrderConditionComparator occ = order->GetConditionComparator();
|
||||
@@ -1693,13 +1708,18 @@ CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
|
||||
order->SetConditionValue(0);
|
||||
break;
|
||||
|
||||
case OCV_SLOT_OCCUPANCY:
|
||||
if (!old_var_was_slot) order->GetXDataRef() = INVALID_TRACE_RESTRICT_SLOT_ID;
|
||||
if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
|
||||
break;
|
||||
|
||||
case OCV_CARGO_ACCEPTANCE:
|
||||
case OCV_CARGO_WAITING:
|
||||
if (!old_var_was_cargo) order->SetConditionValue((uint16) GetFirstValidCargo());
|
||||
if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
|
||||
break;
|
||||
case OCV_REQUIRES_SERVICE:
|
||||
if (old_var_was_cargo) order->SetConditionValue(0);
|
||||
if (old_var_was_cargo || old_var_was_slot) order->SetConditionValue(0);
|
||||
if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
|
||||
order->SetConditionValue(0);
|
||||
break;
|
||||
@@ -1713,7 +1733,7 @@ CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
|
||||
FALLTHROUGH;
|
||||
|
||||
default:
|
||||
if (old_var_was_cargo) order->SetConditionValue(0);
|
||||
if (old_var_was_cargo || old_var_was_slot) order->SetConditionValue(0);
|
||||
if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) order->SetConditionComparator(OCC_EQUALS);
|
||||
break;
|
||||
}
|
||||
@@ -1725,7 +1745,15 @@ CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
|
||||
break;
|
||||
|
||||
case MOF_COND_VALUE:
|
||||
order->SetConditionValue(data);
|
||||
switch (order->GetConditionVariable()) {
|
||||
case OCV_SLOT_OCCUPANCY:
|
||||
order->GetXDataRef() = data;
|
||||
break;
|
||||
|
||||
default:
|
||||
order->SetConditionValue(data);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case MOF_COND_DESTINATION:
|
||||
@@ -2349,6 +2377,7 @@ VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
|
||||
OrderConditionComparator occ = order->GetConditionComparator();
|
||||
uint16 value = order->GetConditionValue();
|
||||
|
||||
// OrderConditionCompare ignores the last parameter for occ == OCC_IS_TRUE or occ == OCC_IS_FALSE.
|
||||
switch (order->GetConditionVariable()) {
|
||||
case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break;
|
||||
case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break;
|
||||
@@ -2366,6 +2395,11 @@ VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
|
||||
if (Station::IsValidID(next_station)) skip_order = OrderConditionCompare(occ, HasBit(Station::Get(next_station)->goods[value].status, GoodsEntry::GES_ACCEPTANCE), value);
|
||||
break;
|
||||
}
|
||||
case OCV_SLOT_OCCUPANCY: {
|
||||
const TraceRestrictSlot* slot = TraceRestrictSlot::GetIfValid(order->GetXData());
|
||||
if (slot != nullptr) skip_order = OrderConditionCompare(occ, slot->occupants.size() >= slot->max_occupancy, value);
|
||||
break;
|
||||
}
|
||||
case OCV_FREE_PLATFORMS: {
|
||||
StationID next_station = GetNextRealStation(v, order);
|
||||
if (Station::IsValidID(next_station)) skip_order = OrderConditionCompare(occ, GetFreeStationPlatforms(next_station), value);
|
||||
|
Reference in New Issue
Block a user