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 "company_func.h"
00014 #include "industry.h"
00015 #include "town.h"
00016 #include "news_func.h"
00017 #include "ai/ai.hpp"
00018 #include "station_base.h"
00019 #include "cargotype.h"
00020 #include "strings_func.h"
00021 #include "window_func.h"
00022 #include "goal_base.h"
00023 #include "core/pool_func.hpp"
00024 #include "core/random_func.hpp"
00025 #include "game/game.hpp"
00026 #include "command_func.h"
00027 #include "company_base.h"
00028 #include "string_func.h"
00029 
00030 #include "table/strings.h"
00031 
00032 GoalID _new_goal_id;
00033 
00034 GoalPool _goal_pool("Goal");
00035 INSTANTIATE_POOL_METHODS(Goal)
00036 
00037 
00048 CommandCost CmdCreateGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00049 {
00050   if (!Goal::CanAllocateItem()) return CMD_ERROR;
00051 
00052   GoalType type = (GoalType)GB(p1, 0, 8);
00053   CompanyID company = (CompanyID)GB(p1, 8, 8);
00054 
00055   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00056   if (StrEmpty(text)) return CMD_ERROR;
00057   if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
00058 
00059   switch (type) {
00060     case GT_NONE:
00061       if (p2 != 0) return CMD_ERROR;
00062       break;
00063 
00064     case GT_TILE:
00065       if (!IsValidTile(p2)) return CMD_ERROR;
00066       break;
00067 
00068     case GT_INDUSTRY:
00069       if (!Industry::IsValidID(p2)) return CMD_ERROR;
00070       break;
00071 
00072     case GT_TOWN:
00073       if (!Town::IsValidID(p2)) return CMD_ERROR;
00074       break;
00075 
00076     case GT_COMPANY:
00077       if (!Company::IsValidID(p2)) return CMD_ERROR;
00078       break;
00079 
00080     default: return CMD_ERROR;
00081   }
00082 
00083   if (flags & DC_EXEC) {
00084     Goal *g = new Goal();
00085     g->type = type;
00086     g->dst = p2;
00087     g->company = company;
00088     g->text = strdup(text);
00089 
00090     InvalidateWindowData(WC_GOALS_LIST, 0);
00091 
00092     _new_goal_id = g->index;
00093   }
00094 
00095   return CommandCost();
00096 }
00097 
00107 CommandCost CmdRemoveGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00108 {
00109   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00110   if (!Goal::IsValidID(p1)) return CMD_ERROR;
00111 
00112   if (flags & DC_EXEC) {
00113     Goal *g = Goal::Get(p1);
00114     delete g;
00115 
00116     InvalidateWindowData(WC_GOALS_LIST, 0);
00117   }
00118 
00119   return CommandCost();
00120 }