2009-01-12 18:11:45 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-30 00:07:38 +01:00
|
|
|
/** @file script_industry.cpp Implementation of AIIndustry. */
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2011-01-22 11:33:16 +01:00
|
|
|
#include "../../stdafx.h"
|
2011-11-30 00:07:38 +01:00
|
|
|
#include "script_industry.hpp"
|
|
|
|
#include "script_cargo.hpp"
|
|
|
|
#include "script_map.hpp"
|
2009-01-12 18:11:45 +01:00
|
|
|
#include "../../industry.h"
|
|
|
|
#include "../../strings_func.h"
|
2009-06-24 19:39:54 +02:00
|
|
|
#include "../../station_base.h"
|
2010-08-07 22:51:53 +02:00
|
|
|
#include "../../newgrf_industries.h"
|
2009-01-12 18:11:45 +01:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
|
|
|
/* static */ int32 AIIndustry::GetIndustryCount()
|
|
|
|
{
|
2009-05-22 17:23:47 +02:00
|
|
|
return (int32)::Industry::GetNumItems();
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool AIIndustry::IsValidIndustry(IndustryID industry_id)
|
|
|
|
{
|
2009-05-17 03:00:56 +02:00
|
|
|
return ::Industry::IsValidID(industry_id);
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2010-05-09 17:59:30 +02:00
|
|
|
/* static */ IndustryID AIIndustry::GetIndustryID(TileIndex tile)
|
|
|
|
{
|
|
|
|
if (!::IsValidTile(tile) || !::IsTileType(tile, MP_INDUSTRY)) return INVALID_INDUSTRY;
|
|
|
|
return ::GetIndustryIndex(tile);
|
|
|
|
}
|
|
|
|
|
2009-01-17 16:31:30 +01:00
|
|
|
/* static */ char *AIIndustry::GetName(IndustryID industry_id)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return NULL;
|
|
|
|
static const int len = 64;
|
|
|
|
char *industry_name = MallocT<char>(len);
|
|
|
|
|
|
|
|
::SetDParam(0, industry_id);
|
2009-07-20 13:21:57 +02:00
|
|
|
::GetString(industry_name, STR_INDUSTRY_NAME, &industry_name[len - 1]);
|
2009-01-12 18:11:45 +01:00
|
|
|
|
|
|
|
return industry_name;
|
|
|
|
}
|
|
|
|
|
2010-08-07 22:51:53 +02:00
|
|
|
/* static */ AIIndustry::CargoAcceptState AIIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2010-08-07 22:51:53 +02:00
|
|
|
if (!IsValidIndustry(industry_id)) return CAS_NOT_ACCEPTED;
|
|
|
|
if (!AICargo::IsValidCargo(cargo_id)) return CAS_NOT_ACCEPTED;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2010-08-07 22:51:53 +02:00
|
|
|
Industry *i = ::Industry::Get(industry_id);
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2009-01-23 23:09:31 +01:00
|
|
|
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
|
2010-08-07 22:51:53 +02:00
|
|
|
if (i->accepts_cargo[j] == cargo_id) {
|
|
|
|
if (IndustryTemporarilyRefusesCargo(i, cargo_id)) return CAS_TEMP_REFUSED;
|
|
|
|
return CAS_ACCEPTED;
|
|
|
|
}
|
2009-01-23 23:09:31 +01:00
|
|
|
}
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2010-08-07 22:51:53 +02:00
|
|
|
return CAS_NOT_ACCEPTED;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ int32 AIIndustry::GetStockpiledCargo(IndustryID industry_id, CargoID cargo_id)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return -1;
|
|
|
|
if (!AICargo::IsValidCargo(cargo_id)) return -1;
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
Industry *ind = ::Industry::Get(industry_id);
|
2009-01-12 18:11:45 +01:00
|
|
|
for (uint i = 0; i < lengthof(ind->accepts_cargo); i++) {
|
|
|
|
CargoID cid = ind->accepts_cargo[i];
|
|
|
|
if (cid == cargo_id) {
|
|
|
|
return ind->incoming_cargo_waiting[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ int32 AIIndustry::GetLastMonthProduction(IndustryID industry_id, CargoID cargo_id)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return -1;
|
|
|
|
if (!AICargo::IsValidCargo(cargo_id)) return -1;
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
const Industry *i = ::Industry::Get(industry_id);
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2009-01-23 23:09:31 +01:00
|
|
|
for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
|
|
|
|
if (i->produced_cargo[j] == cargo_id) return i->last_month_production[j];
|
|
|
|
}
|
2009-01-12 18:11:45 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ int32 AIIndustry::GetLastMonthTransported(IndustryID industry_id, CargoID cargo_id)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return -1;
|
|
|
|
if (!AICargo::IsValidCargo(cargo_id)) return -1;
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
const Industry *i = ::Industry::Get(industry_id);
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2009-01-23 23:09:31 +01:00
|
|
|
for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
|
|
|
|
if (i->produced_cargo[j] == cargo_id) return i->last_month_transported[j];
|
|
|
|
}
|
2009-01-12 18:11:45 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-08-27 15:50:07 +02:00
|
|
|
/* static */ int32 AIIndustry::GetLastMonthTransportedPercentage(IndustryID industry_id, CargoID cargo_id)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return -1;
|
|
|
|
if (!AICargo::IsValidCargo(cargo_id)) return -1;
|
|
|
|
|
|
|
|
const Industry *i = ::Industry::Get(industry_id);
|
|
|
|
|
|
|
|
for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
|
|
|
|
if (i->produced_cargo[j] == cargo_id) return ::ToPercent8(i->last_month_pct_transported[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-12 18:11:45 +01:00
|
|
|
/* static */ TileIndex AIIndustry::GetLocation(IndustryID industry_id)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return INVALID_TILE;
|
|
|
|
|
2010-01-04 19:21:07 +01:00
|
|
|
return ::Industry::Get(industry_id)->location.tile;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ int32 AIIndustry::GetAmountOfStationsAround(IndustryID industry_id)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return -1;
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
Industry *ind = ::Industry::Get(industry_id);
|
2009-01-13 21:43:53 +01:00
|
|
|
StationList stations;
|
2010-01-04 19:21:07 +01:00
|
|
|
::FindStationsAroundTiles(ind->location, &stations);
|
2009-01-13 21:43:53 +01:00
|
|
|
return (int32)stations.Length();
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ int32 AIIndustry::GetDistanceManhattanToTile(IndustryID industry_id, TileIndex tile)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return -1;
|
|
|
|
|
|
|
|
return AIMap::DistanceManhattan(tile, GetLocation(industry_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ int32 AIIndustry::GetDistanceSquareToTile(IndustryID industry_id, TileIndex tile)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return -1;
|
|
|
|
|
|
|
|
return AIMap::DistanceSquare(tile, GetLocation(industry_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool AIIndustry::IsBuiltOnWater(IndustryID industry_id)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return false;
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
return (::GetIndustrySpec(::Industry::Get(industry_id)->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2009-02-14 22:09:48 +01:00
|
|
|
/* static */ bool AIIndustry::HasHeliport(IndustryID industry_id)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return false;
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
return (::GetIndustrySpec(::Industry::Get(industry_id)->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2009-02-14 22:09:48 +01:00
|
|
|
/* static */ TileIndex AIIndustry::GetHeliportLocation(IndustryID industry_id)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return INVALID_TILE;
|
2009-02-14 22:09:48 +01:00
|
|
|
if (!HasHeliport(industry_id)) return INVALID_TILE;
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
const Industry *ind = ::Industry::Get(industry_id);
|
2010-01-04 19:30:10 +01:00
|
|
|
TILE_AREA_LOOP(tile_cur, ind->location) {
|
2009-02-14 22:09:48 +01:00
|
|
|
if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
|
|
|
|
return tile_cur;
|
|
|
|
}
|
2009-07-26 23:50:30 +02:00
|
|
|
}
|
2009-02-14 22:09:48 +01:00
|
|
|
|
|
|
|
return INVALID_TILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool AIIndustry::HasDock(IndustryID industry_id)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return false;
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
return (::GetIndustrySpec(::Industry::Get(industry_id)->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
|
2009-02-14 22:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ TileIndex AIIndustry::GetDockLocation(IndustryID industry_id)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return INVALID_TILE;
|
|
|
|
if (!HasDock(industry_id)) return INVALID_TILE;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
const Industry *ind = ::Industry::Get(industry_id);
|
2010-01-04 19:30:10 +01:00
|
|
|
TILE_AREA_LOOP(tile_cur, ind->location) {
|
2009-01-23 23:36:17 +01:00
|
|
|
if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
|
|
|
|
return tile_cur;
|
|
|
|
}
|
2009-07-26 23:50:30 +02:00
|
|
|
}
|
2009-01-23 23:36:17 +01:00
|
|
|
|
|
|
|
return INVALID_TILE;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ IndustryType AIIndustry::GetIndustryType(IndustryID industry_id)
|
|
|
|
{
|
|
|
|
if (!IsValidIndustry(industry_id)) return INVALID_INDUSTRYTYPE;
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
return ::Industry::Get(industry_id)->type;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|