roadveh_cmd.c
changeset 3977 513433ebd092
parent 3959 48f178431701
child 3990 a317e665fb77
equal deleted inserted replaced
3976:6135bc445350 3977:513433ebd092
    19 #include "pathfind.h"
    19 #include "pathfind.h"
    20 #include "npf.h"
    20 #include "npf.h"
    21 #include "player.h"
    21 #include "player.h"
    22 #include "sound.h"
    22 #include "sound.h"
    23 #include "depot.h"
    23 #include "depot.h"
    24 #include "bridge.h"
       
    25 #include "tunnel_map.h"
    24 #include "tunnel_map.h"
    26 #include "bridge_map.h"
       
    27 #include "vehicle_gui.h"
    25 #include "vehicle_gui.h"
    28 #include "newgrf_engine.h"
    26 #include "newgrf_engine.h"
    29 #include "yapf/yapf.h"
    27 #include "yapf/yapf.h"
    30 
    28 
    31 static const uint16 _roadveh_images[63] = {
    29 static const uint16 _roadveh_images[63] = {
   307 
   305 
   308 static const Depot* FindClosestRoadDepot(const Vehicle* v)
   306 static const Depot* FindClosestRoadDepot(const Vehicle* v)
   309 {
   307 {
   310 	TileIndex tile = v->tile;
   308 	TileIndex tile = v->tile;
   311 
   309 
       
   310 	if (v->u.road.state == 255) tile = GetVehicleOutOfTunnelTile(v);
       
   311 
   312 	if (_patches.yapf.road_use_yapf) {
   312 	if (_patches.yapf.road_use_yapf) {
   313 		Depot* ret = YapfFindNearestRoadDepot(v);
   313 		Depot* ret = YapfFindNearestRoadDepot(v);
   314 		return ret;
   314 		return ret;
   315 	} else if (_patches.new_pathfinding_all) {
   315 	} else if (_patches.new_pathfinding_all) {
   316 		NPFFoundTargetData ftd;
   316 		NPFFoundTargetData ftd;
   403 
   403 
   404 	v = GetVehicle(p1);
   404 	v = GetVehicle(p1);
   405 
   405 
   406 	if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
   406 	if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
   407 
   407 
   408 	if (v->vehstatus & VS_STOPPED ||
   408 	if (v->vehstatus & (VS_HIDDEN | VS_STOPPED) ||
   409 			v->u.road.crashed_ctr != 0 ||
   409 			v->u.road.crashed_ctr != 0 ||
   410 			v->breakdown_ctr != 0 ||
   410 			v->breakdown_ctr != 0 ||
   411 			v->u.road.overtaking != 0 ||
   411 			v->u.road.overtaking != 0 ||
   412 			v->u.road.state == 255 ||
       
   413 			IsRoadVehInDepot(v) ||
       
   414 			v->cur_speed < 5) {
   412 			v->cur_speed < 5) {
   415 		return CMD_ERROR;
   413 		return CMD_ERROR;
   416 	}
   414 	}
   417 
       
   418 	if (IsTunnelTile(v->tile) && DirToDiagDir(v->direction) == GetTunnelDirection(v->tile)) return CMD_ERROR;
       
   419 	if (IsBridgeTile(v->tile) && DirToDiagDir(v->direction) == GetBridgeRampDirection(v->tile)) return CMD_ERROR;
       
   420 
   415 
   421 	if (flags & DC_EXEC) v->u.road.reverse_ctr = 180;
   416 	if (flags & DC_EXEC) v->u.road.reverse_ctr = 180;
   422 
   417 
   423 	return 0;
   418 	return 0;
   424 }
   419 }
   482 static byte SetRoadVehPosition(Vehicle *v, int x, int y)
   477 static byte SetRoadVehPosition(Vehicle *v, int x, int y)
   483 {
   478 {
   484 	byte new_z, old_z;
   479 	byte new_z, old_z;
   485 
   480 
   486 	// need this hint so it returns the right z coordinate on bridges.
   481 	// need this hint so it returns the right z coordinate on bridges.
       
   482 	_get_z_hint = v->z_pos;
   487 	v->x_pos = x;
   483 	v->x_pos = x;
   488 	v->y_pos = y;
   484 	v->y_pos = y;
   489 	new_z = GetSlopeZ(x, y);
   485 	new_z = GetSlopeZ(x, y);
       
   486 	_get_z_hint = 0;
   490 
   487 
   491 	old_z = v->z_pos;
   488 	old_z = v->z_pos;
   492 	v->z_pos = new_z;
   489 	v->z_pos = new_z;
   493 
   490 
   494 	VehiclePositionChanged(v);
   491 	VehiclePositionChanged(v);
   819 	uint spd = v->cur_speed + 1 + (v->u.road.overtaking != 0 ? 1 : 0);
   816 	uint spd = v->cur_speed + 1 + (v->u.road.overtaking != 0 ? 1 : 0);
   820 	byte t;
   817 	byte t;
   821 
   818 
   822 	// Clamp
   819 	// Clamp
   823 	spd = min(spd, v->max_speed);
   820 	spd = min(spd, v->max_speed);
   824 	if (v->u.road.state == 255) spd = min(spd, SetSpeedLimitOnBridge(v));
       
   825 
   821 
   826 	//updates statusbar only if speed have changed to save CPU time
   822 	//updates statusbar only if speed have changed to save CPU time
   827 	if (spd != v->cur_speed) {
   823 	if (spd != v->cur_speed) {
   828 		v->cur_speed = spd;
   824 		v->cur_speed = spd;
   829 		if (_patches.vehicle_speed) {
   825 		if (_patches.vehicle_speed) {
  1288 		if (RoadVehFindCloseTo(v, gp.x, gp.y, v->direction) != NULL) {
  1284 		if (RoadVehFindCloseTo(v, gp.x, gp.y, v->direction) != NULL) {
  1289 			v->cur_speed = 0;
  1285 			v->cur_speed = 0;
  1290 			return;
  1286 			return;
  1291 		}
  1287 		}
  1292 
  1288 
  1293 		if ((IsTunnelTile(gp.new_tile) || IsBridgeTile(gp.new_tile)) && VehicleEnterTile(v, gp.new_tile, gp.x, gp.y) & 4) {
  1289 		if (IsTunnelTile(gp.new_tile) &&
       
  1290 				VehicleEnterTile(v, gp.new_tile, gp.x, gp.y) & 4) {
  1294 			//new_dir = RoadGetNewDirection(v, gp.x, gp.y)
  1291 			//new_dir = RoadGetNewDirection(v, gp.x, gp.y)
  1295 			v->cur_image = GetRoadVehImage(v, v->direction);
  1292 			v->cur_image = GetRoadVehImage(v, v->direction);
  1296 			UpdateRoadVehDeltaXY(v);
  1293 			UpdateRoadVehDeltaXY(v);
  1297 			SetRoadVehPosition(v,gp.x,gp.y);
  1294 			SetRoadVehPosition(v,gp.x,gp.y);
  1298 			return;
  1295 			return;
  1299 		}
  1296 		}
  1300 
  1297 
  1301 		v->x_pos = gp.x;
  1298 		v->x_pos = gp.x;
  1302 		v->y_pos = gp.y;
  1299 		v->y_pos = gp.y;
  1303 		VehiclePositionChanged(v);
  1300 		VehiclePositionChanged(v);
  1304 		if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
       
  1305 		return;
  1301 		return;
  1306 	}
  1302 	}
  1307 
  1303 
  1308 	rd = _road_drive_data[(v->u.road.state + (_opt.road_side << 4)) ^ v->u.road.overtaking][v->u.road.frame + 1];
  1304 	rd = _road_drive_data[(v->u.road.state + (_opt.road_side << 4)) ^ v->u.road.overtaking][v->u.road.frame + 1];
  1309 
  1305