(svn r26557) -Fix: clean up, test (somewhat), and complete the API for waiting cargo

This commit is contained in:
fonsinchen
2014-05-04 13:57:48 +00:00
parent d2357ec435
commit bb269661d4
8 changed files with 127 additions and 41 deletions

View File

@@ -49,6 +49,7 @@ void SQAIStation_Register(Squirrel *engine)
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaiting, "GetCargoWaiting", 3, ".ii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingFrom, "GetCargoWaitingFrom", 4, ".iii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingVia, "GetCargoWaitingVia", 4, ".iii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingFromVia, "GetCargoWaitingFromVia", 5, ".iiii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::HasCargoRating, "HasCargoRating", 3, ".ii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoRating, "GetCargoRating", 3, ".ii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCoverageRadius, "GetCoverageRadius", 2, ".i");

View File

@@ -19,6 +19,9 @@
*
* 1.5.0 is not yet released. The following changes are not set in stone yet.
*
* API additions:
* \li AIStation::GetCargoWaitingFromVia
*
* \b 1.4.0
*
* API additions:

View File

@@ -50,6 +50,7 @@ void SQGSStation_Register(Squirrel *engine)
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaiting, "GetCargoWaiting", 3, ".ii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingFrom, "GetCargoWaitingFrom", 4, ".iii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingVia, "GetCargoWaitingVia", 4, ".iii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingFromVia, "GetCargoWaitingFromVia", 5, ".iiii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::HasCargoRating, "HasCargoRating", 3, ".ii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoRating, "GetCargoRating", 3, ".ii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCoverageRadius, "GetCoverageRadius", 2, ".i");

View File

@@ -19,6 +19,9 @@
*
* 1.5.0 is not yet released. The following changes are not set in stone yet.
*
* API additions:
* \li GSStation::GetCargoWaitingFromVia
*
* \b 1.4.0
*
* API additions:
@@ -32,8 +35,8 @@
* \li GSGoal::SetProgress
* \li GSGoal::SetText
* \li GSStation::HasCargoRating
* \li AIStation::GetCargoWaitingFrom
* \li AIStation::GetCargoWaitingVia
* \li GSStation::GetCargoWaitingFrom
* \li GSStation::GetCargoWaitingVia
* \li GSStoryPage
* \li GSStoryPageList
* \li GSStoryPageElementList

View File

@@ -39,45 +39,51 @@
return ::GetStationIndex(tile);
}
template<bool Tfrom, bool Tvia>
/* static */ int32 ScriptStation::CountCargoWaiting(StationID station_id,
StationID from_station_id, StationID via_station_id, CargoID cargo_id)
{
if (!IsValidStation(station_id)) return -1;
if (Tfrom && !IsValidStation(from_station_id) && from_station_id != STATION_INVALID) return -1;
if (Tvia && !IsValidStation(via_station_id) && via_station_id != STATION_INVALID) return -1;
if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
const StationCargoList &cargo_list = ::Station::Get(station_id)->goods[cargo_id].cargo;
if (!Tfrom && !Tvia) return cargo_list.TotalCount();
uint16 cargo_count = 0;
std::pair<StationCargoList::ConstIterator, StationCargoList::ConstIterator> range = Tvia ?
cargo_list.Packets()->equal_range(via_station_id) :
std::make_pair(cargo_list.Packets()->begin(), cargo_list.Packets()->end());
for (StationCargoList::ConstIterator it = range.first; it != range.second; it++) {
const CargoPacket *cp = *it;
if (!Tfrom || cp->SourceStation() == from_station_id) cargo_count += cp->Count();
}
return cargo_count;
}
/* static */ int32 ScriptStation::GetCargoWaiting(StationID station_id, CargoID cargo_id)
{
if (!IsValidStation(station_id)) return -1;
if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
return ::Station::Get(station_id)->goods[cargo_id].cargo.TotalCount();
return CountCargoWaiting<false, false>(station_id, STATION_INVALID, STATION_INVALID, cargo_id);
}
/* static */ int32 ScriptStation::GetCargoWaitingFrom(StationID station_id, StationID from_station_id, CargoID cargo_id)
/* static */ int32 ScriptStation::GetCargoWaitingFrom(StationID station_id,
StationID from_station_id, CargoID cargo_id)
{
if (!IsValidStation(station_id)) return -1;
if (!IsValidStation(from_station_id) && from_station_id != STATION_INVALID) return -1;
if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
const StationCargoList &cargo_list = ::Station::Get(station_id)->goods[cargo_id].cargo;
uint16 cargo_count = 0;
for (StationCargoList::ConstIterator it = cargo_list.Packets()->begin(); it != cargo_list.Packets()->end(); it++) {
CargoPacket *cp = *it;
if (cp->SourceStation() == from_station_id) cargo_count += cp->Count();
}
return cargo_count;
return CountCargoWaiting<true, false>(station_id, from_station_id, STATION_INVALID, cargo_id);
}
/* static */ int32 ScriptStation::GetCargoWaitingVia(StationID station_id, StationID via_station_id, CargoID cargo_id)
/* static */ int32 ScriptStation::GetCargoWaitingVia(StationID station_id,
StationID via_station_id, CargoID cargo_id)
{
if (!IsValidStation(station_id)) return -1;
if (!IsValidStation(via_station_id) && via_station_id != STATION_INVALID) return -1;
if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
return CountCargoWaiting<false, true>(station_id, STATION_INVALID, via_station_id, cargo_id);
}
const StationCargoList &cargo_list = ::Station::Get(station_id)->goods[cargo_id].cargo;
uint16 cargo_count = 0;
std::pair<StationCargoList::ConstIterator, StationCargoList::ConstIterator> range = cargo_list.Packets()->equal_range(via_station_id);
for (StationCargoList::ConstIterator it = range.first; it != range.second; it++) {
CargoPacket *cp = *it;
cargo_count += cp->Count();
}
return cargo_count;
/* static */ int32 ScriptStation::GetCargoWaitingFromVia(StationID station_id,
StationID from_station_id, StationID via_station_id, CargoID cargo_id)
{
return CountCargoWaiting<true, true>(station_id, from_station_id, via_station_id, cargo_id);
}
/* static */ bool ScriptStation::HasCargoRating(StationID station_id, CargoID cargo_id)

View File

@@ -89,6 +89,7 @@ public:
/**
* See how much cargo with a specific source station there is waiting on a station.
* @param station_id The station to get the cargo-waiting of.
* @param from_station_id The source station of the cargo. Pass STATION_INVALID to get cargo of which the source has been deleted.
* @param cargo_id The cargo to get the cargo-waiting of.
* @pre IsValidStation(station_id).
* @pre IsValidStation(from_station_id) || from_station_id == STATION_INVALID.
@@ -100,17 +101,32 @@ public:
/**
* See how much cargo with a specific via-station there is waiting on a station.
* @param station_id The station to get the cargo-waiting of, or pass STATION_INVALID to get waiting cargo for "via any station".
* @param station_id The station to get the cargo-waiting of.
* @param via_station_id The next station the cargo is going to. Pass STATION_INVALID to get waiting cargo for "via any station".
* @param cargo_id The cargo to get the cargo-waiting of.
* @pre IsValidStation(station_id).
* @pre IsValidStation(via_station_id) || via_station_id == STATION_INVALID.
* @pre IsValidCargo(cargo_id).
* @return The amount of units waiting at the station with via_station_id as next hop.
* @note if ScriptCargo.GetCargoDistributionType(cargo_id) == ScriptCargo.DT_MANUAL, then all waiting cargo will have STATION_INVALID as next hop.
*/
static int32 GetCargoWaitingVia(StationID station_id, StationID via_station_id, CargoID cargo_id);
/**
* See how much cargo with a specific via-station and source station there is waiting on a station.
* @param station_id The station to get the cargo-waiting of.
* @param from_station_id The source station of the cargo. Pass STATION_INVALID to get cargo of which the source has been deleted.
* @param via_station_id The next station the cargo is going to. Pass STATION_INVALID to get waiting cargo for "via any station".
* @param cargo_id The cargo to get the cargo-waiting of.
* @pre IsValidStation(station_id).
* @pre IsValidStation(from_station_id) || from_station_id == STATION_INVALID.
* @pre IsValidStation(via_station_id) || via_station_id == STATION_INVALID.
* @pre IsValidCargo(cargo_id).
* @return The amount of units waiting at the station with from_station_id as source and via_station_id as next hop.
* @note if ScriptCargo.GetCargoDistributionType(cargo_id) == ScriptCargo.DT_MANUAL, then all waiting cargo will have STATION_INVALID as next hop.
*/
static int32 GetCargoWaitingFromVia(StationID station_id, StationID from_station_id, StationID via_station_id, CargoID cargo_id);
/**
* Check whether the given cargo at the given station a rating.
* @param station_id The station to get the cargo-rating state of.
@@ -226,6 +242,12 @@ public:
* @return True if the state could be toggled.
*/
static bool OpenCloseAirport(StationID station_id);
private:
template<bool Tfrom, bool Tvia>
static int32 CountCargoWaiting(StationID station_id, StationID from_station_id,
StationID via_station_id, CargoID cargo_id);
};
DECLARE_ENUM_AS_BIT_SET(ScriptStation::StationType)