train_cmd.c
changeset 2484 8e0c88a833fb
parent 2482 dffcca243dbc
child 2493 d834d0c1502a
equal deleted inserted replaced
2483:ee0517ddfa92 2484:8e0c88a833fb
   761  * - p1 (bit 16 - 31) what wagon to put the source wagon AFTER (p1 & 0xFFFF0000) XXX - 0xFFFF0000 to make a new line
   761  * - p1 (bit 16 - 31) what wagon to put the source wagon AFTER (p1 & 0xFFFF0000) XXX - 0xFFFF0000 to make a new line
   762  * @param p2 (bit 0) move all vehicles following the source vehicle
   762  * @param p2 (bit 0) move all vehicles following the source vehicle
   763  */
   763  */
   764 int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
   764 int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
   765 {
   765 {
       
   766 	VehicleID s = GB(p1, 0, 16);
       
   767 	VehicleID d = GB(p1, 16, 16);
   766 	Vehicle *src, *dst, *src_head, *dst_head;
   768 	Vehicle *src, *dst, *src_head, *dst_head;
   767 	bool is_loco;
   769 	bool is_loco;
   768 
   770 
   769 	if (!IsVehicleIndex(p1 & 0xFFFF)) return CMD_ERROR;
   771 	if (!IsVehicleIndex(s)) return CMD_ERROR;
   770 
   772 
   771 	src = GetVehicle(p1 & 0xFFFF);
   773 	src = GetVehicle(s);
   772 
   774 
   773 	if (src->type != VEH_Train) return CMD_ERROR;
   775 	if (src->type != VEH_Train) return CMD_ERROR;
   774 
   776 
   775 	is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON) && IS_FIRSTHEAD_SPRITE(src->spritenum);
   777 	is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON) && IS_FIRSTHEAD_SPRITE(src->spritenum);
   776 
   778 
   777 	// if nothing is selected as destination, try and find a matching vehicle to drag to.
   779 	// if nothing is selected as destination, try and find a matching vehicle to drag to.
   778 	if (((int32)p1 >> 16) == -1) {
   780 	if (d == INVALID_VEHICLE) {
   779 		dst = NULL;
   781 		dst = NULL;
   780 		if (!is_loco) dst = FindGoodVehiclePos(src);
   782 		if (!is_loco) dst = FindGoodVehiclePos(src);
   781 	} else {
   783 	} else {
   782 		dst = GetVehicle((int32)p1 >> 16);
   784 		dst = GetVehicle(d);
   783 	}
   785 	}
   784 
   786 
   785 	// don't move the same vehicle..
   787 	// don't move the same vehicle..
   786 	if (src == dst) return 0;
   788 	if (src == dst) return 0;
   787 
   789 
  1757 			}
  1759 			}
  1758 			break;
  1760 			break;
  1759 
  1761 
  1760 		case 1:
  1762 		case 1:
  1761 			// diesel smoke
  1763 			// diesel smoke
  1762 			if (u->cur_speed <= 40 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x1E00) {
  1764 			if (u->cur_speed <= 40 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && GB(Random(), 0, 16) <= 0x1E00) {
  1763 				CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
  1765 				CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
  1764 			}
  1766 			}
  1765 			break;
  1767 			break;
  1766 
  1768 
  1767 		case 2:
  1769 		case 2:
  1768 			// blue spark
  1770 			// blue spark
  1769 			if ( (v->tick_counter&0x3) == 0 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x5B0) {
  1771 			if (GB(v->tick_counter, 0, 2) == 0 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && GB(Random(), 0, 16) <= 0x5B0) {
  1770 				CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
  1772 				CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
  1771 			}
  1773 			}
  1772 			break;
  1774 			break;
  1773 		}
  1775 		}
  1774 	} while ( (v = v->next) != NULL );
  1776 	} while ( (v = v->next) != NULL );
  3014 
  3016 
  3015 	if ( (state == 4) && (v->u.rail.track != 0x40) ) {
  3017 	if ( (state == 4) && (v->u.rail.track != 0x40) ) {
  3016 		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
  3018 		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
  3017 	}
  3019 	}
  3018 
  3020 
  3019 	if (state <= 200 && (uint16)(r=Random()) <= 0x2492) {
  3021 	if (state <= 200 && GB(r = Random(), 0, 16) <= 0x2492) {
  3020 		index = (r * 10 >> 16);
  3022 		index = (r * 10 >> 16);
  3021 
  3023 
  3022 		u = v;
  3024 		u = v;
  3023 		do {
  3025 		do {
  3024 			if (--index < 0) {
  3026 			if (--index < 0) {
  3080 
  3082 
  3081 static bool TrainCheckIfLineEnds(Vehicle *v)
  3083 static bool TrainCheckIfLineEnds(Vehicle *v)
  3082 {
  3084 {
  3083 	TileIndex tile;
  3085 	TileIndex tile;
  3084 	uint x,y;
  3086 	uint x,y;
       
  3087 	uint16 break_speed;
  3085 	int t;
  3088 	int t;
  3086 	uint32 ts;
  3089 	uint32 ts;
  3087 	byte trackdir;
  3090 	byte trackdir;
  3088 
  3091 
  3089 	if ((uint)(t=v->breakdown_ctr) > 1) {
  3092 	t = v->breakdown_ctr;
       
  3093 	if (t > 1) {
  3090 		v->vehstatus |= VS_TRAIN_SLOWING;
  3094 		v->vehstatus |= VS_TRAIN_SLOWING;
  3091 
  3095 
  3092 		t = _breakdown_speeds[ ((~t) >> 4) & 0xF];
  3096 		break_speed = _breakdown_speeds[GB(~t, 4, 4)];
  3093 		if ((uint16)t <= v->cur_speed)
  3097 		if (break_speed < v->cur_speed) v->cur_speed = break_speed;
  3094 			v->cur_speed = t;
       
  3095 	} else {
  3098 	} else {
  3096 		v->vehstatus &= ~VS_TRAIN_SLOWING;
  3099 		v->vehstatus &= ~VS_TRAIN_SLOWING;
  3097 	}
  3100 	}
  3098 
  3101 
  3099 	// exit if inside a tunnel
  3102 	// exit if inside a tunnel
  3158 	case 6:
  3161 	case 6:
  3159 		x = (~y) + x + 8;
  3162 		x = (~y) + x + 8;
  3160 		break;
  3163 		break;
  3161 	}
  3164 	}
  3162 
  3165 
  3163 	if ( (uint16)ts != 0) {
  3166 	if (GB(ts, 0, 16) != 0) {
  3164 		/* If we approach a rail-piece which we can't enter, don't enter it! */
  3167 		/* If we approach a rail-piece which we can't enter, don't enter it! */
  3165 		if (x + 4 > 15 && !CheckCompatibleRail(v, tile)) {
  3168 		if (x + 4 > 15 && !CheckCompatibleRail(v, tile)) {
  3166 			v->cur_speed = 0;
  3169 			v->cur_speed = 0;
  3167 			ReverseTrainDirection(v);
  3170 			ReverseTrainDirection(v);
  3168 			return false;
  3171 			return false;
  3206 		};
  3209 		};
  3207 	};
  3210 	};
  3208 
  3211 
  3209 	// slow down
  3212 	// slow down
  3210 	v->vehstatus |= VS_TRAIN_SLOWING;
  3213 	v->vehstatus |= VS_TRAIN_SLOWING;
  3211 	t = _breakdown_speeds[x & 0xF];
  3214 	break_speed = _breakdown_speeds[x & 0xF];
  3212 	if (!(v->direction&1)) t>>=1;
  3215 	if (!(v->direction&1)) break_speed >>= 1;
  3213 	if ((uint16)t < v->cur_speed)
  3216 	if (break_speed < v->cur_speed) v->cur_speed = break_speed;
  3214 		v->cur_speed = t;
       
  3215 
  3217 
  3216 	return true;
  3218 	return true;
  3217 }
  3219 }
  3218 
  3220 
  3219 static void TrainLocoHandler(Vehicle *v, bool mode)
  3221 static void TrainLocoHandler(Vehicle *v, bool mode)