NML - Random vehicle variant + Random cargo variant ?

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

Moderator: Graphics Moderators

Post Reply
User avatar
LaDoncella
Traffic Manager
Traffic Manager
Posts: 249
Joined: 28 Sep 2004 16:01
Contact:

NML - Random vehicle variant + Random cargo variant ?

Post by LaDoncella »

Hello again,
I was trying to make a vehicle with variants and cargo variants like,
Vehicle_Variant1_Cargo1
Vehicle_Variant1_Cargo2
Vehicle_Variant2_Cargo1
Vehicle_Variant2_Cargo2
In which the vehicle variant is chosen randomly at purchase/refit, and cargo variant is chosen randomly when new cargo is loaded.

However I'm not able to come up with a code that does this.
Is it possible? If so, how?


I tried the following setup.

Random Cargo graphics for the given variant:

Code: Select all

random_switch (FEAT_ROADVEHS, SELF, random_switch_spritgroup_VEHICLE_V1_WOOD, bitmask(TRIGGER_VEHICLE_UNLOAD_ALL, TRIGGER_VEHICLE_ANY_LOAD, TRIGGER_ALL_NEEDED)) {
	1: spritgroup_VEHICLE_V1_WOOD_c1;
	1: spritgroup_VEHICLE_V1_WOOD_c2;	//1+1 = 2 = 2^1
}
random_switch (FEAT_ROADVEHS, SELF, random_switch_spritgroup_VEHICLE_V2_WOOD, bitmask(TRIGGER_VEHICLE_UNLOAD_ALL, TRIGGER_VEHICLE_ANY_LOAD, TRIGGER_ALL_NEEDED)) {
	1: spritgroup_VEHICLE_V2_WOOD_c1;
	1: spritgroup_VEHICLE_V2_WOOD_c2;	//1+1 = 2 = 2^1
}
//Same for STEL//
Cargo switches that call the cargo randomizer

Code: Select all

switch (FEAT_ROADVEHS, SELF, switch_spritegroup_VEHICLE_V1, cargo_type_in_veh) {
	WOOD: random_switch_spritgroup_VEHICLE_V1_WOOD;
	STEL: random_switch_spritgroup_VEHICLE_V1_STEL;
}
switch (FEAT_ROADVEHS, SELF, switch_spritegroup_VEHICLE_V2, cargo_type_in_veh) {
	WOOD: random_switch_spritgroup_VEHICLE_V2_WOOD;
	STEL: random_switch_spritgroup_VEHICLE_V2_STEL;
}
Vehicle variant switch, it calls cargo switch depending on vehicle variant

Code: Select all

random_switch (FEAT_ROADVEHS, SELF, random_switch_spritegroup_VEHICLE ) {
	1: switch_spritegroup_VEHICLE_V1;	
	1: switch_spritegroup_VEHICLE_V2;	//1+1 = 2 = 2^1
}
Item code, we call for vehicle variant randomizer

Code: Select all

item(FEAT_ROADVEHS, item_VEHICLE) {
	property {
	    /* Properties */
	}
	graphics
	{
	    default: random_switch_spritegroup_VEHICLE;
	}
The problem with this code is that vehicle variant is also rerandomized when the tiggers for random_switch_spritgroup_VEHICLE_V1_CARGO are fulfilled.
User avatar
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5602
Joined: 13 Sep 2004 13:21
Location: The Moon

Re: NML - Random vehicle variant + Random cargo variant ?

Post by PikkaBird »

You need to use different random bits for each step. This is apparently achieved with the "independent" parameter.
User avatar
LaDoncella
Traffic Manager
Traffic Manager
Posts: 249
Joined: 28 Sep 2004 16:01
Contact:

Re: NML - Random vehicle variant + Random cargo variant ?

Post by LaDoncella »

Thanks, that did the trick.
TadeuszD
Transport Coordinator
Transport Coordinator
Posts: 329
Joined: 07 Nov 2011 19:32
Location: PL

Re: NML - Random vehicle variant + Random cargo variant ?

Post by TadeuszD »

I have similar question about randomization.

In my case I have one vehicle with two liveries randomly selected when the vehicle is bought.
But when this vehicle is created using 'clone' option, all vehicle properties, including random livery, are copied from original vehicle. In the effect, the original vehicle and cloned vehicle look identical. :(

Is there any possibility to force re-randomization for newly cloned vehicle using NML? I checked NML documentation, but most of randomization triggers are related to load/unload process. The only one hope is TRIGGER_VEHICLE_SERVICE. Is this trigger triggered immediately after the cloning/purchasing the vehicle in depot? How can I limit this trigger to its first run only?
Image
Transportman
Tycoon
Tycoon
Posts: 2781
Joined: 22 Feb 2011 18:34

Re: NML - Random vehicle variant + Random cargo variant ?

Post by Transportman »

TadeuszD wrote:I have similar question about randomization.

In my case I have one vehicle with two liveries randomly selected when the vehicle is bought.
But when this vehicle is created using 'clone' option, all vehicle properties, including random livery, are copied from original vehicle. In the effect, the original vehicle and cloned vehicle look identical. :(

Is there any possibility to force re-randomization for newly cloned vehicle using NML? I checked NML documentation, but most of randomization triggers are related to load/unload process. The only one hope is TRIGGER_VEHICLE_SERVICE. Is this trigger triggered immediately after the cloning/purchasing the vehicle in depot? How can I limit this trigger to its first run only?
Are you sure it is not working? I can't check right now, but IIRC the new vehicle should get a new random livery, so it might just be bad luck that you get the same vehicle over again. If you do get the same vehicle over and over again, it sounds like a bug to me.
Coder of the Dutch Trackset | Development support for the Dutch Trainset | Coder of the 2cc TrainsInNML
TadeuszD
Transport Coordinator
Transport Coordinator
Posts: 329
Joined: 07 Nov 2011 19:32
Location: PL

Re: NML - Random vehicle variant + Random cargo variant ?

Post by TadeuszD »

Transportman wrote:...IIRC the new vehicle should get a new random livery, so it might just be bad luck that you get the same vehicle over again.
Thanks. I've checked it again and you're right.
Indeed, liveries are selected randomly, but in practice... the randomness looks surprisingly bad. :| Or I have really bad luck to always obtain the same pattern. :P
Attachments
Cloning.png
Cloning.png (3.7 KiB) Viewed 3055 times
Image
User avatar
peignenappe
Engineer
Engineer
Posts: 9
Joined: 15 Jul 2022 19:59
Location: France

Re: NML - Random vehicle variant + Random cargo variant ?

Post by peignenappe »

Hey ! i'm experiencing a similar issue with a "load triggered" random switch.

I created a flatbed wagon using the following code : (i removed some repetitive parts as it has like 10 liveries and as many container variants)

Code: Select all

// CONTAINERS
spritegroup flatbed_container_spritegroup_a {
	loading: [flatbed_empty, flatbed_container_1a, flatbed_container_1b];
	loaded:  [flatbed_empty, flatbed_container_1b]; }
spritegroup flatbed_container_spritegroup_h {
	loading: [flatbed_empty_8, flatbed_container_8a, flatbed_container_8b];
	loaded:  [flatbed_empty_8, flatbed_container_8b, flatbed_container_8b]; }

random_switch (FEAT_TRAINS, SELF, flatbed_container_spritegroup_switch, bitmask(TRIGGER_VEHICLE_UNLOAD_ALL)) {
    1: flatbed_container_spritegroup_a;
    1: flatbed_container_spritegroup_h; }

// SWITCHES
random_switch(FEAT_TRAINS, SELF, flatbed_randomlivery_switch) {
    1: flatbed_sprite_0;
    1: flatbed_sprite_1;
}

switch(FEAT_TRAINS, SELF, flatbed_livery_switch, cargo_subtype) {
	1: flatbed_sprite_0;
	2: flatbed_sprite_1;
	flatbed_randomlivery_switch;
}

// CARGO SUBTYPE CALLBACK
switch(FEAT_TRAINS, SELF, flatbed_subtype_text_switch, cargo_subtype) {
	0: return string(STR_LIVERY_RND);
	1: return string(STR_LIVERY_0);
	return CB_RESULT_NO_TEXT;
}

switch(FEAT_TRAINS, SELF, flatbed_cargo_switch, cargo_type_in_veh) {
    BATT: flatbed_container_spritegroup_switch;
    BUBL: flatbed_rfpr_spritegroup;
[...]
    WDPR: flatbed_wdpr_spritegroup;
    WOOD: flatbed_wood_spritegroup_switch;
    flatbed_container_spritegroup_switch;
}

switch (FEAT_TRAINS, SELF, flatbed_vehicle_switch, STORE_TEMP(CB_FLAG_MORE_SPRITES | PALETTE_USE_DEFAULT, 0x100)) {
    return flatbed_livery_switch;
}

switch (FEAT_TRAINS, SELF, flatbed_load_switch, STORE_TEMP(PALETTE_USE_DEFAULT, 0x100)) {
    return flatbed_cargo_switch;
}

switch (FEAT_TRAINS, SELF, flatbed_graphics_switch, getbits(extra_callback_info1, 8, 8)) {
    0: return flatbed_vehicle_switch;
    return flatbed_load_switch;
}

item(FEAT_TRAINS, flatbed_wagon) {
	property {
		name: string(STR_FLATBED_WAGON_NAME);
		sprite_id: SPRITE_ID_NEW_TRAIN;
		cargo_capacity: 32;
		loading_speed: 8;
		cost_factor: 216;
		running_cost_factor: 0;
		running_cost_base: RUNNING_COST_NONE;
		power: 0;
		weight: 19 ton;
		bitmask_vehicle_info: 0;
		misc_flags: bitmask(TRAIN_FLAG_2CC, TRAIN_FLAG_SPRITE_STACK); // We use special sprites for passenger and mail wagons
		introduction_date: date(1995,1,1);
		climates_available: ALL_CLIMATES;
		model_life: VEHICLE_NEVER_EXPIRES;
		track_type: ELRL;
        	refittable_cargo_classes:        bitmask(CC_PIECE_GOODS, CC_EXPRESS, CC_OVERSIZED);
        	non_refittable_cargo_classes:    bitmask(CC_PASSENGERS, CC_REFRIGERATED);
        	cargo_allow_refit: [BATT, BDMT, BRCK, BUBL, COPR, DYES, ENSP, FERT, FMSP, FZDR, GLAS, GOOD, MNSP, PAPR, PETR, PLAS, PLST, RFPR, STEL, SWET, TWOD, VEHI, WDPR, WOOD, JAVA, YETI];
        	cargo_disallow_refit:  [PASS, [...] , URAN];
        	default_cargo_type:  WOOD;

}
graphics {
        cargo_capacity:     flatbed_capacity_switch;
	cargo_subtype_text: flatbed_subtype_text_switch;
	flatbed_graphics_switch;
}
}
Everything is working as expected but the random switch with bitmask(TRIGGER_VEHICLE_UNLOAD_ALL .. :?
The switch seems to be triggered at the purchase but not when loading/unloading. I tried using others triggers bitmaks like TRIGGER_VEHICLE_ANY_LOAD but none of them worked.

Is it possible that the switch isn't triggered because of the stacked sprite ?
I can't figure out how "independent:" parameter has to be used, can someone explain a little more about it ?

Thanks ! :]


EDIT :
I found out about random_trigger callback. Now the random switch works but still not as expected. X( When the switch is triggered, the load gets randomized but its the same value for every single wagon of the train.
I think there is something to do with the independent: param but i still dont understand how it works.
Image
Post Reply

Return to “NewGRF Technical Discussions”

Who is online

Users browsing this forum: No registered users and 16 guests