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 "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     g->progress = NULL;
00085     g->completed = false;
00086 
00087     InvalidateWindowData(WC_GOALS_LIST, 0);
00088 
00089     _new_goal_id = g->index;
00090   }
00091 
00092   return CommandCost();
00093 }
00094 
00104 CommandCost CmdRemoveGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00105 {
00106   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00107   if (!Goal::IsValidID(p1)) return CMD_ERROR;
00108 
00109   if (flags & DC_EXEC) {
00110     Goal *g = Goal::Get(p1);
00111     delete g;
00112 
00113     InvalidateWindowData(WC_GOALS_LIST, 0);
00114   }
00115 
00116   return CommandCost();
00117 }
00118 
00128 CommandCost CmdSetGoalText(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00129 {
00130   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00131   if (!Goal::IsValidID(p1)) return CMD_ERROR;
00132   if (StrEmpty(text)) return CMD_ERROR;
00133 
00134   if (flags & DC_EXEC) {
00135     Goal *g = Goal::Get(p1);
00136     free(g->text);
00137     g->text = strdup(text);
00138 
00139     InvalidateWindowData(WC_GOALS_LIST, 0);
00140   }
00141 
00142   return CommandCost();
00143 }
00144 
00154 CommandCost CmdSetGoalProgress(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00155 {
00156   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00157   if (!Goal::IsValidID(p1)) return CMD_ERROR;
00158 
00159   if (flags & DC_EXEC) {
00160     Goal *g = Goal::Get(p1);
00161     free(g->progress);
00162     if (StrEmpty(text)) {
00163       g->progress = NULL;
00164     } else {
00165       g->progress = strdup(text);
00166     }
00167 
00168     InvalidateWindowData(WC_GOALS_LIST, 0);
00169   }
00170 
00171   return CommandCost();
00172 }
00173 
00183 CommandCost CmdSetGoalCompleted(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00184 {
00185   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00186   if (!Goal::IsValidID(p1)) return CMD_ERROR;
00187 
00188   if (flags & DC_EXEC) {
00189     Goal *g = Goal::Get(p1);
00190     g->completed = p2 == 1;
00191 
00192     InvalidateWindowData(WC_GOALS_LIST, 0);
00193   }
00194 
00195   return CommandCost();
00196 }
00197 
00209 CommandCost CmdGoalQuestion(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00210 {
00211   uint16 uniqueid = (GoalType)GB(p1, 0, 16);
00212   CompanyID company = (CompanyID)GB(p1, 16, 8);
00213   byte type = GB(p1, 24, 8);
00214 
00215   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00216   if (StrEmpty(text)) return CMD_ERROR;
00217   if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
00218   if (CountBits(p2) < 1 || CountBits(p2) > 3) return CMD_ERROR;
00219   if (p2 >= (1 << GOAL_QUESTION_BUTTON_COUNT)) return CMD_ERROR;
00220   if (type >= GOAL_QUESTION_TYPE_COUNT) return CMD_ERROR;
00221 
00222   if (flags & DC_EXEC) {
00223     if ((company != INVALID_COMPANY && company == _local_company) || (company == INVALID_COMPANY && Company::IsValidID(_local_company))) ShowGoalQuestion(uniqueid, type, p2, text);
00224   }
00225 
00226   return CommandCost();
00227 }
00228 
00238 CommandCost CmdGoalQuestionAnswer(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00239 {
00240   if (p1 > UINT16_MAX) return CMD_ERROR;
00241   if (p2 >= GOAL_QUESTION_BUTTON_COUNT) return CMD_ERROR;
00242 
00243   if (_current_company == OWNER_DEITY) {
00244     /* It has been requested to close this specific question on all clients */
00245     if (flags & DC_EXEC) DeleteWindowById(WC_GOAL_QUESTION, p1);
00246     return CommandCost();
00247   }
00248 
00249   if (_networking && _local_company == _current_company) {
00250     /* Somebody in the same company answered the question. Close the window */
00251     if (flags & DC_EXEC) DeleteWindowById(WC_GOAL_QUESTION, p1);
00252     if (!_network_server) return CommandCost();
00253   }
00254 
00255   if (flags & DC_EXEC) {
00256     Game::NewEvent(new ScriptEventGoalQuestionAnswer(p1, (ScriptCompany::CompanyID)(byte)_current_company, (ScriptGoal::QuestionButton)(1 << p2)));
00257   }
00258 
00259   return CommandCost();
00260 }