src/ai/trolly/build.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
equal deleted inserted replaced
9912:1ac8aac92385 9913:e79cd19772dd
    34 //    tile : tile where station is going to be build
    34 //    tile : tile where station is going to be build
    35 //    length : in case of AI_TRAIN: length of station
    35 //    length : in case of AI_TRAIN: length of station
    36 //    numtracks : in case of AI_TRAIN: tracks of station
    36 //    numtracks : in case of AI_TRAIN: tracks of station
    37 //    direction : the direction of the station
    37 //    direction : the direction of the station
    38 //    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
    38 //    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
    39 int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag)
    39 CommandCost AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag)
    40 {
    40 {
    41 	if (type == AI_TRAIN)
    41 	if (type == AI_TRAIN)
    42 		return AI_DoCommand(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
    42 		return AI_DoCommand(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
    43 
    43 
    44 	if (type == AI_BUS)
    44 	if (type == AI_BUS)
    51 // Builds a brdige. The second best out of the ones available for this player
    51 // Builds a brdige. The second best out of the ones available for this player
    52 //  Params:
    52 //  Params:
    53 //   tile_a : starting point
    53 //   tile_a : starting point
    54 //   tile_b : end point
    54 //   tile_b : end point
    55 //   flag : flag passed to DoCommand
    55 //   flag : flag passed to DoCommand
    56 int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag)
    56 CommandCost AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag)
    57 {
    57 {
    58 	int bridge_type, bridge_len, type, type2;
    58 	int bridge_type, bridge_len, type, type2;
    59 
    59 
    60 	// Find a good bridgetype (the best money can buy)
    60 	// Find a good bridgetype (the best money can buy)
    61 	bridge_len = GetBridgeLength(tile_a, tile_b);
    61 	bridge_len = GetBridgeLength(tile_a, tile_b);
    88 //  Params:
    88 //  Params:
    89 //   PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder
    89 //   PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder
    90 //   part : Which part we need to build
    90 //   part : Which part we need to build
    91 //
    91 //
    92 // TODO: skip already builded road-pieces (e.g.: cityroad)
    92 // TODO: skip already builded road-pieces (e.g.: cityroad)
    93 int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag)
    93 CommandCost AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag)
    94 {
    94 {
    95 	int part = PathFinderInfo->position;
    95 	int part = PathFinderInfo->position;
    96 	byte *route_extra = PathFinderInfo->route_extra;
    96 	byte *route_extra = PathFinderInfo->route_extra;
    97 	TileIndex *route = PathFinderInfo->route;
    97 	TileIndex *route = PathFinderInfo->route;
    98 	int dir;
    98 	int dir;
    99 	int old_dir = -1;
    99 	int old_dir = -1;
   100 	int cost = 0;
   100 	CommandCost cost = 0;
   101 	int res;
   101 	CommandCost res;
   102 	// We need to calculate the direction with the parent of the parent.. so we skip
   102 	// We need to calculate the direction with the parent of the parent.. so we skip
   103 	//  the first pieces and the last piece
   103 	//  the first pieces and the last piece
   104 	if (part < 1) part = 1;
   104 	if (part < 1) part = 1;
   105 	// When we are done, stop it
   105 	// When we are done, stop it
   106 	if (part >= PathFinderInfo->route_length - 1) {
   106 	if (part >= PathFinderInfo->route_length - 1) {
   204 						DEBUG(ai, 0, "[BuidPath] route building failed at tile 0x%X, aborting", route[part]);
   204 						DEBUG(ai, 0, "[BuidPath] route building failed at tile 0x%X, aborting", route[part]);
   205 						p->ainew.state = AI_STATE_NOTHING;
   205 						p->ainew.state = AI_STATE_NOTHING;
   206 						return 0;
   206 						return 0;
   207 					}
   207 					}
   208 
   208 
   209 					if (!CmdFailed(res)) cost += res;
   209 					if (CmdSucceeded(res)) cost += res;
   210 				}
   210 				}
   211 				// Go to the next tile
   211 				// Go to the next tile
   212 				part++;
   212 				part++;
   213 				// Check if it is still in range..
   213 				// Check if it is still in range..
   214 				if (part >= PathFinderInfo->route_length - 1) break;
   214 				if (part >= PathFinderInfo->route_length - 1) break;
   241 		/* Loop through all road vehicles */
   241 		/* Loop through all road vehicles */
   242 		for (i = start; i != end; i++) {
   242 		for (i = start; i != end; i++) {
   243 			const RoadVehicleInfo *rvi = RoadVehInfo(i);
   243 			const RoadVehicleInfo *rvi = RoadVehInfo(i);
   244 			const Engine* e = GetEngine(i);
   244 			const Engine* e = GetEngine(i);
   245 			int32 rating;
   245 			int32 rating;
   246 			int32 ret;
   246 			CommandCost ret;
   247 
   247 
   248 			/* Skip vehicles which can't take our cargo type */
   248 			/* Skip vehicles which can't take our cargo type */
   249 			if (rvi->cargo_type != p->ainew.cargo && !CanRefitTo(i, p->ainew.cargo)) continue;
   249 			if (rvi->cargo_type != p->ainew.cargo && !CanRefitTo(i, p->ainew.cargo)) continue;
   250 
   250 
   251 			// Is it availiable?
   251 			// Is it availiable?
   291 	}
   291 	}
   292 }
   292 }
   293 
   293 
   294 
   294 
   295 // Builds the best vehicle possible
   295 // Builds the best vehicle possible
   296 int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
   296 CommandCost AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
   297 {
   297 {
   298 	EngineID i = AiNew_PickVehicle(p);
   298 	EngineID i = AiNew_PickVehicle(p);
   299 
   299 
   300 	if (i == INVALID_ENGINE) return CMD_ERROR;
   300 	if (i == INVALID_ENGINE) return CMD_ERROR;
   301 	if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR;
   301 	if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR;
   305 	} else {
   305 	} else {
   306 		return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
   306 		return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
   307 	}
   307 	}
   308 }
   308 }
   309 
   309 
   310 int AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag)
   310 CommandCost AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag)
   311 {
   311 {
   312 	int ret, ret2;
   312 	CommandCost ret, ret2;
   313 	if (p->ainew.tbt == AI_TRAIN) {
   313 	if (p->ainew.tbt == AI_TRAIN) {
   314 		return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
   314 		return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
   315 	} else {
   315 	} else {
   316 		ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
   316 		ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
   317 		if (CmdFailed(ret)) return ret;
   317 		if (CmdFailed(ret)) return ret;