src/articulated_vehicles.cpp
changeset 6772 8672ba1bebc9
parent 6771 b58be16fb268
child 6775 82fc9e993a09
equal deleted inserted replaced
6771:b58be16fb268 6772:8672ba1bebc9
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file train_cmd.cpp */
       
     4 
       
     5 #include "stdafx.h"
       
     6 #include "openttd.h"
       
     7 #include "debug.h"
       
     8 #include "functions.h"
       
     9 #include "command.h"
       
    10 #include "vehicle.h"
       
    11 #include "articulated_vehicles.h"
       
    12 #include "engine.h"
       
    13 #include "train.h"
       
    14 #include "newgrf_callbacks.h"
       
    15 #include "newgrf_engine.h"
       
    16 
       
    17 uint CountArticulatedParts(EngineID engine_type)
       
    18 {
       
    19 	if (!HASBIT(EngInfo(engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return 0;
       
    20 
       
    21 	uint i;
       
    22 	for (i = 1; i < 10; i++) {
       
    23 		uint16 callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, engine_type, NULL);
       
    24 		if (callback == CALLBACK_FAILED || callback == 0xFF) break;
       
    25 	}
       
    26 
       
    27 	return i - 1;
       
    28 }
       
    29 
       
    30 void AddArticulatedParts(Vehicle **vl)
       
    31 {
       
    32 	const Vehicle *v = vl[0];
       
    33 	Vehicle *u = vl[0];
       
    34 
       
    35 	if (!HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return;
       
    36 
       
    37 	for (uint i = 1; i < 10; i++) {
       
    38 		uint16 callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, v->engine_type, v);
       
    39 		if (callback == CALLBACK_FAILED || callback == 0xFF) return;
       
    40 
       
    41 		/* Attempt to use pre-allocated vehicles until they run out. This can happen
       
    42 		 * if the callback returns different values depending on the cargo type. */
       
    43 		u->next = vl[i];
       
    44 		if (u->next == NULL) u->next = AllocateVehicle();
       
    45 		if (u->next == NULL) return;
       
    46 
       
    47 		u = u->next;
       
    48 
       
    49 		EngineID engine_type = GB(callback, 0, 7);
       
    50 		bool flip_image = HASBIT(callback, 7);
       
    51 		const RailVehicleInfo *rvi_artic = RailVehInfo(engine_type);
       
    52 
       
    53 		/* get common values from first engine */
       
    54 		u->direction = v->direction;
       
    55 		u->owner = v->owner;
       
    56 		u->tile = v->tile;
       
    57 		u->x_pos = v->x_pos;
       
    58 		u->y_pos = v->y_pos;
       
    59 		u->z_pos = v->z_pos;
       
    60 		u->u.rail.track = v->u.rail.track;
       
    61 		u->u.rail.railtype = v->u.rail.railtype;
       
    62 		u->build_year = v->build_year;
       
    63 		u->vehstatus = v->vehstatus & ~VS_STOPPED;
       
    64 		u->u.rail.first_engine = v->engine_type;
       
    65 
       
    66 		/* get more settings from rail vehicle info */
       
    67 		u->spritenum = rvi_artic->image_index;
       
    68 		if (flip_image) u->spritenum++;
       
    69 		u->cargo_type = rvi_artic->cargo_type;
       
    70 		u->cargo_subtype = 0;
       
    71 		u->cargo_cap = rvi_artic->capacity;
       
    72 		u->max_speed = 0;
       
    73 		u->max_age = 0;
       
    74 		u->engine_type = engine_type;
       
    75 		u->value = 0;
       
    76 		u = new (u) Train();
       
    77 		u->subtype = 0;
       
    78 		SetArticulatedPart(u);
       
    79 		u->cur_image = 0xAC2;
       
    80 		u->random_bits = VehicleRandomBits();
       
    81 
       
    82 		VehiclePositionChanged(u);
       
    83 	}
       
    84 }