src/ai/trolly/build.cpp
changeset 8229 00e7467ceeee
parent 8144 65cec0877b78
child 8254 1496654ca5e7
equal deleted inserted replaced
8228:a25986306665 8229:00e7467ceeee
    68 	}
    68 	}
    69 	// There is only one bridge that can be built
    69 	// There is only one bridge that can be built
    70 	if (type2 == 0 && type != 0) type2 = type;
    70 	if (type2 == 0 && type != 0) type2 = type;
    71 
    71 
    72 	// Now, simply, build the bridge!
    72 	// Now, simply, build the bridge!
    73 	if (p->ainew.tbt == AI_TRAIN) {
    73 	if (_players_ainew[p->index].tbt == AI_TRAIN) {
    74 		return AI_DoCommand(tile_a, tile_b, (0x00 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    74 		return AI_DoCommand(tile_a, tile_b, (0x00 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    75 	} else {
    75 	} else {
    76 		return AI_DoCommand(tile_a, tile_b, ((0x80 | ROADTYPES_ROAD) << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    76 		return AI_DoCommand(tile_a, tile_b, ((0x80 | ROADTYPES_ROAD) << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
    77 	}
    77 	}
    78 }
    78 }
   142 				old_dir = dir;
   142 				old_dir = dir;
   143 				// Build the tile
   143 				// Build the tile
   144 				res = AI_DoCommand(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
   144 				res = AI_DoCommand(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
   145 				if (CmdFailed(res)) {
   145 				if (CmdFailed(res)) {
   146 					// Problem.. let's just abort it all!
   146 					// Problem.. let's just abort it all!
   147 					p->ainew.state = AI_STATE_NOTHING;
   147 					_players_ainew[p->index].state = AI_STATE_NOTHING;
   148 					return CommandCost();
   148 					return CommandCost();
   149 				}
   149 				}
   150 				cost.AddCost(res);
   150 				cost.AddCost(res);
   151 				// Go to the next tile
   151 				// Go to the next tile
   152 				part++;
   152 				part++;
   198 					res = AI_DoCommand(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
   198 					res = AI_DoCommand(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
   199 					// Currently, we ignore CMD_ERRORs!
   199 					// Currently, we ignore CMD_ERRORs!
   200 					if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_ROAD) && !EnsureNoVehicleOnGround(route[part])) {
   200 					if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_ROAD) && !EnsureNoVehicleOnGround(route[part])) {
   201 						// Problem.. let's just abort it all!
   201 						// Problem.. let's just abort it all!
   202 						DEBUG(ai, 0, "[BuidPath] route building failed at tile 0x%X, aborting", route[part]);
   202 						DEBUG(ai, 0, "[BuidPath] route building failed at tile 0x%X, aborting", route[part]);
   203 						p->ainew.state = AI_STATE_NOTHING;
   203 						_players_ainew[p->index].state = AI_STATE_NOTHING;
   204 						return CommandCost();
   204 						return CommandCost();
   205 					}
   205 					}
   206 
   206 
   207 					if (CmdSucceeded(res)) cost.AddCost(res);
   207 					if (CmdSucceeded(res)) cost.AddCost(res);
   208 				}
   208 				}
   224 
   224 
   225 // This functions tries to find the best vehicle for this type of cargo
   225 // This functions tries to find the best vehicle for this type of cargo
   226 // It returns INVALID_ENGINE if not suitable engine is found
   226 // It returns INVALID_ENGINE if not suitable engine is found
   227 EngineID AiNew_PickVehicle(Player *p)
   227 EngineID AiNew_PickVehicle(Player *p)
   228 {
   228 {
   229 	if (p->ainew.tbt == AI_TRAIN) {
   229 	if (_players_ainew[p->index].tbt == AI_TRAIN) {
   230 		// Not supported yet
   230 		// Not supported yet
   231 		return INVALID_ENGINE;
   231 		return INVALID_ENGINE;
   232 	} else {
   232 	} else {
   233 		EngineID best_veh_index = INVALID_ENGINE;
   233 		EngineID best_veh_index = INVALID_ENGINE;
   234 		int32 best_veh_rating = 0;
   234 		int32 best_veh_rating = 0;
   242 			const Engine* e = GetEngine(i);
   242 			const Engine* e = GetEngine(i);
   243 			int32 rating;
   243 			int32 rating;
   244 			CommandCost ret;
   244 			CommandCost ret;
   245 
   245 
   246 			/* Skip vehicles which can't take our cargo type */
   246 			/* Skip vehicles which can't take our cargo type */
   247 			if (rvi->cargo_type != p->ainew.cargo && !CanRefitTo(i, p->ainew.cargo)) continue;
   247 			if (rvi->cargo_type != _players_ainew[p->index].cargo && !CanRefitTo(i, _players_ainew[p->index].cargo)) continue;
   248 
   248 
   249 			// Is it availiable?
   249 			// Is it availiable?
   250 			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
   250 			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
   251 			if (!HasBit(e->player_avail, _current_player) || e->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
   251 			if (!HasBit(e->player_avail, _current_player) || e->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
   252 
   252 
   270 void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2)
   270 void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2)
   271 {
   271 {
   272 	Player* p = GetPlayer(_current_player);
   272 	Player* p = GetPlayer(_current_player);
   273 
   273 
   274 	if (success) {
   274 	if (success) {
   275 		p->ainew.state = AI_STATE_GIVE_ORDERS;
   275 		_players_ainew[p->index].state = AI_STATE_GIVE_ORDERS;
   276 		p->ainew.veh_id = _new_vehicle_id;
   276 		_players_ainew[p->index].veh_id = _new_vehicle_id;
   277 
   277 
   278 		if (GetVehicle(p->ainew.veh_id)->cargo_type != p->ainew.cargo) {
   278 		if (GetVehicle(_players_ainew[p->index].veh_id)->cargo_type != _players_ainew[p->index].cargo) {
   279 			/* Cargo type doesn't match, so refit it */
   279 			/* Cargo type doesn't match, so refit it */
   280 			if (CmdFailed(DoCommand(tile, p->ainew.veh_id, p->ainew.cargo, DC_EXEC, CMD_REFIT_ROAD_VEH))) {
   280 			if (CmdFailed(DoCommand(tile, _players_ainew[p->index].veh_id, _players_ainew[p->index].cargo, DC_EXEC, CMD_REFIT_ROAD_VEH))) {
   281 				/* Refit failed, so sell the vehicle */
   281 				/* Refit failed, so sell the vehicle */
   282 				DoCommand(tile, p->ainew.veh_id, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
   282 				DoCommand(tile, _players_ainew[p->index].veh_id, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
   283 				p->ainew.state = AI_STATE_NOTHING;
   283 				_players_ainew[p->index].state = AI_STATE_NOTHING;
   284 			}
   284 			}
   285 		}
   285 		}
   286 	} else {
   286 	} else {
   287 		/* XXX this should be handled more gracefully */
   287 		/* XXX this should be handled more gracefully */
   288 		p->ainew.state = AI_STATE_NOTHING;
   288 		_players_ainew[p->index].state = AI_STATE_NOTHING;
   289 	}
   289 	}
   290 }
   290 }
   291 
   291 
   292 
   292 
   293 // Builds the best vehicle possible
   293 // Builds the best vehicle possible
   294 CommandCost AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
   294 CommandCost AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
   295 {
   295 {
   296 	EngineID i = AiNew_PickVehicle(p);
   296 	EngineID i = AiNew_PickVehicle(p);
   297 
   297 
   298 	if (i == INVALID_ENGINE) return CMD_ERROR;
   298 	if (i == INVALID_ENGINE) return CMD_ERROR;
   299 	if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR;
   299 	if (_players_ainew[p->index].tbt == AI_TRAIN) return CMD_ERROR;
   300 
   300 
   301 	if (flag & DC_EXEC) {
   301 	if (flag & DC_EXEC) {
   302 		return AI_DoCommandCc(tile, i, 0, flag, CMD_BUILD_ROAD_VEH, CcAI);
   302 		return AI_DoCommandCc(tile, i, 0, flag, CMD_BUILD_ROAD_VEH, CcAI);
   303 	} else {
   303 	} else {
   304 		return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
   304 		return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
   306 }
   306 }
   307 
   307 
   308 CommandCost AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag)
   308 CommandCost AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag)
   309 {
   309 {
   310 	CommandCost ret, ret2;
   310 	CommandCost ret, ret2;
   311 	if (p->ainew.tbt == AI_TRAIN) {
   311 	if (_players_ainew[p->index].tbt == AI_TRAIN) {
   312 		return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
   312 		return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
   313 	} else {
   313 	} else {
   314 		ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
   314 		ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
   315 		if (CmdFailed(ret2)) return ret;
   315 		if (CmdFailed(ret2)) return ret;
   316 		// Try to build the road from the depot
   316 		// Try to build the road from the depot