src/train_cmd.cpp
branchnoai
changeset 9625 3301b1b3889c
parent 9624 b71483f2330f
child 9626 79f2b5a0cdd7
--- a/src/train_cmd.cpp	Fri May 25 00:25:08 2007 +0000
+++ b/src/train_cmd.cpp	Fri Jun 01 20:54:09 2007 +0000
@@ -15,6 +15,7 @@
 #include "tile.h"
 #include "tunnel_map.h"
 #include "vehicle.h"
+#include "articulated_vehicles.h"
 #include "command.h"
 #include "pathfind.h"
 #include "npf.h"
@@ -501,75 +502,6 @@
 	DrawSprite(image, pal, x, y);
 }
 
-uint CountArticulatedParts(EngineID engine_type)
-{
-	if (!HASBIT(EngInfo(engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return 0;
-
-	uint i;
-	for (i = 1; i < 10; i++) {
-		uint16 callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, engine_type, NULL);
-		if (callback == CALLBACK_FAILED || callback == 0xFF) break;
-	}
-
-	return i - 1;
-}
-
-static void AddArticulatedParts(Vehicle **vl)
-{
-	const Vehicle *v = vl[0];
-	Vehicle *u = vl[0];
-
-	if (!HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return;
-
-	for (uint i = 1; i < 10; i++) {
-		uint16 callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, v->engine_type, v);
-		if (callback == CALLBACK_FAILED || callback == 0xFF) return;
-
-		/* Attempt to use pre-allocated vehicles until they run out. This can happen
-		 * if the callback returns different values depending on the cargo type. */
-		u->next = vl[i];
-		if (u->next == NULL) u->next = AllocateVehicle();
-		if (u->next == NULL) return;
-
-		u = u->next;
-
-		EngineID engine_type = GB(callback, 0, 7);
-		bool flip_image = HASBIT(callback, 7);
-		const RailVehicleInfo *rvi_artic = RailVehInfo(engine_type);
-
-		/* get common values from first engine */
-		u->direction = v->direction;
-		u->owner = v->owner;
-		u->tile = v->tile;
-		u->x_pos = v->x_pos;
-		u->y_pos = v->y_pos;
-		u->z_pos = v->z_pos;
-		u->u.rail.track = v->u.rail.track;
-		u->u.rail.railtype = v->u.rail.railtype;
-		u->build_year = v->build_year;
-		u->vehstatus = v->vehstatus & ~VS_STOPPED;
-		u->u.rail.first_engine = v->engine_type;
-
-		/* get more settings from rail vehicle info */
-		u->spritenum = rvi_artic->image_index;
-		if (flip_image) u->spritenum++;
-		u->cargo_type = rvi_artic->cargo_type;
-		u->cargo_subtype = 0;
-		u->cargo_cap = rvi_artic->capacity;
-		u->max_speed = 0;
-		u->max_age = 0;
-		u->engine_type = engine_type;
-		u->value = 0;
-		u = new (u) Train();
-		u->subtype = 0;
-		SetArticulatedPart(u);
-		u->cur_image = 0xAC2;
-		u->random_bits = VehicleRandomBits();
-
-		VehiclePositionChanged(u);
-	}
-}
-
 static int32 CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
 {
 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
@@ -619,8 +551,10 @@
 			v->u.rail.track = TRACK_BIT_DEPOT;
 			v->vehstatus = VS_HIDDEN | VS_DEFPAL;
 
+			v = new (v) Train();
 			v->subtype = 0;
 			SetTrainWagon(v);
+
 			if (u != NULL) {
 				u->next = v;
 			} else {
@@ -637,7 +571,6 @@
 			v->u.rail.railtype = rvi->railtype;
 
 			v->build_year = _cur_year;
-			v = new (v) Train();
 			v->cur_image = 0xAC2;
 			v->random_bits = VehicleRandomBits();
 
@@ -1017,39 +950,6 @@
 		dst_head = NULL;
 	}
 
-	if (dst != NULL && IsMultiheaded(dst) && !IsTrainEngine(dst) && IsTrainWagon(src)) {
-		/* We are moving a wagon to the rear part of a multiheaded engine */
-		if (dst->next == NULL) {
-			/* It's the last one, so we will add the wagon just before the rear engine */
-			dst = GetPrevVehicleInChain(dst);
-			/* Now if the vehicle we want to link to is the vehicle itself, drop out */
-			if (dst == src) return CMD_ERROR;
-			/* if dst is NULL, it means that dst got a rear multiheaded engine as first engine. We can't use that */
-			if (dst == NULL) return CMD_ERROR;
-		} else {
-			/* there are more units on this train, so we will add the wagon after the next one*/
-			dst = dst->next;
-		}
-	}
-
-	if (IsTrainEngine(src) && dst_head != NULL) {
-		/* we need to make sure that we didn't place it between a pair of multiheaded engines */
-		Vehicle *engine = NULL;
-
-		for (Vehicle *u = dst_head; u != NULL; u = u->next) {
-			if (IsTrainEngine(u) && IsMultiheaded(u) && u->u.rail.other_multiheaded_part != NULL) {
-				engine = u;
-			}
-			if (engine != NULL && engine->u.rail.other_multiheaded_part == u) {
-				engine = NULL;
-			}
-			if (u == dst) {
-				if (engine != NULL) dst = engine->u.rail.other_multiheaded_part;
-				break;
-			}
-		}
-	}
-
 	if (IsMultiheaded(src) && !IsTrainEngine(src)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR);
 
 	/* when moving all wagons, we can't have the same src_head and dst_head */
@@ -1207,26 +1107,6 @@
 			src->u.rail.other_multiheaded_part->first = NULL;
 		}
 
-		if (HASBIT(p2, 0) && src_head != NULL && src_head != src) {
-			/* if we stole a rear multiheaded engine, we better give it back to the front end */
-			Vehicle *engine = NULL, *u;
-			for (u = src_head; u != NULL; u = u->next) {
-				if (IsMultiheaded(u)) {
-					if (IsTrainEngine(u)) {
-						engine = u;
-						continue;
-					}
-					/* we got the rear engine to match with the front one */
-					engine = NULL;
-				}
-			}
-			if (engine != NULL && engine->u.rail.other_multiheaded_part != NULL) {
-				AddWagonToConsist(engine->u.rail.other_multiheaded_part, engine);
-				/* previous line set the front engine to the old front. We need to clear that */
-				engine->u.rail.other_multiheaded_part->first = NULL;
-			}
-		}
-
 		/* If there is an engine behind first_engine we moved away, it should become new first_engine
 		 * To do this, CmdMoveRailVehicle must be called once more
 		 * we can't loop forever here because next time we reach this line we will have a front engine */
@@ -2777,7 +2657,7 @@
 			v != tcc->v_skip &&
 			v->type == VEH_TRAIN &&
 			v->u.rail.track != TRACK_BIT_DEPOT &&
-			myabs(v->z_pos - tcc->v->z_pos) <= 6 &&
+			myabs(v->z_pos - tcc->v->z_pos) < 6 &&
 			myabs(v->x_pos - tcc->v->x_pos) < 6 &&
 			myabs(v->y_pos - tcc->v->y_pos) < 6) {
 		return v;