script_event_types.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_event_types.hpp"
00014 #include "script_vehicle.hpp"
00015 #include "../../command_type.h"
00016 #include "../../strings_func.h"
00017 #include "../../settings_type.h"
00018 #include "../../engine_base.h"
00019 #include "../../articulated_vehicles.h"
00020 #include "table/strings.h"
00021 
00022 bool ScriptEventEnginePreview::IsEngineValid() const
00023 {
00024   const Engine *e = ::Engine::GetIfValid(this->engine);
00025   return e != NULL && e->IsEnabled();
00026 }
00027 
00028 char *ScriptEventEnginePreview::GetName()
00029 {
00030   if (!this->IsEngineValid()) return NULL;
00031   static const int len = 64;
00032   char *engine_name = MallocT<char>(len);
00033 
00034 	::SetDParam(0, this->engine);
00035   ::GetString(engine_name, STR_ENGINE_NAME, &engine_name[len - 1]);
00036   return engine_name;
00037 }
00038 
00039 CargoID ScriptEventEnginePreview::GetCargoType()
00040 {
00041   if (!this->IsEngineValid()) return CT_INVALID;
00042   CargoArray cap = ::GetCapacityOfArticulatedParts(this->engine);
00043 
00044   CargoID most_cargo = CT_INVALID;
00045   uint amount = 0;
00046   for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00047     if (cap[cid] > amount) {
00048       amount = cap[cid];
00049       most_cargo = cid;
00050     }
00051   }
00052 
00053   return most_cargo;
00054 }
00055 
00056 int32 ScriptEventEnginePreview::GetCapacity()
00057 {
00058   if (!this->IsEngineValid()) return -1;
00059   const Engine *e = ::Engine::Get(this->engine);
00060   switch (e->type) {
00061     case VEH_ROAD:
00062     case VEH_TRAIN: {
00063       CargoArray capacities = GetCapacityOfArticulatedParts(this->engine);
00064       for (CargoID c = 0; c < NUM_CARGO; c++) {
00065         if (capacities[c] == 0) continue;
00066         return capacities[c];
00067       }
00068       return -1;
00069     }
00070 
00071     case VEH_SHIP:
00072     case VEH_AIRCRAFT:
00073       return e->GetDisplayDefaultCapacity();
00074 
00075     default: NOT_REACHED();
00076   }
00077 }
00078 
00079 int32 ScriptEventEnginePreview::GetMaxSpeed()
00080 {
00081   if (!this->IsEngineValid()) return -1;
00082   const Engine *e = ::Engine::Get(this->engine);
00083   int32 max_speed = e->GetDisplayMaxSpeed(); // km-ish/h
00084   if (e->type == VEH_AIRCRAFT) max_speed /= _settings_game.vehicle.plane_speed;
00085   return max_speed;
00086 }
00087 
00088 Money ScriptEventEnginePreview::GetPrice()
00089 {
00090   if (!this->IsEngineValid()) return -1;
00091   return ::Engine::Get(this->engine)->GetCost();
00092 }
00093 
00094 Money ScriptEventEnginePreview::GetRunningCost()
00095 {
00096   if (!this->IsEngineValid()) return -1;
00097   return ::Engine::Get(this->engine)->GetRunningCost();
00098 }
00099 
00100 int32 ScriptEventEnginePreview::GetVehicleType()
00101 {
00102   if (!this->IsEngineValid()) return ScriptVehicle::VT_INVALID;
00103   switch (::Engine::Get(this->engine)->type) {
00104     case VEH_ROAD:     return ScriptVehicle::VT_ROAD;
00105     case VEH_TRAIN:    return ScriptVehicle::VT_RAIL;
00106     case VEH_SHIP:     return ScriptVehicle::VT_WATER;
00107     case VEH_AIRCRAFT: return ScriptVehicle::VT_AIR;
00108     default: NOT_REACHED();
00109   }
00110 }
00111 
00112 bool ScriptEventEnginePreview::AcceptPreview()
00113 {
00114   if (!this->IsEngineValid()) return false;
00115   return ScriptObject::DoCommand(0, this->engine, 0, CMD_WANT_ENGINE_PREVIEW);
00116 }
00117 
00118 bool ScriptEventCompanyAskMerger::AcceptMerger()
00119 {
00120   return ScriptObject::DoCommand(0, this->owner, 0, CMD_BUY_COMPANY);
00121 }