How to do simple variable running costs in NML?

Discuss, get help with, or post new graphics for TTDPatch and OpenTTD, using the NewGRF system, here. Graphics for plain TTD also acceptable here.

Moderator: Graphics Moderators

Post Reply
User avatar
NekoMaster
Tycoon
Tycoon
Posts: 4001
Joined: 16 Aug 2008 22:26
Skype: neko-master
Location: Oshawa, Ontario, CANADA

How to do simple variable running costs in NML?

Post by NekoMaster »

So I thought I would try something while I modify the OPenGFX Trains GRF to provide some more 32BPP vehicles modified for my own use, and I wanted to run it with Pineapple rail set.

Pineapple rail set has variable running costs where things cost more to run then when they are stopped (like loading/idling)

I was wondering how I might do a simple variable running cost set up where trains cost less when stopped/idling and cost their normal amount when moving/operating.


I'm not very good at understanding new coding or coming up with new stuff and of course the below doesnt work (NMLC doesnt know what "if" means)

Code: Select all

        if vehicle_is_stopped = 1
		than
		running_cost_factor:			90;
		else
		running_cost_factor: 			180; // $  6,748 xxx
Perhaps someone could help me with that? Or is it even possible to just do a simple if else statement to have things cost less when stopped/idling.
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!
Tony Pixel
Route Supervisor
Route Supervisor
Posts: 459
Joined: 14 Oct 2017 11:06
Location: Ukraine

Re: How to do simple variable running costs in NML?

Post by Tony Pixel »

You make all right, but there are wrong syntax, this is right:

Code: Select all

Running_cost_factor = (Vehicle_is_stopped = 1) ? 90 : 180;
Or You can use switch:

Code: Select all

switch (FEAT_TRAINS, SELF, sw_vehicle_name_running_cost_factor, vehicle_is_stopped) {
1: 90;
180;
}
Read this (https://newgrf-specs.tt-wiki.net/wiki/NML:If) or this (https://newgrf-specs.tt-wiki.net/wiki/NML:Switch)
Image Image
Sprite artist and NewGRF developer, coding in NML. My projects:
NewGRFs: Not Enough Subways Set | North American Passenger Railroads | Elevated Subway Bridge | Tony Pixel's Subway Equipment Set (frozen)
Screenshots: New York City Subway Episodes
Supercheese
Tycoon
Tycoon
Posts: 1660
Joined: 16 Dec 2007 22:24
Location: Idaho, USA

Re: How to do simple variable running costs in NML?

Post by Supercheese »

I do it by checking the vehicle's speed: if the speed = 0, then reduce running costs to 25% of what they would be when in motion.

Code: Select all

switch(FEAT_ROADVEHS, SELF, running_cost_switch, current_speed) {

    /* Running cost reduced to 1/4 if vehicle is not moving. */
    
	0: 				VEHICLE_RUNNING_COST_FACTOR / 4;
	
	return 			VEHICLE_RUNNING_COST_FACTOR;
}
Where VEHICLE_RUNNING_COST_FACTOR is of course the desired full value.
Eyecandy Road Vehicles | Fake Subways | Supercheese's NewObjects

"Fashions and cultures change, but steam trains shall always be majestic."
-Professor Hershel Layton
User avatar
NekoMaster
Tycoon
Tycoon
Posts: 4001
Joined: 16 Aug 2008 22:26
Skype: neko-master
Location: Oshawa, Ontario, CANADA

Re: How to do simple variable running costs in NML?

Post by NekoMaster »

I got it working thanks to some help from my good friend GWYD

Just make a switch above your item code like this

Code: Select all

//	RUNNING COST SWITCH
//	===================================
	switch(FEAT_TRAINS, SELF, switch_running_costs, current_speed) {
	0:			12/2; //IDLING COST TARGET:	 $	1/2 COST
	return  	12/1; //RUNNING COST TARGET:	$	1,640
	}
	
And then in your graphics section, you can add this to the top for variable running costs. By full run costs I mean the cost without any division.

Code: Select all

	graphics {
		running_cost_factor:					switch_running_costs; // USES A SWITCH FOR REAL COST
		purchase_running_cost_factor:		12; // DISPLAYS RUN COST IN PURCHASE MENU, COPY FULL RUN COST HERE
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
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5602
Joined: 13 Sep 2004 13:21
Location: The Moon

Re: How to do simple variable running costs in NML?

Post by PikkaBird »

Well done getting it working. :)

I don't know if we're creating a "standard" here, but like Supercheese I use 25% for the reduced cost when stopped.
Wahazar
Tycoon
Tycoon
Posts: 1451
Joined: 18 Jan 2014 18:10

Re: How to do simple variable running costs in NML?

Post by Wahazar »

BTW, what is a computer performance costs of running costs switch? I mean, how often it is checked?
Formerly known as: McZapkie
Projects: Reproducible Map Generation patch, NewGRFs: Manpower industries, PolTrams, Polroad, 600mm narrow gauge, wired, ECS industry extension, V4 CEE train set, HotHut.
Another favorite games: freeciv longturn, OHOL/2HOL.
User avatar
Gwyd
Chief Executive
Chief Executive
Posts: 721
Joined: 17 Apr 2017 16:52
Location: Western Ile-de-France Region

Re: How to do simple variable running costs in NML?

Post by Gwyd »

Well a running cost switch works the same way as any other switch... So there's one way to find out. Use the same switches to change graphics! I shall do some coding...

[EDIT] So I did some coding. My results for the frequency of updating are inconclusive. Graphics seem to be constantly updated. Stats such as power do not. Here the graphics show 0..9 based on the speed in mph (0=1..10, 1=11..12 etc). Power is set to 9125 for speeds 0..70, then to 1 for more than that. src attached. GPLv2.

Try putting the vehicle onto a stretch of track which alternates between 2 types of electrification (you'll need any other train set installed to allow this). Watch the power in "train details"

[EDIT 2] Some very odd things trigger the power switch to be checked. NewGRF changing? Yes. Stop/Start? Yes. Pausing? No. The actual speed of the vehicle? No. Find more!
Attachments
Switch testing.zip
(3.83 KiB) Downloaded 46 times
switch_test.grf
(5.29 KiB) Downloaded 41 times
User avatar
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5602
Joined: 13 Sep 2004 13:21
Location: The Moon

Re: How to do simple variable running costs in NML?

Post by PikkaBird »

Gwyd wrote:Watch the power in "train details"
Do note that the power being recalculated and the text in the train details being updated are not the same thing. The latter is not a desync risk and therefore does not have to be prioritised.

In any case, vehicle callbacks (and vehicle NewGRF in general) have never been shown to have an appreciable performance impact.
Wahazar
Tycoon
Tycoon
Posts: 1451
Joined: 18 Jan 2014 18:10

Re: How to do simple variable running costs in NML?

Post by Wahazar »

PikkaBird wrote: In any case, vehicle callbacks (and vehicle NewGRF in general) have never been shown to have an appreciable performance impact.
So my I make a conclusion, that running costs callback for each train vehicle (for example costs depending on vehicle load) would not affect computing performance?

I'm trying to improve balance between passenger/mail and freight cars.
Formerly known as: McZapkie
Projects: Reproducible Map Generation patch, NewGRFs: Manpower industries, PolTrams, Polroad, 600mm narrow gauge, wired, ECS industry extension, V4 CEE train set, HotHut.
Another favorite games: freeciv longturn, OHOL/2HOL.
User avatar
NekoMaster
Tycoon
Tycoon
Posts: 4001
Joined: 16 Aug 2008 22:26
Skype: neko-master
Location: Oshawa, Ontario, CANADA

Re: How to do simple variable running costs in NML?

Post by NekoMaster »

PikkaBird wrote:Well done getting it working. :)

I don't know if we're creating a "standard" here, but like Supercheese I use 25% for the reduced cost when stopped.
Do you mean 25% off the running cost (So you have 75% of the original cost) or bringing the running cost down at idle to 25% of the cost (So if the cost was $1000/yr, it would then be $250/yr at idle)
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
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5602
Joined: 13 Sep 2004 13:21
Location: The Moon

Re: How to do simple variable running costs in NML?

Post by PikkaBird »

NekoMaster wrote:Do you mean 25% off the running cost (So you have 75% of the original cost) or bringing the running cost down at idle to 25% of the cost (So if the cost was $1000/yr, it would then be $250/yr at idle)
The latter.
Post Reply

Return to “Graphics Development”

Who is online

Users browsing this forum: Google Adsense [Bot] and 41 guests