src/ai/api/ai_vehicle.cpp
author truelight
Tue, 20 Mar 2007 11:42:07 +0000
branchnoai
changeset 9496 05ebee9884b3
parent 9491 351239ad524c
child 9497 f6678533ccba
permissions -rw-r--r--
(svn r9368) [NoAI] -Fix: store _new_vehicle_id directly after successful handling the command in a per-AI-player-safe piece of memory, so we can restore the value when ever we want later in the process
/* $Id$ */

/** @file ai_vehicle.cpp handles the functions of the AIVehicle class */

#include "ai_vehicle.hpp"
#include "ai_cargo.hpp"
#include "../../command.h"
#include "../../depot.h"
#include "../../engine.h"
#include "../../player.h"
#include "../../vehicle.h"

bool AIVehicle::IsValidEngine(EngineID engine_id)
{
	return ::IsEngineIndex(engine_id); // TODO: implement 'can I build this engine check'
}

bool AIVehicle::IsValidVehicle(VehicleID vehicle_id)
{
	return ::IsValidVehicleID(vehicle_id) && ::GetVehicle(vehicle_id)->owner == _current_player;
}

EngineID AIVehicle::FindBestRoadVehicle(CargoID cargo, uint8 min_reliability)
{
	if (!AICargo::IsValidCargo(cargo) || min_reliability > 100) return INVALID_ENGINE;

	EngineID best_engine = INVALID_ENGINE;
	EngineID engine_id;

	FOR_ALL_ENGINEIDS_OF_TYPE(engine_id, VEH_ROAD) {
		/* Is the vehicle available for this player */
		if (IsEngineBuildable(engine_id, VEH_ROAD, _current_player) &&
				GetEngine(engine_id)->reliability * 100 >= min_reliability << 16 &&
				(RoadVehInfo(engine_id)->cargo_type == cargo || CanRefitTo(engine_id, cargo))) {
			best_engine = engine_id;
		}
	}

	return INVALID_ENGINE;
}

VehicleID AIVehicle::BuildVehicle(TileIndex depot, EngineID engine_id)
{
	if (!this->IsValidEngine(engine_id)) return false;

	bool ret;
	switch (::GetEngine(engine_id)->type) {
		case VEH_ROAD: ret = this->DoCommand(depot, engine_id, 0, CMD_BUILD_ROAD_VEH); break;
		default: NOT_REACHED(); return INVALID_VEHICLE; // TODO: implement trains, ships and aircraft
	}

	return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE;
}

VehicleID AIVehicle::CloneVehicle(TileIndex depot, VehicleID vehicle_id, bool share_orders)
{
	if (!this->IsValidVehicle(vehicle_id)) return false;

	bool ret;
	switch (::GetVehicle(vehicle_id)->type) {
		case VEH_ROAD: ret = this->DoCommand(depot, vehicle_id, share_orders, CMD_CLONE_VEHICLE); break;
		default: return INVALID_VEHICLE; // TODO: implement trains, ships and aircraft
	}

	return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE;
}

bool AIVehicle::RefitVehicle(VehicleID vehicle_id, CargoID cargo)
{
	if (!this->IsValidVehicle(vehicle_id) || !AICargo::IsValidCargo(cargo)) return false;

	switch (::GetVehicle(vehicle_id)->type) {
		case VEH_ROAD: return this->DoCommand(0, vehicle_id, cargo, CMD_REFIT_ROAD_VEH);
		default: return false; // TODO: implement trains, ships and aircraft
	}
}


bool AIVehicle::SellVehicle(VehicleID vehicle_id)
{
	if (!this->IsValidVehicle(vehicle_id)) return false;

	switch (::GetVehicle(vehicle_id)->type) {
		case VEH_ROAD: return this->DoCommand(0, vehicle_id, 0, CMD_SELL_ROAD_VEH);
		default: return false; // TODO: implement trains, ships and aircraft
	}
}

bool AIVehicle::SendVehicleToDepot(VehicleID vehicle_id)
{
	if (!this->IsValidVehicle(vehicle_id)) return false;

	switch (::GetVehicle(vehicle_id)->type) {
		case VEH_ROAD: return this->DoCommand(0, vehicle_id, 0, CMD_SEND_ROADVEH_TO_DEPOT);
		default: return false; // TODO: implement trains, ships and aircraft
	}
}

bool AIVehicle::StartStopVehicle(VehicleID vehicle_id)
{
	if (!this->IsValidVehicle(vehicle_id)) return false;

	switch (::GetVehicle(vehicle_id)->type) {
		case VEH_ROAD: return this->DoCommand(0, vehicle_id, 0, CMD_START_STOP_ROADVEH);
		default: return false; // TODO: implement trains, ships and aircraft
	}
}

bool AIVehicle::SkipVehicleOrder(VehicleID vehicle_id)
{
	if (!this->IsValidVehicle(vehicle_id)) return false;

	return this->DoCommand(0, vehicle_id, 0, CMD_SKIP_ORDER);
}