Changeset - r14174:6999d5f69ce1
[Not reviewed]
master
0 2 0
rubidium - 15 years ago 2010-01-05 17:11:56
rubidium@openttd.org
(svn r18732) -Codechange: move the company related commands of misc_cmd.cpp to company_cmd.cpp
2 files changed with 202 insertions and 203 deletions:
0 comments (0 inline, 0 general)
src/company_cmd.cpp
Show inline comments
 
@@ -29,12 +29,14 @@
 
#include "sound_func.h"
 
#include "autoreplace_func.h"
 
#include "autoreplace_gui.h"
 
#include "rail.h"
 
#include "core/pool_func.hpp"
 
#include "settings_func.h"
 
#include "vehicle_base.h"
 
#include "vehicle_func.h"
 

	
 
#include "table/strings.h"
 

	
 
CompanyByte _local_company;
 
CompanyByte _current_company;
 
/* NOSAVE: can be determined from company structs */
 
@@ -852,6 +854,205 @@ CommandCost CmdCompanyCtrl(TileIndex til
 

	
 
		default: return CMD_ERROR;
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
/** Change the company manager's face.
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 unused
 
 * @param p2 face bitmasked
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSetCompanyManagerFace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	CompanyManagerFace cmf = (CompanyManagerFace)p2;
 

	
 
	if (!IsValidCompanyManagerFace(cmf)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Company::Get(_current_company)->face = cmf;
 
		MarkWholeScreenDirty();
 
	}
 
	return CommandCost();
 
}
 

	
 
/** Change the company's company-colour
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 bitstuffed:
 
 * p1 bits 0-7 scheme to set
 
 * p1 bits 8-9 set in use state or first/second colour
 
 * @param p2 new colour for vehicles, property, etc.
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	if (p2 >= 16) return CMD_ERROR; // max 16 colours
 

	
 
	Colours colour = (Colours)p2;
 

	
 
	LiveryScheme scheme = (LiveryScheme)GB(p1, 0, 8);
 
	byte state = GB(p1, 8, 2);
 

	
 
	if (scheme >= LS_END || state >= 3) return CMD_ERROR;
 

	
 
	Company *c = Company::Get(_current_company);
 

	
 
	/* Ensure no two companies have the same primary colour */
 
	if (scheme == LS_DEFAULT && state == 0) {
 
		const Company *cc;
 
		FOR_ALL_COMPANIES(cc) {
 
			if (cc != c && cc->colour == colour) return CMD_ERROR;
 
		}
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		switch (state) {
 
			case 0:
 
				c->livery[scheme].colour1 = colour;
 

	
 
				/* If setting the first colour of the default scheme, adjust the
 
				 * original and cached company colours too. */
 
				if (scheme == LS_DEFAULT) {
 
					_company_colours[_current_company] = colour;
 
					c->colour = colour;
 
				}
 
				break;
 

	
 
			case 1:
 
				c->livery[scheme].colour2 = colour;
 
				break;
 

	
 
			case 2:
 
				c->livery[scheme].in_use = colour != 0;
 

	
 
				/* Now handle setting the default scheme's in_use flag.
 
				 * This is different to the other schemes, as it signifies if any
 
				 * scheme is active at all. If this flag is not set, then no
 
				 * processing of vehicle types occurs at all, and only the default
 
				 * colours will be used. */
 

	
 
				/* If enabling a scheme, set the default scheme to be in use too */
 
				if (colour != 0) {
 
					c->livery[LS_DEFAULT].in_use = true;
 
					break;
 
				}
 

	
 
				/* Else loop through all schemes to see if any are left enabled.
 
				 * If not, disable the default scheme too. */
 
				c->livery[LS_DEFAULT].in_use = false;
 
				for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
 
					if (c->livery[scheme].in_use) {
 
						c->livery[LS_DEFAULT].in_use = true;
 
						break;
 
					}
 
				}
 
				break;
 

	
 
			default:
 
				break;
 
		}
 
		ResetVehicleColourMap();
 
		MarkWholeScreenDirty();
 

	
 
		/* Company colour data is indirectly cached. */
 
		Vehicle *v;
 
		FOR_ALL_VEHICLES(v) {
 
			if (v->owner == _current_company) v->InvalidateNewGRFCache();
 
		}
 
	}
 
	return CommandCost();
 
}
 

	
 
static bool IsUniqueCompanyName(const char *name)
 
{
 
	const Company *c;
 

	
 
	FOR_ALL_COMPANIES(c) {
 
		if (c->name != NULL && strcmp(c->name, name) == 0) return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
/** Change the name of the company.
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 unused
 
 * @param p2 unused
 
 * @param text the new name or an empty string when resetting to the default
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	bool reset = StrEmpty(text);
 

	
 
	if (!reset) {
 
		if (strlen(text) >= MAX_LENGTH_COMPANY_NAME_BYTES) return CMD_ERROR;
 
		if (!IsUniqueCompanyName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Company *c = Company::Get(_current_company);
 
		free(c->name);
 
		c->name = reset ? NULL : strdup(text);
 
		MarkWholeScreenDirty();
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
static bool IsUniquePresidentName(const char *name)
 
{
 
	const Company *c;
 

	
 
	FOR_ALL_COMPANIES(c) {
 
		if (c->president_name != NULL && strcmp(c->president_name, name) == 0) return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
/** Change the name of the president.
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 unused
 
 * @param p2 unused
 
 * @param text the new name or an empty string when resetting to the default
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	bool reset = StrEmpty(text);
 

	
 
	if (!reset) {
 
		if (strlen(text) >= MAX_LENGTH_PRESIDENT_NAME_BYTES) return CMD_ERROR;
 
		if (!IsUniquePresidentName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Company *c = Company::Get(_current_company);
 
		free(c->president_name);
 

	
 
		if (reset) {
 
			c->president_name = NULL;
 
		} else {
 
			c->president_name = strdup(text);
 

	
 
			if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) {
 
				char buf[80];
 

	
 
				snprintf(buf, lengthof(buf), "%s Transport", text);
 
				DoCommand(0, 0, 0, DC_EXEC, CMD_RENAME_COMPANY, buf);
 
			}
 
		}
 

	
 
		MarkWholeScreenDirty();
 
	}
 

	
 
	return CommandCost();
 
}
src/misc_cmd.cpp
Show inline comments
 
@@ -13,133 +13,20 @@
 
#include "command_func.h"
 
#include "economy_func.h"
 
#include "window_func.h"
 
#include "textbuf_gui.h"
 
#include "network/network.h"
 
#include "network/network_func.h"
 
#include "company_manager_face.h"
 
#include "strings_func.h"
 
#include "gfx_func.h"
 
#include "functions.h"
 
#include "vehicle_func.h"
 
#include "company_func.h"
 
#include "company_gui.h"
 
#include "vehicle_base.h"
 
#include "company_base.h"
 

	
 
#include "table/strings.h"
 

	
 
/** Change the company manager's face.
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 unused
 
 * @param p2 face bitmasked
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSetCompanyManagerFace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	CompanyManagerFace cmf = (CompanyManagerFace)p2;
 

	
 
	if (!IsValidCompanyManagerFace(cmf)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Company::Get(_current_company)->face = cmf;
 
		MarkWholeScreenDirty();
 
	}
 
	return CommandCost();
 
}
 

	
 
/** Change the company's company-colour
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 bitstuffed:
 
 * p1 bits 0-7 scheme to set
 
 * p1 bits 8-9 set in use state or first/second colour
 
 * @param p2 new colour for vehicles, property, etc.
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	if (p2 >= 16) return CMD_ERROR; // max 16 colours
 

	
 
	Colours colour = (Colours)p2;
 

	
 
	LiveryScheme scheme = (LiveryScheme)GB(p1, 0, 8);
 
	byte state = GB(p1, 8, 2);
 

	
 
	if (scheme >= LS_END || state >= 3) return CMD_ERROR;
 

	
 
	Company *c = Company::Get(_current_company);
 

	
 
	/* Ensure no two companies have the same primary colour */
 
	if (scheme == LS_DEFAULT && state == 0) {
 
		const Company *cc;
 
		FOR_ALL_COMPANIES(cc) {
 
			if (cc != c && cc->colour == colour) return CMD_ERROR;
 
		}
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		switch (state) {
 
			case 0:
 
				c->livery[scheme].colour1 = colour;
 

	
 
				/* If setting the first colour of the default scheme, adjust the
 
				 * original and cached company colours too. */
 
				if (scheme == LS_DEFAULT) {
 
					_company_colours[_current_company] = colour;
 
					c->colour = colour;
 
				}
 
				break;
 

	
 
			case 1:
 
				c->livery[scheme].colour2 = colour;
 
				break;
 

	
 
			case 2:
 
				c->livery[scheme].in_use = colour != 0;
 

	
 
				/* Now handle setting the default scheme's in_use flag.
 
				 * This is different to the other schemes, as it signifies if any
 
				 * scheme is active at all. If this flag is not set, then no
 
				 * processing of vehicle types occurs at all, and only the default
 
				 * colours will be used. */
 

	
 
				/* If enabling a scheme, set the default scheme to be in use too */
 
				if (colour != 0) {
 
					c->livery[LS_DEFAULT].in_use = true;
 
					break;
 
				}
 

	
 
				/* Else loop through all schemes to see if any are left enabled.
 
				 * If not, disable the default scheme too. */
 
				c->livery[LS_DEFAULT].in_use = false;
 
				for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
 
					if (c->livery[scheme].in_use) {
 
						c->livery[LS_DEFAULT].in_use = true;
 
						break;
 
					}
 
				}
 
				break;
 

	
 
			default:
 
				break;
 
		}
 
		ResetVehicleColourMap();
 
		MarkWholeScreenDirty();
 

	
 
		/* Company colour data is indirectly cached. */
 
		Vehicle *v;
 
		FOR_ALL_VEHICLES(v) {
 
			if (v->owner == _current_company) v->InvalidateNewGRFCache();
 
		}
 
	}
 
	return CommandCost();
 
}
 

	
 
/** Increase the loan of your company.
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 amount to increase the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
 
 * @param p2 when 0: loans LOAN_INTERVAL
 
 *           when 1: loans the maximum loan permitting money (press CTRL),
 
@@ -225,101 +112,12 @@ CommandCost CmdDecreaseLoan(TileIndex ti
 
		c->current_loan -= loan;
 
		InvalidateCompanyWindows(c);
 
	}
 
	return CommandCost();
 
}
 

	
 
static bool IsUniqueCompanyName(const char *name)
 
{
 
	const Company *c;
 

	
 
	FOR_ALL_COMPANIES(c) {
 
		if (c->name != NULL && strcmp(c->name, name) == 0) return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
/** Change the name of the company.
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 unused
 
 * @param p2 unused
 
 * @param text the new name or an empty string when resetting to the default
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdRenameCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	bool reset = StrEmpty(text);
 

	
 
	if (!reset) {
 
		if (strlen(text) >= MAX_LENGTH_COMPANY_NAME_BYTES) return CMD_ERROR;
 
		if (!IsUniqueCompanyName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Company *c = Company::Get(_current_company);
 
		free(c->name);
 
		c->name = reset ? NULL : strdup(text);
 
		MarkWholeScreenDirty();
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
static bool IsUniquePresidentName(const char *name)
 
{
 
	const Company *c;
 

	
 
	FOR_ALL_COMPANIES(c) {
 
		if (c->president_name != NULL && strcmp(c->president_name, name) == 0) return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
/** Change the name of the president.
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 unused
 
 * @param p2 unused
 
 * @param text the new name or an empty string when resetting to the default
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	bool reset = StrEmpty(text);
 

	
 
	if (!reset) {
 
		if (strlen(text) >= MAX_LENGTH_PRESIDENT_NAME_BYTES) return CMD_ERROR;
 
		if (!IsUniquePresidentName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Company *c = Company::Get(_current_company);
 
		free(c->president_name);
 

	
 
		if (reset) {
 
			c->president_name = NULL;
 
		} else {
 
			c->president_name = strdup(text);
 

	
 
			if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) {
 
				char buf[80];
 

	
 
				snprintf(buf, lengthof(buf), "%s Transport", text);
 
				DoCommand(0, 0, 0, DC_EXEC, CMD_RENAME_COMPANY, buf);
 
			}
 
		}
 

	
 
		MarkWholeScreenDirty();
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
/**
 
 * In case of an unsafe unpause, we want the
 
 * user to confirm that it might crash.
 
 * @param w         unused
 
 * @param confirmed whether the user confirms his/her action
 
 */
0 comments (0 inline, 0 general)