Changeset - r17237:0fc8ed4aa201
[Not reviewed]
master
0 6 0
frosch - 13 years ago 2011-02-05 20:41:13
frosch@openttd.org
(svn r21987) -Fix: Make news items, engine previews and AI preview events deal with no longer existing Engine items after resetting the pool.
6 files changed with 41 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/ai/api/ai_event_types.cpp
Show inline comments
 
@@ -16,24 +16,32 @@
 
#include "../../strings_func.h"
 
#include "../../settings_type.h"
 
#include "../../engine_base.h"
 
#include "../../articulated_vehicles.h"
 
#include "table/strings.h"
 

	
 
bool AIEventEnginePreview::IsEngineValid() const
 
{
 
	const Engine *e = ::Engine::GetIfValid(this->engine);
 
	return e != NULL && e->IsEnabled();
 
}
 

	
 
char *AIEventEnginePreview::GetName()
 
{
 
	if (!this->IsEngineValid()) return NULL;
 
	static const int len = 64;
 
	char *engine_name = MallocT<char>(len);
 

	
 
	::SetDParam(0, this->engine);
 
	::GetString(engine_name, STR_ENGINE_NAME, &engine_name[len - 1]);
 
	return engine_name;
 
}
 

	
 
CargoID AIEventEnginePreview::GetCargoType()
 
{
 
	if (!this->IsEngineValid()) return CT_INVALID;
 
	CargoArray cap = ::GetCapacityOfArticulatedParts(this->engine);
 

	
 
	CargoID most_cargo = CT_INVALID;
 
	uint amount = 0;
 
	for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
 
		if (cap[cid] > amount) {
 
@@ -44,12 +52,13 @@ CargoID AIEventEnginePreview::GetCargoTy
 

	
 
	return most_cargo;
 
}
 

	
 
int32 AIEventEnginePreview::GetCapacity()
 
{
 
	if (!this->IsEngineValid()) return -1;
 
	const Engine *e = ::Engine::Get(this->engine);
 
	switch (e->type) {
 
		case VEH_ROAD:
 
		case VEH_TRAIN: {
 
			CargoArray capacities = GetCapacityOfArticulatedParts(this->engine);
 
			for (CargoID c = 0; c < NUM_CARGO; c++) {
 
@@ -66,41 +75,46 @@ int32 AIEventEnginePreview::GetCapacity(
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
int32 AIEventEnginePreview::GetMaxSpeed()
 
{
 
	if (!this->IsEngineValid()) return -1;
 
	const Engine *e = ::Engine::Get(this->engine);
 
	int32 max_speed = e->GetDisplayMaxSpeed(); // km-ish/h
 
	if (e->type == VEH_AIRCRAFT) max_speed /= _settings_game.vehicle.plane_speed;
 
	return max_speed;
 
}
 

	
 
Money AIEventEnginePreview::GetPrice()
 
{
 
	if (!this->IsEngineValid()) return -1;
 
	return ::Engine::Get(this->engine)->GetCost();
 
}
 

	
 
Money AIEventEnginePreview::GetRunningCost()
 
{
 
	if (!this->IsEngineValid()) return -1;
 
	return ::Engine::Get(this->engine)->GetRunningCost();
 
}
 

	
 
int32 AIEventEnginePreview::GetVehicleType()
 
{
 
	if (!this->IsEngineValid()) return AIVehicle::VT_INVALID;
 
	switch (::Engine::Get(this->engine)->type) {
 
		case VEH_ROAD:     return AIVehicle::VT_ROAD;
 
		case VEH_TRAIN:    return AIVehicle::VT_RAIL;
 
		case VEH_SHIP:     return AIVehicle::VT_WATER;
 
		case VEH_AIRCRAFT: return AIVehicle::VT_AIR;
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
bool AIEventEnginePreview::AcceptPreview()
 
{
 
	if (!this->IsEngineValid()) return false;
 
	return AIObject::DoCommand(0, this->engine, 0, CMD_WANT_ENGINE_PREVIEW);
 
}
 

	
 
bool AIEventCompanyAskMerger::AcceptMerger()
 
{
 
	return AIObject::DoCommand(0, this->owner, 0, CMD_BUY_COMPANY);
src/ai/api/ai_event_types.hpp
Show inline comments
 
@@ -293,12 +293,13 @@ public:
 
	 * @return True when the accepting succeeded.
 
	 */
 
	bool AcceptPreview();
 

	
 
private:
 
	EngineID engine;
 
	bool IsEngineValid() const;
 
};
 

	
 
/**
 
 * Event Company New, indicating a new company has been created.
 
 */
 
class AIEventCompanyNew : public AIEvent {
src/engine.cpp
Show inline comments
 
@@ -496,12 +496,13 @@ void SetCachedEngineCounts()
 
		g->num_engines[v->engine_type]++;
 
	}
 
}
 

	
 
void SetupEngines()
 
{
 
	DeleteWindowByClass(WC_ENGINE_PREVIEW);
 
	_engine_pool.CleanPool();
 

	
 
	assert(_engine_mngr.Length() >= _engine_mngr.NUM_DEFAULT_ENGINES);
 
	const EngineIDMapping *end = _engine_mngr.End();
 
	uint index = 0;
 
	for (const EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
 
@@ -856,12 +857,15 @@ void EnginesMonthlyLoop()
 
			/* Age the vehicle */
 
			if ((e->flags & ENGINE_AVAILABLE) && e->age != MAX_DAY) {
 
				e->age++;
 
				CalcEngineReliability(e);
 
			}
 

	
 
			/* Do not introduce invalid engines */
 
			if (!e->IsEnabled()) continue;
 

	
 
			if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) {
 
				/* Introduce it to all companies */
 
				NewVehicleAvailable(e);
 
			} else if (!(e->flags & (ENGINE_AVAILABLE | ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
 
				/* Introduction date has passed.. show introducing dialog to one companies. */
 
				e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
src/news_func.h
Show inline comments
 
@@ -48,11 +48,12 @@ void InitNewsItemStructs();
 

	
 
extern const NewsItem *_statusbar_news_item;
 
extern bool _news_ticker_sound;
 

	
 
extern NewsTypeData _news_type_data[];
 

	
 
void DeleteInvalidEngineNews();
 
void DeleteVehicleNews(VehicleID vid, StringID news);
 
void DeleteStationNews(StationID sid);
 
void DeleteIndustryNews(IndustryID iid);
 

	
 
#endif /* NEWS_FUNC_H */
src/news_gui.cpp
Show inline comments
 
@@ -25,12 +25,13 @@
 
#include "sound_func.h"
 
#include "string_func.h"
 
#include "widgets/dropdown_func.h"
 
#include "statusbar_gui.h"
 
#include "company_manager_face.h"
 
#include "company_func.h"
 
#include "engine_base.h"
 
#include "engine_gui.h"
 
#include "core/geometry_func.hpp"
 

	
 
#include "table/strings.h"
 

	
 
const NewsItem *_statusbar_news_item = NULL;
 
@@ -806,12 +807,29 @@ void DeleteIndustryNews(IndustryID iid)
 
			DeleteNewsItem(ni);
 
		}
 
		ni = next;
 
	}
 
}
 

	
 
/**
 
 * Remove engine announcements for invalid engines.
 
 */
 
void DeleteInvalidEngineNews()
 
{
 
	NewsItem *ni = _oldest_news;
 

	
 
	while (ni != NULL) {
 
		NewsItem *next = ni->next;
 
		if ((ni->reftype1 == NR_ENGINE && (!Engine::IsValidID(ni->ref1) || !Engine::Get(ni->ref1)->IsEnabled())) ||
 
				(ni->reftype2 == NR_ENGINE && (!Engine::IsValidID(ni->ref2) || !Engine::Get(ni->ref2)->IsEnabled()))) {
 
			DeleteNewsItem(ni);
 
		}
 
		ni = next;
 
	}
 
}
 

	
 
static void RemoveOldNewsItems()
 
{
 
	NewsItem *next;
 
	for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != NULL; cur = next) {
 
		next = cur->next;
 
		if (_date - _news_type_data[_news_subtype_data[cur->subtype].type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
src/saveload/afterload.cpp
Show inline comments
 
@@ -48,12 +48,13 @@
 
#include "../subsidy_func.h"
 
#include "../newgrf.h"
 
#include "../engine_func.h"
 
#include "../rail_gui.h"
 
#include "../core/backup_type.hpp"
 
#include "../smallmap_gui.h"
 
#include "../news_func.h"
 

	
 
#include "table/strings.h"
 

	
 
#include "saveload_internal.h"
 

	
 
#include <signal.h>
 
@@ -2601,12 +2602,14 @@ void ReloadNewGRFData()
 
	StartupEngines();
 
	SetCachedEngineCounts();
 
	/* update station graphics */
 
	AfterLoadStations();
 
	/* Check and update house and town values */
 
	UpdateHousesAndTowns();
 
	/* Delete news referring to no longer existing entities */
 
	DeleteInvalidEngineNews();
 
	/* Update livery selection windows */
 
	for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) InvalidateWindowData(WC_COMPANY_COLOUR, i);
 
	/* redraw the whole screen */
 
	MarkWholeScreenDirty();
 
	CheckTrainsLengths();
 
}
0 comments (0 inline, 0 general)