Difference between revisions of "NML:Old style callbacks"

From GRFSpecs
Jump to navigationJump to search
(content of nml r1625)
 
m (Changed the appropriate tables from t to sortable.)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
{{NMLNavNoSubpages}}
  +
 
This page contains information about the classical way of defining callbacks, which is handling them within a chain of switch-blocks. It is not recommended to use this method, but it is still supported for backwards compatibility, (temporary) support for nfo->nml converted grfs and because it may be useful in some cases. Do not use this for new NewGRFs unless you have a really good reason to do so. Note that this page is no longer updated, the information here may be out of date.
 
This page contains information about the classical way of defining callbacks, which is handling them within a chain of switch-blocks. It is not recommended to use this method, but it is still supported for backwards compatibility, (temporary) support for nfo->nml converted grfs and because it may be useful in some cases. Do not use this for new NewGRFs unless you have a really good reason to do so. Note that this page is no longer updated, the information here may be out of date.
   
Line 7: Line 9:
 
! use
 
! use
 
|-
 
|-
| CB_RANDOM_TRIGGER
+
| <code>CB_RANDOM_TRIGGER</code>
 
| re-randomize random data
 
| re-randomize random data
 
|}
 
|}
Line 15: Line 17:
 
==Callbacks for vehicles==
 
==Callbacks for vehicles==
   
{| class="t"
+
{| class="wikitable sortable"
 
! name
 
! name
 
! bit to set in callback_flags property
 
! bit to set in callback_flags property
Line 22: Line 24:
 
! use
 
! use
 
|-
 
|-
| VEH_CB_VISUAL_EFFECT_AND_POWERED
+
| <code>VEH_CB_VISUAL_EFFECT_AND_POWERED</code>
| VEH_CBF_VISUAL_EFFECT_AND_POWERED
+
| <code>VEH_CBF_VISUAL_EFFECT_AND_POWERED</code>
 
| Trains
 
| Trains
 
| See property <code style="color:darkgreen">visual_effect_and_powered</code>
 
| See property <code style="color:darkgreen">visual_effect_and_powered</code>
 
| decide the visual effect to use and whether a wagon has power
 
| decide the visual effect to use and whether a wagon has power
 
|-
 
|-
| VEH_CB_VISUAL_EFFECT
+
| <code>VEH_CB_VISUAL_EFFECT</code>
| VEH_CBF_VISUAL_EFFECT
+
| <code>VEH_CBF_VISUAL_EFFECT</code>
 
| Road Vehicles, Ships
 
| Road Vehicles, Ships
 
| See property <code style="color:darkgreen">visual_effect</code>
 
| See property <code style="color:darkgreen">visual_effect</code>
 
| decide the visual effect to use (road vehicles / ships)
 
| decide the visual effect to use (road vehicles / ships)
 
|-
 
|-
| VEH_CB_WAGON_LENGTH
+
| <code>VEH_CB_WAGON_LENGTH</code>
| VEH_CBF_WAGON_LENGTH
+
| <code>VEH_CBF_WAGON_LENGTH</code>
 
| Trains, Road Vehicles
 
| Trains, Road Vehicles
 
| See train property <code style="color:darkgreen">shorten_vehicle</code>
 
| See train property <code style="color:darkgreen">shorten_vehicle</code>
 
| decide upon the length of the wagon (note: RVs have no equivalent property)
 
| decide upon the length of the wagon (note: RVs have no equivalent property)
 
|-
 
|-
| VEH_CB_LOAD_AMOUNT
+
| <code>VEH_CB_LOAD_AMOUNT</code>
| VEH_CBF_LOAD_AMOUNT
+
| <code>VEH_CBF_LOAD_AMOUNT</code>
 
| All
 
| All
 
| See property <code style="color:darkgreen">loading_speed</code>
 
| See property <code style="color:darkgreen">loading_speed</code>
 
| decide upon the loading speed (amount loaded per tick)
 
| decide upon the loading speed (amount loaded per tick)
 
|-
 
|-
| VEH_CB_REFITTED_CAPACITY
+
| <code>VEH_CB_REFITTED_CAPACITY</code>
| VEH_CBF_REFITTED_CAPACITY
+
| <code>VEH_CBF_REFITTED_CAPACITY</code>
 
| All
 
| All
 
| Cargo capacity 0 ... 255
 
| Cargo capacity 0 ... 255
 
| decide upon the capacity after refit
 
| decide upon the capacity after refit
 
|-
 
|-
| VEH_CB_ARTICULATED_PARTS
+
| <code>VEH_CB_ARTICULATED_PARTS</code>
| VEH_CBF_ARTICULATED_PARTS
+
| <code>VEH_CBF_ARTICULATED_PARTS</code>
 
| Trains, Road Vehicles
 
| Trains, Road Vehicles
 
| ID of vehicle to attach, 0xFF if none
 
| ID of vehicle to attach, 0xFF if none
 
| decide upon the ID of the next vehicle added to the articulated vehicle chain
 
| decide upon the ID of the next vehicle added to the articulated vehicle chain
 
|-
 
|-
| VEH_CB_CARGO_SUFFIX
+
| <code>VEH_CB_CARGO_SUFFIX</code>
| VEH_CBF_CARGO_SUFFIX
+
| <code>VEH_CBF_CARGO_SUFFIX</code>
 
| All
 
| All
 
| string
 
| string
 
| decide upon cargo subtype display for refit
 
| decide upon cargo subtype display for refit
 
|-
 
|-
| VEH_CB_CAN_ATTACH_WAGON
+
| <code>VEH_CB_CAN_ATTACH_WAGON</code>
 
| None
 
| None
 
| Trains
 
| Trains
Line 70: Line 72:
 
| decide whether a wagon can be attached
 
| decide whether a wagon can be attached
 
|-
 
|-
| VEH_CB_TEXT_PURCHASE_SCREEN
+
| <code>VEH_CB_TEXT_PURCHASE_SCREEN</code>
 
| None
 
| None
 
| All
 
| All
Line 76: Line 78:
 
| decide upon an additional text for the purchase screen
 
| decide upon an additional text for the purchase screen
 
|-
 
|-
| VEH_CB_COLOUR_MAPPING
+
| <code>VEH_CB_COLOUR_MAPPING</code>
| VEH_CBF_COLOUR_MAPPING
+
| <code>VEH_CBF_COLOUR_MAPPING</code>
 
| All
 
| All
 
| sprite number
 
| sprite number
 
| decide upon the colour mapping instead of company colour
 
| decide upon the colour mapping instead of company colour
 
|-
 
|-
| VEH_CB_START_STOP_CHECK
+
| <code>VEH_CB_START_STOP_CHECK</code>
 
| None
 
| None
 
| All
 
| All
Line 88: Line 90:
 
| decide whether a vehicle may be started/stopped.
 
| decide whether a vehicle may be started/stopped.
 
|-
 
|-
| VEH_CB_32DAY
+
| <code>VEH_CB_32DAY</code>
 
| None
 
| None
 
| All
 
| All
Line 95: Line 97:
 
Triggers one or both of Trigger vehicle trigger 10 (bit 0) and Update colour map via VEH_CB_COLOUR_MAPPING (bit 1)
 
Triggers one or both of Trigger vehicle trigger 10 (bit 0) and Update colour map via VEH_CB_COLOUR_MAPPING (bit 1)
 
|-
 
|-
| VEH_CB_SOUND_EFFECT
+
| <code>VEH_CB_SOUND_EFFECT</code>
 
| None
 
| None
 
| All
 
| All
Line 101: Line 103:
 
| decide whether and which sound is being played for certain situations
 
| decide whether and which sound is being played for certain situations
 
|-
 
|-
| VEH_CB_AUTOREPLACE_SELECT
+
| <code>VEH_CB_AUTOREPLACE_SELECT</code>
 
| None
 
| None
 
| All
 
| All
Line 107: Line 109:
 
| decide upon appropriate replacement vehicles (not implemented in OpenTTD)
 
| decide upon appropriate replacement vehicles (not implemented in OpenTTD)
 
|-
 
|-
| VEH_CB_VEHICLE_PROPERTIES
+
| <code>VEH_CB_VEHICLE_PROPERTIES</code>
 
| None
 
| None
 
| All
 
| All
Line 114: Line 116:
 
|}
 
|}
   
In order to set properties for vehicles via callback <code style="color:darkgreen">VEH_CB_VEHICLE_PROPERTIES</code> it's necessary to query the general variable <code style="color:darkgreen">extra_callback_info1</code> which can take different values depending upon the vehicle type:
+
In order to set properties for vehicles via callback <code>VEH_CB_VEHICLE_PROPERTIES</code> it's necessary to query the general variable <code>extra_callback_info1</code> which can take different values depending upon the vehicle type:
   
{| class="t"
+
{| class="wikitable sortable"
 
! property
 
! property
 
! available for
 
! available for
 
! value
 
! value
 
|-
 
|-
| cost_factor
+
| <code>cost_factor</code>
 
| all
 
| all
 
| PROP_XXX_COST_FACTOR
 
| PROP_XXX_COST_FACTOR
 
|-
 
|-
| running_cost_factor
+
| <code>running_cost_factor</code>
 
| all
 
| all
 
| PROP_XXX_RUNNING_COST_FACTOR
 
| PROP_XXX_RUNNING_COST_FACTOR
 
|-
 
|-
| speed
+
| <code>speed</code>
 
| all
 
| all
 
| PROP_XXX_SPEED
 
| PROP_XXX_SPEED
 
|-
 
|-
| power
+
| <code>power</code>
 
| trains and road vehicles
 
| trains and road vehicles
 
| PROP_XXX_POWER
 
| PROP_XXX_POWER
 
|-
 
|-
| weight
+
| <code>weight</code>
 
| trains and road vehicles
 
| trains and road vehicles
 
| PROP_XXX_WEIGHT
 
| PROP_XXX_WEIGHT
 
|-
 
|-
| tractive_effort_coefficient
+
| <code>tractive_effort_coefficient</code>
 
| trains and road vehicles
 
| trains and road vehicles
 
| PROP_XXX_TRACTIVE_EFFORT_COEFFICIENT
 
| PROP_XXX_TRACTIVE_EFFORT_COEFFICIENT
 
|-
 
|-
| cargo_capacity
+
| <code>cargo_capacity</code>
 
| all except aircraft
 
| all except aircraft
 
| PROP_XXX_CARGO_CAPACITY
 
| PROP_XXX_CARGO_CAPACITY
 
|-
 
|-
| passenger_capacity
+
| <code>passenger_capacity</code>
 
| aircraft
 
| aircraft
 
| PROP_AIRCRAFT_PASSENGER_CAPACITY
 
| PROP_AIRCRAFT_PASSENGER_CAPACITY
 
|-
 
|-
| mail_capacity
+
| <code>mail_capacity</code>
 
| aircraft
 
| aircraft
 
| PROP_AIRCRAFT_MAIL_CAPACITY
 
| PROP_AIRCRAFT_MAIL_CAPACITY
 
|-
 
|-
| bitmask_vehicle_info
+
| <code>bitmask_vehicle_info</code>
 
| trains
 
| trains
 
| PROP_TRAINS_BITMASK_VEHICLE_INFO
 
| PROP_TRAINS_BITMASK_VEHICLE_INFO
Line 166: Line 168:
 
The following code is an example for the use of callbacks and most notably also the creation of articulated vehicles which can only be created by means of the VEH_CB_ARTICULATED_PARTS:
 
The following code is an example for the use of callbacks and most notably also the creation of articulated vehicles which can only be created by means of the VEH_CB_ARTICULATED_PARTS:
   
<pre style="color:blue">
+
<pre>
 
/* Here would have to be definition of sprite sets and groups */
 
/* Here would have to be definition of sprite sets and groups */
 
 
Line 213: Line 215:
 
==Industry Tile Callbacks==
 
==Industry Tile Callbacks==
   
{| class="t"
+
{| class="wikitable sortable"
 
! Callback
 
! Callback
 
! Values
 
! Values
Line 253: Line 255:
 
==Industry callback flags==
 
==Industry callback flags==
   
{| class="t"
+
{| class="wikitable sortable"
 
! callback and callback flag<br /> IND_CB_XXX and IND_CBF_XXX
 
! callback and callback flag<br /> IND_CB_XXX and IND_CBF_XXX
 
! return values
 
! return values
Line 320: Line 322:
 
==Cargo callbacks==
 
==Cargo callbacks==
   
{| class="t"
+
{| class="wikitable sortable"
 
! value
 
! value
 
! description
 
! description
Line 333: Line 335:
 
==Object callbacks==
 
==Object callbacks==
   
{| class="t"
+
{| class="wikitable sortable"
 
! Name
 
! Name
 
! Meaning
 
! Meaning

Latest revision as of 21:20, 30 August 2022

This page contains information about the classical way of defining callbacks, which is handling them within a chain of switch-blocks. It is not recommended to use this method, but it is still supported for backwards compatibility, (temporary) support for nfo->nml converted grfs and because it may be useful in some cases. Do not use this for new NewGRFs unless you have a really good reason to do so. Note that this page is no longer updated, the information here may be out of date.

General callbacks

name use
CB_RANDOM_TRIGGER re-randomize random data

This callback may be called for all items that have triggers associated with them. See random switch for more information.

Callbacks for vehicles

name bit to set in callback_flags property available for return value use
VEH_CB_VISUAL_EFFECT_AND_POWERED VEH_CBF_VISUAL_EFFECT_AND_POWERED Trains See property visual_effect_and_powered decide the visual effect to use and whether a wagon has power
VEH_CB_VISUAL_EFFECT VEH_CBF_VISUAL_EFFECT Road Vehicles, Ships See property visual_effect decide the visual effect to use (road vehicles / ships)
VEH_CB_WAGON_LENGTH VEH_CBF_WAGON_LENGTH Trains, Road Vehicles See train property shorten_vehicle decide upon the length of the wagon (note: RVs have no equivalent property)
VEH_CB_LOAD_AMOUNT VEH_CBF_LOAD_AMOUNT All See property loading_speed decide upon the loading speed (amount loaded per tick)
VEH_CB_REFITTED_CAPACITY VEH_CBF_REFITTED_CAPACITY All Cargo capacity 0 ... 255 decide upon the capacity after refit
VEH_CB_ARTICULATED_PARTS VEH_CBF_ARTICULATED_PARTS Trains, Road Vehicles ID of vehicle to attach, 0xFF if none decide upon the ID of the next vehicle added to the articulated vehicle chain
VEH_CB_CARGO_SUFFIX VEH_CBF_CARGO_SUFFIX All string decide upon cargo subtype display for refit
VEH_CB_CAN_ATTACH_WAGON None Trains string containing error message, or one of the following: 0xFD (disallow with message "incomatible railtypes"), 0xFE (allow) or 0xFF (allow if railtypes match) decide whether a wagon can be attached
VEH_CB_TEXT_PURCHASE_SCREEN None All string decide upon an additional text for the purchase screen
VEH_CB_COLOUR_MAPPING VEH_CBF_COLOUR_MAPPING All sprite number decide upon the colour mapping instead of company colour
VEH_CB_START_STOP_CHECK None All string containing error message, or 0xFF to allow decide whether a vehicle may be started/stopped.
VEH_CB_32DAY None All bitmask

Triggers one or both of Trigger vehicle trigger 10 (bit 0) and Update colour map via VEH_CB_COLOUR_MAPPING (bit 1)

VEH_CB_SOUND_EFFECT None All sound effect number decide whether and which sound is being played for certain situations
VEH_CB_AUTOREPLACE_SELECT None All vehicle ID decide upon appropriate replacement vehicles (not implemented in OpenTTD)
VEH_CB_VEHICLE_PROPERTIES None All property-dependent Change vehicle properties, see below

In order to set properties for vehicles via callback VEH_CB_VEHICLE_PROPERTIES it's necessary to query the general variable extra_callback_info1 which can take different values depending upon the vehicle type:

property available for value
cost_factor all PROP_XXX_COST_FACTOR
running_cost_factor all PROP_XXX_RUNNING_COST_FACTOR
speed all PROP_XXX_SPEED
power trains and road vehicles PROP_XXX_POWER
weight trains and road vehicles PROP_XXX_WEIGHT
tractive_effort_coefficient trains and road vehicles PROP_XXX_TRACTIVE_EFFORT_COEFFICIENT
cargo_capacity all except aircraft PROP_XXX_CARGO_CAPACITY
passenger_capacity aircraft PROP_AIRCRAFT_PASSENGER_CAPACITY
mail_capacity aircraft PROP_AIRCRAFT_MAIL_CAPACITY
bitmask_vehicle_info trains PROP_TRAINS_BITMASK_VEHICLE_INFO

Where for the properties available for all vehicle types XXX can either be TRAINS, ROADVEHS, SHIPS or AIRCRAFT. Note also that the return values don't yet support units, you'll need to enter the properties untranslated (e.g. road vehicle weights are in quantities of 0.25 tons).

The following code is an example for the use of callbacks and most notably also the creation of articulated vehicles which can only be created by means of the VEH_CB_ARTICULATED_PARTS:

 /* Here would have to be definition of sprite sets and groups */
 
 /* callback for articulated vehicles: 4 wagons with special sprites for first and last */
 switch(FEAT_TRAINS, SELF, rail_engine_articulated_parts, extra_callback_info1) {
     1..2: return rail_wagon; // middle parts use rail wagons
 	3: return rail_engine;   // last part is this rail engine again
     CB_FAILED;               // no more than 4 vehicles
 }
 
 /* Set the (empty) weight of the vehicle depending on the cargo it carries */
 switch (FEAT_TRAINS, SELF, rail_wagon_cb_property_weight_switch, cargo_type_in_veh) {
     PASS: return 30; // Passenger wagons are heavier
     TOUR: return 30; // Tourists are also passengers
     CB_FAILED;       // use value set in the property block for other cargos
 }
 
 /* Set (conditionally) different properties of the train */
 switch (FEAT_TRAINS, SELF, rail_engine_cb_property_switch, extra_callback_info1) {
     PROP_TRAINS_WEIGHT          : rail_engine_cb_property_weight_switch;
     PROP_TRAINS_CARGO_CAPACITY  : rail_engine_cb_capacity_switch; // not shown here
     CB_FAILED;                                                    // we don't set other properties
 }
 
 /* Check for each (active) callback */
 switch (FEAT_TRAINS, SELF, rail_engine_switch, current_callback) {
     VEH_CB_REFITTED_CAPACITY:  rail_engine_cb_capacity_switch; // switch not shown here
     VEH_CB_VEHICLE_PROPERTIES: rail_engine_cb_property_switch;
     VEH_CB_ARTICULATED_PARTS: rail_engine_articulated_parts;
     rail_engine_graphics_switch; // go to graphics branch, if no callback asked for; not shown here
 }
 
 /* Define this rail engine */
 item(FEAT_TRAINS, rail_engine) {
     property {
         callback_flags:                 bitmask(VEH_CBF_ARTICULATED_PARTS, VEH_CBF_REFITTED_CAPACITY); // activate callbacks
         cargo_capacity:                 20;
         weight:                         24 ton;
     }
     graphics {
         rail_engine_switch;
     }
 }

Industry Tile Callbacks

Callback Values Meaning
INDTILE_CB_ANIM_STARTSTOP animation frame or CB_RESULT_XXX NEXT_FRAME | DO_NOTHING]
INDTILE_CB_ANIM_NEXT_FRAME animation frame or CB_RESULT_XXX NEXT_FRAME]
INDTILE_CB_ANIM_SPEED length Length of the animation frame as defined in the apendix. This is a very expensive callback and try to avoid it, if possible, by using identical animation frames.
INDTILE_CB_CARGO_AMOUNT_ACCEPT special accepted cargo amounts in 1/8. Bits 0...3: 1st cargo, bits 4 ... 7: 2nd cargo, bits 8 ... 11: 3rd cargo
INDTILE_CB_CARGO_TYPE_ACCEPT special accepted cargo types as entry in the CTT. Bits 0...4: 1st cargo, bits 5 ... 9: 2nd cargo, bits 10 ... 14: 3rd cargo
INDTILE_CB_SLOPE_IS_SUITABLE decide whether the location is suitable for the tile (for build checks of the industry)
INDTILE_CB_FOUNDATIONS CB_RESULT_XXX NO_FOUNDATIONS]
INDTILE_CB_AUTOSLOPE CB_RESULT_XXX NO_AUTOSLOPE]

Industry callback flags

callback and callback flag
IND_CB_XXX and IND_CBF_XXX
return values meaning
IND_CBF_AVAILABILITY CB_RESULT_IND_ALLOW
CB_RESULT_IND_DISALLOW
decide upon availability of the industry type.
extra_callback_info_2 allows to get information on how the industry is being built:
0: during map generation
1: random generation ingame
2: user trying to fund or prospect the industry

IND_CBF_PROD_CB_CARGO_ARRIVE
(no callback, use produce block)

n/a (use produce block) handle production when cargo is delivered. See produce block

IND_CBF_PROD_CB_256_TICKS
(no callback, use produce block)

n/a (use produce block) handle production every 256 ticks. See produce block
IND_CBF_LOCATION_CHECK string
CB_RESULT_IND_ALLOW_LOCATION
CB_RESULT_IND_DISALLOW_UNSUITABLE
CB_RESULT_IND_DISALLOW_ONLY_RAINFOREST
CB_RESULT_IND_DISALLOW_ONLY_DESERT
handle check for permissible build locations
extra_callback_info_2 allows to get information on how the industry is being built:
0: during map generation
1: random generation ingame
2: user trying to fund the industry
3: user trying to prospect the industry
IND_CBF_RANDOM_PROD_CHANGE see monthly production changes below handle random production changes
IND_CBF_MONTHLY_PROD_CHANGE CB_RESULT_IND_PROD_NO_CHANGE
CB_RESULT_IND_PROD_HALF
CB_RESULT_IND_PROD_DOUBLE
CB_RESULT_IND_PROD_CLOSE
CB_RESULT_IND_PROD_RANDOM
CB_RESULT_IND_PROD_DIVIDE_BY_4
CB_RESULT_IND_PROD_DIVIDE_BY_8
CB_RESULT_IND_PROD_DIVIDE_BY_16
CB_RESULT_IND_PROD_DIVIDE_BY_32
CB_RESULT_IND_PROD_MULTIPLY_BY_4
CB_RESULT_IND_PROD_MULTIPLY_BY_8
CB_RESULT_IND_PROD_MULTIPLY_BY_16
CB_RESULT_IND_PROD_MULTIPLY_BY_32
CB_RESULT_IND_PROD_DECREMENT_BY_1
CB_RESULT_IND_PROD_INCREMENT_BY_1
CB_RESULT_IND_PROD_SET_BY_0x100
handle monthly production changes
IND_CBF_CARGO_SUBTYPE_DISPLAY string decide on displayed cargo subtypes
IND_CBF_EXTRA_TEXT_FUND string decide on extra text displayed in industry funding window
IND_CBF_EXTRA_TEXT_INDUSTRY string decide on extra text in the industry window
IND_CBF_CONTROL_SPECIAL CB_RESULT_IND_DO_NOT_USE_SPECIAL
CB_RESULT_IND_USE_SPECIAL
decide whether the special defined in spec_flags shall be used or not
IND_CBF_STOP_ACCEPT_CARGO decide on cargo acceptance
IND_CBF_COLOUR 0...15

decide on industry colour. See also the palettes. DARK_BLUE (#775) corresponds to the colour 0.

IND_CBF_CARGO_INPUT cargo label decide on accepted cargo types. extra_callback_info_1 gives the input cargo number. Return 0xFF for any other value than 0, 1 or 2 for extra_callback_info_1.
IND_CBF_CARGO_OUTPUT cargo label decide on output cargo types. extra_callback_info_1 gives the output cargo number. Return 0xFF for any other value than 0 or 1 for extra_callback_info_1.

Cargo callbacks

value description
CARGO_CB_PROFIT Custom profit calculation
CARGO_CB_STATION_RATING Custom station rating calculation

Object callbacks

Name Meaning
OBJ_CB_SLOPE_CHECK Check for the slope of a tile
OBJ_CB_DECIDE_ANIM Decide whether animation continues: 0xFF stops animation, 0xFE continues with next frame
OBJ_CB_DECIDE_ANIM_SPEED Decide the animation frame to jump to. Or alternatively: 0xFF to stop animation, 0xFE to start animation with current frame, 0xFD to keep the current animation frame and do nothing
OBJ_CB_DECIDE_COLOUR Return the colour, or for 2 company colours the two colours, to be used. In the case of 2CC the 2nd company colour goes into the high 4 bits of the return value
OBJ_CB_ADDITIONAL_TEXT Return an additional string which is shown in the purchase menu. It should not exceed in length three lines.
OBJ_CB_AUTOSLOPE Return a value different from 0 in order to disable autoslope behaviour.