00001
00002
00003
00004
00005
00006
00007
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 "story_base.h"
00023 #include "string_func.h"
00024 #include "gui.h"
00025 #include "network/network.h"
00026
00027
00028 GoalID _new_goal_id;
00029
00030 GoalPool _goal_pool("Goal");
00031 INSTANTIATE_POOL_METHODS(Goal)
00032
00033
00044 CommandCost CmdCreateGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00045 {
00046 if (!Goal::CanAllocateItem()) return CMD_ERROR;
00047
00048 GoalType type = (GoalType)GB(p1, 0, 8);
00049 CompanyID company = (CompanyID)GB(p1, 8, 8);
00050
00051 if (_current_company != OWNER_DEITY) return CMD_ERROR;
00052 if (StrEmpty(text)) return CMD_ERROR;
00053 if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
00054
00055 switch (type) {
00056 case GT_NONE:
00057 if (p2 != 0) return CMD_ERROR;
00058 break;
00059
00060 case GT_TILE:
00061 if (!IsValidTile(p2)) return CMD_ERROR;
00062 break;
00063
00064 case GT_INDUSTRY:
00065 if (!Industry::IsValidID(p2)) return CMD_ERROR;
00066 break;
00067
00068 case GT_TOWN:
00069 if (!Town::IsValidID(p2)) return CMD_ERROR;
00070 break;
00071
00072 case GT_COMPANY:
00073 if (!Company::IsValidID(p2)) return CMD_ERROR;
00074 break;
00075
00076 case GT_STORY_PAGE: {
00077 if (!StoryPage::IsValidID(p2)) return CMD_ERROR;
00078 CompanyByte story_company = StoryPage::Get(p2)->company;
00079 if (company == INVALID_COMPANY ? story_company != INVALID_COMPANY : story_company != INVALID_COMPANY && story_company != company) return CMD_ERROR;
00080 break;
00081 }
00082
00083 default: return CMD_ERROR;
00084 }
00085
00086 if (flags & DC_EXEC) {
00087 Goal *g = new Goal();
00088 g->type = type;
00089 g->dst = p2;
00090 g->company = company;
00091 g->text = strdup(text);
00092 g->progress = NULL;
00093 g->completed = false;
00094
00095 InvalidateWindowData(WC_GOALS_LIST, 0);
00096 if (Goal::GetNumItems() == 1) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
00097
00098 _new_goal_id = g->index;
00099 }
00100
00101 return CommandCost();
00102 }
00103
00113 CommandCost CmdRemoveGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00114 {
00115 if (_current_company != OWNER_DEITY) return CMD_ERROR;
00116 if (!Goal::IsValidID(p1)) return CMD_ERROR;
00117
00118 if (flags & DC_EXEC) {
00119 Goal *g = Goal::Get(p1);
00120 delete g;
00121
00122 InvalidateWindowData(WC_GOALS_LIST, 0);
00123 if (Goal::GetNumItems() == 0) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
00124 }
00125
00126 return CommandCost();
00127 }
00128
00138 CommandCost CmdSetGoalText(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00139 {
00140 if (_current_company != OWNER_DEITY) return CMD_ERROR;
00141 if (!Goal::IsValidID(p1)) return CMD_ERROR;
00142 if (StrEmpty(text)) return CMD_ERROR;
00143
00144 if (flags & DC_EXEC) {
00145 Goal *g = Goal::Get(p1);
00146 free(g->text);
00147 g->text = strdup(text);
00148
00149 InvalidateWindowData(WC_GOALS_LIST, 0);
00150 }
00151
00152 return CommandCost();
00153 }
00154
00164 CommandCost CmdSetGoalProgress(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00165 {
00166 if (_current_company != OWNER_DEITY) return CMD_ERROR;
00167 if (!Goal::IsValidID(p1)) return CMD_ERROR;
00168
00169 if (flags & DC_EXEC) {
00170 Goal *g = Goal::Get(p1);
00171 free(g->progress);
00172 if (StrEmpty(text)) {
00173 g->progress = NULL;
00174 } else {
00175 g->progress = strdup(text);
00176 }
00177
00178 InvalidateWindowData(WC_GOALS_LIST, 0);
00179 }
00180
00181 return CommandCost();
00182 }
00183
00193 CommandCost CmdSetGoalCompleted(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00194 {
00195 if (_current_company != OWNER_DEITY) return CMD_ERROR;
00196 if (!Goal::IsValidID(p1)) return CMD_ERROR;
00197
00198 if (flags & DC_EXEC) {
00199 Goal *g = Goal::Get(p1);
00200 g->completed = p2 == 1;
00201
00202 InvalidateWindowData(WC_GOALS_LIST, 0);
00203 }
00204
00205 return CommandCost();
00206 }
00207
00219 CommandCost CmdGoalQuestion(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00220 {
00221 uint16 uniqueid = (GoalType)GB(p1, 0, 16);
00222 CompanyID company = (CompanyID)GB(p1, 16, 8);
00223 byte type = GB(p1, 24, 8);
00224
00225 if (_current_company != OWNER_DEITY) return CMD_ERROR;
00226 if (StrEmpty(text)) return CMD_ERROR;
00227 if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;
00228 if (CountBits(p2) < 1 || CountBits(p2) > 3) return CMD_ERROR;
00229 if (p2 >= (1 << GOAL_QUESTION_BUTTON_COUNT)) return CMD_ERROR;
00230 if (type >= GOAL_QUESTION_TYPE_COUNT) return CMD_ERROR;
00231
00232 if (flags & DC_EXEC) {
00233 if ((company != INVALID_COMPANY && company == _local_company) || (company == INVALID_COMPANY && Company::IsValidID(_local_company))) ShowGoalQuestion(uniqueid, type, p2, text);
00234 }
00235
00236 return CommandCost();
00237 }
00238
00248 CommandCost CmdGoalQuestionAnswer(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00249 {
00250 if (p1 > UINT16_MAX) return CMD_ERROR;
00251 if (p2 >= GOAL_QUESTION_BUTTON_COUNT) return CMD_ERROR;
00252
00253 if (_current_company == OWNER_DEITY) {
00254
00255 if (flags & DC_EXEC) DeleteWindowById(WC_GOAL_QUESTION, p1);
00256 return CommandCost();
00257 }
00258
00259 if (_networking && _local_company == _current_company) {
00260
00261 if (flags & DC_EXEC) DeleteWindowById(WC_GOAL_QUESTION, p1);
00262 if (!_network_server) return CommandCost();
00263 }
00264
00265 if (flags & DC_EXEC) {
00266 Game::NewEvent(new ScriptEventGoalQuestionAnswer(p1, (ScriptCompany::CompanyID)(byte)_current_company, (ScriptGoal::QuestionButton)(1 << p2)));
00267 }
00268
00269 return CommandCost();
00270 }