You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
280 lines
7.0 KiB
C++
280 lines
7.0 KiB
C++
16 years ago
|
/* $Id$ */
|
||
|
|
||
15 years ago
|
/*
|
||
|
* 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/>.
|
||
|
*/
|
||
|
|
||
13 years ago
|
/** @file script_object.cpp Implementation of AIObject. */
|
||
16 years ago
|
|
||
16 years ago
|
#include "../../stdafx.h"
|
||
|
#include "../../script/squirrel.hpp"
|
||
14 years ago
|
#include "../../command_func.h"
|
||
|
#include "../../network/network.h"
|
||
|
#include "../../tunnelbridge.h"
|
||
16 years ago
|
|
||
13 years ago
|
#include "../../ai/ai_storage.hpp"
|
||
|
#include "../../ai/ai_instance.hpp"
|
||
|
#include "script_error.hpp"
|
||
16 years ago
|
|
||
14 years ago
|
/**
|
||
|
* Get the storage associated with the current AIInstance.
|
||
|
* @return The storage.
|
||
|
*/
|
||
16 years ago
|
static AIStorage *GetStorage()
|
||
|
{
|
||
13 years ago
|
return AIObject::GetActiveInstance()->GetStorage();
|
||
16 years ago
|
}
|
||
|
|
||
13 years ago
|
|
||
|
/* static */ AIInstance *AIObject::ActiveInstance::active = NULL;
|
||
|
|
||
|
AIObject::ActiveInstance::ActiveInstance(AIInstance *instance)
|
||
|
{
|
||
|
this->last_active = AIObject::ActiveInstance::active;
|
||
|
AIObject::ActiveInstance::active = instance;
|
||
|
}
|
||
|
|
||
|
AIObject::ActiveInstance::~ActiveInstance()
|
||
|
{
|
||
|
AIObject::ActiveInstance::active = this->last_active;
|
||
|
}
|
||
|
|
||
|
/* static */ AIInstance *AIObject::GetActiveInstance()
|
||
|
{
|
||
|
assert(AIObject::ActiveInstance::active != NULL);
|
||
|
return AIObject::ActiveInstance::active;
|
||
|
}
|
||
|
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetDoCommandDelay(uint ticks)
|
||
16 years ago
|
{
|
||
|
assert(ticks > 0);
|
||
|
GetStorage()->delay = ticks;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ uint AIObject::GetDoCommandDelay()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->delay;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetDoCommandMode(AIModeProc *proc, AIObject *instance)
|
||
16 years ago
|
{
|
||
|
GetStorage()->mode = proc;
|
||
|
GetStorage()->mode_instance = instance;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ AIModeProc *AIObject::GetDoCommandMode()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->mode;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ AIObject *AIObject::GetDoCommandModeInstance()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->mode_instance;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetDoCommandCosts(Money value)
|
||
16 years ago
|
{
|
||
|
GetStorage()->costs = CommandCost(value);
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::IncreaseDoCommandCosts(Money value)
|
||
16 years ago
|
{
|
||
|
GetStorage()->costs.AddCost(value);
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ Money AIObject::GetDoCommandCosts()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->costs.GetCost();
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetLastError(AIErrorType last_error)
|
||
16 years ago
|
{
|
||
|
GetStorage()->last_error = last_error;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ AIErrorType AIObject::GetLastError()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->last_error;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetLastCost(Money last_cost)
|
||
16 years ago
|
{
|
||
|
GetStorage()->last_cost = last_cost;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ Money AIObject::GetLastCost()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->last_cost;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetRoadType(RoadType road_type)
|
||
16 years ago
|
{
|
||
|
GetStorage()->road_type = road_type;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ RoadType AIObject::GetRoadType()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->road_type;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetRailType(RailType rail_type)
|
||
16 years ago
|
{
|
||
|
GetStorage()->rail_type = rail_type;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ RailType AIObject::GetRailType()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->rail_type;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetLastCommandRes(bool res)
|
||
16 years ago
|
{
|
||
|
GetStorage()->last_command_res = res;
|
||
|
/* Also store the results of various global variables */
|
||
|
SetNewVehicleID(_new_vehicle_id);
|
||
|
SetNewSignID(_new_sign_id);
|
||
|
SetNewTunnelEndtile(_build_tunnel_endtile);
|
||
|
SetNewGroupID(_new_group_id);
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ bool AIObject::GetLastCommandRes()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->last_command_res;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetNewVehicleID(VehicleID vehicle_id)
|
||
16 years ago
|
{
|
||
|
GetStorage()->new_vehicle_id = vehicle_id;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ VehicleID AIObject::GetNewVehicleID()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->new_vehicle_id;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetNewSignID(SignID sign_id)
|
||
16 years ago
|
{
|
||
|
GetStorage()->new_sign_id = sign_id;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ SignID AIObject::GetNewSignID()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->new_sign_id;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetNewTunnelEndtile(TileIndex tile)
|
||
16 years ago
|
{
|
||
|
GetStorage()->new_tunnel_endtile = tile;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ TileIndex AIObject::GetNewTunnelEndtile()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->new_tunnel_endtile;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetNewGroupID(GroupID group_id)
|
||
16 years ago
|
{
|
||
|
GetStorage()->new_group_id = group_id;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ GroupID AIObject::GetNewGroupID()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->new_group_id;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetAllowDoCommand(bool allow)
|
||
16 years ago
|
{
|
||
|
GetStorage()->allow_do_command = allow;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ bool AIObject::GetAllowDoCommand()
|
||
15 years ago
|
{
|
||
|
return GetStorage()->allow_do_command;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ bool AIObject::CanSuspend()
|
||
16 years ago
|
{
|
||
13 years ago
|
Squirrel *squirrel = AIObject::GetActiveInstance()->engine;
|
||
16 years ago
|
return GetStorage()->allow_do_command && squirrel->CanSuspend();
|
||
16 years ago
|
}
|
||
|
|
||
13 years ago
|
/* static */ void *&AIObject::GetEventPointer()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->event_data;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void *&AIObject::GetLogPointer()
|
||
16 years ago
|
{
|
||
|
return GetStorage()->log_data;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ void AIObject::SetCallbackVariable(int index, int value)
|
||
16 years ago
|
{
|
||
|
if ((size_t)index >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1);
|
||
|
GetStorage()->callback_value[index] = value;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ int AIObject::GetCallbackVariable(int index)
|
||
16 years ago
|
{
|
||
|
return GetStorage()->callback_value[index];
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ bool AIObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text, AISuspendCallbackProc *callback)
|
||
16 years ago
|
{
|
||
15 years ago
|
if (!AIObject::CanSuspend()) {
|
||
15 years ago
|
throw AI_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator.");
|
||
16 years ago
|
}
|
||
|
|
||
|
/* Set the default callback to return a true/false result of the DoCommand */
|
||
|
if (callback == NULL) callback = &AIInstance::DoCommandReturn;
|
||
|
|
||
15 years ago
|
/* Are we only interested in the estimate costs? */
|
||
|
bool estimate_only = GetDoCommandMode() != NULL && !GetDoCommandMode()();
|
||
16 years ago
|
|
||
14 years ago
|
#ifdef ENABLE_NETWORK
|
||
|
/* Only set p2 when the command does not come from the network. */
|
||
|
if (GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = UINT32_MAX;
|
||
|
#endif
|
||
|
|
||
15 years ago
|
/* Try to perform the command. */
|
||
|
CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, _networking ? CcAI : NULL, text, false, estimate_only);
|
||
|
|
||
|
/* We failed; set the error and bail out */
|
||
15 years ago
|
if (res.Failed()) {
|
||
15 years ago
|
SetLastError(AIError::StringToError(res.GetErrorMessage()));
|
||
16 years ago
|
return false;
|
||
|
}
|
||
|
|
||
15 years ago
|
/* No error, then clear it. */
|
||
|
SetLastError(AIError::ERR_NONE);
|
||
|
|
||
|
/* Estimates, update the cost for the estimate and be done */
|
||
|
if (estimate_only) {
|
||
16 years ago
|
IncreaseDoCommandCosts(res.GetCost());
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
/* Costs of this operation. */
|
||
|
SetLastCost(res.GetCost());
|
||
|
SetLastCommandRes(true);
|
||
16 years ago
|
|
||
15 years ago
|
if (_networking) {
|
||
|
/* Suspend the AI till the command is really executed. */
|
||
16 years ago
|
throw AI_VMSuspend(-(int)GetDoCommandDelay(), callback);
|
||
|
} else {
|
||
|
IncreaseDoCommandCosts(res.GetCost());
|
||
|
|
||
|
/* Suspend the AI player for 1+ ticks, so it simulates multiplayer. This
|
||
|
* both avoids confusion when a developer launched his AI in a
|
||
|
* multiplayer game, but also gives time for the GUI and human player
|
||
|
* to interact with the game. */
|
||
|
throw AI_VMSuspend(GetDoCommandDelay(), callback);
|
||
|
}
|
||
|
|
||
|
NOT_REACHED();
|
||
|
}
|