Changeset - r12639:103f47c555cc
[Not reviewed]
master
0 2 0
smatz - 15 years ago 2009-08-07 20:24:33
smatz@openttd.org
(svn r17104) -Codechange: move code related to subsidy awarding to separate procedure
2 files changed with 39 insertions and 23 deletions:
0 comments (0 inline, 0 general)
src/subsidy.cpp
Show inline comments
 
@@ -14,12 +14,47 @@
 
#include "strings_func.h"
 
#include "window_func.h"
 
#include "subsidy_base.h"
 
#include "subsidy_func.h"
 

	
 
#include "table/strings.h"
 

	
 
/* static */ Subsidy Subsidy::array[MAX_COMPANIES];
 

	
 
/**
 
 * Marks subsidy as awarded, creates news and AI event
 
 * @param from source station
 
 * @param to destination station
 
 * @param company awarded company
 
 */
 
void Subsidy::AwardTo(StationID from, StationID to, CompanyID company)
 
{
 
	assert(!this->IsAwarded());
 

	
 
	this->age = 12;
 
	this->from = from;
 
	this->to = to;
 

	
 
	/* Add a news item */
 
	Pair reftype = SetupSubsidyDecodeParam(this, 0);
 
	InjectDParam(1);
 

	
 
	char *company_name = MallocT<char>(MAX_LENGTH_COMPANY_NAME_BYTES);
 
	SetDParam(0, company);
 
	GetString(company_name, STR_COMPANY_NAME, company_name + MAX_LENGTH_COMPANY_NAME_BYTES - 1);
 

	
 
	SetDParamStr(0, company_name);
 
	AddNewsItem(
 
		STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
 
		NS_SUBSIDIES,
 
		(NewsReferenceType)reftype.a, this->from, (NewsReferenceType)reftype.b, this->to,
 
		company_name
 
	);
 
	AI::BroadcastNewEvent(new AIEventSubsidyAwarded(this->Index()));
 

	
 
	InvalidateWindow(WC_SUBSIDIES_LIST, 0);
 
}
 

	
 
/**
 
 * Allocates one subsidy
 
 * @return pointer to first invalid subsidy, NULL if there is none
 
 */
 
@@ -349,29 +384,7 @@ bool CheckSubsidised(const Station *from
 
			}
 
			if (DistanceMax(xy, to->xy) > 9) continue;
 

	
 
			/* Found a subsidy, change the values to indicate that it's in use */
 
			s->age = 12;
 
			s->from = from->index;
 
			s->to = to->index;
 

	
 
			/* Add a news item */
 
			Pair reftype = SetupSubsidyDecodeParam(s, 0);
 
			InjectDParam(1);
 

	
 
			char *company_name = MallocT<char>(MAX_LENGTH_COMPANY_NAME_BYTES);
 
			SetDParam(0, company);
 
			GetString(company_name, STR_COMPANY_NAME, company_name + MAX_LENGTH_COMPANY_NAME_BYTES - 1);
 

	
 
			SetDParamStr(0, company_name);
 
			AddNewsItem(
 
				STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
 
				NS_SUBSIDIES,
 
				(NewsReferenceType)reftype.a, s->from, (NewsReferenceType)reftype.b, s->to,
 
				company_name
 
			);
 
			AI::BroadcastNewEvent(new AIEventSubsidyAwarded(s->Index()));
 

	
 
			InvalidateWindow(WC_SUBSIDIES_LIST, 0);
 
			s->AwardTo(from->index, to->index, company);
 
			return true;
 
		}
 
	}
src/subsidy_base.h
Show inline comments
 
@@ -7,6 +7,7 @@
 

	
 
#include "cargo_type.h"
 
#include "company_type.h"
 
#include "station_type.h"
 

	
 
typedef uint16 SubsidyID; ///< ID of a subsidy
 

	
 
@@ -26,6 +27,8 @@ struct Subsidy {
 
		return this->age >= 12;
 
	}
 

	
 
	void AwardTo(StationID from, StationID to, CompanyID company);
 

	
 
	/**
 
	 * Determines index of this subsidy
 
	 * @return index (in the Subsidy::array array)
0 comments (0 inline, 0 general)