src/articulated_vehicles.cpp
branchNewGRF_ports
changeset 6720 35756db7e577
parent 6719 4cc327ad39d5
child 6800 6c09e1e86fcb
equal deleted inserted replaced
6719:4cc327ad39d5 6720:35756db7e577
     2 
     2 
     3 /** @file articulated_vehicles.cpp */
     3 /** @file articulated_vehicles.cpp */
     4 
     4 
     5 #include "stdafx.h"
     5 #include "stdafx.h"
     6 #include "openttd.h"
     6 #include "openttd.h"
     7 #include "debug.h"
       
     8 #include "functions.h"
     7 #include "functions.h"
     9 #include "command.h"
     8 #include "command.h"
    10 #include "vehicle.h"
     9 #include "vehicle.h"
    11 #include "articulated_vehicles.h"
    10 #include "articulated_vehicles.h"
    12 #include "engine.h"
    11 #include "engine.h"
    13 #include "train.h"
    12 #include "train.h"
       
    13 #include "roadveh.h"
    14 #include "newgrf_callbacks.h"
    14 #include "newgrf_callbacks.h"
    15 #include "newgrf_engine.h"
    15 #include "newgrf_engine.h"
       
    16 
    16 
    17 
    17 uint CountArticulatedParts(EngineID engine_type)
    18 uint CountArticulatedParts(EngineID engine_type)
    18 {
    19 {
    19 	if (!HASBIT(EngInfo(engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return 0;
    20 	if (!HASBIT(EngInfo(engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return 0;
    20 
    21 
    21 	uint i;
    22 	uint i;
    22 	for (i = 1; i < 10; i++) {
    23 	for (i = 1; i < 10; i++) {
    23 		uint16 callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, engine_type, NULL);
    24 		uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine_type, NULL);
    24 		if (callback == CALLBACK_FAILED || callback == 0xFF) break;
    25 		if (callback == CALLBACK_FAILED || callback == 0xFF) break;
    25 	}
    26 	}
    26 
    27 
    27 	return i - 1;
    28 	return i - 1;
    28 }
    29 }
    29 
    30 
    30 void AddArticulatedParts(Vehicle **vl)
    31 void AddArticulatedParts(Vehicle **vl, VehicleType type)
    31 {
    32 {
    32 	const Vehicle *v = vl[0];
    33 	const Vehicle *v = vl[0];
    33 	Vehicle *u = vl[0];
    34 	Vehicle *u = vl[0];
    34 
    35 
    35 	if (!HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return;
    36 	if (!HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return;
    36 
    37 
    37 	for (uint i = 1; i < 10; i++) {
    38 	for (uint i = 1; i < 10; i++) {
    38 		uint16 callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, v->engine_type, v);
    39 		uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, v->engine_type, v);
    39 		if (callback == CALLBACK_FAILED || callback == 0xFF) return;
    40 		if (callback == CALLBACK_FAILED || callback == 0xFF) return;
    40 
    41 
    41 		/* Attempt to use pre-allocated vehicles until they run out. This can happen
    42 		/* 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 		 * if the callback returns different values depending on the cargo type. */
    43 		u->next = vl[i];
    44 		u->next = vl[i];
    44 		if (u->next == NULL) u->next = AllocateVehicle();
    45 		if (u->next == NULL) u->next = AllocateVehicle();
    45 		if (u->next == NULL) return;
    46 		if (u->next == NULL) return;
    46 
    47 
    47 		u = u->next;
    48 		u = u->next;
    48 
    49 
    49 		EngineID engine_type = GB(callback, 0, 7);
    50 		EngineID engine_type = GetFirstEngineOfType(type) + GB(callback, 0, 7);
    50 		bool flip_image = HASBIT(callback, 7);
    51 		bool flip_image = HASBIT(callback, 7);
    51 		const RailVehicleInfo *rvi_artic = RailVehInfo(engine_type);
       
    52 
    52 
    53 		/* get common values from first engine */
    53 		/* get common values from first engine */
    54 		u->direction = v->direction;
    54 		u->direction = v->direction;
    55 		u->owner = v->owner;
    55 		u->owner = v->owner;
    56 		u->tile = v->tile;
    56 		u->tile = v->tile;
    57 		u->x_pos = v->x_pos;
    57 		u->x_pos = v->x_pos;
    58 		u->y_pos = v->y_pos;
    58 		u->y_pos = v->y_pos;
    59 		u->z_pos = v->z_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;
    60 		u->build_year = v->build_year;
    63 		u->vehstatus = v->vehstatus & ~VS_STOPPED;
    61 		u->vehstatus = v->vehstatus & ~VS_STOPPED;
    64 		u->u.rail.first_engine = v->engine_type;
       
    65 
    62 
    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;
    63 		u->cargo_subtype = 0;
    71 		u->cargo_cap = rvi_artic->capacity;
       
    72 		u->max_speed = 0;
    64 		u->max_speed = 0;
    73 		u->max_age = 0;
    65 		u->max_age = 0;
    74 		u->engine_type = engine_type;
    66 		u->engine_type = engine_type;
    75 		u->value = 0;
    67 		u->value = 0;
    76 		u = new (u) Train();
       
    77 		u->subtype = 0;
    68 		u->subtype = 0;
    78 		SetArticulatedPart(u);
       
    79 		u->cur_image = 0xAC2;
    69 		u->cur_image = 0xAC2;
    80 		u->random_bits = VehicleRandomBits();
    70 		u->random_bits = VehicleRandomBits();
       
    71 
       
    72 		switch (type) {
       
    73 			default: NOT_REACHED();
       
    74 
       
    75 			case VEH_TRAIN: {
       
    76 				const RailVehicleInfo *rvi_artic = RailVehInfo(engine_type);
       
    77 
       
    78 				u = new (u) Train();
       
    79 				u->u.rail.track = v->u.rail.track;
       
    80 				u->u.rail.railtype = v->u.rail.railtype;
       
    81 				u->u.rail.first_engine = v->engine_type;
       
    82 
       
    83 				u->spritenum = rvi_artic->image_index;
       
    84 				u->cargo_type = rvi_artic->cargo_type;
       
    85 				u->cargo_cap = rvi_artic->capacity;
       
    86 
       
    87 				SetArticulatedPart(u);
       
    88 			} break;
       
    89 
       
    90 			case VEH_ROAD: {
       
    91 				const RoadVehicleInfo *rvi_artic = RoadVehInfo(engine_type);
       
    92 
       
    93 				u = new (u) RoadVehicle();
       
    94 				u->u.road.first_engine = v->engine_type;
       
    95 				u->u.road.cached_veh_length = GetRoadVehLength(u);
       
    96 				u->u.road.state = RVSB_IN_DEPOT;
       
    97 
       
    98 				u->u.road.roadtype = v->u.road.roadtype;
       
    99 				u->u.road.compatible_roadtypes = v->u.road.compatible_roadtypes;
       
   100 
       
   101 				u->spritenum = rvi_artic->image_index;
       
   102 				u->cargo_type = rvi_artic->cargo_type;
       
   103 				u->cargo_cap = rvi_artic->capacity;
       
   104 
       
   105 				SetRoadVehArticPart(u);
       
   106 			} break;
       
   107 		}
       
   108 
       
   109 		if (flip_image) u->spritenum++;
    81 
   110 
    82 		VehiclePositionChanged(u);
   111 		VehiclePositionChanged(u);
    83 	}
   112 	}
    84 }
   113 }