00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "command_func.h"
00014 #include "economy_func.h"
00015 #include "window_func.h"
00016 #include "textbuf_gui.h"
00017 #include "network/network.h"
00018 #include "network/network_func.h"
00019 #include "company_manager_face.h"
00020 #include "strings_func.h"
00021 #include "gfx_func.h"
00022 #include "functions.h"
00023 #include "vehicle_func.h"
00024 #include "company_func.h"
00025 #include "company_gui.h"
00026 #include "vehicle_base.h"
00027
00028 #include "table/strings.h"
00029
00038 CommandCost CmdSetCompanyManagerFace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00039 {
00040 CompanyManagerFace cmf = (CompanyManagerFace)p2;
00041
00042 if (!IsValidCompanyManagerFace(cmf)) return CMD_ERROR;
00043
00044 if (flags & DC_EXEC) {
00045 Company::Get(_current_company)->face = cmf;
00046 MarkWholeScreenDirty();
00047 }
00048 return CommandCost();
00049 }
00050
00061 CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00062 {
00063 if (p2 >= 16) return CMD_ERROR;
00064
00065 Colours colour = (Colours)p2;
00066
00067 LiveryScheme scheme = (LiveryScheme)GB(p1, 0, 8);
00068 byte state = GB(p1, 8, 2);
00069
00070 if (scheme >= LS_END || state >= 3) return CMD_ERROR;
00071
00072 Company *c = Company::Get(_current_company);
00073
00074
00075 if (scheme == LS_DEFAULT && state == 0) {
00076 const Company *cc;
00077 FOR_ALL_COMPANIES(cc) {
00078 if (cc != c && cc->colour == colour) return CMD_ERROR;
00079 }
00080 }
00081
00082 if (flags & DC_EXEC) {
00083 switch (state) {
00084 case 0:
00085 c->livery[scheme].colour1 = colour;
00086
00087
00088
00089 if (scheme == LS_DEFAULT) {
00090 _company_colours[_current_company] = colour;
00091 c->colour = colour;
00092 }
00093 break;
00094
00095 case 1:
00096 c->livery[scheme].colour2 = colour;
00097 break;
00098
00099 case 2:
00100 c->livery[scheme].in_use = colour != 0;
00101
00102
00103
00104
00105
00106
00107
00108
00109 if (colour != 0) {
00110 c->livery[LS_DEFAULT].in_use = true;
00111 break;
00112 }
00113
00114
00115
00116 c->livery[LS_DEFAULT].in_use = false;
00117 for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
00118 if (c->livery[scheme].in_use) {
00119 c->livery[LS_DEFAULT].in_use = true;
00120 break;
00121 }
00122 }
00123 break;
00124
00125 default:
00126 break;
00127 }
00128 ResetVehicleColourMap();
00129 MarkWholeScreenDirty();
00130
00131
00132 Vehicle *v;
00133 FOR_ALL_VEHICLES(v) {
00134 if (v->owner == _current_company) v->InvalidateNewGRFCache();
00135 }
00136 }
00137 return CommandCost();
00138 }
00139
00150 CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00151 {
00152 Company *c = Company::Get(_current_company);
00153
00154 if (c->current_loan >= _economy.max_loan) {
00155 SetDParam(0, _economy.max_loan);
00156 return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
00157 }
00158
00159 Money loan;
00160 switch (p2) {
00161 default: return CMD_ERROR;
00162 case 0:
00163 loan = LOAN_INTERVAL;
00164 break;
00165 case 1:
00166 loan = _economy.max_loan - c->current_loan;
00167 break;
00168 case 2:
00169 if ((((int32)p1 < LOAN_INTERVAL) || c->current_loan + (int32)p1 > _economy.max_loan || (p1 % LOAN_INTERVAL) != 0)) return CMD_ERROR;
00170 loan = p1;
00171 break;
00172 }
00173
00174
00175 if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
00176
00177 if (flags & DC_EXEC) {
00178 c->money += loan;
00179 c->current_loan += loan;
00180 InvalidateCompanyWindows(c);
00181 }
00182
00183 return CommandCost(EXPENSES_OTHER);
00184 }
00185
00196 CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00197 {
00198 Company *c = Company::Get(_current_company);
00199
00200 if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
00201
00202 Money loan;
00203 switch (p2) {
00204 default: return CMD_ERROR;
00205 case 0:
00206 loan = min(c->current_loan, (Money)LOAN_INTERVAL);
00207 break;
00208 case 1:
00209 loan = max(min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
00210 loan -= loan % LOAN_INTERVAL;
00211 break;
00212 case 2:
00213 if ((p1 % LOAN_INTERVAL != 0) || ((int32)p1 < LOAN_INTERVAL)) return CMD_ERROR;
00214 loan = p1;
00215 break;
00216 }
00217
00218 if (c->money < loan) {
00219 SetDParam(0, loan);
00220 return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
00221 }
00222
00223 if (flags & DC_EXEC) {
00224 c->money -= loan;
00225 c->current_loan -= loan;
00226 InvalidateCompanyWindows(c);
00227 }
00228 return CommandCost();
00229 }
00230
00231 static bool IsUniqueCompanyName(const char *name)
00232 {
00233 const Company *c;
00234
00235 FOR_ALL_COMPANIES(c) {
00236 if (c->name != NULL && strcmp(c->name, name) == 0) return false;
00237 }
00238
00239 return true;
00240 }
00241
00250 CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00251 {
00252 bool reset = StrEmpty(text);
00253
00254 if (!reset) {
00255 if (strlen(text) >= MAX_LENGTH_COMPANY_NAME_BYTES) return CMD_ERROR;
00256 if (!IsUniqueCompanyName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
00257 }
00258
00259 if (flags & DC_EXEC) {
00260 Company *c = Company::Get(_current_company);
00261 free(c->name);
00262 c->name = reset ? NULL : strdup(text);
00263 MarkWholeScreenDirty();
00264 }
00265
00266 return CommandCost();
00267 }
00268
00269 static bool IsUniquePresidentName(const char *name)
00270 {
00271 const Company *c;
00272
00273 FOR_ALL_COMPANIES(c) {
00274 if (c->president_name != NULL && strcmp(c->president_name, name) == 0) return false;
00275 }
00276
00277 return true;
00278 }
00279
00288 CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00289 {
00290 bool reset = StrEmpty(text);
00291
00292 if (!reset) {
00293 if (strlen(text) >= MAX_LENGTH_PRESIDENT_NAME_BYTES) return CMD_ERROR;
00294 if (!IsUniquePresidentName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
00295 }
00296
00297 if (flags & DC_EXEC) {
00298 Company *c = Company::Get(_current_company);
00299 free(c->president_name);
00300
00301 if (reset) {
00302 c->president_name = NULL;
00303 } else {
00304 c->president_name = strdup(text);
00305
00306 if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) {
00307 char buf[80];
00308
00309 snprintf(buf, lengthof(buf), "%s Transport", text);
00310 DoCommand(0, 0, 0, DC_EXEC, CMD_RENAME_COMPANY, buf);
00311 }
00312 }
00313
00314 MarkWholeScreenDirty();
00315 }
00316
00317 return CommandCost();
00318 }
00319
00326 static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
00327 {
00328 DoCommandP(0, PM_PAUSED_ERROR, confirmed ? 0 : 1, CMD_PAUSE);
00329 }
00330
00343 CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00344 {
00345 switch (p1) {
00346 case PM_PAUSED_SAVELOAD:
00347 case PM_PAUSED_ERROR:
00348 case PM_PAUSED_NORMAL:
00349 break;
00350
00351 #ifdef ENABLE_NETWORK
00352 case PM_PAUSED_JOIN:
00353 case PM_PAUSED_ACTIVE_CLIENTS:
00354 if (!_networking) return CMD_ERROR;
00355 break;
00356 #endif
00357
00358 default: return CMD_ERROR;
00359 }
00360 if (flags & DC_EXEC) {
00361 if (p1 == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
00362 ShowQuery(
00363 STR_NEWGRF_UNPAUSE_WARNING_TITLE,
00364 STR_NEWGRF_UNPAUSE_WARNING,
00365 NULL,
00366 AskUnsafeUnpauseCallback
00367 );
00368 } else {
00369 #ifdef ENABLE_NETWORK
00370 PauseMode prev_mode = _pause_mode;
00371 #endif
00372
00373 if (p2 == 0) {
00374 _pause_mode = _pause_mode & ~p1;
00375 } else {
00376 _pause_mode = _pause_mode | p1;
00377 }
00378
00379 #ifdef ENABLE_NETWORK
00380 NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
00381 #endif
00382 }
00383
00384 SetWindowDirty(WC_STATUS_BAR, 0);
00385 SetWindowDirty(WC_MAIN_TOOLBAR, 0);
00386 }
00387 return CommandCost();
00388 }
00389
00400 CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00401 {
00402 #ifndef _DEBUG
00403 if (_networking) return CMD_ERROR;
00404 #endif
00405 return CommandCost(EXPENSES_OTHER, -(int32)p1);
00406 }
00407
00419 CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00420 {
00421 if (!_settings_game.economy.give_money) return CMD_ERROR;
00422
00423 const Company *c = Company::Get(_current_company);
00424 CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL));
00425
00426
00427 if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() <= 0) return CMD_ERROR;
00428 if (!_networking || !Company::IsValidID((CompanyID)p2)) return CMD_ERROR;
00429
00430 if (flags & DC_EXEC) {
00431
00432 CompanyID old_company = _current_company;
00433 _current_company = (CompanyID)p2;
00434 SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost()));
00435 _current_company = old_company;
00436 }
00437
00438
00439 return amount;
00440 }