<?php
/*
 * 2CC Tram Set
 * Copyright (C) 2014-2015 Jasper Vries
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

function nml_roadvehicle($vehicleinfo, $vehid, $numvehid) {
	//start nml components	
	$nml = "
////////////////////////
//Pre-purchase sprites//
////////////////////////

spriteset(spriteset_{$vehid}_purchase, \"gfx/{$vehicleinfo['graphic']['default']['purchase']['sprite_file']}\") {
	template_roadvehicle_purchase({$vehicleinfo['graphic']['default']['purchase']['x_offset']}, {$vehicleinfo['graphic']['default']['purchase']['y_offset']})
}

spriteset(spriteset_{$vehid}_gui, \"gfx/{$vehicleinfo['graphic']['default']['gui']['sprite_file']}\") {
	template_roadvehicle_gui({$vehicleinfo['graphic']['default']['gui']['x_offset']}, {$vehicleinfo['graphic']['default']['gui']['y_offset']})
}

switch (FEAT_ROADVEHS, SELF, switch_{$vehid}_purchase, (extra_callback_info1 & 0xFF)) {
	0x21: spriteset_{$vehid}_gui; //drawn in exclusive preview and advertisement news
    spriteset_{$vehid}_purchase; //drawn in purchase list
}

///////////////////
//Vehicle sprites//
///////////////////
";

	$spriteset_template = '
spriteset(spriteset_ID_LIV_SEC, "gfx/FILE") {
	TEMPLATE(X_OFF, Y_OFF)
}
';

	$livery = 'default';
	$switch = 'switch (FEAT_ROADVEHS, SELF, switch_' .$vehid. '_' .$livery. '_position, position_in_consist) {' . PHP_EOL;
	$num_parts = count($vehicleinfo['graphic'][$livery]) - 3;
	ksort($vehicleinfo['graphic'][$livery]);
	foreach($vehicleinfo['graphic'][$livery] as $section => $values) {
		if ($section == 'purchase') {
			$template = 'template_roadvehicle_depot';
		}
		elseif ($section == 'gui') {
			$template = 'template_roadvehicle_gui';
		}
		else {
			$template = 'template_roadvehicle_'.$values['length'];
		}
		$search = array('ID', 'LIV', 'SEC', 'FILE', 'TEMPLATE', 'X_OFF', 'Y_OFF');
		$replace = array($vehid, $livery, $section, $vehicleinfo['graphic'][$livery][$section]['sprite_file'], $template, $values['x_offset'], $values['y_offset']);
		$nml .= str_replace($search, $replace, $spriteset_template);
		if (($section != 'purchase') && ($section != 'gui')) {
			$switch .= '    ';
			if ($section != 'part'.($vehicleinfo['property']['vehicleparts'])) {
				$switch .= (substr($section, 4) - 1) . ': ';
			}
			$switch .= 'spriteset_' .$vehid. '_' .$livery. '_' .$section. ';' . PHP_EOL;
		}
	}
	$switch .= '}' . PHP_EOL;
	$nml .= $switch;
	
	$nml .= "
switch (FEAT_ROADVEHS, SELF, switch_{$vehid}_{$livery}_graphics_window, (extra_callback_info1 & 0xFF)) {
	0x10: spriteset_{$vehid}_{$livery}_purchase; //drawn in depot
	spriteset_{$vehid}_{$livery}_gui; //drawn in vehicle details (0x11) and in vehicle list (0x12)
}
switch (FEAT_ROADVEHS, SELF, switch_{$vehid}_{$livery}_graphics_gui, position_in_consist) {
	0: switch_{$vehid}_{$livery}_graphics_window; //first in consist
	spriteset_blank; //other parts blank
}
switch (FEAT_ROADVEHS, SELF, switch_{$vehid}_graphics, (extra_callback_info1 & 0xFF)) {
	0x10..0x12: switch_{$vehid}_{$livery}_graphics_gui; //drawn in windows
    switch_{$vehid}_{$livery}_position; //drawn on map
}
";

	if ($vehicleinfo['property']['vehicleparts'] > 1) {
		$nml .= "
/////////////////////
//vehicle callbacks//
/////////////////////
";	
		//articulated vehicle callback
		$nml .= 'switch (FEAT_ROADVEHS, SELF, switch_' .$vehid. '_articulated, extra_callback_info1 ) {' . PHP_EOL;
		$nml .= "\t" . '0..' . ($vehicleinfo['property']['vehicleparts'] - 1) . ': return item_roadvehicle_' .$vehid. ';' . PHP_EOL;
		$nml .= "\t" . 'return CB_FAILED;' . PHP_EOL;
		$nml .= '}' . PHP_EOL;
		
		//length callback
		$nml .= 'switch (FEAT_ROADVEHS, SELF, switch_' .$vehid. '_length, position_in_consist ) {' . PHP_EOL;
		for ($i = 0; $i < $vehicleinfo['property']['vehicleparts']; $i++) {
			$nml .= "\t";
			if ($i < $vehicleinfo['property']['vehicleparts'] - 1) {
				$nml .= $i . ': ';
			}
			$nml .= 'return ' .$vehicleinfo['graphic'][$livery]['part'.($i+1)]['length']. ';' . PHP_EOL;
		}
		$nml .= '}' . PHP_EOL;
		
		//capacity callback
		$nml .= 'switch (FEAT_ROADVEHS, SELF, switch_' .$vehid. '_capacity, position_in_consist ) {' . PHP_EOL;
		for ($i = 0; $i < $vehicleinfo['property']['vehicleparts']; $i++) {
			$nml .= "\t";
			if ($i < $vehicleinfo['property']['vehicleparts'] - 1) {
				$nml .= $i . ': ';
			}
			$nml .= 'return ' .$vehicleinfo['property']['capacity'][$i]. ';' . PHP_EOL;
		}
		$nml .= '}' . PHP_EOL;
		
	}
	
	/*
	PROPERTY BLOCK
	*/
	
	//construct graphics rules for articulated vehicle, inserted in vehicle properties
	if ($vehicleinfo['property']['vehicleparts'] > 1) {
		$articulated  = "articulated_part:               switch_{$vehid}_articulated;" . PHP_EOL;
		$articulated .= "        length:                         switch_{$vehid}_length;" . PHP_EOL;
		$articulated .= "        cargo_capacity:                 switch_{$vehid}_capacity;" . PHP_EOL;
		$articulated .= "        purchase_cargo_capacity:        return ".$vehicleinfo['property']['purchase_cargo_capacity'].";";
		$length = '';
	}
    else {
       	$length = 'length:                         ' . min($vehicleinfo['graphic']['default']['part1']['length'], 8) . ';';
		$articulated = '';
	}
	//vehicle properties
	$nml .= "	
//////////////////////
//vehicle properties//
//////////////////////

item (FEAT_ROADVEHS, item_roadvehicle_$vehid, $numvehid) {
    property {
        name:                           string({$vehicleinfo['str_name']});
        introduction_date:              date({$vehicleinfo['property']['introduction_date']});
        model_life:                     {$vehicleinfo['property']['model_life']};
        retire_early:                   {$vehicleinfo['property']['retire_early']};
        vehicle_life:                   {$vehicleinfo['property']['vehicle_life']};
        loading_speed:                  {$vehicleinfo['property']['loading_speed']}; //should be 5*number of doors
        cost_factor:                    {$vehicleinfo['property']['cost_factor']};
        running_cost_factor:            {$vehicleinfo['property']['running_cost_factor']};
        speed:                          {$vehicleinfo['property']['speed']} km/h;
        power:                          {$vehicleinfo['property']['power']} kW;
        weight:                         {$vehicleinfo['property']['weight']};
        cargo_capacity:                 {$vehicleinfo['property']['cargo_capacity']};
		{$length}
        tractive_effort_coefficient:    {$vehicleinfo['property']['tractive_effort_coefficient']}; //percentage of powered axles
        air_drag_coefficient:           {$vehicleinfo['property']['air_drag_coefficient']}; //0=default
        //sound_effect:                 ; //not implemented
        //visual_effect:                visual_effect(VISUAL_EFFECT_DEFAULT, 0); //not implemented
        
        reliability_decay:              {$vehicleinfo['property']['reliability_decay']};
        climates_available:             ALL_CLIMATES;
        refittable_cargo_classes:       bitmask(CC_PASSENGERS);
        sprite_id:                      SPRITE_ID_NEW_ROADVEH;
        misc_flags:                     bitmask(ROADVEH_FLAG_TRAM, ROADVEH_FLAG_2CC, ROADVEH_FLAG_AUTOREFIT);
        refit_cost:                     0;
        running_cost_base:              RUNNING_COST_ROADVEH;
    }

    graphics {
        purchase:                       switch_{$vehid}_purchase;
        //additional_text:              //return string(STR_DESC_);
        {$articulated}
        switch_{$vehid}_graphics;
    }
}

//cargo refit options
if (param_refit == 1) {
    item (FEAT_ROADVEHS, item_roadvehicle_{$vehid}) {
        property {
            refittable_cargo_classes:	bitmask(CC_PASSENGERS, CC_MAIL, CC_EXPRESS);
        }
    }
}
else if (param_refit == 2) {
    item (FEAT_ROADVEHS, item_roadvehicle_{$vehid}) {
        property {
            refittable_cargo_classes:	ALL_NORMAL_CARGO_CLASSES;
        }
    }
}";
	
	//return NML
	return $nml;
}
?>