Skip to content

Commit

Permalink
Add: [Script] ScriptCargo::GetName, to get the human readable name of…
Browse files Browse the repository at this point in the history
… a cargo (OpenTTD#8544)

Of course this translates into AICargo.GetName() for AIs and
GSCargo.GetName() for GameScripts.
  • Loading branch information
TrueBrain committed Jan 9, 2021
1 parent 3dbdb1c commit 218f40e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion regression/regression/main.nut
Expand Up @@ -332,7 +332,8 @@ function Regression::Cargo()
for (local i = -1; i < 15; i++) {
print(" Cargo " + i);
print(" IsValidCargo(): " + AICargo.IsValidCargo(i));
print(" GetCargoLabel(): '" + AICargo.GetCargoLabel(i)+ "'");
print(" GetName(): '" + AICargo.GetName(i) + "'");
print(" GetCargoLabel(): '" + AICargo.GetCargoLabel(i) + "'");
print(" IsFreight(): " + AICargo.IsFreight(i));
print(" HasCargoClass(): " + AICargo.HasCargoClass(i, AICargo.CC_PASSENGERS));
print(" GetTownEffect(): " + AICargo.GetTownEffect(i));
Expand Down
16 changes: 16 additions & 0 deletions regression/regression/result.txt
Expand Up @@ -1109,6 +1109,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
--AICargo--
Cargo -1
IsValidCargo(): false
GetName(): '(null : 0x00000000)'
GetCargoLabel(): '(null : 0x00000000)'
IsFreight(): false
HasCargoClass(): false
Expand All @@ -1120,6 +1121,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 0
IsValidCargo(): true
GetName(): 'Passengers'
GetCargoLabel(): 'PASS'
IsFreight(): false
HasCargoClass(): true
Expand All @@ -1131,6 +1133,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 0
Cargo 1
IsValidCargo(): true
GetName(): 'Coal'
GetCargoLabel(): 'COAL'
IsFreight(): true
HasCargoClass(): false
Expand All @@ -1142,6 +1145,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 2
IsValidCargo(): true
GetName(): 'Mail'
GetCargoLabel(): 'MAIL'
IsFreight(): false
HasCargoClass(): false
Expand All @@ -1153,6 +1157,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 3
IsValidCargo(): true
GetName(): 'Oil'
GetCargoLabel(): 'OIL_'
IsFreight(): true
HasCargoClass(): false
Expand All @@ -1164,6 +1169,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 4
IsValidCargo(): true
GetName(): 'Livestock'
GetCargoLabel(): 'LVST'
IsFreight(): true
HasCargoClass(): false
Expand All @@ -1175,6 +1181,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 5
IsValidCargo(): true
GetName(): 'Goods'
GetCargoLabel(): 'GOOD'
IsFreight(): true
HasCargoClass(): false
Expand All @@ -1186,6 +1193,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 6
IsValidCargo(): true
GetName(): 'Grain'
GetCargoLabel(): 'GRAI'
IsFreight(): true
HasCargoClass(): false
Expand All @@ -1197,6 +1205,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 7
IsValidCargo(): true
GetName(): 'Wood'
GetCargoLabel(): 'WOOD'
IsFreight(): true
HasCargoClass(): false
Expand All @@ -1208,6 +1217,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 8
IsValidCargo(): true
GetName(): 'Iron Ore'
GetCargoLabel(): 'IORE'
IsFreight(): true
HasCargoClass(): false
Expand All @@ -1219,6 +1229,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 9
IsValidCargo(): true
GetName(): 'Steel'
GetCargoLabel(): 'STEL'
IsFreight(): true
HasCargoClass(): false
Expand All @@ -1230,6 +1241,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 10
IsValidCargo(): true
GetName(): 'Valuables'
GetCargoLabel(): 'VALU'
IsFreight(): true
HasCargoClass(): false
Expand All @@ -1241,6 +1253,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 11
IsValidCargo(): false
GetName(): '(null : 0x00000000)'
GetCargoLabel(): '(null : 0x00000000)'
IsFreight(): false
HasCargoClass(): false
Expand All @@ -1252,6 +1265,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 12
IsValidCargo(): false
GetName(): '(null : 0x00000000)'
GetCargoLabel(): '(null : 0x00000000)'
IsFreight(): false
HasCargoClass(): false
Expand All @@ -1263,6 +1277,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 13
IsValidCargo(): false
GetName(): '(null : 0x00000000)'
GetCargoLabel(): '(null : 0x00000000)'
IsFreight(): false
HasCargoClass(): false
Expand All @@ -1274,6 +1289,7 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetRoadVehicleTypeForCargo(): 1
Cargo 14
IsValidCargo(): false
GetName(): '(null : 0x00000000)'
GetCargoLabel(): '(null : 0x00000000)'
IsFreight(): false
HasCargoClass(): false
Expand Down
1 change: 1 addition & 0 deletions src/script/api/ai_changelog.hpp
Expand Up @@ -18,6 +18,7 @@
* This version is not yet released. The following changes are not set in stone yet.
*
* API additions:
* \li AICargo::GetName
* \li AIPriorityQueue
*
* \b 1.10.0
Expand Down
1 change: 1 addition & 0 deletions src/script/api/game_changelog.hpp
Expand Up @@ -18,6 +18,7 @@
* This version is not yet released. The following changes are not set in stone yet.
*
* API additions:
* \li GSCargo::GetName
* \li GSEventStoryPageButtonClick
* \li GSEventStoryPageTileSelect
* \li GSEventStoryPageVehicleSelect
Expand Down
10 changes: 10 additions & 0 deletions src/script/api/script_cargo.cpp
Expand Up @@ -11,7 +11,9 @@
#include "script_cargo.hpp"
#include "../../economy_func.h"
#include "../../core/bitmath_func.hpp"
#include "../../strings_func.h"
#include "../../settings_type.h"
#include "table/strings.h"

#include "../../safeguards.h"

Expand All @@ -25,6 +27,14 @@
return (towneffect_type >= (TownEffect)TE_BEGIN && towneffect_type < (TownEffect)TE_END);
}

/* static */ char *ScriptCargo::GetName(CargoID cargo_type)
{
if (!IsValidCargo(cargo_type)) return nullptr;

::SetDParam(0, 1 << cargo_type);
return GetString(STR_JUST_CARGO_LIST);
}

/* static */ char *ScriptCargo::GetCargoLabel(CargoID cargo_type)
{
if (!IsValidCargo(cargo_type)) return nullptr;
Expand Down
8 changes: 8 additions & 0 deletions src/script/api/script_cargo.hpp
Expand Up @@ -84,6 +84,14 @@ class ScriptCargo : public ScriptObject {
*/
static bool IsValidTownEffect(TownEffect towneffect_type);

/**
* Get the name of the cargo type.
* @param cargo_type The cargo type to get the name of.
* @pre IsValidCargo(cargo_type).
* @return The name of the cargo type.
*/
static char *GetName(CargoID cargo_type);

/**
* Gets the string representation of the cargo label.
* @param cargo_type The cargo to get the string representation of.
Expand Down

0 comments on commit 218f40e

Please sign in to comment.