# misc_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: misc_cmd.cpp 15434 2009-02-09 21:20:05Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "command_func.h"
    00008 #include "economy_func.h"
    00009 #include "window_func.h"
    00010 #include "textbuf_gui.h"
    00011 #include "network/network.h"
    00012 #include "company_manager_face.h"
    00013 #include "strings_func.h"
    00014 #include "gfx_func.h"
    00015 #include "functions.h"
    00016 #include "vehicle_func.h"
    00017 #include "string_func.h"
    00018 #include "company_func.h"
    00019 #include "company_base.h"
    00020 #include "company_gui.h"
    00021 #include "settings_type.h"
    00022
    00023 #include "table/strings.h"
    00024
    00031 CommandCost CmdSetCompanyManagerFace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00032 {
    00033   CompanyManagerFace cmf = (CompanyManagerFace)p2;
    00034
    00035   if (!IsValidCompanyManagerFace(cmf)) return CMD_ERROR;
    00036
    00037   if (flags & DC_EXEC) {
    00038     GetCompany(_current_company)->face = cmf;
    00039     MarkWholeScreenDirty();
    00040   }
    00041   return CommandCost();
    00042 }
    00043
    00052 CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00053 {
    00054   if (p2 >= 16) return CMD_ERROR; // max 16 colours
    00055
    00056   Colours colour = (Colours)p2;
    00057
    00058   LiveryScheme scheme = (LiveryScheme)GB(p1, 0, 8);
    00059   byte state = GB(p1, 8, 2);
    00060
    00061   if (scheme >= LS_END || state >= 3) return CMD_ERROR;
    00062
    00063   Company *c = GetCompany(_current_company);
    00064
    00065   /* Ensure no two companies have the same primary colour */
    00066   if (scheme == LS_DEFAULT && state == 0) {
    00067     const Company *cc;
    00068     FOR_ALL_COMPANIES(cc) {
    00069       if (cc != c && cc->colour == colour) return CMD_ERROR;
    00070     }
    00071   }
    00072
    00073   if (flags & DC_EXEC) {
    00074     switch (state) {
    00075       case 0:
    00076         c->livery[scheme].colour1 = colour;
    00077
    00078         /* If setting the first colour of the default scheme, adjust the
    00079          * original and cached company colours too. */
    00080         if (scheme == LS_DEFAULT) {
    00081           _company_colours[_current_company] = colour;
    00082           c->colour = colour;
    00083         }
    00084         break;
    00085
    00086       case 1:
    00087         c->livery[scheme].colour2 = colour;
    00088         break;
    00089
    00090       case 2:
    00091         c->livery[scheme].in_use = colour != 0;
    00092
    00093         /* Now handle setting the default scheme's in_use flag.
    00094          * This is different to the other schemes, as it signifies if any
    00095          * scheme is active at all. If this flag is not set, then no
    00096          * processing of vehicle types occurs at all, and only the default
    00097          * colours will be used. */
    00098
    00099         /* If enabling a scheme, set the default scheme to be in use too */
    00100         if (colour != 0) {
    00101           c->livery[LS_DEFAULT].in_use = true;
    00102           break;
    00103         }
    00104
    00105         /* Else loop through all schemes to see if any are left enabled.
    00106          * If not, disable the default scheme too. */
    00107         c->livery[LS_DEFAULT].in_use = false;
    00108         for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
    00109           if (c->livery[scheme].in_use) {
    00110             c->livery[LS_DEFAULT].in_use = true;
    00111             break;
    00112           }
    00113         }
    00114         break;
    00115
    00116       default:
    00117         break;
    00118     }
    00119     ResetVehicleColourMap();
    00120     MarkWholeScreenDirty();
    00121   }
    00122   return CommandCost();
    00123 }
    00124
    00133 CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00134 {
    00135   Company *c = GetCompany(_current_company);
    00136
    00137   if (c->current_loan >= _economy.max_loan) {
    00138     SetDParam(0, _economy.max_loan);
    00139     return_cmd_error(STR_702B_MAXIMUM_PERMITTED_LOAN);
    00140   }
    00141
    00142   Money loan;
    00143   switch (p2) {
    00144     default: return CMD_ERROR; // Invalid method
    00145     case 0: // Take some extra loan
    00146       loan = LOAN_INTERVAL;
    00147       break;
    00148     case 1: // Take a loan as big as possible
    00149       loan = _economy.max_loan - c->current_loan;
    00150       break;
    00151     case 2: // Take the given amount of loan
    00152       if ((((int32)p1 < LOAN_INTERVAL) || c->current_loan + (int32)p1 > _economy.max_loan || (p1 % LOAN_INTERVAL) != 0)) return CMD_ERROR;
    00153       loan = p1;
    00154       break;
    00155   }
    00156
    00157   /* Overflow protection */
    00158   if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
    00159
    00160   if (flags & DC_EXEC) {
    00161     c->money        += loan;
    00162     c->current_loan += loan;
    00163     InvalidateCompanyWindows(c);
    00164   }
    00165
    00166   return CommandCost(EXPENSES_OTHER);
    00167 }
    00168
    00177 CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00178 {
    00179   Company *c = GetCompany(_current_company);
    00180
    00181   if (c->current_loan == 0) return_cmd_error(STR_702D_LOAN_ALREADY_REPAYED);
    00182
    00183   Money loan;
    00184   switch (p2) {
    00185     default: return CMD_ERROR; // Invalid method
    00186     case 0: // Pay back one step
    00187       loan = min(c->current_loan, (Money)LOAN_INTERVAL);
    00188       break;
    00189     case 1: // Pay back as much as possible
    00190       loan = max(min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
    00191       loan -= loan % LOAN_INTERVAL;
    00192       break;
    00193     case 2: // Repay the given amount of loan
    00194       if ((p1 % LOAN_INTERVAL != 0) || ((int32)p1 < LOAN_INTERVAL)) return CMD_ERROR; // Invalid amount to loan
    00195       loan = p1;
    00196       break;
    00197   }
    00198
    00199   if (c->money < loan) {
    00200     SetDParam(0, loan);
    00201     return_cmd_error(STR_702E_REQUIRED);
    00202   }
    00203
    00204   if (flags & DC_EXEC) {
    00205     c->money        -= loan;
    00206     c->current_loan -= loan;
    00207     InvalidateCompanyWindows(c);
    00208   }
    00209   return CommandCost();
    00210 }
    00211
    00212 static bool IsUniqueCompanyName(const char *name)
    00213 {
    00214   const Company *c;
    00215
    00216   FOR_ALL_COMPANIES(c) {
    00217     if (c->name != NULL && strcmp(c->name, name) == 0) return false;
    00218   }
    00219
    00220   return true;
    00221 }
    00222
    00229 CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00230 {
    00231   bool reset = StrEmpty(text);
    00232
    00233   if (!reset) {
    00234     if (strlen(text) >= MAX_LENGTH_COMPANY_NAME_BYTES) return CMD_ERROR;
    00235     if (!IsUniqueCompanyName(text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
    00236   }
    00237
    00238   if (flags & DC_EXEC) {
    00239     Company *c = GetCompany(_current_company);
    00240     free(c->name);
    00241     c->name = reset ? NULL : strdup(text);
    00242     MarkWholeScreenDirty();
    00243   }
    00244
    00245   return CommandCost();
    00246 }
    00247
    00248 static bool IsUniquePresidentName(const char *name)
    00249 {
    00250   const Company *c;
    00251
    00252   FOR_ALL_COMPANIES(c) {
    00253     if (c->president_name != NULL && strcmp(c->president_name, name) == 0) return false;
    00254   }
    00255
    00256   return true;
    00257 }
    00258
    00265 CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00266 {
    00267   bool reset = StrEmpty(text);
    00268
    00269   if (!reset) {
    00270     if (strlen(text) >= MAX_LENGTH_PRESIDENT_NAME_BYTES) return CMD_ERROR;
    00271     if (!IsUniquePresidentName(text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
    00272   }
    00273
    00274   if (flags & DC_EXEC) {
    00275     Company *c = GetCompany(_current_company);
    00276     free(c->president_name);
    00277
    00278     if (reset) {
    00279       c->president_name = NULL;
    00280     } else {
    00281       c->president_name = strdup(text);
    00282
    00283       if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) {
    00284         char buf[80];
    00285
    00286         snprintf(buf, lengthof(buf), "%s Transport", text);
    00287         DoCommand(0, 0, 0, DC_EXEC, CMD_RENAME_COMPANY, buf);
    00288       }
    00289     }
    00290
    00291     MarkWholeScreenDirty();
    00292   }
    00293
    00294   return CommandCost();
    00295 }
    00296
    00303 static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
    00304 {
    00305   DoCommandP(0, confirmed ? 0 : 1, 0, CMD_PAUSE);
    00306 }
    00307
    00317 CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00318 {
    00319   if (flags & DC_EXEC) {
    00320     _pause_game += (p1 == 0) ? -1 : 1;
    00321
    00322     switch (_pause_game) {
    00323       case -4:
    00324       case -1:
    00325         _pause_game = 0;
    00326         break;
    00327       case -3:
    00328         ShowQuery(
    00329           STR_NEWGRF_UNPAUSE_WARNING_TITLE,
    00330           STR_NEWGRF_UNPAUSE_WARNING,
    00331           NULL,
    00332           AskUnsafeUnpauseCallback
    00333         );
    00334         break;
    00335
    00336       default: break;
    00337     }
    00338
    00339     InvalidateWindow(WC_STATUS_BAR, 0);
    00340     InvalidateWindow(WC_MAIN_TOOLBAR, 0);
    00341   }
    00342   return CommandCost();
    00343 }
    00344
    00353 CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00354 {
    00355 #ifndef _DEBUG
    00356   if (_networking) return CMD_ERROR;
    00357 #endif
    00358   return CommandCost(EXPENSES_OTHER, -(int32)p1);
    00359 }
    00360
    00370 CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00371 {
    00372   if (!_settings_game.economy.give_money) return CMD_ERROR;
    00373
    00374   const Company *c = GetCompany(_current_company);
    00375   CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL));
    00376
    00377   /* You can only transfer funds that is in excess of your loan */
    00378   if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() <= 0) return CMD_ERROR;
    00379   if (!_networking || !IsValidCompanyID((CompanyID)p2)) return CMD_ERROR;
    00380
    00381   if (flags & DC_EXEC) {
    00382     /* Add money to company */
    00383     CompanyID old_company = _current_company;
    00384     _current_company = (CompanyID)p2;
    00385     SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost()));
    00386     _current_company = old_company;
    00387   }
    00388
    00389   /* Subtract money from local-company */
    00390   return amount;
    00391 }