train_cmd.c
changeset 2602 1a6561c9a408
parent 2599 ad75080afea4
child 2607 9915a4829de8
equal deleted inserted replaced
2601:d1a193af01e6 2602:1a6561c9a408
    44 
    44 
    45 	for (u = v; u != NULL; u = u->next) {
    45 	for (u = v; u != NULL; u = u->next) {
    46 		const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
    46 		const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
    47 		uint16 vweight = 0;
    47 		uint16 vweight = 0;
    48 
    48 
    49 		// vehicle weight is the sum of the weight of the vehicle and the weight of its cargo
       
    50 		vweight += rvi->weight;
       
    51 		vweight += (_cargoc.weights[u->cargo_type] * u->cargo_count) / 16;
    49 		vweight += (_cargoc.weights[u->cargo_type] * u->cargo_count) / 16;
    52 		// powered wagons have extra weight added
    50 
    53 		if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON))
    51 		// Vehicle weight is not added for articulated parts.
    54 			vweight += RailVehInfo(v->engine_type)->pow_wag_weight;
    52 		if (u->subtype != TS_Artic_Part) {
       
    53 			// vehicle weight is the sum of the weight of the vehicle and the weight of its cargo
       
    54 			vweight += rvi->weight;
       
    55 
       
    56 			// powered wagons have extra weight added
       
    57 			if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON))
       
    58 				vweight += RailVehInfo(v->engine_type)->pow_wag_weight;
       
    59 		}
    55 
    60 
    56 		// consist weight is the sum of the weight of all vehicles in the consist
    61 		// consist weight is the sum of the weight of all vehicles in the consist
    57 		weight += vweight;
    62 		weight += vweight;
    58 
    63 
    59 		// store vehicle weight in cache
    64 		// store vehicle weight in cache
    90 		uint16 veh_len;
    95 		uint16 veh_len;
    91 
    96 
    92 		// update the 'first engine'
    97 		// update the 'first engine'
    93 		u->u.rail.first_engine = (v == u) ? INVALID_VEHICLE : first_engine;
    98 		u->u.rail.first_engine = (v == u) ? INVALID_VEHICLE : first_engine;
    94 
    99 
    95 		// power is the sum of the powers of all engines and powered wagons in the consist
       
    96 		power += rvi_u->power;
       
    97 
       
    98 		if (rvi_u->visual_effect != 0) {
   100 		if (rvi_u->visual_effect != 0) {
    99 			u->u.rail.cached_vis_effect = rvi_u->visual_effect;
   101 			u->u.rail.cached_vis_effect = rvi_u->visual_effect;
   100 		} else {
   102 		} else {
   101 			if (rvi_u->flags & RVI_WAGON) {
   103 			if (rvi_u->flags & RVI_WAGON || u->subtype == TS_Artic_Part) {
   102 				// Wagons have no effect by default
   104 				// Wagons and articulated parts have no effect by default
   103 				u->u.rail.cached_vis_effect = 0x40;
   105 				u->u.rail.cached_vis_effect = 0x40;
   104 			} else if (rvi_u->engclass == 0) {
   106 			} else if (rvi_u->engclass == 0) {
   105 				// Steam is offset by -4 units
   107 				// Steam is offset by -4 units
   106 				u->u.rail.cached_vis_effect = 4;
   108 				u->u.rail.cached_vis_effect = 4;
   107 			} else {
   109 			} else {
   108 				// Diesel fumes and sparks come from the centre
   110 				// Diesel fumes and sparks come from the centre
   109 				u->u.rail.cached_vis_effect = 8;
   111 				u->u.rail.cached_vis_effect = 8;
   110 			}
   112 			}
   111 		}
   113 		}
   112 
   114 
   113 		// check if its a powered wagon
   115 		if (u->subtype != TS_Artic_Part) {
   114 		CLRBIT(u->u.rail.flags, VRF_POWEREDWAGON);
   116 			// power is the sum of the powers of all engines and powered wagons in the consist
   115 		if ((rvi_v->pow_wag_power != 0) && (rvi_u->flags & RVI_WAGON) && UsesWagonOverride(u)) {
   117 			power += rvi_u->power;
   116 			if (HASBIT(rvi_u->callbackmask, CBM_WAGON_POWER)) {
   118 
   117 				uint16 callback = GetCallBackResult(CBID_WAGON_POWER,  u->engine_type, u);
   119 			// check if its a powered wagon
   118 
   120 			CLRBIT(u->u.rail.flags, VRF_POWEREDWAGON);
   119 				if (callback != CALLBACK_FAILED)
   121 			if ((rvi_v->pow_wag_power != 0) && (rvi_u->flags & RVI_WAGON) && UsesWagonOverride(u)) {
   120 					u->u.rail.cached_vis_effect = callback;
   122 				if (HASBIT(rvi_u->callbackmask, CBM_WAGON_POWER)) {
   121 			}
   123 					uint16 callback = GetCallBackResult(CBID_WAGON_POWER,  u->engine_type, u);
   122 
   124 
   123 			if (u->u.rail.cached_vis_effect < 0x40) {
   125 					if (callback != CALLBACK_FAILED)
   124 				/* wagon is powered */
   126 						u->u.rail.cached_vis_effect = callback;
   125 				SETBIT(u->u.rail.flags, VRF_POWEREDWAGON); // cache 'powered' status
   127 				}
   126 				power += rvi_v->pow_wag_power;
   128 
   127 			}
   129 				if (u->u.rail.cached_vis_effect < 0x40) {
   128 		}
   130 					/* wagon is powered */
   129 
   131 					SETBIT(u->u.rail.flags, VRF_POWEREDWAGON); // cache 'powered' status
   130 		// max speed is the minimum of the speed limits of all vehicles in the consist
   132 					power += rvi_v->pow_wag_power;
   131 		if (!(rvi_u->flags & RVI_WAGON) || _patches.wagon_speed_limits)
   133 				}
   132 			if (rvi_u->max_speed != 0 && !UsesWagonOverride(u))
   134 			}
   133 				max_speed = min(rvi_u->max_speed, max_speed);
   135 
       
   136 			// max speed is the minimum of the speed limits of all vehicles in the consist
       
   137 			if (!(rvi_u->flags & RVI_WAGON) || _patches.wagon_speed_limits)
       
   138 				if (rvi_u->max_speed != 0 && !UsesWagonOverride(u))
       
   139 					max_speed = min(rvi_u->max_speed, max_speed);
       
   140 		}
   134 
   141 
   135 		// check the vehicle length (callback)
   142 		// check the vehicle length (callback)
   136 		veh_len = CALLBACK_FAILED;
   143 		veh_len = CALLBACK_FAILED;
   137 		if (HASBIT(rvi_u->callbackmask, CBM_VEH_LENGTH))
   144 		if (HASBIT(rvi_u->callbackmask, CBM_VEH_LENGTH))
   138 			veh_len = GetCallBackResult(CBID_VEH_LENGTH,  u->engine_type, u);
   145 			veh_len = GetCallBackResult(CBID_VEH_LENGTH,  u->engine_type, u);
   416 		}
   423 		}
   417 	}
   424 	}
   418 	DrawSprite(image | image_ormod, x, y);
   425 	DrawSprite(image | image_ormod, x, y);
   419 }
   426 }
   420 
   427 
       
   428 static uint CountArticulatedParts(const RailVehicleInfo *rvi, EngineID engine_type)
       
   429 {
       
   430 	uint16 callback;
       
   431 	uint i;
       
   432 
       
   433 	if (!HASBIT(rvi->callbackmask, CBM_ARTIC_ENGINE))
       
   434 		return 0;
       
   435 
       
   436 	for (i = 1; i < 10; i++) {
       
   437 		callback = GetCallBackResult(CBID_ARTIC_ENGINE + (i << 8), engine_type, NULL);
       
   438 		if (callback == CALLBACK_FAILED || callback == 0xFF)
       
   439 			break;
       
   440 	}
       
   441 
       
   442 	return i;
       
   443 }
       
   444 
       
   445 static void AddArticulatedParts(const RailVehicleInfo *rvi, Vehicle **vl)
       
   446 {
       
   447 	const RailVehicleInfo *rvi_artic;
       
   448 	EngineID engine_type;
       
   449 	Vehicle *v = vl[0];
       
   450 	Vehicle *u = v;
       
   451 	uint16 callback;
       
   452 	bool flip_image;
       
   453 	uint i;
       
   454 
       
   455 	if (!HASBIT(rvi->callbackmask, CBM_ARTIC_ENGINE))
       
   456 		return;
       
   457 
       
   458 	for (i = 1; i < 10; i++) {
       
   459 		callback = GetCallBackResult(CBID_ARTIC_ENGINE + (i << 8), v->engine_type, NULL);
       
   460 		if (callback == CALLBACK_FAILED || callback == 0xFF)
       
   461 			return;
       
   462 
       
   463 		u->next = vl[i];
       
   464 		u = u->next;
       
   465 
       
   466 		engine_type = GB(callback, 0, 6);
       
   467 		flip_image = HASBIT(callback, 7);
       
   468 		rvi_artic = RailVehInfo(engine_type);
       
   469 
       
   470 		// get common values from first engine
       
   471 		u->direction = v->direction;
       
   472 		u->owner = v->owner;
       
   473 		u->tile = v->tile;
       
   474 		u->x_pos = v->x_pos;
       
   475 		u->y_pos = v->y_pos;
       
   476 		u->z_pos = v->z_pos;
       
   477 		u->z_height = v->z_height;
       
   478 		u->u.rail.track = v->u.rail.track;
       
   479 		u->u.rail.railtype = v->u.rail.railtype;
       
   480 		u->build_year = v->build_year;
       
   481 		u->vehstatus = v->vehstatus & ~VS_STOPPED;
       
   482 		u->u.rail.first_engine = v->engine_type;
       
   483 
       
   484 		// get more settings from rail vehicle info
       
   485 		u->spritenum = rvi_artic->image_index;
       
   486 		if (flip_image) u->spritenum++;
       
   487 		u->cargo_type = rvi_artic->cargo_type;
       
   488 		u->cargo_cap = rvi_artic->capacity;
       
   489 		u->max_speed = 0;
       
   490 		u->max_age = 0;
       
   491 		u->engine_type = engine_type;
       
   492 		u->value = 0;
       
   493 		u->type = VEH_Train;
       
   494 		u->subtype = TS_Artic_Part;
       
   495 		u->cur_image = 0xAC2;
       
   496 
       
   497 		VehiclePositionChanged(u);
       
   498 	}
       
   499 }
   421 
   500 
   422 static int32 CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
   501 static int32 CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
   423 {
   502 {
   424 	int32 value;
   503 	int32 value;
   425 	Vehicle *v;
   504 	Vehicle *v;
   426 	const RailVehicleInfo *rvi;
   505 	const RailVehicleInfo *rvi;
   427 	const Engine *e;
   506 	const Engine *e;
   428 	int x,y;
   507 	int x,y;
       
   508 	uint num_vehicles;
   429 
   509 
   430 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
   510 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
   431 
   511 
   432 	rvi = RailVehInfo(engine);
   512 	rvi = RailVehInfo(engine);
   433 	value = (rvi->base_cost * _price.build_railwagon) >> 8;
   513 	value = (rvi->base_cost * _price.build_railwagon) >> 8;
   434 
   514 
       
   515 	num_vehicles = 1 + CountArticulatedParts(rvi, engine);
       
   516 
   435 	if (!(flags & DC_QUERY_COST)) {
   517 	if (!(flags & DC_QUERY_COST)) {
   436 		_error_message = STR_00E1_TOO_MANY_VEHICLES_IN_GAME;
   518 		Vehicle *vl[num_vehicles];
   437 
   519 
   438 		v = AllocateVehicle();
   520 		if (!AllocateVehicles(vl, num_vehicles))
   439 		if (v == NULL)
   521 			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
   440 			return CMD_ERROR;
       
   441 
   522 
   442 		if (flags & DC_EXEC) {
   523 		if (flags & DC_EXEC) {
   443 			byte img = rvi->image_index;
   524 			byte img = rvi->image_index;
   444 			Vehicle *u, *w;
   525 			Vehicle *u, *w;
   445 			uint dir;
   526 			uint dir;
   446 
   527 
       
   528 			v = vl[0];
   447 			v->spritenum = img;
   529 			v->spritenum = img;
   448 
   530 
   449 			u = NULL;
   531 			u = NULL;
   450 
   532 
   451 			FOR_ALL_VEHICLES(w) {
   533 			FOR_ALL_VEHICLES(w) {
   489 			v->u.rail.railtype = e->railtype;
   571 			v->u.rail.railtype = e->railtype;
   490 
   572 
   491 			v->build_year = _cur_year;
   573 			v->build_year = _cur_year;
   492 			v->type = VEH_Train;
   574 			v->type = VEH_Train;
   493 			v->cur_image = 0xAC2;
   575 			v->cur_image = 0xAC2;
       
   576 
       
   577 			AddArticulatedParts(rvi, vl);
   494 
   578 
   495 			_new_wagon_id = v->index;
   579 			_new_wagon_id = v->index;
   496 			_new_vehicle_id = v->index;
   580 			_new_vehicle_id = v->index;
   497 
   581 
   498 			VehiclePositionChanged(v);
   582 			VehiclePositionChanged(v);
   579  */
   663  */
   580 int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
   664 int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
   581 {
   665 {
   582 	const RailVehicleInfo *rvi;
   666 	const RailVehicleInfo *rvi;
   583 	int value;
   667 	int value;
   584 	Vehicle *v, *u;
   668 	Vehicle *v;
   585 	UnitID unit_num;
   669 	UnitID unit_num;
   586 	Engine *e;
   670 	Engine *e;
   587 	TileIndex tile = TileVirtXY(x, y);
   671 	TileIndex tile = TileVirtXY(x, y);
       
   672 	uint num_vehicles;
   588 
   673 
   589 	/* Check if the engine-type is valid (for the player) */
   674 	/* Check if the engine-type is valid (for the player) */
   590 	if (!IsEngineBuildable(p1, VEH_Train)) return CMD_ERROR;
   675 	if (!IsEngineBuildable(p1, VEH_Train)) return CMD_ERROR;
   591 
   676 
   592 	/* Check if the train is actually being built in a depot belonging
   677 	/* Check if the train is actually being built in a depot belonging
   610 
   695 
   611 	//make sure we only pay for half a dualheaded engine if we only requested half of it
   696 	//make sure we only pay for half a dualheaded engine if we only requested half of it
   612 	if (rvi->flags&RVI_MULTIHEAD && HASBIT(p2,0))
   697 	if (rvi->flags&RVI_MULTIHEAD && HASBIT(p2,0))
   613 		value /= 2;
   698 		value /= 2;
   614 
   699 
       
   700 	num_vehicles = (rvi->flags & RVI_MULTIHEAD && HASBIT(p2, 0)) ? 2 : 1;
       
   701 	num_vehicles += CountArticulatedParts(rvi, p1);
   615 
   702 
   616 	if (!(flags & DC_QUERY_COST)) {
   703 	if (!(flags & DC_QUERY_COST)) {
   617 		v = AllocateVehicle();
   704 		Vehicle *vl[num_vehicles];
   618 		if (v == NULL || IsOrderPoolFull())
   705 		if (!AllocateVehicles(vl, num_vehicles) || IsOrderPoolFull())
   619 			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
   706 			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
       
   707 
       
   708 		v = vl[0];
   620 
   709 
   621 		unit_num = GetFreeUnitNumber(VEH_Train);
   710 		unit_num = GetFreeUnitNumber(VEH_Train);
   622 		if (unit_num > _patches.max_trains)
   711 		if (unit_num > _patches.max_trains)
   623 			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
   712 			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
   624 
   713 
   663 			v->type = VEH_Train;
   752 			v->type = VEH_Train;
   664 			v->cur_image = 0xAC2;
   753 			v->cur_image = 0xAC2;
   665 
   754 
   666 			VehiclePositionChanged(v);
   755 			VehiclePositionChanged(v);
   667 
   756 
   668 			if (rvi->flags&RVI_MULTIHEAD && (u = AllocateVehicle()) != NULL && !HASBIT(p2,0)) {
   757 			if (rvi->flags & RVI_MULTIHEAD && !HASBIT(p2, 0)) {
   669 					AddRearEngineToMultiheadedTrain(v, u, true);
   758 				AddRearEngineToMultiheadedTrain(vl[0], vl[1], true);
       
   759 			} else {
       
   760 				AddArticulatedParts(rvi, vl);
   670 			}
   761 			}
   671 
   762 
   672 			TrainConsistChanged(v);
   763 			TrainConsistChanged(v);
   673 			UpdateTrainAcceleration(v);
   764 			UpdateTrainAcceleration(v);
   674 
   765 
   714 	}
   805 	}
   715 
   806 
   716 	return count;
   807 	return count;
   717 }
   808 }
   718 
   809 
   719 // unlink a rail wagon from the linked list.
   810 /**
   720 // returns the new value of first
   811  * Unlink a rail wagon from the consist.
       
   812  * @param v Vehicle to remove.
       
   813  * @param first The first vehicle of the consist.
       
   814  * @return The first vehicle of the consist.
       
   815  */
   721 static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first)
   816 static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first)
   722 {
   817 {
   723 	Vehicle *u;
   818 	Vehicle *u;
   724 
   819 
   725 	// unlinking the first vehicle of the chain?
   820 	// unlinking the first vehicle of the chain?
   726 	if (v == first) {
   821 	if (v == first) {
   727 		v = v->next;
   822 		v = GetNextVehicle(v);
   728 		if (v == NULL) return NULL;
   823 		if (v == NULL) return NULL;
   729 
   824 
   730 		v->subtype = TS_Free_Car;
   825 		v->subtype = TS_Free_Car;
   731 		return v;
   826 		return v;
   732 	}
   827 	}
   733 
   828 
   734 	for (u = first; u->next != v; u = u->next) {}
   829 	for (u = first; GetNextVehicle(u) != v; u = GetNextVehicle(u)) {}
   735 	u->next = v->next;
   830 	GetLastEnginePart(u)->next = GetNextVehicle(v);
   736 	return first;
   831 	return first;
   737 }
   832 }
   738 
   833 
   739 static Vehicle *FindGoodVehiclePos(const Vehicle *src)
   834 static Vehicle *FindGoodVehiclePos(const Vehicle *src)
   740 {
   835 {
   786 		if (!is_loco) dst = FindGoodVehiclePos(src);
   881 		if (!is_loco) dst = FindGoodVehiclePos(src);
   787 	} else {
   882 	} else {
   788 		dst = GetVehicle(d);
   883 		dst = GetVehicle(d);
   789 	}
   884 	}
   790 
   885 
       
   886 	// if an articulated part is being handled, deal with its parent vehicle
       
   887 	while (src->subtype == TS_Artic_Part) src = GetPrevVehicleInChain(src);
       
   888 	if (dst != NULL) {
       
   889 		while (dst->subtype == TS_Artic_Part) dst = GetPrevVehicleInChain(dst);
       
   890 	}
       
   891 
   791 	// don't move the same vehicle..
   892 	// don't move the same vehicle..
   792 	if (src == dst) return 0;
   893 	if (src == dst) return 0;
   793 
   894 
   794 	/* the player must be the owner */
   895 	/* the player must be the owner */
   795 	if (!CheckOwnership(src->owner) || (dst!=NULL && !CheckOwnership(dst->owner)))
   896 	if (!CheckOwnership(src->owner) || (dst!=NULL && !CheckOwnership(dst->owner)))
   796 		return CMD_ERROR;
   897 		return CMD_ERROR;
   797 
   898 
   798 	/* locate the head of the two chains */
   899 	/* locate the head of the two chains */
   799 	src_head = GetFirstVehicleInChain(src);
   900 	src_head = GetFirstVehicleInChain(src);
   800 	dst_head = NULL;
   901 	dst_head = NULL;
   801 	if (dst != NULL) dst_head = GetFirstVehicleInChain(dst);
   902 	if (dst != NULL) {
       
   903 		dst_head = GetFirstVehicleInChain(dst);
       
   904 		// Now deal with articulated part of destination wagon
       
   905 		dst = GetLastEnginePart(dst);
       
   906 	}
   802 
   907 
   803 	/* clear the ->first cache */
   908 	/* clear the ->first cache */
   804 	{
   909 	{
   805 		Vehicle *u;
   910 		Vehicle *u;
   806 
   911 
   850 	if (flags & DC_EXEC) {
   955 	if (flags & DC_EXEC) {
   851 		if (HASBIT(p2, 0)) {
   956 		if (HASBIT(p2, 0)) {
   852 			// unlink ALL wagons
   957 			// unlink ALL wagons
   853 			if (src != src_head) {
   958 			if (src != src_head) {
   854 				Vehicle *v = src_head;
   959 				Vehicle *v = src_head;
   855 				while (v->next != src) v=v->next;
   960 				while (GetNextVehicle(v) != src) v = GetNextVehicle(v);
   856 				v->next = NULL;
   961 				GetLastEnginePart(v)->next = NULL;
   857 			} else {
   962 			} else {
   858 				src_head = NULL;
   963 				src_head = NULL;
   859 			}
   964 			}
   860 		} else {
   965 		} else {
   861 			// if moving within the same chain, dont use dst_head as it may get invalidated
   966 			// if moving within the same chain, dont use dst_head as it may get invalidated
   862 			if (src_head == dst_head)
   967 			if (src_head == dst_head)
   863 				dst_head = NULL;
   968 				dst_head = NULL;
   864 			// unlink single wagon from linked list
   969 			// unlink single wagon from linked list
   865 			src_head = UnlinkWagon(src, src_head);
   970 			src_head = UnlinkWagon(src, src_head);
   866 			src->next = NULL;
   971 			GetLastEnginePart(src)->next = NULL;
   867 		}
   972 		}
   868 
   973 
   869 		if (dst == NULL) {
   974 		if (dst == NULL) {
   870 			// move the train to an empty line. for locomotives, we set the type to 0. for wagons, 4.
   975 			// move the train to an empty line. for locomotives, we set the type to 0. for wagons, 4.
   871 			if (is_loco) {
   976 			if (is_loco) {
   891 
   996 
   892 			// link in the wagon(s) in the chain.
   997 			// link in the wagon(s) in the chain.
   893 			{
   998 			{
   894 				Vehicle *v;
   999 				Vehicle *v;
   895 
  1000 
   896 				for (v = src; v->next != NULL; v = v->next) {};
  1001 				for (v = src; GetNextVehicle(v) != NULL; v = GetNextVehicle(v));
   897 				v->next = dst->next;
  1002 				GetLastEnginePart(v)->next = dst->next;
   898 			}
  1003 			}
   899 			dst->next = src;
  1004 			dst->next = src;
   900 		}
  1005 		}
   901 
  1006 
   902 		if (src_head) {
  1007 		if (src_head) {
   997 
  1102 
   998 	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
  1103 	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
   999 
  1104 
  1000 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
  1105 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
  1001 
  1106 
       
  1107 	while (v->subtype == TS_Artic_Part) v = GetPrevVehicleInChain(v);
  1002 	first = GetFirstVehicleInChain(v);
  1108 	first = GetFirstVehicleInChain(v);
  1003 
  1109 
  1004 	// make sure the vehicle is stopped in the depot
  1110 	// make sure the vehicle is stopped in the depot
  1005 	if (CheckTrainStoppedInDepot(first) < 0) return CMD_ERROR;
  1111 	if (CheckTrainStoppedInDepot(first) < 0) return CMD_ERROR;
  1006 
  1112 
  1030 			}
  1136 			}
  1031 
  1137 
  1032 			/* 2. We are selling the first engine, some special action might be required
  1138 			/* 2. We are selling the first engine, some special action might be required
  1033 				* here, so take attention */
  1139 				* here, so take attention */
  1034 			if ((flags & DC_EXEC) && v == first) {
  1140 			if ((flags & DC_EXEC) && v == first) {
  1035 				Vehicle *new_f = first->next;
  1141 				Vehicle *new_f = GetNextVehicle(first);
  1036 
  1142 
  1037 				/* 2.1 If the first wagon is sold, update the first-> pointers to NULL */
  1143 				/* 2.1 If the first wagon is sold, update the first-> pointers to NULL */
  1038 				for (tmp = first; tmp != NULL; tmp = tmp->next) tmp->first = NULL;
  1144 				for (tmp = first; tmp != NULL; tmp = tmp->next) tmp->first = NULL;
  1039 
  1145 
  1040 				/* 2.2 If there are wagons present after the deleted front engine, check
  1146 				/* 2.2 If there are wagons present after the deleted front engine, check
  1080 				* up on a new line to be added to the newly built loco. Replace it is.
  1186 				* up on a new line to be added to the newly built loco. Replace it is.
  1081 				* Totally braindead cause building a new engine adds all loco-less
  1187 				* Totally braindead cause building a new engine adds all loco-less
  1082 				* engines to its train anyways */
  1188 				* engines to its train anyways */
  1083 				if (p2 == 2 && ori_subtype == TS_Front_Engine) {
  1189 				if (p2 == 2 && ori_subtype == TS_Front_Engine) {
  1084 					for (v = first; v != NULL; v = tmp) {
  1190 					for (v = first; v != NULL; v = tmp) {
  1085 						tmp = v->next;
  1191 						tmp = GetNextVehicle(v);
  1086 						DoCommandByTile(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
  1192 						DoCommandByTile(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
  1087 					}
  1193 					}
  1088 				}
  1194 				}
  1089 			}
  1195 			}
  1090 		} break;
  1196 		} break;
  1091 		case 1: { /* Delete wagon and all wagons after it given certain criteria */
  1197 		case 1: { /* Delete wagon and all wagons after it given certain criteria */
  1092 			/* 1. Count the number for first and rear engines for dualheads
  1198 			/* 1. Count the number for first and rear engines for dualheads
  1093 			* to be able to deduce which ones go with which ones */
  1199 			* to be able to deduce which ones go with which ones */
  1094 			int enf_count = 0;
  1200 			int enf_count = 0;
  1095 			int enr_count = 0;
  1201 			int enr_count = 0;
  1096 			for (tmp = first; tmp != NULL; tmp = tmp->next) {
  1202 			for (tmp = first; tmp != NULL; tmp = GetNextVehicle(tmp)) {
  1097 				if (RailVehInfo(tmp->engine_type)->flags & RVI_MULTIHEAD)
  1203 				if (RailVehInfo(tmp->engine_type)->flags & RVI_MULTIHEAD)
  1098 					(IS_FIRSTHEAD_SPRITE(tmp->spritenum)) ? enf_count++ : enr_count++;
  1204 					(IS_FIRSTHEAD_SPRITE(tmp->spritenum)) ? enf_count++ : enr_count++;
  1099 			}
  1205 			}
  1100 
  1206 
  1101 			/* 2. Start deleting every vehicle after the selected one
  1207 			/* 2. Start deleting every vehicle after the selected one
  1102 			* If we encounter a matching rear-engine to a front-engine
  1208 			* If we encounter a matching rear-engine to a front-engine
  1103 			* earlier in the chain (before deletion), leave it alone */
  1209 			* earlier in the chain (before deletion), leave it alone */
  1104 			for (; v != NULL; v = tmp) {
  1210 			for (; v != NULL; v = tmp) {
  1105 				tmp = v->next;
  1211 				tmp = GetNextVehicle(v);
  1106 
  1212 
  1107 				if (RailVehInfo(v->engine_type)->flags & RVI_MULTIHEAD) {
  1213 				if (RailVehInfo(v->engine_type)->flags & RVI_MULTIHEAD) {
  1108 					if (IS_FIRSTHEAD_SPRITE(v->spritenum)) {
  1214 					if (IS_FIRSTHEAD_SPRITE(v->spritenum)) {
  1109 						/* Always delete newly encountered front-engines */
  1215 						/* Always delete newly encountered front-engines */
  1110 						enf_count--;
  1216 						enf_count--;