script_goal.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_goal.hpp"
00014 #include "script_error.hpp"
00015 #include "script_industry.hpp"
00016 #include "script_map.hpp"
00017 #include "script_town.hpp"
00018 #include "../script_instance.hpp"
00019 #include "../../command_type.h"
00020 #include "../../settings_type.h"
00021 #include "../../openttd.h"
00022 #include "../../network/network.h"
00023 #include "../../goal_base.h"
00024 #include "../../string_func.h"
00025 
00026 /* static */ bool ScriptGoal::IsValidGoal(GoalID goal_id)
00027 {
00028   return ::Goal::IsValidID(goal_id);
00029 }
00030 
00031 /* static */ ScriptGoal::GoalID ScriptGoal::New(ScriptCompany::CompanyID company, Text *goal, GoalType type, uint32 destination)
00032 {
00033   CCountedPtr<Text> counter(goal);
00034 
00035   EnforcePrecondition(GOAL_INVALID, goal != NULL);
00036   EnforcePrecondition(GOAL_INVALID, !StrEmpty(goal->GetEncodedText()));
00037   EnforcePrecondition(GOAL_INVALID, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
00038   EnforcePrecondition(GOAL_INVALID, (type == GT_NONE && destination == 0) || (type == GT_TILE && ScriptMap::IsValidTile(destination)) || (type == GT_INDUSTRY && ScriptIndustry::IsValidIndustry(destination)) || (type == GT_TOWN && ScriptTown::IsValidTown(destination)) || (type == GT_COMPANY && ScriptCompany::ResolveCompanyID((ScriptCompany::CompanyID)destination) != ScriptCompany::COMPANY_INVALID));
00039 
00040   uint8 c = company;
00041   if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
00042 
00043   if (!ScriptObject::DoCommand(0, type | (c << 8), destination, CMD_CREATE_GOAL, goal->GetEncodedText(), &ScriptInstance::DoCommandReturnGoalID)) return GOAL_INVALID;
00044 
00045   /* In case of test-mode, we return GoalID 0 */
00046   return (ScriptGoal::GoalID)0;
00047 }
00048 
00049 /* static */ bool ScriptGoal::Remove(GoalID goal_id)
00050 {
00051   EnforcePrecondition(false, IsValidGoal(goal_id));
00052 
00053   return ScriptObject::DoCommand(0, goal_id, 0, CMD_REMOVE_GOAL);
00054 }