src/ai/api/ai_industry.cpp
author truebrain
Sun, 24 Feb 2008 23:00:00 +0000
branchnoai
changeset 9756 7e637829cbd3
parent 9737 ee408edf3851
child 9795 679ba7cd8426
permissions -rw-r--r--
(svn r12241) [NoAI] -Fix r12236: global search/replace to the unreadable documentation! WHOHO! (tnx Progman)
/* $Id$ */

/** @file ai_industry.cpp handles the query-related of the AIIndustry class */

#include "ai_industry.hpp"
#include "ai_cargo.hpp"
#include "../../industry.h"
#include "../../strings_func.h"
#include "table/strings.h"

/* static */ IndustryID AIIndustry::GetMaxIndustryID()
{
	return ::GetMaxIndustryIndex();
}

/* static */ int32 AIIndustry::GetIndustryCount()
{
	return ::GetNumIndustries();
}

/* static */ bool AIIndustry::IsValidIndustry(IndustryID industry_id)
{
	return ::IsValidIndustryID(industry_id);
}

/* static */ char *AIIndustry::GetName(IndustryID industry_id)
{
	if (!IsValidIndustry(industry_id)) return NULL;
	static const int len = 64;
	char *industry_name = MallocT<char>(len);

	::SetDParam(0, industry_id);
	::GetString(industry_name, STR_INDUSTRY, &industry_name[len - 1]);

	return industry_name;
}

/* static */ int32 AIIndustry::GetProduction(IndustryID industry_id, CargoID cargo_id)
{
	if (!IsValidIndustry(industry_id)) return -1;
	if (!AICargo::IsValidCargo(cargo_id)) return -1;

	const Industry *i = ::GetIndustry(industry_id);
	const IndustrySpec *indsp = ::GetIndustrySpec(i->type);

	if (indsp->produced_cargo[0] == cargo_id) return i->production_rate[0];
	if (indsp->produced_cargo[1] == cargo_id) return i->production_rate[1];
	return -1;
}

/* static */ bool AIIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id)
{
	if (!IsValidIndustry(industry_id)) return false;
	if (!AICargo::IsValidCargo(cargo_id)) return false;

	const Industry *i = ::GetIndustry(industry_id);
	const IndustrySpec *indsp = ::GetIndustrySpec(i->type);

	if (indsp->accepts_cargo[0] == cargo_id) return true;
	if (indsp->accepts_cargo[1] == cargo_id) return true;
	if (indsp->accepts_cargo[2] == cargo_id) return true;

	return false;
}

/* static */ TileIndex AIIndustry::GetLocation(IndustryID industry_id)
{
	if (!IsValidIndustry(industry_id)) return INVALID_TILE;
	return ::GetIndustry(industry_id)->xy;
}