/*
 * This file is aimed to provide an example on how to code a vehicle in NML
 * In this case a road vehicle is coded, other vehicle types work in a similar fashion.
 * To keep the code readable, not every property or variable is documented in
 * detail, refer to the vehicle-specific reference in the documentation.
 * The coded vehicle is relatively simple. Apart from the minimum required,
 * it has cargo-specific capacaity and graphics. For a more advanced example,
 * refer to the train example.
 *
 * The vehicle coded here is a first-generation flatbed truck.
 * It is taken from OpenGFX+ Road Vehicles, with some modifications
 * to provide a better example. Original graphics are by DanMack and Zephyris,
 * coding by Terkhen and planetmaker.
 *
 * Apart from this file, you will also need the following
 * - Graphics, found in 'gfx' folder.
 * - Language files, to be placed in the 'lang' folder.
 *      Currently only english.lng is supplied.
 */

/*
 * First, define a grf block. This defines some basic properties of the grf,
 * which are required for the grf to be valid and loadable. Additionally,
 * user-configurable parameters are defined here also.
 */
grf {
    /* This grf is part of NML, therefore "NML" is chosen as the first three
     * characters of the GRFID. It is the fourth real grf defined as part of
     * NML, therefore the last character is set to 3 as numbering is zero-based.
     * Successive grfs will have 4, 5, etc., to make sure each example grf has
     * a unique GRFID.
     */
    grfid: "NML\03";
    /* GRF name and description strings are defined in the lang files */
    name: string(STR_GRF_NAME);
    desc: string(STR_GRF_DESC);
    /* This is the first version, start numbering at 1. */
    version: 1;
    min_compatible_version: 1;
}

/* Check for engine pool */
if (!dynamic_engines) {
    error(ERROR, USED_WITH, string(STR_ERROR_ENGINE_POOL));
}

/* Define a cargo translation table
 * All cargo types that need any special treatment must be included here
 * Add cargos used in the refit mask first, as those are limited to 32 bits
 * so these cargos must be within the first 32 entries */
cargotable {
/* Used in refit mask */
    LVST, // Livestock (piece goods)
    WOOL, // Wool (piece goods, covered)
    SCRP, // Scrap metal (bulk)
    FICR, // Fibre crops (bulk, piece goods)
    PETR, // Petrol / fuel oil (liquid)
    RFPR, // Refined products (liquid)

/* Not used in refit mask */
    GOOD, // Goods (express)
    ENSP, // Engineering supplies (express, piece goods)
    FMSP, // Farm supplies (express, piece goods)
    MNSP, // Manufacturing supplies (express, piece goods)
    PAPR, // Paper (piece goods)
    STEL, // Steel (piece goods)
    VEHI, // Vehicles (piece goods, oversized)
    COPR, // Copper (piece goods)
    WOOD, // Wood (piece goods)
}

roadtypetable {
    ROAD,
    ELRD,
    "2YEL", // Labels beginning with a number must be given as strings to avoid a syntax error.

    // RED_TEST refers to RED_ if a roadtype with that label exists in-game.
    // If not, the fallbacks will be tried until reaching one that does exist.
    RED_TEST: [RED_, REDR, ROAD]
}

/* Sprite template for a truck */
template tmpl_truck(x) {
    [ 0 + x,   0,  8, 18,  -3, -10]
    [ 16 + x,  0, 20, 16, -14,  -7]
    [ 48 + x,  0, 28, 12, -14,  -6]
    [ 96 + x,  0, 20, 16,  -6,  -7]
    [ 128 + x, 0,  8, 18,  -3, -10]
    [ 144 + x, 0, 20, 16, -14,  -7]
    [ 176 + x, 0, 28, 12, -14,  -6]
    [ 224 + x, 0, 20, 16,  -6,  -7]
}

/* Define various cargo-specific graphics */

/* Paper */
spriteset(flatbed_truck_1_paper_empty, "gfx/flatbed_truck_1_paper.png") { tmpl_truck(0) }
spriteset(flatbed_truck_1_paper_full, "gfx/flatbed_truck_1_paper.png") { tmpl_truck(260) }
spritegroup flatbed_truck_1_paper {
    loaded: [flatbed_truck_1_paper_empty, flatbed_truck_1_paper_full];
    loading: [flatbed_truck_1_paper_empty, flatbed_truck_1_paper_full];
}

/* Steel */
spriteset(flatbed_truck_1_steel_empty, "gfx/flatbed_truck_1_steel.png") { tmpl_truck(0) }
spriteset(flatbed_truck_1_steel_full, "gfx/flatbed_truck_1_steel.png") { tmpl_truck(260) }
spritegroup flatbed_truck_1_steel {
    loaded: [flatbed_truck_1_steel_empty, flatbed_truck_1_steel_full];
    loading: [flatbed_truck_1_steel_empty, flatbed_truck_1_steel_full];
}

/* Wood */
spriteset(flatbed_truck_1_wood_empty, "gfx/flatbed_truck_1_wood.png") { tmpl_truck(0) }
spriteset(flatbed_truck_1_wood_full, "gfx/flatbed_truck_1_wood.png") { tmpl_truck(260) }
spritegroup flatbed_truck_1_wood {
    loaded: [flatbed_truck_1_wood_empty, flatbed_truck_1_wood_full];
    loading: [flatbed_truck_1_wood_empty, flatbed_truck_1_wood_full];
}

/* Copper */
spriteset(flatbed_truck_1_copper_empty, "gfx/flatbed_truck_1_copper.png") { tmpl_truck(0) }
spriteset(flatbed_truck_1_copper_full, "gfx/flatbed_truck_1_copper.png") { tmpl_truck(260) }
spritegroup flatbed_truck_1_copper {
    loaded: [flatbed_truck_1_copper_empty, flatbed_truck_1_copper_full];
    loading: [flatbed_truck_1_copper_empty, flatbed_truck_1_copper_full];
}

/* Goods */
spriteset(flatbed_truck_1_goods_empty, "gfx/flatbed_truck_1_goods.png") { tmpl_truck(0) }
spriteset(flatbed_truck_1_goods_full, "gfx/flatbed_truck_1_goods.png") { tmpl_truck(260) }
spritegroup flatbed_truck_1_goods {
    loaded: [flatbed_truck_1_goods_empty, flatbed_truck_1_goods_full];
    loading: [flatbed_truck_1_goods_empty, flatbed_truck_1_goods_full];
}

/* Decide capacity based on cargo type (cargo_capacity callback)
 * cargo_type_in_veh is available in the purchase list also,
 * so a separate CB for in the purchase list is not needed */
switch (FEAT_ROADVEHS, SELF, flatbed_truck_1_capacity_switch, cargo_type_in_veh) {
    GOOD: return 14;
    ENSP: return 14;
    FMSP: return 14;
    MNSP: return 14;
    PAPR: return 15;
    PETR: return 10;
    RFPR: return 10;
    STEL: return 15;
    VEHI: return 12;
    return 20;
}

/* Define the road vehicle */
item(FEAT_ROADVEHS, flatbed_truck_1) {
    property {
        /* Properties common to all vehicle types */
        name: string(STR_NAME_FLATBED_TRUCK_1);
        climates_available: bitmask(CLIMATE_TEMPERATE, CLIMATE_ARCTIC, CLIMATE_TROPICAL);
        road_type: ROAD;
        introduction_date: date(1926,01,01);
        model_life: 65;
        /* retire_early not set, use default retirement behaviour */
        vehicle_life: 15;
        reliability_decay: 20;
        refittable_cargo_classes: bitmask(CC_PIECE_GOODS, CC_EXPRESS);
        non_refittable_cargo_classes: bitmask(CC_PASSENGERS, CC_REFRIGERATED);
        /* Livestock, wool and fibre crops can be transported acc. cargo classes
         * But we don't want that, so disable refitting for them.
         * Scrap metal, petrol and refined products cannot be transported acc. cargo classes
         * We do want to transport those, so enable refitting for them. */
        cargo_allow_refit: [LVST, WOOL, SCRP, FICR, PETR, RFPR];
        cargo_disallow_refit: []; // we allow other cargoes, if class matches
        loading_speed: 5;
        cost_factor: 108;
        running_cost_factor: 90;
        /* cargo_age_period is left at default */

        /* RV-specific properties */
        sprite_id: SPRITE_ID_NEW_ROADVEH;
        speed: 48 km/h;
        misc_flags: bitmask(ROADVEH_FLAG_2CC);
        refit_cost: 0; // Refitting is free
        /* callback_flags are not set, no need to manually enable callbacks */
        running_cost_base: RUNNING_COST_ROADVEH;
        power: 120 hp;
        weight: 9.5 ton;
        /* TE and air drag coefficient is left at default */
        cargo_capacity: 20; // Changed by callback
        sound_effect: SOUND_DEPARTURE_OLD_RV_1;
        /* Visual effect is left at default (no effect) */
    }

    /* Define graphics for various cargo types, as well as the capacity callback */
    graphics {
        cargo_capacity: flatbed_truck_1_capacity_switch;
        PAPR: flatbed_truck_1_paper;
        STEL: flatbed_truck_1_steel;
        COPR: flatbed_truck_1_copper;
        WOOD: flatbed_truck_1_wood;
        default: flatbed_truck_1_goods; // Default to Goods.
    }
}

/* Define the road vehicle */
item(FEAT_ROADVEHS, flatbed_truck_2) {
    property {
        /* Properties common to all vehicle types */
        name: string(STR_NAME_FLATBED_TRUCK_2);
        climates_available: bitmask(CLIMATE_TEMPERATE, CLIMATE_ARCTIC, CLIMATE_TROPICAL);
        road_type: ELRD;
        introduction_date: date(1926,01,01);
        model_life: 65;
        /* retire_early not set, use default retirement behaviour */
        vehicle_life: 15;
        reliability_decay: 20;
        refittable_cargo_classes: bitmask(CC_PIECE_GOODS, CC_EXPRESS);
        non_refittable_cargo_classes: bitmask(CC_PASSENGERS, CC_REFRIGERATED);
        /* Livestock, wool and fibre crops can be transported acc. cargo classes
         * But we don't want that, so disable refitting for them.
         * Scrap metal, petrol and refined products cannot be transported acc. cargo classes
         * We do want to transport those, so enable refitting for them. */
        cargo_allow_refit: [LVST, WOOL, SCRP, FICR, PETR, RFPR];
        cargo_disallow_refit: []; // we allow other cargoes, if class matches
        loading_speed: 5;
        cost_factor: 108;
        running_cost_factor: 90;
        /* cargo_age_period is left at default */

        /* RV-specific properties */
        sprite_id: SPRITE_ID_NEW_ROADVEH;
        speed: 48 km/h;
        misc_flags: bitmask(ROADVEH_FLAG_2CC);
        refit_cost: 0; // Refitting is free
        /* callback_flags are not set, no need to manually enable callbacks */
        running_cost_base: RUNNING_COST_ROADVEH;
        power: 120 hp;
        weight: 9.5 ton;
        /* TE and air drag coefficient is left at default */
        cargo_capacity: 20; // Changed by callback
        sound_effect: SOUND_DEPARTURE_OLD_RV_1;
        /* Visual effect is left at default (no effect) */
    }

    /* Define graphics for various cargo types, as well as the capacity callback */
    graphics {
        cargo_capacity: flatbed_truck_1_capacity_switch;
        PAPR: flatbed_truck_1_paper;
        STEL: flatbed_truck_1_steel;
        COPR: flatbed_truck_1_copper;
        WOOD: flatbed_truck_1_wood;
        default: flatbed_truck_1_goods; // Default to Goods.
    }
}

/* Define the road vehicle */
item(FEAT_ROADVEHS, flatbed_truck_3) {
    property {
        /* Properties common to all vehicle types */
        name: string(STR_NAME_FLATBED_TRUCK_3);
        climates_available: bitmask(CLIMATE_TEMPERATE, CLIMATE_ARCTIC, CLIMATE_TROPICAL);
        road_type: roadtype("2YEL"); // Must use `roadtype("")` when the label starts with a number.
        introduction_date: date(1926,01,01);
        model_life: 65;
        /* retire_early not set, use default retirement behaviour */
        vehicle_life: 15;
        reliability_decay: 20;
        refittable_cargo_classes: bitmask(CC_PIECE_GOODS, CC_EXPRESS);
        non_refittable_cargo_classes: bitmask(CC_PASSENGERS, CC_REFRIGERATED);
        /* Livestock, wool and fibre crops can be transported acc. cargo classes
         * But we don't want that, so disable refitting for them.
         * Scrap metal, petrol and refined products cannot be transported acc. cargo classes
         * We do want to transport those, so enable refitting for them. */
        cargo_allow_refit: [LVST, WOOL, SCRP, FICR, PETR, RFPR];
        cargo_disallow_refit: []; // we allow other cargoes, if class matches
        loading_speed: 5;
        cost_factor: 108;
        running_cost_factor: 90;
        /* cargo_age_period is left at default */

        /* RV-specific properties */
        sprite_id: SPRITE_ID_NEW_ROADVEH;
        speed: 48 km/h;
        misc_flags: bitmask(ROADVEH_FLAG_2CC);
        refit_cost: 0; // Refitting is free
        /* callback_flags are not set, no need to manually enable callbacks */
        running_cost_base: RUNNING_COST_ROADVEH;
        power: 120 hp;
        weight: 9.5 ton;
        /* TE and air drag coefficient is left at default */
        cargo_capacity: 20; // Changed by callback
        sound_effect: SOUND_DEPARTURE_OLD_RV_1;
        /* Visual effect is left at default (no effect) */
    }

    /* Define graphics for various cargo types, as well as the capacity callback */
    graphics {
        cargo_capacity: flatbed_truck_1_capacity_switch;
        PAPR: flatbed_truck_1_paper;
        STEL: flatbed_truck_1_steel;
        COPR: flatbed_truck_1_copper;
        WOOD: flatbed_truck_1_wood;
        default: flatbed_truck_1_goods; // Default to Goods.
    }
}

/* Define the road vehicle */
item(FEAT_ROADVEHS, flatbed_truck_4) {
    property {
        /* Properties common to all vehicle types */
        name: string(STR_NAME_FLATBED_TRUCK_4);
        climates_available: bitmask(CLIMATE_TEMPERATE, CLIMATE_ARCTIC, CLIMATE_TROPICAL);
        road_type: RED_TEST;
        introduction_date: date(1926,01,01);
        model_life: 65;
        /* retire_early not set, use default retirement behaviour */
        vehicle_life: 15;
        reliability_decay: 20;
        refittable_cargo_classes: bitmask(CC_PIECE_GOODS, CC_EXPRESS);
        non_refittable_cargo_classes: bitmask(CC_PASSENGERS, CC_REFRIGERATED);
        /* Livestock, wool and fibre crops can be transported acc. cargo classes
         * But we don't want that, so disable refitting for them.
         * Scrap metal, petrol and refined products cannot be transported acc. cargo classes
         * We do want to transport those, so enable refitting for them. */
        cargo_allow_refit: [LVST, WOOL, SCRP, FICR, PETR, RFPR];
        cargo_disallow_refit: []; // we allow other cargoes, if class matches
        loading_speed: 5;
        cost_factor: 108;
        running_cost_factor: 90;
        /* cargo_age_period is left at default */

        /* RV-specific properties */
        sprite_id: SPRITE_ID_NEW_ROADVEH;
        speed: 48 km/h;
        misc_flags: bitmask(ROADVEH_FLAG_2CC);
        refit_cost: 0; // Refitting is free
        /* callback_flags are not set, no need to manually enable callbacks */
        running_cost_base: RUNNING_COST_ROADVEH;
        power: 120 hp;
        weight: 9.5 ton;
        /* TE and air drag coefficient is left at default */
        cargo_capacity: 20; // Changed by callback
        sound_effect: SOUND_BUS_START_PULL_AWAY;
        /* Visual effect is left at default (no effect) */
    }

    /* Define graphics for various cargo types, as well as the capacity callback */
    graphics {
        cargo_capacity: flatbed_truck_1_capacity_switch;
        PAPR: flatbed_truck_1_paper;
        STEL: flatbed_truck_1_steel;
        COPR: flatbed_truck_1_copper;
        WOOD: flatbed_truck_1_wood;
        default: flatbed_truck_1_goods; // Default to Goods.
    }
}
