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 #include "gui.h"
00030 #include "network/network.h"
00031 
00032 #include "table/strings.h"
00033 
00034 GoalID _new_goal_id;
00035 
00036 GoalPool _goal_pool("Goal");
00037 INSTANTIATE_POOL_METHODS(Goal)
00038 
00039 
00050 CommandCost CmdCreateGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00051 {
00052   if (!Goal::CanAllocateItem()) return CMD_ERROR;
00053 
00054   GoalType type = (GoalType)GB(p1, 0, 8);
00055   CompanyID company = (CompanyID)GB(p1, 8, 8);
00056 
00057   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00058   if (StrEmpty(text)) return CMD_ERROR;
00059   if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
00060 
00061   switch (type) {
00062     case GT_NONE:
00063       if (p2 != 0) return CMD_ERROR;
00064       break;
00065 
00066     case GT_TILE:
00067       if (!IsValidTile(p2)) return CMD_ERROR;
00068       break;
00069 
00070     case GT_INDUSTRY:
00071       if (!Industry::IsValidID(p2)) return CMD_ERROR;
00072       break;
00073 
00074     case GT_TOWN:
00075       if (!Town::IsValidID(p2)) return CMD_ERROR;
00076       break;
00077 
00078     case GT_COMPANY:
00079       if (!Company::IsValidID(p2)) return CMD_ERROR;
00080       break;
00081 
00082     default: return CMD_ERROR;
00083   }
00084 
00085   if (flags & DC_EXEC) {
00086     Goal *g = new Goal();
00087     g->type = type;
00088     g->dst = p2;
00089     g->company = company;
00090     g->text = strdup(text);
00091 
00092     InvalidateWindowData(WC_GOALS_LIST, 0);
00093 
00094     _new_goal_id = g->index;
00095   }
00096 
00097   return CommandCost();
00098 }
00099 
00109 CommandCost CmdRemoveGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00110 {
00111   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00112   if (!Goal::IsValidID(p1)) return CMD_ERROR;
00113 
00114   if (flags & DC_EXEC) {
00115     Goal *g = Goal::Get(p1);
00116     delete g;
00117 
00118     InvalidateWindowData(WC_GOALS_LIST, 0);
00119   }
00120 
00121   return CommandCost();
00122 }
00123 
00124 
00136 CommandCost CmdGoalQuestion(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00137 {
00138   uint16 uniqueid = (GoalType)GB(p1, 0, 16);
00139   CompanyID company = (CompanyID)GB(p1, 16, 8);
00140 
00141   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00142   if (StrEmpty(text)) return CMD_ERROR;
00143   if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
00144   if (CountBits(p2) < 1 || CountBits(p2) > 3) return CMD_ERROR;
00145 
00146   if (flags & DC_EXEC) {
00147     if (company == _local_company || (company == INVALID_COMPANY && Company::IsValidID(_local_company))) ShowGoalQuestion(uniqueid, p2, text);
00148   }
00149 
00150   return CommandCost();
00151 }
00152 
00162 CommandCost CmdGoalQuestionAnswer(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00163 {
00164   if (p1 > UINT16_MAX) return CMD_ERROR;
00165   if (p2 >= GOAL_QUESTION_BUTTON_COUNT) return CMD_ERROR;
00166 
00167   if (_current_company == OWNER_DEITY) {
00168     /* It has been requested to close this specific question on all clients */
00169     if (flags & DC_EXEC) DeleteWindowById(WC_GOAL_QUESTION, p1);
00170     return CommandCost();
00171   }
00172 
00173   if (_networking && _local_company == _current_company) {
00174     /* Somebody in the same company answered the question. Close the window */
00175     if (flags & DC_EXEC) DeleteWindowById(WC_GOAL_QUESTION, p1);
00176     if (!_network_server) return CommandCost();
00177   }
00178 
00179   if (flags & DC_EXEC) {
00180     Game::NewEvent(new ScriptEventGoalQuestionAnswer(p1, (ScriptCompany::CompanyID)(byte)_current_company, (ScriptGoal::QuestionButton)(1 << p2)));
00181   }
00182 
00183   return CommandCost();
00184 }