(svn r9312) [NoAI] -Codechange: add IsValidCargo. noai
authorrubidium
Mon, 19 Mar 2007 10:08:18 +0000
branchnoai
changeset 9472 063c3f50972a
parent 9471 467ee8749166
child 9473 dcbcd1c4496d
(svn r9312) [NoAI] -Codechange: add IsValidCargo.
bin/ai/regression/regression.txt
src/ai/api/ai_cargo.cpp
src/ai/api/ai_cargo.hpp
--- a/bin/ai/regression/regression.txt	Mon Mar 19 00:19:42 2007 +0000
+++ b/bin/ai/regression/regression.txt	Mon Mar 19 10:08:18 2007 +0000
@@ -109,7 +109,7 @@
     GetCargoIncome(100, 10): 88
     GetCargoIncome(10, 100): 3
   Cargo 11
-    GetCargoLabel():         ''
+    GetCargoLabel():         '(null : 0x00000000)'
     IsFreight():             false
     GetCargoIncome(0, 0):    0
     GetCargoIncome(10, 10):  0
--- a/src/ai/api/ai_cargo.cpp	Mon Mar 19 00:19:42 2007 +0000
+++ b/src/ai/api/ai_cargo.cpp	Mon Mar 19 10:08:18 2007 +0000
@@ -7,9 +7,14 @@
 #include "../../player.h" // For economy.h
 #include "../../economy.h"
 
+bool AICargo::IsValidCargo(CargoID cargo_type)
+{
+	return (cargo_type < NUM_CARGO && GetCargo(cargo_type)->IsValid());
+}
+
 char *AICargo::GetCargoLabel(CargoID cargo_type)
 {
-	if (cargo_type >= NUM_CARGO) return NULL;
+	if (!this->IsValidCargo(cargo_type)) return NULL;
 	const CargoSpec *cargo = GetCargo(cargo_type);
 
 	/* cargo->label is a uint32 packing a 4 character non-terminated string,
@@ -24,13 +29,13 @@
 
 bool AICargo::IsFreight(CargoID cargo_type)
 {
-	if (cargo_type >= NUM_CARGO) return false;
+	if (!this->IsValidCargo(cargo_type)) return false;
 	const CargoSpec *cargo = GetCargo(cargo_type);
 	return cargo->is_freight;
 }
 
 int32 AICargo::GetCargoIncome(uint32 distance, uint32 days_in_transit, CargoID cargo_type)
 {
-	if (cargo_type >= NUM_CARGO) return 0;
+	if (!this->IsValidCargo(cargo_type)) return 0;
 	return GetTransportedGoodsIncome(1, distance, days_in_transit, cargo_type);
 }
--- a/src/ai/api/ai_cargo.hpp	Mon Mar 19 00:19:42 2007 +0000
+++ b/src/ai/api/ai_cargo.hpp	Mon Mar 19 10:08:18 2007 +0000
@@ -13,6 +13,13 @@
 class AICargo : public AIObject {
 public:
 	/**
+	 * Checks whether the given cargo type is valid.
+	 * @param cargo_type the cargo to check.
+	 * @return true if and only if the cargo type is valid.
+	 */
+	bool IsValidCargo(CargoID cargo_type);
+
+	/**
 	 * Gets the string representation of the cargo label.
 	 * @param cargo_type to get the string representation of.
 	 * @return the cargo label.
@@ -43,6 +50,7 @@
 	DefSQClass <AICargo> SQAICargo("AICargo");
 	SQAICargo.PreRegister(engine);
 	SQAICargo.AddConstructor(engine);
+	SQAICargo.DefSQFunction(engine, &AICargo::IsValidCargo,   "IsValidCargo");
 	SQAICargo.DefSQFunction(engine, &AICargo::GetCargoLabel,  "GetCargoLabel");
 	SQAICargo.DefSQFunction(engine, &AICargo::IsFreight,      "IsFreight");
 	SQAICargo.DefSQFunction(engine, &AICargo::GetCargoIncome, "GetCargoIncome");