Using wagons only for certain locomotives

Discussions about the technical aspects of graphics development, including NewGRF tools and utilities.

Moderator: Graphics Moderators

Post Reply
User avatar
Erato
Chief Executive
Chief Executive
Posts: 740
Joined: 25 May 2015 09:09
Location: The Netherlands

Using wagons only for certain locomotives

Post by Erato »

Hello,
I am currently working on a set of freight locomotives and wagons.
I already have several passenger cars, where I have set which cars can have extra passenger carriages attached. For this I use the following code:
[+] Spoiler

Code: Select all

switch (FEAT_TRAINS, PARENT, CAN_ATTACH, vehicle_type_id) {
	ITEM_HSST03			:string(STR_CANNOT_CONNECT);
	ITEM_HSST04			:string(STR_CANNOT_CONNECT);
	ITEM_HSST05			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_HSST100L		:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_HSST100S		:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_LINIMO			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	
	ITEM_M01			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_M80			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_M70			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_E01			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;

	ITEM_TRANSRAPID05	:string(STR_CANNOT_CONNECT);
	ITEM_TRANSRAPID06	:string(STR_CANNOT_CONNECT);
	ITEM_TRANSRAPID07	:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_TRANSRAPID08	:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_TRANSRAPID09	:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;

	ITEM_DMV92			:string(STR_CANNOT_CONNECT);
	ITEM_HML03			:string(STR_CANNOT_CONNECT);
	ITEM_UTM01			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_UTM02			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_ECOBEE			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;

	ITEM_MLX0101		:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_MLX01901		:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_L0				:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	
	ITEM_CFC01			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_CMS02			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_CMS03A			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_WINDCHASER		:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_ZHUIFENGZHE	:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_LINGLONGHAO	:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	
	ITEM_TP01			:string(STR_CANNOT_CONNECT);
	ITEM_TP05			:string(STR_CANNOT_CONNECT);
	
	ITEM_SUMA550		:string(STR_CANNOT_CONNECT);
	ITEM_BIRMAG			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	ITEM_COBRA			:CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES;
	string(STR_CANNOT_CONNECT);
}
switch(FEAT_TRAINS, PARENT, switch_CAN_ATTACH, grfid) {
   str2number("EN\01\01"): return CAN_ATTACH;
   string(STR_CANNOT_CONNECT);
}
The passenger wagon has the name: "ITEM_PASS"
and an example freight wagon has the name "ITEM_ECCO_WAG"

I would like to know how I would have to change this code so I can separately say which wagons I can attach to which trains.
I tried to look into how the 2CC set handled this, but I didn't understand where their code was going.
No pics no clicks. Seriously.
ImageImageImageImageImageImage
User avatar
NekoMaster
Tycoon
Tycoon
Posts: 4001
Joined: 16 Aug 2008 22:26
Skype: neko-master
Location: Oshawa, Ontario, CANADA

Re: Using wagons only for certain locomotives

Post by NekoMaster »

I would kind of like to know how that works as I've had to disable the "can attach" wagon feature in my 2cc code for NARS ADD-ON Set 2cc
Image Proud Canadian Image
Nekomasters Projects! (Downloads available on BaNaNaS!) \(>^w^<)/
# NARS ADD-ON SET 2CC | 2cc Rapid Transit For Me! (2ccRTFM) | 2cc Wagons In NML (2ccWIN)
# NML Category System (Organize your GRFS!) <- TT-Forums Exclusive Download!
User avatar
Erato
Chief Executive
Chief Executive
Posts: 740
Joined: 25 May 2015 09:09
Location: The Netherlands

Re: Using wagons only for certain locomotives

Post by Erato »

NekoMaster wrote:I would kind of like to know how that works as I've had to disable the "can attach" wagon feature in my 2cc code for NARS ADD-ON Set 2cc
That's odd.

I really just do:

Code: Select all

	graphics {
		default: SPRITEGROUP_LINIMO; // Use this group
		purchase: SPRITESET_PURCH_LINIMO; // Use this set for the menu
		can_attach_wagon: switch_CAN_ATTACH;
	}
But with this it's whether I can attach any wagon, or nothing at all.
No pics no clicks. Seriously.
ImageImageImageImageImageImage
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: Using wagons only for certain locomotives

Post by planetmaker »

Erato wrote:
NekoMaster wrote:I would kind of like to know how that works as I've had to disable the "can attach" wagon feature in my 2cc code for NARS ADD-ON Set 2cc
That's odd.

I really just do:

Code: Select all

	graphics {
		default: SPRITEGROUP_LINIMO; // Use this group
		purchase: SPRITESET_PURCH_LINIMO; // Use this set for the menu
		can_attach_wagon: switch_CAN_ATTACH;
	}
But with this it's whether I can attach any wagon, or nothing at all.
Of course. But the important part happens in switch_CAN_ATTACH which you didn't show ;) And there you can also decide which wagon can be attached

Code: Select all

switch (FEAT_TRAINS, SELF, switch_CAN_ATTACH, vehicle_type_id) {
	one_allowed_wagon_id: CB_RESULT_ATTACH_ALLOW;
	another_allowed_wagon_id: CB_RESULT_ATTACH_ALLOW;
	return CB_RESULT_ATTACH_DISALLOW
}
However I'd like to encourage you to not be strict with that, but resort to hints for the player in the purchase menu - but let them decide whether it's a fitting combination given the actual circumstances (e.g. a special scenario in an alternate history or whatever):

Code: Select all

	graphics {
		default: SPRITEGROUP_LINIMO; // Use this group
		purchase: SPRITESET_PURCH_LINIMO; // Use this set for the menu
		can_attach_wagon: CB_RESULT_ATTACH_ALLOW;
		additional_text string(STR_ONLY_SUITABLE_FOR_ENGINES_OF_TYPE_XYZ);
	}
Transportman
Tycoon
Tycoon
Posts: 2781
Joined: 22 Feb 2011 18:34

Re: Using wagons only for certain locomotives

Post by Transportman »

Erato wrote:I tried to look into how the 2CC set handled this, but I didn't understand where their code was going.
What did you not understand in that code? It basically uses the ID of the new vehicle to determine which can_attach-switch should be called, and then uses the ID of the vehicle in front to determine if the new vehicle can be attached.
Coder of the Dutch Trackset | Development support for the Dutch Trainset | Coder of the 2cc TrainsInNML
User avatar
Erato
Chief Executive
Chief Executive
Posts: 740
Joined: 25 May 2015 09:09
Location: The Netherlands

Re: Using wagons only for certain locomotives

Post by Erato »

Transportman wrote:
Erato wrote:I tried to look into how the 2CC set handled this, but I didn't understand where their code was going.
What did you not understand in that code? It basically uses the ID of the new vehicle to determine which can_attach-switch should be called, and then uses the ID of the vehicle in front to determine if the new vehicle can be attached.

Code: Select all

switch(FEAT_TRAINS, PARENT, switch_can_attach_unit_rail, [STORE_TEMP(position_in_consist_from_end, 0x10F), var[0x61, 0, 0x0000FFFF, 0xC6]])
I really don't know what this switch does and how it works. With all the HEX and all.
No pics no clicks. Seriously.
ImageImageImageImageImageImage
Cadde
Transport Coordinator
Transport Coordinator
Posts: 290
Joined: 07 Oct 2004 12:51

Re: Using wagons only for certain locomotives

Post by Cadde »

For you to understand that expression, let's start out with the "var[...]" statement.
It's not documented anywhere, so it's understandable that you don't know it. But if one checks the source for NMLC one can see how it's supposed to work.

Code: Select all

var[num, <shift>, <mask>, <param>]
num - The variable to access. This is feature specific so accessing for example variable 0xB4 on a train would return the current speed.
shift - An optional parameter that performs a bitwise shift on the result. Example: 123 << 4 = 1,968 or in binary: 0111 1011 << 4 = 0111 1011 0000, in other words. It shifts bits left or right. I would assume positive numbers are left shift (<<) and negative numbers are right shift (>>)
mask - An optional parameter that masks the result. For example: 123 & 0xF = 11 or in binary: 0111 1011 & 0000 1111 = 1011. That is, if both bits are 1 in the result and the mask then they are kept.
param - An optional parameter that is sent to the "60+x" variable in question. In this case, since we are working on var 0x61 which is "query variable of n'th vehicle in chain" we are trying to get the value of variable C6 (Vehicle type ID) on the n'th vehicle in the chain.
Or in other words, get variable from n'th vehicle in chain that is "Vehicle type ID" applying no bit shifting and applying logical AND 0x0000FFFF to the result. Or in other words, clamping the result between 0 and 65535.

So, we are trying to base our decision in the switch on the vehicle ID of the wagon that exists before this wagon is attached to the chain.
We only allow adding "unit_rail" wagons to DMU, EMU, unit_rail and SMU's before it.

Now you wonder, how does 0x61 (query var of n'th vehicle in chain) know what the n'th vehicle in the chain is? I mean, there's only a parameter for what variable (0xC6, vehicle id) to get from that n'th vehicle.
Well, when using var 0x61, you have to make use of a temporary variable that refers to the actual offset in the chain. That's what the previous STORE_TEMP(x, 0x10F) does. It stores a value in a SPECIAL register used for this very purpose.
In this case, the value is the "position_in_consist_from_end" variable.

But i fail to see the logic behind this one though... Wouldn't it simply suffice to store 0 (zero) in that temporary register? Or is it actually counting from the engine down because the vehicle being evaluated is a wagon? So many question marks.
Either way, surely there's a reason for that. And if you search for it, you end up on page 43 of the NML language thread: viewtopic.php?t=48891&start=840

And there you have it. Even the comments on the code you pasted tells you the same story. Albeit a bit briefer...

Code: Select all

//Check vehicle ID of the vehicle in front of the unit wagon being attached
//The offset for var 61 is determined by position_in_consist_from_end, which results in the last wagon before the vehicle being attached
If you don't understand some code, but it still works beautifully, then what's the harm in re-using it?
User avatar
Erato
Chief Executive
Chief Executive
Posts: 740
Joined: 25 May 2015 09:09
Location: The Netherlands

Re: Using wagons only for certain locomotives

Post by Erato »

Cadde wrote:For you to understand that expression, let's start out with the "var[...]" statement.
It's not documented anywhere, so it's understandable that you don't know it. But if one checks the source for NMLC one can see how it's supposed to work.

Code: Select all

var[num, <shift>, <mask>, <param>]
num - The variable to access. This is feature specific so accessing for example variable 0xB4 on a train would return the current speed.
shift - An optional parameter that performs a bitwise shift on the result. Example: 123 << 4 = 1,968 or in binary: 0111 1011 << 4 = 0111 1011 0000, in other words. It shifts bits left or right. I would assume positive numbers are left shift (<<) and negative numbers are right shift (>>)
mask - An optional parameter that masks the result. For example: 123 & 0xF = 11 or in binary: 0111 1011 & 0000 1111 = 1011. That is, if both bits are 1 in the result and the mask then they are kept.
param - An optional parameter that is sent to the "60+x" variable in question. In this case, since we are working on var 0x61 which is "query variable of n'th vehicle in chain" we are trying to get the value of variable C6 (Vehicle type ID) on the n'th vehicle in the chain.
Or in other words, get variable from n'th vehicle in chain that is "Vehicle type ID" applying no bit shifting and applying logical AND 0x0000FFFF to the result. Or in other words, clamping the result between 0 and 65535.

So, we are trying to base our decision in the switch on the vehicle ID of the wagon that exists before this wagon is attached to the chain.
We only allow adding "unit_rail" wagons to DMU, EMU, unit_rail and SMU's before it.

Now you wonder, how does 0x61 (query var of n'th vehicle in chain) know what the n'th vehicle in the chain is? I mean, there's only a parameter for what variable (0xC6, vehicle id) to get from that n'th vehicle.
Well, when using var 0x61, you have to make use of a temporary variable that refers to the actual offset in the chain. That's what the previous STORE_TEMP(x, 0x10F) does. It stores a value in a SPECIAL register used for this very purpose.
In this case, the value is the "position_in_consist_from_end" variable.

But i fail to see the logic behind this one though... Wouldn't it simply suffice to store 0 (zero) in that temporary register? Or is it actually counting from the engine down because the vehicle being evaluated is a wagon? So many question marks.
Either way, surely there's a reason for that. And if you search for it, you end up on page 43 of the NML language thread: viewtopic.php?t=48891&start=840

And there you have it. Even the comments on the code you pasted tells you the same story. Albeit a bit briefer...

Code: Select all

//Check vehicle ID of the vehicle in front of the unit wagon being attached
//The offset for var 61 is determined by position_in_consist_from_end, which results in the last wagon before the vehicle being attached
If you don't understand some code, but it still works beautifully, then what's the harm in re-using it?
I see. So I tried using it.
Image

Code: Select all

//ITS
switch(FEAT_TRAINS, PARENT, switch_can_attach_its, [STORE_TEMP(position_in_consist_from_end, 0x10F), var[0x61, 0, 0x0000FFFF, 0xC6]]) {
	ITEM_RANGE_ITS: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
	ITEM_RANGE_ITS_WAG: CB_RESULT_ATTACH_DISALLOW; //Allow self
	CB_RESULT_ATTACH_DISALLOW;
}
The switch being line 12. If I remove the comma. I get an error message for an unknown "var" on line 12.

What happened?
No pics no clicks. Seriously.
ImageImageImageImageImageImage
Cadde
Transport Coordinator
Transport Coordinator
Posts: 290
Joined: 07 Oct 2004 12:51

Re: Using wagons only for certain locomotives

Post by Cadde »

You most likely have a problem on a line further up. The "serializer" can travel quite a bit before it finds an unexpected character and only then will it complain.
If you aren't already, use notepad++ and import the NML syntax highlighting from here: http://dev.openttdcoop.org/documents/24
Then check to make sure you have all the required opening and closing brackets "(){}" etc prior in your file.

If you don't believe me, make a dummy GRF with just that switch statement in it besides the required GRF block.

EDIT: Oh and make sure you are on the latest NMLC version.

Code: Select all

C:\Users\Cadde>nmlc --version
0.4.4
Library versions encountered:
PLY: 3.4
PIL: 1.1.7
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Using wagons only for certain locomotives

Post by Alberth »

The switch being line 12. If I remove the comma. I get an error message for an unknown "var" on line 12.
"Unexpected token ','" means the parser expected something else than a comma.
Removing symbols that look ok on first sight is likely not the solution. (But I don't know that "var" thing, so my "ok" doesn't mean much.)

A common trick here is to split the line into several lines, where each line then contains a single comma.That is change the line to

Code: Select all

switch(FEAT_TRAINS,
 PARENT,
  switch_can_attach_its,
   [STORE_TEMP(position_in_consist_from_end,
    0x10F),
     var[0x61,
      0,
       0x0000FFFF,
        0xC6]]) {
Then run the program again, and the error message will point at a different line (most likely). Since there is only one comma at that line, you get precisely which comma is not recognized.

Before that comma, something is not right, most likely some bracket or a wrong number of arguments, as Cadde already said.
Being a retired OpenTTD developer does not mean I know what I am doing.
User avatar
Erato
Chief Executive
Chief Executive
Posts: 740
Joined: 25 May 2015 09:09
Location: The Netherlands

Re: Using wagons only for certain locomotives

Post by Erato »

Cadde wrote:You most likely have a problem on a line further up. The "serializer" can travel quite a bit before it finds an unexpected character and only then will it complain.
If you aren't already, use notepad++ and import the NML syntax highlighting from here: http://dev.openttdcoop.org/documents/24
Then check to make sure you have all the required opening and closing brackets "(){}" etc prior in your file.

If you don't believe me, make a dummy GRF with just that switch statement in it besides the required GRF block.

EDIT: Oh and make sure you are on the latest NMLC version.

Code: Select all

C:\Users\Cadde>nmlc --version
0.4.4
Library versions encountered:
PLY: 3.4
PIL: 1.1.7
All my brackets are properly opened and closed.
As for the version:

Code: Select all

nmlc --version
unknown
Library versions encountered:
PLY: 3.10
PIL: 1.1.7
Alberth wrote:
The switch being line 12. If I remove the comma. I get an error message for an unknown "var" on line 12.
"Unexpected token ','" means the parser expected something else than a comma.
Removing symbols that look ok on first sight is likely not the solution. (But I don't know that "var" thing, so my "ok" doesn't mean much.)

A common trick here is to split the line into several lines, where each line then contains a single comma.That is change the line to

Code: Select all

switch(FEAT_TRAINS,
 PARENT,
  switch_can_attach_its,
   [STORE_TEMP(position_in_consist_from_end,
    0x10F),
     var[0x61,
      0,
       0x0000FFFF,
        0xC6]]) {
Then run the program again, and the error message will point at a different line (most likely). Since there is only one comma at that line, you get precisely which comma is not recognized.

Before that comma, something is not right, most likely some bracket or a wrong number of arguments, as Cadde already said.
That, interestingly enough, results in an error on the latter of these lines:

Code: Select all

								0xC6]]) {
	ITEM_RANGE_ITS: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
It's the same error, even though there is no comma on that line, before that line and on the next line.
In a way, this error is still on the same line.
No pics no clicks. Seriously.
ImageImageImageImageImageImage
rowdog
Engineer
Engineer
Posts: 67
Joined: 24 May 2017 12:51
Location: East Texas

Re: Using wagons only for certain locomotives

Post by rowdog »

Erato wrote: That, interestingly enough, results in an error on the latter of these lines:

Code: Select all

								0xC6]]) {
	ITEM_RANGE_ITS: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
It's the same error, even though there is no comma on that line, before that line and on the next line.
In a way, this error is still on the same line.
Double check that your substitutions are getting expanded properly. I copied your code into a trivial nml and it compiled without error but, of course, I had to replace ITEM_RANGE_ITS* with constants.

In case it matters, I used the NotRoadTypes branch of nmlc.

Code: Select all

$ nmlc --version
v6347:96b8395ca393 from 2017-05-18
Library versions encountered:
PIL: 1.1.7
PLY: 3.10
User avatar
Erato
Chief Executive
Chief Executive
Posts: 740
Joined: 25 May 2015 09:09
Location: The Netherlands

Re: Using wagons only for certain locomotives

Post by Erato »

rowdog wrote:
Erato wrote: That, interestingly enough, results in an error on the latter of these lines:

Code: Select all

								0xC6]]) {
	ITEM_RANGE_ITS: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
It's the same error, even though there is no comma on that line, before that line and on the next line.
In a way, this error is still on the same line.
Double check that your substitutions are getting expanded properly. I copied your code into a trivial nml and it compiled without error but, of course, I had to replace ITEM_RANGE_ITS* with constants.

In case it matters, I used the NotRoadTypes branch of nmlc.

Code: Select all

$ nmlc --version
v6347:96b8395ca393 from 2017-05-18
Library versions encountered:
PIL: 1.1.7
PLY: 3.10
It makes sense that the code should work. For the range, I did this, which should work:

Code: Select all

#define ITEM_RANGE_ITS	\
	ITEM_ITS_PERSONEN,	\
	ITEM_ITS_MATERIAL,	\
	ITEM_ITS_MASSENGUT,	\
No pics no clicks. Seriously.
ImageImageImageImageImageImage
Cadde
Transport Coordinator
Transport Coordinator
Posts: 290
Joined: 07 Oct 2004 12:51

Re: Using wagons only for certain locomotives

Post by Cadde »

Well, your NMLC version reports as "unknown" and that to me sounds like you have some experimental NMLC to begin with.
User avatar
Erato
Chief Executive
Chief Executive
Posts: 740
Joined: 25 May 2015 09:09
Location: The Netherlands

Re: Using wagons only for certain locomotives

Post by Erato »

Cadde wrote:Well, your NMLC version reports as "unknown" and that to me sounds like you have some experimental NMLC to begin with.
I'm pretty certain that wouldn't be the case.
I tried it again in cmd and got this instead:

Code: Select all

v6201:45b1e0507a8d from 2016-12-23
Library versions encountered:
PLY: 3.4
PIL: 1.1.7
No pics no clicks. Seriously.
ImageImageImageImageImageImage
Cadde
Transport Coordinator
Transport Coordinator
Posts: 290
Joined: 07 Oct 2004 12:51

Re: Using wagons only for certain locomotives

Post by Cadde »

Right, still looks experimental to me. ;)
But now that i am a bit more awake, the #defines you have there... I don't normally use #defines, but that surely looks like it's gonna resolve to this:

Code: Select all

//ITS
switch(FEAT_TRAINS, PARENT, switch_can_attach_its, [STORE_TEMP(position_in_consist_from_end, 0x10F), var[0x61, 0, 0x0000FFFF, 0xC6]]) {
   ITEM_ITS_PERSONEN,   
   ITEM_ITS_MATERIAL,   
   ITEM_ITS_MASSENGUT,: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
   ITEM_RANGE_ITS_WAG: CB_RESULT_ATTACH_DISALLOW; //Allow self
   CB_RESULT_ATTACH_DISALLOW;
}
So, as you can see, not correct NML switch syntax unless the revision you are using allows that? If it does then show me that documentation please. ;)
But if it were me, assuming your id's are in sequential order... Let's for instance say that this were true:

Code: Select all

#define ITEM_ITS_PERSONEN 0x01
#define ITEM_ITS_MATERIAL 0x02   
#define ITEM_ITS_MASSENGUT 0x03
Then you could do this:

Code: Select all

#define ITEM_RANGE_ITS 0x01..0x03
#define ITEM_ITS_PERSONEN 0x01
#define ITEM_ITS_MATERIAL 0x02   
#define ITEM_ITS_MASSENGUT 0x03

#define ITEM_RANGE_ITS_WAG ...
...
//ITS
switch(FEAT_TRAINS, PARENT, switch_can_attach_its, [STORE_TEMP(position_in_consist_from_end, 0x10F), var[0x61, 0, 0x0000FFFF, 0xC6]]) {
   ITEM_RANGE_ITS: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
   ITEM_RANGE_ITS_WAG: CB_RESULT_ATTACH_DISALLOW; //Allow self
   CB_RESULT_ATTACH_DISALLOW;
}
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Using wagons only for certain locomotives

Post by Alberth »

@rowdog: Good catch, I didn't consider #define macros.
Cadde wrote:Right, still looks experimental to me. ;)
But now that i am a bit more awake, the #defines you have there... I don't normally use #defines, but that surely looks like it's gonna resolve to this:

Code: Select all

//ITS
switch(FEAT_TRAINS, PARENT, switch_can_attach_its, [STORE_TEMP(position_in_consist_from_end, 0x10F), var[0x61, 0, 0x0000FFFF, 0xC6]]) {
   ITEM_ITS_PERSONEN,   
   ITEM_ITS_MATERIAL,   
   ITEM_ITS_MASSENGUT,: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
   ITEM_RANGE_ITS_WAG: CB_RESULT_ATTACH_DISALLOW; //Allow self
   CB_RESULT_ATTACH_DISALLOW;
}
So, as you can see, not correct NML switch syntax unless the revision you are using allows that?/code]
Indeed, this is wrong. There is no NML version that allows trailing commas.

@Erato:To be fully correct, you also have to remove the \ at the end of the last line. (Now it also merges the line below it, that is, your #define is one line longer than you think it is.)
Being a retired OpenTTD developer does not mean I know what I am doing.
Transportman
Tycoon
Tycoon
Posts: 2781
Joined: 22 Feb 2011 18:34

Re: Using wagons only for certain locomotives

Post by Transportman »

Cadde wrote:Right, still looks experimental to me. ;)
But now that i am a bit more awake, the #defines you have there... I don't normally use #defines, but that surely looks like it's gonna resolve to this:

Code: Select all

//ITS
switch(FEAT_TRAINS, PARENT, switch_can_attach_its, [STORE_TEMP(position_in_consist_from_end, 0x10F), var[0x61, 0, 0x0000FFFF, 0xC6]]) {
   ITEM_ITS_PERSONEN,   
   ITEM_ITS_MATERIAL,   
   ITEM_ITS_MASSENGUT,: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
   ITEM_RANGE_ITS_WAG: CB_RESULT_ATTACH_DISALLOW; //Allow self
   CB_RESULT_ATTACH_DISALLOW;
}
So, as you can see, not correct NML switch syntax unless the revision you are using allows that? If it does then show me that documentation please. ;)
But if it were me, assuming your id's are in sequential order... Let's for instance say that this were true:

Code: Select all

#define ITEM_ITS_PERSONEN 0x01
#define ITEM_ITS_MATERIAL 0x02   
#define ITEM_ITS_MASSENGUT 0x03
Then you could do this:

Code: Select all

#define ITEM_RANGE_ITS 0x01..0x03
#define ITEM_ITS_PERSONEN 0x01
#define ITEM_ITS_MATERIAL 0x02   
#define ITEM_ITS_MASSENGUT 0x03

#define ITEM_RANGE_ITS_WAG ...
...
//ITS
switch(FEAT_TRAINS, PARENT, switch_can_attach_its, [STORE_TEMP(position_in_consist_from_end, 0x10F), var[0x61, 0, 0x0000FFFF, 0xC6]]) {
   ITEM_RANGE_ITS: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
   ITEM_RANGE_ITS_WAG: CB_RESULT_ATTACH_DISALLOW; //Allow self
   CB_RESULT_ATTACH_DISALLOW;
}
That is the reason I switched vehicle ID's in the 2cc TrainsInNML, to create ranges of vehicle IDs that are of the same type, so I could use those ranges in this type of switches.
Coder of the Dutch Trackset | Development support for the Dutch Trainset | Coder of the 2cc TrainsInNML
User avatar
Erato
Chief Executive
Chief Executive
Posts: 740
Joined: 25 May 2015 09:09
Location: The Netherlands

Re: Using wagons only for certain locomotives

Post by Erato »

Cadde wrote:Right, still looks experimental to me. ;)
But now that i am a bit more awake, the #defines you have there... I don't normally use #defines, but that surely looks like it's gonna resolve to this:

Code: Select all

//ITS
switch(FEAT_TRAINS, PARENT, switch_can_attach_its, [STORE_TEMP(position_in_consist_from_end, 0x10F), var[0x61, 0, 0x0000FFFF, 0xC6]]) {
   ITEM_ITS_PERSONEN,   
   ITEM_ITS_MATERIAL,   
   ITEM_ITS_MASSENGUT,: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
   ITEM_RANGE_ITS_WAG: CB_RESULT_ATTACH_DISALLOW; //Allow self
   CB_RESULT_ATTACH_DISALLOW;
}
So, as you can see, not correct NML switch syntax unless the revision you are using allows that? If it does then show me that documentation please. ;)
But if it were me, assuming your id's are in sequential order... Let's for instance say that this were true:

Code: Select all

#define ITEM_ITS_PERSONEN 0x01
#define ITEM_ITS_MATERIAL 0x02   
#define ITEM_ITS_MASSENGUT 0x03
Then you could do this:

Code: Select all

#define ITEM_RANGE_ITS 0x01..0x03
#define ITEM_ITS_PERSONEN 0x01
#define ITEM_ITS_MATERIAL 0x02   
#define ITEM_ITS_MASSENGUT 0x03

#define ITEM_RANGE_ITS_WAG ...
...
//ITS
switch(FEAT_TRAINS, PARENT, switch_can_attach_its, [STORE_TEMP(position_in_consist_from_end, 0x10F), var[0x61, 0, 0x0000FFFF, 0xC6]]) {
   ITEM_RANGE_ITS: CB_RESULT_ATTACH_ALLOW_IF_RAILTYPES; //Allow
   ITEM_RANGE_ITS_WAG: CB_RESULT_ATTACH_DISALLOW; //Allow self
   CB_RESULT_ATTACH_DISALLOW;
}
Ah. I understand.

Also. This worked :D thanks.
No pics no clicks. Seriously.
ImageImageImageImageImageImage
Post Reply

Return to “NewGRF Technical Discussions”

Who is online

Users browsing this forum: No registered users and 5 guests