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 "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
00245 if (flags & DC_EXEC) DeleteWindowById(WC_GOAL_QUESTION, p1);
00246 return CommandCost();
00247 }
00248
00249 if (_networking && _local_company == _current_company) {
00250
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 }