Request for GS api addition : GSCargo.GetCargoName

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
hpfx
Engineer
Engineer
Posts: 55
Joined: 09 Nov 2013 00:19

Request for GS api addition : GSCargo.GetCargoName

Post by hpfx »

Hello,

If it's possible,
I would like to request an API addition for class GSCargo.
Please add the following Static Public Member Function : static char * GetCargoName(CargoID cargo_type)

That function would returns : The cargo name (I mean, for example "Passangers") like it's defined in newGRF.
also it would be in plain user language (as ottd display to user, including falling back to English if not defined in GRF)
that mean for example getting "passeggeri" (italian) or "passagers" (french) strings according to user language.

note : this function behaves like GSCargo.GetCargoLabel, but gives cargo name for UI purpose.

Thank you,
hpfx.
3298
Traffic Manager
Traffic Manager
Posts: 143
Joined: 02 Apr 2011 12:55

Re: Request for GS api addition : GSCargo.GetCargoName

Post by 3298 »

What if the client in a multiplayer game has a different language than the server? This is possible after all.
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: Request for GS api addition : GSCargo.GetCargoName

Post by krinn »

It's not as useful as you think :

- AI doesn't display anything to user : the console is not really part of the game for a player.
So who cares AI API get the function to display real cargo names ? (as API can display there label already).

- GS does already display cargo names, just like openttd : in your english.txt:

Code: Select all

STR_CARNEED : You need to move {NUM} {CARGO_LIST} to {TOWN}.
-> You need to move 100 Passengers to Paris

and in french.txt

Code: Select all

STR_CARNEED : Vous devez déplacer {NUM} {CARGO_LIST} vers {TOWN}.
-> Vous devez déplacer 100 passagers vers Paris

With GS code : assuming 0 is the passenger cargo, and 12 "Paris" town.

Code: Select all

GSText(GSText.STR_CARNEED, 100, 0, 12);
Check out this link, i use it a lot : http://wiki.openttd.org/OpenTTDDevBlack ... al_strings
edit: oh and btw it's funny you pickup the cargo example as this special string is missing in the doc :)
But you already know its name now.

So i think what you are asking exist already.
hpfx
Engineer
Engineer
Posts: 55
Joined: 09 Nov 2013 00:19

Re: Request for GS api addition : GSCargo.GetCargoName

Post by hpfx »

krinn wrote:It's not as useful as you think :
- GS does already display cargo names, just like openttd : in your english.txt:

Code: Select all

STR_CARNEED : You need to move {NUM} {CARGO_LIST} to {TOWN}.
With GS code : assuming 0 is the passenger cargo, and 12 "Paris" town.

Code: Select all

GSText(GSText.STR_CARNEED, 100, 0, 12);
Indeed !
Yes, that's exactely what I was looking for,
I didn't think it was existing, by looking stuff from existing GS around here, some of them use cargo name hardcoded in language file, like this :

Code: Select all

STR_PASS : you must deliver {NUM} Passengers...
Which is something I don't like at all !
by seeing that (and after I did check API), I believed it was not build in... yet, I was wrong.
That feature is just great !

Thank you.
hpfx
Engineer
Engineer
Posts: 55
Joined: 09 Nov 2013 00:19

Re: Request for GS api addition : GSCargo.GetCargoName

Post by hpfx »

Sorry,
it's not so easy...
{CARGO_LIST} does not give the name of the cargo, it gives something I don't understand :
for example : english.txt

Code: Select all

STR_INFO : id {NUM} = {CARGO_LIST}
with code :

Code: Select all

GSText(GSText.STR_INFO,7,7);
you will get: id 7 = Passenger, Mail, Coal
while you can expect "Goods" for cargoid 7 (in temperate/sub-tropical/sub-arctic climate)

I don't know what this list does mean, but that can explain why it's called "cargo_list".

I took a look at documentation, {CARGO} need two parameters because it display cargo and unit ("950 tonnes of Coal " while I just need "Coal")
3298
Traffic Manager
Traffic Manager
Posts: 143
Joined: 02 Apr 2011 12:55

Re: Request for GS api addition : GSCargo.GetCargoName

Post by 3298 »

Try {CARGO_TINY}, {CARGO_SHORT} or {CARGO_LONG} instead of {CARGO_LIST} (at least in OpenTTD's language files these are used, not sure whether they are all available for GS). Or give it 1 << 7 (a one left-shifted by 7, which is 1*(2^7) = 128) as a parameter instead of just the 7.
For {CARGO_LIST} the parameter apparently is a bitfield, so convert the number to binary to see which cargoes will be displayed. 7 is 111 in binary, so bits #0, #1 and #2 are 1 (they are counted from the right), which makes the game display the names of the cargoes with IDs 0, 1 and 2.
hpfx
Engineer
Engineer
Posts: 55
Joined: 09 Nov 2013 00:19

Re: Request for GS api addition : GSCargo.GetCargoName

Post by hpfx »

3298 wrote:Try {CARGO_TINY}, {CARGO_SHORT} or {CARGO_LONG}
No, these are all related to display quantity figures.

Only this worked (Thank to your remark):

Code: Select all

GSText(GSText.STR_STRANGETHING, cargoid, 1<<cargoid);
which looks like a workaroud, but ok... it does the job.


Also I have an issue with this implementation : I would like to convert this GSText into string (ok to loose colors or other formating feature)
That's just it's impossible to perform correct debugging without sending this text to "AI/GameScript debug" window.

Thank you.
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: Request for GS api addition : GSCargo.GetCargoName

Post by krinn »

Well, you have the cargoid and the GetCargoLabel, it won't really kill you to log PASS instead of passengers :)
hpfx
Engineer
Engineer
Posts: 55
Joined: 09 Nov 2013 00:19

Re: Request for GS api addition : GSCargo.GetCargoName

Post by hpfx »

krinn wrote:Well, you have the cargoid and the GetCargoLabel, it won't really kill you to log PASS instead of passengers :)
Well,
I like to check exactly,
if I had to rely on that

Code: Select all

GSText(GSText.STR_CARNEED, 100, 0, 12);
I will get very surprised by the return "You need to move 100 Nothing to Paris",
Instead of that you just log cargoid or cargolabel, you will have "100 PASS"... and you never see the bug in your code until you can display it in a proper way ;)

But Ok, it's not possible, I have to live with it.
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 991
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: Request for GS api addition : GSCargo.GetCargoName

Post by frosch »

The {CARGO_LIST} command is the right one you are looking for.
If you want to display a single cargo, you set the parameter to 1 << (cargo index).

An example can be found in Sillicon Valley:
https://dev.openttdcoop.org/projects/si ... n.nut#L322
https://dev.openttdcoop.org/projects/si ... ish.txt#L6

Oh, and don't hard code "7" for a specific cargo. The cargo indices are arbitrary numbers which change.
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
hpfx
Engineer
Engineer
Posts: 55
Joined: 09 Nov 2013 00:19

Re: Request for GS api addition : GSCargo.GetCargoName

Post by hpfx »

Hi,
frosch wrote:The {CARGO_LIST} command is the right one you are looking for.
If you want to display a single cargo, you set the parameter to 1 << (cargo index).

An example can be found in Sillicon Valley:
https://dev.openttdcoop.org/projects/si ... n.nut#L322
https://dev.openttdcoop.org/projects/si ... ish.txt#L6

Oh, and don't hard code "7" for a specific cargo. The cargo indices are arbitrary numbers which change.
Thank you.

don't fear, I'm not going hardcode cargoid, I've seen that ECS, FIRS and base cargo each use their own id (for example WATR and FRUT have different id). that's good to remind to anyone, you right.

I understand now why last cargoid is 31 (1<<31).
Regards,
hpfx
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 15 guests