src/train_cmd.cpp
changeset 7268 9c77f6baef3b
parent 7267 f49300df3842
child 7280 358095cd3c51
equal deleted inserted replaced
7267:f49300df3842 7268:9c77f6baef3b
    13 #include "table/strings.h"
    13 #include "table/strings.h"
    14 #include "map.h"
    14 #include "map.h"
    15 #include "tile.h"
    15 #include "tile.h"
    16 #include "tunnel_map.h"
    16 #include "tunnel_map.h"
    17 #include "vehicle.h"
    17 #include "vehicle.h"
       
    18 #include "articulated_vehicles.h"
    18 #include "command.h"
    19 #include "command.h"
    19 #include "pathfind.h"
    20 #include "pathfind.h"
    20 #include "npf.h"
    21 #include "npf.h"
    21 #include "station.h"
    22 #include "station.h"
    22 #include "table/train_cmd.h"
    23 #include "table/train_cmd.h"
   497 				((6 + _engine_sprite_add[img + 1]) & _engine_sprite_and[img + 1]) +
   498 				((6 + _engine_sprite_add[img + 1]) & _engine_sprite_and[img + 1]) +
   498 				_engine_sprite_base[img + 1];
   499 				_engine_sprite_base[img + 1];
   499 		}
   500 		}
   500 	}
   501 	}
   501 	DrawSprite(image, pal, x, y);
   502 	DrawSprite(image, pal, x, y);
   502 }
       
   503 
       
   504 uint CountArticulatedParts(EngineID engine_type)
       
   505 {
       
   506 	if (!HASBIT(EngInfo(engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return 0;
       
   507 
       
   508 	uint i;
       
   509 	for (i = 1; i < 10; i++) {
       
   510 		uint16 callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, engine_type, NULL);
       
   511 		if (callback == CALLBACK_FAILED || callback == 0xFF) break;
       
   512 	}
       
   513 
       
   514 	return i - 1;
       
   515 }
       
   516 
       
   517 static void AddArticulatedParts(Vehicle **vl)
       
   518 {
       
   519 	const Vehicle *v = vl[0];
       
   520 	Vehicle *u = vl[0];
       
   521 
       
   522 	if (!HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return;
       
   523 
       
   524 	for (uint i = 1; i < 10; i++) {
       
   525 		uint16 callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, v->engine_type, v);
       
   526 		if (callback == CALLBACK_FAILED || callback == 0xFF) return;
       
   527 
       
   528 		/* Attempt to use pre-allocated vehicles until they run out. This can happen
       
   529 		 * if the callback returns different values depending on the cargo type. */
       
   530 		u->next = vl[i];
       
   531 		if (u->next == NULL) u->next = AllocateVehicle();
       
   532 		if (u->next == NULL) return;
       
   533 
       
   534 		u = u->next;
       
   535 
       
   536 		EngineID engine_type = GB(callback, 0, 7);
       
   537 		bool flip_image = HASBIT(callback, 7);
       
   538 		const RailVehicleInfo *rvi_artic = RailVehInfo(engine_type);
       
   539 
       
   540 		/* get common values from first engine */
       
   541 		u->direction = v->direction;
       
   542 		u->owner = v->owner;
       
   543 		u->tile = v->tile;
       
   544 		u->x_pos = v->x_pos;
       
   545 		u->y_pos = v->y_pos;
       
   546 		u->z_pos = v->z_pos;
       
   547 		u->u.rail.track = v->u.rail.track;
       
   548 		u->u.rail.railtype = v->u.rail.railtype;
       
   549 		u->build_year = v->build_year;
       
   550 		u->vehstatus = v->vehstatus & ~VS_STOPPED;
       
   551 		u->u.rail.first_engine = v->engine_type;
       
   552 
       
   553 		/* get more settings from rail vehicle info */
       
   554 		u->spritenum = rvi_artic->image_index;
       
   555 		if (flip_image) u->spritenum++;
       
   556 		u->cargo_type = rvi_artic->cargo_type;
       
   557 		u->cargo_subtype = 0;
       
   558 		u->cargo_cap = rvi_artic->capacity;
       
   559 		u->max_speed = 0;
       
   560 		u->max_age = 0;
       
   561 		u->engine_type = engine_type;
       
   562 		u->value = 0;
       
   563 		u = new (u) Train();
       
   564 		u->subtype = 0;
       
   565 		SetArticulatedPart(u);
       
   566 		u->cur_image = 0xAC2;
       
   567 		u->random_bits = VehicleRandomBits();
       
   568 
       
   569 		VehiclePositionChanged(u);
       
   570 	}
       
   571 }
   503 }
   572 
   504 
   573 static int32 CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
   505 static int32 CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
   574 {
   506 {
   575 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
   507 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);