# ai_object.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "../../stdafx.h"
    00013 #include "../../script/squirrel.hpp"
    00014 #include "../../command_func.h"
    00015 #include "../../company_base.h"
    00016 #include "../../company_func.h"
    00017 #include "../../network/network.h"
    00018 #include "../../tunnelbridge.h"
    00019
    00020 #include "../ai_storage.hpp"
    00021 #include "../ai_instance.hpp"
    00022 #include "ai_error.hpp"
    00023
    00024 static AIStorage *GetStorage()
    00025 {
    00026   return AIInstance::GetStorage();
    00027 }
    00028
    00029 void AIObject::SetDoCommandDelay(uint ticks)
    00030 {
    00031   assert(ticks > 0);
    00032   GetStorage()->delay = ticks;
    00033 }
    00034
    00035 uint AIObject::GetDoCommandDelay()
    00036 {
    00037   return GetStorage()->delay;
    00038 }
    00039
    00040 void AIObject::SetDoCommandMode(AIModeProc *proc, AIObject *instance)
    00041 {
    00042   GetStorage()->mode = proc;
    00043   GetStorage()->mode_instance = instance;
    00044 }
    00045
    00046 AIModeProc *AIObject::GetDoCommandMode()
    00047 {
    00048   return GetStorage()->mode;
    00049 }
    00050
    00051 AIObject *AIObject::GetDoCommandModeInstance()
    00052 {
    00053   return GetStorage()->mode_instance;
    00054 }
    00055
    00056 void AIObject::SetDoCommandCosts(Money value)
    00057 {
    00058   GetStorage()->costs = CommandCost(value);
    00059 }
    00060
    00061 void AIObject::IncreaseDoCommandCosts(Money value)
    00062 {
    00063   GetStorage()->costs.AddCost(value);
    00064 }
    00065
    00066 Money AIObject::GetDoCommandCosts()
    00067 {
    00068   return GetStorage()->costs.GetCost();
    00069 }
    00070
    00071 void AIObject::SetLastError(AIErrorType last_error)
    00072 {
    00073   GetStorage()->last_error = last_error;
    00074 }
    00075
    00076 AIErrorType AIObject::GetLastError()
    00077 {
    00078   return GetStorage()->last_error;
    00079 }
    00080
    00081 void AIObject::SetLastCost(Money last_cost)
    00082 {
    00083   GetStorage()->last_cost = last_cost;
    00084 }
    00085
    00086 Money AIObject::GetLastCost()
    00087 {
    00088   return GetStorage()->last_cost;
    00089 }
    00090
    00091 void AIObject::SetRoadType(RoadType road_type)
    00092 {
    00093   GetStorage()->road_type = road_type;
    00094 }
    00095
    00096 RoadType AIObject::GetRoadType()
    00097 {
    00098   return GetStorage()->road_type;
    00099 }
    00100
    00101 void AIObject::SetRailType(RailType rail_type)
    00102 {
    00103   GetStorage()->rail_type = rail_type;
    00104 }
    00105
    00106 RailType AIObject::GetRailType()
    00107 {
    00108   return GetStorage()->rail_type;
    00109 }
    00110
    00111 void AIObject::SetLastCommandRes(bool res)
    00112 {
    00113   GetStorage()->last_command_res = res;
    00114   /* Also store the results of various global variables */
    00115   SetNewVehicleID(_new_vehicle_id);
    00116   SetNewSignID(_new_sign_id);
    00117   SetNewTunnelEndtile(_build_tunnel_endtile);
    00118   SetNewGroupID(_new_group_id);
    00119 }
    00120
    00121 bool AIObject::GetLastCommandRes()
    00122 {
    00123   return GetStorage()->last_command_res;
    00124 }
    00125
    00126 void AIObject::SetNewVehicleID(VehicleID vehicle_id)
    00127 {
    00128   GetStorage()->new_vehicle_id = vehicle_id;
    00129 }
    00130
    00131 VehicleID AIObject::GetNewVehicleID()
    00132 {
    00133   return GetStorage()->new_vehicle_id;
    00134 }
    00135
    00136 void AIObject::SetNewSignID(SignID sign_id)
    00137 {
    00138   GetStorage()->new_sign_id = sign_id;
    00139 }
    00140
    00141 SignID AIObject::GetNewSignID()
    00142 {
    00143   return GetStorage()->new_sign_id;
    00144 }
    00145
    00146 void AIObject::SetNewTunnelEndtile(TileIndex tile)
    00147 {
    00148   GetStorage()->new_tunnel_endtile = tile;
    00149 }
    00150
    00151 TileIndex AIObject::GetNewTunnelEndtile()
    00152 {
    00153   return GetStorage()->new_tunnel_endtile;
    00154 }
    00155
    00156 void AIObject::SetNewGroupID(GroupID group_id)
    00157 {
    00158   GetStorage()->new_group_id = group_id;
    00159 }
    00160
    00161 GroupID AIObject::GetNewGroupID()
    00162 {
    00163   return GetStorage()->new_group_id;
    00164 }
    00165
    00166 void AIObject::SetAllowDoCommand(bool allow)
    00167 {
    00168   GetStorage()->allow_do_command = allow;
    00169 }
    00170
    00171 bool AIObject::GetAllowDoCommand()
    00172 {
    00173   return GetStorage()->allow_do_command;
    00174 }
    00175
    00176 bool AIObject::CanSuspend()
    00177 {
    00178   Squirrel *squirrel = Company::Get(_current_company)->ai_instance->engine;
    00179   return GetStorage()->allow_do_command && squirrel->CanSuspend();
    00180 }
    00181
    00182 void *&AIObject::GetEventPointer()
    00183 {
    00184   return GetStorage()->event_data;
    00185 }
    00186
    00187 void *&AIObject::GetLogPointer()
    00188 {
    00189   return GetStorage()->log_data;
    00190 }
    00191
    00192 void AIObject::SetCallbackVariable(int index, int value)
    00193 {
    00194   if ((size_t)index >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1);
    00195   GetStorage()->callback_value[index] = value;
    00196 }
    00197
    00198 int AIObject::GetCallbackVariable(int index)
    00199 {
    00200   return GetStorage()->callback_value[index];
    00201 }
    00202
    00203 bool AIObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text, AISuspendCallbackProc *callback)
    00204 {
    00205   if (!AIObject::CanSuspend()) {
    00206     throw AI_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator.");
    00207   }
    00208
    00209   /* Set the default callback to return a true/false result of the DoCommand */
    00210   if (callback == NULL) callback = &AIInstance::DoCommandReturn;
    00211
    00212   /* Are we only interested in the estimate costs? */
    00213   bool estimate_only = GetDoCommandMode() != NULL && !GetDoCommandMode()();
    00214
    00215 #ifdef ENABLE_NETWORK
    00216   /* Only set p2 when the command does not come from the network. */
    00217   if (GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = UINT32_MAX;
    00218 #endif
    00219
    00220   /* Try to perform the command. */
    00221   CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, _networking ? CcAI : NULL, text, false, estimate_only);
    00222
    00223   /* We failed; set the error and bail out */
    00224   if (res.Failed()) {
    00225     SetLastError(AIError::StringToError(res.GetErrorMessage()));
    00226     return false;
    00227   }
    00228
    00229   /* No error, then clear it. */
    00230   SetLastError(AIError::ERR_NONE);
    00231
    00232   /* Estimates, update the cost for the estimate and be done */
    00233   if (estimate_only) {
    00234     IncreaseDoCommandCosts(res.GetCost());
    00235     return true;
    00236   }
    00237
    00238   /* Costs of this operation. */
    00239   SetLastCost(res.GetCost());
    00240   SetLastCommandRes(true);
    00241
    00242   if (_networking) {
    00243     /* Suspend the AI till the command is really executed. */
    00244     throw AI_VMSuspend(-(int)GetDoCommandDelay(), callback);
    00245   } else {
    00246     IncreaseDoCommandCosts(res.GetCost());
    00247
    00248     /* Suspend the AI player for 1+ ticks, so it simulates multiplayer. This
    00249      *  both avoids confusion when a developer launched his AI in a
    00250      *  multiplayer game, but also gives time for the GUI and human player
    00251      *  to interact with the game. */
    00252     throw AI_VMSuspend(GetDoCommandDelay(), callback);
    00253   }
    00254
    00255   NOT_REACHED();
    00256 }