# goal.cpp

Go to the documentation of this file.

    00001 /* $Id: goal.cpp 23950 2012-02-15 15:18:38Z 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 "company_func.h"
    00014 #include "industry.h"
    00015 #include "town.h"
    00016 #include "window_func.h"
    00017 #include "goal_base.h"
    00018 #include "core/pool_func.hpp"
    00019 #include "game/game.hpp"
    00020 #include "command_func.h"
    00021 #include "company_base.h"
    00022 #include "string_func.h"
    00023 #include "gui.h"
    00024 #include "network/network.h"
    00025
    00026
    00027 GoalID _new_goal_id;
    00028
    00029 GoalPool _goal_pool("Goal");
    00030 INSTANTIATE_POOL_METHODS(Goal)
    00031
    00032
    00043 CommandCost CmdCreateGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00044 {
    00045   if (!Goal::CanAllocateItem()) return CMD_ERROR;
    00046
    00047   GoalType type = (GoalType)GB(p1, 0, 8);
    00048   CompanyID company = (CompanyID)GB(p1, 8, 8);
    00049
    00050   if (_current_company != OWNER_DEITY) return CMD_ERROR;
    00051   if (StrEmpty(text)) return CMD_ERROR;
    00052   if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
    00053
    00054   switch (type) {
    00055     case GT_NONE:
    00056       if (p2 != 0) return CMD_ERROR;
    00057       break;
    00058
    00059     case GT_TILE:
    00060       if (!IsValidTile(p2)) return CMD_ERROR;
    00061       break;
    00062
    00063     case GT_INDUSTRY:
    00064       if (!Industry::IsValidID(p2)) return CMD_ERROR;
    00065       break;
    00066
    00067     case GT_TOWN:
    00068       if (!Town::IsValidID(p2)) return CMD_ERROR;
    00069       break;
    00070
    00071     case GT_COMPANY:
    00072       if (!Company::IsValidID(p2)) return CMD_ERROR;
    00073       break;
    00074
    00075     default: return CMD_ERROR;
    00076   }
    00077
    00078   if (flags & DC_EXEC) {
    00079     Goal *g = new Goal();
    00080     g->type = type;
    00081     g->dst = p2;
    00082     g->company = company;
    00083     g->text = strdup(text);
    00084
    00085     InvalidateWindowData(WC_GOALS_LIST, 0);
    00086
    00087     _new_goal_id = g->index;
    00088   }
    00089
    00090   return CommandCost();
    00091 }
    00092
    00102 CommandCost CmdRemoveGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00103 {
    00104   if (_current_company != OWNER_DEITY) return CMD_ERROR;
    00105   if (!Goal::IsValidID(p1)) return CMD_ERROR;
    00106
    00107   if (flags & DC_EXEC) {
    00108     Goal *g = Goal::Get(p1);
    00109     delete g;
    00110
    00111     InvalidateWindowData(WC_GOALS_LIST, 0);
    00112   }
    00113
    00114   return CommandCost();
    00115 }
    00116
    00117
    00129 CommandCost CmdGoalQuestion(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00130 {
    00131   uint16 uniqueid = (GoalType)GB(p1, 0, 16);
    00132   CompanyID company = (CompanyID)GB(p1, 16, 8);
    00133   byte type = GB(p1, 24, 8);
    00134
    00135   if (_current_company != OWNER_DEITY) return CMD_ERROR;
    00136   if (StrEmpty(text)) return CMD_ERROR;
    00137   if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
    00138   if (CountBits(p2) < 1 || CountBits(p2) > 3) return CMD_ERROR;
    00139   if (p2 >= (1 << GOAL_QUESTION_BUTTON_COUNT)) return CMD_ERROR;
    00140   if (type >= GOAL_QUESTION_TYPE_COUNT) return CMD_ERROR;
    00141
    00142   if (flags & DC_EXEC) {
    00143     if ((company != INVALID_COMPANY && company == _local_company) || (company == INVALID_COMPANY && Company::IsValidID(_local_company))) ShowGoalQuestion(uniqueid, type, p2, text);
    00144   }
    00145
    00146   return CommandCost();
    00147 }
    00148
    00158 CommandCost CmdGoalQuestionAnswer(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00159 {
    00160   if (p1 > UINT16_MAX) return CMD_ERROR;
    00161   if (p2 >= GOAL_QUESTION_BUTTON_COUNT) return CMD_ERROR;
    00162
    00163   if (_current_company == OWNER_DEITY) {
    00164     /* It has been requested to close this specific question on all clients */
    00165     if (flags & DC_EXEC) DeleteWindowById(WC_GOAL_QUESTION, p1);
    00166     return CommandCost();
    00167   }
    00168
    00169   if (_networking && _local_company == _current_company) {
    00170     /* Somebody in the same company answered the question. Close the window */
    00171     if (flags & DC_EXEC) DeleteWindowById(WC_GOAL_QUESTION, p1);
    00172     if (!_network_server) return CommandCost();
    00173   }
    00174
    00175   if (flags & DC_EXEC) {
    00176     Game::NewEvent(new ScriptEventGoalQuestionAnswer(p1, (ScriptCompany::CompanyID)(byte)_current_company, (ScriptGoal::QuestionButton)(1 << p2)));
    00177   }
    00178
    00179   return CommandCost();
    00180 }