Changeset - r25759:5af213b2a854
[Not reviewed]
master
0 4 0
rubidium42 - 3 years ago 2021-07-03 06:03:33
rubidium@openttd.org
Codechange: replace InjectDParam/ShiftParameters by setting the right parameter values in the first place
4 files changed with 13 insertions and 36 deletions:
0 comments (0 inline, 0 general)
src/strings.cpp
Show inline comments
 
@@ -81,22 +81,12 @@ int64 StringParameters::GetInt64(WChar t
 
		this->type[this->offset] = type;
 
	}
 
	return this->data[this->offset++];
 
}
 

	
 
/**
 
 * Shift all data in the data array by the given amount to make
 
 * room for some extra parameters.
 
 */
 
void StringParameters::ShiftParameters(uint amount)
 
{
 
	assert(amount <= this->num_param);
 
	MemMoveT(this->data + amount, this->data, this->num_param - amount);
 
}
 

	
 
/**
 
 * Set DParam n to some number that is suitable for string size computations.
 
 * @param n Index of the string parameter.
 
 * @param max_value The biggest value which shall be displayed.
 
 *                  For the result only the number of digits of \a max_value matter.
 
 * @param min_count Minimum number of digits independent of \a max.
 
 * @param size  Font of the number
 
@@ -317,21 +307,12 @@ void SetDParamStr(uint n, const char *st
 
void SetDParamStr(uint n, const std::string &str)
 
{
 
	SetDParamStr(n, str.c_str());
 
}
 

	
 
/**
 
 * Shift the string parameters in the global string parameter array by \a amount positions, making room at the beginning.
 
 * @param amount Number of positions to shift.
 
 */
 
void InjectDParam(uint amount)
 
{
 
	_global_string_params.ShiftParameters(amount);
 
}
 

	
 
/**
 
 * Format a number into a string.
 
 * @param buff      the buffer to write to
 
 * @param number    the number to write down
 
 * @param last      the last element in the buffer
 
 * @param separator the thousands-separator to use
 
 * @param zerofill  minimum number of digits to print for the integer part. The number will be filled with zeros at the front if necessary.
src/strings_func.h
Show inline comments
 
@@ -119,14 +119,12 @@ public:
 
	/** Read an int32 from the argument array. @see GetInt64. */
 
	int32 GetInt32(WChar type = 0)
 
	{
 
		return (int32)this->GetInt64(type);
 
	}
 

	
 
	void ShiftParameters(uint amount);
 

	
 
	/** Get a pointer to the current element in the data array. */
 
	uint64 *GetDataPointer() const
 
	{
 
		return &this->data[this->offset];
 
	}
 

	
 
@@ -176,14 +174,12 @@ std::string GetString(StringID string);
 
char *GetStringWithArgs(char *buffr, StringID string, StringParameters *args, const char *last, uint case_index = 0, bool game_script = false);
 
const char *GetStringPtr(StringID string);
 

	
 
uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
 
uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
 

	
 
void InjectDParam(uint amount);
 

	
 
/**
 
 * Set a string parameter \a v at index \a n in a given array \a s.
 
 * @param s Array of string parameters.
 
 * @param n Index of the string parameter.
 
 * @param v Value of the string parameter.
 
 */
src/subsidy.cpp
Show inline comments
 
@@ -44,14 +44,13 @@ void Subsidy::AwardTo(CompanyID company)
 
	this->remaining = _settings_game.difficulty.subsidy_duration * MONTHS_IN_YEAR;
 

	
 
	SetDParam(0, company);
 
	NewsStringData *company_name = new NewsStringData(GetString(STR_COMPANY_NAME));
 

	
 
	/* Add a news item */
 
	std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(this, SubsidyDecodeParamType::NewsAwarded);
 
	InjectDParam(1);
 
	std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(this, SubsidyDecodeParamType::NewsAwarded, 1);
 

	
 
	SetDParamStr(0, company_name->string);
 
	AddNewsItem(
 
		STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
 
		NT_SUBSIDIES, NF_NORMAL,
 
		reftype.first, this->src, reftype.second, this->dst,
 
@@ -64,56 +63,57 @@ void Subsidy::AwardTo(CompanyID company)
 
}
 

	
 
/**
 
 * Setup the string parameters for printing the subsidy at the screen, and compute the news reference for the subsidy.
 
 * @param s %Subsidy being printed.
 
 * @param mode Type of subsidy news message to decide on parameter format.
 
 * @param parameter_offset The location/index in the String DParams to start decoding the subsidy's parameters. Defaults to 0.
 
 * @return Reference of the subsidy in the news system.
 
 */
 
std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const Subsidy *s, SubsidyDecodeParamType mode)
 
std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const Subsidy *s, SubsidyDecodeParamType mode, uint parameter_offset)
 
{
 
	NewsReferenceType reftype1 = NR_NONE;
 
	NewsReferenceType reftype2 = NR_NONE;
 

	
 
	/* Choose whether to use the singular or plural form of the cargo name based on how we're printing the subsidy */
 
	const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
 
	if (mode == SubsidyDecodeParamType::Gui || mode == SubsidyDecodeParamType::NewsWithdrawn) {
 
		SetDParam(0, cs->name);
 
		SetDParam(parameter_offset, cs->name);
 
	} else {
 
		SetDParam(0, cs->name_single);
 
		SetDParam(parameter_offset, cs->name_single);
 
	}
 

	
 
	switch (s->src_type) {
 
		case ST_INDUSTRY:
 
			reftype1 = NR_INDUSTRY;
 
			SetDParam(1, STR_INDUSTRY_NAME);
 
			SetDParam(parameter_offset + 1, STR_INDUSTRY_NAME);
 
			break;
 
		case ST_TOWN:
 
			reftype1 = NR_TOWN;
 
			SetDParam(1, STR_TOWN_NAME);
 
			SetDParam(parameter_offset + 1, STR_TOWN_NAME);
 
			break;
 
		default: NOT_REACHED();
 
	}
 
	SetDParam(2, s->src);
 
	SetDParam(parameter_offset + 2, s->src);
 

	
 
	switch (s->dst_type) {
 
		case ST_INDUSTRY:
 
			reftype2 = NR_INDUSTRY;
 
			SetDParam(4, STR_INDUSTRY_NAME);
 
			SetDParam(parameter_offset + 4, STR_INDUSTRY_NAME);
 
			break;
 
		case ST_TOWN:
 
			reftype2 = NR_TOWN;
 
			SetDParam(4, STR_TOWN_NAME);
 
			SetDParam(parameter_offset + 4, STR_TOWN_NAME);
 
			break;
 
		default: NOT_REACHED();
 
	}
 
	SetDParam(5, s->dst);
 
	SetDParam(parameter_offset + 5, s->dst);
 

	
 
	/* If the subsidy is being offered or awarded, the news item mentions the subsidy duration. */
 
	if (mode == SubsidyDecodeParamType::NewsOffered || mode == SubsidyDecodeParamType::NewsAwarded) {
 
		SetDParam(7, _settings_game.difficulty.subsidy_duration);
 
		SetDParam(parameter_offset + 7, _settings_game.difficulty.subsidy_duration);
 
	}
 

	
 
	return std::pair<NewsReferenceType, NewsReferenceType>(reftype1, reftype2);
 
}
 

	
 
/**
src/subsidy_func.h
Show inline comments
 
@@ -14,13 +14,13 @@
 
#include "station_type.h"
 
#include "company_type.h"
 
#include "cargo_type.h"
 
#include "news_type.h"
 
#include "subsidy_base.h"
 

	
 
std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const struct Subsidy *s, SubsidyDecodeParamType mode);
 
std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const struct Subsidy *s, SubsidyDecodeParamType mode, uint parameter_offset = 0);
 
void DeleteSubsidyWith(SourceType type, SourceID index);
 
bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st);
 
void RebuildSubsidisedSourceAndDestinationCache();
 
void DeleteSubsidy(struct Subsidy *s);
 

	
 
#endif /* SUBSIDY_FUNC_H */
0 comments (0 inline, 0 general)