script_group.cpp

Go to the documentation of this file.
00001 /* $Id: script_group.cpp 23636 2011-12-19 21:06:06Z truebrain $ */
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_group.hpp"
00014 #include "script_engine.hpp"
00015 #include "../script_instance.hpp"
00016 #include "../../company_func.h"
00017 #include "../../group.h"
00018 #include "../../string_func.h"
00019 #include "../../strings_func.h"
00020 #include "../../autoreplace_func.h"
00021 #include "../../settings_func.h"
00022 #include "table/strings.h"
00023 
00024 /* static */ bool ScriptGroup::IsValidGroup(GroupID group_id)
00025 {
00026   const Group *g = ::Group::GetIfValid(group_id);
00027   return g != NULL && g->owner == ScriptObject::GetCompany();
00028 }
00029 
00030 /* static */ ScriptGroup::GroupID ScriptGroup::CreateGroup(ScriptVehicle::VehicleType vehicle_type)
00031 {
00032   if (!ScriptObject::DoCommand(0, (::VehicleType)vehicle_type, 0, CMD_CREATE_GROUP, NULL, &ScriptInstance::DoCommandReturnGroupID)) return GROUP_INVALID;
00033 
00034   /* In case of test-mode, we return GroupID 0 */
00035   return (ScriptGroup::GroupID)0;
00036 }
00037 
00038 /* static */ bool ScriptGroup::DeleteGroup(GroupID group_id)
00039 {
00040   EnforcePrecondition(false, IsValidGroup(group_id));
00041 
00042   return ScriptObject::DoCommand(0, group_id, 0, CMD_DELETE_GROUP);
00043 }
00044 
00045 /* static */ ScriptVehicle::VehicleType ScriptGroup::GetVehicleType(GroupID group_id)
00046 {
00047   if (!IsValidGroup(group_id)) return ScriptVehicle::VT_INVALID;
00048 
00049   return (ScriptVehicle::VehicleType)((::VehicleType)::Group::Get(group_id)->vehicle_type);
00050 }
00051 
00052 /* static */ bool ScriptGroup::SetName(GroupID group_id, Text *name)
00053 {
00054   CCountedPtr<Text> counter(name);
00055 
00056   EnforcePrecondition(false, IsValidGroup(group_id));
00057   EnforcePrecondition(false, name != NULL);
00058   const char *text = name->GetEncodedText();
00059   EnforcePrecondition(false, !::StrEmpty(text));
00060   EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_GROUP_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
00061 
00062   return ScriptObject::DoCommand(0, group_id, 0, CMD_RENAME_GROUP, text);
00063 }
00064 
00065 /* static */ char *ScriptGroup::GetName(GroupID group_id)
00066 {
00067   if (!IsValidGroup(group_id)) return NULL;
00068 
00069   static const int len = 64;
00070   char *group_name = MallocT<char>(len);
00071 
00072 	::SetDParam(0, group_id);
00073   ::GetString(group_name, STR_GROUP_NAME, &group_name[len - 1]);
00074   return group_name;
00075 }
00076 
00077 /* static */ bool ScriptGroup::EnableAutoReplaceProtection(GroupID group_id, bool enable)
00078 {
00079   EnforcePrecondition(false, IsValidGroup(group_id));
00080 
00081   return ScriptObject::DoCommand(0, group_id, enable ? 1 : 0, CMD_SET_GROUP_REPLACE_PROTECTION);
00082 }
00083 
00084 /* static */ bool ScriptGroup::GetAutoReplaceProtection(GroupID group_id)
00085 {
00086   if (!IsValidGroup(group_id)) return false;
00087 
00088   return ::Group::Get(group_id)->replace_protection;
00089 }
00090 
00091 /* static */ int32 ScriptGroup::GetNumEngines(GroupID group_id, EngineID engine_id)
00092 {
00093   if (!IsValidGroup(group_id) && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
00094 
00095   return GetGroupNumEngines(ScriptObject::GetCompany(), group_id, engine_id);
00096 }
00097 
00098 /* static */ bool ScriptGroup::MoveVehicle(GroupID group_id, VehicleID vehicle_id)
00099 {
00100   EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT);
00101   EnforcePrecondition(false, ScriptVehicle::IsValidVehicle(vehicle_id));
00102 
00103   return ScriptObject::DoCommand(0, group_id, vehicle_id, CMD_ADD_VEHICLE_GROUP);
00104 }
00105 
00106 /* static */ bool ScriptGroup::EnableWagonRemoval(bool enable_removal)
00107 {
00108   if (HasWagonRemoval() == enable_removal) return true;
00109 
00110   return ScriptObject::DoCommand(0, ::GetCompanySettingIndex("company.renew_keep_length"), enable_removal ? 1 : 0, CMD_CHANGE_COMPANY_SETTING);
00111 }
00112 
00113 /* static */ bool ScriptGroup::HasWagonRemoval()
00114 {
00115   return ::Company::Get(ScriptObject::GetCompany())->settings.renew_keep_length;
00116 }
00117 
00118 /* static */ bool ScriptGroup::SetAutoReplace(GroupID group_id, EngineID engine_id_old, EngineID engine_id_new)
00119 {
00120   EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL);
00121   EnforcePrecondition(false, ScriptEngine::IsBuildable(engine_id_new));
00122 
00123   return ScriptObject::DoCommand(0, group_id << 16, (engine_id_new << 16) | engine_id_old, CMD_SET_AUTOREPLACE);
00124 }
00125 
00126 /* static */ EngineID ScriptGroup::GetEngineReplacement(GroupID group_id, EngineID engine_id)
00127 {
00128   if (!IsValidGroup(group_id) && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return ::INVALID_ENGINE;
00129 
00130   return ::EngineReplacementForCompany(Company::Get(ScriptObject::GetCompany()), engine_id, group_id);
00131 }
00132 
00133 /* static */ bool ScriptGroup::StopAutoReplace(GroupID group_id, EngineID engine_id)
00134 {
00135   EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL);
00136 
00137   return ScriptObject::DoCommand(0, group_id << 16, (::INVALID_ENGINE << 16) | engine_id, CMD_SET_AUTOREPLACE);
00138 }